PageRenderTime 66ms CodeModel.GetById 40ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Oauth/Client.php

https://github.com/Doap/iCms---intelligent-Content-management-system
PHP | 339 lines | 173 code | 20 blank | 146 comment | 29 complexity | 378e485e492b9cc1bd4c3a24a5da58da 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: Client.php 23076 2010-10-10 21:37:20Z padraic $
  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-2010 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. );
  233. $this->setHeaders('Authorization', $oauthHeaderValue);
  234. } elseif ($requestScheme == Zend_Oauth::REQUEST_SCHEME_POSTBODY) {
  235. if ($requestMethod == self::GET) {
  236. require_once 'Zend/Oauth/Exception.php';
  237. throw new Zend_Oauth_Exception(
  238. 'The client is configured to'
  239. . ' pass OAuth parameters through a POST body but request method'
  240. . ' is set to GET'
  241. );
  242. }
  243. $raw = $this->getToken()->toQueryString(
  244. $this->getUri(true),
  245. $this->_config,
  246. $this->_getSignableParametersAsQueryString()
  247. );
  248. $this->setRawData($raw, 'application/x-www-form-urlencoded');
  249. $this->paramsPost = array();
  250. } elseif ($requestScheme == Zend_Oauth::REQUEST_SCHEME_QUERYSTRING) {
  251. $params = array();
  252. $query = $this->getUri()->getQuery();
  253. if ($query) {
  254. $queryParts = explode('&', $this->getUri()->getQuery());
  255. foreach ($queryParts as $queryPart) {
  256. $kvTuple = explode('=', $queryPart);
  257. $params[urldecode($kvTuple[0])] =
  258. (array_key_exists(1, $kvTuple) ? urldecode($kvTuple[1]) : NULL);
  259. }
  260. }
  261. if (!empty($this->paramsPost)) {
  262. $params = array_merge($params, $this->paramsPost);
  263. $query = $this->getToken()->toQueryString(
  264. $this->getUri(true), $this->_config, $params
  265. );
  266. }
  267. $query = $this->getToken()->toQueryString(
  268. $this->getUri(true), $this->_config, $params
  269. );
  270. $this->getUri()->setQuery($query);
  271. $this->paramsGet = array();
  272. } else {
  273. require_once 'Zend/Oauth/Exception.php';
  274. throw new Zend_Oauth_Exception('Invalid request scheme: ' . $requestScheme);
  275. }
  276. }
  277. /**
  278. * Collect all signable parameters into a single array across query string
  279. * and POST body. These are returned as a properly formatted single
  280. * query string.
  281. *
  282. * @return string
  283. */
  284. protected function _getSignableParametersAsQueryString()
  285. {
  286. $params = array();
  287. if (!empty($this->paramsGet)) {
  288. $params = array_merge($params, $this->paramsGet);
  289. $query = $this->getToken()->toQueryString(
  290. $this->getUri(true), $this->_config, $params
  291. );
  292. }
  293. if (!empty($this->paramsPost)) {
  294. $params = array_merge($params, $this->paramsPost);
  295. $query = $this->getToken()->toQueryString(
  296. $this->getUri(true), $this->_config, $params
  297. );
  298. }
  299. return $params;
  300. }
  301. /**
  302. * Simple Proxy to the current Zend_Oauth_Config method. It's that instance
  303. * which holds all configuration methods and values this object also presents
  304. * as it's API.
  305. *
  306. * @param string $method
  307. * @param array $args
  308. * @return mixed
  309. * @throws Zend_Oauth_Exception if method does not exist in config object
  310. */
  311. public function __call($method, array $args)
  312. {
  313. if (!method_exists($this->_config, $method)) {
  314. require_once 'Zend/Oauth/Exception.php';
  315. throw new Zend_Oauth_Exception('Method does not exist: ' . $method);
  316. }
  317. return call_user_func_array(array($this->_config,$method), $args);
  318. }
  319. }