From 2038eec6d72e1d4357184a424c95049f36ddfb50 Mon Sep 17 00:00:00 2001 From: Philipp Date: Mon, 4 Oct 2021 11:40:05 +0200 Subject: [PATCH] Add WebDavConfig --- webdav_storage/src/WebDav.php | 123 ++-------------------- webdav_storage/src/WebDavConfig.php | 156 ++++++++++++++++++++++++++++ webdav_storage/webdav_storage.php | 12 ++- 3 files changed, 177 insertions(+), 114 deletions(-) create mode 100644 webdav_storage/src/WebDavConfig.php diff --git a/webdav_storage/src/WebDav.php b/webdav_storage/src/WebDav.php index fbe7413a..4502b935 100644 --- a/webdav_storage/src/WebDav.php +++ b/webdav_storage/src/WebDav.php @@ -3,8 +3,6 @@ namespace Friendica\Addon\webdav_storage\src; use Exception; -use Friendica\Core\Config\IConfig; -use Friendica\Core\L10n; use Friendica\Model\Storage\IWritableStorage; use Friendica\Model\Storage\ReferenceStorageException; use Friendica\Model\Storage\StorageException; @@ -20,12 +18,6 @@ class WebDav implements IWritableStorage { const NAME = 'WebDav'; - /** @var L10n */ - private $l10n; - - /** @var IConfig */ - private $config; - /** @var string */ private $url; @@ -38,27 +30,23 @@ class WebDav implements IWritableStorage /** @var array */ private $authOptions; - public function __construct(L10n $l10n, IConfig $config, IHTTPClient $client, LoggerInterface $logger) + /** + * WebDav constructor + * + * @param string $url The full URL to the webdav endpoint (including the subdirectories) + * @param array $authOptions The authentication options for the http calls ( ['username', 'password', 'auth_type'] ) + * @param IHTTPClient $client The http client for communicating with the WebDav endpoint + * @param LoggerInterface $logger The standard logging class + */ + public function __construct(string $url, array $authOptions, IHTTPClient $client, LoggerInterface $logger) { - $this->l10n = $l10n; - $this->config = $config; $this->client = $client; $this->logger = $logger; - $this->authOptions = null; - - if (!empty($this->config->get('webdav', 'username'))) { - $this->authOptions = [ - $this->config->get('webdav', 'username'), - (string)$this->config->get('webdav', 'password', ''), - $this->config->get('webdav', 'auth_type', 'basic') - ]; - } - - $this->url = $this->config->get('webdav', 'url'); + $this->authOptions = $authOptions; + $this->url = $url; } - /** * Split data ref and return file path * @@ -262,95 +250,6 @@ class WebDav implements IWritableStorage $this->checkAndDeletePath($file[1]); } - /** - * @inheritDoc - */ - public function getOptions(): array - { - $auths = [ - '' => 'None', - 'basic' => 'Basic', - 'digest' => 'Digest', - ]; - - return [ - 'url' => [ - 'input', - $this->l10n->t('URL'), - $this->url, - $this->l10n->t('URL to the Webdav endpoint, where files can be saved'), - true - ], - 'username' => [ - 'input', - $this->l10n->t('Username'), - $this->config->get('webdav', 'username', ''), - $this->l10n->t('Username to authenticate to the Webdav endpoint') - ], - 'password' => [ - 'password', - $this->l10n->t('Password'), - $this->config->get('webdav', 'username', ''), - $this->l10n->t('Password to authenticate to the Webdav endpoint') - ], - 'auth_type' => [ - 'select', - $this->l10n->t('Authentication type'), - $this->config->get('webdav', 'auth_type', ''), - $this->l10n->t('authentication type to the Webdav endpoint'), - $auths, - ] - ]; - } - - /** - * @inheritDoc - */ - public function saveOptions(array $data): array - { - $url = $data['url'] ?? ''; - $username = $data['username'] ?? ''; - $password = $data['password'] ?? ''; - - $auths = [ - '' => 'None', - 'basic' => 'Basic', - 'digest' => 'Digest', - ]; - - $authType = $data['auth_type'] ?? ''; - if (!key_exists($authType, $auths)) { - return [ - 'auth_type' => $this->l10n->t('Authentication type is invalid.'), - ]; - } - - $options = null; - - if (!empty($username)) { - $options = [ - $username, - $password, - $authType - ]; - } - - if (!$this->client->head($url, [HTTPClientOptions::AUTH => $options])->isSuccess()) { - return [ - 'url' => $this->l10n->t('url is either invalid or not reachable'), - ]; - } - - $this->config->set('webdav', 'url', $url); - $this->config->set('webdav', 'username', $username); - $this->config->set('webdav', 'password', $password); - $this->config->set('webdav', 'auth_type', $authType); - - $this->url = $url; - - return []; - } - /** * {@inheritDoc} */ diff --git a/webdav_storage/src/WebDavConfig.php b/webdav_storage/src/WebDavConfig.php new file mode 100644 index 00000000..5e2bbaf9 --- /dev/null +++ b/webdav_storage/src/WebDavConfig.php @@ -0,0 +1,156 @@ +url; + } + + /** + * @return array + */ + public function getAuthOptions(): array + { + return $this->authOptions; + } + + public function __construct(L10n $l10n, IConfig $config, IHTTPClient $client) + { + $this->l10n = $l10n; + $this->config = $config; + $this->client = $client; + + $this->authOptions = null; + + if (!empty($this->config->get('webdav', 'username'))) { + $this->authOptions = [ + $this->config->get('webdav', 'username'), + (string)$this->config->get('webdav', 'password', ''), + $this->config->get('webdav', 'auth_type', 'basic') + ]; + } + + $this->url = $this->config->get('webdav', 'url'); + } + + /** + * @inheritDoc + */ + public function getOptions(): array + { + $auths = [ + '' => 'None', + 'basic' => 'Basic', + 'digest' => 'Digest', + ]; + + return [ + 'url' => [ + 'input', + $this->l10n->t('URL'), + $this->url, + $this->l10n->t('URL to the Webdav endpoint, where files can be saved'), + true + ], + 'username' => [ + 'input', + $this->l10n->t('Username'), + $this->config->get('webdav', 'username', ''), + $this->l10n->t('Username to authenticate to the Webdav endpoint') + ], + 'password' => [ + 'password', + $this->l10n->t('Password'), + $this->config->get('webdav', 'username', ''), + $this->l10n->t('Password to authenticate to the Webdav endpoint') + ], + 'auth_type' => [ + 'select', + $this->l10n->t('Authentication type'), + $this->config->get('webdav', 'auth_type', ''), + $this->l10n->t('authentication type to the Webdav endpoint'), + $auths, + ] + ]; + } + + /** + * @inheritDoc + */ + public function saveOptions(array $data): array + { + $url = $data['url'] ?? ''; + $username = $data['username'] ?? ''; + $password = $data['password'] ?? ''; + + $auths = [ + '' => 'None', + 'basic' => 'Basic', + 'digest' => 'Digest', + ]; + + $authType = $data['auth_type'] ?? ''; + if (!key_exists($authType, $auths)) { + return [ + 'auth_type' => $this->l10n->t('Authentication type is invalid.'), + ]; + } + + $options = null; + + if (!empty($username)) { + $options = [ + $username, + $password, + $authType + ]; + } + + if (!$this->client->head($url, [HTTPClientOptions::AUTH => $options])->isSuccess()) { + return [ + 'url' => $this->l10n->t('url is either invalid or not reachable'), + ]; + } + + $this->config->set('webdav', 'url', $url); + $this->config->set('webdav', 'username', $username); + $this->config->set('webdav', 'password', $password); + $this->config->set('webdav', 'auth_type', $authType); + + $this->url = $url; + + return []; + } +} diff --git a/webdav_storage/webdav_storage.php b/webdav_storage/webdav_storage.php index 422a5041..7686ca9f 100644 --- a/webdav_storage/webdav_storage.php +++ b/webdav_storage/webdav_storage.php @@ -7,6 +7,7 @@ */ use Friendica\Addon\webdav_storage\src\WebDav; +use Friendica\Addon\webdav_storage\src\WebDavConfig; use Friendica\App; use Friendica\Core\Hook; use Friendica\DI; @@ -14,15 +15,22 @@ use Friendica\DI; function webdav_storage_install($a) { Hook::register('storage_instance' , __FILE__, 'webdav_storage_instance'); + Hook::register('storage_config' , __FILE__, 'webdav_storage_config'); DI::storageManager()->register(WebDav::class); } function webdav_storage_uninstall() { - DI::storageManager()->unregister(WebDav::getName()); + DI::storageManager()->unregister(WebDav::class); } function webdav_storage_instance(App $a, array &$data) { - $data['storage'] = new WebDav(DI::l10n(), DI::config(), DI::httpClient(), DI::logger()); + $config = new WebDavConfig(DI::l10n(), DI::config(), DI::httpClient()); + $data['storage'] = new WebDav($config->getUrl(), $config->getAuthOptions(), DI::httpClient(), DI::logger()); +} + +function webdav_storage_config(App $a, array &$data) +{ + $data['storage_config'] = new WebDavConfig(DI::l10n(), DI::config(), DI::httpClient()); } -- 2.39.5