From f7da4280ca1512a4a8224d28e3f6fd954c3b7205 Mon Sep 17 00:00:00 2001 From: Philipp Kapfer Date: Fri, 8 Mar 2013 18:15:20 +0100 Subject: [PATCH 1/9] Modified app to dynamically register backends instead of hard-coding them Moved dependency check to the individual backends instead of the config Conflicts: apps/files_external/appinfo/app.php apps/files_external/lib/config.php --- apps/files_external/appinfo/app.php | 75 ++++++++++++ apps/files_external/lib/config.php | 184 ++++------------------------ apps/files_external/lib/ftp.php | 12 ++ apps/files_external/lib/smb.php | 14 +++ 4 files changed, 128 insertions(+), 157 deletions(-) diff --git a/apps/files_external/appinfo/app.php b/apps/files_external/appinfo/app.php index aeb7a2cb23a..d6238932cdb 100644 --- a/apps/files_external/appinfo/app.php +++ b/apps/files_external/appinfo/app.php @@ -31,3 +31,78 @@ OCP\Util::connectHook('OC_Filesystem', 'post_initMountPoints', '\OC_Mount_Config OCP\Util::connectHook('OC_User', 'post_login', 'OC\Files\Storage\iRODS', 'login'); OCP\Util::connectHook('OC_User', 'post_login', 'OC\Files\Storage\SMB_OC', 'login'); +OC_Mount_Config::registerBackend('\OC\Files\Storage\Local', array( + 'backend' => 'Local', + 'configuration' => array( + 'datadir' => 'Location'))); + +OC_Mount_Config::registerBackend('\OC\Files\Storage\AmazonS3', array( + 'backend' => 'Amazon S3', + 'configuration' => array( + 'key' => 'Key', + 'secret' => '*Secret', + 'bucket' => 'Bucket'))); + +OC_Mount_Config::registerBackend('\OC\Files\Storage\Dropbox', array( + 'backend' => 'Dropbox', + 'configuration' => array( + 'configured' => '#configured', + 'app_key' => 'App key', + 'app_secret' => 'App secret', + 'token' => '#token', + 'token_secret' => '#token_secret'), + 'custom' => 'dropbox')); + +OC_Mount_Config::registerBackend('\OC\Files\Storage\FTP', array( + 'backend' => 'FTP', + 'configuration' => array( + 'host' => 'URL', + 'user' => 'Username', + 'password' => '*Password', + 'root' => '&Root', + 'secure' => '!Secure ftps://'), + 'has_dependencies' => true)); + +OC_Mount_Config::registerBackend('\OC\Files\Storage\Google', array( + 'backend' => 'Google Drive', + 'configuration' => array( + 'configured' => '#configured', + 'token' => '#token', + 'token_secret' => '#token secret'), + 'custom' => 'google')); + +OC_Mount_Config::registerBackend('\OC\Files\Storage\SWIFT', array( + 'backend' => 'OpenStack Swift', + 'configuration' => array( + 'host' => 'URL', + 'user' => 'Username', + 'token' => '*Token', + 'root' => '&Root', + 'secure' => '!Secure ftps://'))); + +OC_Mount_Config::registerBackend('\OC\Files\Storage\SMB', array( + 'backend' => 'SMB / CIFS', + 'configuration' => array( + 'host' => 'URL', + 'user' => 'Username', + 'password' => '*Password', + 'share' => 'Share', + 'root' => '&Root'), + 'has_dependencies' => true)); + +OC_Mount_Config::registerBackend('\OC\Files\Storage\DAV', array( + 'backend' => 'ownCloud / WebDAV', + 'configuration' => array( + 'host' => 'URL', + 'user' => 'Username', + 'password' => '*Password', + 'root' => '&Root', + 'secure' => '!Secure https://'))); + +OC_Mount_Config::registerBackend('\OC\Files\Storage\SFTP', array( + 'backend' => 'SFTP', + 'configuration' => array( + 'host' => 'URL', + 'user' => 'Username', + 'password' => '*Password', + 'root' => '&Root'))); diff --git a/apps/files_external/lib/config.php b/apps/files_external/lib/config.php index 472c3963d51..00f77ea686b 100755 --- a/apps/files_external/lib/config.php +++ b/apps/files_external/lib/config.php @@ -39,6 +39,17 @@ class OC_Mount_Config { // whether to skip backend test (for unit tests, as this static class is not mockable) public static $skipTest = false; + private static $backends = array(); + + public static function registerBackend($class, $definition) { + if (!isset($definition['backend'])) { + return false; + } + + OC_Mount_Config::$backends[$class] = $definition; + return true; + } + /** * Get details on each of the external storage backends, used for the mount config UI * If a custom UI is needed, add the key 'custom' and a javascript file with that name will be loaded @@ -49,127 +60,20 @@ class OC_Mount_Config { * @return string */ public static function getBackends() { + $sortFunc = function($a, $b) { + return strcasecmp($a['backend'], $b['backend']); + }; - // FIXME: do not rely on php key order for the options order in the UI - $backends['\OC\Files\Storage\Local']=array( - 'backend' => 'Local', - 'configuration' => array( - 'datadir' => 'Location')); - - $backends['\OC\Files\Storage\AmazonS3']=array( - 'backend' => 'Amazon S3 and compliant', - 'configuration' => array( - 'key' => 'Access Key', - 'secret' => '*Secret Key', - 'bucket' => 'Bucket', - 'hostname' => '&Hostname (optional)', - 'port' => '&Port (optional)', - 'region' => '&Region (optional)', - 'use_ssl' => '!Enable SSL', - 'use_path_style' => '!Enable Path Style')); - - $backends['\OC\Files\Storage\Dropbox']=array( - 'backend' => 'Dropbox', - 'configuration' => array( - 'configured' => '#configured', - 'app_key' => 'App key', - 'app_secret' => '*App secret', - 'token' => '#token', - 'token_secret' => '#token_secret'), - 'custom' => 'dropbox'); - - if(OC_Mount_Config::checkphpftp()) $backends['\OC\Files\Storage\FTP']=array( - 'backend' => 'FTP', - 'configuration' => array( - 'host' => 'Hostname', - 'user' => 'Username', - 'password' => '*Password', - 'root' => '&Root', - 'secure' => '!Secure ftps://')); - - if(OC_Mount_Config::checkcurl()) $backends['\OC\Files\Storage\Google']=array( - 'backend' => 'Google Drive', - 'configuration' => array( - 'configured' => '#configured', - 'client_id' => 'Client ID', - 'client_secret' => '*Client secret', - 'token' => '#token'), - 'custom' => 'google'); - - if(OC_Mount_Config::checkcurl()) { - $backends['\OC\Files\Storage\Swift'] = array( - 'backend' => 'OpenStack Object Storage', - 'configuration' => array( - 'user' => 'Username (required)', - 'bucket' => 'Bucket (required)', - 'region' => '&Region (optional for OpenStack Object Storage)', - 'key' => '*API Key (required for Rackspace Cloud Files)', - 'tenant' => '&Tenantname (required for OpenStack Object Storage)', - 'password' => '*Password (required for OpenStack Object Storage)', - 'service_name' => '&Service Name (required for OpenStack Object Storage)', - 'url' => '&URL of identity endpoint (required for OpenStack Object Storage)', - 'timeout' => '&Timeout of HTTP requests in seconds (optional)', - ) - ); - } - - if (!OC_Util::runningOnWindows()) { - if (OC_Mount_Config::checksmbclient()) { - $backends['\OC\Files\Storage\SMB'] = array( - 'backend' => 'SMB / CIFS', - 'configuration' => array( - 'host' => 'URL', - 'user' => 'Username', - 'password' => '*Password', - 'share' => 'Share', - 'root' => '&Root')); - $backends['\OC\Files\Storage\SMB_OC'] = array( - 'backend' => 'SMB / CIFS using OC login', - 'configuration' => array( - 'host' => 'URL', - 'username_as_share' => '!Username as share', - 'share' => '&Share', - 'root' => '&Root')); + foreach (OC_Mount_Config::$backends as $class => $backend) { + if (isset($backend['has_dependencies']) and $backend['has_dependencies'] === true) { + if ($class::checkDependencies() !== true) { + continue; + } } + $backends[$class] = $backend; } - if(OC_Mount_Config::checkcurl()){ - $backends['\OC\Files\Storage\DAV']=array( - 'backend' => 'WebDAV', - 'configuration' => array( - 'host' => 'URL', - 'user' => 'Username', - 'password' => '*Password', - 'root' => '&Root', - 'secure' => '!Secure https://')); - $backends['\OC\Files\Storage\OwnCloud']=array( - 'backend' => 'ownCloud', - 'configuration' => array( - 'host' => 'URL', - 'user' => 'Username', - 'password' => '*Password', - 'root' => '&Remote subfolder', - 'secure' => '!Secure https://')); - } - - $backends['\OC\Files\Storage\SFTP']=array( - 'backend' => 'SFTP', - 'configuration' => array( - 'host' => 'URL', - 'user' => 'Username', - 'password' => '*Password', - 'root' => '&Root')); - - $backends['\OC\Files\Storage\iRODS']=array( - 'backend' => 'iRODS', - 'configuration' => array( - 'host' => 'Host', - 'port' => 'Port', - 'use_logon_credentials' => '!Use ownCloud login', - 'user' => 'Username', - 'password' => '*Password', - 'auth_mode' => 'Authentication Mode', - 'zone' => 'Zone')); + uasort($backends, $sortFunc); return($backends); } @@ -613,53 +517,19 @@ class OC_Mount_Config { return true; } - /** - * check if smbclient is installed - */ - public static function checksmbclient() { - if(function_exists('shell_exec')) { - $output=shell_exec('command -v smbclient 2> /dev/null'); - return !empty($output); - }else{ - return false; - } - } - - /** - * check if php-ftp is installed - */ - public static function checkphpftp() { - if(function_exists('ftp_login')) { - return true; - }else{ - return false; - } - } - - /** - * check if curl is installed - */ - public static function checkcurl() { - return function_exists('curl_init'); - } - /** * check dependencies */ public static function checkDependencies() { - $l= new OC_L10N('files_external'); $txt=''; - if (!OC_Util::runningOnWindows()) { - if(!OC_Mount_Config::checksmbclient()) { - $txt.=$l->t('Warning: "smbclient" is not installed. Mounting of CIFS/SMB shares is not possible. Please ask your system administrator to install it.').'
'; + foreach (OC_Mount_Config::$backends as $class => $backend) { + if (isset($backend['has_dependencies']) and $backend['has_dependencies'] === true) { + $result = $class::checkDependencies(); + if ($result !== true) { + $txt.=$result.'
'; + } } } - if(!OC_Mount_Config::checkphpftp()) { - $txt.=$l->t('Warning: The FTP support in PHP is not enabled or installed. Mounting of FTP shares is not possible. Please ask your system administrator to install it.').'
'; - } - if(!OC_Mount_Config::checkcurl()) { - $txt.=$l->t('Warning: The Curl support in PHP is not enabled or installed. Mounting of ownCloud / WebDAV or GoogleDrive is not possible. Please ask your system administrator to install it.').'
'; - } return $txt; } diff --git a/apps/files_external/lib/ftp.php b/apps/files_external/lib/ftp.php index 00bf7a189ce..43635f7f94e 100644 --- a/apps/files_external/lib/ftp.php +++ b/apps/files_external/lib/ftp.php @@ -17,6 +17,18 @@ class FTP extends \OC\Files\Storage\StreamWrapper{ private static $tempFiles=array(); + /** + * check if php-ftp is installed + */ + public static function checkDependencies() { + if (function_exists('ftp_login')) { + return(true); + } else { + $l = new \OC_L10N('files_external'); + return $l->t('Warning: The FTP support in PHP is not enabled or installed. Mounting of FTP shares is not possible. Please ask your system administrator to install it.'); + } + } + public function __construct($params) { if (isset($params['host']) && isset($params['user']) && isset($params['password'])) { $this->host=$params['host']; diff --git a/apps/files_external/lib/smb.php b/apps/files_external/lib/smb.php index f3f3b3ed7f3..bfe494e867e 100644 --- a/apps/files_external/lib/smb.php +++ b/apps/files_external/lib/smb.php @@ -17,6 +17,20 @@ class SMB extends \OC\Files\Storage\StreamWrapper{ private $root; private $share; + /** + * check if smbclient is installed + */ + public static function checkDependencies() { + if (function_exists('shell_exec')) { + $output = shell_exec('which smbclient'); + if (!empty($output)) { + return true; + } + } + $l = new \OC_L10N('files_external'); + return $l->t('Warning: "smbclient" is not installed. Mounting of CIFS/SMB shares is not possible. Please ask your system administrator to install it.'); + } + public function __construct($params) { if (isset($params['host']) && isset($params['user']) && isset($params['password']) && isset($params['share'])) { $this->host=$params['host']; From 8ca897df76d2a93f66dbe097984eb1e8752410ce Mon Sep 17 00:00:00 2001 From: Philipp Kapfer Date: Sat, 25 May 2013 17:10:20 +0200 Subject: [PATCH 2/9] Added cURL dependency check to Google Drive and WebDAV backend Added check for backend's checkDependencies method to OC_Mount_Config::getBackends() when backend is configured to have some instead of blindly calling it and crashing Conflicts: apps/files_external/lib/config.php apps/files_external/lib/google.php --- apps/files_external/appinfo/app.php | 6 ++++-- apps/files_external/lib/config.php | 7 +++++-- apps/files_external/lib/google.php | 12 ++++++++++++ apps/files_external/lib/webdav.php | 12 ++++++++++++ 4 files changed, 33 insertions(+), 4 deletions(-) diff --git a/apps/files_external/appinfo/app.php b/apps/files_external/appinfo/app.php index d6238932cdb..39881b8455b 100644 --- a/apps/files_external/appinfo/app.php +++ b/apps/files_external/appinfo/app.php @@ -69,7 +69,8 @@ OC_Mount_Config::registerBackend('\OC\Files\Storage\Google', array( 'configured' => '#configured', 'token' => '#token', 'token_secret' => '#token secret'), - 'custom' => 'google')); + 'custom' => 'google', + 'has_dependencies' => true)); OC_Mount_Config::registerBackend('\OC\Files\Storage\SWIFT', array( 'backend' => 'OpenStack Swift', @@ -97,7 +98,8 @@ OC_Mount_Config::registerBackend('\OC\Files\Storage\DAV', array( 'user' => 'Username', 'password' => '*Password', 'root' => '&Root', - 'secure' => '!Secure https://'))); + 'secure' => '!Secure https://'), + 'has_dependencies' => true)); OC_Mount_Config::registerBackend('\OC\Files\Storage\SFTP', array( 'backend' => 'SFTP', diff --git a/apps/files_external/lib/config.php b/apps/files_external/lib/config.php index 00f77ea686b..ebc83c7af84 100755 --- a/apps/files_external/lib/config.php +++ b/apps/files_external/lib/config.php @@ -66,7 +66,10 @@ class OC_Mount_Config { foreach (OC_Mount_Config::$backends as $class => $backend) { if (isset($backend['has_dependencies']) and $backend['has_dependencies'] === true) { - if ($class::checkDependencies() !== true) { + if (!method_exists($class, 'checkDependencies')) { + \OCP\Util::writeLog('files_external', "Backend class $class has dependencies but doesn't provide method checkDependencies()", \OCP\Util::DEBUG); + continue; + } elseif ($class::checkDependencies() !== true) { continue; } } @@ -75,7 +78,7 @@ class OC_Mount_Config { uasort($backends, $sortFunc); - return($backends); + return $backends; } /** diff --git a/apps/files_external/lib/google.php b/apps/files_external/lib/google.php index 35457f68528..936153b35a5 100644 --- a/apps/files_external/lib/google.php +++ b/apps/files_external/lib/google.php @@ -41,6 +41,18 @@ class Google extends \OC\Files\Storage\Common { const DRAWING = 'application/vnd.google-apps.drawing'; const PRESENTATION = 'application/vnd.google-apps.presentation'; + /** + * check if curl is installed + */ + public static function checkDependencies() { + if (function_exists('curl_init')) { + return true; + } else { + $l = new \OC_L10N('files_external'); + return $l->t('Warning: The cURL support in PHP is not enabled or installed. Mounting of Google Drive is not possible. Please ask your system administrator to install it.'); + } + } + public function __construct($params) { if (isset($params['configured']) && $params['configured'] === 'true' && isset($params['client_id']) && isset($params['client_secret']) diff --git a/apps/files_external/lib/webdav.php b/apps/files_external/lib/webdav.php index 279ae716935..ac6663133d0 100644 --- a/apps/files_external/lib/webdav.php +++ b/apps/files_external/lib/webdav.php @@ -23,6 +23,18 @@ class DAV extends \OC\Files\Storage\Common { private static $tempFiles = array(); + /** + * check if curl is installed + */ + public static function checkDependencies() { + if (function_exists('curl_init')) { + return true; + } else { + $l = new \OC_L10N('files_external'); + return $l->t('Warning: The cURL support in PHP is not enabled or installed. Mounting of ownCloud / WebDAV is not possible. Please ask your system administrator to install it.'); + } + } + public function __construct($params) { if (isset($params['host']) && isset($params['user']) && isset($params['password'])) { $host = $params['host']; From 46379113a118e8c4c8ed26026f2d2a610380a368 Mon Sep 17 00:00:00 2001 From: Philipp Kapfer Date: Thu, 30 May 2013 17:37:47 +0200 Subject: [PATCH 3/9] Changed dependency check messages from warnings to notes Added check for duplicate dependency check messages to display only the first --- apps/files_external/lib/config.php | 41 +++++++++++++++++++++++++++--- apps/files_external/lib/ftp.php | 2 +- apps/files_external/lib/google.php | 2 +- apps/files_external/lib/smb.php | 2 +- apps/files_external/lib/webdav.php | 2 +- 5 files changed, 41 insertions(+), 8 deletions(-) diff --git a/apps/files_external/lib/config.php b/apps/files_external/lib/config.php index ebc83c7af84..2112266efc4 100755 --- a/apps/files_external/lib/config.php +++ b/apps/files_external/lib/config.php @@ -524,17 +524,50 @@ class OC_Mount_Config { * check dependencies */ public static function checkDependencies() { - $txt=''; + $dependencyMessages = array(); foreach (OC_Mount_Config::$backends as $class => $backend) { if (isset($backend['has_dependencies']) and $backend['has_dependencies'] === true) { $result = $class::checkDependencies(); - if ($result !== true) { - $txt.=$result.'
'; + if ($result !== true and OC_Mount_Config::findFirstSentence($dependencyMessages, $result) < 0) { + $dependencyMessages[] = $result; } } } - return $txt; + if (count($dependencyMessages) > 0) { + return implode('
', $dependencyMessages); + } + return ''; + } + + /** + * Finds the first string in an array that has the same first sentence (or part thereof) + * as a given comparison string + * @param $arr array An array of strings + * @param $str string The string to find + * @return int The position of the first occurrence or -1 if not found + */ + private static function findFirstSentence($arr, $str) { + foreach ($arr as $i => $item) { + $itemPos = strpos($item, '.'); + $strPos = strpos($str, '.'); + + if ($itemPos < 0 && $strPos < 0) { + $itemPos = strpos($item, ','); + $strPos = strpos($str, ','); + + if ($itemPos < 0 && $strPos < 0) { + $itemPos = strlen($item); + $strPos = strlen($str); + } + } + + if ($itemPos === $strPos and strncasecmp($item, $str, $itemPos) === 0) { + return $i; + } + } + + return -1; } /** diff --git a/apps/files_external/lib/ftp.php b/apps/files_external/lib/ftp.php index 43635f7f94e..a6a775ff6d1 100644 --- a/apps/files_external/lib/ftp.php +++ b/apps/files_external/lib/ftp.php @@ -25,7 +25,7 @@ class FTP extends \OC\Files\Storage\StreamWrapper{ return(true); } else { $l = new \OC_L10N('files_external'); - return $l->t('Warning: The FTP support in PHP is not enabled or installed. Mounting of FTP shares is not possible. Please ask your system administrator to install it.'); + return $l->t('Note: The FTP support in PHP is not enabled or installed. Mounting of FTP shares is not possible. Please ask your system administrator to install it.'); } } diff --git a/apps/files_external/lib/google.php b/apps/files_external/lib/google.php index 936153b35a5..cfb7005c4c7 100644 --- a/apps/files_external/lib/google.php +++ b/apps/files_external/lib/google.php @@ -49,7 +49,7 @@ class Google extends \OC\Files\Storage\Common { return true; } else { $l = new \OC_L10N('files_external'); - return $l->t('Warning: The cURL support in PHP is not enabled or installed. Mounting of Google Drive is not possible. Please ask your system administrator to install it.'); + return $l->t('Note: The cURL support in PHP is not enabled or installed. Mounting of Google Drive is not possible. Please ask your system administrator to install it.'); } } diff --git a/apps/files_external/lib/smb.php b/apps/files_external/lib/smb.php index bfe494e867e..6fb262323ff 100644 --- a/apps/files_external/lib/smb.php +++ b/apps/files_external/lib/smb.php @@ -28,7 +28,7 @@ class SMB extends \OC\Files\Storage\StreamWrapper{ } } $l = new \OC_L10N('files_external'); - return $l->t('Warning: "smbclient" is not installed. Mounting of CIFS/SMB shares is not possible. Please ask your system administrator to install it.'); + return $l->t('Note: "smbclient" is not installed. Mounting of CIFS/SMB shares is not possible. Please ask your system administrator to install it.'); } public function __construct($params) { diff --git a/apps/files_external/lib/webdav.php b/apps/files_external/lib/webdav.php index ac6663133d0..0ea7a085041 100644 --- a/apps/files_external/lib/webdav.php +++ b/apps/files_external/lib/webdav.php @@ -31,7 +31,7 @@ class DAV extends \OC\Files\Storage\Common { return true; } else { $l = new \OC_L10N('files_external'); - return $l->t('Warning: The cURL support in PHP is not enabled or installed. Mounting of ownCloud / WebDAV is not possible. Please ask your system administrator to install it.'); + return $l->t('Note: The cURL support in PHP is not enabled or installed. Mounting of ownCloud / WebDAV is not possible. Please ask your system administrator to install it.'); } } From ff9a4a6d91b4a23be91a462806b8162543bb6c95 Mon Sep 17 00:00:00 2001 From: Philipp Kapfer Date: Sat, 1 Jun 2013 11:28:02 +0200 Subject: [PATCH 4/9] Added cURL dependency for Amazon S3, Dropbox and SWIFT Conflicts: apps/files_external/lib/amazons3.php apps/files_external/lib/swift.php --- apps/files_external/appinfo/app.php | 9 ++++++--- apps/files_external/lib/amazons3.php | 12 ++++++++++++ apps/files_external/lib/dropbox.php | 12 ++++++++++++ apps/files_external/lib/swift.php | 12 ++++++++++++ 4 files changed, 42 insertions(+), 3 deletions(-) diff --git a/apps/files_external/appinfo/app.php b/apps/files_external/appinfo/app.php index 39881b8455b..f49d2973234 100644 --- a/apps/files_external/appinfo/app.php +++ b/apps/files_external/appinfo/app.php @@ -41,7 +41,8 @@ OC_Mount_Config::registerBackend('\OC\Files\Storage\AmazonS3', array( 'configuration' => array( 'key' => 'Key', 'secret' => '*Secret', - 'bucket' => 'Bucket'))); + 'bucket' => 'Bucket'), + 'has_dependencies' => true)); OC_Mount_Config::registerBackend('\OC\Files\Storage\Dropbox', array( 'backend' => 'Dropbox', @@ -51,7 +52,8 @@ OC_Mount_Config::registerBackend('\OC\Files\Storage\Dropbox', array( 'app_secret' => 'App secret', 'token' => '#token', 'token_secret' => '#token_secret'), - 'custom' => 'dropbox')); + 'custom' => 'dropbox', + 'has_dependencies' => true)); OC_Mount_Config::registerBackend('\OC\Files\Storage\FTP', array( 'backend' => 'FTP', @@ -79,7 +81,8 @@ OC_Mount_Config::registerBackend('\OC\Files\Storage\SWIFT', array( 'user' => 'Username', 'token' => '*Token', 'root' => '&Root', - 'secure' => '!Secure ftps://'))); + 'secure' => '!Secure ftps://'), + 'has_dependencies' => true)); OC_Mount_Config::registerBackend('\OC\Files\Storage\SMB', array( 'backend' => 'SMB / CIFS', diff --git a/apps/files_external/lib/amazons3.php b/apps/files_external/lib/amazons3.php index 06ccd5d16fa..969070360fe 100644 --- a/apps/files_external/lib/amazons3.php +++ b/apps/files_external/lib/amazons3.php @@ -73,6 +73,18 @@ class AmazonS3 extends \OC\Files\Storage\Common { } } + /** + * check if curl is installed + */ + public static function checkDependencies() { + if (function_exists('curl_init')) { + return true; + } else { + $l = new \OC_L10N('files_external'); + return $l->t('Note: The cURL support in PHP is not enabled or installed. Mounting of Amazon S3 is not possible. Please ask your system administrator to install it.'); + } + } + public function __construct($params) { if (!isset($params['key']) || !isset($params['secret']) || !isset($params['bucket'])) { throw new \Exception("Access Key, Secret and Bucket have to be configured."); diff --git a/apps/files_external/lib/dropbox.php b/apps/files_external/lib/dropbox.php index 0214e18020c..f6a54bc0ca8 100755 --- a/apps/files_external/lib/dropbox.php +++ b/apps/files_external/lib/dropbox.php @@ -33,6 +33,18 @@ class Dropbox extends \OC\Files\Storage\Common { private static $tempFiles = array(); + /** + * check if curl is installed + */ + public static function checkDependencies() { + if (function_exists('curl_init')) { + return true; + } else { + $l = new \OC_L10N('files_external'); + return $l->t('Note: The cURL support in PHP is not enabled or installed. Mounting of Dropbox is not possible. Please ask your system administrator to install it.'); + } + } + public function __construct($params) { if (isset($params['configured']) && $params['configured'] == 'true' && isset($params['app_key']) diff --git a/apps/files_external/lib/swift.php b/apps/files_external/lib/swift.php index 1337d9f581d..a66d53fc1a8 100644 --- a/apps/files_external/lib/swift.php +++ b/apps/files_external/lib/swift.php @@ -65,6 +65,18 @@ class Swift extends \OC\Files\Storage\Common { return $path; } + /** + * check if curl is installed + */ + public static function checkDependencies() { + if (function_exists('curl_init')) { + return true; + } else { + $l = new \OC_L10N('files_external'); + return $l->t('Note: The cURL support in PHP is not enabled or installed. Mounting of OpenStack Swift is not possible. Please ask your system administrator to install it.'); + } + } + /** * @param string $path */ From 730bca98b48384816792423e97c53de112ac8aeb Mon Sep 17 00:00:00 2001 From: Philipp Kapfer Date: Fri, 2 Aug 2013 15:44:56 +0200 Subject: [PATCH 5/9] Moved dependency checks to end of class files Dependency messages now appear below the configuration options instead of above Reworked dependency check method to support consolidated messages for multiple backends Conflicts: apps/files_external/lib/google.php apps/files_external/lib/swift.php apps/files_external/templates/settings.php --- apps/files_external/lib/amazons3.php | 12 + apps/files_external/lib/config.php | 85 ++++-- apps/files_external/lib/dropbox.php | 23 +- apps/files_external/lib/ftp.php | 24 +- apps/files_external/lib/google.php | 23 +- apps/files_external/lib/smb.php | 28 +- apps/files_external/lib/swift.php | 28 +- apps/files_external/lib/webdav.php | 23 +- apps/files_external/templates/settings.php | 304 ++++++++++----------- 9 files changed, 299 insertions(+), 251 deletions(-) diff --git a/apps/files_external/lib/amazons3.php b/apps/files_external/lib/amazons3.php index 969070360fe..352885121f9 100644 --- a/apps/files_external/lib/amazons3.php +++ b/apps/files_external/lib/amazons3.php @@ -560,4 +560,16 @@ class AmazonS3 extends \OC\Files\Storage\Common { return false; } } + + /** + * check if curl is installed + */ + public static function checkDependencies() { + if (function_exists('curl_init')) { + return true; + } else { + return array('curl'); + } + } + } diff --git a/apps/files_external/lib/config.php b/apps/files_external/lib/config.php index 2112266efc4..242cdff911c 100755 --- a/apps/files_external/lib/config.php +++ b/apps/files_external/lib/config.php @@ -524,50 +524,85 @@ class OC_Mount_Config { * check dependencies */ public static function checkDependencies() { - $dependencyMessages = array(); + $dependencies = array(); foreach (OC_Mount_Config::$backends as $class => $backend) { if (isset($backend['has_dependencies']) and $backend['has_dependencies'] === true) { $result = $class::checkDependencies(); - if ($result !== true and OC_Mount_Config::findFirstSentence($dependencyMessages, $result) < 0) { - $dependencyMessages[] = $result; + if ($result !== true) { + foreach ($result as $key => $value) { + if (is_numeric($key)) { + OC_Mount_Config::addDependency($dependencies, $value, $backend['backend']); + } else { + OC_Mount_Config::addDependency($dependencies, $key, $backend['backend'], $value); + } + } } } } - if (count($dependencyMessages) > 0) { - return implode('
', $dependencyMessages); + if (count($dependencies) > 0) { + return OC_Mount_Config::generateDependencyMessage($dependencies); } return ''; } - /** - * Finds the first string in an array that has the same first sentence (or part thereof) - * as a given comparison string - * @param $arr array An array of strings - * @param $str string The string to find - * @return int The position of the first occurrence or -1 if not found - */ - private static function findFirstSentence($arr, $str) { - foreach ($arr as $i => $item) { - $itemPos = strpos($item, '.'); - $strPos = strpos($str, '.'); + private static function addDependency(&$dependencies, $module, $backend, $message=null) { + if (!isset($dependencies[$module])) { + $dependencies[$module] = array(); + } - if ($itemPos < 0 && $strPos < 0) { - $itemPos = strpos($item, ','); - $strPos = strpos($str, ','); + if ($message === null) { + $dependencies[$module][] = $backend; + } else { + $dependencies[$module][] = array('backend' => $backend, 'message' => $message); + } + } - if ($itemPos < 0 && $strPos < 0) { - $itemPos = strlen($item); - $strPos = strlen($str); + private static function generateDependencyMessage($dependencies) { + $l = new \OC_L10N('files_external'); + $dependencyMessage = ''; + foreach ($dependencies as $module => $backends) { + $dependencyGroup = array(); + foreach ($backends as $backend) { + if (is_array($backend)) { + $dependencyMessage .= '
' . $l->t('Note: ') . $backend['message']; + } else { + $dependencyGroup[] = $backend; } } - if ($itemPos === $strPos and strncasecmp($item, $str, $itemPos) === 0) { - return $i; + if (count($dependencyGroup) > 0) { + $backends = ''; + for ($i = 0; $i < count($dependencyGroup); $i++) { + if ($i > 0 && $i === count($dependencyGroup) - 1) { + $backends .= $l->t(' and '); + } elseif ($i > 0) { + $backends .= ', '; + } + $backends .= '' . $dependencyGroup[$i] . ''; + } + $dependencyMessage .= '
' . OC_Mount_Config::getSingleDependencyMessage($l, $module, $backends); } } + return $dependencyMessage; + } - return -1; + /** + * Returns a dependency missing message + * @param $l OC_L10N + * @param $module string + * @param $backend string + * @return string + */ + private static function getSingleDependencyMessage($l, $module, $backend) { + switch (strtolower($module)) { + case 'curl': + return $l->t('Note: The cURL support in PHP is not enabled or installed. Mounting of %s is not possible. Please ask your system administrator to install it.', $backend); + case 'ftp': + return $l->t('Note: The FTP support in PHP is not enabled or installed. Mounting of %s is not possible. Please ask your system administrator to install it.', $backend); + default: + return $l->t('Note: "%s" is not installed. Mounting of %s is not possible. Please ask your system administrator to install it.', array($module, $backend)); + } } /** diff --git a/apps/files_external/lib/dropbox.php b/apps/files_external/lib/dropbox.php index f6a54bc0ca8..38de3360f2b 100755 --- a/apps/files_external/lib/dropbox.php +++ b/apps/files_external/lib/dropbox.php @@ -33,18 +33,6 @@ class Dropbox extends \OC\Files\Storage\Common { private static $tempFiles = array(); - /** - * check if curl is installed - */ - public static function checkDependencies() { - if (function_exists('curl_init')) { - return true; - } else { - $l = new \OC_L10N('files_external'); - return $l->t('Note: The cURL support in PHP is not enabled or installed. Mounting of Dropbox is not possible. Please ask your system administrator to install it.'); - } - } - public function __construct($params) { if (isset($params['configured']) && $params['configured'] == 'true' && isset($params['app_key']) @@ -323,4 +311,15 @@ class Dropbox extends \OC\Files\Storage\Common { return true; } + /** + * check if curl is installed + */ + public static function checkDependencies() { + if (function_exists('curl_init')) { + return true; + } else { + return array('curl'); + } + } + } diff --git a/apps/files_external/lib/ftp.php b/apps/files_external/lib/ftp.php index a6a775ff6d1..b3f8b1444ae 100644 --- a/apps/files_external/lib/ftp.php +++ b/apps/files_external/lib/ftp.php @@ -17,18 +17,6 @@ class FTP extends \OC\Files\Storage\StreamWrapper{ private static $tempFiles=array(); - /** - * check if php-ftp is installed - */ - public static function checkDependencies() { - if (function_exists('ftp_login')) { - return(true); - } else { - $l = new \OC_L10N('files_external'); - return $l->t('Note: The FTP support in PHP is not enabled or installed. Mounting of FTP shares is not possible. Please ask your system administrator to install it.'); - } - } - public function __construct($params) { if (isset($params['host']) && isset($params['user']) && isset($params['password'])) { $this->host=$params['host']; @@ -131,4 +119,16 @@ class FTP extends \OC\Files\Storage\StreamWrapper{ unlink($tmpFile); } } + + /** + * check if php-ftp is installed + */ + public static function checkDependencies() { + if (function_exists('ftp_login')) { + return(true); + } else { + return array('ftp'); + } + } + } diff --git a/apps/files_external/lib/google.php b/apps/files_external/lib/google.php index cfb7005c4c7..56c0d451651 100644 --- a/apps/files_external/lib/google.php +++ b/apps/files_external/lib/google.php @@ -41,18 +41,6 @@ class Google extends \OC\Files\Storage\Common { const DRAWING = 'application/vnd.google-apps.drawing'; const PRESENTATION = 'application/vnd.google-apps.presentation'; - /** - * check if curl is installed - */ - public static function checkDependencies() { - if (function_exists('curl_init')) { - return true; - } else { - $l = new \OC_L10N('files_external'); - return $l->t('Note: The cURL support in PHP is not enabled or installed. Mounting of Google Drive is not possible. Please ask your system administrator to install it.'); - } - } - public function __construct($params) { if (isset($params['configured']) && $params['configured'] === 'true' && isset($params['client_id']) && isset($params['client_secret']) @@ -598,4 +586,15 @@ class Google extends \OC\Files\Storage\Common { return false; } + /** + * check if curl is installed + */ + public static function checkDependencies() { + if (function_exists('curl_init')) { + return true; + } else { + return array('curl'); + } + } + } diff --git a/apps/files_external/lib/smb.php b/apps/files_external/lib/smb.php index 6fb262323ff..3ef13b633a9 100644 --- a/apps/files_external/lib/smb.php +++ b/apps/files_external/lib/smb.php @@ -17,20 +17,6 @@ class SMB extends \OC\Files\Storage\StreamWrapper{ private $root; private $share; - /** - * check if smbclient is installed - */ - public static function checkDependencies() { - if (function_exists('shell_exec')) { - $output = shell_exec('which smbclient'); - if (!empty($output)) { - return true; - } - } - $l = new \OC_L10N('files_external'); - return $l->t('Note: "smbclient" is not installed. Mounting of CIFS/SMB shares is not possible. Please ask your system administrator to install it.'); - } - public function __construct($params) { if (isset($params['host']) && isset($params['user']) && isset($params['password']) && isset($params['share'])) { $this->host=$params['host']; @@ -148,4 +134,18 @@ class SMB extends \OC\Files\Storage\StreamWrapper{ } return $lastCtime; } + + /** + * check if smbclient is installed + */ + public static function checkDependencies() { + if (function_exists('shell_exec')) { + $output = shell_exec('which smbclient'); + if (!empty($output)) { + return true; + } + } + return array('smbclient'); + } + } diff --git a/apps/files_external/lib/swift.php b/apps/files_external/lib/swift.php index a66d53fc1a8..a202d3843cb 100644 --- a/apps/files_external/lib/swift.php +++ b/apps/files_external/lib/swift.php @@ -65,16 +65,16 @@ class Swift extends \OC\Files\Storage\Common { return $path; } + const SUBCONTAINER_FILE='.subcontainers'; + /** - * check if curl is installed + * translate directory path to container name + * @param string $path + * @return string */ - public static function checkDependencies() { - if (function_exists('curl_init')) { - return true; - } else { - $l = new \OC_L10N('files_external'); - return $l->t('Note: The cURL support in PHP is not enabled or installed. Mounting of OpenStack Swift is not possible. Please ask your system administrator to install it.'); - } + private function getContainerName($path) { + $path=trim(trim($this->root, '/') . "/".$path, '/.'); + return str_replace('/', '\\', $path); } /** @@ -502,4 +502,16 @@ class Swift extends \OC\Files\Storage\Common { ), $tmpFile); unlink($tmpFile); } + + /** + * check if curl is installed + */ + public static function checkDependencies() { + if (function_exists('curl_init')) { + return true; + } else { + return array('curl'); + } + } + } diff --git a/apps/files_external/lib/webdav.php b/apps/files_external/lib/webdav.php index 0ea7a085041..b43c65e8e6c 100644 --- a/apps/files_external/lib/webdav.php +++ b/apps/files_external/lib/webdav.php @@ -23,18 +23,6 @@ class DAV extends \OC\Files\Storage\Common { private static $tempFiles = array(); - /** - * check if curl is installed - */ - public static function checkDependencies() { - if (function_exists('curl_init')) { - return true; - } else { - $l = new \OC_L10N('files_external'); - return $l->t('Note: The cURL support in PHP is not enabled or installed. Mounting of ownCloud / WebDAV is not possible. Please ask your system administrator to install it.'); - } - } - public function __construct($params) { if (isset($params['host']) && isset($params['user']) && isset($params['password'])) { $host = $params['host']; @@ -397,5 +385,16 @@ class DAV extends \OC\Files\Storage\Common { return false; } } + + /** + * check if curl is installed + */ + public static function checkDependencies() { + if (function_exists('curl_init')) { + return true; + } else { + return array('curl'); + } + } } diff --git a/apps/files_external/templates/settings.php b/apps/files_external/templates/settings.php index e8815acaf16..84d9d3408d1 100644 --- a/apps/files_external/templates/settings.php +++ b/apps/files_external/templates/settings.php @@ -1,170 +1,162 @@ -
-

t('External Storage')); ?>

- '')) print_unescaped(''.$_['dependencies'].''); ?> - '> - + +
+ t('External Storage')); ?> +
'> + - '.$l->t('Available for').''); ?> + '.$l->t('Applicable').''); ?> - - - array())); ?> - - > - - - - - - - - - - - - - - - -
t('Folder name')); ?> t('External storage')); ?> t('Configuration')); ?>  
- - - - - - - - $value): ?> - - - - class="optional" - data-parameter="" - value="" - placeholder="" /> - - - - - - class="optional" - data-parameter="" - value="" - placeholder="" /> - - - - - - - - ' - data-applicable-users=''> - - class="remove" - style="visibility:hidden;" - ><?php p($l->t('Delete')); ?>
-
- - -
- /> - - -

class="hidden"> - t('Allow users to mount the following external storage')); ?>
- $backend): ?> - /> -
- - -

- -
- - -
-

t('SSL root certificates'));?>

- '> + - - - - - + array())); ?> + $mount): ?> + > + + + + + + + + + + + + +
class="remove" - style="visibility:hidden;" - ><?php p($l->t('Delete')); ?>
+ + + + + + + + $value): ?> + + + + + + + + + + + + + + + + + + + + ' + data-applicable-users=''> + + class="remove" + style="visibility:hidden;" + ><?php p($l->t('Delete')); ?>
- - - + '')) print_unescaped(''.$_['dependencies'].''); ?> +
+ + +
+ /> +
+ t('Allow users to mount their own external storage')); ?> + +
+ + +
+
+ t('SSL root certificates'));?> + '> + + + + + + + + +
class="remove" + style="visibility:hidden;" + ><?php p($l->t('Delete')); ?>
+ + + +
+
From c0ebc1dfb1108b7cbe0fa134def00aa6ce0066b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20M=C3=BCller?= Date: Thu, 3 Apr 2014 16:57:26 +0200 Subject: [PATCH 6/9] Update backends and their configurations --- apps/files_external/appinfo/app.php | 93 ++++++++++++++++++++++------- 1 file changed, 73 insertions(+), 20 deletions(-) diff --git a/apps/files_external/appinfo/app.php b/apps/files_external/appinfo/app.php index f49d2973234..0017b22caad 100644 --- a/apps/files_external/appinfo/app.php +++ b/apps/files_external/appinfo/app.php @@ -44,12 +44,25 @@ OC_Mount_Config::registerBackend('\OC\Files\Storage\AmazonS3', array( 'bucket' => 'Bucket'), 'has_dependencies' => true)); +OC_Mount_Config::registerBackend('\OC\Files\Storage\AmazonS3', array( + 'backend' => 'Amazon S3 and compliant', + 'configuration' => array( + 'key' => 'Access Key', + 'secret' => '*Secret Key', + 'bucket' => 'Bucket', + 'hostname' => '&Hostname (optional)', + 'port' => '&Port (optional)', + 'region' => '&Region (optional)', + 'use_ssl' => '!Enable SSL', + 'use_path_style' => '!Enable Path Style'), + 'has_dependencies' => true)); + OC_Mount_Config::registerBackend('\OC\Files\Storage\Dropbox', array( 'backend' => 'Dropbox', 'configuration' => array( 'configured' => '#configured', 'app_key' => 'App key', - 'app_secret' => 'App secret', + 'app_secret' => '*App secret', 'token' => '#token', 'token_secret' => '#token_secret'), 'custom' => 'dropbox', @@ -69,33 +82,52 @@ OC_Mount_Config::registerBackend('\OC\Files\Storage\Google', array( 'backend' => 'Google Drive', 'configuration' => array( 'configured' => '#configured', - 'token' => '#token', - 'token_secret' => '#token secret'), + 'client_id' => 'Client ID', + 'client_secret' => '*Client secret', + 'token' => '#token'), 'custom' => 'google', 'has_dependencies' => true)); -OC_Mount_Config::registerBackend('\OC\Files\Storage\SWIFT', array( - 'backend' => 'OpenStack Swift', + +OC_Mount_Config::registerBackend('\OC\Files\Storage\Swift', array( + 'backend' => 'OpenStack Object Storage', 'configuration' => array( - 'host' => 'URL', - 'user' => 'Username', - 'token' => '*Token', - 'root' => '&Root', - 'secure' => '!Secure ftps://'), + 'user' => 'Username (required)', + 'bucket' => 'Bucket (required)', + 'region' => '&Region (optional for OpenStack Object Storage)', + 'key' => '*API Key (required for Rackspace Cloud Files)', + 'tenant' => '&Tenantname (required for OpenStack Object Storage)', + 'password' => '*Password (required for OpenStack Object Storage)', + 'service_name' => '&Service Name (required for OpenStack Object Storage)', + 'url' => '&URL of identity endpoint (required for OpenStack Object Storage)', + 'timeout' => '&Timeout of HTTP requests in seconds (optional)', + ), 'has_dependencies' => true)); -OC_Mount_Config::registerBackend('\OC\Files\Storage\SMB', array( - 'backend' => 'SMB / CIFS', - 'configuration' => array( - 'host' => 'URL', - 'user' => 'Username', - 'password' => '*Password', - 'share' => 'Share', - 'root' => '&Root'), - 'has_dependencies' => true)); + +if (!OC_Util::runningOnWindows()) { + OC_Mount_Config::registerBackend('\OC\Files\Storage\SMB', array( + 'backend' => 'SMB / CIFS', + 'configuration' => array( + 'host' => 'URL', + 'user' => 'Username', + 'password' => '*Password', + 'share' => 'Share', + 'root' => '&Root'), + 'has_dependencies' => true)); + + OC_Mount_Config::registerBackend('\OC\Files\Storage\SMB_OC', array( + 'backend' => 'SMB / CIFS using OC login', + 'configuration' => array( + 'host' => 'URL', + 'username_as_share' => '!Username as share', + 'share' => '&Share', + 'root' => '&Root'), + 'has_dependencies' => true)); +} OC_Mount_Config::registerBackend('\OC\Files\Storage\DAV', array( - 'backend' => 'ownCloud / WebDAV', + 'backend' => 'WebDAV', 'configuration' => array( 'host' => 'URL', 'user' => 'Username', @@ -104,6 +136,16 @@ OC_Mount_Config::registerBackend('\OC\Files\Storage\DAV', array( 'secure' => '!Secure https://'), 'has_dependencies' => true)); +OC_Mount_Config::registerBackend('\OC\Files\Storage\OwnCloud', array( + 'backend' => 'ownCloud', + 'configuration' => array( + 'host' => 'URL', + 'user' => 'Username', + 'password' => '*Password', + 'root' => '&Remote subfolder', + 'secure' => '!Secure https://'))); + + OC_Mount_Config::registerBackend('\OC\Files\Storage\SFTP', array( 'backend' => 'SFTP', 'configuration' => array( @@ -111,3 +153,14 @@ OC_Mount_Config::registerBackend('\OC\Files\Storage\SFTP', array( 'user' => 'Username', 'password' => '*Password', 'root' => '&Root'))); + +OC_Mount_Config::registerBackend('\OC\Files\Storage\iRODS', array( + 'backend' => 'iRODS', + 'configuration' => array( + 'host' => 'Host', + 'port' => 'Port', + 'use_logon_credentials' => '!Use ownCloud login', + 'user' => 'Username', + 'password' => '*Password', + 'auth_mode' => 'Authentication Mode', + 'zone' => 'Zone'))); From c6976d89a6fbfd59f966d676e23b39de62711b06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20M=C3=BCller?= Date: Thu, 3 Apr 2014 17:09:40 +0200 Subject: [PATCH 7/9] use command -v to detect if smbclient exists --- apps/files_external/lib/smb.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/files_external/lib/smb.php b/apps/files_external/lib/smb.php index 3ef13b633a9..b1d355323d9 100644 --- a/apps/files_external/lib/smb.php +++ b/apps/files_external/lib/smb.php @@ -140,7 +140,7 @@ class SMB extends \OC\Files\Storage\StreamWrapper{ */ public static function checkDependencies() { if (function_exists('shell_exec')) { - $output = shell_exec('which smbclient'); + $output=shell_exec('command -v smbclient 2> /dev/null'); if (!empty($output)) { return true; } From e3b9a861841f53dea810c1246ffa3cf809d0bfab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20M=C3=BCller?= Date: Thu, 3 Apr 2014 17:17:44 +0200 Subject: [PATCH 8/9] remove duplicate method checkDependencies() --- apps/files_external/lib/amazons3.php | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/apps/files_external/lib/amazons3.php b/apps/files_external/lib/amazons3.php index 352885121f9..2093fb7e58c 100644 --- a/apps/files_external/lib/amazons3.php +++ b/apps/files_external/lib/amazons3.php @@ -73,18 +73,6 @@ class AmazonS3 extends \OC\Files\Storage\Common { } } - /** - * check if curl is installed - */ - public static function checkDependencies() { - if (function_exists('curl_init')) { - return true; - } else { - $l = new \OC_L10N('files_external'); - return $l->t('Note: The cURL support in PHP is not enabled or installed. Mounting of Amazon S3 is not possible. Please ask your system administrator to install it.'); - } - } - public function __construct($params) { if (!isset($params['key']) || !isset($params['secret']) || !isset($params['bucket'])) { throw new \Exception("Access Key, Secret and Bucket have to be configured."); From 3ac009c2b45e1980222e9a8b990b2f1b794c82a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20M=C3=BCller?= Date: Mon, 7 Apr 2014 20:18:57 +0200 Subject: [PATCH 9/9] unit tests for dynamic backend registration --- apps/files_external/lib/config.php | 52 ++++++--- .../tests/dynamicmountconfig.php | 103 ++++++++++++++++++ 2 files changed, 141 insertions(+), 14 deletions(-) create mode 100644 apps/files_external/tests/dynamicmountconfig.php diff --git a/apps/files_external/lib/config.php b/apps/files_external/lib/config.php index 242cdff911c..613f0b2609c 100755 --- a/apps/files_external/lib/config.php +++ b/apps/files_external/lib/config.php @@ -41,6 +41,11 @@ class OC_Mount_Config { private static $backends = array(); + /** + * @param string $class + * @param array $definition + * @return bool + */ public static function registerBackend($class, $definition) { if (!isset($definition['backend'])) { return false; @@ -50,6 +55,18 @@ class OC_Mount_Config { return true; } + /** + * Setup backends + * + * @return array of previously registered backends + */ + public static function setUp($backends = array()) { + $backup = self::$backends; + self::$backends = $backends; + + return $backup; + } + /** * Get details on each of the external storage backends, used for the mount config UI * If a custom UI is needed, add the key 'custom' and a javascript file with that name will be loaded @@ -57,28 +74,32 @@ class OC_Mount_Config { * If the configuration parameter is a boolean, add a '!' to the beginning of the value * If the configuration parameter is optional, add a '&' to the beginning of the value * If the configuration parameter is hidden, add a '#' to the beginning of the value - * @return string + * @return array */ public static function getBackends() { $sortFunc = function($a, $b) { return strcasecmp($a['backend'], $b['backend']); }; + $backEnds = array(); + foreach (OC_Mount_Config::$backends as $class => $backend) { if (isset($backend['has_dependencies']) and $backend['has_dependencies'] === true) { if (!method_exists($class, 'checkDependencies')) { - \OCP\Util::writeLog('files_external', "Backend class $class has dependencies but doesn't provide method checkDependencies()", \OCP\Util::DEBUG); + \OCP\Util::writeLog('files_external', + "Backend class $class has dependencies but doesn't provide method checkDependencies()", + \OCP\Util::DEBUG); continue; } elseif ($class::checkDependencies() !== true) { continue; } } - $backends[$class] = $backend; + $backEnds[$class] = $backend; } - uasort($backends, $sortFunc); + uasort($backEnds, $sortFunc); - return $backends; + return $backEnds; } /** @@ -185,19 +206,19 @@ class OC_Mount_Config { */ public static function getPersonalBackends() { - $backends = self::getBackends(); + $backEnds = self::getBackends(); // Remove local storage and other disabled storages - unset($backends['\OC\Files\Storage\Local']); + unset($backEnds['\OC\Files\Storage\Local']); - $allowed_backends = explode(',', OCP\Config::getAppValue('files_external', 'user_mounting_backends', '')); - foreach ($backends as $backend => $null) { - if (!in_array($backend, $allowed_backends)) { - unset($backends[$backend]); + $allowedBackEnds = explode(',', OCP\Config::getAppValue('files_external', 'user_mounting_backends', '')); + foreach ($backEnds as $backend => $null) { + if (!in_array($backend, $allowedBackEnds)) { + unset($backEnds[$backend]); } } - return $backends; + return $backEnds; } /** @@ -280,7 +301,7 @@ class OC_Mount_Config { */ public static function getPersonalMountPoints() { $mountPoints = self::readData(true); - $backends = self::getBackends(); + $backEnds = self::getBackends(); $uid = OCP\User::getUser(); $personal = array(); if (isset($mountPoints[self::MOUNT_TYPE_USER][$uid])) { @@ -294,7 +315,7 @@ class OC_Mount_Config { 'class' => $mount['class'], // Remove '/uid/files/' from mount point 'mountpoint' => substr($mountPoint, strlen($uid) + 8), - 'backend' => $backends[$mount['class']]['backend'], + 'backend' => $backEnds[$mount['class']]['backend'], 'options' => $mount['options'], 'status' => self::getBackendStatus($mount['class'], $mount['options'], true) ); @@ -529,6 +550,9 @@ class OC_Mount_Config { if (isset($backend['has_dependencies']) and $backend['has_dependencies'] === true) { $result = $class::checkDependencies(); if ($result !== true) { + if (!is_array($result)) { + $result = array($result); + } foreach ($result as $key => $value) { if (is_numeric($key)) { OC_Mount_Config::addDependency($dependencies, $value, $backend['backend']); diff --git a/apps/files_external/tests/dynamicmountconfig.php b/apps/files_external/tests/dynamicmountconfig.php new file mode 100644 index 00000000000..81a31e14c60 --- /dev/null +++ b/apps/files_external/tests/dynamicmountconfig.php @@ -0,0 +1,103 @@ + + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE + * License as published by the Free Software Foundation; either + * version 3 of the License, or any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU AFFERO GENERAL PUBLIC LICENSE for more details. + * + * You should have received a copy of the GNU Affero General Public + * License along with this library. If not, see . + * + */ + +require_once __DIR__ . '/../../../lib/base.php'; + +require __DIR__ . '/../lib/config.php'; + +/** + * Class Test_Mount_Config_Dummy_Backend + */ +class Test_Mount_Config_Dummy_Backend { + public static $checkDependencies = true; + + public static function checkDependencies() { + return self::$checkDependencies; + } +} + +/** + * Class Test_Dynamic_Mount_Config + */ +class Test_Dynamic_Mount_Config extends \PHPUnit_Framework_TestCase { + + private $backup; + + public function testRegistration() { + + // second registration shall return false + $result = OC_Mount_Config::registerBackend('Test_Mount_Config_Dummy_Backend', array( + 'backend' => 'Test Dummy', + 'configuration' => array(), + 'has_dependencies' => true)); + + $this->assertTrue($result); + } + + public function testDependencyGetBackend() { + + // is the backend listed? + Test_Mount_Config_Dummy_Backend::$checkDependencies = true; + $backEnds = OC_Mount_Config::getBackends(); + $this->assertArrayHasKey('Test_Mount_Config_Dummy_Backend', $backEnds); + + // backend shall not be listed + Test_Mount_Config_Dummy_Backend::$checkDependencies = false; + + $backEnds = OC_Mount_Config::getBackends(); + $this->assertArrayNotHasKey('Test_Mount_Config_Dummy_Backend', $backEnds); + + } + + public function testCheckDependencies() { + + Test_Mount_Config_Dummy_Backend::$checkDependencies = true; + $message = OC_Mount_Config::checkDependencies(); + $this->assertEmpty($message); + + // backend shall not be listed + Test_Mount_Config_Dummy_Backend::$checkDependencies = array('dummy'); + + $message = OC_Mount_Config::checkDependencies(); + $this->assertEquals('
Note: "dummy" is not installed. Mounting of Test Dummy is not possible. Please ask your system administrator to install it.', + $message); + + } + + protected function setUp() { + + $this->backup = OC_Mount_Config::setUp(); + + // register dummy backend + $result = OC_Mount_Config::registerBackend('Test_Mount_Config_Dummy_Backend', array( + 'backend' => 'Test Dummy', + 'configuration' => array(), + 'has_dependencies' => true)); + + $this->assertTrue($result); + } + + protected function tearDown() + { + OC_Mount_Config::setUp($this->backup); + } +}