diff options
Diffstat (limited to 'apps/files_external/lib/config.php')
-rwxr-xr-x | apps/files_external/lib/config.php | 385 |
1 files changed, 195 insertions, 190 deletions
diff --git a/apps/files_external/lib/config.php b/apps/files_external/lib/config.php index c5b091336e1..71f6ae78878 100755 --- a/apps/files_external/lib/config.php +++ b/apps/files_external/lib/config.php @@ -5,6 +5,7 @@ * @author Michael Gapczynski * @copyright 2012 Michael Gapczynski mtgap@owncloud.com * @copyright 2014 Vincent Petry <pvince81@owncloud.com> +* @copyright 2014 Robin McCorkell <rmccorkell@karoshi.org.uk> * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE @@ -38,6 +39,34 @@ 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(); + + /** + * @param string $class + * @param array $definition + * @return bool + */ + public static function registerBackend($class, $definition) { + if (!isset($definition['backend'])) { + return false; + } + + OC_Mount_Config::$backends[$class] = $definition; + 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 @@ -45,125 +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() { - - // 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')); + $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); + continue; + } elseif ($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')); - - return($backends); + uasort($backEnds, $sortFunc); + + return $backEnds; } /** @@ -185,7 +121,7 @@ class OC_Mount_Config { * @return array of mount point string as key, mountpoint config as value */ public static function getAbsoluteMountPoints($user) { - $mountPoints = array(); + $mountPoints = array(); $datadir = \OC_Config::getValue("datadirectory", \OC::$SERVERROOT . "/data"); $mount_file = \OC_Config::getValue("mount_file", $datadir . "/mount.json"); @@ -270,19 +206,25 @@ class OC_Mount_Config { */ public static function getPersonalBackends() { - $backends = self::getBackends(); + // Check whether the user has permissions to add personal storage backends + // return an empty array if this is not the case + if(OCP\Config::getAppValue('files_external', 'allow_user_mounting', 'yes') !== 'yes') { + return array(); + } + + $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; } /** @@ -304,18 +246,23 @@ class OC_Mount_Config { $mount['options'] = self::decryptPasswords($mount['options']); // Remove '/$user/files/' from mount point $mountPoint = substr($mountPoint, 13); - // Merge the mount point into the current mount points - if (isset($system[$mountPoint]) && $system[$mountPoint]['configuration'] == $mount['options']) { - $system[$mountPoint]['applicable']['groups'] - = array_merge($system[$mountPoint]['applicable']['groups'], array($group)); + + $config = array( + 'class' => $mount['class'], + 'mountpoint' => $mountPoint, + 'backend' => $backends[$mount['class']]['backend'], + 'options' => $mount['options'], + 'applicable' => array('groups' => array($group), 'users' => array()), + 'status' => self::getBackendStatus($mount['class'], $mount['options'], false) + ); + $hash = self::makeConfigHash($config); + // If an existing config exists (with same class, mountpoint and options) + if (isset($system[$hash])) { + // add the groups into that config + $system[$hash]['applicable']['groups'] + = array_merge($system[$hash]['applicable']['groups'], array($group)); } else { - $system[$mountPoint] = array( - 'class' => $mount['class'], - 'backend' => $backends[$mount['class']]['backend'], - 'configuration' => $mount['options'], - 'applicable' => array('groups' => array($group), 'users' => array()), - 'status' => self::getBackendStatus($mount['class'], $mount['options']) - ); + $system[$hash] = $config; } } } @@ -330,23 +277,27 @@ class OC_Mount_Config { $mount['options'] = self::decryptPasswords($mount['options']); // Remove '/$user/files/' from mount point $mountPoint = substr($mountPoint, 13); - // Merge the mount point into the current mount points - if (isset($system[$mountPoint]) && $system[$mountPoint]['configuration'] == $mount['options']) { - $system[$mountPoint]['applicable']['users'] - = array_merge($system[$mountPoint]['applicable']['users'], array($user)); + $config = array( + 'class' => $mount['class'], + 'mountpoint' => $mountPoint, + 'backend' => $backends[$mount['class']]['backend'], + 'options' => $mount['options'], + 'applicable' => array('groups' => array(), 'users' => array($user)), + 'status' => self::getBackendStatus($mount['class'], $mount['options'], false) + ); + $hash = self::makeConfigHash($config); + // If an existing config exists (with same class, mountpoint and options) + if (isset($system[$hash])) { + // add the users into that config + $system[$hash]['applicable']['users'] + = array_merge($system[$hash]['applicable']['users'], array($user)); } else { - $system[$mountPoint] = array( - 'class' => $mount['class'], - 'backend' => $backends[$mount['class']]['backend'], - 'configuration' => $mount['options'], - 'applicable' => array('groups' => array(), 'users' => array($user)), - 'status' => self::getBackendStatus($mount['class'], $mount['options']) - ); + $system[$hash] = $config; } } } } - return $system; + return array_values($system); } /** @@ -356,7 +307,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])) { @@ -366,12 +317,13 @@ class OC_Mount_Config { $mount['class'] = '\OC\Files\Storage\\'.substr($mount['class'], 15); } $mount['options'] = self::decryptPasswords($mount['options']); - // Remove '/uid/files/' from mount point - $personal[substr($mountPoint, strlen($uid) + 8)] = array( + $personal[] = array( 'class' => $mount['class'], - 'backend' => $backends[$mount['class']]['backend'], - 'configuration' => $mount['options'], - 'status' => self::getBackendStatus($mount['class'], $mount['options']) + // Remove '/uid/files/' from mount point + 'mountpoint' => substr($mountPoint, strlen($uid) + 8), + 'backend' => $backEnds[$mount['class']]['backend'], + 'options' => $mount['options'], + 'status' => self::getBackendStatus($mount['class'], $mount['options'], true) ); } } @@ -384,7 +336,7 @@ class OC_Mount_Config { * @param array $options backend configuration options * @return bool true if the connection succeeded, false otherwise */ - private static function getBackendStatus($class, $options) { + private static function getBackendStatus($class, $options, $isPersonal) { if (self::$skipTest) { return true; } @@ -394,7 +346,7 @@ class OC_Mount_Config { if (class_exists($class)) { try { $storage = new $class($options); - return $storage->test(); + return $storage->test($isPersonal); } catch (Exception $exception) { \OCP\Util::logException('files_external', $exception); return false; @@ -461,7 +413,7 @@ class OC_Mount_Config { $mountPoints[$mountType] = $mount; } self::writeData($isPersonal, $mountPoints); - return self::getBackendStatus($class, $classOptions); + return self::getBackendStatus($class, $classOptions, $isPersonal); } /** @@ -596,54 +548,91 @@ class OC_Mount_Config { } /** - * check if smbclient is installed + * check dependencies */ - public static function checksmbclient() { - if(function_exists('shell_exec')) { - $output=shell_exec('command -v smbclient 2> /dev/null'); - return !empty($output); - }else{ - return false; + public static function checkDependencies() { + $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) { + if (!is_array($result)) { + $result = array($result); + } + 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($dependencies) > 0) { + return OC_Mount_Config::generateDependencyMessage($dependencies); } + return ''; } - /** - * check if php-ftp is installed - */ - public static function checkphpftp() { - if(function_exists('ftp_login')) { - return true; - }else{ - return false; + private static function addDependency(&$dependencies, $module, $backend, $message=null) { + if (!isset($dependencies[$module])) { + $dependencies[$module] = array(); + } + + if ($message === null) { + $dependencies[$module][] = $backend; + } else { + $dependencies[$module][] = array('backend' => $backend, 'message' => $message); } } - /** - * check if curl is installed - */ - public static function checkcurl() { - return function_exists('curl_init'); + 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 .= '<br />' . $l->t('<b>Note:</b> ') . $backend['message']; + } else { + $dependencyGroup[] = $backend; + } + } + + 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 .= '<i>' . $dependencyGroup[$i] . '</i>'; + } + $dependencyMessage .= '<br />' . OC_Mount_Config::getSingleDependencyMessage($l, $module, $backends); + } + } + return $dependencyMessage; } /** - * check dependencies + * Returns a dependency missing message + * @param $l OC_L10N + * @param $module string + * @param $backend string + * @return string */ - public static function checkDependencies() { - $l= new OC_L10N('files_external'); - $txt=''; - if (!OC_Util::runningOnWindows()) { - if(!OC_Mount_Config::checksmbclient()) { - $txt.=$l->t('<b>Warning:</b> "smbclient" is not installed. Mounting of CIFS/SMB shares is not possible. Please ask your system administrator to install it.').'<br />'; - } - } - if(!OC_Mount_Config::checkphpftp()) { - $txt.=$l->t('<b>Warning:</b> 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.').'<br />'; + private static function getSingleDependencyMessage($l, $module, $backend) { + switch (strtolower($module)) { + case 'curl': + return $l->t('<b>Note:</b> 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('<b>Note:</b> 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('<b>Note:</b> "%s" is not installed. Mounting of %s is not possible. Please ask your system administrator to install it.', array($module, $backend)); } - if(!OC_Mount_Config::checkcurl()) { - $txt.=$l->t('<b>Warning:</b> 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.').'<br />'; - } - - return $txt; } /** @@ -712,4 +701,20 @@ class OC_Mount_Config { $cipher->setKey(\OCP\Config::getSystemValue('passwordsalt')); return $cipher; } + + /** + * Computes a hash based on the given configuration. + * This is mostly used to find out whether configurations + * are the same. + */ + private static function makeConfigHash($config) { + $data = json_encode( + array( + 'c' => $config['class'], + 'm' => $config['mountpoint'], + 'o' => $config['options'] + ) + ); + return hash('md5', $data); + } } |