Client.php 7.1 KB

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