PageRenderTime 22ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/OAuth/Client.php

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