Continued a bit:
[mailer.git] / inc / request-functions.php
1 <?php
2 /************************************************************************
3  * Mailer v0.2.1-FINAL                                Start: 02/28/2009 *
4  * ===================                          Last change: 02/28/2009 *
5  *                                                                      *
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 - 2016 by Mailer Developer Team                   *
15  * For more information visit: http://mxchange.org                      *
16  *                                                                      *
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.                                  *
21  *                                                                      *
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.                         *
26  *                                                                      *
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,               *
30  * MA  02110-1301  USA                                                  *
31  ************************************************************************/
32
33 // Some security stuff...
34 if (!defined('__SECURITY')) {
35         die();
36 } // END - if
37
38 // Initialize the request elements
39 function initRequest () {
40         $GLOBALS['raw_request']['get']  = (array) $_GET;
41         $GLOBALS['raw_request']['post'] = (array) $_POST;
42 }
43
44 // Wrapper for elements in $_GET
45 function getRequestElement ($element) {
46         // By default no element is there
47         $value = NULL;
48
49         // Is the element cached or there?
50         if (isset($GLOBALS['cache_request']['get'][$element])) {
51                 // Then use the cache
52                 $value = $GLOBALS['cache_request']['get'][$element];
53         } elseif (isGetRequestElementSet($element)) {
54                 // Then get it directly
55                 $value = sqlEscapeString($GLOBALS['raw_request']['get'][$element]);
56
57                 // Store it in cache
58                 $GLOBALS['cache_request']['get'][$element] = $value;
59         } // END - if
60
61         // Return value
62         return $value;
63 }
64
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] . '') != ''));
69         } else {
70                 return ((isset($GLOBALS['raw_request']['get'][$element][$subElement])) && ('' . ($GLOBALS['raw_request']['get'][$element][$subElement] . '') != ''));
71         }
72 }
73
74 // Removes an element from $_GET
75 function unsetGetRequestElement ($element) {
76         unset($GLOBALS['cache_request']['get'][$element]);
77         unset($GLOBALS['raw_request']['get'][$element]);
78 }
79
80 // Getter for whole $_GET array
81 function getRequestArray () {
82         return $GLOBALS['raw_request']['get'];
83 }
84
85 // Counts entries in $_GET or returns false if not an array
86 function countRequestGet () {
87         // By default this is not an array
88         $count = FALSE;
89
90         // Get the array
91         $GET = getRequestArray();
92
93         // Is it an array?
94         if (is_array($GET)) {
95                 // Then count it
96                 $count = count($GET);
97         } // END - if
98
99         // Return value
100 }
101
102 // Setter for element in $_GET
103 function setGetRequestElement ($element, $value) {
104         // Escape both
105         $element = sqlEscapeString($element);
106         $value   = sqlEscapeString($value);
107
108         // Set in $_GET
109         $GLOBALS['raw_request']['get'][$element] = $value;
110
111         // Update cache
112         $GLOBALS['cache_request']['get'][$element] = $value;
113 }
114
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
119         $value = NULL;
120
121         // Is the element in cache?
122         if (isset($GLOBALS['cache_request']['post'][$element][$subElement])) {
123                 // Then use it
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)) {
127                 // Then use it
128                 $value = $GLOBALS['raw_request']['post'][$element];
129
130                 // Is $subElement set?
131                 if ((!is_null($subElement)) && (isPostRequestElementSet($element, $subElement))) {
132                         // Then use this
133                         $value = sqlEscapeString($value[$subElement]);
134                         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'element=' . $element . ',subElement=' . $subElement . ' - SUB!');
135                 } elseif ((!is_array($value)) && (function_exists('sqlEscapeString'))) {
136                         // Escape it here
137                         $value = sqlEscapeString($value);
138                         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'element=' . $element . ' - REGULAR!');
139                 }
140
141                 // Set it in cache
142                 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'element=' . $element . ',subElement=' . $subElement . ',value=' . $value.' - ADDED!');
143                 $GLOBALS['cache_request']['post'][$element][$subElement] = $value;
144         } // END - if
145
146         // Return value
147         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'element[' . gettype($element) . ']=' . $element . ',subElement[' . gettype($subElement) . ']=' . $subElement . ',value[' . gettype($value) . ']=' . $value . ' - EXIT!');
148         return $value;
149 }
150
151 // Checks if an element in $_POST exists
152 function isPostRequestElementSet ($element, $subElement = NULL) {
153         /*
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
156          * project).
157          */
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))));
160
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] . '') != '')));
165         } else {
166                 // Yes, then check both together
167                 return ((isset($GLOBALS['raw_request']['post'][$element][$subElement])) && (('' . $GLOBALS['raw_request']['post'][$element][$subElement] . '') != ''));
168         }
169 }
170
171 // Removes an element from $_POST
172 function unsetPostRequestElement ($element) {
173         unset($GLOBALS['raw_request']['post'][$element]);
174         unset($GLOBALS['cache_request']['post'][$element]);
175 }
176
177 // Getter for whole $_POST array
178 function postRequestArray () {
179         return $GLOBALS['raw_request']['post'];
180 }
181
182 // Setter for whole $_POST array
183 function setPostRequestArray ($postData) {
184         $GLOBALS['raw_request']['post'] = $postData;
185 }
186
187 // Counts entries in $_POST or returns false if not an array
188 function countRequestPost () {
189         // By default this is not an array
190         $count = FALSE;
191
192         // Get the array
193         $postData = postRequestArray();
194
195         // Is it an array?
196         if (is_array($postData)) {
197                 // Then count it
198                 $count = count($postData);
199         } // END - if
200
201         // Return value
202         return $count;
203 }
204
205 // Setter for element in $_POST
206 function setPostRequestElement ($element, $value) {
207         // Is $element or $value an array?
208         if (is_array($element)) {
209                 // Set array
210                 $eval = "\$GLOBALS['raw_request']['post']['";
211
212                 // Add all entries
213                 $eval .= implode("']['", $element);
214
215                 // Finish eval() command
216                 $eval .= sprintf("'] = \"%s\";", sqlEscapeString($value));
217
218                 // And run it
219                 eval($eval);
220         } elseif (is_array($value)) {
221                 // Escape element
222                 $element = sqlEscapeString($element);
223
224                 // Value is an array so set it directly
225                 $GLOBALS['raw_request']['post'][$element] = $value;
226         } else {
227                 // Debug message
228                 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'element=' . $element . ',value=' . $value . ' - BEFORE!');
229
230                 // Escape both
231                 $element = sqlEscapeString($element);
232                 $value   = sqlEscapeString($value);
233
234                 // Debug message
235                 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'element=' . $element . ',value=' . $value . ' - AFTER!');
236
237                 // Set regular entry
238                 $GLOBALS['raw_request']['post'][$element] = $value;
239         }
240
241         // Update cache
242         $GLOBALS['cache_request']['post'][$element][NULL] = $value;
243 }
244
245 // Checks whether a form was sent. If so, the $_POST['ok'] element must be set
246 function isFormSent ($requestParameter = 'ok') {
247         // Simply wrap it!
248         return isPostRequestElementSet($requestParameter);
249 }
250
251 // Getter for request URI
252 function getRequestUri () {
253         // Is it not set?
254         if (!isset($_SERVER['REQUEST_URI'])) {
255                 // Return empty string
256                 return '';
257         } // END - if
258
259         // Return it
260         return $_SERVER['REQUEST_URI'];
261 }
262
263 // Add all GET parameters to a string (without leading sign)
264 function addAllGetRequestParameters () {
265         // Init variable
266         $return = '';
267
268         // Now add all parameters
269         foreach (getRequestArray() as $key => $value) {
270                 // Add it secured
271                 $return .= sqlEscapeString($key) . '=' . sqlEscapeString($value) . '&amp;';
272         } // END - foreach
273
274         // Remove trailing &amp;
275         $return = substr($return, 0, -5);
276
277         // Return it
278         return $return;
279 }
280
281 // [EOF]
282 ?>