Fixes for broken order page and themes
[mailer.git] / inc / request-functions.php
1 <?php
2 /************************************************************************
3  * MXChange v0.2.1                                    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  * Needs to be in all Files and every File needs "svn propset           *
18  * svn:keywords Date Revision" (autoprobset!) at least!!!!!!            *
19  * -------------------------------------------------------------------- *
20  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
21  * For more information visit: http://www.mxchange.org                  *
22  *                                                                      *
23  * This program is free software; you can redistribute it and/or modify *
24  * it under the terms of the GNU General Public License as published by *
25  * the Free Software Foundation; either version 2 of the License, or    *
26  * (at your option) any later version.                                  *
27  *                                                                      *
28  * This program is distributed in the hope that it will be useful,      *
29  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
30  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
31  * GNU General Public License for more details.                         *
32  *                                                                      *
33  * You should have received a copy of the GNU General Public License    *
34  * along with this program; if not, write to the Free Software          *
35  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
36  * MA  02110-1301  USA                                                  *
37  ************************************************************************/
38
39 // Some security stuff...
40 if (!defined('__SECURITY')) {
41         die();
42 } // END - if
43
44 // Initialize the request elements
45 function initRequest () {
46         $GLOBALS['raw_request']['get']  = $_GET;
47         $GLOBALS['raw_request']['post'] = $_POST;
48 }
49
50 // Wrapper for elements in $_GET
51 function getRequestElement ($element) {
52         // By default no element is there
53         $value = null;
54
55         // Is the element cached or there?
56         if (isset($GLOBALS['cache_request']['request_get'][$element])) {
57                 // Then use the cache
58                 $value = $GLOBALS['cache_request']['request_get'][$element];
59         } elseif (isGetRequestElementSet($element)) {
60                 // Then get it directly
61                 $value = SQL_ESCAPE($GLOBALS['raw_request']['get'][$element]);
62
63                 // Store it in cache
64                 $GLOBALS['cache_request']['request_get'][$element] = $value;
65         } // END - if
66
67         // Return value
68         return $value;
69 }
70
71 // Checks if an element in $_GET exists
72 function isGetRequestElementSet ($element, $subElement = '') {
73         if (empty($subElement)) {
74                 return ((isset($GLOBALS['raw_request']['get'][$element])) && (!empty($GLOBALS['raw_request']['get'][$element])));
75         } else {
76                 return ((isset($GLOBALS['raw_request']['get'][$element][$subElement])) && (!empty($GLOBALS['raw_request']['get'][$element][$subElement])));
77         }
78 }
79
80 // Removes an element from $_GET
81 function unsetGetRequestElement ($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 setRequestGetElement ($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']['request_get'][$element] = $value;
118 }
119
120 // Wrapper for elements in $_POST
121 function postRequestElement ($element, $subElement=null) {
122         // By default no element is there
123         $value = null;
124
125         // Is the element in cache?
126         if (isset($GLOBALS['cache_request']['request_post'][$element][$subElement])) {
127                 // Then use it
128                 $value = $GLOBALS['cache_request']['request_post'][$element][$subElement];
129         } elseif (isPostRequestElementSet($element)) {
130                 // Then use it
131                 $value = $GLOBALS['raw_request']['post'][$element];
132
133                 // Is $subElement set?
134                 if ((!is_null($subElement)) && (isPostRequestElementSet($element, $subElement))) {
135                         // Then use this
136                         $value = SQL_ESCAPE($value[$subElement]);
137                 } elseif (!is_array($value)) {
138                         // Escape it here
139                         $value = SQL_ESCAPE($value);
140                 }
141
142                 // Set it in cache
143                 $GLOBALS['cache_request']['request_post'][$element][$subElement] = $value;
144         } // END - if
145
146         // Return value
147         return $value;
148 }
149
150 // Checks if an element in $_POST exists
151 function isPostRequestElementSet ($element, $subElement=null) {
152         if (is_null($subElement)) {
153                 return ((isset($GLOBALS['raw_request']['post'][$element])) && (!empty($GLOBALS['raw_request']['post'][$element])));
154         } else {
155                 return ((isset($GLOBALS['raw_request']['post'][$element][$subElement])) && (!empty($GLOBALS['raw_request']['post'][$element][$subElement])));
156         }
157 }
158
159 // Removes an element from $_POST
160 function unsetPostRequestElement ($element) {
161         unset($GLOBALS['raw_request']['post'][$element]);
162 }
163
164 // Getter for whole $_POST array
165 function postRequestArray () {
166         return $GLOBALS['raw_request']['post'];
167 }
168
169 // Setter for whole $_POST array
170 function setPostRequestArray (array $postData) {
171         $GLOBALS['raw_request']['post'] = $postData;
172 }
173
174 // Counts entries in $_POST or returns false if not an array
175 function countRequestPost () {
176         // By default this is not an array
177         $count = false;
178
179         // Get the array
180         $postData = postRequestArray();
181
182         // Is it an array?
183         if (is_array($postData)) {
184                 // Then count it
185                 $count = count($postData);
186         } // END - if
187
188         // Return value
189 }
190
191 // Setter for element in $_POST
192 function setRequestPostElement ($element, $value) {
193         // Is $element or $value an array?
194         if (is_array($element)) {
195                 // Set array
196                 $eval = "\$GLOBALS['raw_request']['post']['";
197
198                 // Add all entries
199                 $eval .= implode("']['", $element);
200
201                 // Finish eval() command
202                 $eval .= sprintf("'] = \"%s\";", SQL_ESCAPE($value));
203
204                 // And run it
205                 eval($eval);
206         } elseif (is_array($value)) {
207                 // Escape element
208                 $element = SQL_ESCAPE($element);
209
210                 // Value is an array so set it directly
211                 $GLOBALS['raw_request']['post'][$element] = $value;
212         } else {
213                 // Escape both
214                 $element = SQL_ESCAPE($element);
215                 $value   = SQL_ESCAPE($value);
216
217                 // Set regular entry
218                 $GLOBALS['raw_request']['post'][$element] = $value;
219         }
220
221         // Update cache
222         $GLOBALS['cache_request']['request_post'][$element] = $value;
223 }
224
225 // Checks wether a form was sent. If so, the $_POST['ok'] element must be set
226 function isFormSent () {
227         // Simply wrap it!
228         return isPostRequestElementSet('ok');
229 }
230
231 // Setter for content type
232 function setContentType ($contentType) {
233         $GLOBALS['content_type'] = (string) $contentType;
234 }
235
236 // Getter for content type
237 function getContentType () {
238         return $GLOBALS['content_type'];
239 }
240
241 // Getter for request URI
242 function getRequestUri () {
243         return $_SERVER['REQUEST_URI'];
244 }
245
246 // [EOF]
247 ?>