2006 => 2007 Happy New Year\!
[platal.git] / classes / env.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2007 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 b($key, $default = false)
45 {
46 return (bool)Env::_get($key, $default);
47 }
48
49 public static function i($key, $default = 0)
50 {
51 $i = Env::_get($key, $default);
52 return is_numeric($i) ? intval($i) : $default;
53 }
54 }
55
56 class Post
57 {
58 public static function _get($key, $default)
59 {
60 return isset($_POST[$key]) ? $_POST[$key] : $default;
61 }
62
63 public static function has($key)
64 {
65 return isset($_POST[$key]);
66 }
67
68 public static function kill($key)
69 {
70 unset($_POST[$key]);
71 }
72
73 public static function v($key, $default = null)
74 {
75 return Post::_get($key, $default);
76 }
77
78 public static function b($key, $default = false)
79 {
80 return (bool)Post::_get($key, $default);
81 }
82
83 public static function i($key, $default = 0)
84 {
85 $i = Post::_get($key, $default);
86 return is_numeric($i) ? intval($i) : $default;
87 }
88 }
89
90 class Get
91 {
92 public static function _get($key, $default)
93 {
94 return isset($_GET[$key]) ? $_GET[$key] : $default;
95 }
96
97 public static function has($key)
98 {
99 return isset($_GET[$key]);
100 }
101
102 public static function kill($key)
103 {
104 unset($_GET[$key]);
105 }
106
107 public static function v($key, $default = null)
108 {
109 return Get::_get($key, $default);
110 }
111
112 public static function b($key, $default = false)
113 {
114 return (bool)Get::_get($key, $default);
115 }
116
117 public static function i($key, $default = 0)
118 {
119 $i = Get::_get($key, $default);
120 return is_numeric($i) ? intval($i) : $default;
121 }
122 }
123
124 class Cookie
125 {
126 public static function _get($key, $default)
127 {
128 return isset($_COOKIE[$key]) ? $_COOKIE[$key] : $default;
129 }
130
131 public static function has($key)
132 {
133 return isset($_COOKIE[$key]);
134 }
135
136 public static function kill($key)
137 {
138 unset($_COOKIE[$key]);
139 }
140
141 public static function v($key, $default = null)
142 {
143 return Cookie::_get($key, $default);
144 }
145
146 public static function b($key, $default = false)
147 {
148 return (bool)Cookie::_get($key, $default);
149 }
150
151 public static function i($key, $default = 0)
152 {
153 $i = Cookie::_get($key, $default);
154 return is_numeric($i) ? intval($i) : $default;
155 }
156 }
157
158 function fix_gpc_magic(&$item, $key) {
159 if (is_array($item)) {
160 array_walk($item, 'fix_gpc_magic');
161 } else {
162 $item = stripslashes($item);
163 }
164 }
165 function fix_encoding(&$item, $key) {
166 if (is_array($item)) {
167 array_walk($item, 'fix_encoding');
168 } elseif (preg_match('/[\x80-\x9f]/', $item)) {
169 $item = iconv('CP1252', 'ISO-8859-15//TRANSLIT', $item);
170 }
171 }
172
173 if (ini_get('magic_quotes_gpc') && empty($DONT_FIX_GPC)) {
174 array_walk($_GET, 'fix_gpc_magic');
175 array_walk($_POST, 'fix_gpc_magic');
176 array_walk($_COOKIE, 'fix_gpc_magic');
177 array_walk($_REQUEST, 'fix_gpc_magic');
178 }
179 array_walk($_POST, 'fix_encoding');
180
181 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker:
182 ?>