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 * -------------------------------------------------------------------- *
13 * Copyright (c) 2003 - 2009 by Roland Haeder *
14 * Copyright (c) 2009 - 2015 by Mailer Developer Team *
15 * For more information visit: http://mxchange.org *
17 * This program is free software; you can redistribute it and/or modify *
18 * it under the terms of the GNU General Public License as published by *
19 * the Free Software Foundation; either version 2 of the License, or *
20 * (at your option) any later version. *
22 * This program is distributed in the hope that it will be useful, *
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
25 * GNU General Public License for more details. *
27 * You should have received a copy of the GNU General Public License *
28 * along with this program; if not, write to the Free Software *
29 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, *
31 ************************************************************************/
33 // Some security stuff...
34 if (!defined('__SECURITY')) {
38 // Initialize the request elements
39 function initRequest () {
40 $GLOBALS['raw_request']['get'] = (array) $_GET;
41 $GLOBALS['raw_request']['post'] = (array) $_POST;
44 // Wrapper for elements in $_GET
45 function getRequestElement ($element) {
46 // By default no element is there
49 // Is the element cached or there?
50 if (isset($GLOBALS['cache_request']['get'][$element])) {
52 $value = $GLOBALS['cache_request']['get'][$element];
53 } elseif (isGetRequestElementSet($element)) {
54 // Then get it directly
55 $value = sqlEscapeString($GLOBALS['raw_request']['get'][$element]);
58 $GLOBALS['cache_request']['get'][$element] = $value;
65 // Checks if an element in $_GET exists
66 function isGetRequestElementSet ($element, $subElement = '') {
67 if (empty($subElement)) {
68 return ((isset($GLOBALS['raw_request']['get'][$element])) && ('' . ($GLOBALS['raw_request']['get'][$element] . '') != ''));
70 return ((isset($GLOBALS['raw_request']['get'][$element][$subElement])) && ('' . ($GLOBALS['raw_request']['get'][$element][$subElement] . '') != ''));
74 // Removes an element from $_GET
75 function unsetGetRequestElement ($element) {
76 unset($GLOBALS['cache_request']['get'][$element]);
77 unset($GLOBALS['raw_request']['get'][$element]);
80 // Getter for whole $_GET array
81 function getRequestArray () {
82 return $GLOBALS['raw_request']['get'];
85 // Counts entries in $_GET or returns false if not an array
86 function countRequestGet () {
87 // By default this is not an array
91 $GET = getRequestArray();
102 // Setter for element in $_GET
103 function setGetRequestElement ($element, $value) {
105 $element = sqlEscapeString($element);
106 $value = sqlEscapeString($value);
109 $GLOBALS['raw_request']['get'][$element] = $value;
112 $GLOBALS['cache_request']['get'][$element] = $value;
115 // Wrapper for elements in $_POST
116 function postRequestElement ($element, $subElement = NULL) {
117 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'element[' . gettype($element) . ']=' . $element . ',subElement[' . gettype($subElement) . ']=' . $subElement . ' - ENTERED!');
118 // By default no element is there
121 // Is the element in cache?
122 if (isset($GLOBALS['cache_request']['post'][$element][$subElement])) {
124 $value = $GLOBALS['cache_request']['post'][$element][$subElement];
125 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'element[' . gettype($element) . ']=' . $element . ',subElement[' . gettype($subElement) . ']=' . $subElement . ',value[' . gettype($value) . ']=' . $value . ' - CACHE!');
126 } elseif (isPostRequestElementSet($element)) {
128 $value = $GLOBALS['raw_request']['post'][$element];
130 // Is $subElement set?
131 if ((!is_null($subElement)) && (isPostRequestElementSet($element, $subElement))) {
133 $value = sqlEscapeString($value[$subElement]);
134 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'element=' . $element . ',subElement=' . $subElement . ' - SUB!');
135 } elseif ((!is_array($value)) && (function_exists('sqlEscapeString'))) {
137 $value = sqlEscapeString($value);
138 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'element=' . $element . ' - REGULAR!');
142 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'element=' . $element . ',subElement=' . $subElement . ',value=' . $value.' - ADDED!');
143 $GLOBALS['cache_request']['post'][$element][$subElement] = $value;
147 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'element[' . gettype($element) . ']=' . $element . ',subElement[' . gettype($subElement) . ']=' . $subElement . ',value[' . gettype($value) . ']=' . $value . ' - EXIT!');
151 // Checks if an element in $_POST exists
152 function isPostRequestElementSet ($element, $subElement = NULL) {
154 * Always check that $element is a string and that $subElement is NULL or
155 * a string as numerical indexes are not wanted in POST data (in this
158 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'element[]=' . gettype($element) . ',subElement[]=' . gettype($subElement));
159 assert(is_string($element) && ((is_null($subElement)) || (is_string($subElement)) || (is_int($subElement)) || (is_double($subElement))));
161 // Is a sub element set?
162 if (is_null($subElement)) {
163 // No, then only check $element
164 return ((isset($GLOBALS['raw_request']['post'][$element])) && ((is_array($GLOBALS['raw_request']['post'][$element])) || (('' . $GLOBALS['raw_request']['post'][$element] . '') != '')));
166 // Yes, then check both together
167 return ((isset($GLOBALS['raw_request']['post'][$element][$subElement])) && (('' . $GLOBALS['raw_request']['post'][$element][$subElement] . '') != ''));
171 // Removes an element from $_POST
172 function unsetPostRequestElement ($element) {
173 unset($GLOBALS['raw_request']['post'][$element]);
174 unset($GLOBALS['cache_request']['post'][$element]);
177 // Getter for whole $_POST array
178 function postRequestArray () {
179 return $GLOBALS['raw_request']['post'];
182 // Setter for whole $_POST array
183 function setPostRequestArray ($postData) {
184 $GLOBALS['raw_request']['post'] = $postData;
187 // Counts entries in $_POST or returns false if not an array
188 function countRequestPost () {
189 // By default this is not an array
193 $postData = postRequestArray();
196 if (is_array($postData)) {
198 $count = count($postData);
205 // Setter for element in $_POST
206 function setPostRequestElement ($element, $value) {
207 // Is $element or $value an array?
208 if (is_array($element)) {
210 $eval = "\$GLOBALS['raw_request']['post']['";
213 $eval .= implode("']['", $element);
215 // Finish eval() command
216 $eval .= sprintf("'] = \"%s\";", sqlEscapeString($value));
220 } elseif (is_array($value)) {
222 $element = sqlEscapeString($element);
224 // Value is an array so set it directly
225 $GLOBALS['raw_request']['post'][$element] = $value;
228 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'element=' . $element . ',value=' . $value . ' - BEFORE!');
231 $element = sqlEscapeString($element);
232 $value = sqlEscapeString($value);
235 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'element=' . $element . ',value=' . $value . ' - AFTER!');
238 $GLOBALS['raw_request']['post'][$element] = $value;
242 $GLOBALS['cache_request']['post'][$element][NULL] = $value;
245 // Checks whether a form was sent. If so, the $_POST['ok'] element must be set
246 function isFormSent ($requestParameter = 'ok') {
248 return isPostRequestElementSet($requestParameter);
251 // Getter for request URI
252 function getRequestUri () {
254 if (!isset($_SERVER['REQUEST_URI'])) {
255 // Return empty string
260 return $_SERVER['REQUEST_URI'];
263 // Add all GET parameters to a string (without leading sign)
264 function addAllGetRequestParameters () {
268 // Now add all parameters
269 foreach (getRequestArray() as $key => $value) {
271 $return .= sqlEscapeString($key) . '=' . sqlEscapeString($value) . '&';
274 // Remove trailing &
275 $return = substr($return, 0, -5);