seperate hooks for OC_FILESYSTEM and OC_FILESTORAGE

This commit is contained in:
Robin Appelman 2011-04-18 12:16:47 +02:00
parent 761a517888
commit 67ba9b320e
3 changed files with 86 additions and 24 deletions

View file

@ -80,7 +80,6 @@ require_once('appconfig.php');
require_once('files.php');
require_once('filesystem.php');
require_once('filestorage.php');
require_once('fileobserver.php');
require_once('log.php');
require_once('user.php');
require_once('group.php');

View file

@ -24,11 +24,11 @@
* Privde a common interface to all different storage options
*
* Hooks provided:
* read(path)
* write(path)
* create(path)
* delete(path)
* rename(oldpath,newpath)
* read(storage,path)
* write(storage,path)
* create(storage,path) (when a file is created both, write and create will be emited)
* delete(storage,path)
* rename(storage,oldpath,newpath)
*/
class OC_FILESTORAGE{
public function __construct($parameters){}
@ -85,14 +85,14 @@ class OC_FILESTORAGE_LOCAL extends OC_FILESTORAGE{
}
public function rmdir($path){
if($return=rmdir($this->datadir.$path)){
OC_HOOK::emit( "OC_FILESYSTEM", "delete", array( 'path' => $path));
OC_HOOK::emit( 'OC_FILESTORAGE', "delete", array( 'storage'=>$this, 'path' => $path));
$this->clearFolderSizeCache($path);
}
return $return;
}
public function opendir($path){
if($return=opendir($this->datadir.$path)){
OC_HOOK::emit( "OC_FILESYSTEM", "read", array( 'path' => $path));
OC_HOOK::emit( 'OC_FILESTORAGE', "read", array( 'storage'=>$this, 'path' => $path));
}
return $return;
}
@ -130,7 +130,7 @@ class OC_FILESTORAGE_LOCAL extends OC_FILESTORAGE{
}
public function readfile($path){
if($return=readfile($this->datadir.$path)){
OC_HOOK::emit( "OC_FILESYSTEM", "read", array( 'path' => $path));
OC_HOOK::emit( 'OC_FILESTORAGE', "read", array( 'storage'=>$this, 'path' => $path));
}
return $return;
}
@ -145,26 +145,29 @@ class OC_FILESTORAGE_LOCAL extends OC_FILESTORAGE{
}
public function file_get_contents($path){
if($return=file_get_contents($this->datadir.$path)){
OC_HOOK::emit( "OC_FILESYSTEM", "read", array( 'path' => $path));
OC_HOOK::emit( 'OC_FILESTORAGE', "read", array( 'storage'=>$this, 'path' => $path));
}
return $return;
}
public function file_put_contents($path,$data){
if(!$this->file_exists($path)){
OC_HOOK::emit( 'OC_FILESTORAGE', 'create', array( 'storage'=>$this, 'path' => $path));
}
if($return=file_put_contents($this->datadir.$path,$data)){
OC_HOOK::emit( "OC_FILESYSTEM", "write", array( 'path' => $path));
OC_HOOK::emit( 'OC_FILESTORAGE', "write", array( 'storage'=>$this, 'path' => $path));
$this->clearFolderSizeCache($path);
}
}
public function unlink($path){
if($return=unlink($this->datadir.$path)){
OC_HOOK::emit( "OC_FILESYSTEM", "delete", array( 'path' => $path));
OC_HOOK::emit( 'OC_FILESTORAGE', "delete", array( 'storage'=>$this, 'path' => $path));
$this->clearFolderSizeCache($path);
}
return $return;
}
public function rename($path1,$path2){
if($return=rename($this->datadir.$path1,$this->datadir.$path2)){
OC_HOOK::emit( "OC_FILESYSTEM", "rename", array( 'oldpath' => $path1, 'newpath' => $path2));
OC_HOOK::emit( 'OC_FILESTORAGE', "rename", array( 'storage'=>$this, 'oldpath' => $path1, 'newpath' => $path2));
}
return $return;
}
@ -177,29 +180,36 @@ class OC_FILESTORAGE_LOCAL extends OC_FILESTORAGE{
$path2.=$source;
}
if($return=copy($this->datadir.$path1,$this->datadir.$path2)){
OC_HOOK::emit( "OC_FILESYSTEM", "create", array( 'path' => $path2));
OC_HOOK::emit( 'OC_FILESTORAGE', "create", array( 'storage'=>$this, 'path' => $path2));
$this->clearFolderSizeCache($path);
}
return $return;
}
public function fopen($path,$mode){
$exists=$this->file_exists($path);
if($return=fopen($this->datadir.$path,$mode)){
switch($mode){
case 'r':
OC_HOOK::emit( "OC_FILESYSTEM", "read", array( 'path' => $path));
OC_HOOK::emit( 'OC_FILESTORAGE', "read", array( 'storage'=>$this, 'path' => $path));
break;
case 'r+':
case 'w+':
case 'x+':
case 'a+':
OC_HOOK::emit( "OC_FILESYSTEM", "read", array( 'path' => $path));
OC_HOOK::emit( "OC_FILESYSTEM", "write", array( 'path' => $path));
OC_HOOK::emit( 'OC_FILESTORAGE', "read", array( 'storage'=>$this, 'path' => $path));
OC_HOOK::emit( 'OC_FILESTORAGE', "write", array( 'storage'=>$this, 'path' => $path));
if(!$exists($path)){
OC_HOOK::emit( 'OC_FILESTORAGE', 'create', array( 'storage'=>$this, 'path' => $path));
}
$this->clearFolderSizeCache($path);
break;
case 'w':
case 'x':
case 'a':
OC_HOOK::emit( "OC_FILESYSTEM", "write", array( 'path' => $path));
OC_HOOK::emit( 'OC_FILESTORAGE', "write", array( 'storage'=>$this, 'path' => $path));
if(!$exists($path)){
OC_HOOK::emit( 'OC_FILESTORAGE', 'create', array( 'storage'=>$this, 'path' => $path));
}
$this->clearFolderSizeCache($path);
break;
}
@ -362,7 +372,7 @@ class OC_FILESTORAGE_LOCAL extends OC_FILESTORAGE{
$fileStats = stat($this->datadir.$path);
if(copy($this->datadir.$path,$filename)){
touch($filename, $fileStats['mtime'], $fileStats['atime']);
OC_HOOK::emit( "OC_FILESYSTEM", "read", array( 'path' => $path));
OC_HOOK::emit( 'OC_FILESTORAGE', "read", array( 'storage'=>$this, 'path' => $path));
return $filename;
}else{
return false;
@ -373,7 +383,7 @@ class OC_FILESTORAGE_LOCAL extends OC_FILESTORAGE{
$fileStats = stat($tmpFile);
if(rename($tmpFile,$this->datadir.$path)){
touch($this->datadir.$path, $fileStats['mtime'], $fileStats['atime']);
OC_HOOK::emit( "OC_FILESYSTEM", "create", array( 'path' => $path));
OC_HOOK::emit( 'OC_FILESTORAGE', "create", array( 'storage'=>$this, 'path' => $path));
$this->clearFolderSizeCache($path);
return true;
}else{
@ -385,7 +395,7 @@ class OC_FILESTORAGE_LOCAL extends OC_FILESTORAGE{
$fileStats = stat($tmpFile);
if(move_uploaded_file($tmpFile,$this->datadir.$path)){
touch($this->datadir.$path, $fileStats['mtime'], $fileStats['atime']);
OC_HOOK::emit( "OC_FILESYSTEM", "create", array( 'path' => $path));
OC_HOOK::emit( 'OC_FILESTORAGE', "create", array( 'storage'=>$this, 'path' => $path));
$this->clearFolderSizeCache($path);
return true;
}else{
@ -402,7 +412,7 @@ class OC_FILESTORAGE_LOCAL extends OC_FILESTORAGE{
if ($item == '.' || $item == '..') continue;
if(is_file($dir.'/'.$item)){
if(unlink($dir.'/'.$item)){
OC_HOOK::emit( "OC_FILESYSTEM", "delete", array( 'path' => $dir.'/'.$item));
OC_HOOK::emit( 'OC_FILESTORAGE', "delete", array( 'storage'=>$this, 'path' => $dir.'/'.$item));
$this->clearFolderSizeCache($path);
}
}elseif(is_dir($dir.'/'.$item)){
@ -412,7 +422,7 @@ class OC_FILESTORAGE_LOCAL extends OC_FILESTORAGE{
}
}
if($return=rmdir($dir)){
OC_HOOK::emit( "OC_FILESYSTEM", "delete", array( 'path' => $dir));
OC_HOOK::emit( 'OC_FILESTORAGE', "delete", array( 'storage'=>$this, 'path' => $dir));
$this->clearFolderSizeCache($path);
}
return $return;
@ -450,7 +460,7 @@ class OC_FILESTORAGE_LOCAL extends OC_FILESTORAGE{
public function hash($type,$path,$raw){
if($return=hash_file($type,$this->datadir.$path,$raw)){
OC_HOOK::emit( "OC_FILESYSTEM", "read", array( 'path' => $path));
OC_HOOK::emit( 'OC_FILESTORAGE', "read", array( 'storage'=>$this, 'path' => $path));
}
return $return;
}

View file

@ -26,6 +26,13 @@
* Class for abstraction of filesystem functions
* This class won't call any filesystem functions for itself but but will pass them to the correct OC_FILESTORAGE object
* this class should also handle all the file premission related stuff
*
* Hooks provided:
* read(path)
* write(path)
* create(path) (when a file is created both, write and create will be emited)
* delete(path)
* rename(oldpath,newpath)
*/
class OC_FILESYSTEM{
static private $storages=array();
@ -191,16 +198,19 @@ class OC_FILESYSTEM{
static public function mkdir($path){
$parent=substr($path,0,strrpos($path,'/'));
if(self::canWrite($parent) and $storage=self::getStorage($path)){
OC_HOOK::emit( 'OC_FILESYSTEM', 'create', array( 'path' => $path));
return $storage->mkdir(self::getInternalPath($path));
}
}
static public function rmdir($path){
if(self::canWrite($path) and $storage=self::getStorage($path)){
OC_HOOK::emit( 'OC_FILESYSTEM', 'delete', array( 'path' => $path));
return $storage->rmdir(self::getInternalPath($path));
}
}
static public function opendir($path){
if(self::canRead($path) and $storage=self::getStorage($path)){
OC_HOOK::emit( 'OC_FILESYSTEM', 'read', array( 'path' => $path));
return $storage->opendir(self::getInternalPath($path));
}
}
@ -237,6 +247,7 @@ class OC_FILESYSTEM{
}
static public function readfile($path){
if(self::canRead($path) and $storage=self::getStorage($path)){
OC_HOOK::emit( 'OC_FILESYSTEM', 'read', array( 'path' => $path));
return $storage->readfile(self::getInternalPath($path));
}
}
@ -278,16 +289,22 @@ class OC_FILESYSTEM{
}
static public function file_get_contents($path){
if(self::canRead($path) and $storage=self::getStorage($path)){
OC_HOOK::emit( 'OC_FILESYSTEM', 'read', array( 'path' => $path));
return $storage->file_get_contents(self::getInternalPath($path));
}
}
static public function file_put_contents($path,$data){
if(self::canWrite($path) and $storage=self::getStorage($path)){
OC_HOOK::emit( 'OC_FILESYSTEM', 'write', array( 'path' => $path));
if(!self::file_exists($path)){
OC_HOOK::emit( 'OC_FILESYSTEM', 'create', array( 'path' => $path));
}
return $storage->file_put_contents(self::getInternalPath($path),$data);
}
}
static public function unlink($path){
if(self::canWrite($path) and $storage=self::getStorage($path)){
OC_HOOK::emit( 'OC_FILESYSTEM', 'delete', array( 'path' => $path));
return $storage->unlink(self::getInternalPath($path));
}
}
@ -305,6 +322,7 @@ class OC_FILESYSTEM{
$storage1->unlink(self::getInternalPath($path1));
return $result;
}
OC_HOOK::emit( 'OC_FILESYSTEM', 'rename', array( 'oldpath' => $path1 ,'newpath'=>$path2));
}
}
static public function copy($path1,$path2){
@ -319,28 +337,61 @@ class OC_FILESYSTEM{
$tmpFile=$storage1->toTmpFile(self::getInternalPath($path1));
return $storage2->fromTmpFile(self::getInternalPath($path2));
}
OC_HOOK::emit( 'OC_FILESYSTEM', 'create', array( 'path' => $path2));
}
}
static public function fopen($path,$mode){
$allowed=((strpos($path,'r')===false and strpos($path,'r+')!==false and self::canRead) or self::canWrite($path));
if($allowed){
if($storage=self::getStorage($path)){
switch($mode){
case 'r':
OC_HOOK::emit( 'OC_FILESYSTEM', 'read', array( 'path' => $path));
break;
case 'r+':
case 'w+':
case 'x+':
case 'a+':
OC_HOOK::emit( 'OC_FILESYSTEM', 'read', array( 'path' => $path));
OC_HOOK::emit( 'OC_FILESYSTEM', 'write', array( 'path' => $path));
if(!self::file_exists($path)){
OC_HOOK::emit( 'OC_FILESYSTEM', 'create', array( 'path' => $path));
}
break;
case 'w':
case 'x':
case 'a':
OC_HOOK::emit( 'OC_FILESYSTEM', 'write', array( 'path' => $path));
if(!self::file_exists($path)){
OC_HOOK::emit( 'OC_FILESYSTEM', 'create', array( 'path' => $path));
}
break;
}
return $storage->fopen(self::getInternalPath($path),$mode);
}
}
}
static public function toTmpFile($path){
if(self::canRead($path) and $storage=self::getStorage($path)){
OC_HOOK::emit( 'OC_FILESYSTEM', 'read', array( 'path' => $path));
return $storage->toTmpFile(self::getInternalPath($path));
}
}
static public function fromTmpFile($tmpFile,$path){
if(self::canWrite($path) and $storage=self::getStorage($path)){
OC_HOOK::emit( 'OC_FILESYSTEM', 'write', array( 'path' => $path));
if(!self::file_exists($path)){
OC_HOOK::emit( 'OC_FILESYSTEM', 'create', array( 'path' => $path));
}
return $storage->fromTmpFile($tmpFile,self::getInternalPath($path));
}
}
static public function fromUploadedFile($tmpFile,$path){
if(self::canWrite($path) and $storage=self::getStorage($path)){
OC_HOOK::emit( 'OC_FILESYSTEM', 'write', array( 'path' => $path));
if(!self::file_exists($path)){
OC_HOOK::emit( 'OC_FILESYSTEM', 'create', array( 'path' => $path));
}
return $storage->fromUploadedFile($tmpFile,self::getInternalPath($path));
}
}
@ -351,6 +402,7 @@ class OC_FILESYSTEM{
}
static public function delTree($path){
if(self::canWrite($path) and $storage=self::getStorage($path)){
OC_HOOK::emit( 'OC_FILESYSTEM', 'delete', array( 'path' => $path));
return $storage->delTree(self::getInternalPath($path));
}
}
@ -383,6 +435,7 @@ class OC_FILESYSTEM{
}
static public function hash($type,$path,$raw=false){
if(self::canRead($path) and $storage=self::getStorage($path)){
OC_HOOK::emit( 'OC_FILESYSTEM', 'read', array( 'path' => $path));
return $storage->hash($type,self::getInternalPath($path),$raw);
}
}