File: escapingUtils.php

package info (click to toggle)
fusionforge 5.3.2%2B20141104-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 60,472 kB
  • sloc: php: 271,846; sql: 36,817; python: 14,575; perl: 6,406; sh: 5,980; xml: 4,294; pascal: 1,411; makefile: 911; cpp: 52; awk: 27
file content (365 lines) | stat: -rw-r--r-- 10,316 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
<?php
/**
 * FusionForge escaping library
 *
 * Copyright 2003-2004, Guillaume Smet
 *
 * This file is part of FusionForge. FusionForge is free software;
 * you can redistribute it and/or modify it under the terms of the
 * GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the Licence, or (at your option)
 * any later version.
 *
 * FusionForge is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with FusionForge; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

/**
 * getIntFromRequest - get an int from REQUEST
 *
 * @param string $key key of the wanted value
 * @param int $defaultValue if we can't find the wanted value, it returns the default value
 * @return int the value
 */
function getIntFromRequest($key, $defaultValue = 0) {
	return _getIntFromArray(_getRequestArray(), $key, $defaultValue);
}

/**
 * getStringFromRequest - get a string from REQUEST
 *
 * @param string $key key of the wanted value
 * @param string $defaultValue if we can't find the wanted value, it returns the default value
 * @return string the value
 */
function getStringFromRequest($key, $defaultValue = '') {
	return _getStringFromArray(_getRequestArray(), $key, $defaultValue);
}

/**
 * getHtmlStringFromRequest - get an HTML converted string from REQUEST
 *
 * @param string $key key of the wanted value
 * @param string $defaultValue if we can't find the wanted value, it returns the default value
 * @return string the value
 */
function getHtmlStringFromRequest($key, $defaultValue = '') {
	return htmlspecialchars(getStringFromRequest($key, $defaultValue));
}

/**
 * getHtmlTextFromRequest - get an HTML converted text string from REQUEST (same as string but for textarea)
 *
 * @param string $key key of the wanted value
 * @param string $defaultValue if we can't find the wanted value, it returns the default value
 * @return string the value
 */
function getHtmlTextFromRequest($key, $defaultValue = '') {
	$text = _getStringFromArray(_getRequestArray(), $key, $defaultValue);

	if (getStringFromRequest('_'.$key.'_content_type') == 'html') {
		return TextSanitizer::purify($text);
	} else {
		return htmlspecialchars($text);
	}
}

/**
 * getArrayFromRequest - get an array from REQUEST
 * @param	string $key	Key of the wanted value
 * @param	array $defaultValue	if we can't find the wanted value, it returns the default value
 * @return	array	The value
 */
function getArrayFromRequest($key, $defaultValue = array()) {
	return _getArrayFromArray(_getRequestArray(), $key, $defaultValue);
}

/**
 * getIntFromPost - get an int from POST
 *
 * @param string $key key of the wanted value
 * @param int $defaultValue if we can't find the wanted value, it returns the default value
 * @return int the value
 */
function getIntFromPost($key, $defaultValue = 0) {
	return _getIntFromArray(_getPostArray(), $key, $defaultValue);
}

/**
 * getStringFromPost - get a string from POST
 *
 * @param string $key key of the wanted value
 * @param string $defaultValue if we can't find the wanted value, it returns the default value
 * @return string the value
 */
function getStringFromPost($key, $defaultValue = '') {
	return _getStringFromArray(_getPostArray(), $key, $defaultValue);
}

/**
 * getIntFromGet - get an int from GET
 *
 * @param string $key key of the wanted value
 * @param int $defaultValue if we can't find the wanted value, it returns the default value
 * @return int the value
 */
function getIntFromGet($key, $defaultValue = 0) {
	return _getIntFromArray(_getGetArray(), $key, $defaultValue);
}

/**
 * getStringFromGet - get a string from GET
 *
 * @param string $key key of the wanted value
 * @param string $defaultValue if we can't find the wanted value, it returns the default value
 * @return string the value
 */
function getStringFromGet($key, $defaultValue = '') {
	return _getStringFromArray(_getGetArray(), $key, $defaultValue);
}

/**
 * getIntFromCookie - get an int set by a cookie
 *
 * @param string $key key of the wanted value
 * @param int $defaultValue if we can't find the wanted value, it returns the default value
 * @return int the value
 */
function getIntFromCookie($key, $defaultValue = 0) {
	return _getIntFromArray(_getCookieArray(), $key, $defaultValue);
}

/**
 * getStringFromCookie - get a string set by a cookie
 *
 * @param string $key key of the wanted value
 * @param string $defaultValue if we can't find the wanted value, it returns the default value
 * @return string the value
 */
function getStringFromCookie($key, $defaultValue = '') {
	return _getStringFromArray(_getCookieArray(), $key, $defaultValue);
}

/**
 * getUploadedFile - get the uploaded file information
 *
 * The returned array is in the format given by PHP, as described in
 * http://php.net/manual/en/features.file-upload.php
 *
 * If there was no such file upload control in form, empty array is
 * returned.  If there was file upload control but no file was
 * entered, then $result['tmp_name'] is empty string.
 *
 * @param string $key	name of the file
 * @return array uploaded file information
 */
function getUploadedFile($key) {
	$filesArray = & _getFilesArray();
	if(isset($filesArray[$key])) {
		$result = $filesArray[$key];
		if ($result['tmp_name'] == 'none') {
			$result['tmp_name'] = '';
		}
		return $result;
	}
	else {
		return array();
	}
}

/**
 * getStringFromServer - get a string from Server environment
 *
 * @param string $key key of the wanted value
 * @return string the value
 */
function getStringFromServer($key) {
	$serverArray = & _getServerArray();
	if(isset($serverArray[$key])) {
		return $serverArray[$key];
	}
	else {
		return '';
	}
}

/* private */

/**
 * _getIntFromArray - get an int from an array
 *
 * @param array $array the array
 * @param string $key the key of the wanted value
 * @param int $defaultValue an int which is returned if we can't find the key in the array
 * @return int the wanted value
 */
function _getIntFromArray( $array, $key, $defaultValue = 0) {
	if(isset($array[$key]) && is_numeric($array[$key]) &&
		$array[$key] <= 2147483647 && $array[$key] >= -2147483648 ) {
		return (int) $array[$key];
	}
	elseif(is_numeric($defaultValue)) {
		return (int) $defaultValue;
	}
	else {
		return 0;
	}
}

/**
 * _getStringFromArray - get a string from an array
 *
 * @param array $array the array
 * @param string $key the key of the wanted value
 * @param string $defaultValue an int which is returned if we can't find the key in the array
 * @return string the wanted value
 */
function _getStringFromArray( $array, $key, $defaultValue = '') {
	if(isset($array[$key])) {
		return $array[$key];
	}
	else {
		return $defaultValue;
	}
}

/**
 * _getArrayFromArray - get an array from another array
 *
 * @param array $array the array
 * @param string $key the key of the wanted value
 * @param array $defaultValue an array which is returned if we can't find the key in the array
 * @return string the wanted value
 */
function _getArrayFromArray( $array, $key, $defaultValue = array()) {
	if(isset($array[$key])) {
		return $array[$key];
	}
	else {
		return $defaultValue;
	}
}

/**
 * _getPredefinedArray - get one of the predefined array (GET, POST, COOKIE...)
 *
 * @param string $superGlobalName name of the super global array (_POST, _GET)
 * @param string $oldName name of the old array (HTTP_POST_VARS, HTTP_GET_VARS) for older php versions
 * @return array a predefined array
 */
function & _getPredefinedArray($superGlobalName, $oldName) {
	if(isset($GLOBALS[$superGlobalName])) {
		$array = & $GLOBALS[$superGlobalName];
	} elseif(isset($GLOBALS[$oldName])) {
		$array = & $GLOBALS[$oldName];
	} else {
		$array = array();
	}
	return $array;
}

/**
 * _getRequestArray - wrapper to get the request array
 *
 * @return array the REQUEST array
 */
function & _getRequestArray() {
	if(isset($_REQUEST)) {
		return $_REQUEST;
	} else {
		return array_merge($GLOBALS['HTTP_GET_VARS'], $GLOBALS['HTTP_POST_VARS'], $GLOBALS['HTTP_COOKIE_VARS']);
	}
}

/**
 * _getPostArray - wrapper to get the post array
 *
 * @return array the POST array
 */
function & _getPostArray() {
	return _getPredefinedArray('_POST', 'HTTP_POST_VARS');
}

/**
 * _getPostArray - wrapper to get the GET array
 *
 * @return array the GET array
 */
function & _getGetArray() {
	return _getPredefinedArray('_GET', 'HTTP_GET_VARS');
}

/**
 * _getFilesArray - wrapper to get the FILES array
 *
 * @return array the FILES array
 */
function & _getFilesArray() {
	return _getPredefinedArray('_FILES', 'HTTP_POST_FILES');
}

/**
 * _getServerArray - wrapper to get the SERVER array
 *
 * @return array the SERVER array
 */
function & _getServerArray() {
	return _getPredefinedArray('_SERVER', 'HTTP_SERVER_VARS');
}

/**
 * _getCookieArray - wrapper to get the post array
 *
 * @return array the COOKIE array
 */
function & _getCookieArray() {
	return _getPredefinedArray('_COOKIE', 'HTTP_COOKIE_VARS');
}

/**
* inputSpecialChars - escape a string which is in an input
*
* @param string $string string to escape
* @return string escaped string
*/
function inputSpecialChars($string) {
	return str_replace('"', '&quot;', $string);
}

/**
* unInputSpecialChars - clean a string escaped with inputSpecialChars
*
* @param string $string escaped string
* @return string clean string
*/
function unInputSpecialChars($string) {
	return str_replace('&quot;', '"', $string);
}

/**
 * getFilteredStringFromRequest - get a string from REQUEST
 *
 * @param string $string key of the wanted value
 * @param string $pattern Regular expression of allowed values.
 * @param string $defaultValue if we can't find the wanted value, it returns the default value
 * @return string the value or false if not valid.
 */
function getFilteredStringFromRequest($string, $pattern, $defaultValue = '') {
	$value = getStringFromRequest($string, $defaultValue);
	if (preg_match($pattern, $value)) {
		return $value;
	} else {
		return $defaultValue;
	}
}

// Local Variables:
// mode: php
// c-file-style: "bsd"
// End: