Test Failed
Push — 1.0.0-dev ( 9f6e47...c74626 )
by nguereza
02:36
created

Request::cookie()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
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 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
        public $get = null;
34
35
        /**
36
         * The value for the super global $_POST
37
         * @var array
38
         */
39
        public $post = null;
40
41
        /**
42
         * The value for the super global $_SERVER
43
         * @var array
44
         */
45
        public $server = null;
46
47
        /**
48
         * The value for the super global $_COOKIE
49
         * @var array
50
         */
51
        public $cookie = null;
52
53
        /**
54
         * The value for the super global $_FILES
55
         * @var array
56
         */
57
        public $file = null;
58
59
        /**
60
         * The value for the super global $_REQUEST
61
         * @var array
62
         */
63
        public $query = null;
64
		
65
        /**
66
         * The session instance
67
         * @var Session
68
         */
69
        public $session = null;
70
		
71
        /**
72
         * The request headers
73
         * @var array
74
         */
75
        public $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
         * Get the value from $_GET, $_POST, $_SERVER etc. for given key. if the key is empty will return the all values
201
         * @param string $type the type can be "post", "get", etc.
202
         * @param  string  $key the item key to be fetched
203
         * @param  boolean $xss if need apply some XSS rule on the value
204
         * @return array|mixed       the item value if the key exists or all array if the key is null
205
         */
206
        protected function getVars($type, $key = null, $xss = true) {
207
            $data = null;
208
            if ($key !== null) {
209
                //return all
210
                $data = $this->{$type};
211
            } else if (array_key_exists($key, $this->{$type})) {
212
                $data = $this->{$type}[$key];
213
            }
214
            if ($xss) {
215
                $data = clean_input($data);
216
            }
217
            return $data;
218
        }
219
220
       
221
		
222
    }
223