PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/src/libraries/external/Dropbox/API.php

https://github.com/yodasan/frontend
PHP | 388 lines | 135 code | 76 blank | 177 comment | 19 complexity | 0584f4bdac31a28bce18fc68cfadca36 MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause
  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 = 'api.dropbox.com/1/';
  23. /**
  24. * Content API URl
  25. */
  26. protected $api_content_url = '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. /**
  40. * Determines if connections should use SSL or not.
  41. * Defaults to true in construtor
  42. *
  43. * @deprecated New Dropbox rest calls require SSL.
  44. * @var boolean
  45. */
  46. protected $useSSL;
  47. /**
  48. * Constructor
  49. *
  50. * @param Dropbox_OAuth Dropbox_Auth object
  51. * @param string $root default root path (sandbox or dropbox)
  52. */
  53. public function __construct(Dropbox_OAuth $oauth, $root = self::ROOT_DROPBOX, $useSSL = true) {
  54. $this->oauth = $oauth;
  55. $this->root = $root;
  56. $this->useSSL = $useSSL;
  57. if ($this->useSSL)
  58. {
  59. $this->api_url = 'https://' . $this->api_url;
  60. $this->api_content_url = 'https://' . $this->api_content_url;
  61. } else
  62. {
  63. $this->api_url = 'http://' . $this->api_url;
  64. $this->api_content_url = 'http://' . $this->api_content_url;
  65. }
  66. }
  67. /**
  68. * Returns OAuth tokens based on an email address and passwords
  69. *
  70. * This can be used to bypass the regular oauth workflow.
  71. *
  72. * This method returns an array with 2 elements:
  73. * * token
  74. * * secret
  75. *
  76. * @param string $email
  77. * @param string $password
  78. * @return array
  79. */
  80. public function getToken($email, $password) {
  81. $data = $this->oauth->fetch($this->api_url . 'token', array(
  82. 'email' => $email,
  83. 'password' => $password
  84. ),'POST');
  85. $data = json_decode($data['body']);
  86. return array(
  87. 'token' => $data->token,
  88. 'token_secret' => $data->secret,
  89. );
  90. }
  91. /**
  92. * Returns information about the current dropbox account
  93. *
  94. * @return stdclass
  95. */
  96. public function getAccountInfo() {
  97. $data = $this->oauth->fetch($this->api_url . 'account/info');
  98. return json_decode($data['body'],true);
  99. }
  100. /**
  101. * Creates a new Dropbox account
  102. *
  103. * @param string $email
  104. * @param string $first_name
  105. * @param string $last_name
  106. * @param string $password
  107. * @return bool
  108. */
  109. public function createAccount($email, $first_name, $last_name, $password) {
  110. $result = $this->oauth->fetch($this->api_url . 'account',array(
  111. 'email' => $email,
  112. 'first_name' => $first_name,
  113. 'last_name' => $last_name,
  114. 'password' => $password,
  115. ), 'POST');
  116. return $result['body']==='OK';
  117. }
  118. /**
  119. * Returns a file's contents
  120. *
  121. * @param string $path path
  122. * @param string $root Use this to override the default root path (sandbox/dropbox)
  123. * @return string
  124. */
  125. public function getFile($path = '', $root = null) {
  126. if (is_null($root)) $root = $this->root;
  127. $path = str_replace(array('%2F','~'), array('/','%7E'), rawurlencode($path));
  128. $result = $this->oauth->fetch($this->api_content_url . 'files/' . $root . '/' . ltrim($path,'/'));
  129. return $result['body'];
  130. }
  131. /**
  132. * Returns a file's signed URL
  133. *
  134. * @param string $path path
  135. * @param string $root Use this to override the default root path (sandbox/dropbox)
  136. * @return string
  137. */
  138. public function getFileUrl($path = '', $root = null) {
  139. if (is_null($root)) $root = $this->root;
  140. $path = str_replace(array('%2F','~'), array('/','%7E'), rawurlencode($path));
  141. $url = $this->oauth->fetchUrl($this->api_content_url . 'files/' . $root . '/' . ltrim($path,'/'));
  142. return $url;
  143. }
  144. /**
  145. * Uploads a new file
  146. *
  147. * @param string $path Target path (including filename)
  148. * @param string $file Either a path to a file or a stream resource
  149. * @param string $root Use this to override the default root path (sandbox/dropbox)
  150. * @return bool
  151. */
  152. public function putFile($path, $file, $root = null) {
  153. $directory = dirname($path);
  154. $filename = basename($path);
  155. if($directory==='.') $directory = '';
  156. if (is_null($root)) $root = $this->root;
  157. if (is_string($file)) {
  158. $file = fopen($file,'r');
  159. } elseif (!is_resource($file)) {
  160. throw new Dropbox_Exception('File must be a file-resource or a string');
  161. }
  162. $result=$this->multipartFetch($this->api_content_url . 'files/' .
  163. $root . '/' . trim($directory,'/'), $file, $filename);
  164. if(!isset($result["httpStatus"]) || $result["httpStatus"] != 200)
  165. throw new Dropbox_Exception("Uploading file to Dropbox failed");
  166. return true;
  167. }
  168. /**
  169. * Copies a file or directory from one location to another
  170. *
  171. * This method returns the file information of the newly created file.
  172. *
  173. * @param string $from source path
  174. * @param string $to destination path
  175. * @param string $root Use this to override the default root path (sandbox/dropbox)
  176. * @return stdclass
  177. */
  178. public function copy($from, $to, $root = null) {
  179. if (is_null($root)) $root = $this->root;
  180. $response = $this->oauth->fetch($this->api_url . 'fileops/copy', array('from_path' => $from, 'to_path' => $to, 'root' => $root));
  181. return json_decode($response['body'],true);
  182. }
  183. /**
  184. * Creates a new folder
  185. *
  186. * This method returns the information from the newly created directory
  187. *
  188. * @param string $path
  189. * @param string $root Use this to override the default root path (sandbox/dropbox)
  190. * @return stdclass
  191. */
  192. public function createFolder($path, $root = null) {
  193. if (is_null($root)) $root = $this->root;
  194. // Making sure the path starts with a /
  195. $path = '/' . ltrim($path,'/');
  196. $response = $this->oauth->fetch($this->api_url . 'fileops/create_folder', array('path' => $path, 'root' => $root),'POST');
  197. return json_decode($response['body'],true);
  198. }
  199. /**
  200. * Deletes a file or folder.
  201. *
  202. * This method will return the metadata information from the deleted file or folder, if successful.
  203. *
  204. * @param string $path Path to new folder
  205. * @param string $root Use this to override the default root path (sandbox/dropbox)
  206. * @return array
  207. */
  208. public function delete($path, $root = null) {
  209. if (is_null($root)) $root = $this->root;
  210. $response = $this->oauth->fetch($this->api_url . 'fileops/delete', array('path' => $path, 'root' => $root), 'POST');
  211. return json_decode($response['body']);
  212. }
  213. /**
  214. * Moves a file or directory to a new location
  215. *
  216. * This method returns the information from the newly created directory
  217. *
  218. * @param mixed $from Source path
  219. * @param mixed $to destination path
  220. * @param string $root Use this to override the default root path (sandbox/dropbox)
  221. * @return stdclass
  222. */
  223. public function move($from, $to, $root = null) {
  224. if (is_null($root)) $root = $this->root;
  225. $response = $this->oauth->fetch($this->api_url . 'fileops/move', array('from_path' => rawurldecode($from), 'to_path' => rawurldecode($to), 'root' => $root));
  226. return json_decode($response['body'],true);
  227. }
  228. /**
  229. * Returns a list of links for a directory
  230. *
  231. * The links can be used to securely open files throug a browser. The links are cookie protected
  232. * so a user is asked to login if there's no valid session cookie.
  233. *
  234. * @param string $path Path to directory or file
  235. * @param string $root Use this to override the default root path (sandbox/dropbox)
  236. * @deprecated This method is no longer supported
  237. * @return array
  238. */
  239. public function getLinks($path, $root = null) {
  240. throw new Dropbox_Exception('This API method is currently broken, and dropbox documentation about this is no longer online. Please ask Dropbox support if you really need this.');
  241. /*
  242. if (is_null($root)) $root = $this->root;
  243. $response = $this->oauth->fetch($this->api_url . 'links/' . $root . '/' . ltrim($path,'/'));
  244. return json_decode($response,true);
  245. */
  246. }
  247. /**
  248. * Returns file and directory information
  249. *
  250. * @param string $path Path to receive information from
  251. * @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.
  252. * @param string $hash If a hash is supplied, this method simply returns true if nothing has changed since the last request. Good for caching.
  253. * @param int $fileLimit Maximum number of file-information to receive
  254. * @param string $root Use this to override the default root path (sandbox/dropbox)
  255. * @return array|true
  256. */
  257. public function getMetaData($path, $list = true, $hash = null, $fileLimit = null, $root = null) {
  258. if (is_null($root)) $root = $this->root;
  259. $args = array(
  260. 'list' => $list,
  261. );
  262. if (!is_null($hash)) $args['hash'] = $hash;
  263. if (!is_null($fileLimit)) $args['file_limit'] = $fileLimit;
  264. $path = str_replace(array('%2F','~'), array('/','%7E'), rawurlencode($path));
  265. $response = $this->oauth->fetch($this->api_url . 'metadata/' . $root . '/' . ltrim($path,'/'), $args);
  266. /* 304 is not modified */
  267. if ($response['httpStatus']==304) {
  268. return true;
  269. } else {
  270. return json_decode($response['body'],true);
  271. }
  272. }
  273. /**
  274. * Returns a thumbnail (as a string) for a file path.
  275. *
  276. * @param string $path Path to file
  277. * @param string $size small, medium or large
  278. * @param string $root Use this to override the default root path (sandbox/dropbox)
  279. * @return string
  280. */
  281. public function getThumbnail($path, $size = 'small', $root = null) {
  282. if (is_null($root)) $root = $this->root;
  283. $response = $this->oauth->fetch($this->api_content_url . 'thumbnails/' . $root . '/' . ltrim($path,'/'),array('size' => $size));
  284. return $response['body'];
  285. }
  286. /**
  287. * This method is used to generate multipart POST requests for file upload
  288. *
  289. * @param string $uri
  290. * @param array $arguments
  291. * @return bool
  292. */
  293. protected function multipartFetch($uri, $file, $filename) {
  294. /* random string */
  295. $boundary = 'R50hrfBj5JYyfR3vF3wR96GPCC9Fd2q2pVMERvEaOE3D8LZTgLLbRpNwXek3';
  296. $headers = array(
  297. 'Content-Type' => 'multipart/form-data; boundary=' . $boundary,
  298. );
  299. $body="--" . $boundary . "\r\n";
  300. $body.="Content-Disposition: form-data; name=file; filename=".rawurldecode($filename)."\r\n";
  301. $body.="Content-type: application/octet-stream\r\n";
  302. $body.="\r\n";
  303. $body.=stream_get_contents($file);
  304. $body.="\r\n";
  305. $body.="--" . $boundary . "--";
  306. // Dropbox requires the filename to also be part of the regular arguments, so it becomes
  307. // part of the signature.
  308. $uri.='?file=' . $filename;
  309. return $this->oauth->fetch($uri, $body, 'POST', $headers);
  310. }
  311. }