2 /************************************************************************
3 * Mailer v0.2.1-FINAL Start: 02/28/2009 *
4 * =================== Last change: 02/28/2009 *
6 * -------------------------------------------------------------------- *
7 * File : request-functions.php *
8 * -------------------------------------------------------------------- *
9 * Short description : Special functions for request handling *
10 * -------------------------------------------------------------------- *
11 * Kurzbeschreibung : Spezialle Funktionen fuer die Anfragebehandlung *
12 * -------------------------------------------------------------------- *
15 * $Tag:: 0.2.1-FINAL $ *
17 * -------------------------------------------------------------------- *
18 * Copyright (c) 2003 - 2009 by Roland Haeder *
19 * Copyright (c) 2009 - 2013 by Mailer Developer Team *
20 * For more information visit: http://mxchange.org *
22 * This program is free software; you can redistribute it and/or modify *
23 * it under the terms of the GNU General Public License as published by *
24 * the Free Software Foundation; either version 2 of the License, or *
25 * (at your option) any later version. *
27 * This program is distributed in the hope that it will be useful, *
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
30 * GNU General Public License for more details. *
32 * You should have received a copy of the GNU General Public License *
33 * along with this program; if not, write to the Free Software *
34 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, *
36 ************************************************************************/
38 // Some security stuff...
39 if (!defined('__SECURITY')) {
43 // Initialize the request elements
44 function initRequest () {
45 $GLOBALS['raw_request']['get'] = (array) $_GET;
46 $GLOBALS['raw_request']['post'] = (array) $_POST;
49 // Wrapper for elements in $_GET
50 function getRequestElement ($element) {
51 // By default no element is there
54 // Is the element cached or there?
55 if (isset($GLOBALS['cache_request']['get'][$element])) {
57 $value = $GLOBALS['cache_request']['get'][$element];
58 } elseif (isGetRequestElementSet($element)) {
59 // Then get it directly
60 $value = SQL_ESCAPE($GLOBALS['raw_request']['get'][$element]);
63 $GLOBALS['cache_request']['get'][$element] = $value;
70 // Checks if an element in $_GET exists
71 function isGetRequestElementSet ($element, $subElement = '') {
72 if (empty($subElement)) {
73 return ((isset($GLOBALS['raw_request']['get'][$element])) && ('' . ($GLOBALS['raw_request']['get'][$element] . '') != ''));
75 return ((isset($GLOBALS['raw_request']['get'][$element][$subElement])) && ('' . ($GLOBALS['raw_request']['get'][$element][$subElement] . '') != ''));
79 // Removes an element from $_GET
80 function unsetGetRequestElement ($element) {
81 unset($GLOBALS['cache_request']['get'][$element]);
82 unset($GLOBALS['raw_request']['get'][$element]);
85 // Getter for whole $_GET array
86 function getRequestArray () {
87 return $GLOBALS['raw_request']['get'];
90 // Counts entries in $_GET or returns false if not an array
91 function countRequestGet () {
92 // By default this is not an array
96 $GET = getRequestArray();
101 $count = count($GET);
107 // Setter for element in $_GET
108 function setGetRequestElement ($element, $value) {
110 $element = SQL_ESCAPE($element);
111 $value = SQL_ESCAPE($value);
114 $GLOBALS['raw_request']['get'][$element] = $value;
117 $GLOBALS['cache_request']['get'][$element] = $value;
120 // Wrapper for elements in $_POST
121 function postRequestElement ($element, $subElement = NULL) {
122 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'element[' . gettype($element) . ']=' . $element . ',subElement[' . gettype($subElement) . ']=' . $subElement . ' - ENTERED!');
123 // By default no element is there
126 // Is the element in cache?
127 if (isset($GLOBALS['cache_request']['post'][$element][$subElement])) {
129 $value = $GLOBALS['cache_request']['post'][$element][$subElement];
130 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'element[' . gettype($element) . ']=' . $element . ',subElement[' . gettype($subElement) . ']=' . $subElement . ',value[' . gettype($value) . ']=' . $value . ' - CACHE!');
131 } elseif (isPostRequestElementSet($element)) {
133 $value = $GLOBALS['raw_request']['post'][$element];
135 // Is $subElement set?
136 if ((!is_null($subElement)) && (isPostRequestElementSet($element, $subElement))) {
138 $value = SQL_ESCAPE($value[$subElement]);
139 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'element=' . $element . ',subElement=' . $subElement . ' - SUB!');
140 } elseif (!is_array($value)) {
142 $value = SQL_ESCAPE($value);
143 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'element=' . $element . ' - REGULAR!');
147 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'element=' . $element . ',subElement=' . $subElement . ',value=' . $value.' - ADDED!');
148 $GLOBALS['cache_request']['post'][$element][$subElement] = $value;
152 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'element[' . gettype($element) . ']=' . $element . ',subElement[' . gettype($subElement) . ']=' . $subElement . ',value[' . gettype($value) . ']=' . $value . ' - EXIT!');
156 // Checks if an element in $_POST exists
157 function isPostRequestElementSet ($element, $subElement = NULL) {
159 * Always check that $element is a string and that $subElement is NULL or
160 * a string as numerical indexes are not wanted in POST data (in this
163 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'element[]=' . gettype($element) . ',subElement[]=' . gettype($subElement));
164 assert(is_string($element) && ((is_null($subElement)) || (is_string($subElement)) || (is_int($subElement)) || (is_double($subElement))));
166 // Is a sub element set?
167 if (is_null($subElement)) {
168 // No, then only check $element
169 return ((isset($GLOBALS['raw_request']['post'][$element])) && ((is_array($GLOBALS['raw_request']['post'][$element])) || (('' . $GLOBALS['raw_request']['post'][$element] . '') != '')));
171 // Yes, then check both together
172 return ((isset($GLOBALS['raw_request']['post'][$element][$subElement])) && (('' . $GLOBALS['raw_request']['post'][$element][$subElement] . '') != ''));
176 // Removes an element from $_POST
177 function unsetPostRequestElement ($element) {
178 unset($GLOBALS['raw_request']['post'][$element]);
179 unset($GLOBALS['cache_request']['post'][$element]);
182 // Getter for whole $_POST array
183 function postRequestArray () {
184 return $GLOBALS['raw_request']['post'];
187 // Setter for whole $_POST array
188 function setPostRequestArray ($postData) {
189 $GLOBALS['raw_request']['post'] = $postData;
192 // Counts entries in $_POST or returns false if not an array
193 function countRequestPost () {
194 // By default this is not an array
198 $postData = postRequestArray();
201 if (is_array($postData)) {
203 $count = count($postData);
210 // Setter for element in $_POST
211 function setPostRequestElement ($element, $value) {
212 // Is $element or $value an array?
213 if (is_array($element)) {
215 $eval = "\$GLOBALS['raw_request']['post']['";
218 $eval .= implode("']['", $element);
220 // Finish eval() command
221 $eval .= sprintf("'] = \"%s\";", SQL_ESCAPE($value));
225 } elseif (is_array($value)) {
227 $element = SQL_ESCAPE($element);
229 // Value is an array so set it directly
230 $GLOBALS['raw_request']['post'][$element] = $value;
233 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'element=' . $element . ',value=' . $value . ' - BEFORE!');
236 $element = SQL_ESCAPE($element);
237 $value = SQL_ESCAPE($value);
240 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'element=' . $element . ',value=' . $value . ' - AFTER!');
243 $GLOBALS['raw_request']['post'][$element] = $value;
247 $GLOBALS['cache_request']['post'][$element][NULL] = $value;
250 // Checks whether a form was sent. If so, the $_POST['ok'] element must be set
251 function isFormSent ($requestParameter = 'ok') {
253 return isPostRequestElementSet($requestParameter);
256 // Getter for request URI
257 function getRequestUri () {
259 if (!isset($_SERVER['REQUEST_URI'])) {
260 // Return empty string
265 return $_SERVER['REQUEST_URI'];
268 // Add all GET parameters to a string (without leading sign)
269 function addAllGetRequestParameters () {
273 // Now add all parameters
274 foreach (getRequestArray() as $key => $value) {
276 $return .= SQL_ESCAPE($key) . '=' . SQL_ESCAPE($value) . '&';
279 // Remove trailing &
280 $return = substr($return, 0, -5);