PEAR.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. if (!class_exists('HTTP_OAuth_Consumer')) {
  11. // We're going to try to load in manually
  12. include 'HTTP/OAuth/Consumer.php';
  13. }
  14. if (!class_exists('HTTP_OAuth_Consumer'))
  15. throw new Dropbox_Exception('The HTTP_OAuth_Consumer class could not be found! Did you install the pear HTTP_OAUTH class?');
  16. /**
  17. * This class is used to sign all requests to dropbox
  18. *
  19. * This classes use the PEAR HTTP_OAuth package. Make sure this is installed.
  20. */
  21. class Dropbox_OAuth_PEAR extends Dropbox_OAuth {
  22. /**
  23. * OAuth object
  24. *
  25. * @var OAuth
  26. */
  27. protected $oAuth;
  28. /**
  29. * OAuth consumer key
  30. *
  31. * We need to keep this around for later.
  32. *
  33. * @var string
  34. */
  35. protected $consumerKey;
  36. /**
  37. * Constructor
  38. *
  39. * @param string $consumerKey
  40. * @param string $consumerSecret
  41. */
  42. public function __construct($consumerKey, $consumerSecret)
  43. {
  44. $this->OAuth = new Dropbox_OAuth_Consumer_Dropbox($consumerKey, $consumerSecret);
  45. $this->consumerKey = $consumerKey;
  46. }
  47. /**
  48. * Sets the request token and secret.
  49. *
  50. * The tokens can also be passed as an array into the first argument.
  51. * The array must have the elements token and token_secret.
  52. *
  53. * @param string|array $token
  54. * @param string $token_secret
  55. * @return void
  56. */
  57. public function setToken($token, $token_secret = null) {
  58. parent::setToken($token,$token_secret);
  59. $this->OAuth->setToken($this->oauth_token);
  60. $this->OAuth->setTokenSecret($this->oauth_token_secret);
  61. }
  62. /**
  63. * Fetches a secured oauth url and returns the response body.
  64. *
  65. * @param string $uri
  66. * @param mixed $arguments
  67. * @param string $method
  68. * @param array $httpHeaders
  69. * @return string
  70. */
  71. public function fetch($uri, $arguments = array(), $method = 'GET', $httpHeaders = array())
  72. {
  73. $httpRequest = new HTTP_Request2(null,
  74. HTTP_Request2::METHOD_GET,
  75. array(
  76. 'ssl_verify_peer' => false,
  77. 'ssl_verify_host' => false
  78. )
  79. );
  80. $consumerRequest = new HTTP_OAuth_Consumer_Request();
  81. $consumerRequest->accept($httpRequest);
  82. $consumerRequest->setUrl($uri);
  83. $consumerRequest->setMethod($method);
  84. $consumerRequest->setSecrets($this->OAuth->getSecrets());
  85. $parameters = array(
  86. 'oauth_consumer_key' => $this->consumerKey,
  87. 'oauth_signature_method' => 'HMAC-SHA1',
  88. 'oauth_token' => $this->oauth_token,
  89. );
  90. if (is_array($arguments)) {
  91. $parameters = array_merge($parameters,$arguments);
  92. } elseif (is_string($arguments)) {
  93. $consumerRequest->setBody($arguments);
  94. }
  95. $consumerRequest->setParameters($parameters);
  96. if (count($httpHeaders)) {
  97. foreach($httpHeaders as $k=>$v) {
  98. $consumerRequest->setHeader($k, $v);
  99. }
  100. }
  101. $response = $consumerRequest->send();
  102. switch($response->getStatus()) {
  103. // Not modified
  104. case 304 :
  105. return array(
  106. 'httpStatus' => 304,
  107. 'body' => null,
  108. );
  109. break;
  110. case 400 :
  111. throw new Dropbox_Exception_Forbidden('Forbidden. Bad input parameter. Error message should indicate which one and why.');
  112. case 401 :
  113. 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.');
  114. case 403 :
  115. throw new Dropbox_Exception_Forbidden('Forbidden. This could mean a bad OAuth request, or a file or folder already existing at the target location.');
  116. case 404 :
  117. throw new Dropbox_Exception_NotFound('Resource at uri: ' . $uri . ' could not be found');
  118. case 405 :
  119. throw new Dropbox_Exception_Forbidden('Forbidden. Request method not expected (generally should be GET or POST).');
  120. case 500 :
  121. throw new Dropbox_Exception_Forbidden('Server error. ' . $e->getMessage());
  122. case 503 :
  123. 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.');
  124. case 507 :
  125. throw new Dropbox_Exception_OverQuota('This dropbox is full');
  126. }
  127. return array(
  128. 'httpStatus' => $response->getStatus(),
  129. 'body' => $response->getBody()
  130. );
  131. }
  132. /**
  133. * Requests the OAuth request token.
  134. *
  135. * @return void
  136. */
  137. public function getRequestToken() {
  138. $this->OAuth->getRequestToken(self::URI_REQUEST_TOKEN);
  139. $this->setToken($this->OAuth->getToken(), $this->OAuth->getTokenSecret());
  140. return $this->getToken();
  141. }
  142. /**
  143. * Requests the OAuth access tokens.
  144. *
  145. * This method requires the 'unauthorized' request tokens
  146. * and, if successful will set the authorized request tokens.
  147. *
  148. * @return void
  149. */
  150. public function getAccessToken() {
  151. $this->OAuth->getAccessToken(self::URI_ACCESS_TOKEN);
  152. $this->setToken($this->OAuth->getToken(), $this->OAuth->getTokenSecret());
  153. return $this->getToken();
  154. }
  155. }