Http.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Oauth
  17. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  20. */
  21. /** Zend_Oauth_Http_Utility */
  22. #require_once 'Zend/Oauth/Http/Utility.php';
  23. /** Zend_Uri_Http */
  24. #require_once 'Zend/Uri/Http.php';
  25. /**
  26. * @category Zend
  27. * @package Zend_Oauth
  28. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. */
  31. class Zend_Oauth_Http
  32. {
  33. /**
  34. * Array of all custom service parameters to be sent in the HTTP request
  35. * in addition to the usual OAuth parameters.
  36. *
  37. * @var array
  38. */
  39. protected $_parameters = array();
  40. /**
  41. * Reference to the Zend_Oauth_Consumer instance in use.
  42. *
  43. * @var string
  44. */
  45. protected $_consumer = null;
  46. /**
  47. * OAuth specifies three request methods, this holds the current preferred
  48. * one which by default uses the Authorization Header approach for passing
  49. * OAuth parameters, and a POST body for non-OAuth custom parameters.
  50. *
  51. * @var string
  52. */
  53. protected $_preferredRequestScheme = null;
  54. /**
  55. * Request Method for the HTTP Request.
  56. *
  57. * @var string
  58. */
  59. protected $_preferredRequestMethod = Zend_Oauth::POST;
  60. /**
  61. * Instance of the general Zend_Oauth_Http_Utility class.
  62. *
  63. * @var Zend_Oauth_Http_Utility
  64. */
  65. protected $_httpUtility = null;
  66. /**
  67. * Constructor
  68. *
  69. * @param Zend_Oauth_Consumer $consumer
  70. * @param null|array $parameters
  71. * @param null|Zend_Oauth_Http_Utility $utility
  72. * @return void
  73. */
  74. public function __construct(
  75. Zend_Oauth_Consumer $consumer,
  76. array $parameters = null,
  77. Zend_Oauth_Http_Utility $utility = null
  78. ) {
  79. $this->_consumer = $consumer;
  80. $this->_preferredRequestScheme = $this->_consumer->getRequestScheme();
  81. if ($parameters !== null) {
  82. $this->setParameters($parameters);
  83. }
  84. if ($utility !== null) {
  85. $this->_httpUtility = $utility;
  86. } else {
  87. $this->_httpUtility = new Zend_Oauth_Http_Utility;
  88. }
  89. }
  90. /**
  91. * Set a preferred HTTP request method.
  92. *
  93. * @param string $method
  94. * @return Zend_Oauth_Http
  95. */
  96. public function setMethod($method)
  97. {
  98. if (!in_array($method, array(Zend_Oauth::POST, Zend_Oauth::GET))) {
  99. #require_once 'Zend/Oauth/Exception.php';
  100. throw new Zend_Oauth_Exception('invalid HTTP method: ' . $method);
  101. }
  102. $this->_preferredRequestMethod = $method;
  103. return $this;
  104. }
  105. /**
  106. * Preferred HTTP request method accessor.
  107. *
  108. * @return string
  109. */
  110. public function getMethod()
  111. {
  112. return $this->_preferredRequestMethod;
  113. }
  114. /**
  115. * Mutator to set an array of custom parameters for the HTTP request.
  116. *
  117. * @param array $customServiceParameters
  118. * @return Zend_Oauth_Http
  119. */
  120. public function setParameters(array $customServiceParameters)
  121. {
  122. $this->_parameters = $customServiceParameters;
  123. return $this;
  124. }
  125. /**
  126. * Accessor for an array of custom parameters.
  127. *
  128. * @return array
  129. */
  130. public function getParameters()
  131. {
  132. return $this->_parameters;
  133. }
  134. /**
  135. * Return the Consumer instance in use.
  136. *
  137. * @return Zend_Oauth_Consumer
  138. */
  139. public function getConsumer()
  140. {
  141. return $this->_consumer;
  142. }
  143. /**
  144. * Commence a request cycle where the current HTTP method and OAuth
  145. * request scheme set an upper preferred HTTP request style and where
  146. * failures generate a new HTTP request style further down the OAuth
  147. * preference list for OAuth Request Schemes.
  148. * On success, return the Request object that results for processing.
  149. *
  150. * @param array $params
  151. * @return Zend_Http_Response
  152. * @throws Zend_Oauth_Exception on HTTP request errors
  153. * @todo Remove cycling?; Replace with upfront do-or-die configuration
  154. */
  155. public function startRequestCycle(array $params)
  156. {
  157. $response = null;
  158. $body = null;
  159. $status = null;
  160. try {
  161. $response = $this->_attemptRequest($params);
  162. } catch (Zend_Http_Client_Exception $e) {
  163. #require_once 'Zend/Oauth/Exception.php';
  164. throw new Zend_Oauth_Exception('Error in HTTP request', null, $e);
  165. }
  166. if ($response !== null) {
  167. $body = $response->getBody();
  168. $status = $response->getStatus();
  169. }
  170. if ($response === null // Request failure/exception
  171. || $status == 500 // Internal Server Error
  172. || $status == 400 // Bad Request
  173. || $status == 401 // Unauthorized
  174. || empty($body) // Missing token
  175. ) {
  176. $this->_assessRequestAttempt($response);
  177. $response = $this->startRequestCycle($params);
  178. }
  179. return $response;
  180. }
  181. /**
  182. * Return an instance of Zend_Http_Client configured to use the Query
  183. * String scheme for an OAuth driven HTTP request.
  184. *
  185. * @param array $params
  186. * @param string $url
  187. * @return Zend_Http_Client
  188. */
  189. public function getRequestSchemeQueryStringClient(array $params, $url)
  190. {
  191. $client = Zend_Oauth::getHttpClient();
  192. $client->setUri($url);
  193. $client->getUri()->setQuery(
  194. $this->_httpUtility->toEncodedQueryString($params)
  195. );
  196. $client->setMethod($this->_preferredRequestMethod);
  197. return $client;
  198. }
  199. /**
  200. * Manages the switch from OAuth request scheme to another lower preference
  201. * scheme during a request cycle.
  202. *
  203. * @param Zend_Http_Response
  204. * @return void
  205. * @throws Zend_Oauth_Exception if unable to retrieve valid token response
  206. */
  207. protected function _assessRequestAttempt(Zend_Http_Response $response = null)
  208. {
  209. switch ($this->_preferredRequestScheme) {
  210. case Zend_Oauth::REQUEST_SCHEME_HEADER:
  211. $this->_preferredRequestScheme = Zend_Oauth::REQUEST_SCHEME_POSTBODY;
  212. break;
  213. case Zend_Oauth::REQUEST_SCHEME_POSTBODY:
  214. $this->_preferredRequestScheme = Zend_Oauth::REQUEST_SCHEME_QUERYSTRING;
  215. break;
  216. default:
  217. #require_once 'Zend/Oauth/Exception.php';
  218. throw new Zend_Oauth_Exception(
  219. 'Could not retrieve a valid Token response from Token URL:'
  220. . ($response !== null
  221. ? PHP_EOL . $response->getBody()
  222. : ' No body - check for headers')
  223. );
  224. }
  225. }
  226. /**
  227. * Generates a valid OAuth Authorization header based on the provided
  228. * parameters and realm.
  229. *
  230. * @param array $params
  231. * @param string $realm
  232. * @return string
  233. */
  234. protected function _toAuthorizationHeader(array $params, $realm = null)
  235. {
  236. $headerValue = array();
  237. $headerValue[] = 'OAuth realm="' . $realm . '"';
  238. foreach ($params as $key => $value) {
  239. if (!preg_match("/^oauth_/", $key)) {
  240. continue;
  241. }
  242. $headerValue[] = Zend_Oauth_Http_Utility::urlEncode($key)
  243. . '="'
  244. . Zend_Oauth_Http_Utility::urlEncode($value)
  245. . '"';
  246. }
  247. return implode(",", $headerValue);
  248. }
  249. }