GlobalVar::env()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 2
rs 10
1
<?php
2
    defined('ROOT_PATH') or exit('Access denied');
3
    /**
4
     * TNH Framework
5
     *
6
     * A simple PHP framework using HMVC architecture
7
     *
8
     * This content is released under the MIT License (MIT)
9
     *
10
     * Copyright (c) 2017 TNH Framework
11
     *
12
     * Permission is hereby granted, free of charge, to any person obtaining a copy
13
     * of this software and associated documentation files (the "Software"), to deal
14
     * in the Software without restriction, including without limitation the rights
15
     * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16
     * copies of the Software, and to permit persons to whom the Software is
17
     * furnished to do so, subject to the following conditions:
18
     *
19
     * The above copyright notice and this permission notice shall be included in all
20
     * copies or substantial portions of the Software.
21
     *
22
     * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23
     * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24
     * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25
     * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26
     * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27
     * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28
     * SOFTWARE.
29
     */
30
31
    class GlobalVar {
32
        
33
        /**
34
         * Get the value from $_GET for given key. if the key is empty will return all values
35
         * @see GlobalVar::getVars 
36
         */
37
        public function get($key = null, $xss = true) {
38
            return $this->getVars($_GET, $key, $xss);
39
        }
40
41
        /**
42
         * Set the value for $_GET for the given key.
43
         * @see GlobalVar::setVars 
44
         */
45
        public function setGet($key, $value = null) {
46
            return $this->setVars($_GET, $key, $value);
47
        }
48
49
        /**
50
         * Remove the value from $_GET for the given key.
51
         * @see GlobalVar::removeVars 
52
         */
53
        public function removeGet($key) {
54
            return $this->removeVars($_GET, $key);
55
        }
56
57
        /**
58
         * Get the value from $_POST for given key. if the key is empty will return all values
59
         * @see GlobalVar::getVars 
60
         */
61
        public function post($key = null, $xss = true) {
62
            return $this->getVars($_POST, $key, $xss);
63
        }
64
65
        /**
66
         * Set the value for $_POST for the given key.
67
         * @see GlobalVar::setVars 
68
         */
69
        public function setPost($key, $value = null) {
70
            return $this->setVars($_POST, $key, $value);
71
        }
72
73
        /**
74
         * Remove the value from $_POST for the given key.
75
         * @see GlobalVar::removeVars 
76
         */
77
        public function removePost($key) {
78
            return $this->removeVars($_POST, $key);
79
        }
80
81
        /**
82
         * Get the value from $_FILES for given key. if the key is empty will return all values
83
         * @see GlobalVar::getVars 
84
         */
85
        public function files($key = null, $xss = true) {
86
            return $this->getVars($_FILES, $key, $xss);
87
        }
88
89
        /**
90
         * Set the value for $_FILES for the given key.
91
         * @see GlobalVar::setVars 
92
         */
93
        public function setFiles($key, $value = null) {
94
            return $this->setVars($_FILES, $key, $value);
95
        }
96
97
        /**
98
         * Remove the value from $_FILES for the given key.
99
         * @see GlobalVar::removeVars 
100
         */
101
        public function removeFiles($key) {
102
            return $this->removeVars($_FILES, $key);
103
        }
104
105
        /**
106
         * Get the value from $_REQUEST for given key. if the key is empty will return all values
107
         * @see GlobalVar::getVars 
108
         */
109
        public function request($key = null, $xss = true) {
110
            return $this->getVars($_REQUEST, $key, $xss);
111
        }
112
113
        /**
114
         * Set the value for $_REQUEST for the given key.
115
         * @see GlobalVar::setVars 
116
         */
117
        public function setRequest($key, $value = null) {
118
            return $this->setVars($_REQUEST, $key, $value);
119
        }
120
121
        /**
122
         * Remove the value from $_REQUEST for the given key.
123
         * @see GlobalVar::removeVars 
124
         */
125
        public function removeRequest($key) {
126
            return $this->removeVars($_REQUEST, $key);
127
        }
128
129
        /**
130
         * Get the value from $_COOKIE for given key. if the key is empty will return all values
131
         *
132
         * NOTE: This super global is not filter by default
133
         * 
134
         * @see GlobalVar::getVars 
135
         */
136
        public function cookie($key = null, $xss = false) {
137
            return $this->getVars($_COOKIE, $key, $xss);
138
        }
139
140
        /**
141
         * Set the value for $_COOKIE for the given key.
142
         * @see GlobalVar::setVars 
143
         */
144
        public function setCookie($key, $value = null) {
145
            return $this->setVars($_COOKIE, $key, $value);
146
        }
147
148
        /**
149
         * Remove the value from $_COOKIE for the given key.
150
         * @see GlobalVar::removeVars 
151
         */
152
        public function removeCookie($key) {
153
            return $this->removeVars($_COOKIE, $key);
154
        }
155
156
        /**
157
         * Get the value from $_SESSION for given key. if the key is empty will return all values
158
         *
159
         * NOTE: This super global is not filter by default
160
         * 
161
         * @see GlobalVar::getVars 
162
         */
163
        public function session($key = null, $xss = false) {
164
            return $this->getVars($_SESSION, $key, $xss);
165
        }
166
167
        /**
168
         * Set the value for $_SESSION for the given key.
169
         * @see GlobalVar::setVars 
170
         */
171
        public function setSession($key, $value = null) {
172
            return $this->setVars($_SESSION, $key, $value);
173
        }
174
175
        /**
176
         * Remove the value from $_SESSION for the given key.
177
         * @see GlobalVar::removeVars 
178
         */
179
        public function removeSession($key) {
180
            return $this->removeVars($_SESSION, $key);
181
        }
182
183
        /**
184
         * Get the value from $_SERVER for given key. if the key is empty will return all values
185
         *
186
         * NOTE: This super global is not filter by default
187
         * 
188
         * @see GlobalVar::getVars 
189
         */
190
        public function server($key = null, $xss = false) {
191
            return $this->getVars($_SERVER, $key, $xss);
192
        }
193
194
        /**
195
         * Set the value for $_SERVER for the given key.
196
         * @see GlobalVar::setVars 
197
         */
198
        public function setServer($key, $value = null) {
199
            return $this->setVars($_SERVER, $key, $value);
200
        }
201
202
        /**
203
         * Remove the value from $_SERVER for the given key.
204
         * @see GlobalVar::removeVars 
205
         */
206
        public function removeServer($key) {
207
            return $this->removeVars($_SERVER, $key);
208
        }
209
		
210
        /**
211
         * Get the value from $_ENV for given key. if the key is empty will return all values
212
         *
213
         * NOTE: This super global is not filter by default
214
         * 
215
         * @see GlobalVar::getVars 
216
         */
217
        public function env($key = null, $xss = false) {
218
            return $this->getVars($_ENV, $key, $xss);
219
        }
220
221
        /**
222
         * Set the value for $_ENV for the given key.
223
         * @see GlobalVar::setVars 
224
         */
225
        public function setEnv($key, $value = null) {
226
            return $this->setVars($_ENV, $key, $value);
227
        }
228
229
        /**
230
         * Remove the value from $_ENV for the given key.
231
         * @see GlobalVar::removeVars 
232
         */
233
        public function removeEnv($key) {
234
            return $this->removeVars($_ENV, $key);
235
        }
236
237
         /**
238
         * Get the value from $GLOBALS for given key. if the key is empty will return all values
239
         * @see GlobalVar::getVars 
240
         */
241
        public function globals($key = null, $xss = true) {
242
            return $this->getVars($GLOBALS, $key, $xss);
243
        }
244
245
        /**
246
         * Set the value for $GLOBALS for the given key.
247
         * @see GlobalVar::setVars 
248
         */
249
        public function setGlobals($key, $value = null) {
250
            return $this->setVars($GLOBALS, $key, $value);
251
        }
252
253
        /**
254
         * Remove the value from $GLOBALS for the given key.
255
         * @see GlobalVar::removeVars 
256
         */
257
        public function removeGlobals($key) {
258
            return $this->removeVars($GLOBALS, $key);
259
        }
260
261
        
262
         /**
263
         * Set the value for $_GET, $_POST, $_SERVER etc. if the key is an array will
264
         * set the current super variable value by this.
265
         * @param array $var the super global variable to use, can be "$_POST", "$_GET", etc.
266
         * @param  string|array  $key the item key to be set or array if need set the current global variable 
267
         * by this value
268
         * @param mixed $value the value to set if $key is not an array
269
         *
270
         * @return object       the current instance
271
         */
272
        protected function setVars(&$var, $key, $value = null) {
273
            if (is_array($key)) {
274
                //set all
275
                $var = $key;
276
            } else {
277
                $var[$key] = $value;
278
            }
279
            return $this;
280
        }
281
282
        /**
283
         * Get the value from $_GET, $_POST, $_SERVER etc. for given key. if the key is empty will return all values
284
         * @param array $var the super global variable to use, can be "$_POST", "$_GET", etc.
285
         * @param  string  $key the item key to be fetched
286
         * @param  boolean $xss if need apply some XSS rule on the value
287
         * @return array|mixed       the item value if the key exists or all array if the key is null
288
         */
289
        protected function getVars(&$var, $key = null, $xss = true) {
290
            $data = null;
291
            if ($key === null) {
292
                //return all
293
                $data = $var;
294
            } else if (array_key_exists($key, $var)) {
295
                $data = $var[$key];
296
            }
297
            if ($xss) {
298
                $data = clean_input($data);
299
            }
300
            return $data;
301
        }
302
303
        /**
304
         * Delete the value from $_GET, $_POST, $_SERVER etc. for given key.
305
         * @param array $var the super global variable to use, can be "$_POST", "$_GET", etc.
306
         * @param  string  $key the item key to be deleted
307
         * 
308
         * @return boolean true if the key is found and removed otherwise false
309
         */
310
        protected function removeVars(&$var, $key) {
311
            if (array_key_exists($key, $var)) {
312
                unset($var[$key]);
313
                return true;
314
            }
315
            return false;
316
        }
317
    }
318