Client.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. }
  90. return parent::setMethod($method);
  91. }
  92. /**
  93. * Same as Zend_Http_Client::request() except just before the request is
  94. * executed, we automatically append any necessary OAuth parameters and
  95. * sign the request using the relevant signature method.
  96. *
  97. * @param string $method
  98. * @return Zend_Http_Response
  99. */
  100. public function request($method = null)
  101. {
  102. if (!is_null($method)) {
  103. $this->setMethod($method);
  104. }
  105. $this->prepareOauth();
  106. return parent::request();
  107. }
  108. /**
  109. * Performs OAuth preparation on the request before sending.
  110. *
  111. * This primarily means taking a request, correctly encoding and signing
  112. * all parameters, and applying the correct OAuth scheme to the method
  113. * being used.
  114. *
  115. * @return void
  116. * @throws Zend_Oauth_Exception If POSTBODY scheme requested, but GET request method used; or if invalid request scheme provided
  117. */
  118. public function prepareOauth()
  119. {
  120. $requestScheme = $this->getRequestScheme();
  121. $requestMethod = $this->getRequestMethod();
  122. $query = null;
  123. if ($requestScheme == Zend_Oauth::REQUEST_SCHEME_HEADER) {
  124. $params = array();
  125. if (!empty($this->paramsGet)) {
  126. $params = array_merge($params, $this->paramsGet);
  127. $query = $this->getToken()->toQueryString(
  128. $this->getUri(true), $this->_config, $params
  129. );
  130. }
  131. if (!empty($this->paramsPost)) {
  132. $params = array_merge($params, $this->paramsPost);
  133. $query = $this->getToken()->toQueryString(
  134. $this->getUri(true), $this->_config, $params
  135. );
  136. }
  137. $oauthHeaderValue = $this->getToken()->toHeader(
  138. $this->getUri(true), $this->_config, $params
  139. );
  140. $this->setHeaders('Authorization', $oauthHeaderValue);
  141. } elseif ($requestScheme == Zend_Oauth::REQUEST_SCHEME_POSTBODY) {
  142. if ($requestMethod == self::GET) {
  143. require_once 'Zend/Oauth/Exception.php';
  144. throw new Zend_Oauth_Exception(
  145. 'The client is configured to'
  146. . ' pass OAuth parameters through a POST body but request method'
  147. . ' is set to GET'
  148. );
  149. }
  150. $raw = $this->getToken()->toQueryString(
  151. $this->getUri(true), $this->_config, $this->paramsPost
  152. );
  153. $this->setRawData($raw);
  154. $this->paramsPost = array();
  155. } elseif ($requestScheme == Zend_Oauth::REQUEST_SCHEME_QUERYSTRING) {
  156. $params = array();
  157. $query = $this->getUri()->getQuery();
  158. if ($query) {
  159. $queryParts = split('&', $this->getUri()->getQuery());
  160. foreach ($queryParts as $queryPart) {
  161. $kvTuple = split('=', $queryPart);
  162. $params[$kvTuple[0]] =
  163. (array_key_exists(1, $kvTuple) ? $kvTuple[1] : NULL);
  164. }
  165. }
  166. $query = $this->getToken()->toQueryString(
  167. $this->getUri(true), $this->_config, $params
  168. );
  169. $this->getUri()->setQuery($query);
  170. $this->paramsGet = array();
  171. } else {
  172. require_once 'Zend/Oauth/Exception.php';
  173. throw new Zend_Oauth_Exception('Invalid request scheme: ' . $requestScheme);
  174. }
  175. }
  176. /**
  177. * Simple Proxy to the current Zend_Oauth_Config method. It's that instance
  178. * which holds all configuration methods and values this object also presents
  179. * as it's API.
  180. *
  181. * @param string $method
  182. * @param array $args
  183. * @return mixed
  184. * @throws Zend_Oauth_Exception if method does not exist in config object
  185. */
  186. public function __call($method, array $args)
  187. {
  188. if (!method_exists($this->_config, $method)) {
  189. require_once 'Zend/Oauth/Exception.php';
  190. throw new Zend_Oauth_Exception('Method does not exist: ' . $method);
  191. }
  192. return call_user_func_array(array($this->_config,$method), $args);
  193. }
  194. }