From 5162809c8afd722a00de90390f94e173cac8431a Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Mon, 6 Sep 2010 20:02:17 +0200 Subject: [PATCH] make the filesystem configurable (no gui yet) --- inc/lib_base.php | 27 ++++++++++++++++++++--- inc/lib_filestorage.php | 2 ++ inc/lib_filesystem.php | 47 +++++++++++++++++++++++++++++++++++++++++ webdav/owncloud.php | 18 +--------------- 4 files changed, 74 insertions(+), 20 deletions(-) diff --git a/inc/lib_base.php b/inc/lib_base.php index f989bc4dbd..0f355eb348 100644 --- a/inc/lib_base.php +++ b/inc/lib_base.php @@ -111,17 +111,23 @@ $loginresult=OC_USER::loginlisener(); */ class OC_UTIL { public static $scripts=array(); + private static $fsSetup=false; public static function setupFS(){// configure the initial filesystem based on the configuration + if(self::$fsSetup){//setting up the filesystem twice can only lead to trouble + return false; + } global $CONFIG_DATADIRECTORY_ROOT; global $CONFIG_DATADIRECTORY; global $CONFIG_BACKUPDIRECTORY; global $CONFIG_ENABLEBACKUP; + global $CONFIG_FILESYSTEM; if(!is_dir($CONFIG_DATADIRECTORY_ROOT)){ @mkdir($CONFIG_DATADIRECTORY_ROOT) or die("Can't create data directory ($CONFIG_DATADIRECTORY_ROOT), you can usually fix this by setting the owner of '$SERVERROOT' to the user that the web server uses (www-data for debian/ubuntu)"); } - if(OC_USER::isLoggedIn()){ - $rootStorage=new OC_FILESTORAGE_LOCAL(array('datadir'=>$CONFIG_DATADIRECTORY)); + if(OC_USER::isLoggedIn()){ //if we aren't logged in, there is no use to set up the filesystem + //first set up the local "root" storage and the backupstorage if needed + $rootStorage=OC_FILESYSTEM::createStorage('local',array('datadir'=>$CONFIG_DATADIRECTORY)); if($CONFIG_ENABLEBACKUP){ if(!is_dir($CONFIG_BACKUPDIRECTORY)){ mkdir($CONFIG_BACKUPDIRECTORY); @@ -129,7 +135,7 @@ class OC_UTIL { if(!is_dir($CONFIG_BACKUPDIRECTORY.'/'.$_SESSION['username_clean'])){ mkdir($CONFIG_BACKUPDIRECTORY.'/'.$_SESSION['username_clean']); } - $backupStorage=new OC_FILESTORAGE_LOCAL(array('datadir'=>$CONFIG_BACKUPDIRECTORY)); + $backupStorage=OC_FILESYSTEM::createStorage('local',array('datadir'=>$CONFIG_BACKUPDIRECTORY)); $backup=new OC_FILEOBSERVER_BACKUP(array('storage'=>$backupStorage)); $rootStorage->addObserver($backup); } @@ -140,7 +146,22 @@ class OC_UTIL { mkdir($CONFIG_DATADIRECTORY); } + //set up the other storages according to the system settings + foreach($CONFIG_FILESYSTEM as $storageConfig){ + if(OC_FILESYSTEM::hasStorageType($storageConfig['type'])){ + $arguments=$storageConfig; + unset($arguments['type']); + unset($arguments['mountpoint']); + $storage=OC_FILESYSTEM::createStorage($storageConfig['type'],$arguments); + if($storage){ + OC_FILESYSTEM::mount($storage,$storageConfig['mountpoint']); + } + } + } + + //jail the user into his "home" directory OC_FILESYSTEM::chroot('/'.$_SESSION['username_clean']); + self::$fsSetup=true; } } diff --git a/inc/lib_filestorage.php b/inc/lib_filestorage.php index b63dd200b3..62532916ca 100644 --- a/inc/lib_filestorage.php +++ b/inc/lib_filestorage.php @@ -75,6 +75,8 @@ class OC_FILESTORAGE{ public function getTree($path){} } + +OC_FILESYSTEM::registerStorageType('local','OC_FILESTORAGE_LOCAL',array('datadir'=>'string')); /** * for local filestore, we only have to map the paths */ diff --git a/inc/lib_filesystem.php b/inc/lib_filesystem.php index 3a2373f166..263b1d7a41 100644 --- a/inc/lib_filesystem.php +++ b/inc/lib_filesystem.php @@ -30,6 +30,53 @@ class OC_FILESYSTEM{ static private $storages=array(); static private $fakeRoot=''; + static private $storageTypes=array(); + + + /** + * register a storage type + * @param string type + * @param string classname + * @param array arguments an associative array in the form of name=>type (eg array('datadir'=>'string')) + */ + static public function registerStorageType($type,$classname,$arguments){ + self::$storageTypes[$type]=array('type'=>$type,'classname'=>$classname,'arguments'=>$arguments); + } + + /** + * check if the filesystem supports a specific storagetype + * @param string type + * @return bool + */ + static public function hasStorageType($type){ + return isset(self::$storageTypes[$type]); + } + + /** + * get the list of names of storagetypes that the filesystem supports + * @return array + */ + static public function getStorageTypeNames(){ + return array_keys(self::$storageTypes); + } + + /** + * create a new storage of a specific type + * @param string type + * @param array arguments + * @return OC_FILESTORAGE + */ + static public function createStorage($type,$arguments){ + if(!self::hasStorageType($type)){ + return false; + } + $className=self::$storageTypes[$type]['classname']; + if(class_exists($className)){ + return new $className($arguments); + }else{ + return false; + } + } /** * change the root to a fake toor diff --git a/webdav/owncloud.php b/webdav/owncloud.php index 575e9b43a9..387eae3c19 100644 --- a/webdav/owncloud.php +++ b/webdav/owncloud.php @@ -39,23 +39,7 @@ if(empty($_SERVER['PHP_AUTH_USER']) && empty($_SERVER['REDIRECT_REMOTE_USER'])) $user=$_SERVER['PHP_AUTH_USER']; $passwd=$_SERVER['PHP_AUTH_PW']; if(OC_USER::login($user,$passwd)){ - $CONFIG_DATADIRECTORY=$CONFIG_DATADIRECTORY_ROOT.'/'.$user; - if(!is_dir($CONFIG_DATADIRECTORY)){ - mkdir($CONFIG_DATADIRECTORY); - } - $rootStorage=new OC_FILESTORAGE_LOCAL(array('datadir'=>$CONFIG_DATADIRECTORY)); - if($CONFIG_ENABLEBACKUP){ - if(!is_dir($CONFIG_BACKUPDIRECTORY)){ - mkdir($CONFIG_BACKUPDIRECTORY); - } - if(!is_dir($CONFIG_BACKUPDIRECTORY.'/'.$user)){ - mkdir($CONFIG_BACKUPDIRECTORY.'/'.$user); - } - $backupStorage=new OC_FILESTORAGE_LOCAL(array('datadir'=>$CONFIG_BACKUPDIRECTORY.'/'.$user)); - $backup=new OC_FILEOBSERVER_BACKUP(array('storage'=>$backupStorage)); - $rootStorage->addObserver($backup); - } - OC_FILESYSTEM::mount($rootStorage,'/'); + OC_UTIL::setUpFS(); $server = new HTTP_WebDAV_Server_Filesystem(); $server->db_name = $CONFIG_DBNAME; $server->ServeRequest($CONFIG_DATADIRECTORY);