common.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. }