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 HTML converted 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 HTML converted 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 array $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 $key 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 * @return string the value
182 function getStringFromServer($key) {
183 $serverArray = & _getServerArray();
184 if(isset($serverArray[$key])) {
185 return $serverArray[$key];
195 * _getIntFromArray - get an int from an array
197 * @param array $array the array
198 * @param string $key the key of the wanted value
199 * @param int $defaultValue an int which is returned if we can't find the key in the array
200 * @return int the wanted value
202 function _getIntFromArray( $array, $key, $defaultValue = 0) {
203 if(isset($array[$key]) && is_numeric($array[$key]) &&
204 $array[$key] <= 2147483647 && $array[$key] >= -2147483648 ) {
205 return (int) $array[$key];
207 elseif(is_numeric($defaultValue)) {
208 return (int) $defaultValue;
216 * _getStringFromArray - get a string from an array
218 * @param array $array the array
219 * @param string $key the key of the wanted value
220 * @param string $defaultValue an int which is returned if we can't find the key in the array
221 * @return string the wanted value
223 function _getStringFromArray( $array, $key, $defaultValue = '') {
224 if(isset($array[$key])) {
228 return $defaultValue;
233 * _getArrayFromArray - get an array from another array
235 * @param array $array the array
236 * @param string $key the key of the wanted value
237 * @param array $defaultValue an array which is returned if we can't find the key in the array
238 * @return string the wanted value
240 function _getArrayFromArray( $array, $key, $defaultValue = array()) {
241 if(isset($array[$key])) {
245 return $defaultValue;
250 * _getPredefinedArray - get one of the predefined array (GET, POST, COOKIE...)
252 * @param string $superGlobalName name of the super global array (_POST, _GET)
253 * @param string $oldName name of the old array (HTTP_POST_VARS, HTTP_GET_VARS) for older php versions
254 * @return array a predefined array
256 function & _getPredefinedArray($superGlobalName, $oldName) {
257 if(isset($GLOBALS[$superGlobalName])) {
258 $array = & $GLOBALS[$superGlobalName];
259 } elseif(isset($GLOBALS[$oldName])) {
260 $array = & $GLOBALS[$oldName];
268 * _getRequestArray - wrapper to get the request array
270 * @return array the REQUEST array
272 function & _getRequestArray() {
273 if(isset($_REQUEST)) {
276 return array_merge($GLOBALS['HTTP_GET_VARS'], $GLOBALS['HTTP_POST_VARS'], $GLOBALS['HTTP_COOKIE_VARS']);
281 * _getPostArray - wrapper to get the post array
283 * @return array the POST array
285 function & _getPostArray() {
286 return _getPredefinedArray('_POST', 'HTTP_POST_VARS');
290 * _getPostArray - wrapper to get the GET array
292 * @return array the GET array
294 function & _getGetArray() {
295 return _getPredefinedArray('_GET', 'HTTP_GET_VARS');
299 * _getFilesArray - wrapper to get the FILES array
301 * @return array the FILES array
303 function & _getFilesArray() {
304 return _getPredefinedArray('_FILES', 'HTTP_POST_FILES');
308 * _getServerArray - wrapper to get the SERVER array
310 * @return array the SERVER array
312 function & _getServerArray() {
313 return _getPredefinedArray('_SERVER', 'HTTP_SERVER_VARS');
317 * _getCookieArray - wrapper to get the post array
319 * @return array the COOKIE array
321 function & _getCookieArray() {
322 return _getPredefinedArray('_COOKIE', 'HTTP_COOKIE_VARS');
326 * inputSpecialChars - escape a string which is in an input
328 * @param string $string string to escape
329 * @return string escaped string
331 function inputSpecialChars($string) {
332 return str_replace('"', '"', $string);
336 * unInputSpecialChars - clean a string escaped with inputSpecialChars
338 * @param string $string escaped string
339 * @return string clean string
341 function unInputSpecialChars($string) {
342 return str_replace('"', '"', $string);
346 * getFilteredStringFromRequest - get a string from REQUEST
348 * @param string $string key of the wanted value
349 * @param string $pattern Regular expression of allowed values.
350 * @param string $defaultValue if we can't find the wanted value, it returns the default value
351 * @return string the value or false if not valid.
353 function getFilteredStringFromRequest($string, $pattern, $defaultValue = '') {
354 $value = getStringFromRequest($string, $defaultValue);
355 if (preg_match($pattern, $value)) {
358 return $defaultValue;
364 // c-file-style: "bsd"