1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace yrc\web; |
4
|
|
|
|
5
|
|
|
use yii\base\InvalidConfigException; |
6
|
|
|
use yii\web\Request as BaseRequest; |
7
|
|
|
use yii\web\RequestParserInterface; |
8
|
|
|
use yrc\web\Response; |
9
|
|
|
use Yii; |
10
|
|
|
|
11
|
|
|
class Request extends BaseRequest |
12
|
|
|
{ |
13
|
|
|
private $_bodyParams; |
14
|
|
|
|
15
|
|
|
private $_decryptedBody; |
16
|
|
|
|
17
|
|
|
public function getDecryptedBody() |
18
|
|
|
{ |
19
|
|
|
if ($this->getContentType() !== 'application/vnd.25519+json') { |
20
|
|
|
return $this->getRawBody(); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
$this->getBodyParams(); |
24
|
|
|
return $this->_decryptedBody; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Returns the request parameters given in the request body. |
29
|
|
|
* |
30
|
|
|
* Request parameters are determined using the parsers configured in [[parsers]] property. |
31
|
|
|
* If no parsers are configured for the current [[contentType]] it uses the PHP function `mb_parse_str()` |
32
|
|
|
* to parse the [[rawBody|request body]]. |
33
|
|
|
* @return array the request parameters given in the request body. |
34
|
|
|
* @throws \yii\base\InvalidConfigException if a registered parser does not implement the [[RequestParserInterface]]. |
35
|
|
|
* @see getMethod() |
36
|
|
|
* @see getBodyParam() |
37
|
|
|
* @see setBodyParams() |
38
|
|
|
*/ |
39
|
|
|
public function getBodyParams() |
40
|
|
|
{ |
41
|
|
|
if ($this->_bodyParams === null) { |
42
|
|
|
if (isset($_POST[$this->methodParam])) { |
43
|
|
|
$this->_bodyParams = $_POST; |
44
|
|
|
unset($this->_bodyParams[$this->methodParam]); |
45
|
|
|
return $this->_bodyParams; |
46
|
|
|
} |
47
|
|
|
$rawContentType = $this->getContentType(); |
48
|
|
|
if (($pos = strpos($rawContentType, ';')) !== false) { |
49
|
|
|
// e.g. text/html; charset=UTF-8 |
50
|
|
|
$contentType = substr($rawContentType, 0, $pos); |
51
|
|
|
} else { |
52
|
|
|
$contentType = $rawContentType; |
53
|
|
|
} |
54
|
|
|
if (isset($this->parsers[$contentType])) { |
55
|
|
|
$parser = Yii::createObject($this->parsers[$contentType]); |
56
|
|
|
if (!($parser instanceof RequestParserInterface)) { |
57
|
|
|
throw new InvalidConfigException("The '$contentType' request parser is invalid. It must implement the yii\\web\\RequestParserInterface."); |
58
|
|
|
} |
59
|
|
|
$this->_bodyParams = $parser->parse($this->getRawBody(), $rawContentType); |
60
|
|
|
if ($rawContentType === 'application/vnd.25519+json') { |
61
|
|
|
$this->_decryptedBody = $parser->getDecryptedBody(); |
|
|
|
|
62
|
|
|
} |
63
|
|
|
} elseif (isset($this->parsers['*'])) { |
64
|
|
|
$parser = Yii::createObject($this->parsers['*']); |
65
|
|
|
if (!($parser instanceof RequestParserInterface)) { |
66
|
|
|
throw new InvalidConfigException('The fallback request parser is invalid. It must implement the yii\\web\\RequestParserInterface.'); |
67
|
|
|
} |
68
|
|
|
$this->_bodyParams = $parser->parse($this->getRawBody(), $rawContentType); |
69
|
|
|
} elseif ($this->getMethod() === 'POST') { |
70
|
|
|
// PHP has already parsed the body so we have all params in $_POST |
71
|
|
|
$this->_bodyParams = $_POST; |
72
|
|
|
} else { |
73
|
|
|
$this->_bodyParams = []; |
74
|
|
|
mb_parse_str($this->getRawBody(), $this->_bodyParams); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
return $this->_bodyParams; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|