123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460 |
- <?php
- /**
- * ownCloud
- *
- * @author Michael Gapczynski
- * @copyright 2012 Michael Gapczynski mtgap@owncloud.com
- *
- * 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 <http://www.gnu.org/licenses/>.
- */
- /**
- * Class to configure the config/mount.php and data/$user/mount.php files
- */
- class OC_Mount_Config {
- const MOUNT_TYPE_GLOBAL = 'global';
- const MOUNT_TYPE_GROUP = 'group';
- const MOUNT_TYPE_USER = 'user';
- /**
- * 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
- * If the configuration parameter should be secret, add a '*' to the beginning of the value
- * 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 array
- */
- public static function getBackends() {
- $backends['\OC\Files\Storage\Local']=array(
- 'backend' => 'Local',
- 'configuration' => array(
- 'datadir' => 'Location'));
- $backends['\OC\Files\Storage\AmazonS3']=array(
- 'backend' => 'Amazon S3',
- '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' => 'URL',
- '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');
- $backends['\OC\Files\Storage\SWIFT']=array(
- 'backend' => 'OpenStack Swift',
- 'configuration' => array(
- 'host' => 'URL',
- 'user' => 'Username',
- 'token' => '*Token',
- 'root' => '&Root',
- 'secure' => '!Secure ftps://'));
- 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'));
- if(OC_Mount_Config::checkcurl()) $backends['\OC\Files\Storage\DAV']=array(
- 'backend' => 'ownCloud / WebDAV',
- 'configuration' => array(
- 'host' => 'URL',
- 'user' => 'Username',
- 'password' => '*Password',
- 'root' => '&Root',
- '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);
- }
- /**
- * Get the system mount points
- * The returned array is not in the same format as getUserMountPoints()
- * @return array
- */
- public static function getSystemMountPoints() {
- $mountPoints = self::readData(false);
- $backends = self::getBackends();
- $system = array();
- if (isset($mountPoints[self::MOUNT_TYPE_GROUP])) {
- foreach ($mountPoints[self::MOUNT_TYPE_GROUP] as $group => $mounts) {
- foreach ($mounts as $mountPoint => $mount) {
- // Update old classes to new namespace
- if (strpos($mount['class'], 'OC_Filestorage_') !== false) {
- $mount['class'] = '\OC\Files\Storage\\'.substr($mount['class'], 15);
- }
- // 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));
- } 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'])
- );
- }
- }
- }
- }
- if (isset($mountPoints[self::MOUNT_TYPE_USER])) {
- foreach ($mountPoints[self::MOUNT_TYPE_USER] as $user => $mounts) {
- foreach ($mounts as $mountPoint => $mount) {
- // Update old classes to new namespace
- if (strpos($mount['class'], 'OC_Filestorage_') !== false) {
- $mount['class'] = '\OC\Files\Storage\\'.substr($mount['class'], 15);
- }
- // 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));
- } 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'])
- );
- }
- }
- }
- }
- return $system;
- }
- /**
- * Get the personal mount points of the current user
- * The returned array is not in the same format as getUserMountPoints()
- * @return array
- */
- public static function getPersonalMountPoints() {
- $mountPoints = self::readData(true);
- $backends = self::getBackends();
- $uid = OCP\User::getUser();
- $personal = array();
- if (isset($mountPoints[self::MOUNT_TYPE_USER][$uid])) {
- foreach ($mountPoints[self::MOUNT_TYPE_USER][$uid] as $mountPoint => $mount) {
- // Update old classes to new namespace
- if (strpos($mount['class'], 'OC_Filestorage_') !== false) {
- $mount['class'] = '\OC\Files\Storage\\'.substr($mount['class'], 15);
- }
- // Remove '/uid/files/' from mount point
- $personal[substr($mountPoint, strlen($uid) + 8)] = array(
- 'class' => $mount['class'],
- 'backend' => $backends[$mount['class']]['backend'],
- 'configuration' => $mount['options'],
- 'status' => self::getBackendStatus($mount['class'], $mount['options'])
- );
- }
- }
- return $personal;
- }
- private static function getBackendStatus($class, $options) {
- foreach ($options as &$option) {
- $option = str_replace('$user', OCP\User::getUser(), $option);
- }
- if (class_exists($class)) {
- try {
- $storage = new $class($options);
- return $storage->test();
- } catch (Exception $exception) {
- return false;
- }
- }
- return false;
- }
- /**
- * Add a mount point to the filesystem
- * @param string Mount point
- * @param string Backend class
- * @param array Backend parameters for the class
- * @param string MOUNT_TYPE_GROUP | MOUNT_TYPE_USER
- * @param string User or group to apply mount to
- * @param bool Personal or system mount point i.e. is this being called from the personal or admin page
- * @return bool
- */
- public static function addMountPoint($mountPoint,
- $class,
- $classOptions,
- $mountType,
- $applicable,
- $isPersonal = false) {
- if ($isPersonal) {
- // Verify that the mount point applies for the current user
- // Prevent non-admin users from mounting local storage
- if ($applicable != OCP\User::getUser() || $class == '\OC\Files\Storage\Local') {
- return false;
- }
- $mountPoint = '/'.$applicable.'/files/'.ltrim($mountPoint, '/');
- } else {
- $mountPoint = '/$user/files/'.ltrim($mountPoint, '/');
- }
- $mount = array($applicable => array($mountPoint => array('class' => $class, 'options' => $classOptions)));
- $mountPoints = self::readData($isPersonal);
- // Merge the new mount point into the current mount points
- if (isset($mountPoints[$mountType])) {
- if (isset($mountPoints[$mountType][$applicable])) {
- $mountPoints[$mountType][$applicable]
- = array_merge($mountPoints[$mountType][$applicable], $mount[$applicable]);
- } else {
- $mountPoints[$mountType] = array_merge($mountPoints[$mountType], $mount);
- }
- } else {
- $mountPoints[$mountType] = $mount;
- }
- self::writeData($isPersonal, $mountPoints);
- return self::getBackendStatus($class, $classOptions);
- }
- /**
- *
- * @param string Mount point
- * @param string MOUNT_TYPE_GROUP | MOUNT_TYPE_USER
- * @param string User or group to remove mount from
- * @param bool Personal or system mount point
- * @return bool
- */
- public static function removeMountPoint($mountPoint, $mountType, $applicable, $isPersonal = false) {
- // Verify that the mount point applies for the current user
- if ($isPersonal) {
- if ($applicable != OCP\User::getUser()) {
- return false;
- }
- $mountPoint = '/'.$applicable.'/files/'.ltrim($mountPoint, '/');
- } else {
- $mountPoint = '/$user/files/'.ltrim($mountPoint, '/');
- }
- $mountPoints = self::readData($isPersonal);
- // Remove mount point
- unset($mountPoints[$mountType][$applicable][$mountPoint]);
- // Unset parent arrays if empty
- if (empty($mountPoints[$mountType][$applicable])) {
- unset($mountPoints[$mountType][$applicable]);
- if (empty($mountPoints[$mountType])) {
- unset($mountPoints[$mountType]);
- }
- }
- self::writeData($isPersonal, $mountPoints);
- return true;
- }
- /**
- * Read the mount points in the config file into an array
- * @param bool Personal or system config file
- * @return array
- */
- private static function readData($isPersonal) {
- $parser = new \OC\ArrayParser();
- if ($isPersonal) {
- $phpFile = OC_User::getHome(OCP\User::getUser()).'/mount.php';
- $jsonFile = OC_User::getHome(OCP\User::getUser()).'/mount.json';
- } else {
- $datadir = \OC_Config::getValue("datadirectory", \OC::$SERVERROOT . "/data");
- $phpFile = OC::$SERVERROOT.'/config/mount.php';
- $jsonFile = $datadir . '/mount.json';
- }
- if (is_file($jsonFile)) {
- $mountPoints = json_decode(file_get_contents($jsonFile), true);
- if (is_array($mountPoints)) {
- return $mountPoints;
- }
- } elseif (is_file($phpFile)) {
- $mountPoints = $parser->parsePHP(file_get_contents($phpFile));
- if (is_array($mountPoints)) {
- return $mountPoints;
- }
- }
- return array();
- }
- /**
- * Write the mount points to the config file
- * @param bool Personal or system config file
- * @param array Mount points
- */
- private static function writeData($isPersonal, $data) {
- if ($isPersonal) {
- $file = OC_User::getHome(OCP\User::getUser()).'/mount.json';
- } else {
- $datadir = \OC_Config::getValue("datadirectory", \OC::$SERVERROOT . "/data");
- $file = $datadir . '/mount.json';
- }
- $content = json_encode($data);
- @file_put_contents($file, $content);
- @chmod($file, 0640);
- }
- /**
- * Returns all user uploaded ssl root certificates
- * @return array
- */
- public static function getCertificates() {
- $view = \OCP\Files::getStorage('files_external');
- $path=\OCP\Config::getSystemValue('datadirectory').$view->getAbsolutePath("").'uploads/';
- \OCP\Util::writeLog('files_external', 'checking path '.$path, \OCP\Util::INFO);
- if ( ! is_dir($path)) {
- //path might not exist (e.g. non-standard OC_User::getHome() value)
- //in this case create full path using 3rd (recursive=true) parameter.
- mkdir($path, 0777, true);
- }
- $result = array();
- $handle = opendir($path);
- if ( ! $handle) {
- return array();
- }
- while (false !== ($file = readdir($handle))) {
- if ($file != '.' && $file != '..') $result[] = $file;
- }
- return $result;
- }
- /**
- * creates certificate bundle
- */
- public static function createCertificateBundle() {
- $view = \OCP\Files::getStorage("files_external");
- $path = \OCP\Config::getSystemValue('datadirectory').$view->getAbsolutePath("");
- $certs = OC_Mount_Config::getCertificates();
- $fh_certs = fopen($path."/rootcerts.crt", 'w');
- foreach ($certs as $cert) {
- $file=$path.'/uploads/'.$cert;
- $fh = fopen($file, "r");
- $data = fread($fh, filesize($file));
- fclose($fh);
- if (strpos($data, 'BEGIN CERTIFICATE')) {
- fwrite($fh_certs, $data);
- fwrite($fh_certs, "\r\n");
- }
- }
- fclose($fh_certs);
- return true;
- }
- /**
- * check if smbclient is installed
- */
- public static function checksmbclient() {
- if(function_exists('shell_exec')) {
- $output=shell_exec('which smbclient');
- return (empty($output)?false:true);
- }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_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 />';
- }
- 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);
- }
- }
|