PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/forum/library/Zend/Oauth/Consumer.php

https://github.com/albrzykowski/zforum
PHP | 273 lines | 128 code | 21 blank | 124 comment | 16 complexity | c1e958d833b34db29033bdea6a43f4a1 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: Consumer.php 22475 2010-06-20 18:25:36Z padraic $
  20. */
  21. /** Zend_Oauth */
  22. require_once 'Zend/Oauth.php';
  23. /** Zend_Uri */
  24. require_once 'Zend/Uri.php';
  25. /** Zend_Oauth_Http_RequestToken */
  26. require_once 'Zend/Oauth/Http/RequestToken.php';
  27. /** Zend_Oauth_Http_UserAuthorization */
  28. require_once 'Zend/Oauth/Http/UserAuthorization.php';
  29. /** Zend_Oauth_Http_AccessToken */
  30. require_once 'Zend/Oauth/Http/AccessToken.php';
  31. /** Zend_Oauth_Token_AuthorizedRequest */
  32. require_once 'Zend/Oauth/Token/AuthorizedRequest.php';
  33. /** Zend_Oauth_Config */
  34. require_once 'Zend/Oauth/Config.php';
  35. /**
  36. * @category Zend
  37. * @package Zend_Oauth
  38. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  39. * @license http://framework.zend.com/license/new-bsd New BSD License
  40. */
  41. class Zend_Oauth_Consumer extends Zend_Oauth
  42. {
  43. public $switcheroo = false; // replace later when this works
  44. /**
  45. * Request Token retrieved from OAuth Provider
  46. *
  47. * @var Zend_Oauth_Token_Request
  48. */
  49. protected $_requestToken = null;
  50. /**
  51. * Access token retrieved from OAuth Provider
  52. *
  53. * @var Zend_Oauth_Token_Access
  54. */
  55. protected $_accessToken = null;
  56. /**
  57. * @var Zend_Oauth_Config
  58. */
  59. protected $_config = null;
  60. /**
  61. * Constructor; create a new object with an optional array|Zend_Config
  62. * instance containing initialising options.
  63. *
  64. * @param array|Zend_Config $options
  65. * @return void
  66. */
  67. public function __construct($options = null)
  68. {
  69. $this->_config = new Zend_Oauth_Config;
  70. if (!is_null($options)) {
  71. if ($options instanceof Zend_Config) {
  72. $options = $options->toArray();
  73. }
  74. $this->_config->setOptions($options);
  75. }
  76. }
  77. /**
  78. * Attempts to retrieve a Request Token from an OAuth Provider which is
  79. * later exchanged for an authorized Access Token used to access the
  80. * protected resources exposed by a web service API.
  81. *
  82. * @param null|array $customServiceParameters Non-OAuth Provider-specified parameters
  83. * @param null|string $httpMethod
  84. * @param null|Zend_Oauth_Http_RequestToken $request
  85. * @return Zend_Oauth_Token_Request
  86. */
  87. public function getRequestToken(
  88. array $customServiceParameters = null,
  89. $httpMethod = null,
  90. Zend_Oauth_Http_RequestToken $request = null
  91. ) {
  92. if (is_null($request)) {
  93. $request = new Zend_Oauth_Http_RequestToken($this, $customServiceParameters);
  94. } elseif(!is_null($customServiceParameters)) {
  95. $request->setParameters($customServiceParameters);
  96. }
  97. if (!is_null($httpMethod)) {
  98. $request->setMethod($httpMethod);
  99. } else {
  100. $request->setMethod($this->getRequestMethod());
  101. }
  102. $this->_requestToken = $request->execute();
  103. return $this->_requestToken;
  104. }
  105. /**
  106. * After a Request Token is retrieved, the user may be redirected to the
  107. * OAuth Provider to authorize the application's access to their
  108. * protected resources - the redirect URL being provided by this method.
  109. * Once the user has authorized the application for access, they are
  110. * redirected back to the application which can now exchange the previous
  111. * Request Token for a fully authorized Access Token.
  112. *
  113. * @param null|array $customServiceParameters
  114. * @param null|Zend_Oauth_Token_Request $token
  115. * @param null|Zend_OAuth_Http_UserAuthorization $redirect
  116. * @return string
  117. */
  118. public function getRedirectUrl(
  119. array $customServiceParameters = null,
  120. Zend_Oauth_Token_Request $token = null,
  121. Zend_Oauth_Http_UserAuthorization $redirect = null
  122. ) {
  123. if (is_null($redirect)) {
  124. $redirect = new Zend_Oauth_Http_UserAuthorization($this, $customServiceParameters);
  125. } elseif(!is_null($customServiceParameters)) {
  126. $redirect->setParameters($customServiceParameters);
  127. }
  128. if (!is_null($token)) {
  129. $this->_requestToken = $token;
  130. }
  131. return $redirect->getUrl();
  132. }
  133. /**
  134. * Rather than retrieve a redirect URL for use, e.g. from a controller,
  135. * one may perform an immediate redirect.
  136. *
  137. * Sends headers and exit()s on completion.
  138. *
  139. * @param null|array $customServiceParameters
  140. * @param null|Zend_Oauth_Http_UserAuthorization $request
  141. * @return void
  142. */
  143. public function redirect(
  144. array $customServiceParameters = null,
  145. Zend_Oauth_Http_UserAuthorization $request = null
  146. ) {
  147. $redirectUrl = $this->getRedirectUrl($customServiceParameters, $request);
  148. header('Location: ' . $redirectUrl);
  149. exit(1);
  150. }
  151. /**
  152. * Retrieve an Access Token in exchange for a previously received/authorized
  153. * Request Token.
  154. *
  155. * @param array $queryData GET data returned in user's redirect from Provider
  156. * @param Zend_Oauth_Token_Request Request Token information
  157. * @param string $httpMethod
  158. * @param Zend_Oauth_Http_AccessToken $request
  159. * @return Zend_Oauth_Token_Access
  160. * @throws Zend_Oauth_Exception on invalid authorization token, non-matching response authorization token, or unprovided authorization token
  161. */
  162. public function getAccessToken(
  163. $queryData,
  164. Zend_Oauth_Token_Request $token,
  165. $httpMethod = null,
  166. Zend_Oauth_Http_AccessToken $request = null
  167. ) {
  168. $authorizedToken = new Zend_Oauth_Token_AuthorizedRequest($queryData);
  169. if (!$authorizedToken->isValid()) {
  170. require_once 'Zend/Oauth/Exception.php';
  171. throw new Zend_Oauth_Exception(
  172. 'Response from Service Provider is not a valid authorized request token');
  173. }
  174. if (is_null($request)) {
  175. $request = new Zend_Oauth_Http_AccessToken($this);
  176. }
  177. // OAuth 1.0a Verifier
  178. if (!is_null($authorizedToken->getParam('oauth_verifier'))) {
  179. $params = array_merge($request->getParameters(), array(
  180. 'oauth_verifier' => $authorizedToken->getParam('oauth_verifier')
  181. ));
  182. $request->setParameters($params);
  183. }
  184. if (!is_null($httpMethod)) {
  185. $request->setMethod($httpMethod);
  186. } else {
  187. $request->setMethod($this->getRequestMethod());
  188. }
  189. if (isset($token)) {
  190. if ($authorizedToken->getToken() !== $token->getToken()) {
  191. require_once 'Zend/Oauth/Exception.php';
  192. throw new Zend_Oauth_Exception(
  193. 'Authorized token from Service Provider does not match'
  194. . ' supplied Request Token details'
  195. );
  196. }
  197. } else {
  198. require_once 'Zend/Oauth/Exception.php';
  199. throw new Zend_Oauth_Exception('Request token must be passed to method');
  200. }
  201. $this->_requestToken = $token;
  202. $this->_accessToken = $request->execute();
  203. return $this->_accessToken;
  204. }
  205. /**
  206. * Return whatever the last Request Token retrieved was while using the
  207. * current Consumer instance.
  208. *
  209. * @return Zend_Oauth_Token_Request
  210. */
  211. public function getLastRequestToken()
  212. {
  213. return $this->_requestToken;
  214. }
  215. /**
  216. * Return whatever the last Access Token retrieved was while using the
  217. * current Consumer instance.
  218. *
  219. * @return Zend_Oauth_Token_Access
  220. */
  221. public function getLastAccessToken()
  222. {
  223. return $this->_accessToken;
  224. }
  225. /**
  226. * Alias to self::getLastAccessToken()
  227. *
  228. * @return Zend_Oauth_Token_Access
  229. */
  230. public function getToken()
  231. {
  232. return $this->_accessToken;
  233. }
  234. /**
  235. * Simple Proxy to the current Zend_Oauth_Config method. It's that instance
  236. * which holds all configuration methods and values this object also presents
  237. * as it's API.
  238. *
  239. * @param string $method
  240. * @param array $args
  241. * @return mixed
  242. * @throws Zend_Oauth_Exception if method does not exist in config object
  243. */
  244. public function __call($method, array $args)
  245. {
  246. if (!method_exists($this->_config, $method)) {
  247. require_once 'Zend/Oauth/Exception.php';
  248. throw new Zend_Oauth_Exception('Method does not exist: '.$method);
  249. }
  250. return call_user_func_array(array($this->_config,$method), $args);
  251. }
  252. }