You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

dropbox.php 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. <?php
  2. /**
  3. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  4. * @author Michael Gapczynski <GapczynskiM@gmail.com>
  5. * @author Morris Jobke <hey@morrisjobke.de>
  6. * @author Philipp Kapfer <philipp.kapfer@gmx.at>
  7. * @author Robin Appelman <icewind@owncloud.com>
  8. * @author Robin McCorkell <rmccorkell@karoshi.org.uk>
  9. * @author Sascha Schmidt <realriot@realriot.de>
  10. * @author Thomas Müller <thomas.mueller@tmit.eu>
  11. * @author Vincent Petry <pvince81@owncloud.com>
  12. *
  13. * @copyright Copyright (c) 2015, ownCloud, Inc.
  14. * @license AGPL-3.0
  15. *
  16. * This code is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License, version 3,
  18. * as published by the Free Software Foundation.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License, version 3,
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>
  27. *
  28. */
  29. namespace OC\Files\Storage;
  30. require_once __DIR__ . '/../3rdparty/Dropbox/autoload.php';
  31. class Dropbox extends \OC\Files\Storage\Common {
  32. private $dropbox;
  33. private $root;
  34. private $id;
  35. private $metaData = array();
  36. private static $tempFiles = array();
  37. public function __construct($params) {
  38. if (isset($params['configured']) && $params['configured'] == 'true'
  39. && isset($params['app_key'])
  40. && isset($params['app_secret'])
  41. && isset($params['token'])
  42. && isset($params['token_secret'])
  43. ) {
  44. $this->root = isset($params['root']) ? $params['root'] : '';
  45. $this->id = 'dropbox::'.$params['app_key'] . $params['token']. '/' . $this->root;
  46. $oauth = new \Dropbox_OAuth_Curl($params['app_key'], $params['app_secret']);
  47. $oauth->setToken($params['token'], $params['token_secret']);
  48. // note: Dropbox_API connection is lazy
  49. $this->dropbox = new \Dropbox_API($oauth, 'auto');
  50. } else {
  51. throw new \Exception('Creating \OC\Files\Storage\Dropbox storage failed');
  52. }
  53. }
  54. /**
  55. * @param string $path
  56. */
  57. private function deleteMetaData($path) {
  58. $path = $this->root.$path;
  59. if (isset($this->metaData[$path])) {
  60. unset($this->metaData[$path]);
  61. return true;
  62. }
  63. return false;
  64. }
  65. /**
  66. * Returns the path's metadata
  67. * @param string $path path for which to return the metadata
  68. * @param bool $list if true, also return the directory's contents
  69. * @return mixed directory contents if $list is true, file metadata if $list is
  70. * false, null if the file doesn't exist or "false" if the operation failed
  71. */
  72. private function getMetaData($path, $list = false) {
  73. $path = $this->root.$path;
  74. if ( ! $list && isset($this->metaData[$path])) {
  75. return $this->metaData[$path];
  76. } else {
  77. if ($list) {
  78. try {
  79. $response = $this->dropbox->getMetaData($path);
  80. } catch (\Exception $exception) {
  81. \OCP\Util::writeLog('files_external', $exception->getMessage(), \OCP\Util::ERROR);
  82. return false;
  83. }
  84. $contents = array();
  85. if ($response && isset($response['contents'])) {
  86. // Cache folder's contents
  87. foreach ($response['contents'] as $file) {
  88. if (!isset($file['is_deleted']) || !$file['is_deleted']) {
  89. $this->metaData[$path.'/'.basename($file['path'])] = $file;
  90. $contents[] = $file;
  91. }
  92. }
  93. unset($response['contents']);
  94. }
  95. if (!isset($response['is_deleted']) || !$response['is_deleted']) {
  96. $this->metaData[$path] = $response;
  97. }
  98. // Return contents of folder only
  99. return $contents;
  100. } else {
  101. try {
  102. $requestPath = $path;
  103. if ($path === '.') {
  104. $requestPath = '';
  105. }
  106. $response = $this->dropbox->getMetaData($requestPath, 'false');
  107. if (!isset($response['is_deleted']) || !$response['is_deleted']) {
  108. $this->metaData[$path] = $response;
  109. return $response;
  110. }
  111. return null;
  112. } catch (\Exception $exception) {
  113. if ($exception instanceof \Dropbox_Exception_NotFound) {
  114. // don't log, might be a file_exist check
  115. return false;
  116. }
  117. \OCP\Util::writeLog('files_external', $exception->getMessage(), \OCP\Util::ERROR);
  118. return false;
  119. }
  120. }
  121. }
  122. }
  123. public function getId(){
  124. return $this->id;
  125. }
  126. public function mkdir($path) {
  127. $path = $this->root.$path;
  128. try {
  129. $this->dropbox->createFolder($path);
  130. return true;
  131. } catch (\Exception $exception) {
  132. \OCP\Util::writeLog('files_external', $exception->getMessage(), \OCP\Util::ERROR);
  133. return false;
  134. }
  135. }
  136. public function rmdir($path) {
  137. return $this->unlink($path);
  138. }
  139. public function opendir($path) {
  140. $contents = $this->getMetaData($path, true);
  141. if ($contents !== false) {
  142. $files = array();
  143. foreach ($contents as $file) {
  144. $files[] = basename($file['path']);
  145. }
  146. \OC\Files\Stream\Dir::register('dropbox'.$path, $files);
  147. return opendir('fakedir://dropbox'.$path);
  148. }
  149. return false;
  150. }
  151. public function stat($path) {
  152. $metaData = $this->getMetaData($path);
  153. if ($metaData) {
  154. $stat['size'] = $metaData['bytes'];
  155. $stat['atime'] = time();
  156. $stat['mtime'] = (isset($metaData['modified'])) ? strtotime($metaData['modified']) : time();
  157. return $stat;
  158. }
  159. return false;
  160. }
  161. public function filetype($path) {
  162. if ($path == '' || $path == '/') {
  163. return 'dir';
  164. } else {
  165. $metaData = $this->getMetaData($path);
  166. if ($metaData) {
  167. if ($metaData['is_dir'] == 'true') {
  168. return 'dir';
  169. } else {
  170. return 'file';
  171. }
  172. }
  173. }
  174. return false;
  175. }
  176. public function file_exists($path) {
  177. if ($path == '' || $path == '/') {
  178. return true;
  179. }
  180. if ($this->getMetaData($path)) {
  181. return true;
  182. }
  183. return false;
  184. }
  185. public function unlink($path) {
  186. try {
  187. $this->dropbox->delete($this->root.$path);
  188. $this->deleteMetaData($path);
  189. return true;
  190. } catch (\Exception $exception) {
  191. \OCP\Util::writeLog('files_external', $exception->getMessage(), \OCP\Util::ERROR);
  192. return false;
  193. }
  194. }
  195. public function rename($path1, $path2) {
  196. try {
  197. // overwrite if target file exists and is not a directory
  198. $destMetaData = $this->getMetaData($path2);
  199. if (isset($destMetaData) && $destMetaData !== false && !$destMetaData['is_dir']) {
  200. $this->unlink($path2);
  201. }
  202. $this->dropbox->move($this->root.$path1, $this->root.$path2);
  203. $this->deleteMetaData($path1);
  204. return true;
  205. } catch (\Exception $exception) {
  206. \OCP\Util::writeLog('files_external', $exception->getMessage(), \OCP\Util::ERROR);
  207. return false;
  208. }
  209. }
  210. public function copy($path1, $path2) {
  211. $path1 = $this->root.$path1;
  212. $path2 = $this->root.$path2;
  213. try {
  214. $this->dropbox->copy($path1, $path2);
  215. return true;
  216. } catch (\Exception $exception) {
  217. \OCP\Util::writeLog('files_external', $exception->getMessage(), \OCP\Util::ERROR);
  218. return false;
  219. }
  220. }
  221. public function fopen($path, $mode) {
  222. $path = $this->root.$path;
  223. switch ($mode) {
  224. case 'r':
  225. case 'rb':
  226. $tmpFile = \OC_Helper::tmpFile();
  227. try {
  228. $data = $this->dropbox->getFile($path);
  229. file_put_contents($tmpFile, $data);
  230. return fopen($tmpFile, 'r');
  231. } catch (\Exception $exception) {
  232. \OCP\Util::writeLog('files_external', $exception->getMessage(), \OCP\Util::ERROR);
  233. return false;
  234. }
  235. case 'w':
  236. case 'wb':
  237. case 'a':
  238. case 'ab':
  239. case 'r+':
  240. case 'w+':
  241. case 'wb+':
  242. case 'a+':
  243. case 'x':
  244. case 'x+':
  245. case 'c':
  246. case 'c+':
  247. if (strrpos($path, '.') !== false) {
  248. $ext = substr($path, strrpos($path, '.'));
  249. } else {
  250. $ext = '';
  251. }
  252. $tmpFile = \OC_Helper::tmpFile($ext);
  253. \OC\Files\Stream\Close::registerCallback($tmpFile, array($this, 'writeBack'));
  254. if ($this->file_exists($path)) {
  255. $source = $this->fopen($path, 'r');
  256. file_put_contents($tmpFile, $source);
  257. }
  258. self::$tempFiles[$tmpFile] = $path;
  259. return fopen('close://'.$tmpFile, $mode);
  260. }
  261. return false;
  262. }
  263. public function writeBack($tmpFile) {
  264. if (isset(self::$tempFiles[$tmpFile])) {
  265. $handle = fopen($tmpFile, 'r');
  266. try {
  267. $this->dropbox->putFile(self::$tempFiles[$tmpFile], $handle);
  268. unlink($tmpFile);
  269. } catch (\Exception $exception) {
  270. \OCP\Util::writeLog('files_external', $exception->getMessage(), \OCP\Util::ERROR);
  271. }
  272. }
  273. }
  274. public function getMimeType($path) {
  275. if ($this->filetype($path) == 'dir') {
  276. return 'httpd/unix-directory';
  277. } else {
  278. $metaData = $this->getMetaData($path);
  279. if ($metaData) {
  280. return $metaData['mime_type'];
  281. }
  282. }
  283. return false;
  284. }
  285. public function free_space($path) {
  286. try {
  287. $info = $this->dropbox->getAccountInfo();
  288. return $info['quota_info']['quota'] - $info['quota_info']['normal'];
  289. } catch (\Exception $exception) {
  290. \OCP\Util::writeLog('files_external', $exception->getMessage(), \OCP\Util::ERROR);
  291. return false;
  292. }
  293. }
  294. public function touch($path, $mtime = null) {
  295. if ($this->file_exists($path)) {
  296. return false;
  297. } else {
  298. $this->file_put_contents($path, '');
  299. }
  300. return true;
  301. }
  302. /**
  303. * check if curl is installed
  304. */
  305. public static function checkDependencies() {
  306. return true;
  307. }
  308. }