Client.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. /** 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. * Constructor; creates a new HTTP Client instance which itself is
  53. * just a typical Zend_Http_Client subclass with some OAuth icing to
  54. * assist in automating OAuth parameter generation, addition and
  55. * cryptographioc signing of requests.
  56. *
  57. * @param array $oauthOptions
  58. * @param string $uri
  59. * @param array|Zend_Config $config
  60. * @return void
  61. */
  62. public function __construct(array $oauthOptions, $uri = null, $config = null)
  63. {
  64. parent::__construct($uri, $config);
  65. $this->_config = new Zend_Oauth_Config;
  66. if (!is_null($oauthOptions)) {
  67. if ($oauthOptions instanceof Zend_Config) {
  68. $oauthOptions = $oauthOptions->toArray();
  69. }
  70. $this->_config->setOptions($oauthOptions);
  71. }
  72. }
  73. /**
  74. * Same as Zend_Http_Client::setMethod() except it also creates an
  75. * Oauth specific reference to the method type.
  76. * Might be defunct and removed in a later iteration.
  77. *
  78. * @param string $method
  79. * @return Zend_Http_Client
  80. */
  81. public function setMethod($method = self::GET)
  82. {
  83. if ($method == self::GET) {
  84. $this->setRequestMethod(self::GET);
  85. } elseif($method == self::POST) {
  86. $this->setRequestMethod(self::POST);
  87. } elseif($method == self::PUT) {
  88. $this->setRequestMethod(self::PUT);
  89. } elseif($method == self::DELETE) {
  90. $this->setRequestMethod(self::DELETE);
  91. } elseif($method == self::HEAD) {
  92. $this->setRequestMethod(self::HEAD);
  93. }
  94. return parent::setMethod($method);
  95. }
  96. /**
  97. * Same as Zend_Http_Client::request() except just before the request is
  98. * executed, we automatically append any necessary OAuth parameters and
  99. * sign the request using the relevant signature method.
  100. *
  101. * @param string $method
  102. * @return Zend_Http_Response
  103. */
  104. public function request($method = null)
  105. {
  106. if (!is_null($method)) {
  107. $this->setMethod($method);
  108. }
  109. $this->prepareOauth();
  110. return parent::request();
  111. }
  112. /**
  113. * Performs OAuth preparation on the request before sending.
  114. *
  115. * This primarily means taking a request, correctly encoding and signing
  116. * all parameters, and applying the correct OAuth scheme to the method
  117. * being used.
  118. *
  119. * @return void
  120. * @throws Zend_Oauth_Exception If POSTBODY scheme requested, but GET request method used; or if invalid request scheme provided
  121. */
  122. public function prepareOauth()
  123. {
  124. $requestScheme = $this->getRequestScheme();
  125. $requestMethod = $this->getRequestMethod();
  126. $query = null;
  127. if ($requestScheme == Zend_Oauth::REQUEST_SCHEME_HEADER) {
  128. $params = array();
  129. if (!empty($this->paramsGet)) {
  130. $params = array_merge($params, $this->paramsGet);
  131. $query = $this->getToken()->toQueryString(
  132. $this->getUri(true), $this->_config, $params
  133. );
  134. }
  135. if (!empty($this->paramsPost)) {
  136. $params = array_merge($params, $this->paramsPost);
  137. $query = $this->getToken()->toQueryString(
  138. $this->getUri(true), $this->_config, $params
  139. );
  140. }
  141. $oauthHeaderValue = $this->getToken()->toHeader(
  142. $this->getUri(true), $this->_config, $params
  143. );
  144. $this->setHeaders('Authorization', $oauthHeaderValue);
  145. } elseif ($requestScheme == Zend_Oauth::REQUEST_SCHEME_POSTBODY) {
  146. if ($requestMethod == self::GET) {
  147. require_once 'Zend/Oauth/Exception.php';
  148. throw new Zend_Oauth_Exception(
  149. 'The client is configured to'
  150. . ' pass OAuth parameters through a POST body but request method'
  151. . ' is set to GET'
  152. );
  153. }
  154. $raw = $this->getToken()->toQueryString(
  155. $this->getUri(true), $this->_config, $this->paramsPost
  156. );
  157. $this->setRawData($raw);
  158. $this->paramsPost = array();
  159. } elseif ($requestScheme == Zend_Oauth::REQUEST_SCHEME_QUERYSTRING) {
  160. $params = array();
  161. $query = $this->getUri()->getQuery();
  162. if ($query) {
  163. $queryParts = split('&', $this->getUri()->getQuery());
  164. foreach ($queryParts as $queryPart) {
  165. $kvTuple = split('=', $queryPart);
  166. $params[$kvTuple[0]] =
  167. (array_key_exists(1, $kvTuple) ? $kvTuple[1] : NULL);
  168. }
  169. }
  170. $query = $this->getToken()->toQueryString(
  171. $this->getUri(true), $this->_config, $params
  172. );
  173. $this->getUri()->setQuery($query);
  174. $this->paramsGet = array();
  175. } else {
  176. require_once 'Zend/Oauth/Exception.php';
  177. throw new Zend_Oauth_Exception('Invalid request scheme: ' . $requestScheme);
  178. }
  179. }
  180. /**
  181. * Simple Proxy to the current Zend_Oauth_Config method. It's that instance
  182. * which holds all configuration methods and values this object also presents
  183. * as it's API.
  184. *
  185. * @param string $method
  186. * @param array $args
  187. * @return mixed
  188. * @throws Zend_Oauth_Exception if method does not exist in config object
  189. */
  190. public function __call($method, array $args)
  191. {
  192. if (!method_exists($this->_config, $method)) {
  193. require_once 'Zend/Oauth/Exception.php';
  194. throw new Zend_Oauth_Exception('Method does not exist: ' . $method);
  195. }
  196. return call_user_func_array(array($this->_config,$method), $args);
  197. }
  198. }