Release plat/al core v1.1.13
[platal.git] / classes / env.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2011 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
18 * Foundation, Inc., *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
21
22 class Env
23 {
24 public static function _get($key, $default)
25 {
26 return isset($_REQUEST[$key]) ? $_REQUEST[$key] : $default;
27 }
28
29 public static function has($key)
30 {
31 return isset($_REQUEST[$key]);
32 }
33
34 public static function kill($key)
35 {
36 unset($_REQUEST[$key]);
37 }
38
39 public static function v($key, $default = null)
40 {
41 return Env::_get($key, $default);
42 }
43
44 public static function s($key, $default = '')
45 {
46 return (string)Env::_get($key, $default);
47 }
48
49 public static function t($key, $default = '')
50 {
51 return trim(Env::s($key, $default));
52 }
53
54 public static function blank($key, $strict = false)
55 {
56 if (!Env::has($key)) {
57 return true;
58 }
59 $var = $strict ? Env::s($key) : Env::t($key);
60 return empty($var);
61 }
62
63 public static function b($key, $default = false)
64 {
65 return (bool)Env::_get($key, $default);
66 }
67
68 public static function i($key, $default = 0)
69 {
70 $i = to_integer(Env::_get($key, $default));
71 return $i === false ? $default : $i;
72 }
73
74 public static function l(array $keys)
75 {
76 return array_map(array('Env', 'v'), $keys);
77 }
78
79 public static function set($key, $value)
80 {
81 $_REQUEST[$key] =& $value;
82 }
83
84 public static function bootstrap($key, $value)
85 {
86 if (!Env::has($key)) {
87 Env::set($key, $value);
88 }
89 }
90 }
91
92 class Post
93 {
94 public static function _get($key, $default)
95 {
96 return isset($_POST[$key]) ? $_POST[$key] : $default;
97 }
98
99 public static function has($key)
100 {
101 return isset($_POST[$key]);
102 }
103
104 public static function kill($key)
105 {
106 unset($_POST[$key]);
107 }
108
109 public static function v($key, $default = null)
110 {
111 return Post::_get($key, $default);
112 }
113
114 public static function b($key, $default = false)
115 {
116 return (bool)Post::_get($key, $default);
117 }
118
119 public static function s($key, $default = '')
120 {
121 return (string)Post::_get($key, $default);
122 }
123
124 public static function t($key, $default = '')
125 {
126 return trim(Post::s($key, $default));
127 }
128
129 public static function blank($key, $strict = false)
130 {
131 if (!Post::has($key)) {
132 return true;
133 }
134 $var = $strict ? Post::s($key) : Post::t($key);
135 return empty($var);
136 }
137
138 public static function i($key, $default = 0)
139 {
140 $i = to_integer(Post::_get($key, $default));
141 return $i === false ? $default : $i;
142 }
143
144 public static function l(array $keys)
145 {
146 return array_map(array('Post', 'v'), $keys);
147 }
148
149 public static function set($key, $value)
150 {
151 $_POST[$key] =& $value;
152 Env::set($key, $value);
153 }
154
155 public static function bootstrap($key, $value)
156 {
157 if (!Post::has($key)) {
158 Post::set($key, $value);
159 }
160 }
161 }
162
163 class Get
164 {
165 public static function _get($key, $default)
166 {
167 return isset($_GET[$key]) ? $_GET[$key] : $default;
168 }
169
170 public static function has($key)
171 {
172 return isset($_GET[$key]);
173 }
174
175 public static function kill($key)
176 {
177 unset($_GET[$key]);
178 }
179
180 public static function v($key, $default = null)
181 {
182 return Get::_get($key, $default);
183 }
184
185 public static function b($key, $default = false)
186 {
187 return (bool)Get::_get($key, $default);
188 }
189
190 public static function s($key, $default = '')
191 {
192 return (string)Get::_get($key, $default);
193 }
194
195 public static function t($key, $default = '')
196 {
197 return trim(Get::s($key, $default));
198 }
199
200 public static function blank($key, $strict = false)
201 {
202 if (!Get::has($key)) {
203 return true;
204 }
205 $var = $strict ? Get::s($key) : Get::t($key);
206 return empty($var);
207 }
208
209 public static function i($key, $default = 0)
210 {
211 $i = to_integer(Get::_get($key, $default));
212 return $i === false ? $default : $i;
213 }
214
215 public static function l(array $keys)
216 {
217 return array_map(array('Get', 'v'), $keys);
218 }
219
220 public static function set($key, $value)
221 {
222 $_GET[$key] =& $value;
223 Env::set($key, $value);
224 }
225
226 public static function bootstrap($key, $value)
227 {
228 if (!Get::has($key)) {
229 Get::set($key, $value);
230 }
231 }
232 }
233
234 class Cookie
235 {
236 public static function _get($key, $default)
237 {
238 global $globals;
239 $key = $globals->cookie_ns . $key;
240 return isset($_COOKIE[$key]) ? $_COOKIE[$key] : $default;
241 }
242
243 public static function has($key)
244 {
245 global $globals;
246 $key = $globals->cookie_ns . $key;
247 return isset($_COOKIE[$key]);
248 }
249
250 public static function kill($key)
251 {
252 global $globals;
253 $key = $globals->cookie_ns . $key;
254 setcookie($key, '', time() - 3600, $globals->cookie_path);
255 unset($_COOKIE[$key]);
256 }
257
258 public static function set($key, $value, $days, $secure = false) {
259 global $globals;
260 $key = $globals->cookie_ns . $key;
261 if (!$secure || @$_SERVER['HTTPS']) {
262 setcookie($key, $value, time() + 86400 * $days, $globals->cookie_path, '',
263 $secure, $secure);
264 $_COOKIE[$key] = $value;
265 Env::set($key, $value);
266 }
267 }
268
269 public static function v($key, $default = null)
270 {
271 return Cookie::_get($key, $default);
272 }
273
274 public static function s($key, $default = '')
275 {
276 return (string)Cookie::_get($key, $default);
277 }
278
279 public static function t($key, $default = '')
280 {
281 return trim(Cookie::s($key, $default));
282 }
283
284 public static function blank($key, $strict = false)
285 {
286 if (!Cookie::has($key)) {
287 return true;
288 }
289 $var = $strict ? Cookie::s($key) : Cookie::t($key);
290 return empty($var);
291 }
292
293 public static function b($key, $default = false)
294 {
295 return (bool)Cookie::_get($key, $default);
296 }
297
298 public static function i($key, $default = 0)
299 {
300 $i = to_integer(Cookie::_get($key, $default));
301 return $i === false ? $default : $i;
302 }
303
304 public static function l(array $keys)
305 {
306 return array_map(array('Cookie', 'v'), $keys);
307 }
308 }
309
310 function fix_gpc_magic(&$item, $key) {
311 if (is_array($item)) {
312 array_walk($item, 'fix_gpc_magic');
313 } else {
314 $item = stripslashes($item);
315 }
316 }
317
318 if (ini_get('magic_quotes_gpc') && empty($DONT_FIX_GPC)) {
319 array_walk($_GET, 'fix_gpc_magic');
320 array_walk($_POST, 'fix_gpc_magic');
321 array_walk($_COOKIE, 'fix_gpc_magic');
322 array_walk($_REQUEST, 'fix_gpc_magic');
323 }
324
325 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker fenc=utf-8:
326 ?>