PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/filestorage/common.php

https://github.com/jlgg/simple_trash
PHP | 291 lines | 190 code | 37 blank | 64 comment | 37 complexity | 6c355eb1e52ba24b4569acec8e11284d MD5 | raw file
Possible License(s): AGPL-3.0, AGPL-1.0, MPL-2.0-no-copyleft-exception
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @author Michael Gapczynski
  6. * @copyright 2012 Michael Gapczynski GapczynskiM@gmail.com
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. /**
  22. * Storage backend class for providing common filesystem operation methods
  23. * which are not storage-backend specific.
  24. *
  25. * OC_Filestorage_Common is never used directly; it is extended by all other
  26. * storage backends, where its methods may be overridden, and additional
  27. * (backend-specific) methods are defined.
  28. *
  29. * Some OC_Filestorage_Common methods call functions which are first defined
  30. * in classes which extend it, e.g. $this->stat() .
  31. */
  32. abstract class OC_Filestorage_Common extends OC_Filestorage {
  33. public function __construct($parameters) {}
  34. // abstract public function mkdir($path);
  35. // abstract public function rmdir($path);
  36. // abstract public function opendir($path);
  37. public function is_dir($path) {
  38. return $this->filetype($path)=='dir';
  39. }
  40. public function is_file($path) {
  41. return $this->filetype($path)=='file';
  42. }
  43. // abstract public function stat($path);
  44. // abstract public function filetype($path);
  45. public function filesize($path) {
  46. if($this->is_dir($path)) {
  47. return 0;//by definition
  48. }else{
  49. $stat = $this->stat($path);
  50. return $stat['size'];
  51. }
  52. }
  53. public function isCreatable($path) {
  54. return $this->isUpdatable($path);
  55. }
  56. // abstract public function isReadable($path);
  57. // abstract public function isUpdatable($path);
  58. public function isDeletable($path) {
  59. return $this->isUpdatable($path);
  60. }
  61. public function isSharable($path) {
  62. return $this->isReadable($path);
  63. }
  64. // abstract public function file_exists($path);
  65. public function filectime($path) {
  66. $stat = $this->stat($path);
  67. return $stat['ctime'];
  68. }
  69. public function filemtime($path) {
  70. $stat = $this->stat($path);
  71. return $stat['mtime'];
  72. }
  73. public function fileatime($path) {
  74. $stat = $this->stat($path);
  75. return $stat['atime'];
  76. }
  77. public function file_get_contents($path) {
  78. $handle = $this->fopen($path, "r");
  79. if(!$handle) {
  80. return false;
  81. }
  82. $size=$this->filesize($path);
  83. if($size==0) {
  84. return '';
  85. }
  86. return fread($handle, $size);
  87. }
  88. public function file_put_contents($path, $data) {
  89. $handle = $this->fopen($path, "w");
  90. return fwrite($handle, $data);
  91. }
  92. // abstract public function unlink($path);
  93. public function rename($path1, $path2) {
  94. if($this->copy($path1, $path2)) {
  95. return $this->unlink($path1);
  96. }else{
  97. return false;
  98. }
  99. }
  100. public function copy($path1, $path2) {
  101. $source=$this->fopen($path1, 'r');
  102. $target=$this->fopen($path2, 'w');
  103. $count=OC_Helper::streamCopy($source, $target);
  104. return $count>0;
  105. }
  106. // abstract public function fopen($path, $mode);
  107. /**
  108. * @brief Deletes all files and folders recursively within a directory
  109. * @param $directory The directory whose contents will be deleted
  110. * @param $empty Flag indicating whether directory will be emptied
  111. * @returns true/false
  112. *
  113. * @note By default the directory specified by $directory will be
  114. * deleted together with its contents. To avoid this set $empty to true
  115. */
  116. public function deleteAll( $directory, $empty = false ) {
  117. // strip leading slash
  118. if( substr( $directory, 0, 1 ) == "/" ) {
  119. $directory = substr( $directory, 1 );
  120. }
  121. // strip trailing slash
  122. if( substr( $directory, -1) == "/" ) {
  123. $directory = substr( $directory, 0, -1 );
  124. }
  125. if ( !$this->file_exists( \OCP\USER::getUser() . '/' . $directory ) || !$this->is_dir( \OCP\USER::getUser() . '/' . $directory ) ) {
  126. return false;
  127. } elseif( !$this->is_readable( \OCP\USER::getUser() . '/' . $directory ) ) {
  128. return false;
  129. } else {
  130. $directoryHandle = $this->opendir( \OCP\USER::getUser() . '/' . $directory );
  131. while ( $contents = readdir( $directoryHandle ) ) {
  132. if ( $contents != '.' && $contents != '..') {
  133. $path = $directory . "/" . $contents;
  134. if ( $this->is_dir( $path ) ) {
  135. deleteAll( $path );
  136. } else {
  137. $this->unlink( \OCP\USER::getUser() .'/' . $path ); // TODO: make unlink use same system path as is_dir
  138. }
  139. }
  140. }
  141. //$this->closedir( $directoryHandle ); // TODO: implement closedir in OC_FSV
  142. if ( $empty == false ) {
  143. if ( !$this->rmdir( $directory ) ) {
  144. return false;
  145. }
  146. }
  147. return true;
  148. }
  149. }
  150. public function getMimeType($path) {
  151. if(!$this->file_exists($path)) {
  152. return false;
  153. }
  154. if($this->is_dir($path)) {
  155. return 'httpd/unix-directory';
  156. }
  157. $source=$this->fopen($path, 'r');
  158. if(!$source) {
  159. return false;
  160. }
  161. $head=fread($source, 8192);//8kb should suffice to determine a mimetype
  162. if($pos=strrpos($path, '.')) {
  163. $extension=substr($path, $pos);
  164. }else{
  165. $extension='';
  166. }
  167. $tmpFile=OC_Helper::tmpFile($extension);
  168. file_put_contents($tmpFile, $head);
  169. $mime=OC_Helper::getMimeType($tmpFile);
  170. unlink($tmpFile);
  171. return $mime;
  172. }
  173. public function hash($type, $path, $raw = false) {
  174. $tmpFile=$this->getLocalFile();
  175. $hash=hash($type, $tmpFile, $raw);
  176. unlink($tmpFile);
  177. return $hash;
  178. }
  179. // abstract public function free_space($path);
  180. public function search($query) {
  181. return $this->searchInDir($query);
  182. }
  183. public function getLocalFile($path) {
  184. return $this->toTmpFile($path);
  185. }
  186. private function toTmpFile($path) {//no longer in the storage api, still usefull here
  187. $source=$this->fopen($path, 'r');
  188. if(!$source) {
  189. return false;
  190. }
  191. if($pos=strrpos($path, '.')) {
  192. $extension=substr($path, $pos);
  193. }else{
  194. $extension='';
  195. }
  196. $tmpFile=OC_Helper::tmpFile($extension);
  197. $target=fopen($tmpFile, 'w');
  198. OC_Helper::streamCopy($source, $target);
  199. return $tmpFile;
  200. }
  201. public function getLocalFolder($path) {
  202. $baseDir=OC_Helper::tmpFolder();
  203. $this->addLocalFolder($path, $baseDir);
  204. return $baseDir;
  205. }
  206. private function addLocalFolder($path, $target) {
  207. if($dh=$this->opendir($path)) {
  208. while($file=readdir($dh)) {
  209. if($file!=='.' and $file!=='..') {
  210. if($this->is_dir($path.'/'.$file)) {
  211. mkdir($target.'/'.$file);
  212. $this->addLocalFolder($path.'/'.$file, $target.'/'.$file);
  213. }else{
  214. $tmp=$this->toTmpFile($path.'/'.$file);
  215. rename($tmp, $target.'/'.$file);
  216. }
  217. }
  218. }
  219. }
  220. }
  221. // abstract public function touch($path, $mtime=null);
  222. protected function searchInDir($query, $dir='') {
  223. $files=array();
  224. $dh=$this->opendir($dir);
  225. if($dh) {
  226. while($item=readdir($dh)) {
  227. if ($item == '.' || $item == '..') continue;
  228. if(strstr(strtolower($item), strtolower($query))!==false) {
  229. $files[]=$dir.'/'.$item;
  230. }
  231. if($this->is_dir($dir.'/'.$item)) {
  232. $files=array_merge($files, $this->searchInDir($query, $dir.'/'.$item));
  233. }
  234. }
  235. }
  236. return $files;
  237. }
  238. /**
  239. * check if a file or folder has been updated since $time
  240. * @param int $time
  241. * @return bool
  242. */
  243. public function hasUpdated($path, $time) {
  244. return $this->filemtime($path)>$time;
  245. }
  246. /**
  247. * get the owner of a path
  248. * @param $path The path to get the owner
  249. * @return string uid or false
  250. */
  251. public function getOwner($path) {
  252. return OC_User::getUser();
  253. }
  254. }