You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

filesystem.php 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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. /**
  23. * Class for abstraction of filesystem functions
  24. * This class won't call any filesystem functions for itself but but will pass them to the correct OC_Filestorage object
  25. * this class should also handle all the file premission related stuff
  26. *
  27. * Hooks provided:
  28. * read(path)
  29. * write(path, &run)
  30. * post_write(path)
  31. * create(path, &run) (when a file is created, both create and write will be emited in that order)
  32. * post_create(path)
  33. * delete(path, &run)
  34. * post_delete(path)
  35. * rename(oldpath,newpath, &run)
  36. * post_rename(oldpath,newpath)
  37. * copy(oldpath,newpath, &run) (if the newpath doesn't exists yes, copy, create and write will be emited in that order)
  38. * post_rename(oldpath,newpath)
  39. *
  40. * the &run parameter can be set to false to prevent the operation from occuring
  41. */
  42. class OC_Filesystem{
  43. static private $storages=array();
  44. static private $mounts=array();
  45. static private $fakeRoot='';
  46. static private $storageTypes=array();
  47. /**
  48. * register a storage type
  49. * @param string type
  50. * @param string classname
  51. * @param array arguments an associative array in the form of name=>type (eg array('datadir'=>'string'))
  52. */
  53. static public function registerStorageType($type,$classname,$arguments){
  54. self::$storageTypes[$type]=array('type'=>$type,'classname'=>$classname,'arguments'=>$arguments);
  55. }
  56. /**
  57. * check if the filesystem supports a specific storagetype
  58. * @param string type
  59. * @return bool
  60. */
  61. static public function hasStorageType($type){
  62. return isset(self::$storageTypes[$type]);
  63. }
  64. /**
  65. * get the list of names of storagetypes that the filesystem supports
  66. * @return array
  67. */
  68. static public function getStorageTypeNames(){
  69. return array_keys(self::$storageTypes);
  70. }
  71. /**
  72. * tear down the filesystem, removing all storage providers
  73. */
  74. static public function tearDown(){
  75. foreach(self::$storages as $mountpoint=>$storage){
  76. unset(self::$storages[$mountpoint]);
  77. }
  78. $fakeRoot='';
  79. }
  80. /**
  81. * create a new storage of a specific type
  82. * @param string type
  83. * @param array arguments
  84. * @return OC_Filestorage
  85. */
  86. static private function createStorage($type,$arguments){
  87. if(!self::hasStorageType($type)){
  88. return false;
  89. }
  90. $className=self::$storageTypes[$type]['classname'];
  91. if(class_exists($className)){
  92. return new $className($arguments);
  93. }else{
  94. return false;
  95. }
  96. }
  97. /**
  98. * change the root to a fake toor
  99. * @param string fakeRoot
  100. * @return bool
  101. */
  102. static public function chroot($fakeRoot){
  103. if(!$fakeRoot==''){
  104. if($fakeRoot[0]!=='/'){
  105. $fakeRoot='/'.$fakeRoot;
  106. }
  107. }
  108. self::$fakeRoot=$fakeRoot;
  109. }
  110. /**
  111. * get the part of the path relative to the mountpoint of the storage it's stored in
  112. * @param string path
  113. * @return bool
  114. */
  115. static public function getInternalPath($path){
  116. $mountPoint=self::getMountPoint($path);
  117. $path=self::$fakeRoot.$path;
  118. $internalPath=substr($path,strlen($mountPoint));
  119. return $internalPath;
  120. }
  121. /**
  122. * check if the current users has the right premissions to read a file
  123. * @param string path
  124. * @return bool
  125. */
  126. static private function canRead($path){
  127. if(substr($path,0,1)!=='/'){
  128. $path='/'.$path;
  129. }
  130. if(strstr($path,'/../') || strrchr($path, '/') === '/..' ){
  131. return false;
  132. }
  133. return true;//dummy untill premissions are correctly implemented, also the correcty value because for now users are locked in their seperate data dir and can read/write everything in there
  134. }
  135. /**
  136. * check if the current users has the right premissions to write a file
  137. * @param string path
  138. * @return bool
  139. */
  140. static private function canWrite($path){
  141. if(substr($path,0,1)!=='/'){
  142. $path='/'.$path;
  143. }
  144. if(strstr($path,'/../') || strrchr($path, '/') === '/..' ){
  145. return false;
  146. }
  147. return true;//dummy untill premissions are correctly implemented, also the correcty value because for now users are locked in their seperate data dir and can read/write everything in there
  148. }
  149. /**
  150. * mount an OC_Filestorage in our virtual filesystem
  151. * @param OC_Filestorage storage
  152. * @param string mountpoint
  153. */
  154. static public function mount($type,$arguments,$mountpoint){
  155. if(substr($mountpoint,0,1)!=='/'){
  156. $mountpoint='/'.$mountpoint;
  157. }
  158. self::$mounts[$mountpoint]=array('type'=>$type,'arguments'=>$arguments);
  159. }
  160. /**
  161. * get the storage object for a path
  162. * @param string path
  163. * @return OC_Filestorage
  164. */
  165. static public function getStorage($path){
  166. $mountpoint=self::getMountPoint($path);
  167. if($mountpoint){
  168. if(!isset(self::$storages[$mountpoint])){
  169. $mount=self::$mounts[$mountpoint];
  170. self::$storages[$mountpoint]=self::createStorage($mount['type'],$mount['arguments']);
  171. }
  172. return self::$storages[$mountpoint];
  173. }
  174. }
  175. /**
  176. * get the mountpoint of the storage object for a path
  177. ( 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
  178. *
  179. * @param string path
  180. * @return string
  181. */
  182. static public function getMountPoint($path){
  183. if(!$path){
  184. $path='/';
  185. }
  186. if(substr($path,0,1)!=='/'){
  187. $path='/'.$path;
  188. }
  189. if(substr($path,-1)!=='/'){
  190. $path=$path.'/';
  191. }
  192. $path=self::$fakeRoot.$path;
  193. $foundMountPoint='';
  194. foreach(self::$mounts as $mountpoint=>$storage){
  195. if(substr($mountpoint,-1)!=='/'){
  196. $mountpoint=$mountpoint.'/';
  197. }
  198. if($mountpoint==$path){
  199. return $mountpoint;
  200. }
  201. if(strpos($path,$mountpoint)===0 and strlen($mountpoint)>strlen($foundMountPoint)){
  202. $foundMountPoint=$mountpoint;
  203. }
  204. }
  205. return $foundMountPoint;
  206. }
  207. /**
  208. * return the path to a local version of the file
  209. * 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
  210. * @param string path
  211. * @return string
  212. */
  213. static public function getLocalFile($path){
  214. $parent=substr($path,0,strrpos($path,'/'));
  215. if(self::canRead($parent) and $storage=self::getStorage($path)){
  216. return $storage->getLocalFile(self::getInternalPath($path));
  217. }
  218. }
  219. static public function mkdir($path){
  220. return self::basicOperation('mkdir',$path,array('create','write'));
  221. }
  222. static public function rmdir($path){
  223. return self::basicOperation('rmdir',$path,array('delete'));
  224. }
  225. static public function opendir($path){
  226. return self::basicOperation('opendir',$path,array('read'));
  227. }
  228. static public function is_dir($path){
  229. if($path=='/'){
  230. return true;
  231. }
  232. return self::basicOperation('is_dir',$path);
  233. }
  234. static public function is_file($path){
  235. if($path=='/'){
  236. return false;
  237. }
  238. return self::basicOperation('is_file',$path);
  239. }
  240. static public function stat($path){
  241. return self::basicOperation('stat',$path);
  242. }
  243. static public function filetype($path){
  244. return self::basicOperation('filetype',$path);
  245. }
  246. static public function filesize($path){
  247. return self::basicOperation('filesize',$path);
  248. }
  249. static public function readfile($path){
  250. return self::basicOperation('readfile',$path,array('read'));
  251. }
  252. static public function is_readable($path){
  253. return self::basicOperation('is_readable',$path);
  254. }
  255. static public function is_writeable($path){
  256. return self::basicOperation('is_writeable',$path);
  257. }
  258. static public function file_exists($path){
  259. if($path=='/'){
  260. return true;
  261. }
  262. return self::basicOperation('file_exists',$path);
  263. }
  264. static public function filectime($path){
  265. return self::basicOperation('filectime',$path);
  266. }
  267. static public function filemtime($path){
  268. return self::basicOperation('filemtime',$path);
  269. }
  270. static public function fileatime($path){
  271. return self::basicOperation('fileatime',$path);
  272. }
  273. static public function file_get_contents($path){
  274. return self::basicOperation('file_get_contents',$path,array('read'));
  275. }
  276. static public function file_put_contents($path,$data){
  277. return self::basicOperation('file_put_contents',$path,array('create','write'),$data);
  278. }
  279. static public function unlink($path){
  280. return self::basicOperation('unlink',$path,array('delete'));
  281. }
  282. static public function rename($path1,$path2){
  283. if(OC_FileProxy::runPreProxies('rename',$path1,$path2) and self::canWrite($path1) and self::canWrite($path2)){
  284. $run=true;
  285. OC_Hook::emit( 'OC_Filesystem', 'rename', array( 'oldpath' => $path1 ,'newpath'=>$path2, 'run' => &$run));
  286. if($run){
  287. $mp1=self::getMountPoint($path1);
  288. $mp2=self::getMountPoint($path2);
  289. if($mp1==$mp2){
  290. if($storage=self::getStorage($path1)){
  291. $result=$storage->rename(self::getInternalPath($path1),self::getInternalPath($path2));
  292. }
  293. }elseif($storage1=self::getStorage($path1) and $storage2=self::getStorage($path2)){
  294. $tmpFile=$storage1->toTmpFile(self::getInternalPath($path1));
  295. $result=$storage2->fromTmpFile($tmpFile,self::getInternalPath($path2));
  296. $storage1->unlink(self::getInternalPath($path1));
  297. }
  298. OC_Hook::emit( 'OC_Filesystem', 'post_rename', array( 'oldpath' => $path1, 'newpath'=>$path2));
  299. return $result;
  300. }
  301. }
  302. }
  303. static public function copy($path1,$path2){
  304. if(OC_FileProxy::runPreProxies('copy',$path1,$path2) and self::canRead($path1) and self::canWrite($path2)){
  305. $run=true;
  306. OC_Hook::emit( 'OC_Filesystem', 'copy', array( 'oldpath' => $path1 ,'newpath'=>$path2, 'run' => &$run));
  307. $exists=self::file_exists($path2);
  308. if($run and !$exists){
  309. OC_Hook::emit( 'OC_Filesystem', 'create', array( 'path' => $path2, 'run' => &$run));
  310. }
  311. if($run){
  312. OC_Hook::emit( 'OC_Filesystem', 'write', array( 'path' => $path2, 'run' => &$run));
  313. }
  314. if($run){
  315. $mp1=self::getMountPoint($path1);
  316. $mp2=self::getMountPoint($path2);
  317. if($mp1==$mp2){
  318. if($storage=self::getStorage($path1)){
  319. $result=$storage->copy(self::getInternalPath($path1),self::getInternalPath($path2));
  320. }
  321. }elseif($storage1=self::getStorage($path1) and $storage2=self::getStorage($path2)){
  322. $tmpFile=$storage1->toTmpFile(self::getInternalPath($path1));
  323. $result=$storage2->fromTmpFile($tmpFile,self::getInternalPath($path2));
  324. }
  325. OC_Hook::emit( 'OC_Filesystem', 'post_copy', array( 'oldpath' => $path1 ,'newpath'=>$path2));
  326. if(!$exists){
  327. OC_Hook::emit( 'OC_Filesystem', 'post_create', array( 'path' => $path2));
  328. }
  329. OC_Hook::emit( 'OC_Filesystem', 'post_write', array( 'path' => $path2));
  330. return $result;
  331. }
  332. }
  333. }
  334. static public function fopen($path,$mode){
  335. $hooks=array();
  336. switch($mode){
  337. case 'r':
  338. $hooks[]='read';
  339. break;
  340. case 'r+':
  341. case 'w+':
  342. case 'x+':
  343. case 'a+':
  344. $hooks[]='read';
  345. $hooks[]='write';
  346. break;
  347. case 'w':
  348. case 'x':
  349. case 'a':
  350. $hooks[]='write';
  351. break;
  352. }
  353. return self::basicOperation('fopen',$path,$hooks);
  354. }
  355. static public function toTmpFile($path){
  356. if(OC_FileProxy::runPreProxies('toTmpFile',$path) and self::canRead($path) and $storage=self::getStorage($path)){
  357. OC_Hook::emit( 'OC_Filesystem', 'read', array( 'path' => $path));
  358. return $storage->toTmpFile(self::getInternalPath($path));
  359. }
  360. }
  361. static public function fromTmpFile($tmpFile,$path){
  362. if(OC_FileProxy::runPreProxies('copy',$tmpFile,$path) and self::canWrite($path) and $storage=self::getStorage($path)){
  363. $run=true;
  364. $exists=self::file_exists($path);
  365. if(!$exists){
  366. OC_Hook::emit( 'OC_Filesystem', 'create', array( 'path' => $path, 'run' => &$run));
  367. }
  368. if($run){
  369. OC_Hook::emit( 'OC_Filesystem', 'write', array( 'path' => $path, 'run' => &$run));
  370. }
  371. if($run){
  372. $result=$storage->fromTmpFile($tmpFile,self::getInternalPath($path));
  373. if(!$exists){
  374. OC_Hook::emit( 'OC_Filesystem', 'post_create', array( 'path' => $path));
  375. }
  376. OC_Hook::emit( 'OC_Filesystem', 'post_write', array( 'path' => $path));
  377. return $result;
  378. }
  379. }
  380. }
  381. static public function fromUploadedFile($tmpFile,$path){
  382. if(OC_FileProxy::runPreProxies('fromUploadedFile',$tmpFile,$path) and self::canWrite($path) and $storage=self::getStorage($path)){
  383. $run=true;
  384. $exists=self::file_exists($path);
  385. if(!$exists){
  386. OC_Hook::emit( 'OC_Filesystem', 'create', array( 'path' => $path, 'run' => &$run));
  387. }
  388. if($run){
  389. OC_Hook::emit( 'OC_Filesystem', 'write', array( 'path' => $path, 'run' => &$run));
  390. }
  391. if($run){
  392. $result=$storage->fromUploadedFile($tmpFile,self::getInternalPath($path));
  393. if(!$exists){
  394. OC_Hook::emit( 'OC_Filesystem', 'post_create', array( 'path' => $path));
  395. }
  396. OC_Hook::emit( 'OC_Filesystem', 'post_write', array( 'path' => $path));
  397. return $result;
  398. }
  399. }
  400. }
  401. static public function getMimeType($path){
  402. return self::basicOperation('getMimeType',$path);
  403. }
  404. static public function hash($type,$path){
  405. return self::basicOperation('hash',$path,array('read'));
  406. }
  407. static public function free_space($path='/'){
  408. return self::basicOperation('free_space',$path);
  409. }
  410. static public function search($query){
  411. $files=array();
  412. $fakeRoot=self::$fakeRoot;
  413. $fakeRootLength=strlen($fakeRoot);
  414. foreach(self::$storages as $mountpoint=>$storage){
  415. $results=$storage->search($query);
  416. if(is_array($results)){
  417. foreach($results as $result){
  418. $file=str_replace('//','/',$mountpoint.$result);
  419. if(substr($file,0,$fakeRootLength)==$fakeRoot){
  420. $file=substr($file,$fakeRootLength);
  421. $files[]=$file;
  422. }
  423. }
  424. }
  425. }
  426. return $files;
  427. }
  428. static public function update_session_file_hash($sessionname,$sessionvalue){
  429. $_SESSION[$sessionname] = $sessionvalue;
  430. }
  431. /**
  432. * abstraction for running most basic operations
  433. * @param string $operation
  434. * @param string #path
  435. * @param array (optional) hooks
  436. * @param mixed (optional) $extraParam
  437. * @return mixed
  438. */
  439. private static function basicOperation($operation,$path,$hooks=array(),$extraParam=null){
  440. if(OC_FileProxy::runPreProxies($operation,$path, $extraParam) and self::canRead($path) and $storage=self::getStorage($path)){
  441. $interalPath=self::getInternalPath($path);
  442. $run=true;
  443. foreach($hooks as $hook){
  444. if($hook!='read'){
  445. OC_Hook::emit( 'OC_Filesystem', $hook, array( 'path' => $path, 'run' => &$run));
  446. }else{
  447. OC_Hook::emit( 'OC_Filesystem', $hook, array( 'path' => $path));
  448. }
  449. }
  450. if($run){
  451. if($extraParam){
  452. $result=$storage->$operation($interalPath,$extraParam);
  453. }else{
  454. $result=$storage->$operation($interalPath);
  455. }
  456. $result=OC_FileProxy::runPostProxies($operation,$path,$result);
  457. foreach($hooks as $hook){
  458. if($hook!='read'){
  459. OC_Hook::emit( 'OC_Filesystem', 'post_'.$hook, array( 'path' => $path));
  460. }
  461. }
  462. return $result;
  463. }
  464. }
  465. return null;
  466. }
  467. }