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
|
|
|
|
*
|
|
|
|
* CakePHP : Rapid Development Framework <http://www.cakephp.org/>
|
2006-01-20 09:37:15 +00:00
|
|
|
* Copyright (c) 2006, Cake Software Foundation, Inc.
|
2005-12-23 21:57:26 +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
|
2006-01-20 07:46:14 +00:00
|
|
|
* @copyright Copyright (c) 2006, Cake Software Foundation, Inc.
|
2005-12-23 21:57:26 +00:00
|
|
|
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP Project
|
2005-10-03 04:48:00 +00:00
|
|
|
* @package cake
|
2005-10-09 01:56:21 +00:00
|
|
|
* @subpackage cake.cake.libs
|
|
|
|
* @since CakePHP v 0.10.0.1076
|
2005-10-03 04:48:00 +00:00
|
|
|
* @version $Revision$
|
|
|
|
* @modifiedby $LastChangedBy$
|
|
|
|
* @lastmodified $Date$
|
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
*
|
|
|
|
* @package cake
|
2005-10-09 01:56:21 +00:00
|
|
|
* @subpackage cake.cake.libs
|
|
|
|
* @since CakePHP v 0.10.0.1076
|
2005-10-03 04:48:00 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
class Sanitize
|
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes any non-alphanumeric characters.
|
|
|
|
*
|
|
|
|
* @param string $string
|
|
|
|
* @return string
|
|
|
|
*/
|
2006-01-20 09:37:15 +00:00
|
|
|
function paranoid($string, $allowed = array())
|
2005-12-27 03:33:44 +00:00
|
|
|
{
|
2006-01-20 09:37:15 +00:00
|
|
|
$allow = null;
|
|
|
|
|
|
|
|
if(!empty($allowed))
|
|
|
|
{
|
|
|
|
foreach ($allowed as $value)
|
|
|
|
{
|
|
|
|
$allow .= "\\$value";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(is_array($string))
|
|
|
|
{
|
2006-02-18 23:42:21 +00:00
|
|
|
foreach ($string as $key => $clean)
|
|
|
|
{
|
2006-01-20 09:37:15 +00:00
|
|
|
$cleaned[$key] = preg_replace( "/[^{$allow}a-zA-Z0-9]/", "", $clean);
|
2006-02-18 23:42:21 +00:00
|
|
|
}
|
2006-01-20 09:37:15 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$cleaned = preg_replace( "/[^{$allow}a-zA-Z0-9]/", "", $string );
|
|
|
|
}
|
|
|
|
return $cleaned;
|
2005-12-27 03:33:44 +00:00
|
|
|
}
|
2005-10-03 04:48:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Makes a string SQL-safe by adding slashes (if needed).
|
|
|
|
*
|
|
|
|
* @param string $string
|
|
|
|
* @return string
|
|
|
|
*/
|
2006-02-18 23:42:21 +00:00
|
|
|
function sql($string)
|
|
|
|
{
|
2005-10-03 04:48:00 +00:00
|
|
|
if (!ini_get('magic_quotes_gpc'))
|
|
|
|
{
|
|
|
|
$string = addslashes($string);
|
|
|
|
}
|
2006-01-20 09:37:15 +00:00
|
|
|
|
2005-10-03 04:48:00 +00:00
|
|
|
return $string;
|
2006-02-18 23:42:21 +00:00
|
|
|
}
|
2006-01-20 09:37:15 +00:00
|
|
|
|
2005-10-03 04:48:00 +00:00
|
|
|
/**
|
2006-02-18 23:42:21 +00:00
|
|
|
* Returns given string safe for display as HTML. Renders entities and converts newlines to <br />.
|
2005-10-03 04:48:00 +00:00
|
|
|
*
|
|
|
|
* @param string $string
|
2005-10-18 22:27:39 +00:00
|
|
|
* @param boolean $remove If true, the string is stripped of all HTML tags
|
2005-10-03 04:48:00 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2006-02-18 23:42:21 +00:00
|
|
|
function html($string, $remove = false)
|
|
|
|
{
|
2005-10-03 04:48:00 +00:00
|
|
|
if ($remove)
|
|
|
|
{
|
|
|
|
$string = strip_tags($string);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-02-18 23:42:21 +00:00
|
|
|
$patterns = array("/\&/", "/%/", "/</", "/>/", '/"/', "/'/", "/\(/", "/\)/", "/\+/", "/-/", "/\n/");
|
|
|
|
$replacements = array("&", "%", "<", ">", """, "'", "(", ")", "+", "-", "<br />");
|
2005-12-27 03:33:44 +00:00
|
|
|
$string = preg_replace($patterns, $replacements, $string);
|
2005-10-03 04:48:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $string;
|
2006-02-18 23:42:21 +00:00
|
|
|
}
|
2006-01-20 09:37:15 +00:00
|
|
|
|
2005-10-03 04:48:00 +00:00
|
|
|
/**
|
2005-10-18 22:27:39 +00:00
|
|
|
* Recursively sanitizes given array of data for safe input.
|
2005-10-03 04:48:00 +00:00
|
|
|
*
|
|
|
|
* @param mixed $toClean
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2006-02-18 23:42:21 +00:00
|
|
|
function cleanArray(&$toClean)
|
|
|
|
{
|
2005-10-03 04:48:00 +00:00
|
|
|
return $this->cleanArrayR($toClean);
|
2006-02-18 23:42:21 +00:00
|
|
|
}
|
2005-10-03 04:48:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Private method used for recursion (see cleanArray()).
|
|
|
|
*
|
|
|
|
* @param array $toClean
|
|
|
|
* @return array
|
2005-10-18 22:27:39 +00:00
|
|
|
* @see cleanArray
|
2005-10-03 04:48:00 +00:00
|
|
|
*/
|
2006-02-18 23:42:21 +00:00
|
|
|
function cleanArrayR(&$toClean)
|
|
|
|
{
|
2006-01-20 09:37:15 +00:00
|
|
|
if (is_array($toClean))
|
2005-10-03 04:48:00 +00:00
|
|
|
{
|
|
|
|
while(list($k, $v) = each($toClean))
|
|
|
|
{
|
2006-01-20 09:37:15 +00:00
|
|
|
if ( is_array($toClean[$k]) )
|
2005-10-03 04:48:00 +00:00
|
|
|
{
|
2006-02-18 23:42:21 +00:00
|
|
|
$this->cleanArray($toClean[$k]);
|
2006-01-20 09:37:15 +00:00
|
|
|
}
|
|
|
|
else
|
2005-10-03 04:48:00 +00:00
|
|
|
{
|
2006-02-18 23:42:21 +00:00
|
|
|
$toClean[$k] = $this->cleanValue($v);
|
2005-10-03 04:48:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-01-20 09:37:15 +00:00
|
|
|
else
|
2005-10-03 04:48:00 +00:00
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
2006-02-18 23:42:21 +00:00
|
|
|
}
|
2006-01-20 09:37:15 +00:00
|
|
|
|
2005-10-03 04:48:00 +00:00
|
|
|
/**
|
|
|
|
* Do we really need to sanitize array keys? If so, we can use this code...
|
|
|
|
|
2006-02-18 23:42:21 +00:00
|
|
|
function cleanKey($key)
|
|
|
|
{
|
2006-01-20 09:37:15 +00:00
|
|
|
if ($key == "")
|
2005-10-03 04:48:00 +00:00
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
2006-01-20 09:37:15 +00:00
|
|
|
|
2006-02-18 23:42:21 +00:00
|
|
|
//URL decode and convert chars to HTML entities
|
2005-10-03 04:48:00 +00:00
|
|
|
$key = htmlspecialchars(urldecode($key));
|
2006-02-18 23:42:21 +00:00
|
|
|
//Remove ..
|
2005-10-03 04:48:00 +00:00
|
|
|
$key = preg_replace( "/\.\./", "", $key );
|
2006-02-18 23:42:21 +00:00
|
|
|
//Remove __FILE__, etc.
|
2005-10-03 04:48:00 +00:00
|
|
|
$key = preg_replace( "/\_\_(.+?)\_\_/", "", $key );
|
2006-02-18 23:42:21 +00:00
|
|
|
//Trim word chars, '.', '-', '_'
|
2005-10-03 04:48:00 +00:00
|
|
|
$key = preg_replace( "/^([\w\.\-\_]+)$/", "$1", $key );
|
2006-01-20 09:37:15 +00:00
|
|
|
|
2005-10-03 04:48:00 +00:00
|
|
|
return $key;
|
2006-02-18 23:42:21 +00:00
|
|
|
}
|
2005-10-03 04:48:00 +00:00
|
|
|
*/
|
2006-01-20 09:37:15 +00:00
|
|
|
|
2005-10-03 04:48:00 +00:00
|
|
|
/**
|
2005-10-18 22:27:39 +00:00
|
|
|
* Method used by cleanArray() to sanitize array nodes.
|
2005-10-03 04:48:00 +00:00
|
|
|
*
|
|
|
|
* @param string $val
|
|
|
|
* @return string
|
|
|
|
*/
|
2006-02-18 23:42:21 +00:00
|
|
|
function cleanValue($val)
|
|
|
|
{
|
2005-10-03 04:48:00 +00:00
|
|
|
if ($val == "")
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2006-02-18 23:42:21 +00:00
|
|
|
//Replace odd spaces with safe ones
|
2005-10-03 04:48:00 +00:00
|
|
|
$val = str_replace(" ", " ", $val);
|
|
|
|
$val = str_replace(chr(0xCA), "", $val);
|
|
|
|
|
2006-02-18 23:42:21 +00:00
|
|
|
//Encode any HTML to entities (including \n --> <br />)
|
2005-10-03 04:48:00 +00:00
|
|
|
$val = $this->html($val);
|
|
|
|
|
2006-02-18 23:42:21 +00:00
|
|
|
//Double-check special chars and remove carriage returns
|
|
|
|
//For increased SQL security
|
2005-12-27 03:33:44 +00:00
|
|
|
$val = preg_replace( "/\\\$/" ,"$" ,$val);
|
|
|
|
$val = preg_replace( "/\r/" ,"" ,$val);
|
|
|
|
$val = str_replace ( "!" ,"!" ,$val);
|
|
|
|
$val = str_replace ( "'" , "'" ,$val);
|
2005-10-03 04:48:00 +00:00
|
|
|
|
2006-02-18 23:42:21 +00:00
|
|
|
//Allow unicode (?)
|
2005-10-03 04:48:00 +00:00
|
|
|
$val = preg_replace("/&#([0-9]+);/s", "&#\\1;", $val );
|
|
|
|
|
2006-02-18 23:42:21 +00:00
|
|
|
//Add slashes for SQL
|
2005-10-03 04:48:00 +00:00
|
|
|
$val = $this->sql($val);
|
|
|
|
|
2006-02-18 23:42:21 +00:00
|
|
|
//Swap user-inputted backslashes (?)
|
2005-10-03 04:48:00 +00:00
|
|
|
$val = preg_replace( "/\\\(?!&#|\?#)/", "\\", $val );
|
|
|
|
|
|
|
|
return $val;
|
2006-02-18 23:42:21 +00:00
|
|
|
}
|
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
|
|
|
|
* @return void
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function formatColumns(&$model)
|
|
|
|
{
|
|
|
|
foreach ($model->data as $name => $values)
|
|
|
|
{
|
|
|
|
if ($name == $model->name)
|
|
|
|
{
|
|
|
|
$curModel =& $model;
|
|
|
|
}
|
|
|
|
else if (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)
|
|
|
|
{
|
2006-03-15 20:45:59 +00:00
|
|
|
$db =& ConnectionManager::getDataSource($curModel->useDbConfig);
|
|
|
|
$colData = $db->columns[$colType];
|
|
|
|
|
2006-03-12 00:11:40 +00:00
|
|
|
if (isset($colData['limit']) && strlen(strval($data)) > $colData['limit'])
|
|
|
|
{
|
|
|
|
$data = substr(strval($data), 0, $colData['limit']);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($colData['formatter']) || isset($colData['format']))
|
|
|
|
{
|
|
|
|
switch(low($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
|
|
|
?>
|