common.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. <?php
  2. /**
  3. * Copyright (c) 2012 Robin Appelman <icewind@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace OC\Files\Storage;
  9. /**
  10. * Storage backend class for providing common filesystem operation methods
  11. * which are not storage-backend specific.
  12. *
  13. * \OC\Files\Storage\Common is never used directly; it is extended by all other
  14. * storage backends, where its methods may be overridden, and additional
  15. * (backend-specific) methods are defined.
  16. *
  17. * Some \OC\Files\Storage\Common methods call functions which are first defined
  18. * in classes which extend it, e.g. $this->stat() .
  19. */
  20. abstract class Common implements \OC\Files\Storage\Storage {
  21. private $cache;
  22. private $scanner;
  23. private $permissioncache;
  24. private $watcher;
  25. private $storageCache;
  26. public function __construct($parameters) {
  27. }
  28. public function is_dir($path) {
  29. return $this->filetype($path) == 'dir';
  30. }
  31. public function is_file($path) {
  32. return $this->filetype($path) == 'file';
  33. }
  34. public function filesize($path) {
  35. if ($this->is_dir($path)) {
  36. return 0; //by definition
  37. } else {
  38. $stat = $this->stat($path);
  39. if (isset($stat['size'])) {
  40. return $stat['size'];
  41. } else {
  42. return 0;
  43. }
  44. }
  45. }
  46. public function isCreatable($path) {
  47. if ($this->is_dir($path) && $this->isUpdatable($path)) {
  48. return true;
  49. }
  50. return false;
  51. }
  52. public function isDeletable($path) {
  53. return $this->isUpdatable($path);
  54. }
  55. public function isSharable($path) {
  56. return $this->isReadable($path);
  57. }
  58. public function getPermissions($path) {
  59. $permissions = 0;
  60. if ($this->isCreatable($path)) {
  61. $permissions |= \OCP\PERMISSION_CREATE;
  62. }
  63. if ($this->isReadable($path)) {
  64. $permissions |= \OCP\PERMISSION_READ;
  65. }
  66. if ($this->isUpdatable($path)) {
  67. $permissions |= \OCP\PERMISSION_UPDATE;
  68. }
  69. if ($this->isDeletable($path)) {
  70. $permissions |= \OCP\PERMISSION_DELETE;
  71. }
  72. if ($this->isSharable($path)) {
  73. $permissions |= \OCP\PERMISSION_SHARE;
  74. }
  75. return $permissions;
  76. }
  77. public function filemtime($path) {
  78. $stat = $this->stat($path);
  79. if (isset($stat['mtime'])) {
  80. return $stat['mtime'];
  81. } else {
  82. return 0;
  83. }
  84. }
  85. public function file_get_contents($path) {
  86. $handle = $this->fopen($path, "r");
  87. if (!$handle) {
  88. return false;
  89. }
  90. $size = $this->filesize($path);
  91. if ($size == 0) {
  92. return '';
  93. }
  94. return fread($handle, $size);
  95. }
  96. public function file_put_contents($path, $data) {
  97. $handle = $this->fopen($path, "w");
  98. return fwrite($handle, $data);
  99. }
  100. public function rename($path1, $path2) {
  101. if ($this->copy($path1, $path2)) {
  102. return $this->unlink($path1);
  103. } else {
  104. return false;
  105. }
  106. }
  107. public function copy($path1, $path2) {
  108. $source = $this->fopen($path1, 'r');
  109. $target = $this->fopen($path2, 'w');
  110. list($count, $result) = \OC_Helper::streamCopy($source, $target);
  111. return $result;
  112. }
  113. /**
  114. * @brief Deletes all files and folders recursively within a directory
  115. * @param string $directory The directory whose contents will be deleted
  116. * @param bool $empty Flag indicating whether directory will be emptied
  117. * @returns bool
  118. *
  119. * @note By default the directory specified by $directory will be
  120. * deleted together with its contents. To avoid this set $empty to true
  121. */
  122. public function deleteAll($directory, $empty = false) {
  123. $directory = trim($directory, '/');
  124. if (!$this->is_dir($directory) || !$this->isReadable($directory)) {
  125. return false;
  126. } else {
  127. $directoryHandle = $this->opendir($directory);
  128. if(is_resource($directoryHandle)) {
  129. while (($contents = readdir($directoryHandle)) !== false) {
  130. if (!\OC\Files\Filesystem::isIgnoredDir($contents)) {
  131. $path = $directory . '/' . $contents;
  132. if ($this->is_dir($path)) {
  133. $this->deleteAll($path);
  134. } else {
  135. $this->unlink($path);
  136. }
  137. }
  138. }
  139. }
  140. if ($empty === false) {
  141. if (!$this->rmdir($directory)) {
  142. return false;
  143. }
  144. }
  145. return true;
  146. }
  147. }
  148. public function getMimeType($path) {
  149. if (!$this->file_exists($path)) {
  150. return false;
  151. }
  152. if ($this->is_dir($path)) {
  153. return 'httpd/unix-directory';
  154. }
  155. $source = $this->fopen($path, 'r');
  156. if (!$source) {
  157. return false;
  158. }
  159. $head = fread($source, 8192); //8kb should suffice to determine a mimetype
  160. if ($pos = strrpos($path, '.')) {
  161. $extension = substr($path, $pos);
  162. } else {
  163. $extension = '';
  164. }
  165. $tmpFile = \OC_Helper::tmpFile($extension);
  166. file_put_contents($tmpFile, $head);
  167. $mime = \OC_Helper::getMimeType($tmpFile);
  168. unlink($tmpFile);
  169. return $mime;
  170. }
  171. public function hash($type, $path, $raw = false) {
  172. $tmpFile = $this->getLocalFile($path);
  173. $hash = hash($type, $tmpFile, $raw);
  174. unlink($tmpFile);
  175. return $hash;
  176. }
  177. public function search($query) {
  178. return $this->searchInDir($query);
  179. }
  180. public function getLocalFile($path) {
  181. return $this->toTmpFile($path);
  182. }
  183. private function toTmpFile($path) { //no longer in the storage api, still useful here
  184. $source = $this->fopen($path, 'r');
  185. if (!$source) {
  186. return false;
  187. }
  188. if ($pos = strrpos($path, '.')) {
  189. $extension = substr($path, $pos);
  190. } else {
  191. $extension = '';
  192. }
  193. $tmpFile = \OC_Helper::tmpFile($extension);
  194. $target = fopen($tmpFile, 'w');
  195. \OC_Helper::streamCopy($source, $target);
  196. return $tmpFile;
  197. }
  198. public function getLocalFolder($path) {
  199. $baseDir = \OC_Helper::tmpFolder();
  200. $this->addLocalFolder($path, $baseDir);
  201. return $baseDir;
  202. }
  203. private function addLocalFolder($path, $target) {
  204. $dh = $this->opendir($path);
  205. if(is_resource($dh)) {
  206. while (($file = readdir($dh)) !== false) {
  207. if ($file !== '.' and $file !== '..') {
  208. if ($this->is_dir($path . '/' . $file)) {
  209. mkdir($target . '/' . $file);
  210. $this->addLocalFolder($path . '/' . $file, $target . '/' . $file);
  211. } else {
  212. $tmp = $this->toTmpFile($path . '/' . $file);
  213. rename($tmp, $target . '/' . $file);
  214. }
  215. }
  216. }
  217. }
  218. }
  219. protected function searchInDir($query, $dir = '') {
  220. $files = array();
  221. $dh = $this->opendir($dir);
  222. if (is_resource($dh)) {
  223. while (($item = readdir($dh)) !== false) {
  224. if ($item == '.' || $item == '..') continue;
  225. if (strstr(strtolower($item), strtolower($query)) !== false) {
  226. $files[] = $dir . '/' . $item;
  227. }
  228. if ($this->is_dir($dir . '/' . $item)) {
  229. $files = array_merge($files, $this->searchInDir($query, $dir . '/' . $item));
  230. }
  231. }
  232. }
  233. return $files;
  234. }
  235. /**
  236. * check if a file or folder has been updated since $time
  237. *
  238. * @param string $path
  239. * @param int $time
  240. * @return bool
  241. */
  242. public function hasUpdated($path, $time) {
  243. return $this->filemtime($path) > $time;
  244. }
  245. public function getCache($path = '') {
  246. if (!isset($this->cache)) {
  247. $this->cache = new \OC\Files\Cache\Cache($this);
  248. }
  249. return $this->cache;
  250. }
  251. public function getScanner($path = '') {
  252. if (!isset($this->scanner)) {
  253. $this->scanner = new \OC\Files\Cache\Scanner($this);
  254. }
  255. return $this->scanner;
  256. }
  257. public function getPermissionsCache($path = '') {
  258. if (!isset($this->permissioncache)) {
  259. $this->permissioncache = new \OC\Files\Cache\Permissions($this);
  260. }
  261. return $this->permissioncache;
  262. }
  263. public function getWatcher($path = '') {
  264. if (!isset($this->watcher)) {
  265. $this->watcher = new \OC\Files\Cache\Watcher($this);
  266. }
  267. return $this->watcher;
  268. }
  269. public function getStorageCache(){
  270. if (!isset($this->storageCache)) {
  271. $this->storageCache = new \OC\Files\Cache\Storage($this);
  272. }
  273. return $this->storageCache;
  274. }
  275. /**
  276. * get the owner of a path
  277. *
  278. * @param string $path The path to get the owner
  279. * @return string uid or false
  280. */
  281. public function getOwner($path) {
  282. return \OC_User::getUser();
  283. }
  284. /**
  285. * get the ETag for a file or folder
  286. *
  287. * @param string $path
  288. * @return string
  289. */
  290. public function getETag($path) {
  291. $ETagFunction = \OC_Connector_Sabre_Node::$ETagFunction;
  292. if ($ETagFunction) {
  293. $hash = call_user_func($ETagFunction, $path);
  294. return $hash;
  295. } else {
  296. return uniqid();
  297. }
  298. }
  299. /**
  300. * clean a path, i.e. remove all redundant '.' and '..'
  301. * making sure that it can't point to higher than '/'
  302. *
  303. * @param $path The path to clean
  304. * @return string cleaned path
  305. */
  306. public function cleanPath($path) {
  307. if (strlen($path) == 0 or $path[0] != '/') {
  308. $path = '/' . $path;
  309. }
  310. $output = array();
  311. foreach (explode('/', $path) as $chunk) {
  312. if ($chunk == '..') {
  313. array_pop($output);
  314. } else if ($chunk == '.') {
  315. } else {
  316. $output[] = $chunk;
  317. }
  318. }
  319. return implode('/', $output);
  320. }
  321. public function test() {
  322. if ($this->stat('')) {
  323. return true;
  324. }
  325. return false;
  326. }
  327. /**
  328. * get the free space in the storage
  329. *
  330. * @param $path
  331. * @return int
  332. */
  333. public function free_space($path) {
  334. return \OC\Files\SPACE_UNKNOWN;
  335. }
  336. }