Passed
Push — 1.0.0-dev ( 632010...3e5cd9 )
by nguereza
02:59
created

Request::setVars()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 3
dl 0
loc 8
rs 10
c 0
b 0
f 0
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 GNU GPL License (GPL)
9
     *
10
     * Copyright (C) 2017 Tony NGUEREZA
11
     *
12
     * This program is free software; you can redistribute it and/or
13
     * modify it under the terms of the GNU General Public License
14
     * as published by the Free Software Foundation; either version 3
15
     * of the License, or (at your option) any later version.
16
     *
17
     * This program is distributed in the hope that it will be useful,
18
     * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
     * GNU General Public License for more details.
21
     *
22
     * You should have received a copy of the GNU General Public License
23
     * along with this program; if not, write to the Free Software
24
     * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25
     */
26
27
    class Request {
28
		
29
        /**
30
         * The value for the super global $_GET
31
         * @var array
32
         */
33
        private $get = null;
34
35
        /**
36
         * The value for the super global $_POST
37
         * @var array
38
         */
39
        private $post = null;
40
41
        /**
42
         * The value for the super global $_SERVER
43
         * @var array
44
         */
45
        private $server = null;
46
47
        /**
48
         * The value for the super global $_COOKIE
49
         * @var array
50
         */
51
        private $cookie = null;
52
53
        /**
54
         * The value for the super global $_FILES
55
         * @var array
56
         */
57
        private $file = null;
58
59
        /**
60
         * The value for the super global $_REQUEST
61
         * @var array
62
         */
63
        private $query = null;
64
		
65
        /**
66
         * The session instance
67
         * @var Session
68
         */
69
        private $session = null;
70
		
71
        /**
72
         * The request headers
73
         * @var array
74
         */
75
        private $header = null;
76
77
        /**
78
         * The current request method 'GET', 'POST', 'PUT', etc.
79
         * @var null
80
         */
81
        private $method = null;
82
83
        /**
84
         * The current request URI
85
         * @var string
86
         */
87
        private $requestUri = null;
88
		
89
		
90
        /**
91
         * Construct new request instance
92
         */
93
        public function __construct() {
94
            $this->get = $_GET;
95
            $this->post = $_POST;
96
            $this->server = $_SERVER;
97
            $this->query = $_REQUEST;
98
            $this->cookie = $_COOKIE;
99
            $this->file = $_FILES;
100
            $this->session = & class_loader('Session', 'classes');
101
            $this->method = $this->server('REQUEST_METHOD');
102
            $this->requestUri = $this->server('REQUEST_URI');
103
            $this->header = array();
104
            if (function_exists('apache_request_headers')) {
105
                $this->header = apache_request_headers();
106
            } else if (function_exists('getallheaders')) {
107
                $this->header = getallheaders();
108
            }
109
        }
110
111
        /**
112
         * Get the request method
113
         * @return string
114
         */
115
        public function method() {
116
            return $this->method;
117
        }
118
		
119
        /**
120
         * Get the request URI
121
         * @return string
122
         */
123
        public function requestUri() {
124
            return $this->requestUri;
125
        }
126
127
        /**
128
         * Get the value from $_REQUEST for given key. if the key is empty will return the all values
129
         * @see Request::getVars 
130
         */
131
        public function query($key = null, $xss = true) {
132
            return $this->getVars('query', $key, $xss);
133
        }
134
		
135
        /**
136
         * Get the value from $_GET for given key. if the key is empty will return the all values
137
         * @see Request::getVars 
138
         */
139
        public function get($key = null, $xss = true) {
140
            return $this->getVars('get', $key, $xss);
141
        }
142
		
143
        /**
144
         * Get the value from $_POST for given key. if the key is empty will return the all values
145
         * @see Request::getVars 
146
         */
147
        public function post($key = null, $xss = true) {
148
            return $this->getVars('post', $key, $xss);
149
        }
150
		
151
        /**
152
         * Get the value from $_SERVER for given key. if the key is empty will return the all values
153
         * @see Request::getVars 
154
         */
155
        public function server($key = null, $xss = true) {
156
            return $this->getVars('server', $key, $xss);
157
        }
158
		
159
        /**
160
         * Get the value from $_COOKIE for given key. if the key is empty will return the all values
161
         * @see Request::getVars 
162
         */
163
        public function cookie($key = null, $xss = true) {
164
            return $this->getVars('cookie', $key, $xss);
165
        }
166
167
        /**
168
         * Get the value from header array for given key.
169
         * @see Request::getVars 
170
         */
171
        public function header($key = null, $xss = true) {
172
            return $this->getVars('header', $key, $xss);
173
        }
174
		
175
        /**
176
         * Get the value from $_FILES for given key. if the key is empty will return the all values
177
         * @param  string  $key the item key to be fetched
178
         * @return array|mixed       the item value if the key exists or all array if the key does not exists or is empty
179
         */
180
        public function file($key) {
181
            $file = array_key_exists($key, $this->file) ? $this->file[$key] : null;
182
            return $file;
183
        }
184
		
185
        /**
186
         * Get the value from $_SESSION for given key. if the key is empty will return the all values
187
         * @param  string  $key the item key to be fetched
188
         * @param  boolean $xss if need apply some XSS attack rule on the value
189
         * @return array|mixed       the item value if the key exists or null if the key does not exists
190
         */
191
        public function session($key, $xss = true) {
192
            $session = $this->session->get($key);
193
            if ($xss) {
194
                $session = clean_input($session);
195
            }
196
            return $session;
197
        }
198
199
        /**
200
         * Set the value for $_REQUEST for the given key.
201
         * @see Request::setVars 
202
         */
203
        public function setQuery($key, $value = null) {
204
            return $this->setVars('query', $key, $value);
205
        }
206
207
        /**
208
         * Set the value for $_GET for the given key.
209
         * @see Request::setVars 
210
         */
211
        public function setGet($key, $value = null) {
212
            return $this->setVars('get', $key, $value);
213
        }
214
215
        /**
216
         * Set the value for $_POST for the given key.
217
         * @see Request::setVars 
218
         */
219
        public function setPost($key, $value = null) {
220
            return $this->setVars('post', $key, $value);
221
        }
222
223
        /**
224
         * Set the value for $_SERVER for the given key.
225
         * @see Request::setVars 
226
         */
227
        public function setServer($key, $value = null) {
228
            return $this->setVars('server', $key, $value);
229
        }
230
231
        /**
232
         * Set the value for $_COOKIE for the given key.
233
         * @see Request::setVars 
234
         */
235
        public function setCookie($key, $value = null) {
236
            return $this->setVars('cookie', $key, $value);
237
        }
238
239
        /**
240
         * Set the value for header for the given key.
241
         * @see Request::setVars 
242
         */
243
        public function setHeader($key, $value = null) {
244
            return $this->setVars('header', $key, $value);
245
        }
246
247
        /**
248
         * Set the value for $_FILES for the given key.
249
         * @see Request::setVars 
250
         */
251
        public function setFile($key, $value = null) {
252
            return $this->setVars('file', $key, $value);
253
        }
254
255
        /**
256
         * Set the instance for session.
257
         * @param object|null $session the object of Session to be set
258
         * @return object the current instance
259
         */
260
        public function setSession(Session $session = null) {
261
            $this->session = $session;
262
            return $this;
263
        }
264
265
         /**
266
         * Return the instance of session.
267
         * @return object the session instance
268
         */
269
        public function getSession() {
270
            return $this->session;
271
        }
272
273
         /**
274
         * Set the value for $_GET, $_POST, $_SERVER etc. if the key is an array will
275
         * set the current super variable value by this.
276
         * @param string $type the type can be "post", "get", etc.
277
         * @param  string|array  $key the item key to be set or array if need set the current global variable 
278
         * by this value
279
         * @param mixed $value the value to set if $key is not an array
280
         *
281
         * @return object       the current instance
282
         */
283
        protected function setVars($type, $key, $value = null) {
284
            if (is_array($key)) {
285
                //set all
286
                $this->{$type} = $key;
287
            } else {
288
                $this->{$type}[$key] = $value;
289
            }
290
            return $this;
291
        }
292
293
        /**
294
         * Get the value from $_GET, $_POST, $_SERVER etc. for given key. if the key is empty will return the all values
295
         * @param string $type the type can be "post", "get", etc.
296
         * @param  string  $key the item key to be fetched
297
         * @param  boolean $xss if need apply some XSS rule on the value
298
         * @return array|mixed       the item value if the key exists or all array if the key is null
299
         */
300
        protected function getVars($type, $key = null, $xss = true) {
301
            $data = null;
302
            if ($key === null) {
303
                //return all
304
                $data = $this->{$type};
305
            } else if (array_key_exists($key, $this->{$type})) {
306
                $data = $this->{$type}[$key];
307
            }
308
            if ($xss) {
309
                $data = clean_input($data);
310
            }
311
            return $data;
312
        }
313
314
       
315
		
316
    }
317