PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/src/application/libraries/Zend/Oauth/Client.php

https://bitbucket.org/masnug/grc276-blog-laravel
PHP | 334 lines | 174 code | 19 blank | 141 comment | 33 complexity | 5feca2b04e62709b5d13155890bfd06e 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-2011 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 24415 2011-08-27 22:09:37Z adamlundrigan $
  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|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. }
  189. return parent::setMethod($method);
  190. }
  191. /**
  192. * Same as Zend_Http_Client::request() except just before the request is
  193. * executed, we automatically append any necessary OAuth parameters and
  194. * sign the request using the relevant signature method.
  195. *
  196. * @param string $method
  197. * @return Zend_Http_Response
  198. */
  199. public function request($method = null)
  200. {
  201. if ($method !== null) {
  202. $this->setMethod($method);
  203. }
  204. $this->prepareOauth();
  205. return parent::request();
  206. }
  207. /**
  208. * Performs OAuth preparation on the request before sending.
  209. *
  210. * This primarily means taking a request, correctly encoding and signing
  211. * all parameters, and applying the correct OAuth scheme to the method
  212. * being used.
  213. *
  214. * @return void
  215. * @throws Zend_Oauth_Exception If POSTBODY scheme requested, but GET request method used; or if invalid request scheme provided
  216. */
  217. public function prepareOauth()
  218. {
  219. $requestScheme = $this->getRequestScheme();
  220. $requestMethod = $this->getRequestMethod();
  221. $query = null;
  222. if ($requestScheme == Zend_Oauth::REQUEST_SCHEME_HEADER) {
  223. $oauthHeaderValue = $this->getToken()->toHeader(
  224. $this->getUri(true),
  225. $this->_config,
  226. $this->_getSignableParametersAsQueryString(),
  227. $this->getRealm()
  228. );
  229. $this->setHeaders('Authorization', $oauthHeaderValue);
  230. } elseif ($requestScheme == Zend_Oauth::REQUEST_SCHEME_POSTBODY) {
  231. if ($requestMethod == self::GET) {
  232. require_once 'Zend/Oauth/Exception.php';
  233. throw new Zend_Oauth_Exception(
  234. 'The client is configured to'
  235. . ' pass OAuth parameters through a POST body but request method'
  236. . ' is set to GET'
  237. );
  238. }
  239. $raw = $this->getToken()->toQueryString(
  240. $this->getUri(true),
  241. $this->_config,
  242. $this->_getSignableParametersAsQueryString()
  243. );
  244. $this->setRawData($raw, 'application/x-www-form-urlencoded');
  245. $this->paramsPost = array();
  246. } elseif ($requestScheme == Zend_Oauth::REQUEST_SCHEME_QUERYSTRING) {
  247. $params = $this->paramsGet;
  248. $query = $this->getUri()->getQuery();
  249. if ($query) {
  250. $queryParts = explode('&', $this->getUri()->getQuery());
  251. foreach ($queryParts as $queryPart) {
  252. $kvTuple = explode('=', $queryPart);
  253. $params[urldecode($kvTuple[0])] =
  254. (array_key_exists(1, $kvTuple) ? urldecode($kvTuple[1]) : null);
  255. }
  256. }
  257. if (!empty($this->paramsPost)) {
  258. $params = array_merge($params, $this->paramsPost);
  259. $query = $this->getToken()->toQueryString(
  260. $this->getUri(true), $this->_config, $params
  261. );
  262. }
  263. $query = $this->getToken()->toQueryString(
  264. $this->getUri(true), $this->_config, $params
  265. );
  266. $this->getUri()->setQuery($query);
  267. $this->paramsGet = array();
  268. } else {
  269. require_once 'Zend/Oauth/Exception.php';
  270. throw new Zend_Oauth_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. require_once 'Zend/Oauth/Exception.php';
  311. throw new Zend_Oauth_Exception('Method does not exist: ' . $method);
  312. }
  313. return call_user_func_array(array($this->_config,$method), $args);
  314. }
  315. }