PageRenderTime 51ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/files/storage/common.php

https://github.com/sezuan/core
PHP | 371 lines | 275 code | 38 blank | 58 comment | 67 complexity | acec8be243bebe4a1d7b1a9b2a7ed46c MD5 | raw file
Possible License(s): AGPL-3.0, AGPL-1.0, MPL-2.0-no-copyleft-exception
  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. while ($contents = readdir($directoryHandle)) {
  129. if (!\OC\Files\Filesystem::isIgnoredDir($contents)) {
  130. $path = $directory . '/' . $contents;
  131. if ($this->is_dir($path)) {
  132. $this->deleteAll($path);
  133. } else {
  134. $this->unlink($path);
  135. }
  136. }
  137. }
  138. if ($empty === false) {
  139. if (!$this->rmdir($directory)) {
  140. return false;
  141. }
  142. }
  143. return true;
  144. }
  145. }
  146. public function getMimeType($path) {
  147. if (!$this->file_exists($path)) {
  148. return false;
  149. }
  150. if ($this->is_dir($path)) {
  151. return 'httpd/unix-directory';
  152. }
  153. $source = $this->fopen($path, 'r');
  154. if (!$source) {
  155. return false;
  156. }
  157. $head = fread($source, 8192); //8kb should suffice to determine a mimetype
  158. if ($pos = strrpos($path, '.')) {
  159. $extension = substr($path, $pos);
  160. } else {
  161. $extension = '';
  162. }
  163. $tmpFile = \OC_Helper::tmpFile($extension);
  164. file_put_contents($tmpFile, $head);
  165. $mime = \OC_Helper::getMimeType($tmpFile);
  166. unlink($tmpFile);
  167. return $mime;
  168. }
  169. public function hash($type, $path, $raw = false) {
  170. $tmpFile = $this->getLocalFile($path);
  171. $hash = hash($type, $tmpFile, $raw);
  172. unlink($tmpFile);
  173. return $hash;
  174. }
  175. public function search($query) {
  176. return $this->searchInDir($query);
  177. }
  178. public function getLocalFile($path) {
  179. return $this->toTmpFile($path);
  180. }
  181. private function toTmpFile($path) { //no longer in the storage api, still useful here
  182. $source = $this->fopen($path, 'r');
  183. if (!$source) {
  184. return false;
  185. }
  186. if ($pos = strrpos($path, '.')) {
  187. $extension = substr($path, $pos);
  188. } else {
  189. $extension = '';
  190. }
  191. $tmpFile = \OC_Helper::tmpFile($extension);
  192. $target = fopen($tmpFile, 'w');
  193. \OC_Helper::streamCopy($source, $target);
  194. return $tmpFile;
  195. }
  196. public function getLocalFolder($path) {
  197. $baseDir = \OC_Helper::tmpFolder();
  198. $this->addLocalFolder($path, $baseDir);
  199. return $baseDir;
  200. }
  201. private function addLocalFolder($path, $target) {
  202. if ($dh = $this->opendir($path)) {
  203. while ($file = readdir($dh)) {
  204. if ($file !== '.' and $file !== '..') {
  205. if ($this->is_dir($path . '/' . $file)) {
  206. mkdir($target . '/' . $file);
  207. $this->addLocalFolder($path . '/' . $file, $target . '/' . $file);
  208. } else {
  209. $tmp = $this->toTmpFile($path . '/' . $file);
  210. rename($tmp, $target . '/' . $file);
  211. }
  212. }
  213. }
  214. }
  215. }
  216. protected function searchInDir($query, $dir = '') {
  217. $files = array();
  218. $dh = $this->opendir($dir);
  219. if ($dh) {
  220. while ($item = readdir($dh)) {
  221. if ($item == '.' || $item == '..') continue;
  222. if (strstr(strtolower($item), strtolower($query)) !== false) {
  223. $files[] = $dir . '/' . $item;
  224. }
  225. if ($this->is_dir($dir . '/' . $item)) {
  226. $files = array_merge($files, $this->searchInDir($query, $dir . '/' . $item));
  227. }
  228. }
  229. }
  230. return $files;
  231. }
  232. /**
  233. * check if a file or folder has been updated since $time
  234. *
  235. * @param string $path
  236. * @param int $time
  237. * @return bool
  238. */
  239. public function hasUpdated($path, $time) {
  240. return $this->filemtime($path) > $time;
  241. }
  242. public function getCache($path = '') {
  243. if (!isset($this->cache)) {
  244. $this->cache = new \OC\Files\Cache\Cache($this);
  245. }
  246. return $this->cache;
  247. }
  248. public function getScanner($path = '') {
  249. if (!isset($this->scanner)) {
  250. $this->scanner = new \OC\Files\Cache\Scanner($this);
  251. }
  252. return $this->scanner;
  253. }
  254. public function getPermissionsCache($path = '') {
  255. if (!isset($this->permissioncache)) {
  256. $this->permissioncache = new \OC\Files\Cache\Permissions($this);
  257. }
  258. return $this->permissioncache;
  259. }
  260. public function getWatcher($path = '') {
  261. if (!isset($this->watcher)) {
  262. $this->watcher = new \OC\Files\Cache\Watcher($this);
  263. }
  264. return $this->watcher;
  265. }
  266. public function getStorageCache(){
  267. if (!isset($this->storageCache)) {
  268. $this->storageCache = new \OC\Files\Cache\Storage($this);
  269. }
  270. return $this->storageCache;
  271. }
  272. /**
  273. * get the owner of a path
  274. *
  275. * @param string $path The path to get the owner
  276. * @return string uid or false
  277. */
  278. public function getOwner($path) {
  279. return \OC_User::getUser();
  280. }
  281. /**
  282. * get the ETag for a file or folder
  283. *
  284. * @param string $path
  285. * @return string
  286. */
  287. public function getETag($path) {
  288. $ETagFunction = \OC_Connector_Sabre_Node::$ETagFunction;
  289. if ($ETagFunction) {
  290. $hash = call_user_func($ETagFunction, $path);
  291. return $hash;
  292. } else {
  293. return uniqid();
  294. }
  295. }
  296. /**
  297. * clean a path, i.e. remove all redundant '.' and '..'
  298. * making sure that it can't point to higher than '/'
  299. *
  300. * @param $path The path to clean
  301. * @return string cleaned path
  302. */
  303. public function cleanPath($path) {
  304. if (strlen($path) == 0 or $path[0] != '/') {
  305. $path = '/' . $path;
  306. }
  307. $output = array();
  308. foreach (explode('/', $path) as $chunk) {
  309. if ($chunk == '..') {
  310. array_pop($output);
  311. } else if ($chunk == '.') {
  312. } else {
  313. $output[] = $chunk;
  314. }
  315. }
  316. return implode('/', $output);
  317. }
  318. public function test() {
  319. if ($this->stat('')) {
  320. return true;
  321. }
  322. return false;
  323. }
  324. /**
  325. * get the free space in the storage
  326. *
  327. * @param $path
  328. * @return int
  329. */
  330. public function free_space($path) {
  331. return \OC\Files\FREE_SPACE_UNKNOWN;
  332. }
  333. }