2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Washes strings from unwanted noise.
|
|
|
|
*
|
|
|
|
* Helpful methods to make unsafe strings usable.
|
|
|
|
*
|
|
|
|
* PHP versions 4 and 5
|
|
|
|
*
|
2009-11-06 06:46:59 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2010-01-26 19:18:20 +00:00
|
|
|
* Copyright 2005-2010, 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.
|
|
|
|
*
|
2010-01-26 19:18:20 +00:00
|
|
|
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2009-11-06 06:00:11 +00:00
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
2008-10-30 17:30:26 +00:00
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.libs
|
|
|
|
* @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
|
|
|
*/
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Data Sanitization.
|
|
|
|
*
|
|
|
|
* Removal of alpahnumeric characters, SQL-safe slash-added strings, HTML-friendly strings,
|
|
|
|
* and all of the above on arrays.
|
|
|
|
*
|
2008-10-30 17:30:26 +00:00
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.libs
|
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
|
|
|
|
* @access public
|
|
|
|
* @static
|
|
|
|
*/
|
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
|
|
|
|
* @access public
|
|
|
|
* @static
|
|
|
|
*/
|
2010-04-14 04:29:44 +00:00
|
|
|
public static function escape($string, $connection = 'default') {
|
2008-05-30 11:40:08 +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;
|
|
|
|
}
|
|
|
|
$string = substr($db->value($string), 1);
|
|
|
|
$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
|
|
|
|
*
|
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
|
|
|
|
* @access public
|
|
|
|
* @static
|
|
|
|
*/
|
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-01-07 20:15:10 +00:00
|
|
|
'quotes' => ENT_QUOTES
|
|
|
|
);
|
|
|
|
|
|
|
|
$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
|
|
|
|
|
|
|
return htmlentities($string, $options['quotes'], $options['charset']);
|
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
|
|
|
* @access public
|
|
|
|
* @static
|
|
|
|
*/
|
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);
|
|
|
|
return preg_replace('/\s{2,}/', ' ', $r);
|
|
|
|
}
|
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
|
|
|
* @access public
|
|
|
|
* @static
|
|
|
|
*/
|
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
|
2008-09-25 16:49:56 +00:00
|
|
|
* @return string String with <script>, <style>, <link> elements removed.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @access public
|
|
|
|
* @static
|
|
|
|
*/
|
2010-04-14 04:29:44 +00:00
|
|
|
public static function stripScripts($str) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return preg_replace('/(<link[^>]+rel="[^"]*stylesheet"[^>]*>|<img[^>]*>|style="[^"]*")|<script[^>]*>.*?<\/script>|<style[^>]*>.*?<\/style>|<!--.*?-->/i', '', $str);
|
|
|
|
}
|
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.
|
|
|
|
*
|
2008-05-30 11:40:08 +00:00
|
|
|
* @param string $str String to sanitize
|
|
|
|
* @param string $tag Tag to remove (add more parameters as needed)
|
2008-09-25 16:49:56 +00:00
|
|
|
* @return string sanitized String
|
2008-05-30 11:40:08 +00:00
|
|
|
* @access public
|
|
|
|
* @static
|
|
|
|
*/
|
2010-04-14 04:29:44 +00:00
|
|
|
public static function stripTags() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$params = params(func_get_args());
|
|
|
|
$str = $params[0];
|
|
|
|
|
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
|
|
|
|
* @access public
|
|
|
|
* @static
|
|
|
|
*/
|
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);
|
|
|
|
} else if (!is_array($options)) {
|
|
|
|
$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']) {
|
|
|
|
$data = str_replace(chr(0xCA), '', str_replace(' ', ' ', $data));
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
$data = str_replace("'", "'", str_replace("!", "!", $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;
|
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Formats column data from definition in DBO's $columns array
|
|
|
|
*
|
|
|
|
* @param Model $model The model containing the data to be formatted
|
|
|
|
* @access public
|
|
|
|
* @static
|
|
|
|
*/
|
2010-04-14 04:29:44 +00:00
|
|
|
public static function formatColumns(&$model) {
|
2008-05-30 11:40:08 +00:00
|
|
|
foreach ($model->data as $name => $values) {
|
2008-08-03 15:32:33 +00:00
|
|
|
if ($name == $model->alias) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$curModel =& $model;
|
|
|
|
} elseif (isset($model->{$name}) && is_object($model->{$name}) && is_subclass_of($model->{$name}, 'Model')) {
|
|
|
|
$curModel =& $model->{$name};
|
|
|
|
} else {
|
|
|
|
$curModel = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($curModel != null) {
|
|
|
|
foreach ($values as $column => $data) {
|
|
|
|
$colType = $curModel->getColumnType($column);
|
|
|
|
|
|
|
|
if ($colType != null) {
|
|
|
|
$db =& ConnectionManager::getDataSource($curModel->useDbConfig);
|
|
|
|
$colData = $db->columns[$colType];
|
|
|
|
|
|
|
|
if (isset($colData['limit']) && strlen(strval($data)) > $colData['limit']) {
|
|
|
|
$data = substr(strval($data), 0, $colData['limit']);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($colData['formatter']) || isset($colData['format'])) {
|
|
|
|
|
2008-10-23 00:10:44 +00:00
|
|
|
switch (strtolower($colData['formatter'])) {
|
2008-05-30 11:40:08 +00:00
|
|
|
case 'date':
|
|
|
|
$data = date($colData['format'], strtotime($data));
|
|
|
|
break;
|
|
|
|
case 'sprintf':
|
|
|
|
$data = sprintf($colData['format'], $data);
|
|
|
|
break;
|
|
|
|
case 'intval':
|
|
|
|
$data = intval($data);
|
|
|
|
break;
|
|
|
|
case 'floatval':
|
|
|
|
$data = floatval($data);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$model->data[$name][$column]=$data;
|
|
|
|
/*
|
2008-10-23 00:10:44 +00:00
|
|
|
switch ($colType) {
|
2008-05-30 11:40:08 +00:00
|
|
|
case 'integer':
|
|
|
|
case 'int':
|
|
|
|
return $data;
|
|
|
|
break;
|
|
|
|
case 'string':
|
|
|
|
case 'text':
|
|
|
|
case 'binary':
|
|
|
|
case 'date':
|
|
|
|
case 'time':
|
|
|
|
case 'datetime':
|
|
|
|
case 'timestamp':
|
|
|
|
case 'date':
|
|
|
|
return "'" . $data . "'";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
?>
|