/plugins/sfDoctrineOAuthPlugin/lib/base/sfOAuth1.class.php

https://github.com/edse/Elastball · PHP · 383 lines · 196 code · 51 blank · 136 comment · 14 complexity · 833a963d223e11611e16cebb423e74d2 MD5 · raw file

  1. <?php
  2. /**
  3. *
  4. *
  5. *
  6. * Implementation of OAuth version 1
  7. *
  8. * @author Maxime Picaud
  9. * @since 21 août 2010
  10. */
  11. class sfOAuth1 extends sfOAuth
  12. {
  13. /**
  14. *
  15. * contains consumer_key and consumer_secret
  16. * @var OAuthConsumer $consumer
  17. */
  18. protected $consumer;
  19. /**
  20. *
  21. * url to request token
  22. * @var string $request_token_url
  23. */
  24. protected $request_token_url;
  25. /**
  26. * parameters passed for each api request.
  27. *
  28. * @var array $parameters
  29. */
  30. protected $request_parameters = array();
  31. /**
  32. * Constructor - set version = 1
  33. *
  34. * @author Maxime Picaud
  35. * @since 21 août 2010
  36. */
  37. public function __construct($key, $secret, $token = null, $config = array())
  38. {
  39. $this->version = 1;
  40. $this->init($config, 'request_token_url');
  41. $this->init($config, 'consumer');
  42. $this->init($config, 'request_parameters', 'add');
  43. parent::__construct($key, $secret, $token, $config);
  44. }
  45. /**
  46. * getter $consumer
  47. *
  48. * @return OAuthConsumer
  49. *
  50. * @author Maxime Picaud
  51. * @since 21 août 2010
  52. */
  53. public function getConsumer()
  54. {
  55. if(is_null($this->consumer))
  56. {
  57. $this->consumer = new OAuthConsumer($this->getKey(), $this->getSecret());
  58. }
  59. return $this->consumer;
  60. }
  61. /**
  62. *
  63. * @param OAuthConsumer $consumer
  64. *
  65. * setter $consumer
  66. *
  67. * @author Maxime Picaud
  68. * @since 21 août 2010
  69. */
  70. public function setConsumer(OAuthConsumer $consumer)
  71. {
  72. $this->consumer = $consumer;
  73. }
  74. /**
  75. * getter $request_token_url
  76. *
  77. * @return string
  78. *
  79. * @author Maxime Picaud
  80. * @since 21 août 2010
  81. */
  82. public function getRequestTokenUrl()
  83. {
  84. return $this->request_token_url;
  85. }
  86. /**
  87. *
  88. * @param string $request_token_url
  89. *
  90. * setter $request_token_url
  91. *
  92. * @author Maxime Picaud
  93. * @since 21 août 2010
  94. */
  95. public function setRequestTokenUrl($request_token_url)
  96. {
  97. $this->request_token_url = $request_token_url;
  98. }
  99. /**
  100. *
  101. * retrieve the request token
  102. *
  103. * @return Token
  104. *
  105. * @author Maxime Picaud
  106. * @since 21 août 2010
  107. */
  108. public function getRequestToken($parameters = array())
  109. {
  110. $this->addRequestParameters($parameters);
  111. $this->setRequestParameter('oauth_callback', $this->getCallback());
  112. $request = OAuthRequest::from_consumer_and_token($this->getConsumer(), $this->getToken('oauth'), 'POST', $this->getRequestTokenUrl(), $this->getRequestParameters());
  113. $request->sign_request(new OAuthSignatureMethod_HMAC_SHA1(), $this->getConsumer(), $this->getToken('oauth'));
  114. $params = OAuthUtil::parse_parameters($this->call($this->getRequestTokenUrl(), $request->to_postdata()));
  115. $oauth_token = isset($params['oauth_token'])?$params['oauth_token']:null;
  116. $oauth_token_secret = isset($params['oauth_token_secret'])?$params['oauth_token_secret']:null;
  117. if((is_null($oauth_token) || is_null($oauth_token_secret)) && $this->getLogger())
  118. {
  119. $error = sprintf('{OAuth} access token failed - %s returns %s', $this->getName(), print_r($params, true));
  120. $this->getLogger()->err($error);
  121. }
  122. elseif($this->getLogger())
  123. {
  124. $message = sprintf('{OAuth} %s return %s', $this->getName(), print_r($params, true));
  125. $this->getLogger()->info($message);
  126. }
  127. $token = new Token();
  128. $token->setTokenKey($oauth_token);
  129. $token->setTokenSecret($oauth_token_secret);
  130. $token->setName($this->getName());
  131. $token->setStatus(Token::STATUS_REQUEST);
  132. $token->setOAuthVersion($this->getVersion());
  133. unset($params['oauth_token'], $params['oauth_token_secret']);
  134. if(count($params) > 0)
  135. {
  136. $token->setParams($params);
  137. }
  138. $this->setToken($token);
  139. return $token;
  140. }
  141. /**
  142. * (non-PHPdoc)
  143. * @see plugins/sfDoctrineOAuthPlugin/lib/sfOAuth::requestAuth()
  144. */
  145. public function requestAuth($parameters = array())
  146. {
  147. if(is_null($this->getToken()))
  148. {
  149. throw new sfException(sprintf('there is no available token to request auth in "%s" oauth', $this->getName()));
  150. }
  151. if($this->getController())
  152. {
  153. $this->setAuthParameter('oauth_token', $this->getToken()->getTokenKey());
  154. $this->addAuthParameters($parameters);
  155. $url = $this->getRequestAuthUrl().'?'.http_build_query($this->getAuthParameters());
  156. if($this->getLogger())
  157. {
  158. $this->getLogger()->info(sprintf('{OAuth} "%s" call url "%s" with params "%s"',
  159. $this->getName(),
  160. $this->getRequestAuthUrl(),
  161. var_export($this->getAuthParameters(), true)
  162. )
  163. );
  164. }
  165. $this->getController()->redirect($url);
  166. }
  167. else
  168. {
  169. if($this->getLogger())
  170. {
  171. $this->getLogger()->err(sprintf('{OAuth} "%s" no controller to execute the request', $this->getName()));
  172. }
  173. }
  174. }
  175. /**
  176. * (non-PHPdoc)
  177. * @see plugins/sfDoctrineOAuthPlugin/lib/sfOAuth::getAccessToken()
  178. */
  179. public function getAccessToken($verifier, $parameters = array())
  180. {
  181. $this->setAccessParameter('oauth_verifier', $verifier);
  182. $this->addAccessParameters($parameters);
  183. $request = OAuthRequest::from_consumer_and_token($this->getConsumer(), $this->getToken('oauth'), 'POST', $this->getAccessTokenUrl(), $this->getAccessParameters());
  184. $request->sign_request(new OAuthSignatureMethod_HMAC_SHA1(), $this->getConsumer(), $this->getToken('oauth'));
  185. $params = OAuthUtil::parse_parameters($this->call($this->getAccessTokenUrl(), $request->to_postdata()));
  186. $oauth_token = isset($params['oauth_token'])?$params['oauth_token']:null;
  187. $oauth_token_secret = isset($params['oauth_token_secret'])?$params['oauth_token_secret']:null;
  188. if((is_null($oauth_token) || is_null($oauth_token_secret)) && $this->getLogger())
  189. {
  190. $error = sprintf('{OAuth} access token failed - %s returns %s', $this->getName(), print_r($params, true));
  191. $this->getLogger()->err($error);
  192. }
  193. elseif($this->getLogger())
  194. {
  195. $message = sprintf('{OAuth} %s return %s', $this->getName(), print_r($params, true));
  196. $this->getLogger()->info($message);
  197. }
  198. $token = new Token();
  199. $token->setTokenKey($oauth_token);
  200. $token->setTokenSecret($oauth_token_secret);
  201. $token->setName($this->getName());
  202. $token->setStatus(Token::STATUS_ACCESS);
  203. $token->setOAuthVersion($this->getVersion());
  204. unset($params['oauth_token'], $params['oauth_token_secret']);
  205. if(count($params) > 0)
  206. {
  207. $token->setParams($params);
  208. }
  209. $this->setExpire($token);
  210. //override request_token
  211. $this->setToken($token);
  212. $token->setIdentifier($this->getIdentifier());
  213. $this->setToken($token);
  214. return $token;
  215. }
  216. protected function prepareCall($action, $aliases = null, $params = array(), $method = 'GET')
  217. {
  218. if(in_array($method, array('GET', 'POST')))
  219. {
  220. $this->addCallParameters($params);
  221. }
  222. else
  223. {
  224. $method = 'POST';
  225. }
  226. $url = $this->formatUrl($action, $aliases);
  227. $request = OAuthRequest::from_consumer_and_token($this->getConsumer(), $this->getToken('oauth'), $method, $url, $this->getCallParameters());
  228. $request->sign_request(new OAuthSignatureMethod_HMAC_SHA1(), $this->getConsumer(), $this->getToken('oauth'));
  229. return array($url, $request);
  230. }
  231. /**
  232. * overriden for OAuth 1
  233. *
  234. * @author Maxime Picaud
  235. * @since 19 août 2010
  236. */
  237. public function get($action, $aliases = null, $params = array())
  238. {
  239. list($url, $request) = $this->prepareCall($action, $aliases, $params, 'GET');
  240. $response = $this->call($request->to_url(), null, null, 'GET');
  241. return $this->formatResult($response);
  242. }
  243. public function post($action, $aliases = null, $params = array())
  244. {
  245. list($url, $request) = $this->prepareCall($action, $aliases, $params, 'POST');
  246. $this->setCallParameters($request->to_postdata());
  247. $response = $this->call($url, $this->getCallParameters(), null, 'POST');
  248. return $this->formatResult($response);
  249. }
  250. public function put($action, $aliases = null, $params = array())
  251. {
  252. list($url, $request) = $this->prepareCall($action, $aliases, $params, 'PUT');
  253. $this->setCallParameters($request->to_postdata());
  254. $response = $this->call($url, $this->getCallParameters(), $params, 'PUT');
  255. return $this->formatResult($response);
  256. }
  257. public function delete($action, $aliases = null, $params = array())
  258. {
  259. list($url, $request) = $this->prepareCall($action, $aliases, $params, 'DELETE');
  260. $this->setCallParameters($request->to_postdata());
  261. $response = $this->call($url, $this->getCallParameters(), $params, 'DELETE');
  262. return $this->formatResult($response);
  263. }
  264. /**
  265. *
  266. * @param array $parameters
  267. *
  268. * setter $parameters
  269. *
  270. * @author Maxime Picaud
  271. * @since 21 août 2010
  272. */
  273. public function setRequestParameters($parameters)
  274. {
  275. $this->request_parameters = $parameters;
  276. }
  277. /**
  278. *
  279. * @param mixed $key
  280. * @param mixed $value
  281. *
  282. * set a parameter
  283. *
  284. * @author Maxime Picaud
  285. * @since 21 août 2010
  286. */
  287. public function setRequestParameter($key, $value)
  288. {
  289. $this->request_parameters[$key] = $value;
  290. }
  291. /**
  292. * getter $parameters
  293. *
  294. * @return array
  295. *
  296. * @author Maxime Picaud
  297. * @since 21 août 2010
  298. */
  299. public function getRequestParameters()
  300. {
  301. return $this->request_parameters;
  302. }
  303. /**
  304. *
  305. * @param mixed $key
  306. * @param mixed $default
  307. *
  308. * Retrieve a parameter by its key and return $default if is undefined
  309. *
  310. * @return mixed
  311. *
  312. * @author Maxime Picaud
  313. * @since 21 août 2010
  314. */
  315. public function getRequestParameter($key, $default = null)
  316. {
  317. return isset($this->request_parameters[$key])?$this->request_parameters[$key]:$default;
  318. }
  319. /**
  320. *
  321. * @param array $parameters
  322. *
  323. * merge current parameters with this $parameters
  324. *
  325. * @author Maxime Picaud
  326. * @since 21 août 2010
  327. */
  328. public function addRequestParameters($parameters)
  329. {
  330. $this->request_parameters = array_merge($this->request_parameters, $parameters);
  331. }
  332. }