From 830676b475ad072aa7d6e69a5facae319befa52c Mon Sep 17 00:00:00 2001 From: Michael Gapczynski Date: Mon, 13 Aug 2012 17:10:36 -0400 Subject: Improve usability for configuring Dropbox and Google Drive external storage --- apps/files_external/js/dropbox.js | 46 ++- apps/files_external/js/google.js | 76 +++-- apps/files_external/lib/config.php | 530 ++++++++++++++--------------- apps/files_external/templates/settings.php | 2 +- 4 files changed, 350 insertions(+), 304 deletions(-) (limited to 'apps/files_external') diff --git a/apps/files_external/js/dropbox.js b/apps/files_external/js/dropbox.js index 08ce88b1026..92194792f42 100644 --- a/apps/files_external/js/dropbox.js +++ b/apps/files_external/js/dropbox.js @@ -1,15 +1,17 @@ $(document).ready(function() { - $('#externalStorage tbody tr').each(function() { - if ($(this).find('.backend').data('class') == 'OC_Filestorage_Dropbox') { + $('#externalStorage tbody tr.OC_Filestorage_Dropbox').each(function() { + var configured = $(this).find('[data-parameter="configured"]'); + if ($(configured).val() == 'true') { + $(this).find('.configuration input').attr('disabled', 'disabled'); + $(this).find('.configuration').append('Access granted'); + } else { var app_key = $(this).find('.configuration [data-parameter="app_key"]').val(); var app_secret = $(this).find('.configuration [data-parameter="app_secret"]').val(); - if (app_key == '' && app_secret == '') { - $(this).find('.configuration').append('Grant access'); - } else { + var config = $(this).find('.configuration'); + if (app_key != '' && app_secret != '') { var pos = window.location.search.indexOf('oauth_token') + 12 var token = $(this).find('.configuration [data-parameter="token"]'); - var access = true; if (pos != -1 && window.location.search.substr(pos, $(token).val().length) == $(token).val()) { var token_secret = $(this).find('.configuration [data-parameter="token_secret"]'); var tr = $(this); @@ -18,16 +20,31 @@ $(document).ready(function() { $(token).val(result.access_token); $(token_secret).val(result.access_token_secret); OC.MountConfig.saveStorage(tr); + $(tr).find('.configuration input').attr('disabled', 'disabled'); + $(tr).find('.configuration').append('Access granted'); } else { - access = false; OC.dialogs.alert(result.data.message, 'Error configuring Dropbox storage'); } }); } - if (access && $(this).find('.configuration #granted').length == 0) { - $(this).find('.configuration input').attr('disabled', 'disabled'); - $(this).find('.configuration').append('Access granted'); + } else if ($(this).find('.mountPoint input').val() != '' && $(config).find('[data-parameter="app_key"]').val() != '' && $(config).find('[data-parameter="app_secret"]').val() != '' && $(this).find('.dropbox').length == 0) { + $(this).find('.configuration').append('Grant access'); + } + } + }); + + $('#externalStorage tbody tr input').live('keyup', function() { + var tr = $(this).parent().parent(); + if ($(tr).hasClass('OC_Filestorage_Dropbox') && $(tr).find('[data-parameter="configured"]').val() != 'true') { + var config = $(tr).find('.configuration'); + if ($(tr).find('.mountPoint input').val() != '' && $(config).find('[data-parameter="app_key"]').val() != '' && $(config).find('[data-parameter="app_secret"]').val() != '') { + if ($(tr).find('.dropbox').length == 0) { + $(config).append('Grant access'); + } else { + $(tr).find('.dropbox').show(); } + } else if ($(tr).find('.dropbox').length > 0) { + $(tr).find('.dropbox').hide(); } } }); @@ -38,14 +55,19 @@ $(document).ready(function() { var app_secret = $(this).parent().find('[data-parameter="app_secret"]').val(); if (app_key != '' && app_secret != '') { var tr = $(this).parent().parent(); + var configured = $(this).parent().find('[data-parameter="configured"]'); var token = $(this).parent().find('[data-parameter="token"]'); var token_secret = $(this).parent().find('[data-parameter="token_secret"]'); $.post(OC.filePath('files_external', 'ajax', 'dropbox.php'), { step: 1, app_key: app_key, app_secret: app_secret, callback: window.location.href }, function(result) { if (result && result.status == 'success') { + $(configured).val('false'); $(token).val(result.data.request_token); $(token_secret).val(result.data.request_token_secret); - OC.MountConfig.saveStorage(tr); - window.location = result.data.url; + if (OC.MountConfig.saveStorage(tr)) { + window.location = result.data.url; + } else { + OC.dialogs.alert('Fill out all required fields', 'Error configuring Dropbox storage'); + } } else { OC.dialogs.alert(result.data.message, 'Error configuring Dropbox storage'); } diff --git a/apps/files_external/js/google.js b/apps/files_external/js/google.js index ede5fb2e775..7c62297df4d 100644 --- a/apps/files_external/js/google.js +++ b/apps/files_external/js/google.js @@ -1,48 +1,72 @@ $(document).ready(function() { - $('#externalStorage tbody tr').each(function() { - if ($(this).find('.backend').data('class') == 'OC_Filestorage_Google') { + $('#externalStorage tbody tr.OC_Filestorage_Google').each(function() { + var configured = $(this).find('[data-parameter="configured"]'); + if ($(configured).val() == 'true') { + $(this).find('.configuration').append('Access granted'); + } else { var token = $(this).find('[data-parameter="token"]'); var token_secret = $(this).find('[data-parameter="token_secret"]'); - if ($(token).val() == '' && $(token).val() == '') { - $(this).find('.configuration').append('Grant access'); - } else { - var params = {}; - window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m, key, value) { - params[key] = value; + var params = {}; + window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m, key, value) { + params[key] = value; + }); + if (params['oauth_token'] !== undefined && params['oauth_verifier'] !== undefined && decodeURIComponent(params['oauth_token']) == $(token).val()) { + var tr = $(this); + $.post(OC.filePath('files_external', 'ajax', 'google.php'), { step: 2, oauth_verifier: params['oauth_verifier'], request_token: $(token).val(), request_token_secret: $(token_secret).val() }, function(result) { + if (result && result.status == 'success') { + $(token).val(result.access_token); + $(token_secret).val(result.access_token_secret); + $(configured).val('true'); + OC.MountConfig.saveStorage(tr); + $(tr).find('.configuration').append('Access granted'); + } else { + OC.dialogs.alert(result.data.message, 'Error configuring Google Drive storage'); + } }); - var access = true; - if (params['oauth_token'] !== undefined && params['oauth_verifier'] !== undefined && decodeURIComponent(params['oauth_token']) == $(token).val()) { - var tr = $(this); - $.post(OC.filePath('files_external', 'ajax', 'google.php'), { step: 2, oauth_verifier: params['oauth_verifier'], request_token: $(token).val(), request_token_secret: $(token_secret).val() }, function(result) { - if (result && result.status == 'success') { - $(token).val(result.access_token); - $(token_secret).val(result.access_token_secret); - OC.MountConfig.saveStorage(tr); - } else { - access = false; - OC.dialogs.alert(result.data.message, 'Error configuring Google Drive storage'); - } - }); - } - if (access && $(this).find('.configuration #granted').length == 0) { - $(this).find('.configuration').append('Access granted'); + } else if ($(this).find('.google').length == 0) { + $(this).find('.configuration').append('Grant access'); + } + } + }); + + $('#externalStorage tbody tr').live('change', function() { + if ($(this).hasClass('OC_Filestorage_Google') && $(this).find('[data-parameter="configured"]').val() != 'true') { + if ($(this).find('.mountPoint input').val() != '') { + if ($(this).find('.google').length == 0) { + $(this).find('.configuration').append('Grant access'); } } } }); + $('#externalStorage tbody tr .mountPoint input').live('keyup', function() { + var tr = $(this).parent().parent(); + if ($(tr).hasClass('OC_Filestorage_Google') && $(tr).find('[data-parameter="configured"]').val() != 'true' && $(tr).find('.google').length > 0) { + if ($(this).val() != '') { + $(tr).find('.google').show(); + } else { + $(tr).find('.google').hide(); + } + } + }); + $('.google').live('click', function(event) { event.preventDefault(); var tr = $(this).parent().parent(); + var configured = $(this).parent().find('[data-parameter="configured"]'); var token = $(this).parent().find('[data-parameter="token"]'); var token_secret = $(this).parent().find('[data-parameter="token_secret"]'); $.post(OC.filePath('files_external', 'ajax', 'google.php'), { step: 1, callback: window.location.href }, function(result) { if (result && result.status == 'success') { + $(configured).val('false'); $(token).val(result.data.request_token); $(token_secret).val(result.data.request_token_secret); - OC.MountConfig.saveStorage(tr); - window.location = result.data.url; + if (OC.MountConfig.saveStorage(tr)) { + window.location = result.data.url; + } else { + OC.dialogs.alert('Fill out all required fields', 'Error configuring Google Drive storage'); + } } else { OC.dialogs.alert(result.data.message, 'Error configuring Google Drive storage'); } diff --git a/apps/files_external/lib/config.php b/apps/files_external/lib/config.php index 5630df77a91..f1bc16e2537 100755 --- a/apps/files_external/lib/config.php +++ b/apps/files_external/lib/config.php @@ -1,263 +1,263 @@ -. -*/ - -/** -* 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 begining of the value - * @return array - */ - public static function getBackends() { - return array( - 'OC_Filestorage_Local' => array('backend' => 'Local', 'configuration' => array('datadir' => 'Location')), - 'OC_Filestorage_AmazonS3' => array('backend' => 'Amazon S3', 'configuration' => array('key' => 'Key', 'secret' => '*Secret', 'bucket' => 'Bucket')), - 'OC_Filestorage_Dropbox' => array('backend' => 'Dropbox', 'configuration' => array('app_key' => 'App key', 'app_secret' => 'App secret', 'token' => '#token', 'token_secret' => '#token_secret'), 'custom' => 'dropbox'), - 'OC_Filestorage_FTP' => array('backend' => 'FTP', 'configuration' => array('host' => 'URL', 'user' => 'Username', 'password' => '*Password', 'root' => '&Root', 'secure' => '!Secure ftps://')), - 'OC_Filestorage_Google' => array('backend' => 'Google Drive', 'configuration' => array('token' => '#token', 'token_secret' => '#token secret'), 'custom' => 'google'), - 'OC_Filestorage_SWIFT' => array('backend' => 'OpenStack Swift', 'configuration' => array('host' => 'URL', 'user' => 'Username', 'token' => '*Token', 'root' => '&Root', 'secure' => '!Secure ftps://')), - 'OC_Filestorage_SMB' => array('backend' => 'SMB', 'configuration' => array('host' => 'URL', 'user' => 'Username', 'password' => '*Password', 'share' => 'Share', 'root' => '&Root')), - 'OC_Filestorage_DAV' => array('backend' => 'WebDAV', 'configuration' => array('host' => 'URL', 'user' => 'Username', 'password' => '*Password', 'root' => '&Root', 'secure' => '!Secure https://')) - ); - } - - /** - * 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) { - // 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())); - } - } - } - } - if (isset($mountPoints[self::MOUNT_TYPE_USER])) { - foreach ($mountPoints[self::MOUNT_TYPE_USER] as $user => $mounts) { - foreach ($mounts as $mountPoint => $mount) { - // 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))); - } - } - } - } - 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) { - // Remove '/uid/files/' from mount point - $personal[substr($mountPoint, strlen($uid) + 8)] = array('class' => $mount['class'], 'backend' => $backends[$mount['class']]['backend'], 'configuration' => $mount['options']); - } - } - return $personal; - } - - - /** - * 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_Filestorage_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 true; - } - - /** - * - * @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) { - if ($isPersonal) { - $file = OC::$SERVERROOT.'/data/'.OCP\User::getUser().'/mount.php'; - } else { - $file = OC::$SERVERROOT.'/config/mount.php'; - } - if (is_file($file)) { - $mountPoints = include($file); - 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::$SERVERROOT.'/data/'.OCP\User::getUser().'/mount.php'; - } else { - $file = OC::$SERVERROOT.'/config/mount.php'; - } - $content = " array (\n"; - foreach ($data[self::MOUNT_TYPE_GROUP] as $group => $mounts) { - $content .= "\t\t'".$group."' => array (\n"; - foreach ($mounts as $mountPoint => $mount) { - $content .= "\t\t\t'".$mountPoint."' => ".str_replace("\n", '', var_export($mount, true)).",\n"; - - } - $content .= "\t\t),\n"; - } - $content .= "\t),\n"; - } - if (isset($data[self::MOUNT_TYPE_USER])) { - $content .= "\t'user' => array (\n"; - foreach ($data[self::MOUNT_TYPE_USER] as $user => $mounts) { - $content .= "\t\t'".$user."' => array (\n"; - foreach ($mounts as $mountPoint => $mount) { - $content .= "\t\t\t'".$mountPoint."' => ".str_replace("\n", '', var_export($mount, true)).",\n"; - } - $content .= "\t\t),\n"; - } - $content .= "\t),\n"; - } - $content .= ");\n?>"; - @file_put_contents($file, $content); - } - +. +*/ + +/** +* 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 begining of the value + * @return array + */ + public static function getBackends() { + return array( + 'OC_Filestorage_Local' => array('backend' => 'Local', 'configuration' => array('datadir' => 'Location')), + 'OC_Filestorage_AmazonS3' => array('backend' => 'Amazon S3', 'configuration' => array('key' => 'Key', 'secret' => '*Secret', 'bucket' => 'Bucket')), + 'OC_Filestorage_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_Filestorage_FTP' => array('backend' => 'FTP', 'configuration' => array('host' => 'URL', 'user' => 'Username', 'password' => '*Password', 'root' => '&Root', 'secure' => '!Secure ftps://')), + 'OC_Filestorage_Google' => array('backend' => 'Google Drive', 'configuration' => array('configured' => '#configured', 'token' => '#token', 'token_secret' => '#token secret'), 'custom' => 'google'), + 'OC_Filestorage_SWIFT' => array('backend' => 'OpenStack Swift', 'configuration' => array('host' => 'URL', 'user' => 'Username', 'token' => '*Token', 'root' => '&Root', 'secure' => '!Secure ftps://')), + 'OC_Filestorage_SMB' => array('backend' => 'SMB', 'configuration' => array('host' => 'URL', 'user' => 'Username', 'password' => '*Password', 'share' => 'Share', 'root' => '&Root')), + 'OC_Filestorage_DAV' => array('backend' => 'WebDAV', 'configuration' => array('host' => 'URL', 'user' => 'Username', 'password' => '*Password', 'root' => '&Root', 'secure' => '!Secure https://')) + ); + } + + /** + * 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) { + // 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())); + } + } + } + } + if (isset($mountPoints[self::MOUNT_TYPE_USER])) { + foreach ($mountPoints[self::MOUNT_TYPE_USER] as $user => $mounts) { + foreach ($mounts as $mountPoint => $mount) { + // 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))); + } + } + } + } + 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) { + // Remove '/uid/files/' from mount point + $personal[substr($mountPoint, strlen($uid) + 8)] = array('class' => $mount['class'], 'backend' => $backends[$mount['class']]['backend'], 'configuration' => $mount['options']); + } + } + return $personal; + } + + + /** + * 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_Filestorage_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 true; + } + + /** + * + * @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) { + if ($isPersonal) { + $file = OC::$SERVERROOT.'/data/'.OCP\User::getUser().'/mount.php'; + } else { + $file = OC::$SERVERROOT.'/config/mount.php'; + } + if (is_file($file)) { + $mountPoints = include($file); + 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::$SERVERROOT.'/data/'.OCP\User::getUser().'/mount.php'; + } else { + $file = OC::$SERVERROOT.'/config/mount.php'; + } + $content = " array (\n"; + foreach ($data[self::MOUNT_TYPE_GROUP] as $group => $mounts) { + $content .= "\t\t'".$group."' => array (\n"; + foreach ($mounts as $mountPoint => $mount) { + $content .= "\t\t\t'".$mountPoint."' => ".str_replace("\n", '', var_export($mount, true)).",\n"; + + } + $content .= "\t\t),\n"; + } + $content .= "\t),\n"; + } + if (isset($data[self::MOUNT_TYPE_USER])) { + $content .= "\t'user' => array (\n"; + foreach ($data[self::MOUNT_TYPE_USER] as $user => $mounts) { + $content .= "\t\t'".$user."' => array (\n"; + foreach ($mounts as $mountPoint => $mount) { + $content .= "\t\t\t'".$mountPoint."' => ".str_replace("\n", '', var_export($mount, true)).",\n"; + } + $content .= "\t\t),\n"; + } + $content .= "\t),\n"; + } + $content .= ");\n?>"; + @file_put_contents($file, $content); + } + /** * 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/'; - if (!is_dir($path)) mkdir($path); - $result = array(); - $handle = opendir($path); - while (false !== ($file = readdir($handle))) { - if($file != '.' && $file != '..') $result[] = $file; - } - return $result; - } - - /** - * creates certificate bundle - */ - public static function createCertificateBundle() { + public static function getCertificates() { + $view = \OCP\Files::getStorage('files_external'); + $path=\OCP\Config::getSystemValue('datadirectory').$view->getAbsolutePath("").'uploads/'; + if (!is_dir($path)) mkdir($path); + $result = array(); + $handle = opendir($path); + 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(""); @@ -267,17 +267,17 @@ class OC_Mount_Config { $file=$path.'/uploads/'.$cert; $fh = fopen($file, "r"); $data = fread($fh, filesize($file)); - fclose($fh); + fclose($fh); if (strpos($data, 'BEGIN CERTIFICATE')) { - fwrite($fh_certs, $data); + fwrite($fh_certs, $data); } } - fclose($fh_certs); - - return true; - } - -} - + fclose($fh_certs); + + return true; + } + +} + ?> \ No newline at end of file diff --git a/apps/files_external/templates/settings.php b/apps/files_external/templates/settings.php index e8bc94790dc..397f0d951b0 100644 --- a/apps/files_external/templates/settings.php +++ b/apps/files_external/templates/settings.php @@ -15,7 +15,7 @@ array())); ?> $mount): ?> - > + > -- cgit v1.2.3