Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Request.php
Go to the documentation of this file.
1 <?php
7 
9 use Zend\Uri\UriFactory;
10 
11 class Request
12 {
16  const HTTP_OK = 200;
17 
18  const HTTP_BAD_REQUEST = 400;
19 
20  const HTTP_UNAUTHORIZED = 401;
21 
23 
24  const HTTP_INTERNAL_ERROR = 500;
25 
34  public function prepareRequest($httpRequest)
35  {
36  $oauthParams = $this->_processRequest(
37  $httpRequest->getHeader('Authorization'),
38  $httpRequest->getHeader(\Zend_Http_Client::CONTENT_TYPE),
39  $httpRequest->getContent(),
40  $this->getRequestUrl($httpRequest)
41  );
42  return $oauthParams;
43  }
44 
51  public function getRequestUrl($httpRequest)
52  {
53  return $httpRequest->getScheme() . '://' . $httpRequest->getHttpHost(false) . $httpRequest->getRequestUri();
54  }
55 
76  protected function _processRequest($authHeaderValue, $contentTypeHeader, $requestBodyString, $requestUrl)
77  {
78  $protocolParams = [];
79 
80  if (!$this->_processHeader($authHeaderValue, $protocolParams)) {
81  return [];
82  }
83 
84  if ($contentTypeHeader && 0 === strpos($contentTypeHeader, \Zend_Http_Client::ENC_URLENCODED)) {
85  $protocolParamsNotSet = !$protocolParams;
86 
87  parse_str($requestBodyString, $protocolBodyParams);
88 
89  foreach ($protocolBodyParams as $bodyParamName => $bodyParamValue) {
90  if (!$this->_isProtocolParameter($bodyParamName)) {
91  $protocolParams[$bodyParamName] = $bodyParamValue;
92  } elseif ($protocolParamsNotSet) {
93  $protocolParams[$bodyParamName] = $bodyParamValue;
94  }
95  }
96  }
97  $protocolParamsNotSet = !$protocolParams;
98 
99  $queryString = UriFactory::factory($requestUrl)->getQuery();
100  $this->_extractQueryStringParams($protocolParams, $queryString);
101 
102  if ($protocolParamsNotSet) {
103  $this->_fetchProtocolParamsFromQuery($protocolParams, $queryString);
104  }
105 
106  // Combine request and header parameters
107  return $protocolParams;
108  }
109 
117  protected function _fetchProtocolParamsFromQuery(&$protocolParams, $queryString)
118  {
119  if (is_array($queryString)) {
120  foreach ($queryString as $queryParamName => $queryParamValue) {
121  if ($this->_isProtocolParameter($queryParamName)) {
122  $protocolParams[$queryParamName] = $queryParamValue;
123  }
124  }
125  }
126  }
127 
134  protected function _isProtocolParameter($attrName)
135  {
136  return (bool)preg_match('/oauth_[a-z_-]+/', $attrName);
137  }
138 
146  protected function _processHeader($authHeaderValue, &$protocolParams)
147  {
148  $oauthValuePosition = stripos(($authHeaderValue ? $authHeaderValue : ''), 'oauth ');
149  if ($authHeaderValue && $oauthValuePosition !== false) {
150  // Ignore anything before and including 'OAuth ' (trailing values validated later)
151  $authHeaderValue = substr($authHeaderValue, $oauthValuePosition + 6);
152  foreach (explode(',', $authHeaderValue) as $paramStr) {
153  $nameAndValue = explode('=', trim($paramStr), 2);
154 
155  if (count($nameAndValue) < 2) {
156  continue;
157  }
158  if ($this->_isProtocolParameter($nameAndValue[0])) {
159  $protocolParams[rawurldecode($nameAndValue[0])] = rawurldecode(trim($nameAndValue[1], '"'));
160  }
161  }
162  return true;
163  }
164  return false;
165  }
166 
174  protected function _extractQueryStringParams(&$protocolParams, $queryString)
175  {
176  if ($queryString) {
177  foreach (explode('&', $queryString) as $paramToValue) {
178  $paramData = explode('=', $paramToValue);
179 
180  if (2 === count($paramData) && !$this->_isProtocolParameter($paramData[0])) {
181  $protocolParams[rawurldecode($paramData[0])] = rawurldecode($paramData[1]);
182  }
183  }
184  }
185  }
186 
194  public function prepareErrorResponse(
195  \Exception $exception,
196  \Magento\Framework\HTTP\PhpEnvironment\Response $response = null
197  ) {
198  $errorMsg = $exception->getMessage();
199 
200  if ($exception instanceof \Magento\Framework\Oauth\Exception) {
201  $responseCode = self::HTTP_UNAUTHORIZED;
202  } elseif ($exception instanceof \Magento\Framework\Oauth\OauthInputException) {
203  $responseCode = self::HTTP_BAD_REQUEST;
204  if ($errorMsg == 'One or more input exceptions have occurred.') {
205  $errorMsg = $exception->getAggregatedErrorMessage();
206  }
207  } else {
208  $errorMsg = 'internal_error&message=' . ($errorMsg ? $errorMsg : 'empty_message');
209  $responseCode = self::HTTP_INTERNAL_ERROR;
210  }
211 
212  $response->setHttpResponseCode($responseCode);
213  return ['oauth_problem' => $errorMsg];
214  }
215 }
$response
Definition: 404.php:11
_processHeader($authHeaderValue, &$protocolParams)
Definition: Request.php:146
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
_fetchProtocolParamsFromQuery(&$protocolParams, $queryString)
Definition: Request.php:117
const ENC_URLENCODED
Definition: Client.php:109
_processRequest($authHeaderValue, $contentTypeHeader, $requestBodyString, $requestUrl)
Definition: Request.php:76
const CONTENT_TYPE
Definition: Client.php:103
prepareErrorResponse(\Exception $exception, \Magento\Framework\HTTP\PhpEnvironment\Response $response=null)
Definition: Request.php:194
_extractQueryStringParams(&$protocolParams, $queryString)
Definition: Request.php:174