mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-31 09:06:17 +00:00
Merge pull request #825 from dogmatic69/sanitize-simplify-code
Simplify the code for sanitize class
This commit is contained in:
commit
5d8ca79b00
1 changed files with 51 additions and 51 deletions
|
@ -46,14 +46,15 @@ class Sanitize {
|
|||
}
|
||||
}
|
||||
|
||||
if (is_array($string)) {
|
||||
$cleaned = array();
|
||||
foreach ($string as $key => $clean) {
|
||||
$cleaned[$key] = preg_replace("/[^{$allow}a-zA-Z0-9]/", '', $clean);
|
||||
}
|
||||
} else {
|
||||
$cleaned = preg_replace("/[^{$allow}a-zA-Z0-9]/", '', $string);
|
||||
if (!is_array($string)) {
|
||||
return preg_replace("/[^{$allow}a-zA-Z0-9]/", '', $string);
|
||||
}
|
||||
|
||||
$cleaned = array();
|
||||
foreach ($string as $key => $clean) {
|
||||
$cleaned[$key] = preg_replace("/[^{$allow}a-zA-Z0-9]/", '', $clean);
|
||||
}
|
||||
|
||||
return $cleaned;
|
||||
}
|
||||
|
||||
|
@ -70,14 +71,12 @@ class Sanitize {
|
|||
return $string;
|
||||
}
|
||||
$string = $db->value($string, 'string');
|
||||
if ($string[0] === 'N') {
|
||||
$string = substr($string, 2);
|
||||
} else {
|
||||
$string = substr($string, 1);
|
||||
$start = 1;
|
||||
if ($string{0} === 'N') {
|
||||
$start = 2;
|
||||
}
|
||||
|
||||
$string = substr($string, 0, -1);
|
||||
return $string;
|
||||
return substr(substr($string, 1), 0, -1);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -128,8 +127,7 @@ class Sanitize {
|
|||
* @return string whitespace sanitized string
|
||||
*/
|
||||
public static function stripWhitespace($str) {
|
||||
$r = preg_replace('/[\n\r\t]+/', '', $str);
|
||||
return preg_replace('/\s{2,}/u', ' ', $r);
|
||||
return preg_replace('/\s{2,}/u', ' ', preg_replace('/[\n\r\t]+/', '', $str));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -139,10 +137,13 @@ class Sanitize {
|
|||
* @return string Sting with images stripped.
|
||||
*/
|
||||
public static function stripImages($str) {
|
||||
$str = preg_replace('/(<a[^>]*>)(<img[^>]+alt=")([^"]*)("[^>]*>)(<\/a>)/i', '$1$3$5<br />', $str);
|
||||
$str = preg_replace('/(<img[^>]+alt=")([^"]*)("[^>]*>)/i', '$2<br />', $str);
|
||||
$str = preg_replace('/<img[^>]*>/i', '', $str);
|
||||
return $str;
|
||||
$preg = array(
|
||||
'/(<a[^>]*>)(<img[^>]+alt=")([^"]*)("[^>]*>)(<\/a>)/i' => '$1$3$5<br />',
|
||||
'/(<img[^>]+alt=")([^"]*)("[^>]*>)/i' => '$2<br />',
|
||||
'/<img[^>]*>/i' => ''
|
||||
);
|
||||
|
||||
return preg_replace(array_keys($preg), array_values($preg), $str);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -152,7 +153,8 @@ class Sanitize {
|
|||
* @return string String with <script>, <style>, <link>, <img> elements removed.
|
||||
*/
|
||||
public static function stripScripts($str) {
|
||||
return preg_replace('/(<link[^>]+rel="[^"]*stylesheet"[^>]*>|<img[^>]*>|style="[^"]*")|<script[^>]*>.*?<\/script>|<style[^>]*>.*?<\/style>|<!--.*?-->/is', '', $str);
|
||||
$regex = '/(<link[^>]+rel="[^"]*stylesheet"[^>]*>|<img[^>]*>|style="[^"]*")|<script[^>]*>.*?<\/script>|<style[^>]*>.*?<\/style>|<!--.*?-->/is';
|
||||
return preg_replace($regex, '', $str);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -162,10 +164,11 @@ class Sanitize {
|
|||
* @return string sanitized string
|
||||
*/
|
||||
public static function stripAll($str) {
|
||||
$str = Sanitize::stripWhitespace($str);
|
||||
$str = Sanitize::stripImages($str);
|
||||
$str = Sanitize::stripScripts($str);
|
||||
return $str;
|
||||
return Sanitize::stripScripts(
|
||||
Sanitize::stripImages(
|
||||
Sanitize::stripWhitespace($str)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -212,10 +215,8 @@ class Sanitize {
|
|||
return $data;
|
||||
}
|
||||
|
||||
if (is_string($options)) {
|
||||
if (!is_array($options)) {
|
||||
$options = array('connection' => $options);
|
||||
} elseif (!is_array($options)) {
|
||||
$options = array();
|
||||
}
|
||||
|
||||
$options = array_merge(array(
|
||||
|
@ -235,30 +236,29 @@ class Sanitize {
|
|||
$data[$key] = Sanitize::clean($val, $options);
|
||||
}
|
||||
return $data;
|
||||
} else {
|
||||
if ($options['odd_spaces']) {
|
||||
$data = str_replace(chr(0xCA), '', $data);
|
||||
}
|
||||
if ($options['encode']) {
|
||||
$data = Sanitize::html($data, array('remove' => $options['remove_html']));
|
||||
}
|
||||
if ($options['dollar']) {
|
||||
$data = str_replace("\\\$", "$", $data);
|
||||
}
|
||||
if ($options['carriage']) {
|
||||
$data = str_replace("\r", "", $data);
|
||||
}
|
||||
if ($options['unicode']) {
|
||||
$data = preg_replace("/&#([0-9]+);/s", "&#\\1;", $data);
|
||||
}
|
||||
if ($options['escape']) {
|
||||
$data = Sanitize::escape($data, $options['connection']);
|
||||
}
|
||||
if ($options['backslash']) {
|
||||
$data = preg_replace("/\\\(?!&#|\?#)/", "\\", $data);
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
|
||||
if ($options['odd_spaces']) {
|
||||
$data = str_replace(chr(0xCA), '', $data);
|
||||
}
|
||||
if ($options['encode']) {
|
||||
$data = Sanitize::html($data, array('remove' => $options['remove_html']));
|
||||
}
|
||||
if ($options['dollar']) {
|
||||
$data = str_replace("\\\$", "$", $data);
|
||||
}
|
||||
if ($options['carriage']) {
|
||||
$data = str_replace("\r", "", $data);
|
||||
}
|
||||
if ($options['unicode']) {
|
||||
$data = preg_replace("/&#([0-9]+);/s", "&#\\1;", $data);
|
||||
}
|
||||
if ($options['escape']) {
|
||||
$data = Sanitize::escape($data, $options['connection']);
|
||||
}
|
||||
if ($options['backslash']) {
|
||||
$data = preg_replace("/\\\(?!&#|\?#)/", "\\", $data);
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue