common.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. abstract class OC_Filestorage_Common extends OC_Filestorage {
  22. public function __construct($parameters){}
  23. // abstract public function mkdir($path);
  24. // abstract public function rmdir($path);
  25. // abstract public function opendir($path);
  26. public function is_dir($path){
  27. return $this->filetype($path)=='dir';
  28. }
  29. public function is_file($path){
  30. return $this->filetype($path)=='file';
  31. }
  32. // abstract public function stat($path);
  33. // abstract public function filetype($path);
  34. public function filesize($path) {
  35. if($this->is_dir($path)){
  36. return 0;//by definition
  37. }else{
  38. $stat = $this->stat($path);
  39. return $stat['size'];
  40. }
  41. }
  42. // abstract public function is_readable($path);
  43. // abstract public function is_writable($path);
  44. // abstract public function file_exists($path);
  45. public function filectime($path) {
  46. $stat = $this->stat($path);
  47. return $stat['ctime'];
  48. }
  49. public function filemtime($path) {
  50. $stat = $this->stat($path);
  51. return $stat['mtime'];
  52. }
  53. public function fileatime($path) {
  54. $stat = $this->stat($path);
  55. return $stat['atime'];
  56. }
  57. public function file_get_contents($path) {
  58. $handle = $this->fopen($path, "r");
  59. if(!$handle){
  60. return false;
  61. }
  62. $size=$this->filesize($path);
  63. if($size==0){
  64. return '';
  65. }
  66. return fread($handle, $size);
  67. }
  68. public function file_put_contents($path,$data) {
  69. $handle = $this->fopen($path, "w");
  70. return fwrite($handle, $data);
  71. }
  72. // abstract public function unlink($path);
  73. public function rename($path1,$path2){
  74. if($this->copy($path1,$path2)){
  75. return $this->unlink($path1);
  76. }else{
  77. return false;
  78. }
  79. }
  80. public function copy($path1,$path2) {
  81. $source=$this->fopen($path1,'r');
  82. $target=$this->fopen($path2,'w');
  83. $count=OC_Helper::streamCopy($source,$target);
  84. return $count>0;
  85. }
  86. // abstract public function fopen($path,$mode);
  87. public function getMimeType($path){
  88. if(!$this->file_exists($path)){
  89. return false;
  90. }
  91. if($this->is_dir($path)){
  92. return 'httpd/unix-directory';
  93. }
  94. $source=$this->fopen($path,'r');
  95. if(!$source){
  96. return false;
  97. }
  98. $head=fread($source,8192);//8kb should suffice to determine a mimetype
  99. if($pos=strrpos($path,'.')){
  100. $extension=substr($path,$pos);
  101. }else{
  102. $extension='';
  103. }
  104. $tmpFile=OC_Helper::tmpFile($extension);
  105. file_put_contents($tmpFile,$head);
  106. $mime=OC_Helper::getMimeType($tmpFile);
  107. unlink($tmpFile);
  108. return $mime;
  109. }
  110. public function hash($type,$path,$raw){
  111. $tmpFile=$this->getLocalFile();
  112. $hash=hash($type,$tmpFile,$raw);
  113. unlink($tmpFile);
  114. return $hash;
  115. }
  116. // abstract public function free_space($path);
  117. public function search($query){
  118. return $this->searchInDir($query);
  119. }
  120. public function getLocalFile($path){
  121. return $this->toTmpFile($path);
  122. }
  123. private function toTmpFile($path){//no longer in the storage api, still usefull here
  124. $source=$this->fopen($path,'r');
  125. if(!$source){
  126. return false;
  127. }
  128. if($pos=strrpos($path,'.')){
  129. $extension=substr($path,$pos);
  130. }else{
  131. $extension='';
  132. }
  133. $tmpFile=OC_Helper::tmpFile($extension);
  134. $target=fopen($tmpFile,'w');
  135. $count=OC_Helper::streamCopy($source,$target);
  136. return $tmpFile;
  137. }
  138. // abstract public function touch($path, $mtime=null);
  139. protected function searchInDir($query,$dir=''){
  140. $files=array();
  141. $dh=$this->opendir($dir);
  142. if($dh){
  143. while($item=readdir($dh)){
  144. if ($item == '.' || $item == '..') continue;
  145. if(strstr(strtolower($item),strtolower($query))!==false){
  146. $files[]=$dir.'/'.$item;
  147. }
  148. if($this->is_dir($dir.'/'.$item)){
  149. $files=array_merge($files,$this->searchInDir($query,$dir.'/'.$item));
  150. }
  151. }
  152. }
  153. return $files;
  154. }
  155. /**
  156. * check if a file or folder has been updated since $time
  157. * @param int $time
  158. * @return bool
  159. */
  160. public function hasUpdated($path,$time){
  161. return $this->filemtime($path)>$time;
  162. }
  163. }