filesystemview.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @author Frank Karlitschek
  6. * @copyright 2012 Frank Karlitschek frank@owncloud.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 to provide access to ownCloud filesystem via a "view", and methods for
  23. * working with files within that view (e.g. read, write, delete, etc.). Each
  24. * view is restricted to a set of directories via a virtual root. The default view
  25. * uses the currently logged in user's data directory as root (parts of
  26. * OC_Filesystem are merely a wrapper for OC_FilesystemView).
  27. *
  28. * Apps that need to access files outside of the user data folders (to modify files
  29. * belonging to a user other than the one currently logged in, for example) should
  30. * use this class directly rather than using OC_Filesystem, or making use of PHP's
  31. * built-in file manipulation functions. This will ensure all hooks and proxies
  32. * are triggered correctly.
  33. *
  34. * Filesystem functions are not called directly; they are passed to the correct
  35. * OC_Filestorage object
  36. */
  37. class OC_FilesystemView {
  38. private $fakeRoot='';
  39. private $internal_path_cache=array();
  40. private $storage_cache=array();
  41. public function __construct($root){
  42. $this->fakeRoot=$root;
  43. }
  44. public function getAbsolutePath($path){
  45. if(!$path){
  46. $path='/';
  47. }
  48. if($path[0]!=='/'){
  49. $path='/'.$path;
  50. }
  51. return $this->fakeRoot.$path;
  52. }
  53. /**
  54. * change the root to a fake toor
  55. * @param string fakeRoot
  56. * @return bool
  57. */
  58. public function chroot($fakeRoot){
  59. if(!$fakeRoot==''){
  60. if($fakeRoot[0]!=='/'){
  61. $fakeRoot='/'.$fakeRoot;
  62. }
  63. }
  64. $this->fakeRoot=$fakeRoot;
  65. }
  66. /**
  67. * get the fake root
  68. * @return string
  69. */
  70. public function getRoot(){
  71. return $this->fakeRoot;
  72. }
  73. /**
  74. * get the part of the path relative to the mountpoint of the storage it's stored in
  75. * @param string path
  76. * @return bool
  77. */
  78. public function getInternalPath($path){
  79. if (!isset($this->internal_path_cache[$path])) {
  80. $this->internal_path_cache[$path] = OC_Filesystem::getInternalPath($this->getAbsolutePath($path));
  81. }
  82. return $this->internal_path_cache[$path];
  83. }
  84. /**
  85. * get path relative to the root of the view
  86. * @param string path
  87. * @return string
  88. */
  89. public function getRelativePath($path){
  90. if($this->fakeRoot==''){
  91. return $path;
  92. }
  93. if(strpos($path,$this->fakeRoot)!==0){
  94. return null;
  95. }else{
  96. return substr($path,strlen($this->fakeRoot));
  97. }
  98. }
  99. /**
  100. * get the storage object for a path
  101. * @param string path
  102. * @return OC_Filestorage
  103. */
  104. public function getStorage($path){
  105. if (!isset($this->storage_cache[$path])) {
  106. $this->storage_cache[$path] = OC_Filesystem::getStorage($this->getAbsolutePath($path));
  107. }
  108. return $this->storage_cache[$path];
  109. }
  110. /**
  111. * get the mountpoint of the storage object for a path
  112. ( 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
  113. *
  114. * @param string path
  115. * @return string
  116. */
  117. public function getMountPoint($path){
  118. return OC_Filesystem::getMountPoint($this->getAbsolutePath($path));
  119. }
  120. /**
  121. * return the path to a local version of the file
  122. * 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
  123. * @param string path
  124. * @return string
  125. */
  126. public function getLocalFile($path){
  127. $parent=substr($path,0,strrpos($path,'/'));
  128. if(OC_Filesystem::isValidPath($parent) and $storage=$this->getStorage($path)){
  129. return $storage->getLocalFile($this->getInternalPath($path));
  130. }
  131. }
  132. /**
  133. * the following functions operate with arguments and return values identical
  134. * to those of their PHP built-in equivalents. Mostly they are merely wrappers
  135. * for OC_Filestorage via basicOperation().
  136. */
  137. public function mkdir($path){
  138. return $this->basicOperation('mkdir',$path,array('create','write'));
  139. }
  140. public function rmdir($path){
  141. return $this->basicOperation('rmdir',$path,array('delete'));
  142. }
  143. public function opendir($path){
  144. return $this->basicOperation('opendir',$path,array('read'));
  145. }
  146. public function readdir($handle){
  147. $fsLocal= new OC_Filestorage_Local( array( 'datadir' => '/' ) );
  148. return $fsLocal->readdir( $handle );
  149. }
  150. public function is_dir($path){
  151. if($path=='/'){
  152. return true;
  153. }
  154. return $this->basicOperation('is_dir',$path);
  155. }
  156. public function is_file($path){
  157. if($path=='/'){
  158. return false;
  159. }
  160. return $this->basicOperation('is_file',$path);
  161. }
  162. public function stat($path){
  163. return $this->basicOperation('stat',$path);
  164. }
  165. public function filetype($path){
  166. return $this->basicOperation('filetype',$path);
  167. }
  168. public function filesize($path){
  169. return $this->basicOperation('filesize',$path);
  170. }
  171. public function readfile($path){
  172. @ob_end_clean();
  173. $handle=$this->fopen($path,'rb');
  174. if ($handle) {
  175. $chunkSize = 8192;// 8 MB chunks
  176. while (!feof($handle)) {
  177. echo fread($handle, $chunkSize);
  178. flush();
  179. }
  180. $size=$this->filesize($path);
  181. return $size;
  182. }
  183. return false;
  184. }
  185. public function is_readable($path){
  186. return $this->basicOperation('is_readable',$path);
  187. }
  188. public function is_writable($path){
  189. return $this->basicOperation('is_writable',$path);
  190. }
  191. public function file_exists($path){
  192. if($path=='/'){
  193. return true;
  194. }
  195. return $this->basicOperation('file_exists',$path);
  196. }
  197. public function filectime($path){
  198. return $this->basicOperation('filectime',$path);
  199. }
  200. public function filemtime($path){
  201. return $this->basicOperation('filemtime',$path);
  202. }
  203. public function touch($path, $mtime=null){
  204. return $this->basicOperation('touch', $path, array('write'), $mtime);
  205. }
  206. public function file_get_contents($path){
  207. return $this->basicOperation('file_get_contents',$path,array('read'));
  208. }
  209. public function file_put_contents($path,$data){
  210. if(is_resource($data)){//not having to deal with streams in file_put_contents makes life easier
  211. $exists=$this->file_exists($path);
  212. $run=true;
  213. if(!$exists){
  214. OC_Hook::emit( OC_Filesystem::CLASSNAME, OC_Filesystem::signal_create, array( OC_Filesystem::signal_param_path => $path, OC_Filesystem::signal_param_run => &$run));
  215. }
  216. OC_Hook::emit( OC_Filesystem::CLASSNAME, OC_Filesystem::signal_write, array( OC_Filesystem::signal_param_path => $path, OC_Filesystem::signal_param_run => &$run));
  217. if(!$run){
  218. return false;
  219. }
  220. $target=$this->fopen($path,'w');
  221. if($target){
  222. $count=OC_Helper::streamCopy($data,$target);
  223. fclose($target);
  224. fclose($data);
  225. if(!$exists){
  226. OC_Hook::emit( OC_Filesystem::CLASSNAME, OC_Filesystem::signal_post_create, array( OC_Filesystem::signal_param_path => $path));
  227. }
  228. OC_Hook::emit( OC_Filesystem::CLASSNAME, OC_Filesystem::signal_post_write, array( OC_Filesystem::signal_param_path => $path));
  229. return $count>0;
  230. }else{
  231. return false;
  232. }
  233. }else{
  234. return $this->basicOperation('file_put_contents',$path,array('create','write'),$data);
  235. }
  236. }
  237. public function unlink($path){
  238. return $this->basicOperation('unlink',$path,array('delete'));
  239. }
  240. public function rename($path1,$path2){
  241. $absolutePath1=$this->getAbsolutePath($path1);
  242. $absolutePath2=$this->getAbsolutePath($path2);
  243. if(OC_FileProxy::runPreProxies('rename',$absolutePath1,$absolutePath2) and OC_Filesystem::isValidPath($path2)){
  244. $path1=$this->getRelativePath($absolutePath1);
  245. $path2=$this->getRelativePath($absolutePath2);
  246. if($path1==null or $path2==null){
  247. return false;
  248. }
  249. $run=true;
  250. 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));
  251. if($run){
  252. $mp1=$this->getMountPoint($path1);
  253. $mp2=$this->getMountPoint($path2);
  254. if($mp1==$mp2){
  255. if($storage=$this->getStorage($path1)){
  256. $result=$storage->rename($this->getInternalPath($path1),$this->getInternalPath($path2));
  257. }
  258. }else{
  259. $source=$this->fopen($path1,'r');
  260. $target=$this->fopen($path2,'w');
  261. $count=OC_Helper::streamCopy($data,$target);
  262. $storage1=$this->getStorage($path1);
  263. $storage1->unlink($this->getInternalPath($path1));
  264. $result=$count>0;
  265. }
  266. OC_Hook::emit( OC_Filesystem::CLASSNAME, OC_Filesystem::signal_post_rename, array( OC_Filesystem::signal_param_oldpath => $path1, OC_Filesystem::signal_param_newpath=>$path2));
  267. return $result;
  268. }
  269. }
  270. }
  271. public function copy($path1,$path2){
  272. $absolutePath1=$this->getAbsolutePath($path1);
  273. $absolutePath2=$this->getAbsolutePath($path2);
  274. if(OC_FileProxy::runPreProxies('copy',$absolutePath1,$absolutePath2) and OC_Filesystem::isValidPath($path2)){
  275. $path1=$this->getRelativePath($absolutePath1);
  276. $path2=$this->getRelativePath($absolutePath2);
  277. if($path1==null or $path2==null){
  278. return false;
  279. }
  280. $run=true;
  281. 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));
  282. $exists=$this->file_exists($path2);
  283. if($run and !$exists){
  284. OC_Hook::emit( OC_Filesystem::CLASSNAME, OC_Filesystem::signal_create, array( OC_Filesystem::signal_param_path => $path2, OC_Filesystem::signal_param_run => &$run));
  285. }
  286. if($run){
  287. OC_Hook::emit( OC_Filesystem::CLASSNAME, OC_Filesystem::signal_write, array( OC_Filesystem::signal_param_path => $path2, OC_Filesystem::signal_param_run => &$run));
  288. }
  289. if($run){
  290. $mp1=$this->getMountPoint($path1);
  291. $mp2=$this->getMountPoint($path2);
  292. if($mp1==$mp2){
  293. if($storage=$this->getStorage($path1)){
  294. $result=$storage->copy($this->getInternalPath($path1),$this->getInternalPath($path2));
  295. }
  296. }else{
  297. $source=$this->fopen($path1,'r');
  298. $target=$this->fopen($path2,'w');
  299. $count=OC_Helper::streamCopy($data,$target);
  300. }
  301. OC_Hook::emit( OC_Filesystem::CLASSNAME, OC_Filesystem::signal_post_copy, array( OC_Filesystem::signal_param_oldpath => $path1 , OC_Filesystem::signal_param_newpath=>$path2));
  302. if(!$exists){
  303. OC_Hook::emit( OC_Filesystem::CLASSNAME, OC_Filesystem::signal_post_create, array( OC_Filesystem::signal_param_path => $path2));
  304. }
  305. OC_Hook::emit( OC_Filesystem::CLASSNAME, OC_Filesystem::signal_post_write, array( OC_Filesystem::signal_param_path => $path2));
  306. return $result;
  307. }
  308. }
  309. }
  310. public function fopen($path,$mode){
  311. $hooks=array();
  312. switch($mode){
  313. case 'r':
  314. case 'rb':
  315. $hooks[]='read';
  316. break;
  317. case 'r+':
  318. case 'rb+':
  319. case 'w+':
  320. case 'wb+':
  321. case 'x+':
  322. case 'xb+':
  323. case 'a+':
  324. case 'ab+':
  325. $hooks[]='read';
  326. $hooks[]='write';
  327. break;
  328. case 'w':
  329. case 'wb':
  330. case 'x':
  331. case 'xb':
  332. case 'a':
  333. case 'ab':
  334. $hooks[]='write';
  335. break;
  336. default:
  337. OC_Log::write('core','invalid mode ('.$mode.') for '.$path,OC_Log::ERROR);
  338. }
  339. return $this->basicOperation('fopen',$path,$hooks,$mode);
  340. }
  341. public function toTmpFile($path){
  342. if(OC_Filesystem::isValidPath($path)){
  343. $source=$this->fopen($path,'r');
  344. if($source){
  345. $extension='';
  346. $extOffset=strpos($path,'.');
  347. if($extOffset !== false) {
  348. $extension=substr($path,strrpos($path,'.'));
  349. }
  350. $tmpFile=OC_Helper::tmpFile($extension);
  351. file_put_contents($tmpFile,$source);
  352. return $tmpFile;
  353. }
  354. }
  355. }
  356. public function fromTmpFile($tmpFile,$path){
  357. if(OC_Filesystem::isValidPath($path)){
  358. if(!$tmpFile){
  359. debug_print_backtrace();
  360. }
  361. $source=fopen($tmpFile,'r');
  362. if($source){
  363. $this->file_put_contents($path,$source);
  364. unlink($tmpFile);
  365. return true;
  366. }else{
  367. }
  368. }else{
  369. return false;
  370. }
  371. }
  372. public function getMimeType($path){
  373. return $this->basicOperation('getMimeType',$path);
  374. }
  375. public function hash($type,$path){
  376. return $this->basicOperation('hash',$path,array('read'));
  377. }
  378. public function free_space($path='/'){
  379. return $this->basicOperation('free_space',$path);
  380. }
  381. /**
  382. * @brief abstraction layer for basic filesystem functions: wrapper for OC_Filestorage
  383. * @param string $operation
  384. * @param string #path
  385. * @param array (optional) hooks
  386. * @param mixed (optional) $extraParam
  387. * @return mixed
  388. *
  389. * This method takes requests for basic filesystem functions (e.g. reading & writing
  390. * files), processes hooks and proxies, sanitises paths, and finally passes them on to
  391. * OC_Filestorage for delegation to a storage backend for execution
  392. */
  393. private function basicOperation($operation,$path,$hooks=array(),$extraParam=null){
  394. $absolutePath=$this->getAbsolutePath($path);
  395. if(OC_FileProxy::runPreProxies($operation,$absolutePath, $extraParam) and OC_Filesystem::isValidPath($path)){
  396. $path=$this->getRelativePath($absolutePath);
  397. if($path==null){
  398. return false;
  399. }
  400. $internalPath=$this->getInternalPath($path);
  401. $run=true;
  402. if(OC_Filesystem::$loaded and $this->fakeRoot==OC_Filesystem::getRoot()){
  403. foreach($hooks as $hook){
  404. if($hook!='read'){
  405. OC_Hook::emit( OC_Filesystem::CLASSNAME, $hook, array( OC_Filesystem::signal_param_path => $path, OC_Filesystem::signal_param_run => &$run));
  406. }else{
  407. OC_Hook::emit( OC_Filesystem::CLASSNAME, $hook, array( OC_Filesystem::signal_param_path => $path));
  408. }
  409. }
  410. }
  411. if($run and $storage=$this->getStorage($path)){
  412. if(!is_null($extraParam)){
  413. $result=$storage->$operation($internalPath,$extraParam);
  414. }else{
  415. $result=$storage->$operation($internalPath);
  416. }
  417. $result=OC_FileProxy::runPostProxies($operation,$this->getAbsolutePath($path),$result);
  418. if(OC_Filesystem::$loaded and $this->fakeRoot==OC_Filesystem::getRoot()){
  419. if($operation!='fopen'){//no post hooks for fopen, the file stream is still open
  420. foreach($hooks as $hook){
  421. if($hook!='read'){
  422. OC_Hook::emit( OC_Filesystem::CLASSNAME, 'post_'.$hook, array( OC_Filesystem::signal_param_path => $path));
  423. }
  424. }
  425. }
  426. }
  427. return $result;
  428. }
  429. }
  430. return null;
  431. }
  432. /**
  433. * check if a file or folder has been updated since $time
  434. * @param int $time
  435. * @return bool
  436. */
  437. public function hasUpdated($path,$time){
  438. return $this->basicOperation('hasUpdated',$path,array(),$time);
  439. }
  440. }