local.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. /**
  3. * for local filestore, we only have to map the paths
  4. */
  5. class OC_Filestorage_Local extends OC_Filestorage_Common{
  6. protected $datadir;
  7. public function __construct($arguments) {
  8. $this->datadir=$arguments['datadir'];
  9. if(substr($this->datadir,-1)!=='/') {
  10. $this->datadir.='/';
  11. }
  12. }
  13. public function mkdir($path) {
  14. return @mkdir($this->datadir.$path);
  15. }
  16. public function rmdir($path) {
  17. return @rmdir($this->datadir.$path);
  18. }
  19. public function opendir($path) {
  20. return opendir($this->datadir.$path);
  21. }
  22. public function is_dir($path) {
  23. if(substr($path,-1)=='/') {
  24. $path=substr($path,0,-1);
  25. }
  26. return is_dir($this->datadir.$path);
  27. }
  28. public function is_file($path) {
  29. return is_file($this->datadir.$path);
  30. }
  31. public function stat($path) {
  32. return stat($this->datadir.$path);
  33. }
  34. public function filetype($path) {
  35. $filetype=filetype($this->datadir.$path);
  36. if($filetype=='link') {
  37. $filetype=filetype(realpath($this->datadir.$path));
  38. }
  39. return $filetype;
  40. }
  41. public function filesize($path) {
  42. if($this->is_dir($path)) {
  43. return 0;
  44. }else{
  45. return filesize($this->datadir.$path);
  46. }
  47. }
  48. public function isReadable($path) {
  49. return is_readable($this->datadir.$path);
  50. }
  51. public function isUpdatable($path) {
  52. return is_writable($this->datadir.$path);
  53. }
  54. public function file_exists($path) {
  55. return file_exists($this->datadir.$path);
  56. }
  57. public function filectime($path) {
  58. return filectime($this->datadir.$path);
  59. }
  60. public function filemtime($path) {
  61. return filemtime($this->datadir.$path);
  62. }
  63. public function touch($path, $mtime=null) {
  64. // sets the modification time of the file to the given value.
  65. // If mtime is nil the current time is set.
  66. // note that the access time of the file always changes to the current time.
  67. if(!is_null($mtime)) {
  68. $result=touch( $this->datadir.$path, $mtime );
  69. }else{
  70. $result=touch( $this->datadir.$path);
  71. }
  72. if( $result ) {
  73. clearstatcache( true, $this->datadir.$path );
  74. }
  75. return $result;
  76. }
  77. public function file_get_contents($path) {
  78. return file_get_contents($this->datadir.$path);
  79. }
  80. public function file_put_contents($path,$data) {
  81. return file_put_contents($this->datadir.$path,$data);
  82. }
  83. public function unlink($path) {
  84. return $this->delTree($path);
  85. }
  86. public function rename($path1,$path2) {
  87. if (!$this->isUpdatable($path1)) {
  88. OC_Log::write('core','unable to rename, file is not writable : '.$path1,OC_Log::ERROR);
  89. return false;
  90. }
  91. if(! $this->file_exists($path1)) {
  92. OC_Log::write('core','unable to rename, file does not exists : '.$path1,OC_Log::ERROR);
  93. return false;
  94. }
  95. if($return=rename($this->datadir.$path1,$this->datadir.$path2)) {
  96. }
  97. return $return;
  98. }
  99. public function copy($path1,$path2) {
  100. if($this->is_dir($path2)) {
  101. if(!$this->file_exists($path2)) {
  102. $this->mkdir($path2);
  103. }
  104. $source=substr($path1,strrpos($path1,'/')+1);
  105. $path2.=$source;
  106. }
  107. return copy($this->datadir.$path1,$this->datadir.$path2);
  108. }
  109. public function fopen($path,$mode) {
  110. if($return=fopen($this->datadir.$path,$mode)) {
  111. switch($mode) {
  112. case 'r':
  113. break;
  114. case 'r+':
  115. case 'w+':
  116. case 'x+':
  117. case 'a+':
  118. break;
  119. case 'w':
  120. case 'x':
  121. case 'a':
  122. break;
  123. }
  124. }
  125. return $return;
  126. }
  127. public function getMimeType($path) {
  128. if($this->isReadable($path)) {
  129. return OC_Helper::getMimeType($this->datadir.$path);
  130. }else{
  131. return false;
  132. }
  133. }
  134. private function delTree($dir) {
  135. $dirRelative=$dir;
  136. $dir=$this->datadir.$dir;
  137. if (!file_exists($dir)) return true;
  138. if (!is_dir($dir) || is_link($dir)) return unlink($dir);
  139. foreach (scandir($dir) as $item) {
  140. if ($item == '.' || $item == '..') continue;
  141. if(is_file($dir.'/'.$item)) {
  142. if(unlink($dir.'/'.$item)) {
  143. }
  144. }elseif(is_dir($dir.'/'.$item)) {
  145. if (!$this->delTree($dirRelative. "/" . $item)) {
  146. return false;
  147. };
  148. }
  149. }
  150. if($return=rmdir($dir)) {
  151. }
  152. return $return;
  153. }
  154. public function hash($path,$type,$raw=false) {
  155. return hash_file($type,$this->datadir.$path,$raw);
  156. }
  157. public function free_space($path) {
  158. return @disk_free_space($this->datadir.$path);
  159. }
  160. public function search($query) {
  161. return $this->searchInDir($query);
  162. }
  163. public function getLocalFile($path) {
  164. return $this->datadir.$path;
  165. }
  166. public function getLocalFolder($path) {
  167. return $this->datadir.$path;
  168. }
  169. protected function searchInDir($query,$dir='') {
  170. $files=array();
  171. foreach (scandir($this->datadir.$dir) as $item) {
  172. if ($item == '.' || $item == '..') continue;
  173. if(strstr(strtolower($item),strtolower($query))!==false) {
  174. $files[]=$dir.'/'.$item;
  175. }
  176. if(is_dir($this->datadir.$dir.'/'.$item)) {
  177. $files=array_merge($files,$this->searchInDir($query,$dir.'/'.$item));
  178. }
  179. }
  180. return $files;
  181. }
  182. /**
  183. * check if a file or folder has been updated since $time
  184. * @param int $time
  185. * @return bool
  186. */
  187. public function hasUpdated($path,$time) {
  188. return $this->filemtime($path)>$time;
  189. }
  190. }