OAuth.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 an abstract OAuth class.
  12. *
  13. * It must be extended by classes who wish to provide OAuth functionality
  14. * using different libraries.
  15. */
  16. abstract class Dropbox_OAuth {
  17. /**
  18. * After a user has authorized access, dropbox can redirect the user back
  19. * to this url.
  20. *
  21. * @var string
  22. */
  23. public $authorizeCallbackUrl = null;
  24. /**
  25. * Uri used to fetch request tokens
  26. *
  27. * @var string
  28. */
  29. const URI_REQUEST_TOKEN = 'https://api.dropbox.com/1/oauth/request_token';
  30. /**
  31. * Uri used to redirect the user to for authorization.
  32. *
  33. * @var string
  34. */
  35. const URI_AUTHORIZE = 'https://www.dropbox.com/1/oauth/authorize';
  36. /**
  37. * Uri used to
  38. *
  39. * @var string
  40. */
  41. const URI_ACCESS_TOKEN = 'https://api.dropbox.com/1/oauth/access_token';
  42. /**
  43. * An OAuth request token.
  44. *
  45. * @var string
  46. */
  47. protected $oauth_token = null;
  48. /**
  49. * OAuth token secret
  50. *
  51. * @var string
  52. */
  53. protected $oauth_token_secret = null;
  54. /**
  55. * Get OAuth request last responce
  56. *
  57. * @var array
  58. */
  59. protected $lastResponse = array();
  60. /**
  61. * Input file stream pointer or file path for PUT method
  62. *
  63. * @var resource|string
  64. */
  65. protected $inFile = null;
  66. /**
  67. * Input file size for PUT method
  68. *
  69. * @var resource | string
  70. */
  71. protected $inFileSize = null;
  72. /**
  73. * Is support PUT method on OAuth consumer
  74. *
  75. * @var bool
  76. */
  77. protected $putSupported = false;
  78. /**
  79. * Constructor
  80. *
  81. * @param string $consumerKey
  82. * @param string $consumerSecret
  83. */
  84. abstract public function __construct($consumerKey, $consumerSecret);
  85. /**
  86. * Sets the request token and secret.
  87. *
  88. * The tokens can also be passed as an array into the first argument.
  89. * The array must have the elements token and token_secret.
  90. *
  91. * @param string|array $token
  92. * @param string $token_secret
  93. * @return void
  94. */
  95. public function setToken($token, $token_secret = null) {
  96. if (is_array($token)) {
  97. $this->oauth_token = $token['token'];
  98. $this->oauth_token_secret = $token['token_secret'];
  99. } else {
  100. $this->oauth_token = $token;
  101. $this->oauth_token_secret = $token_secret;
  102. }
  103. }
  104. /**
  105. * Returns the oauth request tokens as an associative array.
  106. *
  107. * The array will contain the elements 'token' and 'token_secret'.
  108. *
  109. * @return array
  110. */
  111. public function getToken() {
  112. return array(
  113. 'token' => $this->oauth_token,
  114. 'token_secret' => $this->oauth_token_secret,
  115. );
  116. }
  117. /**
  118. * Returns the authorization url
  119. *
  120. * @param string $callBack Specify a callback url to automatically redirect the user back
  121. * @return string
  122. */
  123. public function getAuthorizeUrl($callBack = null) {
  124. // Building the redirect uri
  125. $token = $this->getToken();
  126. $uri = self::URI_AUTHORIZE . '?oauth_token=' . $token['token'];
  127. if ($callBack) $uri.='&oauth_callback=' . $callBack;
  128. return $uri;
  129. }
  130. /**
  131. * Set input file for PUT method
  132. *
  133. * @param resource|string $file
  134. * @throws Dropbox_Exception
  135. */
  136. public function setInfile($file) {
  137. if (is_resource($file)) {
  138. $stat = fstat($file);
  139. $this->inFileSize = $stat['size'];
  140. } else if (is_string($file) && is_readable($file)) {
  141. $this->inFileSize = filesize($file);
  142. $file = fopen($file, 'rb');
  143. }
  144. if (!is_resource($file)) {
  145. throw new Dropbox_Exception('File must be a file-resource or a string');
  146. }
  147. $this->inFile = $file;
  148. }
  149. /**
  150. * Return is PUT method supported
  151. *
  152. * @return boolean
  153. */
  154. public function isPutSupport() {
  155. return $this->putSupported;
  156. }
  157. /**
  158. * Get last request response
  159. *
  160. * @return array:
  161. */
  162. public function getLastResponse() {
  163. return $this->lastResponse;
  164. }
  165. /**
  166. * Fetches a secured oauth url and returns the response body.
  167. *
  168. * @param string $uri
  169. * @param mixed $arguments
  170. * @param string $method
  171. * @param array $httpHeaders
  172. * @return string
  173. */
  174. public abstract function fetch($uri, $arguments = array(), $method = 'GET', $httpHeaders = array());
  175. /**
  176. * Requests the OAuth request token.
  177. *
  178. * @return array
  179. */
  180. abstract public function getRequestToken();
  181. /**
  182. * Requests the OAuth access tokens.
  183. *
  184. * @return array
  185. */
  186. abstract public function getAccessToken();
  187. }