PageRenderTime 42ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Request.php

http://github.com/abraham/twitteroauth
PHP | 289 lines | 159 code | 29 blank | 101 comment | 8 complexity | 2321165829c4846037882d7eb9b7f27f MD5 | raw file
  1. <?php
  2. /**
  3. * The MIT License
  4. * Copyright (c) 2007 Andy Smith
  5. */
  6. declare(strict_types=1);
  7. namespace Abraham\TwitterOAuth;
  8. class Request
  9. {
  10. protected $parameters;
  11. protected $httpMethod;
  12. protected $httpUrl;
  13. protected $json;
  14. public static $version = '1.0';
  15. /**
  16. * Constructor
  17. *
  18. * @param string $httpMethod
  19. * @param string $httpUrl
  20. * @param array|null $parameters
  21. */
  22. public function __construct(
  23. string $httpMethod,
  24. string $httpUrl,
  25. ?array $parameters = []
  26. ) {
  27. $parameters = array_merge(
  28. Util::parseParameters(parse_url($httpUrl, PHP_URL_QUERY)),
  29. $parameters,
  30. );
  31. $this->parameters = $parameters;
  32. $this->httpMethod = $httpMethod;
  33. $this->httpUrl = $httpUrl;
  34. }
  35. /**
  36. * pretty much a helper function to set up the request
  37. *
  38. * @param Consumer $consumer
  39. * @param Token $token
  40. * @param string $httpMethod
  41. * @param string $httpUrl
  42. * @param array $parameters
  43. *
  44. * @return Request
  45. */
  46. public static function fromConsumerAndToken(
  47. Consumer $consumer,
  48. Token $token = null,
  49. string $httpMethod,
  50. string $httpUrl,
  51. array $parameters = [],
  52. $json = false
  53. ) {
  54. $defaults = [
  55. 'oauth_version' => Request::$version,
  56. 'oauth_nonce' => Request::generateNonce(),
  57. 'oauth_timestamp' => time(),
  58. 'oauth_consumer_key' => $consumer->key,
  59. ];
  60. if (null !== $token) {
  61. $defaults['oauth_token'] = $token->key;
  62. }
  63. // The json payload is not included in the signature on json requests,
  64. // therefore it shouldn't be included in the parameters array.
  65. if ($json) {
  66. $parameters = $defaults;
  67. } else {
  68. $parameters = array_merge($defaults, $parameters);
  69. }
  70. return new Request($httpMethod, $httpUrl, $parameters);
  71. }
  72. /**
  73. * @param string $name
  74. * @param string $value
  75. */
  76. public function setParameter(string $name, string $value)
  77. {
  78. $this->parameters[$name] = $value;
  79. }
  80. /**
  81. * @param string $name
  82. *
  83. * @return string|null
  84. */
  85. public function getParameter(string $name): ?string
  86. {
  87. return isset($this->parameters[$name])
  88. ? $this->parameters[$name]
  89. : null;
  90. }
  91. /**
  92. * @return array
  93. */
  94. public function getParameters(): array
  95. {
  96. return $this->parameters;
  97. }
  98. /**
  99. * @param string $name
  100. */
  101. public function removeParameter(string $name): void
  102. {
  103. unset($this->parameters[$name]);
  104. }
  105. /**
  106. * The request parameters, sorted and concatenated into a normalized string.
  107. *
  108. * @return string
  109. */
  110. public function getSignableParameters(): string
  111. {
  112. // Grab all parameters
  113. $params = $this->parameters;
  114. // Remove oauth_signature if present
  115. // Ref: Spec: 9.1.1 ("The oauth_signature parameter MUST be excluded.")
  116. if (isset($params['oauth_signature'])) {
  117. unset($params['oauth_signature']);
  118. }
  119. return Util::buildHttpQuery($params);
  120. }
  121. /**
  122. * Returns the base string of this request
  123. *
  124. * The base string defined as the method, the url
  125. * and the parameters (normalized), each urlencoded
  126. * and the concated with &.
  127. *
  128. * @return string
  129. */
  130. public function getSignatureBaseString(): string
  131. {
  132. $parts = [
  133. $this->getNormalizedHttpMethod(),
  134. $this->getNormalizedHttpUrl(),
  135. $this->getSignableParameters(),
  136. ];
  137. $parts = Util::urlencodeRfc3986($parts);
  138. return implode('&', $parts);
  139. }
  140. /**
  141. * Returns the HTTP Method in uppercase
  142. *
  143. * @return string
  144. */
  145. public function getNormalizedHttpMethod(): string
  146. {
  147. return strtoupper($this->httpMethod);
  148. }
  149. /**
  150. * parses the url and rebuilds it to be
  151. * scheme://host/path
  152. *
  153. * @return string
  154. */
  155. public function getNormalizedHttpUrl(): string
  156. {
  157. $parts = parse_url($this->httpUrl);
  158. $scheme = $parts['scheme'];
  159. $host = strtolower($parts['host']);
  160. $path = $parts['path'];
  161. return "$scheme://$host$path";
  162. }
  163. /**
  164. * Builds a url usable for a GET request
  165. *
  166. * @return string
  167. */
  168. public function toUrl(): string
  169. {
  170. $postData = $this->toPostdata();
  171. $out = $this->getNormalizedHttpUrl();
  172. if ($postData) {
  173. $out .= '?' . $postData;
  174. }
  175. return $out;
  176. }
  177. /**
  178. * Builds the data one would send in a POST request
  179. *
  180. * @return string
  181. */
  182. public function toPostdata(): string
  183. {
  184. return Util::buildHttpQuery($this->parameters);
  185. }
  186. /**
  187. * Builds the Authorization: header
  188. *
  189. * @return string
  190. * @throws TwitterOAuthException
  191. */
  192. public function toHeader(): string
  193. {
  194. $first = true;
  195. $out = 'Authorization: OAuth';
  196. foreach ($this->parameters as $k => $v) {
  197. if (substr($k, 0, 5) != 'oauth') {
  198. continue;
  199. }
  200. if (is_array($v)) {
  201. throw new TwitterOAuthException(
  202. 'Arrays not supported in headers',
  203. );
  204. }
  205. $out .= $first ? ' ' : ', ';
  206. $out .=
  207. Util::urlencodeRfc3986($k) .
  208. '="' .
  209. Util::urlencodeRfc3986($v) .
  210. '"';
  211. $first = false;
  212. }
  213. return $out;
  214. }
  215. /**
  216. * @return string
  217. */
  218. public function __toString(): string
  219. {
  220. return $this->toUrl();
  221. }
  222. /**
  223. * @param SignatureMethod $signatureMethod
  224. * @param Consumer $consumer
  225. * @param Token $token
  226. */
  227. public function signRequest(
  228. SignatureMethod $signatureMethod,
  229. Consumer $consumer,
  230. Token $token = null
  231. ) {
  232. $this->setParameter(
  233. 'oauth_signature_method',
  234. $signatureMethod->getName(),
  235. );
  236. $signature = $this->buildSignature($signatureMethod, $consumer, $token);
  237. $this->setParameter('oauth_signature', $signature);
  238. }
  239. /**
  240. * @param SignatureMethod $signatureMethod
  241. * @param Consumer $consumer
  242. * @param Token $token
  243. *
  244. * @return string
  245. */
  246. public function buildSignature(
  247. SignatureMethod $signatureMethod,
  248. Consumer $consumer,
  249. Token $token = null
  250. ): string {
  251. return $signatureMethod->buildSignature($this, $consumer, $token);
  252. }
  253. /**
  254. * @return string
  255. */
  256. public static function generateNonce(): string
  257. {
  258. return md5(microtime() . mt_rand());
  259. }
  260. }