filesystemview.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @author Frank Karlitschek
  6. * @copyright 2010 Frank Karlitschek karlitschek@kde.org
  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. class OC_FilesystemView {
  23. private $fakeRoot='';
  24. public function __construct($root){
  25. $this->fakeRoot=$root;
  26. }
  27. public function getAbsolutePath($path){
  28. if(!$path){
  29. $path='/';
  30. }
  31. if(substr($path,0,1)!=='/'){
  32. $path='/'.$path;
  33. }
  34. return $this->fakeRoot.$path;
  35. }
  36. /**
  37. * change the root to a fake toor
  38. * @param string fakeRoot
  39. * @return bool
  40. */
  41. public function chroot($fakeRoot){
  42. if(!$fakeRoot==''){
  43. if($fakeRoot[0]!=='/'){
  44. $fakeRoot='/'.$fakeRoot;
  45. }
  46. }
  47. $this->fakeRoot=$fakeRoot;
  48. }
  49. /**
  50. * get the fake root
  51. * @return string
  52. */
  53. public function getRoot(){
  54. return $this->fakeRoot;
  55. }
  56. /**
  57. * get the part of the path relative to the mountpoint of the storage it's stored in
  58. * @param string path
  59. * @return bool
  60. */
  61. public function getInternalPath($path){
  62. return OC_Filesystem::getInternalPath($this->getAbsolutePath($path));
  63. }
  64. /**
  65. * get the storage object for a path
  66. * @param string path
  67. * @return OC_Filestorage
  68. */
  69. public function getStorage($path){
  70. return OC_Filesystem::getStorage($this->getAbsolutePath($path));
  71. }
  72. /**
  73. * get the mountpoint of the storage object for a path
  74. ( note: because a storage is not always mounted inside the fakeroot, the returned mountpoint is relative to the absolute root of the filesystem and doesn't take the chroot into account
  75. *
  76. * @param string path
  77. * @return string
  78. */
  79. public function getMountPoint($path){
  80. return OC_Filesystem::getMountPoint($this->getAbsolutePath($path));
  81. }
  82. /**
  83. * return the path to a local version of the file
  84. * we need this because we can't know if a file is stored local or not from outside the filestorage and for some purposes a local file is needed
  85. * @param string path
  86. * @return string
  87. */
  88. public function getLocalFile($path){
  89. $parent=substr($path,0,strrpos($path,'/'));
  90. if(OC_Filesystem::isValidPath($parent) and $storage=$this->getStorage($path)){
  91. return $storage->getLocalFile($this->getInternalPath($path));
  92. }
  93. }
  94. /**
  95. * following functions are equivilent to their php buildin equivilents for arguments/return values.
  96. */
  97. public function mkdir($path){
  98. return $this->basicOperation('mkdir',$path,array('create','write'));
  99. }
  100. public function rmdir($path){
  101. return $this->basicOperation('rmdir',$path,array('delete'));
  102. }
  103. public function opendir($path){
  104. return $this->basicOperation('opendir',$path,array('read'));
  105. }
  106. public function is_dir($path){
  107. if($path=='/'){
  108. return true;
  109. }
  110. return $this->basicOperation('is_dir',$path);
  111. }
  112. public function is_file($path){
  113. if($path=='/'){
  114. return false;
  115. }
  116. return $this->basicOperation('is_file',$path);
  117. }
  118. public function stat($path){
  119. return $this->basicOperation('stat',$path);
  120. }
  121. public function filetype($path){
  122. return $this->basicOperation('filetype',$path);
  123. }
  124. public function filesize($path){
  125. return $this->basicOperation('filesize',$path);
  126. }
  127. public function readfile($path){
  128. @ob_end_clean();
  129. $handle=$this->fopen($path,'r');
  130. if ($handle) {
  131. $chunkSize = 8*1024;// 1 MB chunks
  132. while (!feof($handle)) {
  133. echo fread($handle, $chunkSize);
  134. flush();
  135. }
  136. $size=$this->filesize($path);
  137. return $size;
  138. }
  139. return false;
  140. }
  141. public function is_readable($path){
  142. return $this->basicOperation('is_readable',$path);
  143. }
  144. public function is_writable($path){
  145. return $this->basicOperation('is_writable',$path);
  146. }
  147. public function file_exists($path){
  148. if($path=='/'){
  149. return true;
  150. }
  151. return $this->basicOperation('file_exists',$path);
  152. }
  153. public function filectime($path){
  154. return $this->basicOperation('filectime',$path);
  155. }
  156. public function filemtime($path){
  157. return $this->basicOperation('filemtime',$path);
  158. }
  159. public function touch($path, $mtime=null){
  160. return $this->basicOperation('touch', $path, array('write'), $mtime);
  161. }
  162. public function file_get_contents($path){
  163. return $this->basicOperation('file_get_contents',$path,array('read'));
  164. }
  165. public function file_put_contents($path,$data){
  166. if(is_resource($data)){//not having to deal with streams in file_put_contents makes life easier
  167. $exists=$this->file_exists($path);
  168. $run=true;
  169. if(!$exists){
  170. OC_Hook::emit( OC_Filesystem::CLASSNAME, OC_Filesystem::signal_create, array( OC_Filesystem::signal_param_path => $path, OC_Filesystem::signal_param_run => &$run));
  171. }
  172. OC_Hook::emit( OC_Filesystem::CLASSNAME, OC_Filesystem::signal_write, array( OC_Filesystem::signal_param_path => $path, OC_Filesystem::signal_param_run => &$run));
  173. if(!$run){
  174. return false;
  175. }
  176. $target=$this->fopen($path,'w');
  177. if($target){
  178. $count=OC_Helper::streamCopy($data,$target);
  179. fclose($target);
  180. fclose($data);
  181. if(!$exists){
  182. OC_Hook::emit( OC_Filesystem::CLASSNAME, OC_Filesystem::signal_post_create, array( OC_Filesystem::signal_param_path => $path));
  183. }
  184. OC_Hook::emit( OC_Filesystem::CLASSNAME, OC_Filesystem::signal_post_write, array( OC_Filesystem::signal_param_path => $path));
  185. return $count>0;
  186. }else{
  187. return false;
  188. }
  189. }else{
  190. return $this->basicOperation('file_put_contents',$path,array('create','write'),$data);
  191. }
  192. }
  193. public function unlink($path){
  194. return $this->basicOperation('unlink',$path,array('delete'));
  195. }
  196. public function rename($path1,$path2){
  197. if(OC_FileProxy::runPreProxies('rename',$path1,$path2) and OC_Filesystem::isValidPath($path2)){
  198. $run=true;
  199. OC_Hook::emit( OC_Filesystem::CLASSNAME, OC_Filesystem::signal_rename, array( OC_Filesystem::signal_param_oldpath => $path1 , OC_Filesystem::signal_param_newpath=>$path2, OC_Filesystem::signal_param_run => &$run));
  200. if($run){
  201. $mp1=$this->getMountPoint($path1);
  202. $mp2=$this->getMountPoint($path2);
  203. if($mp1==$mp2){
  204. if($storage=$this->getStorage($path1)){
  205. $result=$storage->rename($this->getInternalPath($path1),$this->getInternalPath($path2));
  206. }
  207. }else{
  208. $source=$this->fopen($path1,'r');
  209. $target=$this->fopen($path2,'w');
  210. $count=OC_Helper::streamCopy($data,$target);
  211. $storage1=$this->getStorage($path1);
  212. $storage1->unlink($this->getInternalPath($path1));
  213. $result=$count>0;
  214. }
  215. OC_Hook::emit( OC_Filesystem::CLASSNAME, OC_Filesystem::signal_post_rename, array( OC_Filesystem::signal_param_oldpath => $path1, OC_Filesystem::signal_param_newpath=>$path2));
  216. return $result;
  217. }
  218. }
  219. }
  220. public function copy($path1,$path2){
  221. if(OC_FileProxy::runPreProxies('copy',$path1,$path2) and $this->is_readable($path1) and OC_Filesystem::isValidPath($path2)){
  222. $run=true;
  223. OC_Hook::emit( OC_Filesystem::CLASSNAME, OC_Filesystem::signal_copy, array( OC_Filesystem::signal_param_oldpath => $path1 , OC_Filesystem::signal_param_newpath=>$path2, OC_Filesystem::signal_param_run => &$run));
  224. $exists=$this->file_exists($path2);
  225. if($run and !$exists){
  226. OC_Hook::emit( OC_Filesystem::CLASSNAME, OC_Filesystem::signal_create, array( OC_Filesystem::signal_param_path => $path2, OC_Filesystem::signal_param_run => &$run));
  227. }
  228. if($run){
  229. OC_Hook::emit( OC_Filesystem::CLASSNAME, OC_Filesystem::signal_write, array( OC_Filesystem::signal_param_path => $path2, OC_Filesystem::signal_param_run => &$run));
  230. }
  231. if($run){
  232. $mp1=$this->getMountPoint($path1);
  233. $mp2=$this->getMountPoint($path2);
  234. if($mp1==$mp2){
  235. if($storage=$this->getStorage($path1)){
  236. $result=$storage->copy($this->getInternalPath($path1),$this->getInternalPath($path2));
  237. }
  238. }else{
  239. $source=$this->fopen($path1,'r');
  240. $target=$this->fopen($path2,'w');
  241. $count=OC_Helper::streamCopy($data,$target);
  242. }
  243. OC_Hook::emit( OC_Filesystem::CLASSNAME, OC_Filesystem::signal_post_copy, array( OC_Filesystem::signal_param_oldpath => $path1 , OC_Filesystem::signal_param_newpath=>$path2));
  244. if(!$exists){
  245. OC_Hook::emit( OC_Filesystem::CLASSNAME, OC_Filesystem::signal_post_create, array( OC_Filesystem::signal_param_path => $path2));
  246. }
  247. OC_Hook::emit( OC_Filesystem::CLASSNAME, OC_Filesystem::signal_post_write, array( OC_Filesystem::signal_param_path => $path2));
  248. return $result;
  249. }
  250. }
  251. }
  252. public function fopen($path,$mode){
  253. $hooks=array();
  254. switch($mode){
  255. case 'r':
  256. case 'rb':
  257. $hooks[]='read';
  258. break;
  259. case 'r+':
  260. case 'rb+':
  261. case 'w+':
  262. case 'wb+':
  263. case 'x+':
  264. case 'xb+':
  265. case 'a+':
  266. case 'ab+':
  267. $hooks[]='read';
  268. $hooks[]='write';
  269. break;
  270. case 'w':
  271. case 'wb':
  272. case 'x':
  273. case 'xb':
  274. case 'a':
  275. case 'ab':
  276. $hooks[]='write';
  277. break;
  278. default:
  279. OC_Log::write('core','invalid mode ('.$mode.') for '.$path,OC_Log::ERROR);
  280. }
  281. return $this->basicOperation('fopen',$path,$hooks,$mode);
  282. }
  283. public function toTmpFile($path){
  284. if(OC_Filesystem::isValidPath($path)){
  285. $source=$this->fopen($path,'r');
  286. if($source){
  287. $extension='';
  288. $extOffset=strpos($path,'.');
  289. if($extOffset !== false) {
  290. $extension=substr($path,strrpos($path,'.'));
  291. }
  292. $tmpFile=OC_Helper::tmpFile($extension);
  293. file_put_contents($tmpFile,$source);
  294. return $tmpFile;
  295. }
  296. }
  297. }
  298. public function fromTmpFile($tmpFile,$path){
  299. if(OC_Filesystem::isValidPath($path)){
  300. if(!$tmpFile){
  301. debug_print_backtrace();
  302. }
  303. $source=fopen($tmpFile,'r');
  304. if($source){
  305. $this->file_put_contents($path,$source);
  306. unlink($tmpFile);
  307. return true;
  308. }else{
  309. }
  310. }else{
  311. return false;
  312. }
  313. }
  314. public function getMimeType($path){
  315. return $this->basicOperation('getMimeType',$path);
  316. }
  317. public function hash($type,$path){
  318. return $this->basicOperation('hash',$path,array('read'));
  319. }
  320. public function free_space($path='/'){
  321. return $this->basicOperation('free_space',$path);
  322. }
  323. /**
  324. * abstraction for running most basic operations
  325. * @param string $operation
  326. * @param string #path
  327. * @param array (optional) hooks
  328. * @param mixed (optional) $extraParam
  329. * @return mixed
  330. */
  331. private function basicOperation($operation,$path,$hooks=array(),$extraParam=null){
  332. if(OC_FileProxy::runPreProxies($operation,$path, $extraParam) and OC_Filesystem::isValidPath($path)){
  333. $interalPath=$this->getInternalPath($path);
  334. $run=true;
  335. if(OC_Filesystem::$loaded and $this->fakeRoot==OC_Filesystem::getRoot()){
  336. foreach($hooks as $hook){
  337. if($hook!='read'){
  338. OC_Hook::emit( OC_Filesystem::CLASSNAME, $hook, array( OC_Filesystem::signal_param_path => $path, OC_Filesystem::signal_param_run => &$run));
  339. }else{
  340. OC_Hook::emit( OC_Filesystem::CLASSNAME, $hook, array( OC_Filesystem::signal_param_path => $path));
  341. }
  342. }
  343. }
  344. if($run and $storage=$this->getStorage($path)){
  345. if(!is_null($extraParam)){
  346. $result=$storage->$operation($interalPath,$extraParam);
  347. }else{
  348. $result=$storage->$operation($interalPath);
  349. }
  350. $result=OC_FileProxy::runPostProxies($operation,$path,$result);
  351. if(OC_Filesystem::$loaded and $this->fakeRoot==OC_Filesystem::getRoot()){
  352. if($operation!='fopen'){//no post hooks for fopen, the file stream is still open
  353. foreach($hooks as $hook){
  354. if($hook!='read'){
  355. OC_Hook::emit( OC_Filesystem::CLASSNAME, 'post_'.$hook, array( OC_Filesystem::signal_param_path => $path));
  356. }
  357. }
  358. }
  359. }
  360. return $result;
  361. }
  362. }
  363. return null;
  364. }
  365. }