Client.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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-2011 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 */
  22. require_once 'Zend/Oauth.php';
  23. /** Zend_Http_Client */
  24. require_once 'Zend/Http/Client.php';
  25. /** Zend_Oauth_Http_Utility */
  26. require_once 'Zend/Oauth/Http/Utility.php';
  27. /** Zend_Oauth_Config */
  28. require_once 'Zend/Oauth/Config.php';
  29. /**
  30. * @category Zend
  31. * @package Zend_Oauth
  32. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. */
  35. class Zend_Oauth_Client extends Zend_Http_Client
  36. {
  37. /**
  38. * Flag to indicate that the client has detected the server as supporting
  39. * OAuth 1.0a
  40. */
  41. public static $supportsRevisionA = false;
  42. /**
  43. * Holds the current OAuth Configuration set encapsulated in an instance
  44. * of Zend_Oauth_Config; it's not a Zend_Config instance since that level
  45. * of abstraction is unnecessary and doesn't let me escape the accessors
  46. * and mutators anyway!
  47. *
  48. * @var Zend_Oauth_Config
  49. */
  50. protected $_config = null;
  51. /**
  52. * True if this request is being made with data supplied by
  53. * a stream object instead of a raw encoded string.
  54. *
  55. * @var bool
  56. */
  57. protected $_streamingRequest = null;
  58. /**
  59. * Constructor; creates a new HTTP Client instance which itself is
  60. * just a typical Zend_Http_Client subclass with some OAuth icing to
  61. * assist in automating OAuth parameter generation, addition and
  62. * cryptographioc signing of requests.
  63. *
  64. * @param array $oauthOptions
  65. * @param string $uri
  66. * @param array|Zend_Config $config
  67. * @return void
  68. */
  69. public function __construct($oauthOptions, $uri = null, $config = null)
  70. {
  71. if (!isset($config['rfc3986_strict'])) {
  72. $config['rfc3986_strict'] = true;
  73. }
  74. parent::__construct($uri, $config);
  75. $this->_config = new Zend_Oauth_Config;
  76. if ($oauthOptions !== null) {
  77. if ($oauthOptions instanceof Zend_Config) {
  78. $oauthOptions = $oauthOptions->toArray();
  79. }
  80. $this->_config->setOptions($oauthOptions);
  81. }
  82. }
  83. /**
  84. * Return the current connection adapter
  85. *
  86. * @return Zend_Http_Client_Adapter_Interface|string $adapter
  87. */
  88. public function getAdapter()
  89. {
  90. return $this->adapter;
  91. }
  92. /**
  93. * Load the connection adapter
  94. *
  95. * @param Zend_Http_Client_Adapter_Interface $adapter
  96. * @return void
  97. */
  98. public function setAdapter($adapter)
  99. {
  100. if ($adapter == null) {
  101. $this->adapter = $adapter;
  102. } else {
  103. parent::setAdapter($adapter);
  104. }
  105. }
  106. /**
  107. * Set the streamingRequest variable which controls whether we are
  108. * sending the raw (already encoded) POST data from a stream source.
  109. *
  110. * @param boolean $value The value to set.
  111. * @return void
  112. */
  113. public function setStreamingRequest($value)
  114. {
  115. $this->_streamingRequest = $value;
  116. }
  117. /**
  118. * Check whether the client is set to perform streaming requests.
  119. *
  120. * @return boolean True if yes, false otherwise.
  121. */
  122. public function getStreamingRequest()
  123. {
  124. if ($this->_streamingRequest) {
  125. return true;
  126. } else {
  127. return false;
  128. }
  129. }
  130. /**
  131. * Prepare the request body (for POST and PUT requests)
  132. *
  133. * @return string
  134. * @throws Zend_Http_Client_Exception
  135. */
  136. protected function _prepareBody()
  137. {
  138. if($this->_streamingRequest) {
  139. $this->setHeaders(self::CONTENT_LENGTH,
  140. $this->raw_post_data->getTotalSize());
  141. return $this->raw_post_data;
  142. }
  143. else {
  144. return parent::_prepareBody();
  145. }
  146. }
  147. /**
  148. * Clear all custom parameters we set.
  149. *
  150. * @return Zend_Http_Client
  151. */
  152. public function resetParameters($clearAll = false)
  153. {
  154. $this->_streamingRequest = false;
  155. return parent::resetParameters($clearAll);
  156. }
  157. /**
  158. * Set the raw (already encoded) POST data from a stream source.
  159. *
  160. * This is used to support POSTing from open file handles without
  161. * caching the entire body into memory. It is a wrapper around
  162. * Zend_Http_Client::setRawData().
  163. *
  164. * @param string $data The request data
  165. * @param string $enctype The encoding type
  166. * @return Zend_Http_Client
  167. */
  168. public function setRawDataStream($data, $enctype = null)
  169. {
  170. $this->_streamingRequest = true;
  171. return $this->setRawData($data, $enctype);
  172. }
  173. /**
  174. * Same as Zend_Http_Client::setMethod() except it also creates an
  175. * Oauth specific reference to the method type.
  176. * Might be defunct and removed in a later iteration.
  177. *
  178. * @param string $method
  179. * @return Zend_Http_Client
  180. */
  181. public function setMethod($method = self::GET)
  182. {
  183. if ($method == self::GET) {
  184. $this->setRequestMethod(self::GET);
  185. } elseif($method == self::POST) {
  186. $this->setRequestMethod(self::POST);
  187. } elseif($method == self::PUT) {
  188. $this->setRequestMethod(self::PUT);
  189. } elseif($method == self::DELETE) {
  190. $this->setRequestMethod(self::DELETE);
  191. } elseif($method == self::HEAD) {
  192. $this->setRequestMethod(self::HEAD);
  193. }
  194. return parent::setMethod($method);
  195. }
  196. /**
  197. * Same as Zend_Http_Client::request() except just before the request is
  198. * executed, we automatically append any necessary OAuth parameters and
  199. * sign the request using the relevant signature method.
  200. *
  201. * @param string $method
  202. * @return Zend_Http_Response
  203. */
  204. public function request($method = null)
  205. {
  206. if ($method !== null) {
  207. $this->setMethod($method);
  208. }
  209. $this->prepareOauth();
  210. return parent::request();
  211. }
  212. /**
  213. * Performs OAuth preparation on the request before sending.
  214. *
  215. * This primarily means taking a request, correctly encoding and signing
  216. * all parameters, and applying the correct OAuth scheme to the method
  217. * being used.
  218. *
  219. * @return void
  220. * @throws Zend_Oauth_Exception If POSTBODY scheme requested, but GET request method used; or if invalid request scheme provided
  221. */
  222. public function prepareOauth()
  223. {
  224. $requestScheme = $this->getRequestScheme();
  225. $requestMethod = $this->getRequestMethod();
  226. $query = null;
  227. if ($requestScheme == Zend_Oauth::REQUEST_SCHEME_HEADER) {
  228. $oauthHeaderValue = $this->getToken()->toHeader(
  229. $this->getUri(true),
  230. $this->_config,
  231. $this->_getSignableParametersAsQueryString(),
  232. $this->getRealm()
  233. );
  234. $this->setHeaders('Authorization', $oauthHeaderValue);
  235. } elseif ($requestScheme == Zend_Oauth::REQUEST_SCHEME_POSTBODY) {
  236. if ($requestMethod == self::GET) {
  237. require_once 'Zend/Oauth/Exception.php';
  238. throw new Zend_Oauth_Exception(
  239. 'The client is configured to'
  240. . ' pass OAuth parameters through a POST body but request method'
  241. . ' is set to GET'
  242. );
  243. }
  244. $raw = $this->getToken()->toQueryString(
  245. $this->getUri(true),
  246. $this->_config,
  247. $this->_getSignableParametersAsQueryString()
  248. );
  249. $this->setRawData($raw, 'application/x-www-form-urlencoded');
  250. $this->paramsPost = array();
  251. } elseif ($requestScheme == Zend_Oauth::REQUEST_SCHEME_QUERYSTRING) {
  252. $params = array();
  253. $query = $this->getUri()->getQuery();
  254. if ($query) {
  255. $queryParts = explode('&', $this->getUri()->getQuery());
  256. foreach ($queryParts as $queryPart) {
  257. $kvTuple = explode('=', $queryPart);
  258. $params[urldecode($kvTuple[0])] =
  259. (array_key_exists(1, $kvTuple) ? urldecode($kvTuple[1]) : NULL);
  260. }
  261. }
  262. if (!empty($this->paramsPost)) {
  263. $params = array_merge($params, $this->paramsPost);
  264. $query = $this->getToken()->toQueryString(
  265. $this->getUri(true), $this->_config, $params
  266. );
  267. }
  268. $query = $this->getToken()->toQueryString(
  269. $this->getUri(true), $this->_config, $params
  270. );
  271. $this->getUri()->setQuery($query);
  272. $this->paramsGet = array();
  273. } else {
  274. require_once 'Zend/Oauth/Exception.php';
  275. throw new Zend_Oauth_Exception('Invalid request scheme: ' . $requestScheme);
  276. }
  277. }
  278. /**
  279. * Collect all signable parameters into a single array across query string
  280. * and POST body. These are returned as a properly formatted single
  281. * query string.
  282. *
  283. * @return string
  284. */
  285. protected function _getSignableParametersAsQueryString()
  286. {
  287. $params = array();
  288. if (!empty($this->paramsGet)) {
  289. $params = array_merge($params, $this->paramsGet);
  290. $query = $this->getToken()->toQueryString(
  291. $this->getUri(true), $this->_config, $params
  292. );
  293. }
  294. if (!empty($this->paramsPost)) {
  295. $params = array_merge($params, $this->paramsPost);
  296. $query = $this->getToken()->toQueryString(
  297. $this->getUri(true), $this->_config, $params
  298. );
  299. }
  300. return $params;
  301. }
  302. /**
  303. * Simple Proxy to the current Zend_Oauth_Config method. It's that instance
  304. * which holds all configuration methods and values this object also presents
  305. * as it's API.
  306. *
  307. * @param string $method
  308. * @param array $args
  309. * @return mixed
  310. * @throws Zend_Oauth_Exception if method does not exist in config object
  311. */
  312. public function __call($method, array $args)
  313. {
  314. if (!method_exists($this->_config, $method)) {
  315. require_once 'Zend/Oauth/Exception.php';
  316. throw new Zend_Oauth_Exception('Method does not exist: ' . $method);
  317. }
  318. return call_user_func_array(array($this->_config,$method), $args);
  319. }
  320. }