Skip to content

Commit

Permalink
Simplify the code for sanitize class
Browse files Browse the repository at this point in the history
removing else statements and variables that are not needed.
eg: return something(); vs $foo = something(); return $foo;
  • Loading branch information
dogmatic69 committed Sep 12, 2012
1 parent b227ff4 commit 44f8f84
Showing 1 changed file with 51 additions and 51 deletions.
102 changes: 51 additions & 51 deletions lib/Cake/Utility/Sanitize.php
Expand Up @@ -46,14 +46,15 @@ public static function paranoid($string, $allowed = array()) {
}
}

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;
}

Expand All @@ -70,14 +71,12 @@ public static function escape($string, $connection = 'default') {
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);
}

/**
Expand Down Expand Up @@ -128,8 +127,7 @@ public static function html($string, $options = array()) {
* @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));
}

/**
Expand All @@ -139,10 +137,13 @@ public static function stripWhitespace($str) {
* @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);
}

/**
Expand All @@ -152,7 +153,8 @@ public static function stripImages($str) {
* @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);
}

/**
Expand All @@ -162,10 +164,11 @@ public static function stripScripts($str) {
* @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)
)
);
}

/**
Expand Down Expand Up @@ -212,10 +215,8 @@ public static function clean($data, $options = array()) {
return $data;
}

if (is_string($options)) {
if (!is_array($options)) {
$options = array('connection' => $options);
} elseif (!is_array($options)) {
$options = array();
}

$options = array_merge(array(
Expand All @@ -235,30 +236,29 @@ public static function clean($data, $options = array()) {
$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("/&amp;#([0-9]+);/s", "&#\\1;", $data);
}
if ($options['escape']) {
$data = Sanitize::escape($data, $options['connection']);
}
if ($options['backslash']) {
$data = preg_replace("/\\\(?!&amp;#|\?#)/", "\\", $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("/&amp;#([0-9]+);/s", "&#\\1;", $data);
}
if ($options['escape']) {
$data = Sanitize::escape($data, $options['connection']);
}
if ($options['backslash']) {
$data = preg_replace("/\\\(?!&amp;#|\?#)/", "\\", $data);
}
return $data;
}
}

0 comments on commit 44f8f84

Please sign in to comment.