Passed
Push — 1.0.0-dev ( 294a70...e45720 )
by nguereza
02:47
created

Request::setServer()   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
nc 1
nop 2
dl 0
loc 2
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 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 Request {
32
		
33
        /**
34
         * The session instance
35
         * @var Session
36
         */
37
        private $session = null;
38
		
39
        /**
40
         * The request headers
41
         * @var array
42
         */
43
        private $header = null;
44
45
        /**
46
         * The current request method 'GET', 'POST', 'PUT', etc.
47
         * @var null
48
         */
49
        private $method = null;
50
51
        /**
52
         * The current request URI
53
         * @var string
54
         */
55
        private $requestUri = null;
56
		
57
        /**
58
         * Construct new request instance
59
         */
60
        public function __construct() {
61
            $this->session = & class_loader('Session', 'classes');
62
            $this->method = $this->server('REQUEST_METHOD');
63
            $this->requestUri = $this->server('REQUEST_URI');
64
            $this->header = array();
65
            //@codeCoverageIgnoreStart
66
            if (function_exists('apache_request_headers')) {
67
                $this->header = apache_request_headers();
68
            } else if (function_exists('getallheaders')) {
69
                $this->header = getallheaders();
70
            }
71
            //@codeCoverageIgnoreEnd
72
        }
73
74
        /**
75
         * Get the request method
76
         * @return string
77
         */
78
        public function method() {
79
            return $this->method;
80
        }
81
		
82
        /**
83
         * Get the request URI
84
         * @return string
85
         */
86
        public function requestUri() {
87
            return $this->requestUri;
88
        }
89
90
        /**
91
         * Get the value from $_REQUEST for given key. if the key is empty will return all values
92
         * @see GlobalVar::request 
93
         */
94
        public function query($key = null, $xss = true) {
95
            return get_instance()->globalvar->request($key, $xss);
96
        }
97
		
98
        /**
99
         * Get the value from $_GET for given key. if the key is empty will return all values
100
         * @see GlobalVar::get 
101
         */
102
        public function get($key = null, $xss = true) {
103
            return get_instance()->globalvar->get($key, $xss);
104
        }
105
		
106
        /**
107
         * Get the value from $_POST for given key. if the key is empty will return all values
108
         * @see GlobalVar::post 
109
         */
110
        public function post($key = null, $xss = true) {
111
            return get_instance()->globalvar->post($key, $xss);
112
        }
113
		
114
        /**
115
         * Get the value from $_SERVER for given key. if the key is empty will return all values
116
         * @see GlobalVar::server 
117
         */
118
        public function server($key = null, $xss = false) {
119
            return get_instance()->globalvar->server($key, $xss);
120
        }
121
		
122
        /**
123
         * Get the value from $_COOKIE for given key. if the key is empty will return all values
124
         *
125
         *  NOTE: This super global is not filter by default
126
         *  
127
         * @see GlobalVar::cookie 
128
         */
129
        public function cookie($key = null, $xss = false) {
130
            return get_instance()->globalvar->cookie($key, $xss);
131
        }
132
		
133
        /**
134
         * Get the value from $_FILES for given key. if the key is empty will return all values
135
         * @see GlobalVar::files 
136
         */
137
        public function file($key, $xss = true) {
138
            return get_instance()->globalvar->files($key, $xss);
139
        }
140
		
141
        /**
142
         * Get the value from $_SESSION for given key. if the key is empty will return the all values
143
         *
144
         *  NOTE: This super global is not filter by default
145
         *  
146
         * @param  string  $key the item key to be set or array if need set the current global variable 
147
         * by this value
148
         * @param  boolean $xss if need apply some XSS attack rule on the value
149
         * @return array|mixed       the item value if the key exists or null if the key does not exists
150
         */
151
        public function session($key, $xss = false) {
152
            $session = $this->session->get($key);
153
            if ($xss) {
154
                $session = clean_input($session);
155
            }
156
            return $session;
157
        }
158
159
        /**
160
         * Get the value for header for given key. if the key is empty will return the all values
161
         *
162
         *  NOTE: This is not filter by default
163
         *  
164
         * @param  string  $key the item key to be fetched
165
         * @param  boolean $xss if need apply some XSS rule on the value
166
         * @return array|mixed       the item value if the key exists or all array if the key is null
167
         */
168
        public function header($key = null, $xss = true) {
169
            $data = null;
170
            if ($key === null) {
171
                //return all
172
                $data = $this->header;
173
            } else if (array_key_exists($key, $this->header)) {
174
                $data = $this->header[$key];
175
            }
176
            if ($xss) {
177
                $data = clean_input($data);
178
            }
179
            return $data;
180
        }
181
182
        /**
183
         * Set the value for header.
184
         * @param  string  $key the item key to be set or array if need set the current header  
185
         * by this value
186
         * @param mixed $value the value to set if $key is not an array
187
         * 
188
         * @return object       the current instance
189
         */
190
        public function setHeader($key, $value = null) {
191
            if (is_array($key)) {
0 ignored issues
show
introduced by
The condition is_array($key) is always false.
Loading history...
192
                //set all
193
                $this->header = $key;
194
            } else {
195
                $this->header[$key] = $value;
196
            }
197
            return $this;
198
        }
199
200
        /**
201
         * Set the instance for session.
202
         * @param object|null $session the object of Session to be set
203
         * @return object the current instance
204
         */
205
        public function setSession(Session $session = null) {
206
            $this->session = $session;
207
            return $this;
208
        }
209
210
         /**
211
         * Return the instance of session.
212
         * @return object the session instance
213
         */
214
        public function getSession() {
215
            return $this->session;
216
        }
217
218
    }
219