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 request headers |
35
|
|
|
* @var array |
36
|
|
|
*/ |
37
|
|
|
private $header = null; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* The current request method 'GET', 'POST', 'PUT', etc. |
41
|
|
|
* @var null |
42
|
|
|
*/ |
43
|
|
|
private $method = null; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* The current request URI |
47
|
|
|
* @var string |
48
|
|
|
*/ |
49
|
|
|
private $requestUri = null; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Json body data |
53
|
|
|
* @var array |
54
|
|
|
*/ |
55
|
|
|
private $json = array(); |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Construct new request instance |
59
|
|
|
*/ |
60
|
|
|
public function __construct() { |
61
|
|
|
$this->method = $this->server('REQUEST_METHOD'); |
62
|
|
|
$this->requestUri = $this->server('REQUEST_URI'); |
63
|
|
|
$this->header = array(); |
64
|
|
|
//@codeCoverageIgnoreStart |
65
|
|
|
if (function_exists('apache_request_headers')) { |
66
|
|
|
$this->header = apache_request_headers(); |
|
|
|
|
67
|
|
|
} else if (function_exists('getallheaders')) { |
68
|
|
|
$this->header = getallheaders(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
//@codeCoverageIgnoreEnd |
72
|
|
|
$jsonBody = file_get_contents('php://input'); |
73
|
|
|
$this->json = json_decode($jsonBody, true); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Get the request method |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
|
|
public function method() { |
81
|
|
|
return $this->method; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Get the request URI |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
|
|
public function requestUri() { |
89
|
|
|
return $this->requestUri; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Get the value from $_REQUEST for given key. if the key is empty will return all values |
94
|
|
|
* @see GlobalVar::request |
95
|
|
|
*/ |
96
|
|
|
public function query($key = null, $xss = true) { |
97
|
|
|
return get_instance()->globalvar->request($key, $xss); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Get the value from $_GET for given key. if the key is empty will return all values |
102
|
|
|
* @see GlobalVar::get |
103
|
|
|
*/ |
104
|
|
|
public function get($key = null, $xss = true) { |
105
|
|
|
return get_instance()->globalvar->get($key, $xss); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Get the value from $_POST for given key. if the key is empty will return all values |
110
|
|
|
* @see GlobalVar::post |
111
|
|
|
*/ |
112
|
|
|
public function post($key = null, $xss = true) { |
113
|
|
|
return get_instance()->globalvar->post($key, $xss); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Get the value from $_SERVER for given key. if the key is empty will return all values |
118
|
|
|
* @see GlobalVar::server |
119
|
|
|
*/ |
120
|
|
|
public function server($key = null, $xss = false) { |
121
|
|
|
return get_instance()->globalvar->server($key, $xss); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Get the value from $_COOKIE for given key. if the key is empty will return all values |
126
|
|
|
* |
127
|
|
|
* NOTE: This super global is not filter by default |
128
|
|
|
* |
129
|
|
|
* @see GlobalVar::cookie |
130
|
|
|
*/ |
131
|
|
|
public function cookie($key = null, $xss = false) { |
132
|
|
|
return get_instance()->globalvar->cookie($key, $xss); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Get the value from $_FILES for given key. if the key is empty will return all values |
137
|
|
|
* @see GlobalVar::files |
138
|
|
|
*/ |
139
|
|
|
public function file($key, $xss = true) { |
140
|
|
|
return get_instance()->globalvar->files($key, $xss); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Get the value for header for given key. if the key is empty will return the all values |
145
|
|
|
* |
146
|
|
|
* NOTE: This is not filter by default |
147
|
|
|
* |
148
|
|
|
* @param string $key the item key to be fetched |
149
|
|
|
* @param boolean $xss if need apply some XSS rule on the value |
150
|
|
|
* @return array|mixed the item value if the key exists or all array if the key is null |
151
|
|
|
*/ |
152
|
|
|
public function header($key = null, $xss = true) { |
153
|
|
|
$data = null; |
154
|
|
|
if ($key === null) { |
155
|
|
|
//return all |
156
|
|
|
$data = $this->header; |
157
|
|
|
} else if (array_key_exists($key, $this->header)) { |
158
|
|
|
$data = $this->header[$key]; |
159
|
|
|
} |
160
|
|
|
if ($xss) { |
161
|
|
|
$data = clean_input($data); |
162
|
|
|
} |
163
|
|
|
return $data; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Set the value for header. |
168
|
|
|
* @param string|array $key the item key to be set or array if need set the current header |
169
|
|
|
* by this value |
170
|
|
|
* @param mixed $value the value to set if $key is not an array |
171
|
|
|
* |
172
|
|
|
* @return object the current instance |
173
|
|
|
*/ |
174
|
|
|
public function setHeader($key, $value = null) { |
175
|
|
|
if (is_array($key)) { |
176
|
|
|
//set all |
177
|
|
|
$this->header = $key; |
178
|
|
|
} else { |
179
|
|
|
$this->header[$key] = $value; |
180
|
|
|
} |
181
|
|
|
return $this; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Get the value for JSON body for given key. if the key is empty will return all values |
186
|
|
|
* |
187
|
|
|
* @codeCoverageIgnore |
188
|
|
|
* @param string $key the item key to be fetched |
189
|
|
|
* @param boolean $xss if need apply some XSS rule on the value |
190
|
|
|
* @return array|mixed the item value if the key exists or all array if the key is null |
191
|
|
|
*/ |
192
|
|
|
public function json($key = null, $xss = true) { |
193
|
|
|
$data = null; |
194
|
|
|
if ($key === null) { |
195
|
|
|
//return all |
196
|
|
|
$data = $this->json; |
197
|
|
|
} else if (array_key_exists($key, $this->json)) { |
198
|
|
|
$data = $this->json[$key]; |
199
|
|
|
} |
200
|
|
|
if ($xss) { |
201
|
|
|
$data = clean_input($data); |
202
|
|
|
} |
203
|
|
|
return $data; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
} |
207
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.