1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* ================================== |
4
|
|
|
* Responsible PHP API |
5
|
|
|
* ================================== |
6
|
|
|
* |
7
|
|
|
* @link Git https://github.com/vince-scarpa/responsibleAPI.git |
8
|
|
|
* |
9
|
|
|
* @api Responible API |
10
|
|
|
* @package responsible\core\headers |
11
|
|
|
* |
12
|
|
|
* @author Vince scarpa <[email protected]> |
13
|
|
|
* |
14
|
|
|
*/ |
15
|
|
|
namespace responsible\core\headers; |
16
|
|
|
|
17
|
|
|
use responsible\core\helpers\help as helper; |
18
|
|
|
|
19
|
|
|
class headerVerbs extends header |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* [$contentType Requested content type] |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
private static $contentType = [ |
26
|
|
|
'name' => '', |
27
|
|
|
'type' => '' |
28
|
|
|
]; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* [__construct] |
32
|
|
|
*/ |
33
|
|
|
public function __construct() |
34
|
|
|
{ |
35
|
|
|
self::setContentType(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* [get GET request method] |
40
|
|
|
* @return array |
41
|
|
|
*/ |
42
|
|
|
public function get(): array |
43
|
|
|
{ |
44
|
|
|
$_GET = ($_GET) ?? []; |
45
|
|
|
return [ |
46
|
|
|
'method' => 'get', |
47
|
|
|
'data' => $_GET, |
48
|
|
|
]; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* [post POST request method] |
53
|
|
|
* @return array |
54
|
|
|
*/ |
55
|
|
|
public function post(): array |
56
|
|
|
{ |
57
|
|
|
$_postData = ($_POST) ?? []; |
58
|
|
|
|
59
|
|
|
$jsonData = json_decode(file_get_contents("php://input")); |
60
|
|
|
|
61
|
|
|
if (is_object($jsonData) || is_array($jsonData)) { |
62
|
|
|
$_postData = json_decode(file_get_contents("php://input"), true); |
63
|
|
|
} |
64
|
|
|
$_POST = array_merge($_REQUEST, $_POST); |
65
|
|
|
$_REQUEST = array_merge($_POST, $_postData); |
66
|
|
|
|
67
|
|
|
$_GET = $this->get()['data']; |
68
|
|
|
$_REQUEST = array_merge($_GET, $_REQUEST); |
69
|
|
|
|
70
|
|
|
return [ |
71
|
|
|
'method' => 'post', |
72
|
|
|
'data' => $_REQUEST, |
73
|
|
|
]; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* [options OPTIONS CORS request] |
78
|
|
|
* @return array |
79
|
|
|
*/ |
80
|
|
|
public function options(): array |
81
|
|
|
{ |
82
|
|
|
return $this->post(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* [post PUT request method] |
87
|
|
|
* @return array |
88
|
|
|
*/ |
89
|
|
|
public function put(): array |
90
|
|
|
{ |
91
|
|
|
$_PUT = []; |
92
|
|
|
|
93
|
|
|
if (self::getContentType()['type'] === 'json') { |
|
|
|
|
94
|
|
|
$putfp = fopen('php://input', 'r'); |
95
|
|
|
$putdata = ''; |
96
|
|
|
while ($data = fread($putfp, 1024)) { |
97
|
|
|
$putdata .= $data; |
98
|
|
|
} |
99
|
|
|
fclose($putfp); |
100
|
|
|
|
101
|
|
|
if (!empty($putdata)) { |
102
|
|
|
$_PUT = json_decode($putdata, true); |
103
|
|
|
if (json_last_error() !== 0) { |
104
|
|
|
$_PUT = []; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
} else { |
109
|
|
|
parse_str(file_get_contents("php://input"), $_PUT); |
110
|
|
|
foreach ($_PUT as $key => $value) { |
111
|
|
|
unset($_PUT[$key]); |
112
|
|
|
$_PUT[str_replace('amp;', '', $key)] = $value; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
// [TODO] |
117
|
|
|
if (self::getContentType()['name'] === 'multipart/form-data') { |
118
|
|
|
|
119
|
|
|
} |
120
|
|
|
if (self::getContentType()['type'] === 'x-www-form-urlencoded') { |
121
|
|
|
|
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
$_REQUEST = array_merge($_REQUEST, $_PUT); |
125
|
|
|
$_REQUEST = array_merge($_POST, $this->post()['data']); |
126
|
|
|
$_REQUEST = array_merge($this->get()['data'], $_REQUEST); |
127
|
|
|
|
128
|
|
|
return [ |
129
|
|
|
'method' => 'put', |
130
|
|
|
'data' => $_REQUEST, |
131
|
|
|
]; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* [post DELETE request method] |
136
|
|
|
* @return array |
137
|
|
|
*/ |
138
|
|
|
public function delete() |
139
|
|
|
{ |
140
|
|
|
return ['method' => 'delete', 'data' => $this->get()['data']]; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* [post PATCH request method] |
145
|
|
|
* @return array |
146
|
|
|
*/ |
147
|
|
|
public function patch() |
148
|
|
|
{ |
149
|
|
|
return ['method' => 'patch', 'data' => $this->put()['data']]; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* [getRequestVerb Get the requested method/ verb] |
154
|
|
|
* @return string |
155
|
|
|
*/ |
156
|
|
|
public static function getRequestVerb():string |
157
|
|
|
{ |
158
|
|
|
$helper = new helper; |
159
|
|
|
$method = $helper->checkVal(@$_SERVER, 'REQUEST_METHOD', '' ); |
160
|
|
|
$method = $helper->checkVal(@$_SERVER, 'HTTP_X_HTTP_METHOD', $method ); |
161
|
|
|
|
162
|
|
|
return strtolower($method); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* [setContentType Set content type] |
167
|
|
|
* @return void |
168
|
|
|
*/ |
169
|
|
|
public static function setContentType() |
170
|
|
|
{ |
171
|
|
|
$helper = new helper; |
172
|
|
|
$contentType = $helper->checkVal(@$_SERVER, 'CONTENT_TYPE', ''); |
173
|
|
|
$contentType = $helper->checkVal(@$_SERVER, 'HTTP_CONTENT_TYPE', $contentType); |
174
|
|
|
$contentType = $helper->checkVal(@$_SERVER, 'HTTP_X_CONTENT_TYPE', $contentType); |
175
|
|
|
|
176
|
|
|
if (empty($contentType)) { |
177
|
|
|
return ''; |
|
|
|
|
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
if (preg_match('@multipart/form-data@', $contentType)) { |
181
|
|
|
self::$contentType = [ |
182
|
|
|
'name' => 'multipart/form-data', |
183
|
|
|
'type' => 'form-data', |
184
|
|
|
]; |
185
|
|
|
return; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
$contentTypeName = explode('/', $contentType); |
189
|
|
|
$contentTypeName = ($contentTypeName[1]) ?? ''; |
190
|
|
|
|
191
|
|
|
self::$contentType = [ |
192
|
|
|
'name' => $contentType, |
193
|
|
|
'type' => $contentTypeName, |
194
|
|
|
]; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* [getContentType Get the requested content type] |
199
|
|
|
* @return array |
200
|
|
|
*/ |
201
|
|
|
public function getContentType(): array |
202
|
|
|
{ |
203
|
|
|
return self::$contentType; |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
|