PluginProbe
ManageWP Worker / 4.9.25
ManageWP Worker v4.9.25
4.9.38 4.9.37 4.9.36 4.9.35 4.9.34 3.8.7 3.8.8 3.9.0 3.9.1 3.9.10 3.9.11 3.9.12 3.9.13 3.9.14 3.9.15 3.9.16 3.9.17 3.9.18 3.9.19 3.9.2 3.9.20 3.9.21 3.9.22 3.9.23 3.9.24 All 73 releases
worker / src / MWP / Worker / Request.php

Request.php in ManageWP Worker 4.9.25, at src/MWP/Worker/Request.php

454 lines 12.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 * This file is part of the ManageWP Worker plugin.
4 *
5 * (c) ManageWP LLC <contact@managewp.com>
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10
11 /**
12 * Class MWP_Worker_Request
13 */
14 class MWP_Worker_Request
15 {
16
17 /**
18 * Header that contains the name of the action to execute.
19 * Must be compliant with {@link http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html RFC 2616}
20 *
21 * @var string
22 */
23 protected $actionHeaderName = 'MWP-Action';
24
25 /**
26 * Header that contains the ID of the action to execute.
27 * Must be compliant with {@link http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html RFC 2616}
28 *
29 * @var string
30 */
31 protected $messageIdHeaderName = 'MWP-Message-ID';
32
33 /**
34 * Header that contains message signature.
35 * Must be compliant with {@link http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html RFC 2616}
36 *
37 * @var string
38 */
39 protected $signatureHeaderName = 'MWP-Signature';
40
41 /**
42 * Header that contains the name of the key used for signing.
43 * Must be compliant with {@link http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html RFC 2616}
44 *
45 * @var string
46 */
47 protected $keyNameHeaderName = 'MWP-Key-Name';
48
49 /**
50 * Header that contains the message signed by the corresponding service.
51 * Must be compliant with {@link http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html RFC 2616}
52 *
53 * @var string
54 */
55 protected $serviceSignatureHeaderName = 'MWP-Service-Signature';
56
57 protected $signatureNoHostHeaderName = 'MWP-Signature-G';
58
59 protected $signatureAlgorithmHeaderName = 'MWP-Signature-Algorithm';
60
61 protected $serviceSignatureSHA256HeaderName = 'MWP-V2-Service-Signature';
62
63 protected $signatureNoHostSHA256HeaderName = 'MWP-V2-Signature-G';
64
65 /**
66 * Header that contains the communication key.
67 * Must be compliant with {@link http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html RFC 2616}
68 *
69 * @var string
70 */
71 protected $communicationKeyHeaderName = 'MWP-Communication-Key';
72
73 protected $siteIdHeaderName = 'MWP-Site-Id';
74
75 protected $protocolVersionHeaderName = 'MWP-Protocol';
76
77 /**
78 * @var bool
79 */
80 private $initialized = false;
81
82 /**
83 * The GET parameters.
84 *
85 * @var array
86 */
87 public $query;
88
89 /**
90 * The POST parameters.
91 *
92 * @var array
93 */
94 public $request;
95
96 /**
97 * The request attributes.
98 *
99 * @var array
100 */
101 public $attributes;
102
103 /**
104 * The COOKIE parameters.
105 *
106 * @var array
107 */
108 public $cookies;
109
110 /**
111 * The FILES parameters.
112 *
113 * @var array
114 */
115 public $files;
116
117 /**
118 * The SERVER parameters.
119 *
120 * @var array
121 */
122 public $server;
123
124 /**
125 * The raw request body data.
126 *
127 * @var string
128 */
129 private $content;
130
131 private $method;
132
133 /**
134 * @param array $query The GET parameters.
135 * @param array $request The POST parameters.
136 * @param array $attributes The request attributes.
137 * @param array $cookies The COOKIE parameters.
138 * @param array $files The FILES parameters.
139 * @param array $server The SERVER parameters.
140 * @param null|string $content The raw request body data. If null, it will be lazy-loaded.
141 */
142 public function __construct($query = array(), $request = array(), $attributes = array(), $cookies = array(), $files = array(), $server = array(), $content = null)
143 {
144 $this->query = $query;
145 $this->request = $request;
146 $this->attributes = $attributes;
147 $this->cookies = $cookies;
148 $this->files = $files;
149 $this->server = $server;
150 $this->content = $content;
151 }
152
153 /**
154 * Gets the request method.
155 *
156 * The method is always an uppercased string.
157 *
158 * @return string The request method
159 */
160 public function getMethod()
161 {
162 if (null === $this->method) {
163 $this->method = isset($this->server['REQUEST_METHOD']) ? strtoupper($this->server['REQUEST_METHOD']) : 'GET';
164 }
165
166 return $this->method;
167 }
168
169 /**
170 * Sets the request method.
171 *
172 * @param string $method
173 */
174 public function setMethod($method)
175 {
176 $this->method = null;
177 $this->server['REQUEST_METHOD'] = $method;
178 }
179
180 /**
181 * MWP_Worker factory.
182 *
183 * @return MWP_Worker_Request
184 */
185 public static function createFromGlobals()
186 {
187 $request = new self($_GET, $_POST, array(), $_COOKIE, $_FILES, $_SERVER);
188
189 return $request;
190 }
191
192 /**
193 * @throws RuntimeException If the request is already initialized.
194 */
195 public function initialize()
196 {
197 if ($this->initialized) {
198 throw new RuntimeException('Request is already initialized.');
199 }
200 $this->initialized = true;
201 $header = '';
202 if (null !== $this->getHeader($this->signatureHeaderName)) {
203 $header = base64_decode($this->getHeader($this->signatureHeaderName));
204 }
205
206 $this->attributes['action'] = $this->getHeader($this->actionHeaderName);
207 $this->attributes['id'] = $this->getHeader($this->messageIdHeaderName);
208 $this->attributes['signature'] = $header;
209 $this->attributes['key_name'] = $this->getHeader($this->keyNameHeaderName);
210 $this->attributes['service_signature'] = $this->getHeader($this->serviceSignatureHeaderName);
211 $this->attributes['service_signature_v2'] = $this->getHeader($this->serviceSignatureSHA256HeaderName);
212 $this->attributes['no_host_signature'] = $this->getHeader($this->signatureNoHostHeaderName);
213 $this->attributes['no_host_signature_v2'] = $this->getHeader($this->signatureNoHostSHA256HeaderName);
214 $this->attributes['communication_key'] = $this->getHeader($this->communicationKeyHeaderName);
215 $this->attributes['site_id'] = $this->getHeader($this->siteIdHeaderName);
216 $this->attributes['data'] = null;
217 $this->attributes['params'] = null;
218 $this->attributes['setting'] = null;
219 $this->attributes['user'] = null;
220 $this->attributes['authenticated'] = false;
221 $this->attributes['protocol'] = (int)$this->getHeader($this->protocolVersionHeaderName);
222 $this->attributes['signature_algorithm'] = null;
223
224 if (!empty($this->attributes['service_signature'])) {
225 $this->attributes['service_signature'] = base64_decode($this->attributes['service_signature']);
226 }
227
228 if (!empty($this->attributes['service_signature_v2'])) {
229 $this->attributes['service_signature_v2'] = base64_decode($this->attributes['service_signature_v2']);
230 }
231
232 if (!empty($this->getHeader($this->signatureAlgorithmHeaderName))) {
233 $this->attributes['signature_algorithm'] = $this->getHeader($this->signatureAlgorithmHeaderName);
234 }
235
236 if (!empty($this->attributes['no_host_signature'])) {
237 $this->attributes['no_host_signature'] = base64_decode($this->attributes['no_host_signature']);
238 }
239
240 if (!empty($this->attributes['no_host_signature_v2'])) {
241 $this->attributes['no_host_signature_v2'] = base64_decode($this->attributes['no_host_signature_v2']);
242 }
243
244 // Do we have {"params":{...}} inside of body?
245 if ($this->isMasterRequest() && is_array($data = json_decode($this->getContent(), true)) && array_key_exists('params', $data)) {
246 $this->attributes['data'] = $data;
247 $this->attributes['params'] = $data['params'];
248 $this->attributes['setting'] = array_key_exists('setting', $data) ? $data['setting'] : null;
249 $this->attributes['user'] = (isset($data['params']['username']) && is_scalar($data['params']['username'])) ? $data['params']['username'] : null;
250 }
251 }
252
253 /**
254 * @return bool
255 */
256 public function isInitialized()
257 {
258 return $this->initialized;
259 }
260
261 /**
262 * @param string $actionHeaderName
263 */
264 public function setActionHeaderName($actionHeaderName)
265 {
266 $this->actionHeaderName = $actionHeaderName;
267 }
268
269 /**
270 * @param string $signatureHeaderName
271 */
272 public function setSignatureHeaderName($signatureHeaderName)
273 {
274 $this->signatureHeaderName = $signatureHeaderName;
275 }
276
277 /**
278 * @param bool $asResource If true, a resource will be returned
279 *
280 * @return resource|string The request body content or a resource to read the body stream.
281 * @throws RuntimeException If attempting to call the method again after getting it as a resource previously.
282 */
283 public function getContent($asResource = false)
284 {
285 if (false === $this->content || (true === $asResource && null !== $this->content)) {
286 throw new RuntimeException('getContent() can only be called once when using the resource return type.');
287 }
288
289 if (true === $asResource) {
290 $this->content = false;
291
292 return fopen('php://input', 'rb');
293 }
294
295 if (null === $this->content) {
296 $this->content = file_get_contents('php://input');
297 }
298
299 return $this->content;
300 }
301
302 /**
303 * @return bool Whether the current request is sent by the master script.
304 */
305 public function isMasterRequest()
306 {
307 if ($this->getMethod() !== 'POST') {
308 return false;
309 }
310
311 $masterHeader = $this->getHeader($this->actionHeaderName);
312 if ($masterHeader === null) {
313 return false;
314 }
315
316 return true;
317 }
318
319 /**
320 * @return null|string
321 */
322 public function getAction()
323 {
324 return $this->attributes['action'];
325 }
326
327 /**
328 * @return null|string
329 */
330 public function getSignature()
331 {
332 return $this->attributes['signature'];
333 }
334
335 /**
336 * @return null|string
337 */
338 public function getKeyName()
339 {
340 return $this->attributes['key_name'];
341 }
342
343 /**
344 * @return null|string
345 */
346 public function getServiceSignature()
347 {
348 return $this->attributes['service_signature'];
349 }
350
351 public function getServiceSignatureV2()
352 {
353 return $this->attributes['service_signature_v2'];
354 }
355
356 public function getSignatureAlgorithm()
357 {
358 return $this->attributes['signature_algorithm'];
359 }
360
361 /**
362 * @return null|string
363 */
364 public function getNoHostSignature()
365 {
366 return $this->attributes['no_host_signature'];
367 }
368
369 public function getNoHostSignatureV2()
370 {
371 return $this->attributes['no_host_signature_v2'];
372 }
373
374 /**
375 * @return string
376 */
377 public function getCommunicationKey()
378 {
379 return !empty($this->attributes['communication_key']) ? $this->attributes['communication_key'] : '';
380 }
381
382 /**
383 * @return string
384 */
385 public function getSiteId()
386 {
387 return !empty($this->attributes['site_id']) ? $this->attributes['site_id'] : '';
388 }
389
390 /**
391 * @return null|string
392 */
393 public function getUsername()
394 {
395 return $this->attributes['user'];
396 }
397
398 public function getNonce()
399 {
400 return $this->attributes['id'];
401 }
402
403 public function getParams()
404 {
405 return $this->attributes['params'];
406 }
407
408 public function getData()
409 {
410 return $this->attributes['data'];
411 }
412
413 public function getSetting()
414 {
415 return $this->attributes['setting'];
416 }
417
418 /**
419 * @param string $header Header name.
420 *
421 * @return string|null Header content, or null if it doesn't exist.
422 */
423 public function getHeader($header)
424 {
425 $header = 'HTTP_'.strtoupper(str_replace('-', '_', $header));
426 if (isset($this->server[$header])) {
427 return $this->server[$header];
428 }
429
430 return null;
431 }
432
433 /**
434 * @return bool
435 */
436 public function isAuthenticated()
437 {
438 return $this->attributes['authenticated'];
439 }
440
441 public function setAuthenticated($isAuthenticated)
442 {
443 $this->attributes['authenticated'] = $isAuthenticated;
444 }
445
446 /**
447 * @return int
448 */
449 public function getProtocol()
450 {
451 return $this->attributes['protocol'];
452 }
453 }
454