Cookie management using Cookie:: class.
[platal.git] / classes / env.php
CommitLineData
0337d704 1<?php
2/***************************************************************************
179afa7f 3 * Copyright (C) 2003-2008 Polytechnique.org *
0337d704 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
0337d704 22class Env
23{
6995a9b9 24 public static function _get($key, $default)
0337d704 25 {
26 return isset($_REQUEST[$key]) ? $_REQUEST[$key] : $default;
27 }
5e2307dc 28
6995a9b9 29 public static function has($key)
0337d704 30 {
31 return isset($_REQUEST[$key]);
32 }
5e2307dc 33
6995a9b9 34 public static function kill($key)
0337d704 35 {
36 unset($_REQUEST[$key]);
37 }
38
6995a9b9 39 public static function v($key, $default = null)
0337d704 40 {
41 return Env::_get($key, $default);
42 }
43
eaf30d86
PH
44 public static function s($key, $default = '')
45 {
46 return (string)Env::_get($key, $default);
47 }
7280eb45 48
720e3261
FB
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
6995a9b9 63 public static function b($key, $default = false)
0337d704 64 {
65 return (bool)Env::_get($key, $default);
66 }
67
6995a9b9 68 public static function i($key, $default = 0)
0337d704 69 {
70 $i = Env::_get($key, $default);
29e515ed 71 return is_numeric($i) ? intval($i) : $default;
0337d704 72 }
7280eb45 73
74 public static function l(array $keys)
75 {
76 return array_map(array('Env', 'v'), $keys);
77 }
0337d704 78}
79
0337d704 80class Post
81{
6995a9b9 82 public static function _get($key, $default)
0337d704 83 {
84 return isset($_POST[$key]) ? $_POST[$key] : $default;
85 }
5e2307dc 86
6995a9b9 87 public static function has($key)
0337d704 88 {
89 return isset($_POST[$key]);
90 }
5e2307dc 91
6995a9b9 92 public static function kill($key)
0337d704 93 {
94 unset($_POST[$key]);
95 }
96
6995a9b9 97 public static function v($key, $default = null)
0337d704 98 {
99 return Post::_get($key, $default);
100 }
101
6995a9b9 102 public static function b($key, $default = false)
0337d704 103 {
104 return (bool)Post::_get($key, $default);
105 }
106
eaf30d86
PH
107 public static function s($key, $default = '')
108 {
109 return (string)Post::_get($key, $default);
110 }
7280eb45 111
720e3261
FB
112 public static function t($key, $default = '')
113 {
114 return trim(Post::s($key, $default));
115 }
116
117 public static function blank($key, $strict = false)
118 {
119 if (!Post::has($key)) {
120 return true;
121 }
122 $var = $strict ? Post::s($key) : Post::t($key);
123 return empty($var);
124 }
125
6995a9b9 126 public static function i($key, $default = 0)
0337d704 127 {
128 $i = Post::_get($key, $default);
29e515ed 129 return is_numeric($i) ? intval($i) : $default;
0337d704 130 }
7280eb45 131
132 public static function l(array $keys)
133 {
134 return array_map(array('Post', 'v'), $keys);
135 }
0337d704 136}
137
0337d704 138class Get
139{
6995a9b9 140 public static function _get($key, $default)
0337d704 141 {
142 return isset($_GET[$key]) ? $_GET[$key] : $default;
143 }
5e2307dc 144
6995a9b9 145 public static function has($key)
0337d704 146 {
147 return isset($_GET[$key]);
148 }
5e2307dc 149
6995a9b9 150 public static function kill($key)
0337d704 151 {
152 unset($_GET[$key]);
153 }
154
6995a9b9 155 public static function v($key, $default = null)
0337d704 156 {
157 return Get::_get($key, $default);
158 }
159
6995a9b9 160 public static function b($key, $default = false)
0337d704 161 {
162 return (bool)Get::_get($key, $default);
163 }
164
7280eb45 165 public static function s($key, $default = '')
166 {
167 return (string)Get::_get($key, $default);
168 }
169
720e3261
FB
170 public static function t($key, $default = '')
171 {
172 return trim(Get::s($key, $default));
173 }
174
175 public static function blank($key, $strict = false)
176 {
177 if (!Get::has($key)) {
178 return true;
179 }
180 $var = $strict ? Get::s($key) : Get::t($key);
181 return empty($var);
182 }
183
6995a9b9 184 public static function i($key, $default = 0)
0337d704 185 {
186 $i = Get::_get($key, $default);
29e515ed 187 return is_numeric($i) ? intval($i) : $default;
0337d704 188 }
7280eb45 189
190 public static function l(array $keys)
191 {
192 return array_map(array('Get', 'v'), $keys);
193 }
0337d704 194}
195
0337d704 196class Cookie
197{
6995a9b9 198 public static function _get($key, $default)
0337d704 199 {
f09d3319
FB
200 global $globals;
201 $key = $globals->cookie_ns . $key;
0337d704 202 return isset($_COOKIE[$key]) ? $_COOKIE[$key] : $default;
203 }
5e2307dc 204
6995a9b9 205 public static function has($key)
0337d704 206 {
f09d3319
FB
207 global $globals;
208 $key = $globals->cookie_ns . $key;
0337d704 209 return isset($_COOKIE[$key]);
210 }
5e2307dc 211
6995a9b9 212 public static function kill($key)
0337d704 213 {
f09d3319
FB
214 global $globals;
215 $key = $globals->cookie_ns . $key;
216 setcookie($key, '', time() - 3600, $globals->cookie_path);
0337d704 217 unset($_COOKIE[$key]);
218 }
219
f09d3319
FB
220 public static function set($key, $value, $days) {
221 global $globals;
222 $key = $globals->cookie_ns . $key;
223 setcookie($key, $value, time() + 86400 * $days, $globals->cookie_path);
224 $_COOKIE[$key] = $value;
225 }
226
6995a9b9 227 public static function v($key, $default = null)
0337d704 228 {
229 return Cookie::_get($key, $default);
230 }
231
eaf30d86 232 public static function s($key, $default = '')
7280eb45 233 {
eaf30d86 234 return (string)Cookie::_get($key, $default);
7280eb45 235 }
236
720e3261
FB
237 public static function t($key, $default = '')
238 {
239 return trim(Cookie::s($key, $default));
240 }
241
242 public static function blank($key, $strict = false)
243 {
244 if (!Cookie::has($key)) {
245 return true;
246 }
247 $var = $strict ? Cookie::s($key) : Cookie::t($key);
248 return empty($var);
249 }
250
6995a9b9 251 public static function b($key, $default = false)
0337d704 252 {
253 return (bool)Cookie::_get($key, $default);
254 }
255
6995a9b9 256 public static function i($key, $default = 0)
0337d704 257 {
258 $i = Cookie::_get($key, $default);
29e515ed 259 return is_numeric($i) ? intval($i) : $default;
0337d704 260 }
7280eb45 261
262 public static function l(array $keys)
263 {
264 return array_map(array('Cookie', 'v'), $keys);
265 }
0337d704 266}
267
0337d704 268function fix_gpc_magic(&$item, $key) {
269 if (is_array($item)) {
270 array_walk($item, 'fix_gpc_magic');
271 } else {
272 $item = stripslashes($item);
273 }
274}
275
5e2307dc 276if (ini_get('magic_quotes_gpc') && empty($DONT_FIX_GPC)) {
0337d704 277 array_walk($_GET, 'fix_gpc_magic');
278 array_walk($_POST, 'fix_gpc_magic');
279 array_walk($_COOKIE, 'fix_gpc_magic');
280 array_walk($_REQUEST, 'fix_gpc_magic');
281}
282
a7de4ef7 283// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
0337d704 284?>