Cast added due to API changes in PHP 5.3.1
[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  * 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  * Copyright (c) 2009, 2010 by Mailer Developer Team                    *
22  * For more information visit: http://www.mxchange.org                  *
23  *                                                                      *
24  * This program is free software; you can redistribute it and/or modify *
25  * it under the terms of the GNU General Public License as published by *
26  * the Free Software Foundation; either version 2 of the License, or    *
27  * (at your option) any later version.                                  *
28  *                                                                      *
29  * This program is distributed in the hope that it will be useful,      *
30  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
31  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
32  * GNU General Public License for more details.                         *
33  *                                                                      *
34  * You should have received a copy of the GNU General Public License    *
35  * along with this program; if not, write to the Free Software          *
36  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
37  * MA  02110-1301  USA                                                  *
38  ************************************************************************/
39
40 // Some security stuff...
41 if (!defined('__SECURITY')) {
42         die();
43 } // END - if
44
45 // Initialize the request elements
46 function initRequest () {
47         $GLOBALS['raw_request']['get']  = $_GET;
48         $GLOBALS['raw_request']['post'] = $_POST;
49 }
50
51 // Wrapper for elements in $_GET
52 function getRequestParameter ($element) {
53         // By default no element is there
54         $value = null;
55
56         // Is the element cached or there?
57         if (isset($GLOBALS['cache_request']['request_get'][$element])) {
58                 // Then use the cache
59                 $value = $GLOBALS['cache_request']['request_get'][$element];
60         } elseif (isGetRequestParameterSet($element)) {
61                 // Then get it directly
62                 $value = SQL_ESCAPE($GLOBALS['raw_request']['get'][$element]);
63
64                 // Store it in cache
65                 $GLOBALS['cache_request']['request_get'][$element] = $value;
66         } // END - if
67
68         // Return value
69         return $value;
70 }
71
72 // Checks if an element in $_GET exists
73 function isGetRequestParameterSet ($element, $subElement = '') {
74         if (empty($subElement)) {
75                 return ((isset($GLOBALS['raw_request']['get'][$element])) && (!empty($GLOBALS['raw_request']['get'][$element])));
76         } else {
77                 return ((isset($GLOBALS['raw_request']['get'][$element][$subElement])) && (!empty($GLOBALS['raw_request']['get'][$element][$subElement])));
78         }
79 }
80
81 // Removes an element from $_GET
82 function unsetGetRequestParameter ($element) {
83         unset($GLOBALS['raw_request']['get'][$element]);
84 }
85
86 // Getter for whole $_GET array
87 function getRequestArray () {
88         return $GLOBALS['raw_request']['get'];
89 }
90
91 // Counts entries in $_GET or returns false if not an array
92 function countRequestGet () {
93         // By default this is not an array
94         $count = false;
95
96         // Get the array
97         $GET = getRequestArray();
98
99         // Is it an array?
100         if (is_array($GET)) {
101                 // Then count it
102                 $count = count($GET);
103         } // END - if
104
105         // Return value
106 }
107
108 // Setter for element in $_GET
109 function setGetRequestParameter ($element, $value) {
110         // Escape both
111         $element = SQL_ESCAPE($element);
112         $value   = SQL_ESCAPE($value);
113
114         // Set in $_GET
115         $GLOBALS['raw_request']['get'][$element] = $value;
116
117         // Update cache
118         $GLOBALS['cache_request']['request_get'][$element] = $value;
119 }
120
121 // Wrapper for elements in $_POST
122 function postRequestParameter ($element, $subElement=null) {
123         // By default no element is there
124         $value = null;
125
126         // Is the element in cache?
127         if (isset($GLOBALS['cache_request']['request_post'][$element][$subElement])) {
128                 // Then use it
129                 $value = $GLOBALS['cache_request']['request_post'][$element][$subElement];
130         } elseif (isPostRequestParameterSet($element)) {
131                 // Then use it
132                 $value = $GLOBALS['raw_request']['post'][$element];
133
134                 // Is $subElement set?
135                 if ((!is_null($subElement)) && (isPostRequestParameterSet($element, $subElement))) {
136                         // Then use this
137                         $value = SQL_ESCAPE($value[$subElement]);
138                 } elseif (!is_array($value)) {
139                         // Escape it here
140                         $value = SQL_ESCAPE($value);
141                 }
142
143                 // Set it in cache
144                 $GLOBALS['cache_request']['request_post'][$element][$subElement] = $value;
145         } // END - if
146
147         // Return value
148         return $value;
149 }
150
151 // Checks if an element in $_POST exists
152 function isPostRequestParameterSet ($element, $subElement=null) {
153         if (is_null($subElement)) {
154                 return ((isset($GLOBALS['raw_request']['post'][$element])) && (isset($GLOBALS['raw_request']['post'][$element])));
155         } else {
156                 return ((isset($GLOBALS['raw_request']['post'][$element][$subElement])) && (isset($GLOBALS['raw_request']['post'][$element][$subElement])));
157         }
158 }
159
160 // Removes an element from $_POST
161 function unsetPostRequestParameter ($element) {
162         unset($GLOBALS['raw_request']['post'][$element]);
163 }
164
165 // Getter for whole $_POST array
166 function postRequestArray () {
167         return $GLOBALS['raw_request']['post'];
168 }
169
170 // Setter for whole $_POST array
171 function setPostRequestArray ($postData) {
172         $GLOBALS['raw_request']['post'] = $postData;
173 }
174
175 // Counts entries in $_POST or returns false if not an array
176 function countRequestPost () {
177         // By default this is not an array
178         $count = false;
179
180         // Get the array
181         $postData = postRequestArray();
182
183         // Is it an array?
184         if (is_array($postData)) {
185                 // Then count it
186                 $count = count($postData);
187         } // END - if
188
189         // Return value
190 }
191
192 // Setter for element in $_POST
193 function setPostRequestParameter ($element, $value) {
194         // Is $element or $value an array?
195         if (is_array($element)) {
196                 // Set array
197                 $eval = "\$GLOBALS['raw_request']['post']['";
198
199                 // Add all entries
200                 $eval .= implode("']['", $element);
201
202                 // Finish eval() command
203                 $eval .= sprintf("'] = \"%s\";", SQL_ESCAPE($value));
204
205                 // And run it
206                 eval($eval);
207         } elseif (is_array($value)) {
208                 // Escape element
209                 $element = SQL_ESCAPE($element);
210
211                 // Value is an array so set it directly
212                 $GLOBALS['raw_request']['post'][$element] = $value;
213         } else {
214                 // Escape both
215                 $element = SQL_ESCAPE($element);
216                 $value   = SQL_ESCAPE($value);
217
218                 // Set regular entry
219                 $GLOBALS['raw_request']['post'][$element] = $value;
220         }
221
222         // Update cache
223         $GLOBALS['cache_request']['request_post'][$element][null] = $value;
224 }
225
226 // Checks wether a form was sent. If so, the $_POST['ok'] element must be set
227 function isFormSent () {
228         // Simply wrap it!
229         return isPostRequestParameterSet('ok');
230 }
231
232 // Checks if 'content_type' is set
233 function isContentTypeSet () {
234         return isset($GLOBALS['content_type']);
235 }
236
237 // Setter for content type
238 function setContentType ($contentType) {
239         $GLOBALS['content_type'] = (string) $contentType;
240 }
241
242 // Getter for content type
243 function getContentType () {
244         return $GLOBALS['content_type'];
245 }
246
247 // Getter for request URI
248 function getRequestUri () {
249         return $_SERVER['REQUEST_URI'];
250 }
251
252 // [EOF]
253 ?>