Passed
Branch master (93b6ae)
by refat
12:48
created

Request::setPost()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 2
b 0
f 0
nc 1
nop 2
dl 0
loc 3
rs 10
1
<?php
2
3
namespace System\Http;
4
5
use System\Application;
6
7
class Request
8
{
9
  /**
10
   * Application Object
11
   *
12
   * @var \System\Application
13
   */
14
  private $app;
15
16
  /**
17
   * Url
18
   *
19
   * @var string
20
   */
21
  private $url;
22
23
  /**
24
   * Base Url
25
   *
26
   * @var string
27
   */
28
  private $baseUrl;
29
30
  /**
31
   * Uploaded files container
32
   *
33
   * @var array
34
   */
35
  private $files = [];
36
37
  /**
38
   * Host
39
   *
40
   * @var string
41
   */
42
  private $host;
43
44
  /**
45
   * Constructor
46
   *
47
   * @param \System\Application $app
48
   */
49
  public function __construct(Application $app)
50
  {
51
    $this->app = $app;
52
  }
53
54
  /**
55
   * Prepare url
56
   *
57
   * @return void
58
   */
59
  public function prepareUrl()
60
  {
61
    $script = dirname($this->server('SCRIPT_NAME'));
62
63
    $requestUri = $this->server('REQUEST_URI');
64
65
    if (strpos($requestUri, '?')) {
66
67
      list($requestUri, $queryString) = explode('?', $requestUri);
68
    }
69
70
    $this->url = $this->cleanUrl($script, $requestUri);
71
72
    $REQUEST_PROTOCOL = $this->isSecure() ? 'https' : 'http';
73
74
    $this->host = $REQUEST_PROTOCOL . '://' . $this->server('HTTP_HOST');
75
76
    $this->baseUrl = $this->host . $requestUri;
77
  }
78
79
  /**
80
   * Clean url
81
   *
82
   * @param string $script
83
   * @param string $default
84
   * @return string
85
   */
86
  private function cleanUrl($script, $requestUri)
87
  {
88
    if (!in_array($script, ['/', '\\'])) {
89
90
      $url = preg_replace('#^' . $script . '#', '', $requestUri);
91
92
    } else {
93
94
      $url = $requestUri;
95
    }
96
97
    if ($url !== '/') {
98
99
      $url = rtrim($url, '/');
100
    }
101
102
    return $url;
103
  }
104
105
  /**
106
   * Check if the website is secure
107
   *
108
   * @return bool
109
   */
110
  private function isSecure()
111
  {
112
    if ($this->checkHttp() || $this->checkHttpXforwardedProto() || $this->checkHttpXforwardedSsl()) {
113
114
      return true;
115
    }
116
117
    return false;
118
  }
119
120
  /**
121
   * Check if HTTPS is 'on'
122
   *
123
   * @return bool
124
   */
125
  private function checkHttp()
126
  {
127
    if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') {
128
129
      return true;
130
    }
131
132
    return false;
133
  }
134
135
  /**
136
   * Check if HTTP_X_FORWARDED_PROTO is not empty or 'https'
137
   *
138
   * @return bool
139
   */
140
  private function checkHttpXforwardedProto()
141
  {
142
    if (!empty($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') {
143
144
      return true;
145
    }
146
147
    return false;
148
  }
149
150
  /**
151
   * Check if HTTP_X_FORWARDED_SSL is 'on'
152
   *
153
   * @return bool
154
   */
155
  private function checkHttpXforwardedSsl()
156
  {
157
    if (!empty($_SERVER['HTTP_X_FORWARDED_SSL']) && $_SERVER['HTTP_X_FORWARDED_SSL'] == 'on') {
158
159
      return true;
160
    }
161
162
    return false;
163
  }
164
165
  /**
166
   * Get value from $_GET by the given key
167
   *
168
   * @param string $key
169
   * @return mixed
170
   */
171
  public function get($key)
172
  {
173
    $value = array_get($_GET, $key);
174
175
    if (is_array($value)) {
176
177
      $value = array_filter($value);
178
179
    } else {
180
181
      $value = trim($value);
182
    }
183
184
    return $value;
185
  }
186
187
  /**
188
   * Get value from $_POST by the given key
189
   *
190
   * @param string $key
191
   * @return mixed
192
   */
193
  public function post($key)
194
  {
195
    $value = array_get($_POST, $key);
196
197
    if (is_array($value)) {
198
199
      $value = array_filter($value);
200
201
    } else {
202
203
      $value = trim($value);
204
    }
205
206
    return $value;
207
  }
208
209
  /**
210
   * Set value To $_POST For the given key
211
   *
212
   * @param string $key
213
   * @param mixed $valuet
214
   * @return mixed
215
   */
216
  public function setPost($key, $value)
217
  {
218
    $_POST[$key] = $value;
219
  }
220
221
  /**
222
   * Get $_POST
223
   *
224
   * @return array
225
   */
226
  public function posts()
227
  {
228
    return $_POST;
229
  }
230
231
  /**
232
   * Get $_GET
233
   *
234
   * @return array
235
   */
236
  public function gets()
237
  {
238
    return $_GET;
239
  }
240
241
  /**
242
   * Get $_FILES
243
   *
244
   * @return array
245
   */
246
  public function files()
247
  {
248
    return $_FILES;
249
  }
250
251
  /**
252
   * Get the uploaded file object for the given input
253
   *
254
   * @param string $input
255
   * @return System\Http\UploadeFile\
0 ignored issues
show
Documentation Bug introduced by
The doc comment System\Http\UploadeFile\ at position 0 could not be parsed: Unknown type name 'System\Http\UploadeFile\' at position 0 in System\Http\UploadeFile\.
Loading history...
256
   */
257
  public function file($input)
258
  {
259
    if (isset($this->files[$input])) {
260
261
      return $this->files[$input];
262
    }
263
264
    $upoadedFile = new UploadeFile($this->app, $input);
265
266
    $this->files[$input] = $upoadedFile;
267
268
    return $this->files[$input];
269
  }
270
271
  /**
272
   * Get value from $_SERVER by the given key
273
   *
274
   * @param string $key
275
   * @return mixed
276
   */
277
  public function server($key)
278
  {
279
    return array_get($_SERVER, $key);
280
  }
281
282
  /**
283
   * Get current request method
284
   *
285
   * @return string
286
   */
287
  public function method()
288
  {
289
    return $this->server('REQUEST_METHOD');
290
  }
291
292
  /**
293
   * Get the referer link
294
   *
295
   * @return string
296
   */
297
  public function referer()
298
  {
299
    return $this->server('HTTP_REFERER');
300
  }
301
302
  /**
303
   * Get full url of the script
304
   *
305
   * @return string
306
   */
307
  public function baseUrl()
308
  {
309
    return $this->baseUrl;
310
  }
311
312
  /**
313
   * Get only relative url (clean url)
314
   *
315
   * @return string
316
   */
317
  public function url()
318
  {
319
    return $this->url;
320
  }
321
322
  /**
323
   * Get only host
324
   *
325
   * @return string
326
   */
327
  public function host()
328
  {
329
    return $this->host;
330
  }
331
332
  /**
333
   * Check if the request to the admin panel
334
   *
335
   * @return bool
336
   */
337
  public function isRequestToAdminManagement()
338
  {
339
    $url = $this->url;
340
341
    $url = ltrim($url, '/');
342
343
    $url = explode('/', $url)[0];
344
345
    return $url == 'admin';
346
  }
347
348
  public function isMatchingRequestMethod($methods = ['GET'])
349
  {
350
    if (!is_array($methods)) {
351
352
      $methods = [$methods];
353
    }
354
355
    if (empty($methods)) {
356
357
      $methods = ['GET'];
358
    }
359
360
    foreach($methods as $method) {
361
362
      if ($this->method() == strtoupper($method)) {
363
364
        return true;
365
      }
366
    }
367
368
    return false;
369
  }
370
371
372
  public function canRequestContinue($middlewares)
373
  {
374
    if (empty($middlewares)) {
375
376
      return true;
377
    }
378
379
    foreach ($middlewares as $middleware) {
380
      $output = $this->app->load->middleware($middleware)->handle();
381
      if (!$output) {
382
383
        return false;
384
      }
385
    }
386
387
    return true;
388
  }
389
}
390