| 1 |
lars |
1 |
<?php
|
|
|
2 |
/* SVN FILE: $Id: sanitize.php 7945 2008-12-19 02:16:01Z gwoo $ */
|
|
|
3 |
/**
|
|
|
4 |
* Washes strings from unwanted noise.
|
|
|
5 |
*
|
|
|
6 |
* Helpful methods to make unsafe strings usable.
|
|
|
7 |
*
|
|
|
8 |
* PHP versions 4 and 5
|
|
|
9 |
*
|
|
|
10 |
* CakePHP(tm) : Rapid Development Framework (http://www.cakephp.org)
|
|
|
11 |
* Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
|
|
|
12 |
*
|
|
|
13 |
* Licensed under The MIT License
|
|
|
14 |
* Redistributions of files must retain the above copyright notice.
|
|
|
15 |
*
|
|
|
16 |
* @filesource
|
|
|
17 |
* @copyright Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
|
|
|
18 |
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
|
|
|
19 |
* @package cake
|
|
|
20 |
* @subpackage cake.cake.libs
|
|
|
21 |
* @since CakePHP(tm) v 0.10.0.1076
|
|
|
22 |
* @version $Revision: 7945 $
|
|
|
23 |
* @modifiedby $LastChangedBy: gwoo $
|
|
|
24 |
* @lastmodified $Date: 2008-12-18 18:16:01 -0800 (Thu, 18 Dec 2008) $
|
|
|
25 |
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
|
|
|
26 |
*/
|
|
|
27 |
/**
|
|
|
28 |
* Data Sanitization.
|
|
|
29 |
*
|
|
|
30 |
* Removal of alpahnumeric characters, SQL-safe slash-added strings, HTML-friendly strings,
|
|
|
31 |
* and all of the above on arrays.
|
|
|
32 |
*
|
|
|
33 |
* @package cake
|
|
|
34 |
* @subpackage cake.cake.libs
|
|
|
35 |
*/
|
|
|
36 |
class Sanitize {
|
|
|
37 |
/**
|
|
|
38 |
* Removes any non-alphanumeric characters.
|
|
|
39 |
*
|
|
|
40 |
* @param string $string String to sanitize
|
|
|
41 |
* @return string Sanitized string
|
|
|
42 |
* @access public
|
|
|
43 |
* @static
|
|
|
44 |
*/
|
|
|
45 |
function paranoid($string, $allowed = array()) {
|
|
|
46 |
$allow = null;
|
|
|
47 |
if (!empty($allowed)) {
|
|
|
48 |
foreach ($allowed as $value) {
|
|
|
49 |
$allow .= "\\$value";
|
|
|
50 |
}
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
if (is_array($string)) {
|
|
|
54 |
$cleaned = array();
|
|
|
55 |
foreach ($string as $key => $clean) {
|
|
|
56 |
$cleaned[$key] = preg_replace("/[^{$allow}a-zA-Z0-9]/", '', $clean);
|
|
|
57 |
}
|
|
|
58 |
} else {
|
|
|
59 |
$cleaned = preg_replace("/[^{$allow}a-zA-Z0-9]/", '', $string);
|
|
|
60 |
}
|
|
|
61 |
return $cleaned;
|
|
|
62 |
}
|
|
|
63 |
/**
|
|
|
64 |
* Makes a string SQL-safe.
|
|
|
65 |
*
|
|
|
66 |
* @param string $string String to sanitize
|
|
|
67 |
* @param string $connection Database connection being used
|
|
|
68 |
* @return string SQL safe string
|
|
|
69 |
* @access public
|
|
|
70 |
* @static
|
|
|
71 |
*/
|
|
|
72 |
function escape($string, $connection = 'default') {
|
|
|
73 |
$db =& ConnectionManager::getDataSource($connection);
|
|
|
74 |
if (is_numeric($string) || $string === null || is_bool($string)) {
|
|
|
75 |
return $string;
|
|
|
76 |
}
|
|
|
77 |
$string = substr($db->value($string), 1);
|
|
|
78 |
$string = substr($string, 0, -1);
|
|
|
79 |
return $string;
|
|
|
80 |
}
|
|
|
81 |
/**
|
|
|
82 |
* Returns given string safe for display as HTML. Renders entities.
|
|
|
83 |
*
|
|
|
84 |
* @param string $string String from where to strip tags
|
|
|
85 |
* @param boolean $remove If true, the string is stripped of all HTML tags
|
|
|
86 |
* @return string Sanitized string
|
|
|
87 |
* @access public
|
|
|
88 |
* @static
|
|
|
89 |
*/
|
|
|
90 |
function html($string, $remove = false) {
|
|
|
91 |
if ($remove) {
|
|
|
92 |
$string = strip_tags($string);
|
|
|
93 |
} else {
|
|
|
94 |
$patterns = array("/\&/", "/%/", "/</", "/>/", '/"/', "/'/", "/\(/", "/\)/", "/\+/", "/-/");
|
|
|
95 |
$replacements = array("&", "%", "<", ">", """, "'", "(", ")", "+", "-");
|
|
|
96 |
$string = preg_replace($patterns, $replacements, $string);
|
|
|
97 |
}
|
|
|
98 |
return $string;
|
|
|
99 |
}
|
|
|
100 |
/**
|
|
|
101 |
* Strips extra whitespace from output
|
|
|
102 |
*
|
|
|
103 |
* @param string $str String to sanitize
|
|
|
104 |
* @return string whitespace sanitized string
|
|
|
105 |
* @access public
|
|
|
106 |
* @static
|
|
|
107 |
*/
|
|
|
108 |
function stripWhitespace($str) {
|
|
|
109 |
$r = preg_replace('/[\n\r\t]+/', '', $str);
|
|
|
110 |
return preg_replace('/\s{2,}/', ' ', $r);
|
|
|
111 |
}
|
|
|
112 |
/**
|
|
|
113 |
* Strips image tags from output
|
|
|
114 |
*
|
|
|
115 |
* @param string $str String to sanitize
|
|
|
116 |
* @return string Sting with images stripped.
|
|
|
117 |
* @access public
|
|
|
118 |
* @static
|
|
|
119 |
*/
|
|
|
120 |
function stripImages($str) {
|
|
|
121 |
$str = preg_replace('/(<a[^>]*>)(<img[^>]+alt=")([^"]*)("[^>]*>)(<\/a>)/i', '$1$3$5<br />', $str);
|
|
|
122 |
$str = preg_replace('/(<img[^>]+alt=")([^"]*)("[^>]*>)/i', '$2<br />', $str);
|
|
|
123 |
$str = preg_replace('/<img[^>]*>/i', '', $str);
|
|
|
124 |
return $str;
|
|
|
125 |
}
|
|
|
126 |
/**
|
|
|
127 |
* Strips scripts and stylesheets from output
|
|
|
128 |
*
|
|
|
129 |
* @param string $str String to sanitize
|
|
|
130 |
* @return string String with <script>, <style>, <link> elements removed.
|
|
|
131 |
* @access public
|
|
|
132 |
* @static
|
|
|
133 |
*/
|
|
|
134 |
function stripScripts($str) {
|
|
|
135 |
return preg_replace('/(<link[^>]+rel="[^"]*stylesheet"[^>]*>|<img[^>]*>|style="[^"]*")|<script[^>]*>.*?<\/script>|<style[^>]*>.*?<\/style>|<!--.*?-->/i', '', $str);
|
|
|
136 |
}
|
|
|
137 |
/**
|
|
|
138 |
* Strips extra whitespace, images, scripts and stylesheets from output
|
|
|
139 |
*
|
|
|
140 |
* @param string $str String to sanitize
|
|
|
141 |
* @return string sanitized string
|
|
|
142 |
* @access public
|
|
|
143 |
*/
|
|
|
144 |
function stripAll($str) {
|
|
|
145 |
$str = Sanitize::stripWhitespace($str);
|
|
|
146 |
$str = Sanitize::stripImages($str);
|
|
|
147 |
$str = Sanitize::stripScripts($str);
|
|
|
148 |
return $str;
|
|
|
149 |
}
|
|
|
150 |
/**
|
|
|
151 |
* Strips the specified tags from output. First parameter is string from
|
|
|
152 |
* where to remove tags. All subsequent parameters are tags.
|
|
|
153 |
*
|
|
|
154 |
* @param string $str String to sanitize
|
|
|
155 |
* @param string $tag Tag to remove (add more parameters as needed)
|
|
|
156 |
* @return string sanitized String
|
|
|
157 |
* @access public
|
|
|
158 |
* @static
|
|
|
159 |
*/
|
|
|
160 |
function stripTags() {
|
|
|
161 |
$params = params(func_get_args());
|
|
|
162 |
$str = $params[0];
|
|
|
163 |
|
|
|
164 |
for ($i = 1; $i < count($params); $i++) {
|
|
|
165 |
$str = preg_replace('/<' . $params[$i] . '\b[^>]*>/i', '', $str);
|
|
|
166 |
$str = preg_replace('/<\/' . $params[$i] . '[^>]*>/i', '', $str);
|
|
|
167 |
}
|
|
|
168 |
return $str;
|
|
|
169 |
}
|
|
|
170 |
/**
|
|
|
171 |
* Sanitizes given array or value for safe input. Use the options to specify
|
|
|
172 |
* the connection to use, and what filters should be applied (with a boolean
|
|
|
173 |
* value). Valid filters: odd_spaces, encode, dollar, carriage, unicode,
|
|
|
174 |
* escape, backslash.
|
|
|
175 |
*
|
|
|
176 |
* @param mixed $data Data to sanitize
|
|
|
177 |
* @param mixed $options If string, DB connection being used, otherwise set of options
|
|
|
178 |
* @return mixed Sanitized data
|
|
|
179 |
* @access public
|
|
|
180 |
* @static
|
|
|
181 |
*/
|
|
|
182 |
function clean($data, $options = array()) {
|
|
|
183 |
if (empty($data)) {
|
|
|
184 |
return $data;
|
|
|
185 |
}
|
|
|
186 |
|
|
|
187 |
if (is_string($options)) {
|
|
|
188 |
$options = array('connection' => $options);
|
|
|
189 |
} else if (!is_array($options)) {
|
|
|
190 |
$options = array();
|
|
|
191 |
}
|
|
|
192 |
|
|
|
193 |
$options = array_merge(array(
|
|
|
194 |
'connection' => 'default',
|
|
|
195 |
'odd_spaces' => true,
|
|
|
196 |
'encode' => true,
|
|
|
197 |
'dollar' => true,
|
|
|
198 |
'carriage' => true,
|
|
|
199 |
'unicode' => true,
|
|
|
200 |
'escape' => true,
|
|
|
201 |
'backslash' => true
|
|
|
202 |
), $options);
|
|
|
203 |
|
|
|
204 |
if (is_array($data)) {
|
|
|
205 |
foreach ($data as $key => $val) {
|
|
|
206 |
$data[$key] = Sanitize::clean($val, $options);
|
|
|
207 |
}
|
|
|
208 |
return $data;
|
|
|
209 |
} else {
|
|
|
210 |
if ($options['odd_spaces']) {
|
|
|
211 |
$data = str_replace(chr(0xCA), '', str_replace(' ', ' ', $data));
|
|
|
212 |
}
|
|
|
213 |
if ($options['encode']) {
|
|
|
214 |
$data = Sanitize::html($data);
|
|
|
215 |
}
|
|
|
216 |
if ($options['dollar']) {
|
|
|
217 |
$data = str_replace("\\\$", "$", $data);
|
|
|
218 |
}
|
|
|
219 |
if ($options['carriage']) {
|
|
|
220 |
$data = str_replace("\r", "", $data);
|
|
|
221 |
}
|
|
|
222 |
|
|
|
223 |
$data = str_replace("'", "'", str_replace("!", "!", $data));
|
|
|
224 |
|
|
|
225 |
if ($options['unicode']) {
|
|
|
226 |
$data = preg_replace("/&#([0-9]+);/s", "&#\\1;", $data);
|
|
|
227 |
}
|
|
|
228 |
if ($options['escape']) {
|
|
|
229 |
$data = Sanitize::escape($data, $options['connection']);
|
|
|
230 |
}
|
|
|
231 |
if ($options['backslash']) {
|
|
|
232 |
$data = preg_replace("/\\\(?!&#|\?#)/", "\\", $data);
|
|
|
233 |
}
|
|
|
234 |
return $data;
|
|
|
235 |
}
|
|
|
236 |
}
|
|
|
237 |
/**
|
|
|
238 |
* Formats column data from definition in DBO's $columns array
|
|
|
239 |
*
|
|
|
240 |
* @param Model $model The model containing the data to be formatted
|
|
|
241 |
* @access public
|
|
|
242 |
* @static
|
|
|
243 |
*/
|
|
|
244 |
function formatColumns(&$model) {
|
|
|
245 |
foreach ($model->data as $name => $values) {
|
|
|
246 |
if ($name == $model->alias) {
|
|
|
247 |
$curModel =& $model;
|
|
|
248 |
} elseif (isset($model->{$name}) && is_object($model->{$name}) && is_subclass_of($model->{$name}, 'Model')) {
|
|
|
249 |
$curModel =& $model->{$name};
|
|
|
250 |
} else {
|
|
|
251 |
$curModel = null;
|
|
|
252 |
}
|
|
|
253 |
|
|
|
254 |
if ($curModel != null) {
|
|
|
255 |
foreach ($values as $column => $data) {
|
|
|
256 |
$colType = $curModel->getColumnType($column);
|
|
|
257 |
|
|
|
258 |
if ($colType != null) {
|
|
|
259 |
$db =& ConnectionManager::getDataSource($curModel->useDbConfig);
|
|
|
260 |
$colData = $db->columns[$colType];
|
|
|
261 |
|
|
|
262 |
if (isset($colData['limit']) && strlen(strval($data)) > $colData['limit']) {
|
|
|
263 |
$data = substr(strval($data), 0, $colData['limit']);
|
|
|
264 |
}
|
|
|
265 |
|
|
|
266 |
if (isset($colData['formatter']) || isset($colData['format'])) {
|
|
|
267 |
|
|
|
268 |
switch (strtolower($colData['formatter'])) {
|
|
|
269 |
case 'date':
|
|
|
270 |
$data = date($colData['format'], strtotime($data));
|
|
|
271 |
break;
|
|
|
272 |
case 'sprintf':
|
|
|
273 |
$data = sprintf($colData['format'], $data);
|
|
|
274 |
break;
|
|
|
275 |
case 'intval':
|
|
|
276 |
$data = intval($data);
|
|
|
277 |
break;
|
|
|
278 |
case 'floatval':
|
|
|
279 |
$data = floatval($data);
|
|
|
280 |
break;
|
|
|
281 |
}
|
|
|
282 |
}
|
|
|
283 |
$model->data[$name][$column]=$data;
|
|
|
284 |
/*
|
|
|
285 |
switch ($colType) {
|
|
|
286 |
case 'integer':
|
|
|
287 |
case 'int':
|
|
|
288 |
return $data;
|
|
|
289 |
break;
|
|
|
290 |
case 'string':
|
|
|
291 |
case 'text':
|
|
|
292 |
case 'binary':
|
|
|
293 |
case 'date':
|
|
|
294 |
case 'time':
|
|
|
295 |
case 'datetime':
|
|
|
296 |
case 'timestamp':
|
|
|
297 |
case 'date':
|
|
|
298 |
return "'" . $data . "'";
|
|
|
299 |
break;
|
|
|
300 |
}
|
|
|
301 |
*/
|
|
|
302 |
}
|
|
|
303 |
}
|
|
|
304 |
}
|
|
|
305 |
}
|
|
|
306 |
}
|
|
|
307 |
}
|
|
|
308 |
?>
|