PageRenderTime 38ms CodeModel.GetById 9ms RepoModel.GetById 1ms app.codeStats 0ms

/ZendFramework/library/Zend/Oauth/Client.php

https://bitbucket.org/Dal-Papa/is-340-publish-base
PHP | 336 lines | 176 code | 19 blank | 141 comment | 34 complexity | 144d40858f7e50ae79fc570d231b8366 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-2012 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: Client.php 25167 2012-12-19 16:28:01Z matthew $
  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-2012 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|Zend_Config $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 ($config instanceof Zend_Config && !isset($config->rfc3986_strict)) {
  72. $config = $config->toArray();
  73. $config['rfc3986_strict'] = true;
  74. } else if (null === $config ||
  75. (is_array($config) && !isset($config['rfc3986_strict']))) {
  76. $config['rfc3986_strict'] = true;
  77. }
  78. parent::__construct($uri, $config);
  79. $this->_config = new Zend_Oauth_Config;
  80. if ($oauthOptions !== null) {
  81. if ($oauthOptions instanceof Zend_Config) {
  82. $oauthOptions = $oauthOptions->toArray();
  83. }
  84. $this->_config->setOptions($oauthOptions);
  85. }
  86. }
  87. /**
  88. * Load the connection adapter
  89. *
  90. * @param Zend_Http_Client_Adapter_Interface $adapter
  91. * @return void
  92. */
  93. public function setAdapter($adapter)
  94. {
  95. if ($adapter == null) {
  96. $this->adapter = $adapter;
  97. } else {
  98. parent::setAdapter($adapter);
  99. }
  100. }
  101. /**
  102. * Set the streamingRequest variable which controls whether we are
  103. * sending the raw (already encoded) POST data from a stream source.
  104. *
  105. * @param boolean $value The value to set.
  106. * @return void
  107. */
  108. public function setStreamingRequest($value)
  109. {
  110. $this->_streamingRequest = $value;
  111. }
  112. /**
  113. * Check whether the client is set to perform streaming requests.
  114. *
  115. * @return boolean True if yes, false otherwise.
  116. */
  117. public function getStreamingRequest()
  118. {
  119. if ($this->_streamingRequest) {
  120. return true;
  121. } else {
  122. return false;
  123. }
  124. }
  125. /**
  126. * Prepare the request body (for POST and PUT requests)
  127. *
  128. * @return string
  129. * @throws Zend_Http_Client_Exception
  130. */
  131. protected function _prepareBody()
  132. {
  133. if($this->_streamingRequest) {
  134. $this->setHeaders(self::CONTENT_LENGTH,
  135. $this->raw_post_data->getTotalSize());
  136. return $this->raw_post_data;
  137. }
  138. else {
  139. return parent::_prepareBody();
  140. }
  141. }
  142. /**
  143. * Clear all custom parameters we set.
  144. *
  145. * @return Zend_Http_Client
  146. */
  147. public function resetParameters($clearAll = false)
  148. {
  149. $this->_streamingRequest = false;
  150. return parent::resetParameters($clearAll);
  151. }
  152. /**
  153. * Set the raw (already encoded) POST data from a stream source.
  154. *
  155. * This is used to support POSTing from open file handles without
  156. * caching the entire body into memory. It is a wrapper around
  157. * Zend_Http_Client::setRawData().
  158. *
  159. * @param string $data The request data
  160. * @param string $enctype The encoding type
  161. * @return Zend_Http_Client
  162. */
  163. public function setRawDataStream($data, $enctype = null)
  164. {
  165. $this->_streamingRequest = true;
  166. return $this->setRawData($data, $enctype);
  167. }
  168. /**
  169. * Same as Zend_Http_Client::setMethod() except it also creates an
  170. * Oauth specific reference to the method type.
  171. * Might be defunct and removed in a later iteration.
  172. *
  173. * @param string $method
  174. * @return Zend_Http_Client
  175. */
  176. public function setMethod($method = self::GET)
  177. {
  178. if ($method == self::GET) {
  179. $this->setRequestMethod(self::GET);
  180. } elseif($method == self::POST) {
  181. $this->setRequestMethod(self::POST);
  182. } elseif($method == self::PUT) {
  183. $this->setRequestMethod(self::PUT);
  184. } elseif($method == self::DELETE) {
  185. $this->setRequestMethod(self::DELETE);
  186. } elseif($method == self::HEAD) {
  187. $this->setRequestMethod(self::HEAD);
  188. } elseif($method == self::OPTIONS) {
  189. $this->setRequestMethod(self::OPTIONS);
  190. }
  191. return parent::setMethod($method);
  192. }
  193. /**
  194. * Same as Zend_Http_Client::request() except just before the request is
  195. * executed, we automatically append any necessary OAuth parameters and
  196. * sign the request using the relevant signature method.
  197. *
  198. * @param string $method
  199. * @return Zend_Http_Response
  200. */
  201. public function request($method = null)
  202. {
  203. if ($method !== null) {
  204. $this->setMethod($method);
  205. }
  206. $this->prepareOauth();
  207. return parent::request();
  208. }
  209. /**
  210. * Performs OAuth preparation on the request before sending.
  211. *
  212. * This primarily means taking a request, correctly encoding and signing
  213. * all parameters, and applying the correct OAuth scheme to the method
  214. * being used.
  215. *
  216. * @return void
  217. * @throws Zend_Oauth_Exception If POSTBODY scheme requested, but GET request method used; or if invalid request scheme provided
  218. */
  219. public function prepareOauth()
  220. {
  221. $requestScheme = $this->getRequestScheme();
  222. $requestMethod = $this->getRequestMethod();
  223. $query = null;
  224. if ($requestScheme == Zend_Oauth::REQUEST_SCHEME_HEADER) {
  225. $oauthHeaderValue = $this->getToken()->toHeader(
  226. $this->getUri(true),
  227. $this->_config,
  228. $this->_getSignableParametersAsQueryString(),
  229. $this->getRealm()
  230. );
  231. $this->setHeaders('Authorization', $oauthHeaderValue);
  232. } elseif ($requestScheme == Zend_Oauth::REQUEST_SCHEME_POSTBODY) {
  233. if ($requestMethod == self::GET) {
  234. require_once 'Zend/Oauth/Exception.php';
  235. throw new Zend_Oauth_Exception(
  236. 'The client is configured to'
  237. . ' pass OAuth parameters through a POST body but request method'
  238. . ' is set to GET'
  239. );
  240. }
  241. $raw = $this->getToken()->toQueryString(
  242. $this->getUri(true),
  243. $this->_config,
  244. $this->_getSignableParametersAsQueryString()
  245. );
  246. $this->setRawData($raw, 'application/x-www-form-urlencoded');
  247. $this->paramsPost = array();
  248. } elseif ($requestScheme == Zend_Oauth::REQUEST_SCHEME_QUERYSTRING) {
  249. $params = $this->paramsGet;
  250. $query = $this->getUri()->getQuery();
  251. if ($query) {
  252. $queryParts = explode('&', $this->getUri()->getQuery());
  253. foreach ($queryParts as $queryPart) {
  254. $kvTuple = explode('=', $queryPart);
  255. $params[urldecode($kvTuple[0])] =
  256. (array_key_exists(1, $kvTuple) ? urldecode($kvTuple[1]) : null);
  257. }
  258. }
  259. if (!empty($this->paramsPost)) {
  260. $params = array_merge($params, $this->paramsPost);
  261. $query = $this->getToken()->toQueryString(
  262. $this->getUri(true), $this->_config, $params
  263. );
  264. }
  265. $query = $this->getToken()->toQueryString(
  266. $this->getUri(true), $this->_config, $params
  267. );
  268. $this->getUri()->setQuery($query);
  269. $this->paramsGet = array();
  270. } else {
  271. require_once 'Zend/Oauth/Exception.php';
  272. throw new Zend_Oauth_Exception('Invalid request scheme: ' . $requestScheme);
  273. }
  274. }
  275. /**
  276. * Collect all signable parameters into a single array across query string
  277. * and POST body. These are returned as a properly formatted single
  278. * query string.
  279. *
  280. * @return string
  281. */
  282. protected function _getSignableParametersAsQueryString()
  283. {
  284. $params = array();
  285. if (!empty($this->paramsGet)) {
  286. $params = array_merge($params, $this->paramsGet);
  287. $query = $this->getToken()->toQueryString(
  288. $this->getUri(true), $this->_config, $params
  289. );
  290. }
  291. if (!empty($this->paramsPost)) {
  292. $params = array_merge($params, $this->paramsPost);
  293. $query = $this->getToken()->toQueryString(
  294. $this->getUri(true), $this->_config, $params
  295. );
  296. }
  297. return $params;
  298. }
  299. /**
  300. * Simple Proxy to the current Zend_Oauth_Config method. It's that instance
  301. * which holds all configuration methods and values this object also presents
  302. * as it's API.
  303. *
  304. * @param string $method
  305. * @param array $args
  306. * @return mixed
  307. * @throws Zend_Oauth_Exception if method does not exist in config object
  308. */
  309. public function __call($method, array $args)
  310. {
  311. if (!method_exists($this->_config, $method)) {
  312. require_once 'Zend/Oauth/Exception.php';
  313. throw new Zend_Oauth_Exception('Method does not exist: ' . $method);
  314. }
  315. return call_user_func_array(array($this->_config,$method), $args);
  316. }
  317. }