dropbox.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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 <robin@mccorkell.me.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) 2016, 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. use Icewind\Streams\IteratorDirectory;
  31. require_once __DIR__ . '/../3rdparty/Dropbox/autoload.php';
  32. class Dropbox extends \OC\Files\Storage\Common {
  33. private $dropbox;
  34. private $root;
  35. private $id;
  36. private $metaData = array();
  37. private static $tempFiles = array();
  38. public function __construct($params) {
  39. if (isset($params['configured']) && $params['configured'] == 'true'
  40. && isset($params['app_key'])
  41. && isset($params['app_secret'])
  42. && isset($params['token'])
  43. && isset($params['token_secret'])
  44. ) {
  45. $this->root = isset($params['root']) ? $params['root'] : '';
  46. $this->id = 'dropbox::'.$params['app_key'] . $params['token']. '/' . $this->root;
  47. $oauth = new \Dropbox_OAuth_Curl($params['app_key'], $params['app_secret']);
  48. $oauth->setToken($params['token'], $params['token_secret']);
  49. // note: Dropbox_API connection is lazy
  50. $this->dropbox = new \Dropbox_API($oauth, 'auto');
  51. } else {
  52. throw new \Exception('Creating \OC\Files\Storage\Dropbox storage failed');
  53. }
  54. }
  55. /**
  56. * @param string $path
  57. */
  58. private function deleteMetaData($path) {
  59. $path = ltrim($this->root.$path, '/');
  60. if (isset($this->metaData[$path])) {
  61. unset($this->metaData[$path]);
  62. return true;
  63. }
  64. return false;
  65. }
  66. private function setMetaData($path, $metaData) {
  67. $this->metaData[ltrim($path, '/')] = $metaData;
  68. }
  69. /**
  70. * Returns the path's metadata
  71. * @param string $path path for which to return the metadata
  72. * @param bool $list if true, also return the directory's contents
  73. * @return mixed directory contents if $list is true, file metadata if $list is
  74. * false, null if the file doesn't exist or "false" if the operation failed
  75. */
  76. private function getDropBoxMetaData($path, $list = false) {
  77. $path = ltrim($this->root.$path, '/');
  78. if ( ! $list && isset($this->metaData[$path])) {
  79. return $this->metaData[$path];
  80. } else {
  81. if ($list) {
  82. try {
  83. $response = $this->dropbox->getMetaData($path);
  84. } catch (\Exception $exception) {
  85. \OCP\Util::writeLog('files_external', $exception->getMessage(), \OCP\Util::ERROR);
  86. return false;
  87. }
  88. $contents = array();
  89. if ($response && isset($response['contents'])) {
  90. // Cache folder's contents
  91. foreach ($response['contents'] as $file) {
  92. if (!isset($file['is_deleted']) || !$file['is_deleted']) {
  93. $this->setMetaData($path.'/'.basename($file['path']), $file);
  94. $contents[] = $file;
  95. }
  96. }
  97. unset($response['contents']);
  98. }
  99. if (!isset($response['is_deleted']) || !$response['is_deleted']) {
  100. $this->setMetaData($path, $response);
  101. }
  102. // Return contents of folder only
  103. return $contents;
  104. } else {
  105. try {
  106. $requestPath = $path;
  107. if ($path === '.') {
  108. $requestPath = '';
  109. }
  110. $response = $this->dropbox->getMetaData($requestPath, 'false');
  111. if (!isset($response['is_deleted']) || !$response['is_deleted']) {
  112. $this->setMetaData($path, $response);
  113. return $response;
  114. }
  115. return null;
  116. } catch (\Exception $exception) {
  117. if ($exception instanceof \Dropbox_Exception_NotFound) {
  118. // don't log, might be a file_exist check
  119. return false;
  120. }
  121. \OCP\Util::writeLog('files_external', $exception->getMessage(), \OCP\Util::ERROR);
  122. return false;
  123. }
  124. }
  125. }
  126. }
  127. public function getId(){
  128. return $this->id;
  129. }
  130. public function mkdir($path) {
  131. $path = $this->root.$path;
  132. try {
  133. $this->dropbox->createFolder($path);
  134. return true;
  135. } catch (\Exception $exception) {
  136. \OCP\Util::writeLog('files_external', $exception->getMessage(), \OCP\Util::ERROR);
  137. return false;
  138. }
  139. }
  140. public function rmdir($path) {
  141. return $this->unlink($path);
  142. }
  143. public function opendir($path) {
  144. $contents = $this->getDropBoxMetaData($path, true);
  145. if ($contents !== false) {
  146. $files = array();
  147. foreach ($contents as $file) {
  148. $files[] = basename($file['path']);
  149. }
  150. return IteratorDirectory::wrap($files);
  151. }
  152. return false;
  153. }
  154. public function stat($path) {
  155. $metaData = $this->getDropBoxMetaData($path);
  156. if ($metaData) {
  157. $stat['size'] = $metaData['bytes'];
  158. $stat['atime'] = time();
  159. $stat['mtime'] = (isset($metaData['modified'])) ? strtotime($metaData['modified']) : time();
  160. return $stat;
  161. }
  162. return false;
  163. }
  164. public function filetype($path) {
  165. if ($path == '' || $path == '/') {
  166. return 'dir';
  167. } else {
  168. $metaData = $this->getDropBoxMetaData($path);
  169. if ($metaData) {
  170. if ($metaData['is_dir'] == 'true') {
  171. return 'dir';
  172. } else {
  173. return 'file';
  174. }
  175. }
  176. }
  177. return false;
  178. }
  179. public function file_exists($path) {
  180. if ($path == '' || $path == '/') {
  181. return true;
  182. }
  183. if ($this->getDropBoxMetaData($path)) {
  184. return true;
  185. }
  186. return false;
  187. }
  188. public function unlink($path) {
  189. try {
  190. $this->dropbox->delete($this->root.$path);
  191. $this->deleteMetaData($path);
  192. return true;
  193. } catch (\Exception $exception) {
  194. \OCP\Util::writeLog('files_external', $exception->getMessage(), \OCP\Util::ERROR);
  195. return false;
  196. }
  197. }
  198. public function rename($path1, $path2) {
  199. try {
  200. // overwrite if target file exists and is not a directory
  201. $destMetaData = $this->getDropBoxMetaData($path2);
  202. if (isset($destMetaData) && $destMetaData !== false && !$destMetaData['is_dir']) {
  203. $this->unlink($path2);
  204. }
  205. $this->dropbox->move($this->root.$path1, $this->root.$path2);
  206. $this->deleteMetaData($path1);
  207. return true;
  208. } catch (\Exception $exception) {
  209. \OCP\Util::writeLog('files_external', $exception->getMessage(), \OCP\Util::ERROR);
  210. return false;
  211. }
  212. }
  213. public function copy($path1, $path2) {
  214. $path1 = $this->root.$path1;
  215. $path2 = $this->root.$path2;
  216. try {
  217. $this->dropbox->copy($path1, $path2);
  218. return true;
  219. } catch (\Exception $exception) {
  220. \OCP\Util::writeLog('files_external', $exception->getMessage(), \OCP\Util::ERROR);
  221. return false;
  222. }
  223. }
  224. public function fopen($path, $mode) {
  225. $path = $this->root.$path;
  226. switch ($mode) {
  227. case 'r':
  228. case 'rb':
  229. $tmpFile = \OCP\Files::tmpFile();
  230. try {
  231. $data = $this->dropbox->getFile($path);
  232. file_put_contents($tmpFile, $data);
  233. return fopen($tmpFile, 'r');
  234. } catch (\Exception $exception) {
  235. \OCP\Util::writeLog('files_external', $exception->getMessage(), \OCP\Util::ERROR);
  236. return false;
  237. }
  238. case 'w':
  239. case 'wb':
  240. case 'a':
  241. case 'ab':
  242. case 'r+':
  243. case 'w+':
  244. case 'wb+':
  245. case 'a+':
  246. case 'x':
  247. case 'x+':
  248. case 'c':
  249. case 'c+':
  250. if (strrpos($path, '.') !== false) {
  251. $ext = substr($path, strrpos($path, '.'));
  252. } else {
  253. $ext = '';
  254. }
  255. $tmpFile = \OCP\Files::tmpFile($ext);
  256. \OC\Files\Stream\Close::registerCallback($tmpFile, array($this, 'writeBack'));
  257. if ($this->file_exists($path)) {
  258. $source = $this->fopen($path, 'r');
  259. file_put_contents($tmpFile, $source);
  260. }
  261. self::$tempFiles[$tmpFile] = $path;
  262. return fopen('close://'.$tmpFile, $mode);
  263. }
  264. return false;
  265. }
  266. public function writeBack($tmpFile) {
  267. if (isset(self::$tempFiles[$tmpFile])) {
  268. $handle = fopen($tmpFile, 'r');
  269. try {
  270. $this->dropbox->putFile(self::$tempFiles[$tmpFile], $handle);
  271. unlink($tmpFile);
  272. $this->deleteMetaData(self::$tempFiles[$tmpFile]);
  273. } catch (\Exception $exception) {
  274. \OCP\Util::writeLog('files_external', $exception->getMessage(), \OCP\Util::ERROR);
  275. }
  276. }
  277. }
  278. public function free_space($path) {
  279. try {
  280. $info = $this->dropbox->getAccountInfo();
  281. return $info['quota_info']['quota'] - $info['quota_info']['normal'];
  282. } catch (\Exception $exception) {
  283. \OCP\Util::writeLog('files_external', $exception->getMessage(), \OCP\Util::ERROR);
  284. return false;
  285. }
  286. }
  287. public function touch($path, $mtime = null) {
  288. if ($this->file_exists($path)) {
  289. return false;
  290. } else {
  291. $this->file_put_contents($path, '');
  292. }
  293. return true;
  294. }
  295. /**
  296. * check if curl is installed
  297. */
  298. public static function checkDependencies() {
  299. return true;
  300. }
  301. }