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