PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/editor/htmlarea/custom_plugins/dropbox/Dropbox/API.php

https://github.com/nadavkav/RTL-BIDI-Hebrew-Moodle-Plugins
PHP | 340 lines | 111 code | 72 blank | 157 comment | 14 complexity | da28a0d8c9bf39373b2e2ea6ecf35c89 MD5 | raw file
Possible License(s): Apache-2.0, GPL-3.0
  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. * OAuth object
  21. *
  22. * @var Dropbox_OAuth
  23. */
  24. protected $oauth;
  25. /**
  26. * Default root-path, this will most likely be 'sandbox' or 'dropbox'
  27. *
  28. * @var string
  29. */
  30. protected $root;
  31. /**
  32. * Constructor
  33. *
  34. * @param Dropbox_OAuth Dropbox_Auth object
  35. * @param string $root default root path (sandbox or dropbox)
  36. */
  37. public function __construct(Dropbox_OAuth $oauth, $root = self::ROOT_DROPBOX) {
  38. $this->oauth = $oauth;
  39. $this->root = $root;
  40. }
  41. /**
  42. * Returns OAuth tokens based on an email address and passwords
  43. *
  44. * This can be used to bypass the regular oauth workflow.
  45. *
  46. * This method returns an array with 2 elements:
  47. * * token
  48. * * secret
  49. *
  50. * @param string $email
  51. * @param string $password
  52. * @return array
  53. */
  54. public function getToken($email, $password) {
  55. $data = $this->oauth->fetch('http://api.dropbox.com/0/token', array(
  56. 'email' => $email,
  57. 'password' => $password
  58. ),'POST');
  59. $data = json_decode($data['body']);
  60. return array(
  61. 'token' => $data->token,
  62. 'token_secret' => $data->secret,
  63. );
  64. }
  65. /**
  66. * Returns information about the current dropbox account
  67. *
  68. * @return stdclass
  69. */
  70. public function getAccountInfo() {
  71. $data = $this->oauth->fetch('http://api.dropbox.com/0/account/info');
  72. return json_decode($data['body'],true);
  73. }
  74. /**
  75. * Creates a new Dropbox account
  76. *
  77. * @param string $email
  78. * @param string $first_name
  79. * @param string $last_name
  80. * @param string $password
  81. * @return bool
  82. */
  83. public function createAccount($email, $first_name, $last_name, $password) {
  84. $result = $this->oauth->fetch('http://api.dropbox.com/0/account',array(
  85. 'email' => $email,
  86. 'first_name' => $first_name,
  87. 'last_name' => $last_name,
  88. 'password' => $password,
  89. ), 'POST');
  90. return $result['body']==='OK';
  91. }
  92. /**
  93. * Returns a file's contents
  94. *
  95. * @param string $path path
  96. * @param string $root Use this to override the default root path (sandbox/dropbox)
  97. * @return string
  98. */
  99. public function getFile($path = '', $root = null) {
  100. if (is_null($root)) $root = $this->root;
  101. $result = $this->oauth->fetch('http://api-content.dropbox.com/0/files/' . $root . '/' . ltrim($path,'/'));
  102. return $result['body'];
  103. }
  104. /**
  105. * Uploads a new file
  106. *
  107. * @param string $path Target path (including filename)
  108. * @param string $file Either a path to a file or a stream resource
  109. * @param string $root Use this to override the default root path (sandbox/dropbox)
  110. * @return bool
  111. */
  112. public function putFile($path, $file, $root = null) {
  113. $directory = dirname($path);
  114. $filename = basename($path);
  115. if($directory==='.') $directory = '';
  116. if (is_null($root)) $root = $this->root;
  117. if (is_string($file)) {
  118. $file = fopen($file,'r');
  119. } elseif (!is_resource($file)) {
  120. throw new Dropbox_Exception('File must be a file-resource or a string');
  121. }
  122. $this->multipartFetch('http://api-content.dropbox.com/0/files/' . $root . '/' . trim($directory,'/'), $file, $filename);
  123. return true;
  124. }
  125. /**
  126. * Copies a file or directory from one location to another
  127. *
  128. * This method returns the file information of the newly created file.
  129. *
  130. * @param string $from source path
  131. * @param string $to destination path
  132. * @param string $root Use this to override the default root path (sandbox/dropbox)
  133. * @return stdclass
  134. */
  135. public function copy($from, $to, $root = null) {
  136. if (is_null($root)) $root = $this->root;
  137. $response = $this->oauth->fetch('http://api.dropbox.com/0/fileops/copy', array('from_path' => $from, 'to_path' => $to, 'root' => $root));
  138. return json_decode($response['body'],true);
  139. }
  140. /**
  141. * Creates a new folder
  142. *
  143. * This method returns the information from the newly created directory
  144. *
  145. * @param string $path
  146. * @param string $root Use this to override the default root path (sandbox/dropbox)
  147. * @return stdclass
  148. */
  149. public function createFolder($path, $root = null) {
  150. if (is_null($root)) $root = $this->root;
  151. // Making sure the path starts with a /
  152. $path = '/' . ltrim($path,'/');
  153. $response = $this->oauth->fetch('http://api.dropbox.com/0/fileops/create_folder', array('path' => $path, 'root' => $root),'POST');
  154. return json_decode($response['body'],true);
  155. }
  156. /**
  157. * Deletes a file or folder.
  158. *
  159. * This method will return the metadata information from the deleted file or folder, if successful.
  160. *
  161. * @param string $path Path to new folder
  162. * @param string $root Use this to override the default root path (sandbox/dropbox)
  163. * @return array
  164. */
  165. public function delete($path, $root = null) {
  166. if (is_null($root)) $root = $this->root;
  167. $response = $this->oauth->fetch('http://api.dropbox.com/0/fileops/delete', array('path' => $path, 'root' => $root));
  168. return json_decode($response['body']);
  169. }
  170. /**
  171. * Moves a file or directory to a new location
  172. *
  173. * This method returns the information from the newly created directory
  174. *
  175. * @param mixed $from Source path
  176. * @param mixed $to destination path
  177. * @param string $root Use this to override the default root path (sandbox/dropbox)
  178. * @return stdclass
  179. */
  180. public function move($from, $to, $root = null) {
  181. if (is_null($root)) $root = $this->root;
  182. $response = $this->oauth->fetch('http://api.dropbox.com/0/fileops/move', array('from_path' => $from, 'to_path' => $to, 'root' => $root));
  183. return json_decode($response['body'],true);
  184. }
  185. /**
  186. * Returns a list of links for a directory
  187. *
  188. * The links can be used to securely open files throug a browser. The links are cookie protected
  189. * so a user is asked to login if there's no valid session cookie.
  190. *
  191. * @param string $path Path to directory or file
  192. * @param string $root Use this to override the default root path (sandbox/dropbox)
  193. * @deprecated This method is no longer supported
  194. * @return array
  195. */
  196. public function getLinks($path, $root = null) {
  197. 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.');
  198. /*
  199. if (is_null($root)) $root = $this->root;
  200. $response = $this->oauth->fetch('http://api.dropbox.com/0/links/' . $root . '/' . ltrim($path,'/'));
  201. return json_decode($response,true);
  202. */
  203. }
  204. /**
  205. * Returns file and directory information
  206. *
  207. * @param string $path Path to receive information from
  208. * @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.
  209. * @param string $hash If a hash is supplied, this method simply returns true if nothing has changed since the last request. Good for caching.
  210. * @param int $fileLimit Maximum number of file-information to receive
  211. * @param string $root Use this to override the default root path (sandbox/dropbox)
  212. * @return array|true
  213. */
  214. public function getMetaData($path, $list = true, $hash = null, $fileLimit = null, $root = null) {
  215. if (is_null($root)) $root = $this->root;
  216. $args = array(
  217. 'list' => $list,
  218. );
  219. if (!is_null($hash)) $args['hash'] = $hash;
  220. if (!is_null($fileLimit)) $args['file_limit'] = $hash;
  221. $response = $this->oauth->fetch('http://api.dropbox.com/0/metadata/' . $root . '/' . ltrim($path,'/'), $args);
  222. /* 304 is not modified */
  223. if ($response['httpStatus']==304) {
  224. return true;
  225. } else {
  226. return json_decode($response['body'],true);
  227. }
  228. }
  229. /**
  230. * Returns a thumbnail (as a string) for a file path.
  231. *
  232. * @param string $path Path to file
  233. * @param string $size small, medium or large
  234. * @param string $root Use this to override the default root path (sandbox/dropbox)
  235. * @return string
  236. */
  237. public function getThumbnail($path, $size = 'small', $root = null) {
  238. if (is_null($root)) $root = $this->root;
  239. $response = $this->oauth->fetch('http://api-content.dropbox.com/0/thumbnails/' . $root . '/' . ltrim($path,'/'),array('size' => $size));
  240. return $response['body'];
  241. }
  242. /**
  243. * This method is used to generate multipart POST requests for file upload
  244. *
  245. * @param string $uri
  246. * @param array $arguments
  247. * @return bool
  248. */
  249. protected function multipartFetch($uri, $file, $filename) {
  250. /* random string */
  251. $boundary = 'R50hrfBj5JYyfR3vF3wR96GPCC9Fd2q2pVMERvEaOE3D8LZTgLLbRpNwXek3';
  252. $headers = array(
  253. 'Content-Type' => 'multipart/form-data; boundary=' . $boundary,
  254. );
  255. $body="--" . $boundary . "\r\n";
  256. $body.="Content-Disposition: form-data; name=file; filename=".$filename."\r\n";
  257. $body.="Content-type: application/octet-stream\r\n";
  258. $body.="\r\n";
  259. $body.=stream_get_contents($file);
  260. $body.="\r\n";
  261. $body.="--" . $boundary . "--";
  262. // Dropbox requires the filename to also be part of the regular arguments, so it becomes
  263. // part of the signature.
  264. $uri.='?file=' . $filename;
  265. return $this->oauth->fetch($uri, $body, 'POST', $headers);
  266. }
  267. }