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)
|
2013-02-08 11:59:49 +00:00
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
2013-02-08 12:22:51 +00:00
|
|
|
* For full copyright and license information, please see the LICENSE.txt
|
2008-05-30 11:40:08 +00:00
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2013-02-08 11:59:49 +00:00
|
|
|
* @copyright Copyright (c) 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
|
2013-05-30 22:11:14 +00:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
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
|
2013-08-09 18:40:05 +00:00
|
|
|
* @deprecated Deprecated since version 2.4
|
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";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-12 17:26:04 +00:00
|
|
|
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);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2012-09-12 17:26:04 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
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') {
|
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;
|
|
|
|
}
|
2013-08-15 10:15:09 +00:00
|
|
|
$db = ConnectionManager::getDataSource($connection);
|
2012-01-07 03:53:39 +00:00
|
|
|
$string = $db->value($string, 'string');
|
2012-09-12 17:26:04 +00:00
|
|
|
$start = 1;
|
|
|
|
if ($string{0} === 'N') {
|
|
|
|
$start = 2;
|
2012-01-07 03:53:39 +00:00
|
|
|
}
|
2012-01-07 04:06:12 +00:00
|
|
|
|
2013-02-01 16:10:24 +00:00
|
|
|
return substr(substr($string, $start), 0, -1);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
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
|
2013-03-05 07:05:14 +00:00
|
|
|
* - double (boolean) double 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) {
|
2012-09-12 17:26:04 +00:00
|
|
|
return preg_replace('/\s{2,}/u', ' ', preg_replace('/[\n\r\t]+/', '', $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 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) {
|
2012-09-12 17:26:04 +00:00
|
|
|
$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);
|
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 scripts and stylesheets from output
|
|
|
|
*
|
|
|
|
* @param string $str String to sanitize
|
2012-09-13 05:30:15 +00:00
|
|
|
* @return string String with <link>, <img>, <script>, <style> elements and html comments removed.
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-14 04:29:44 +00:00
|
|
|
public static function stripScripts($str) {
|
2012-09-13 05:30:15 +00:00
|
|
|
$regex =
|
|
|
|
'/(<link[^>]+rel="[^"]*stylesheet"[^>]*>|' .
|
|
|
|
'<img[^>]*>|style="[^"]*")|' .
|
|
|
|
'<script[^>]*>.*?<\/script>|' .
|
|
|
|
'<style[^>]*>.*?<\/style>|' .
|
|
|
|
'<!--.*?-->/is';
|
2012-09-12 17:26:04 +00:00
|
|
|
return preg_replace($regex, '', $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) {
|
2012-09-12 17:26:04 +00:00
|
|
|
return Sanitize::stripScripts(
|
|
|
|
Sanitize::stripImages(
|
|
|
|
Sanitize::stripWhitespace($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 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
|
|
|
*
|
2012-05-13 00:43:31 +00:00
|
|
|
* @param string|array $data Data to sanitize
|
|
|
|
* @param string|array $options If string, DB connection being used, otherwise set of options
|
2008-05-30 11:40:08 +00:00
|
|
|
* @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;
|
|
|
|
}
|
|
|
|
|
2012-09-12 17:26:04 +00:00
|
|
|
if (!is_array($options)) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$options = array('connection' => $options);
|
|
|
|
}
|
|
|
|
|
|
|
|
$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;
|
|
|
|
}
|
2012-03-03 22:31:47 +00:00
|
|
|
|
2012-09-12 17:26:04 +00:00
|
|
|
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;
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|