2005-10-03 04:48:00 +00:00
|
|
|
<?php
|
|
|
|
/* SVN FILE: $Id$ */
|
|
|
|
/**
|
2005-10-18 22:27:39 +00:00
|
|
|
* Washes strings from unwanted noise.
|
2006-01-20 09:37:15 +00:00
|
|
|
*
|
2005-10-18 22:27:39 +00:00
|
|
|
* Helpful methods to make unsafe strings usable.
|
2005-10-03 04:48:00 +00:00
|
|
|
*
|
|
|
|
* PHP versions 4 and 5
|
|
|
|
*
|
2007-02-02 10:39:45 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework <http://www.cakephp.org/>
|
|
|
|
* Copyright 2005-2007, Cake Software Foundation, Inc.
|
2006-05-26 05:29:17 +00:00
|
|
|
* 1785 E. Sahara Avenue, Suite 490-204
|
|
|
|
* Las Vegas, Nevada 89104
|
2006-01-20 09:37:15 +00:00
|
|
|
*
|
2005-12-23 21:57:26 +00:00
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
2005-10-03 04:48:00 +00:00
|
|
|
*
|
2006-01-20 09:37:15 +00:00
|
|
|
* @filesource
|
2007-02-02 10:39:45 +00:00
|
|
|
* @copyright Copyright 2005-2007, Cake Software Foundation, Inc.
|
|
|
|
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
|
2006-05-26 05:29:17 +00:00
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.libs
|
2007-02-02 10:39:45 +00:00
|
|
|
* @since CakePHP(tm) v 0.10.0.1076
|
2006-05-26 05:29:17 +00:00
|
|
|
* @version $Revision$
|
|
|
|
* @modifiedby $LastChangedBy$
|
|
|
|
* @lastmodified $Date$
|
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
|
2005-10-03 04:48:00 +00:00
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* Data Sanitization.
|
|
|
|
*
|
2005-10-18 22:27:39 +00:00
|
|
|
* Removal of alpahnumeric characters, SQL-safe slash-added strings, HTML-friendly strings,
|
|
|
|
* and all of the above on arrays.
|
2005-10-03 04:48:00 +00:00
|
|
|
*
|
2006-05-26 05:29:17 +00:00
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.libs
|
2005-10-03 04:48:00 +00:00
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
class Sanitize{
|
2005-10-03 04:48:00 +00:00
|
|
|
/**
|
|
|
|
* Removes any non-alphanumeric characters.
|
|
|
|
*
|
2007-05-26 06:35:44 +00:00
|
|
|
* @param string $string String to sanitize
|
|
|
|
* @return string Sanitized string
|
|
|
|
* @access public
|
|
|
|
* @static
|
2005-10-03 04:48:00 +00:00
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
function paranoid($string, $allowed = array()) {
|
|
|
|
$allow = null;
|
|
|
|
if (!empty($allowed)) {
|
2007-06-20 06:15:35 +00:00
|
|
|
foreach ($allowed as $value) {
|
2006-05-26 05:29:17 +00:00
|
|
|
$allow .= "\\$value";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_array($string)) {
|
2007-06-20 06:15:35 +00:00
|
|
|
foreach ($string as $key => $clean) {
|
2007-01-06 06:19:58 +00:00
|
|
|
$cleaned[$key] = preg_replace("/[^{$allow}a-zA-Z0-9]/", '', $clean);
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
|
|
|
} else {
|
2007-01-06 06:19:58 +00:00
|
|
|
$cleaned = preg_replace("/[^{$allow}a-zA-Z0-9]/", '', $string);
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
|
|
|
return $cleaned;
|
|
|
|
}
|
2007-01-06 06:19:58 +00:00
|
|
|
/**
|
|
|
|
* Makes a string SQL-safe.
|
|
|
|
*
|
2007-05-26 06:35:44 +00:00
|
|
|
* @param string $string String to sanitize
|
|
|
|
* @param string $connection Database connection being used
|
|
|
|
* @return string SQL safe string
|
|
|
|
* @access public
|
|
|
|
* @static
|
2007-01-06 06:19:58 +00:00
|
|
|
*/
|
|
|
|
function escape($string, $connection = 'default') {
|
|
|
|
$db = ConnectionManager::getDataSource($connection);
|
2007-07-09 04:32:04 +00:00
|
|
|
$value = substr($db->value($string), 1);
|
|
|
|
$value = substr($value, 0, -1);
|
2007-03-05 12:13:37 +00:00
|
|
|
return $value;
|
2007-01-06 06:19:58 +00:00
|
|
|
}
|
2005-10-03 04:48:00 +00:00
|
|
|
/**
|
2006-08-29 06:07:31 +00:00
|
|
|
* Returns given string safe for display as HTML. Renders entities.
|
2005-10-03 04:48:00 +00:00
|
|
|
*
|
2007-05-26 06:35:44 +00:00
|
|
|
* @param string $string String from where to strip tags
|
2005-10-18 22:27:39 +00:00
|
|
|
* @param boolean $remove If true, the string is stripped of all HTML tags
|
2007-05-26 06:35:44 +00:00
|
|
|
* @return string Sanitized string
|
|
|
|
* @access public
|
|
|
|
* @static
|
2005-10-03 04:48:00 +00:00
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
function html($string, $remove = false) {
|
|
|
|
if ($remove) {
|
|
|
|
$string = strip_tags($string);
|
|
|
|
} else {
|
|
|
|
$patterns = array("/\&/", "/%/", "/</", "/>/", '/"/', "/'/", "/\(/", "/\)/", "/\+/", "/-/");
|
|
|
|
$replacements = array("&", "%", "<", ">", """, "'", "(", ")", "+", "-");
|
|
|
|
$string = preg_replace($patterns, $replacements, $string);
|
|
|
|
}
|
|
|
|
return $string;
|
|
|
|
}
|
2006-08-07 01:22:57 +00:00
|
|
|
/**
|
|
|
|
* Strips extra whitespace from output
|
|
|
|
*
|
2007-05-26 06:35:44 +00:00
|
|
|
* @param string $str String to sanitize
|
|
|
|
* @access public
|
|
|
|
* @static
|
2006-08-07 01:22:57 +00:00
|
|
|
*/
|
|
|
|
function stripWhitespace($str) {
|
|
|
|
$r = preg_replace('/[\n\r\t]+/', '', $str);
|
|
|
|
return preg_replace('/\s{2,}/', ' ', $r);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Strips image tags from output
|
|
|
|
*
|
2007-05-26 06:35:44 +00:00
|
|
|
* @param string $str String to sanitize
|
|
|
|
* @access public
|
|
|
|
* @static
|
2006-08-07 01:22:57 +00:00
|
|
|
*/
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Strips scripts and stylesheets from output
|
|
|
|
*
|
2007-05-26 06:35:44 +00:00
|
|
|
* @param string $str String to sanitize
|
|
|
|
* @access public
|
|
|
|
* @static
|
2006-08-07 01:22:57 +00:00
|
|
|
*/
|
|
|
|
function stripScripts($str) {
|
|
|
|
return preg_replace('/(<link[^>]+rel="[^"]*stylesheet"[^>]*>|<img[^>]*>|style="[^"]*")|<script[^>]*>.*?<\/script>|<style[^>]*>.*?<\/style>|<!--.*?-->/i', '', $str);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Strips extra whitespace, images, scripts and stylesheets from output
|
|
|
|
*
|
2007-05-26 06:35:44 +00:00
|
|
|
* @param string $str String to sanitize
|
|
|
|
* @access public
|
2006-08-07 01:22:57 +00:00
|
|
|
*/
|
|
|
|
function stripAll($str) {
|
|
|
|
$str = $this->stripWhitespace($str);
|
|
|
|
$str = $this->stripImages($str);
|
|
|
|
$str = $this->stripScripts($str);
|
|
|
|
return $str;
|
|
|
|
}
|
|
|
|
/**
|
2007-05-26 06:35:44 +00:00
|
|
|
* Strips the specified tags from output. First parameter is string from
|
|
|
|
* where to remove tags. All subsequent parameters are tags.
|
2006-08-07 01:22:57 +00:00
|
|
|
*
|
2007-05-26 06:35:44 +00:00
|
|
|
* @param string $str String to sanitize
|
|
|
|
* @param string $tag Tag to remove (add more parameters as needed)
|
|
|
|
* @access public
|
|
|
|
* @static
|
2006-08-07 01:22:57 +00:00
|
|
|
*/
|
|
|
|
function stripTags() {
|
|
|
|
$params = params(func_get_args());
|
|
|
|
$str = $params[0];
|
|
|
|
|
2007-06-20 06:15:35 +00:00
|
|
|
for ($i = 1; $i < count($params); $i++) {
|
2006-08-07 01:22:57 +00:00
|
|
|
$str = preg_replace('/<' . $params[$i] . '[^>]*>/i', '', $str);
|
|
|
|
$str = preg_replace('/<\/' . $params[$i] . '[^>]*>/i', '', $str);
|
|
|
|
}
|
|
|
|
return $str;
|
|
|
|
}
|
2007-01-06 06:19:58 +00:00
|
|
|
/**
|
|
|
|
* Sanitizes given array or value for safe input.
|
|
|
|
*
|
2007-05-26 06:35:44 +00:00
|
|
|
* @param mixed $data Data to sanitize
|
|
|
|
* @param string $connection DB connection being used
|
|
|
|
* @return mixed Sanitized data
|
|
|
|
* @access public
|
|
|
|
* @static
|
2007-01-06 06:19:58 +00:00
|
|
|
*/
|
|
|
|
function clean($data, $connection = 'default') {
|
2007-03-04 12:19:13 +00:00
|
|
|
if (empty($data)) {
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
2007-01-06 06:19:58 +00:00
|
|
|
if (is_array($data)) {
|
|
|
|
foreach ($data as $key => $val) {
|
2007-06-05 11:39:48 +00:00
|
|
|
$data[$key] = Sanitize::clean($val, $connection);
|
2007-01-06 06:19:58 +00:00
|
|
|
}
|
2007-03-04 12:28:42 +00:00
|
|
|
return $data;
|
2007-01-06 06:19:58 +00:00
|
|
|
} else {
|
|
|
|
//Replace odd spaces with safe ones
|
|
|
|
$val = str_replace(chr(0xCA), '', str_replace(' ', ' ', $data));
|
|
|
|
//Encode any HTML to entities.
|
|
|
|
$val = Sanitize::html($val);
|
|
|
|
|
|
|
|
//Double-check special chars and remove carriage returns
|
|
|
|
//For increased SQL security
|
|
|
|
$val = preg_replace("/\\\$/", "$", $val);
|
|
|
|
$val = preg_replace("/\r/", "", $val);
|
|
|
|
$val = str_replace("'", "'", str_replace("!", "!", $val));
|
2006-03-12 00:11:40 +00:00
|
|
|
|
2007-01-06 06:19:58 +00:00
|
|
|
//Allow unicode (?)
|
|
|
|
$val = preg_replace("/&#([0-9]+);/s", "&#\\1;", $val);
|
|
|
|
|
|
|
|
// Escape for DB output
|
|
|
|
$val = Sanitize::escape($val, $connection);
|
|
|
|
|
|
|
|
//Swap user-inputted backslashes (?)
|
|
|
|
$val = preg_replace("/\\\(?!&#|\?#)/", "\\", $val);
|
|
|
|
return $val;
|
|
|
|
}
|
|
|
|
}
|
2006-03-12 00:11:40 +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
|
2007-05-26 06:35:44 +00:00
|
|
|
* @static
|
2006-03-12 00:11:40 +00:00
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
function formatColumns(&$model) {
|
2007-06-20 06:15:35 +00:00
|
|
|
foreach ($model->data as $name => $values) {
|
2006-05-26 05:29:17 +00:00
|
|
|
if ($name == $model->name) {
|
|
|
|
$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) {
|
2007-06-20 06:15:35 +00:00
|
|
|
foreach ($values as $column => $data) {
|
2006-05-26 05:29:17 +00:00
|
|
|
$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'])) {
|
|
|
|
|
|
|
|
switch(strtolower($colData['formatter'])) {
|
|
|
|
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;
|
|
|
|
/*
|
|
|
|
switch($colType) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-10-03 04:48:00 +00:00
|
|
|
}
|
2005-07-21 04:02:32 +00:00
|
|
|
?>
|