|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* |
|
4
|
|
|
* KNUT7 K7F (https://marciozebedeu.com/) |
|
5
|
|
|
* KNUT7 K7F (tm) : Rapid Development Framework (https://marciozebedeu.com/) |
|
6
|
|
|
* |
|
7
|
|
|
* Licensed under The MIT License |
|
8
|
|
|
* For full copyright and license information, please see the LICENSE.txt |
|
9
|
|
|
* Redistributions of files must retain the above copyright notice. |
|
10
|
|
|
* |
|
11
|
|
|
* @link https://github.com/knut7/framework/ for the canonical source repository |
|
12
|
|
|
* @copyright (c) 2015. KNUT7 Software Technologies AO Inc. (https://marciozebedeu.com/) |
|
13
|
|
|
* @license https://marciozebedeu.com/license/new-bsd New BSD License |
|
14
|
|
|
* @author Marcio Zebedeu - [email protected] |
|
15
|
|
|
* @version 1.0.14 |
|
16
|
|
|
* |
|
17
|
|
|
* |
|
18
|
|
|
*/ |
|
19
|
|
|
|
|
20
|
|
|
namespace Ballybran\Routing\Router; |
|
21
|
|
|
|
|
22
|
|
|
class RouterRequest |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @var string $validMethods Valid methods for Requests |
|
26
|
|
|
*/ |
|
27
|
|
|
public static $validMethods = 'GET|POST|PUT|DELETE|HEAD|OPTIONS|PATCH|ANY|AJAX|XPOST|XPUT|XDELETE|XPATCH'; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Request method validation |
|
31
|
|
|
* |
|
32
|
|
|
* @param string $data |
|
33
|
|
|
* @param string $method |
|
34
|
|
|
* |
|
35
|
|
|
* @return bool |
|
36
|
|
|
*/ |
|
37
|
|
|
public static function validMethod($data, $method) |
|
38
|
|
|
{ |
|
39
|
|
|
$valid = false; |
|
40
|
|
|
if (strstr($data, '|')) { |
|
41
|
|
|
foreach (explode('|', $data) as $value) { |
|
42
|
|
|
$valid = self::checkMethods($value, $method); |
|
43
|
|
|
if ($valid) { |
|
44
|
|
|
break; |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
} else { |
|
48
|
|
|
$valid = self::checkMethods($data, $method); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
return $valid; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Get the request method used, taking overrides into account |
|
56
|
|
|
* |
|
57
|
|
|
* @return string |
|
58
|
|
|
*/ |
|
59
|
|
|
public static function getRequestMethod() |
|
60
|
|
|
{ |
|
61
|
|
|
// Take the method as found in $_SERVER |
|
62
|
|
|
$method = $_SERVER['REQUEST_METHOD']; |
|
63
|
|
|
// If it's a HEAD request override it to being GET and prevent any output, as per HTTP Specification |
|
64
|
|
|
// @url http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.4 |
|
65
|
|
|
if ($method === 'HEAD') { |
|
66
|
|
|
ob_start(); |
|
67
|
|
|
$method = 'GET'; |
|
68
|
|
|
} elseif ($method === 'POST') { |
|
69
|
|
|
$headers = self::getRequestHeaders(); |
|
70
|
|
|
if (isset($headers['X-HTTP-Method-Override']) && |
|
71
|
|
|
in_array($headers['X-HTTP-Method-Override'], ['PUT', 'DELETE', 'PATCH', 'OPTIONS', 'HEAD'])) { |
|
72
|
|
|
$method = $headers['X-HTTP-Method-Override']; |
|
73
|
|
|
} elseif (! empty($_POST['_method'])) { |
|
74
|
|
|
$method = strtoupper($_POST['_method']); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
return $method; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* check method valid |
|
83
|
|
|
* |
|
84
|
|
|
* @param string $value |
|
85
|
|
|
* @param string $method |
|
86
|
|
|
* |
|
87
|
|
|
* @return bool |
|
88
|
|
|
*/ |
|
89
|
|
|
protected static function checkMethods($value, $method) |
|
90
|
|
|
{ |
|
91
|
|
|
if (in_array($value, explode('|', self::$validMethods))) { |
|
92
|
|
|
if (self::isAjax() && $value === 'AJAX') { |
|
93
|
|
|
return true; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
if (self::isAjax() && strpos($value, 'X') === 0 && $method === ltrim($value, 'X')) { |
|
97
|
|
|
return true; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
if (in_array($value, [$method, 'ANY'])) { |
|
101
|
|
|
return true; |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
return false; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Check ajax request |
|
110
|
|
|
* |
|
111
|
|
|
* @return bool |
|
112
|
|
|
*/ |
|
113
|
|
|
protected static function isAjax() |
|
114
|
|
|
{ |
|
115
|
|
|
return (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] === 'XMLHttpRequest'); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Get all request headers |
|
120
|
|
|
* |
|
121
|
|
|
* @return array |
|
122
|
|
|
*/ |
|
123
|
|
|
protected static function getRequestHeaders() |
|
124
|
|
|
{ |
|
125
|
|
|
// If getallheaders() is available, use that |
|
126
|
|
|
if (function_exists('getallheaders')) { |
|
127
|
|
|
return getallheaders(); |
|
|
|
|
|
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
// Method getallheaders() not available: manually extract 'm |
|
131
|
|
|
$headers = []; |
|
132
|
|
|
foreach ($_SERVER as $name => $value) { |
|
133
|
|
|
if (substr($name, 0, 5) == 'HTTP_' || $name === 'CONTENT_TYPE' || $name === 'CONTENT_LENGTH') { |
|
134
|
|
|
$headerKey = str_replace( |
|
135
|
|
|
[' ', 'Http'], |
|
136
|
|
|
['-', 'HTTP'], |
|
137
|
|
|
ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))) |
|
138
|
|
|
); |
|
139
|
|
|
$headers[$headerKey] = $value; |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
return $headers; |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|
If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.