3 * FusionForge escaping library
5 * Copyright 2003-2004, Guillaume Smet
7 * This file is part of FusionForge. FusionForge is free software;
8 * you can redistribute it and/or modify it under the terms of the
9 * GNU General Public License as published by the Free Software
10 * Foundation; either version 2 of the Licence, or (at your option)
13 * FusionForge is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with FusionForge; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * getIntFromRequest - get an int from REQUEST
26 * @param string $key key of the wanted value
27 * @param int $defaultValue if we can't find the wanted value, it returns the default value
28 * @return int the value
30 function getIntFromRequest($key, $defaultValue = 0) {
31 return _getIntFromArray(_getRequestArray(), $key, $defaultValue);
35 * getStringFromRequest - get a string from REQUEST
37 * @param string $key key of the wanted value
38 * @param string $defaultValue if we can't find the wanted value, it returns the default value
39 * @return string the value
41 function getStringFromRequest($key, $defaultValue = '') {
42 return _getStringFromArray(_getRequestArray(), $key, $defaultValue);
46 * getHtmlStringFromRequest - get an htmlified string from REQUEST
48 * @param string $key key of the wanted value
49 * @param string $defaultValue if we can't find the wanted value, it returns the default value
50 * @return string the value
52 function getHtmlStringFromRequest($key, $defaultValue = '') {
53 return htmlspecialchars(getStringFromRequest($key, $defaultValue));
57 * getHtmlTextFromRequest - get an htmlified text string from REQUEST (same as string but for textarea)
59 * @param string $key key of the wanted value
60 * @param string $defaultValue if we can't find the wanted value, it returns the default value
61 * @return string the value
63 function getHtmlTextFromRequest($key, $defaultValue = '') {
64 $text = _getStringFromArray(_getRequestArray(), $key, $defaultValue);
66 if (getStringFromRequest('_'.$key.'_content_type') == 'html') {
67 return TextSanitizer::purify($text);
69 return htmlspecialchars($text);
74 * getArrayFromRequest - get an array from REQUEST
75 * @param string $key Key of the wanted value
76 * @param string $defaultValue if we can't find the wanted value, it returns the default value
77 * @return array The value
79 function getArrayFromRequest($key, $defaultValue = array()) {
80 return _getArrayFromArray(_getRequestArray(), $key, $defaultValue);
84 * getIntFromPost - get an int from POST
86 * @param string $key key of the wanted value
87 * @param int $defaultValue if we can't find the wanted value, it returns the default value
88 * @return int the value
90 function getIntFromPost($key, $defaultValue = 0) {
91 return _getIntFromArray(_getPostArray(), $key, $defaultValue);
95 * getStringFromPost - get a string from POST
97 * @param string $key key of the wanted value
98 * @param string $defaultValue if we can't find the wanted value, it returns the default value
99 * @return string the value
101 function getStringFromPost($key, $defaultValue = '') {
102 return _getStringFromArray(_getPostArray(), $key, $defaultValue);
106 * getIntFromGet - get an int from GET
108 * @param string $key key of the wanted value
109 * @param int $defaultValue if we can't find the wanted value, it returns the default value
110 * @return int the value
112 function getIntFromGet($key, $defaultValue = 0) {
113 return _getIntFromArray(_getGetArray(), $key, $defaultValue);
117 * getStringFromGet - get a string from GET
119 * @param string $key key of the wanted value
120 * @param string $defaultValue if we can't find the wanted value, it returns the default value
121 * @return string the value
123 function getStringFromGet($key, $defaultValue = '') {
124 return _getStringFromArray(_getGetArray(), $key, $defaultValue);
128 * getIntFromCookie - get an int set by a cookie
130 * @param string $key key of the wanted value
131 * @param int $defaultValue if we can't find the wanted value, it returns the default value
132 * @return int the value
134 function getIntFromCookie($key, $defaultValue = 0) {
135 return _getIntFromArray(_getCookieArray(), $key, $defaultValue);
139 * getStringFromCookie - get a string set by a cookie
141 * @param string $key key of the wanted value
142 * @param string $defaultValue if we can't find the wanted value, it returns the default value
143 * @return string the value
145 function getStringFromCookie($key, $defaultValue = '') {
146 return _getStringFromArray(_getCookieArray(), $key, $defaultValue);
150 * getUploadedFile - get the uploaded file information
152 * The returned array is in the format given by PHP, as described in
153 * http://php.net/manual/en/features.file-upload.php
155 * If there was no such file upload control in form, empty array is
156 * returned. If there was file upload control but no file was
157 * entered, then $result['tmp_name'] is empty string.
159 * @param string name of the file
160 * @return array uploaded file information
162 function getUploadedFile($key) {
163 $filesArray = & _getFilesArray();
164 if(isset($filesArray[$key])) {
165 $result = $filesArray[$key];
166 if ($result['tmp_name'] == 'none') {
167 $result['tmp_name'] = '';
177 * getStringFromServer - get a string from Server environment
179 * @param string $key key of the wanted value
180 * @param string $defaultValue if we can't find the wanted value, it returns the default value
181 * @return string the value
183 function getStringFromServer($key) {
184 $serverArray = & _getServerArray();
185 if(isset($serverArray[$key])) {
186 return $serverArray[$key];
196 * _getIntFromArray - get an int from an array
198 * @param array $array the array
199 * @param string $key the key of the wanted value
200 * @param int $defaultValue an int which is returned if we can't find the key in the array
201 * @return int the wanted value
203 function _getIntFromArray( $array, $key, $defaultValue = 0) {
204 if(isset($array[$key]) && is_numeric($array[$key]) &&
205 $array[$key] <= 2147483647 && $array[$key] >= -2147483648 ) {
206 return (int) $array[$key];
208 elseif(is_numeric($defaultValue)) {
209 return (int) $defaultValue;
217 * _getStringFromArray - get a string from an array
219 * @param array $array the array
220 * @param string $key the key of the wanted value
221 * @param int $defaultValue an int which is returned if we can't find the key in the array
222 * @return string the wanted value
224 function _getStringFromArray( $array, $key, $defaultValue = '') {
225 if(isset($array[$key])) {
229 return $defaultValue;
234 * _getArrayFromArray - get an array from another array
236 * @param array $array the array
237 * @param string $key the key of the wanted value
238 * @param int $defaultValue an array which is returned if we can't find the key in the array
239 * @return string the wanted value
241 function _getArrayFromArray( $array, $key, $defaultValue = array()) {
242 if(isset($array[$key])) {
246 return $defaultValue;
251 * _getPredefinedArray - get one of the predefined array (GET, POST, COOKIE...)
253 * @param string $superGlobalName name of the super global array (_POST, _GET)
254 * @param string $oldName name of the old array (HTTP_POST_VARS, HTTP_GET_VARS) for older php versions
255 * @return array a predefined array
257 function & _getPredefinedArray($superGlobalName, $oldName) {
258 if(isset($GLOBALS[$superGlobalName])) {
259 $array = & $GLOBALS[$superGlobalName];
260 } elseif(isset($GLOBALS[$oldName])) {
261 $array = & $GLOBALS[$oldName];
269 * _getRequestArray - wrapper to get the request array
271 * @return array the REQUEST array
273 function & _getRequestArray() {
274 if(isset($_REQUEST)) {
277 return array_merge($GLOBALS['HTTP_GET_VARS'], $GLOBALS['HTTP_POST_VARS'], $GLOBALS['HTTP_COOKIE_VARS']);
282 * _getPostArray - wrapper to get the post array
284 * @return array the POST array
286 function & _getPostArray() {
287 return _getPredefinedArray('_POST', 'HTTP_POST_VARS');
291 * _getPostArray - wrapper to get the GET array
293 * @return array the GET array
295 function & _getGetArray() {
296 return _getPredefinedArray('_GET', 'HTTP_GET_VARS');
300 * _getFilesArray - wrapper to get the FILES array
302 * @return array the FILES array
304 function & _getFilesArray() {
305 return _getPredefinedArray('_FILES', 'HTTP_POST_FILES');
309 * _getServerArray - wrapper to get the SERVER array
311 * @return array the SERVER array
313 function & _getServerArray() {
314 return _getPredefinedArray('_SERVER', 'HTTP_SERVER_VARS');
318 * _getCookieArray - wrapper to get the post array
320 * @return array the COOKIE array
322 function & _getCookieArray() {
323 return _getPredefinedArray('_COOKIE', 'HTTP_COOKIE_VARS');
327 * inputSpecialchars - escape a string which is in an input
329 * @param string $string string to escape
330 * @return string escaped string
332 function inputSpecialchars($string) {
333 return str_replace('"', '"', $string);
337 * unInputSpecialchars - clean a string escaped with inputSpecialchars
339 * @param string $string escaped string
340 * @return string clean string
342 function unInputSpecialchars($string) {
343 return str_replace('"', '"', $string);
347 * getFilteredStringFromRequest - get a string from REQUEST
349 * @param string $key key of the wanted value
350 * @param string $pattern Regular expression of allowed values.
351 * @param string $defaultValue if we can't find the wanted value, it returns the default value
352 * @return string the value or false if not valid.
354 function getFilteredStringFromRequest($string, $pattern, $defaultValue = '') {
355 $value = getStringFromRequest($string, $defaultValue);
356 if (preg_match($pattern, $value)) {
359 return $defaultValue;
365 // c-file-style: "bsd"