From 7065a60686e1da0a51b9235e0d0750cf797d6d6e Mon Sep 17 00:00:00 2001 From: binsky Date: Sat, 13 Mar 2021 20:05:16 +0100 Subject: [PATCH] migrate admin additional settings implementation --- appinfo/info.xml | 3 + lib/AppInfo/Application.php | 3 - lib/Settings/Admin.php | 113 ++++++++++++++++++++++++ templates/{part.admin.php => admin.php} | 43 +-------- templates/admin.settings.php | 4 - 5 files changed, 120 insertions(+), 46 deletions(-) create mode 100644 lib/Settings/Admin.php rename templates/{part.admin.php => admin.php} (78%) delete mode 100644 templates/admin.settings.php diff --git a/appinfo/info.xml b/appinfo/info.xml index c0ec63b2..adf76a25 100644 --- a/appinfo/info.xml +++ b/appinfo/info.xml @@ -56,4 +56,7 @@ For an demo of this app visit [https://demo.passman.cc](https://demo.passman.cc) + + OCA\Passman\Settings\Admin + diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index 13869283..5ce37d99 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -115,14 +115,11 @@ class Application extends App implements IBootstrap { } public function boot(IBootContext $context): void { - $l = \OC::$server->getL10N(self::APP_ID); - /** @var IManager $manager */ $manager = $context->getAppContainer()->get(IManager::class); $manager->registerNotifierService(Notifier::class); Util::addTranslations(self::APP_ID); - \OCP\App::registerAdmin(self::APP_ID, 'templates/admin.settings'); } /** diff --git a/lib/Settings/Admin.php b/lib/Settings/Admin.php new file mode 100644 index 00000000..8ed3ccba --- /dev/null +++ b/lib/Settings/Admin.php @@ -0,0 +1,113 @@ +. + * + */ + +namespace OCA\Passman\Settings; + +use GuzzleHttp\Client; +use GuzzleHttp\Exception\GuzzleException; +use OCP\App; +use OCP\AppFramework\Http\TemplateResponse; +use OCP\IConfig; +use OCP\IL10N; +use OCP\Settings\ISettings; + +class Admin implements ISettings { + + protected IConfig $config; + private IL10N $l; + + /** + * Admin constructor. + * @param IConfig $config + * @param IL10N $l + */ + public function __construct(IConfig $config, IL10N $l) { + $this->config = $config; + $this->l = $l; + } + + /** + * @return TemplateResponse + */ + public function getForm(): TemplateResponse { + $checkVersion = $this->config->getAppValue('passman', 'check_version', '1') === '1'; + $AppInstance = new App(); + $localVersion = $AppInstance->getAppInfo("passman")["version"]; + $githubVersion = $this->l->t('Unable to get version info'); + if ($checkVersion) { + // get latest master version + $version = false; + + $url = 'https://raw.githubusercontent.com/nextcloud/passman/master/appinfo/info.xml'; + try { + $httpClient = new Client(); + $response = $httpClient->request('get', $url); + $xml = $response->getBody()->getContents(); + } catch (GuzzleException $e) { + $xml = false; + } + + if ($xml) { + $data = @simplexml_load_string($xml); + + // libxml_disable_entity_loader is deprecated with php8, the vulnerability is disabled by default by libxml with php8 + if (\PHP_VERSION_ID < 80000) { + $loadEntities = libxml_disable_entity_loader(true); + $data = @simplexml_load_string($xml); + libxml_disable_entity_loader($loadEntities); + } + + if ($data !== false) { + $version = (string)$data->version; + } else { + libxml_clear_errors(); + } + } + + if ($version !== false) { + $githubVersion = $version; + } + } + // $ciphers = openssl_get_cipher_methods(); + + return new TemplateResponse('passman', 'admin', [ + 'localVersion' => $localVersion, + 'githubVersion' => $githubVersion, + 'checkVersion' => $checkVersion, + ], 'blank'); + } + + /** + * @return string + */ + public function getSection(): string { + return 'additional'; + } + + /** + * @return int + */ + public function getPriority(): int { + return 100; + } +} diff --git a/templates/part.admin.php b/templates/admin.php similarity index 78% rename from templates/part.admin.php rename to templates/admin.php index 1f583cf3..1598eef1 100644 --- a/templates/part.admin.php +++ b/templates/admin.php @@ -1,58 +1,23 @@ getConfig()->getAppValue('passman', 'check_version', '1') === '1'; -$AppInstance = new App(); -$localVersion = $AppInstance->getAppInfo("passman")["version"]; -$githubVersion = $l->t('Unable to get version info'); -if ($checkVersion) { - // get latest master version - $version = false; - - $url = 'https://raw.githubusercontent.com/nextcloud/passman/master/appinfo/info.xml'; - try { - $client = OC::$server->getHTTPClientService()->newClient(); - $response = $client->get($url); - $xml = $response->getBody(); - } catch (\Exception $e) { - $xml = false; - } - - if ($xml) { - $loadEntities = libxml_disable_entity_loader(true); - $data = @simplexml_load_string($xml); - libxml_disable_entity_loader($loadEntities); - if ($data !== false) { - $version = (string)$data->version; - } else { - libxml_clear_errors(); - } - } - - if ($version !== false) { - $githubVersion = $version; - } -} -$ciphers = openssl_get_cipher_methods(); ?>

t('Passman Settings')); ?>

t('GitHub version:') . ' ' . $githubVersion); + if ($_['checkVersion']) { + p($l->t('GitHub version:') . ' ' . $_['githubVersion']); print '
'; } ?> - Local version:
+ Local version:
t('A newer version of Passman is available')); } ?> diff --git a/templates/admin.settings.php b/templates/admin.settings.php deleted file mode 100644 index b5280153..00000000 --- a/templates/admin.settings.php +++ /dev/null @@ -1,4 +0,0 @@ -fetchPage(); \ No newline at end of file