2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Washes strings from unwanted noise.
|
|
|
|
*
|
|
|
|
* Helpful methods to make unsafe strings usable.
|
|
|
|
*
|
2010-10-03 16:38:58 +00:00
|
|
|
* PHP 5
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2009-11-06 06:46:59 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2011-05-29 21:31:39 +00:00
|
|
|
* Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2011-05-29 21:31:39 +00:00
|
|
|
* @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2009-11-06 06:00:11 +00:00
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Utility
|
2008-10-30 17:30:26 +00:00
|
|
|
* @since CakePHP(tm) v 0.10.0.1076
|
2009-11-06 06:51:51 +00:00
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-12-08 15:35:02 +00:00
|
|
|
|
2011-12-06 20:52:48 +00:00
|
|
|
App::import('Model', 'ConnectionManager');
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Data Sanitization.
|
|
|
|
*
|
2011-12-02 05:58:09 +00:00
|
|
|
* Removal of alphanumeric characters, SQL-safe slash-added strings, HTML-friendly strings,
|
2008-05-30 11:40:08 +00:00
|
|
|
* and all of the above on arrays.
|
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Utility
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
|
|
|
class Sanitize {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Removes any non-alphanumeric characters.
|
|
|
|
*
|
|
|
|
* @param string $string String to sanitize
|
2010-01-08 02:47:23 +00:00
|
|
|
* @param array $allowed An array of additional characters that are not to be removed.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return string Sanitized string
|
|
|
|
*/
|
2010-04-14 04:29:44 +00:00
|
|
|
public static function paranoid($string, $allowed = array()) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$allow = null;
|
|
|
|
if (!empty($allowed)) {
|
|
|
|
foreach ($allowed as $value) {
|
|
|
|
$allow .= "\\$value";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
return $cleaned;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Makes a string SQL-safe.
|
|
|
|
*
|
|
|
|
* @param string $string String to sanitize
|
|
|
|
* @param string $connection Database connection being used
|
|
|
|
* @return string SQL safe string
|
|
|
|
*/
|
2010-04-14 04:29:44 +00:00
|
|
|
public static function escape($string, $connection = 'default') {
|
2011-04-29 15:49:33 +00:00
|
|
|
$db = ConnectionManager::getDataSource($connection);
|
2008-08-02 14:31:50 +00:00
|
|
|
if (is_numeric($string) || $string === null || is_bool($string)) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return $string;
|
|
|
|
}
|
2012-01-07 03:53:39 +00:00
|
|
|
$string = $db->value($string, 'string');
|
|
|
|
if ($string[0] === 'N') {
|
|
|
|
$string = substr($string, 2);
|
2012-01-07 04:06:12 +00:00
|
|
|
} else {
|
|
|
|
$string = substr($string, 1);
|
2012-01-07 03:53:39 +00:00
|
|
|
}
|
2012-01-07 04:06:12 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
$string = substr($string, 0, -1);
|
|
|
|
return $string;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Returns given string safe for display as HTML. Renders entities.
|
2010-03-22 19:09:24 +00:00
|
|
|
*
|
2010-01-08 02:47:23 +00:00
|
|
|
* strip_tags() does not validating HTML syntax or structure, so it might strip whole passages
|
2010-01-07 20:15:10 +00:00
|
|
|
* with broken HTML.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-01-08 02:47:23 +00:00
|
|
|
* ### Options:
|
|
|
|
*
|
|
|
|
* - remove (boolean) if true strips all HTML tags before encoding
|
|
|
|
* - charset (string) the charset used to encode the string
|
|
|
|
* - quotes (int) see http://php.net/manual/en/function.htmlentities.php
|
2010-10-12 01:25:11 +00:00
|
|
|
* - double (boolean) doube encode html entities
|
2010-01-08 02:47:23 +00:00
|
|
|
*
|
2008-05-30 11:40:08 +00:00
|
|
|
* @param string $string String from where to strip tags
|
2010-01-08 02:47:23 +00:00
|
|
|
* @param array $options Array of options to use.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return string Sanitized string
|
|
|
|
*/
|
2010-04-14 04:29:44 +00:00
|
|
|
public static function html($string, $options = array()) {
|
2010-03-22 19:09:24 +00:00
|
|
|
static $defaultCharset = false;
|
|
|
|
if ($defaultCharset === false) {
|
|
|
|
$defaultCharset = Configure::read('App.encoding');
|
|
|
|
if ($defaultCharset === null) {
|
|
|
|
$defaultCharset = 'UTF-8';
|
|
|
|
}
|
|
|
|
}
|
2010-01-07 20:15:10 +00:00
|
|
|
$default = array(
|
|
|
|
'remove' => false,
|
2010-03-22 19:09:24 +00:00
|
|
|
'charset' => $defaultCharset,
|
2010-10-12 01:25:11 +00:00
|
|
|
'quotes' => ENT_QUOTES,
|
|
|
|
'double' => true
|
2010-01-07 20:15:10 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$options = array_merge($default, $options);
|
|
|
|
|
|
|
|
if ($options['remove']) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$string = strip_tags($string);
|
|
|
|
}
|
2010-03-22 19:09:24 +00:00
|
|
|
|
2010-10-12 01:25:11 +00:00
|
|
|
return htmlentities($string, $options['quotes'], $options['charset'], $options['double']);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Strips extra whitespace from output
|
|
|
|
*
|
|
|
|
* @param string $str String to sanitize
|
2008-09-25 16:49:56 +00:00
|
|
|
* @return string whitespace sanitized string
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-14 04:29:44 +00:00
|
|
|
public static function stripWhitespace($str) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$r = preg_replace('/[\n\r\t]+/', '', $str);
|
2011-07-31 19:44:10 +00:00
|
|
|
return preg_replace('/\s{2,}/u', ' ', $r);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Strips image tags from output
|
|
|
|
*
|
|
|
|
* @param string $str String to sanitize
|
2008-09-25 16:49:56 +00:00
|
|
|
* @return string Sting with images stripped.
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-14 04:29:44 +00:00
|
|
|
public static function stripImages($str) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$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;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Strips scripts and stylesheets from output
|
|
|
|
*
|
|
|
|
* @param string $str String to sanitize
|
2011-06-02 19:19:57 +00:00
|
|
|
* @return string String with <script>, <style>, <link>, <img> elements removed.
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-14 04:29:44 +00:00
|
|
|
public static function stripScripts($str) {
|
2010-05-04 02:31:55 +00:00
|
|
|
return preg_replace('/(<link[^>]+rel="[^"]*stylesheet"[^>]*>|<img[^>]*>|style="[^"]*")|<script[^>]*>.*?<\/script>|<style[^>]*>.*?<\/style>|<!--.*?-->/is', '', $str);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Strips extra whitespace, images, scripts and stylesheets from output
|
|
|
|
*
|
|
|
|
* @param string $str String to sanitize
|
2008-09-25 16:49:56 +00:00
|
|
|
* @return string sanitized string
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-14 04:29:44 +00:00
|
|
|
public static function stripAll($str) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$str = Sanitize::stripWhitespace($str);
|
|
|
|
$str = Sanitize::stripImages($str);
|
|
|
|
$str = Sanitize::stripScripts($str);
|
|
|
|
return $str;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Strips the specified tags from output. First parameter is string from
|
|
|
|
* where to remove tags. All subsequent parameters are tags.
|
|
|
|
*
|
2010-01-08 02:47:23 +00:00
|
|
|
* Ex.`$clean = Sanitize::stripTags($dirty, 'b', 'p', 'div');`
|
|
|
|
*
|
|
|
|
* Will remove all `<b>`, `<p>`, and `<div>` tags from the $dirty string.
|
|
|
|
*
|
2011-08-22 01:45:34 +00:00
|
|
|
* @param string $str,... String to sanitize
|
2008-09-25 16:49:56 +00:00
|
|
|
* @return string sanitized String
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-08-22 01:45:34 +00:00
|
|
|
public static function stripTags($str) {
|
2010-04-24 01:32:39 +00:00
|
|
|
$params = func_get_args();
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2009-11-21 20:14:21 +00:00
|
|
|
for ($i = 1, $count = count($params); $i < $count; $i++) {
|
2008-11-16 05:00:41 +00:00
|
|
|
$str = preg_replace('/<' . $params[$i] . '\b[^>]*>/i', '', $str);
|
2008-05-30 11:40:08 +00:00
|
|
|
$str = preg_replace('/<\/' . $params[$i] . '[^>]*>/i', '', $str);
|
|
|
|
}
|
|
|
|
return $str;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Sanitizes given array or value for safe input. Use the options to specify
|
|
|
|
* the connection to use, and what filters should be applied (with a boolean
|
2010-03-22 19:09:24 +00:00
|
|
|
* value). Valid filters:
|
2010-01-08 02:47:23 +00:00
|
|
|
*
|
|
|
|
* - odd_spaces - removes any non space whitespace characters
|
|
|
|
* - encode - Encode any html entities. Encode must be true for the `remove_html` to work.
|
|
|
|
* - dollar - Escape `$` with `\$`
|
|
|
|
* - carriage - Remove `\r`
|
2010-03-22 19:09:24 +00:00
|
|
|
* - unicode -
|
2010-01-08 02:47:23 +00:00
|
|
|
* - escape - Should the string be SQL escaped.
|
2010-03-22 19:09:24 +00:00
|
|
|
* - backslash -
|
2010-01-08 02:47:23 +00:00
|
|
|
* - remove_html - Strip HTML with strip_tags. `encode` must be true for this option to work.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @param mixed $data Data to sanitize
|
|
|
|
* @param mixed $options If string, DB connection being used, otherwise set of options
|
|
|
|
* @return mixed Sanitized data
|
|
|
|
*/
|
2010-04-14 04:29:44 +00:00
|
|
|
public static function clean($data, $options = array()) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (empty($data)) {
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_string($options)) {
|
|
|
|
$options = array('connection' => $options);
|
2012-02-23 13:38:02 +00:00
|
|
|
} elseif (!is_array($options)) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$options = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
$options = array_merge(array(
|
|
|
|
'connection' => 'default',
|
|
|
|
'odd_spaces' => true,
|
2010-01-07 20:15:10 +00:00
|
|
|
'remove_html' => false,
|
2008-05-30 11:40:08 +00:00
|
|
|
'encode' => true,
|
|
|
|
'dollar' => true,
|
|
|
|
'carriage' => true,
|
|
|
|
'unicode' => true,
|
|
|
|
'escape' => true,
|
|
|
|
'backslash' => true
|
|
|
|
), $options);
|
|
|
|
|
|
|
|
if (is_array($data)) {
|
|
|
|
foreach ($data as $key => $val) {
|
|
|
|
$data[$key] = Sanitize::clean($val, $options);
|
|
|
|
}
|
|
|
|
return $data;
|
|
|
|
} else {
|
|
|
|
if ($options['odd_spaces']) {
|
2011-06-24 03:40:25 +00:00
|
|
|
$data = str_replace(chr(0xCA), '', $data);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
if ($options['encode']) {
|
2010-01-07 20:15:10 +00:00
|
|
|
$data = Sanitize::html($data, array('remove' => $options['remove_html']));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|