mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-09-01 09:02:38 +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)) {
|
if (!is_array($string)) {
|
||||||
|
return preg_replace("/[^{$allow}a-zA-Z0-9]/", '', $string);
|
||||||
|
}
|
||||||
|
|
||||||
$cleaned = array();
|
$cleaned = array();
|
||||||
foreach ($string as $key => $clean) {
|
foreach ($string as $key => $clean) {
|
||||||
$cleaned[$key] = preg_replace("/[^{$allow}a-zA-Z0-9]/", '', $clean);
|
$cleaned[$key] = preg_replace("/[^{$allow}a-zA-Z0-9]/", '', $clean);
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
$cleaned = preg_replace("/[^{$allow}a-zA-Z0-9]/", '', $string);
|
|
||||||
}
|
|
||||||
return $cleaned;
|
return $cleaned;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -70,14 +71,12 @@ class Sanitize {
|
||||||
return $string;
|
return $string;
|
||||||
}
|
}
|
||||||
$string = $db->value($string, 'string');
|
$string = $db->value($string, 'string');
|
||||||
if ($string[0] === 'N') {
|
$start = 1;
|
||||||
$string = substr($string, 2);
|
if ($string{0} === 'N') {
|
||||||
} else {
|
$start = 2;
|
||||||
$string = substr($string, 1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$string = substr($string, 0, -1);
|
return substr(substr($string, 1), 0, -1);
|
||||||
return $string;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -128,8 +127,7 @@ class Sanitize {
|
||||||
* @return string whitespace sanitized string
|
* @return string whitespace sanitized string
|
||||||
*/
|
*/
|
||||||
public static function stripWhitespace($str) {
|
public static function stripWhitespace($str) {
|
||||||
$r = preg_replace('/[\n\r\t]+/', '', $str);
|
return preg_replace('/\s{2,}/u', ' ', preg_replace('/[\n\r\t]+/', '', $str));
|
||||||
return preg_replace('/\s{2,}/u', ' ', $r);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -139,10 +137,13 @@ class Sanitize {
|
||||||
* @return string Sting with images stripped.
|
* @return string Sting with images stripped.
|
||||||
*/
|
*/
|
||||||
public static function stripImages($str) {
|
public static function stripImages($str) {
|
||||||
$str = preg_replace('/(<a[^>]*>)(<img[^>]+alt=")([^"]*)("[^>]*>)(<\/a>)/i', '$1$3$5<br />', $str);
|
$preg = array(
|
||||||
$str = preg_replace('/(<img[^>]+alt=")([^"]*)("[^>]*>)/i', '$2<br />', $str);
|
'/(<a[^>]*>)(<img[^>]+alt=")([^"]*)("[^>]*>)(<\/a>)/i' => '$1$3$5<br />',
|
||||||
$str = preg_replace('/<img[^>]*>/i', '', $str);
|
'/(<img[^>]+alt=")([^"]*)("[^>]*>)/i' => '$2<br />',
|
||||||
return $str;
|
'/<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.
|
* @return string String with <script>, <style>, <link>, <img> elements removed.
|
||||||
*/
|
*/
|
||||||
public static function stripScripts($str) {
|
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
|
* @return string sanitized string
|
||||||
*/
|
*/
|
||||||
public static function stripAll($str) {
|
public static function stripAll($str) {
|
||||||
$str = Sanitize::stripWhitespace($str);
|
return Sanitize::stripScripts(
|
||||||
$str = Sanitize::stripImages($str);
|
Sanitize::stripImages(
|
||||||
$str = Sanitize::stripScripts($str);
|
Sanitize::stripWhitespace($str)
|
||||||
return $str;
|
)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -212,10 +215,8 @@ class Sanitize {
|
||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (is_string($options)) {
|
if (!is_array($options)) {
|
||||||
$options = array('connection' => $options);
|
$options = array('connection' => $options);
|
||||||
} elseif (!is_array($options)) {
|
|
||||||
$options = array();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$options = array_merge(array(
|
$options = array_merge(array(
|
||||||
|
@ -235,7 +236,8 @@ class Sanitize {
|
||||||
$data[$key] = Sanitize::clean($val, $options);
|
$data[$key] = Sanitize::clean($val, $options);
|
||||||
}
|
}
|
||||||
return $data;
|
return $data;
|
||||||
} else {
|
}
|
||||||
|
|
||||||
if ($options['odd_spaces']) {
|
if ($options['odd_spaces']) {
|
||||||
$data = str_replace(chr(0xCA), '', $data);
|
$data = str_replace(chr(0xCA), '', $data);
|
||||||
}
|
}
|
||||||
|
@ -260,5 +262,3 @@ class Sanitize {
|
||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue