local.php 4.8 KB

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