PHP.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. /**
  3. * Dropbox OAuth
  4. *
  5. * @package Dropbox
  6. * @copyright Copyright (C) 2010 Rooftop Solutions. All rights reserved.
  7. * @author Evert Pot (http://www.rooftopsolutions.nl/)
  8. * @license http://code.google.com/p/dropbox-php/wiki/License MIT
  9. */
  10. /**
  11. * This class is used to sign all requests to dropbox.
  12. *
  13. * This specific class uses the PHP OAuth extension
  14. */
  15. class Dropbox_OAuth_PHP extends Dropbox_OAuth {
  16. /**
  17. * OAuth object
  18. *
  19. * @var OAuth
  20. */
  21. protected $oAuth;
  22. /**
  23. * Constructor
  24. *
  25. * @param string $consumerKey
  26. * @param string $consumerSecret
  27. */
  28. public function __construct($consumerKey, $consumerSecret) {
  29. if (!class_exists('OAuth'))
  30. throw new Dropbox_Exception('The OAuth class could not be found! Did you install and enable the oauth extension?');
  31. $this->OAuth = new OAuth($consumerKey, $consumerSecret,OAUTH_SIG_METHOD_HMACSHA1,OAUTH_AUTH_TYPE_URI);
  32. $this->OAuth->enableDebug();
  33. }
  34. /**
  35. * Sets the request token and secret.
  36. *
  37. * The tokens can also be passed as an array into the first argument.
  38. * The array must have the elements token and token_secret.
  39. *
  40. * @param string|array $token
  41. * @param string $token_secret
  42. * @return void
  43. */
  44. public function setToken($token, $token_secret = null) {
  45. parent::setToken($token,$token_secret);
  46. $this->OAuth->setToken($this->oauth_token, $this->oauth_token_secret);
  47. }
  48. /**
  49. * Fetches a secured oauth url and returns the response body.
  50. *
  51. * @param string $uri
  52. * @param mixed $arguments
  53. * @param string $method
  54. * @param array $httpHeaders
  55. * @return string
  56. */
  57. public function fetch($uri, $arguments = array(), $method = 'GET', $httpHeaders = array()) {
  58. try {
  59. $this->OAuth->fetch($uri, $arguments, $method, $httpHeaders);
  60. $result = $this->OAuth->getLastResponse();
  61. $lastResponseInfo = $this->OAuth->getLastResponseInfo();
  62. return array(
  63. 'httpStatus' => $lastResponseInfo['http_code'],
  64. 'body' => $result,
  65. );
  66. } catch (OAuthException $e) {
  67. $lastResponseInfo = $this->OAuth->getLastResponseInfo();
  68. switch($lastResponseInfo['http_code']) {
  69. // Not modified
  70. case 304 :
  71. return array(
  72. 'httpStatus' => 304,
  73. 'body' => null,
  74. );
  75. break;
  76. case 400 :
  77. throw new Dropbox_Exception_Forbidden('Forbidden. Bad input parameter. Error message should indicate which one and why.');
  78. case 401 :
  79. throw new Dropbox_Exception_Forbidden('Forbidden. Bad or expired token. This can happen if the user or Dropbox revoked or expired an access token. To fix, you should re-authenticate the user.');
  80. case 403 :
  81. throw new Dropbox_Exception_Forbidden('Forbidden. This could mean a bad OAuth request, or a file or folder already existing at the target location.');
  82. case 404 :
  83. throw new Dropbox_Exception_NotFound('Resource at uri: ' . $uri . ' could not be found');
  84. case 405 :
  85. throw new Dropbox_Exception_Forbidden('Forbidden. Request method not expected (generally should be GET or POST).');
  86. case 500 :
  87. throw new Dropbox_Exception_Forbidden('Server error. ' . $e->getMessage());
  88. case 503 :
  89. throw new Dropbox_Exception_Forbidden('Forbidden. Your app is making too many requests and is being rate limited. 503s can trigger on a per-app or per-user basis.');
  90. case 507 :
  91. throw new Dropbox_Exception_OverQuota('This dropbox is full');
  92. default:
  93. // rethrowing
  94. throw $e;
  95. }
  96. }
  97. }
  98. /**
  99. * Requests the OAuth request token.
  100. *
  101. * @return void
  102. */
  103. public function getRequestToken() {
  104. try {
  105. $tokens = $this->OAuth->getRequestToken(self::URI_REQUEST_TOKEN);
  106. $this->setToken($tokens['oauth_token'], $tokens['oauth_token_secret']);
  107. return $this->getToken();
  108. } catch (OAuthException $e) {
  109. throw new Dropbox_Exception_RequestToken('We were unable to fetch request tokens. This likely means that your consumer key and/or secret are incorrect.',0,$e);
  110. }
  111. }
  112. /**
  113. * Requests the OAuth access tokens.
  114. *
  115. * This method requires the 'unauthorized' request tokens
  116. * and, if successful will set the authorized request tokens.
  117. *
  118. * @return void
  119. */
  120. public function getAccessToken() {
  121. $uri = self::URI_ACCESS_TOKEN;
  122. $tokens = $this->OAuth->getAccessToken($uri);
  123. $this->setToken($tokens['oauth_token'], $tokens['oauth_token_secret']);
  124. return $this->getToken();
  125. }
  126. }