API.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. <?php
  2. /**
  3. * Dropbox API class
  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. class Dropbox_API {
  11. /**
  12. * Sandbox root-path
  13. */
  14. const ROOT_SANDBOX = 'sandbox';
  15. /**
  16. * Dropbox root-path
  17. */
  18. const ROOT_DROPBOX = 'dropbox';
  19. /**
  20. * API URl
  21. */
  22. protected $api_url = 'https://api.dropbox.com/1/';
  23. /**
  24. * Content API URl
  25. */
  26. protected $api_content_url = 'https://api-content.dropbox.com/1/';
  27. /**
  28. * OAuth object
  29. *
  30. * @var Dropbox_OAuth
  31. */
  32. protected $oauth;
  33. /**
  34. * Default root-path, this will most likely be 'sandbox' or 'dropbox'
  35. *
  36. * @var string
  37. */
  38. protected $root;
  39. protected $useSSL;
  40. /**
  41. * Constructor
  42. *
  43. * @param Dropbox_OAuth Dropbox_Auth object
  44. * @param string $root default root path (sandbox or dropbox)
  45. */
  46. public function __construct(Dropbox_OAuth $oauth, $root = self::ROOT_DROPBOX, $useSSL = true) {
  47. $this->oauth = $oauth;
  48. $this->root = $root;
  49. $this->useSSL = $useSSL;
  50. if (!$this->useSSL)
  51. {
  52. throw new Dropbox_Exception('Dropbox REST API now requires that all requests use SSL');
  53. }
  54. }
  55. /**
  56. * Returns information about the current dropbox account
  57. *
  58. * @return stdclass
  59. */
  60. public function getAccountInfo() {
  61. $data = $this->oauth->fetch($this->api_url . 'account/info');
  62. return json_decode($data['body'],true);
  63. }
  64. /**
  65. * Returns a file's contents
  66. *
  67. * @param string $path path
  68. * @param string $root Use this to override the default root path (sandbox/dropbox)
  69. * @return string
  70. */
  71. public function getFile($path = '', $root = null) {
  72. if (is_null($root)) $root = $this->root;
  73. $path = str_replace(array('%2F','~'), array('/','%7E'), rawurlencode($path));
  74. $result = $this->oauth->fetch($this->api_content_url . 'files/' . $root . '/' . ltrim($path,'/'));
  75. return $result['body'];
  76. }
  77. /**
  78. * Uploads a new file
  79. *
  80. * @param string $path Target path (including filename)
  81. * @param string $file Either a path to a file or a stream resource
  82. * @param string $root Use this to override the default root path (sandbox/dropbox)
  83. * @return bool
  84. */
  85. public function putFile($path, $file, $root = null) {
  86. $directory = dirname($path);
  87. $filename = basename($path);
  88. if($directory==='.') $directory = '';
  89. $directory = str_replace(array('%2F','~'), array('/','%7E'), rawurlencode($directory));
  90. // $filename = str_replace('~', '%7E', rawurlencode($filename));
  91. if (is_null($root)) $root = $this->root;
  92. if (is_string($file)) {
  93. $file = fopen($file,'rb');
  94. } elseif (!is_resource($file)) {
  95. throw new Dropbox_Exception('File must be a file-resource or a string');
  96. }
  97. $result=$this->multipartFetch($this->api_content_url . 'files/' .
  98. $root . '/' . trim($directory,'/'), $file, $filename);
  99. if(!isset($result["httpStatus"]) || $result["httpStatus"] != 200)
  100. throw new Dropbox_Exception("Uploading file to Dropbox failed");
  101. return true;
  102. }
  103. /**
  104. * Copies a file or directory from one location to another
  105. *
  106. * This method returns the file information of the newly created file.
  107. *
  108. * @param string $from source path
  109. * @param string $to destination path
  110. * @param string $root Use this to override the default root path (sandbox/dropbox)
  111. * @return stdclass
  112. */
  113. public function copy($from, $to, $root = null) {
  114. if (is_null($root)) $root = $this->root;
  115. $response = $this->oauth->fetch($this->api_url . 'fileops/copy', array('from_path' => $from, 'to_path' => $to, 'root' => $root), 'POST');
  116. return json_decode($response['body'],true);
  117. }
  118. /**
  119. * Creates a new folder
  120. *
  121. * This method returns the information from the newly created directory
  122. *
  123. * @param string $path
  124. * @param string $root Use this to override the default root path (sandbox/dropbox)
  125. * @return stdclass
  126. */
  127. public function createFolder($path, $root = null) {
  128. if (is_null($root)) $root = $this->root;
  129. // Making sure the path starts with a /
  130. // $path = '/' . ltrim($path,'/');
  131. $response = $this->oauth->fetch($this->api_url . 'fileops/create_folder', array('path' => $path, 'root' => $root),'POST');
  132. return json_decode($response['body'],true);
  133. }
  134. /**
  135. * Deletes a file or folder.
  136. *
  137. * This method will return the metadata information from the deleted file or folder, if successful.
  138. *
  139. * @param string $path Path to new folder
  140. * @param string $root Use this to override the default root path (sandbox/dropbox)
  141. * @return array
  142. */
  143. public function delete($path, $root = null) {
  144. if (is_null($root)) $root = $this->root;
  145. $response = $this->oauth->fetch($this->api_url . 'fileops/delete', array('path' => $path, 'root' => $root), 'POST');
  146. return json_decode($response['body']);
  147. }
  148. /**
  149. * Moves a file or directory to a new location
  150. *
  151. * This method returns the information from the newly created directory
  152. *
  153. * @param mixed $from Source path
  154. * @param mixed $to destination path
  155. * @param string $root Use this to override the default root path (sandbox/dropbox)
  156. * @return stdclass
  157. */
  158. public function move($from, $to, $root = null) {
  159. if (is_null($root)) $root = $this->root;
  160. $response = $this->oauth->fetch($this->api_url . 'fileops/move', array('from_path' => rawurldecode($from), 'to_path' => rawurldecode($to), 'root' => $root), 'POST');
  161. return json_decode($response['body'],true);
  162. }
  163. /**
  164. * Returns file and directory information
  165. *
  166. * @param string $path Path to receive information from
  167. * @param bool $list When set to true, this method returns information from all files in a directory. When set to false it will only return infromation from the specified directory.
  168. * @param string $hash If a hash is supplied, this method simply returns true if nothing has changed since the last request. Good for caching.
  169. * @param int $fileLimit Maximum number of file-information to receive
  170. * @param string $root Use this to override the default root path (sandbox/dropbox)
  171. * @return array|true
  172. */
  173. public function getMetaData($path, $list = true, $hash = null, $fileLimit = null, $root = null) {
  174. if (is_null($root)) $root = $this->root;
  175. $args = array(
  176. 'list' => $list,
  177. );
  178. if (!is_null($hash)) $args['hash'] = $hash;
  179. if (!is_null($fileLimit)) $args['file_limit'] = $fileLimit;
  180. $path = str_replace(array('%2F','~'), array('/','%7E'), rawurlencode($path));
  181. $response = $this->oauth->fetch($this->api_url . 'metadata/' . $root . '/' . ltrim($path,'/'), $args);
  182. /* 304 is not modified */
  183. if ($response['httpStatus']==304) {
  184. return true;
  185. } else {
  186. return json_decode($response['body'],true);
  187. }
  188. }
  189. /**
  190. * A way of letting you keep up with changes to files and folders in a user's Dropbox. You can periodically call /delta to get a list of "delta entries", which are instructions on how to update your local state to match the server's state.
  191. *
  192. * This method returns the information from the newly created directory
  193. *
  194. * @param string $cursor A string that is used to keep track of your current state. On the next call pass in this value to return delta entries that have been recorded since the cursor was returned.
  195. * @return stdclass
  196. */
  197. public function delta($cursor) {
  198. $arg['cursor'] = $cursor;
  199. $response = $this->oauth->fetch($this->api_url . 'delta', $arg, 'POST');
  200. return json_decode($response['body'],true);
  201. }
  202. /**
  203. * Returns a thumbnail (as a string) for a file path.
  204. *
  205. * @param string $path Path to file
  206. * @param string $size small, medium or large
  207. * @param string $root Use this to override the default root path (sandbox/dropbox)
  208. * @return string
  209. */
  210. public function getThumbnail($path, $size = 'small', $root = null) {
  211. if (is_null($root)) $root = $this->root;
  212. $path = str_replace(array('%2F','~'), array('/','%7E'), rawurlencode($path));
  213. $response = $this->oauth->fetch($this->api_content_url . 'thumbnails/' . $root . '/' . ltrim($path,'/'),array('size' => $size));
  214. return $response['body'];
  215. }
  216. /**
  217. * This method is used to generate multipart POST requests for file upload
  218. *
  219. * @param string $uri
  220. * @param array $arguments
  221. * @return bool
  222. */
  223. protected function multipartFetch($uri, $file, $filename) {
  224. /* random string */
  225. $boundary = 'R50hrfBj5JYyfR3vF3wR96GPCC9Fd2q2pVMERvEaOE3D8LZTgLLbRpNwXek3';
  226. $headers = array(
  227. 'Content-Type' => 'multipart/form-data; boundary=' . $boundary,
  228. );
  229. $body="--" . $boundary . "\r\n";
  230. $body.="Content-Disposition: form-data; name=file; filename=".rawurldecode($filename)."\r\n";
  231. $body.="Content-type: application/octet-stream\r\n";
  232. $body.="\r\n";
  233. $body.=stream_get_contents($file);
  234. $body.="\r\n";
  235. $body.="--" . $boundary . "--";
  236. // Dropbox requires the filename to also be part of the regular arguments, so it becomes
  237. // part of the signature.
  238. $uri.='?file=' . $filename;
  239. return $this->oauth->fetch($uri, $body, 'POST', $headers);
  240. }
  241. /**
  242. * Search
  243. *
  244. * Returns metadata for all files and folders that match the search query.
  245. *
  246. * @added by: diszo.sasil
  247. *
  248. * @param string $query
  249. * @param string $root Use this to override the default root path (sandbox/dropbox)
  250. * @param string $path
  251. * @return array
  252. */
  253. public function search($query = '', $root = null, $path = ''){
  254. if (is_null($root)) $root = $this->root;
  255. if(!empty($path)){
  256. $path = str_replace(array('%2F','~'), array('/','%7E'), rawurlencode($path));
  257. }
  258. $response = $this->oauth->fetch($this->api_url . 'search/' . $root . '/' . ltrim($path,'/'),array('query' => $query));
  259. return json_decode($response['body'],true);
  260. }
  261. /**
  262. * Creates and returns a shareable link to files or folders.
  263. *
  264. * Note: Links created by the /shares API call expire after thirty days.
  265. *
  266. * @param type $path
  267. * @param type $root
  268. * @return type
  269. */
  270. public function share($path, $root = null) {
  271. if (is_null($root)) $root = $this->root;
  272. $path = str_replace(array('%2F','~'), array('/','%7E'), rawurlencode($path));
  273. $response = $this->oauth->fetch($this->api_url. 'shares/'. $root . '/' . ltrim($path, '/'), array(), 'POST');
  274. return json_decode($response['body'],true);
  275. }
  276. /**
  277. * Returns a link directly to a file.
  278. * Similar to /shares. The difference is that this bypasses the Dropbox webserver, used to provide a preview of the file, so that you can effectively stream the contents of your media.
  279. *
  280. * Note: The /media link expires after four hours, allotting enough time to stream files, but not enough to leave a connection open indefinitely.
  281. *
  282. * @param type $path
  283. * @param type $root
  284. * @return type
  285. */
  286. public function media($path, $root = null) {
  287. if (is_null($root)) $root = $this->root;
  288. $path = str_replace(array('%2F','~'), array('/','%7E'), rawurlencode($path));
  289. $response = $this->oauth->fetch($this->api_url. 'media/'. $root . '/' . ltrim($path, '/'), array(), 'POST');
  290. return json_decode($response['body'],true);
  291. }
  292. /**
  293. * Creates and returns a copy_ref to a file. This reference string can be used to copy that file to another user's Dropbox by passing it in as the from_copy_ref parameter on /fileops/copy.
  294. *
  295. * @param type $path
  296. * @param type $root
  297. * @return type
  298. */
  299. public function copy_ref($path, $root = null) {
  300. if (is_null($root)) $root = $this->root;
  301. $path = str_replace(array('%2F','~'), array('/','%7E'), rawurlencode($path));
  302. $response = $this->oauth->fetch($this->api_url. 'copy_ref/'. $root . '/' . ltrim($path, '/'));
  303. return json_decode($response['body'],true);
  304. }
  305. }