From 3d248c8cb4f0a3638982ec5c46e83bc7a3db207b Mon Sep 17 00:00:00 2001 From: SergiX44 Date: Mon, 16 Aug 2021 13:55:31 +0200 Subject: [PATCH] support for theme-park.dev --- CHANGELOG.md | 7 +++ app/Controllers/AdminController.php | 14 ++++-- app/Controllers/Controller.php | 2 - app/Controllers/SettingController.php | 49 +++++++++---------- app/Controllers/UpgradeController.php | 19 ++++---- app/Web/Theme.php | 68 +++++++++++++++++++++++++++ composer.json | 2 +- install/index.php | 15 +++--- src/js/app.js | 3 ++ 9 files changed, 129 insertions(+), 50 deletions(-) create mode 100644 app/Web/Theme.php diff --git a/CHANGELOG.md b/CHANGELOG.md index e4e64d7..46eddd1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,13 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). +## Unreleased +### Added +- Support for theme-park.dev themes. + +### Fixed +- Wrong css when reapplying the default theme. + ## [3.4.1] - 2021-08-11 ### Added - Toggle to disable embeds. diff --git a/app/Controllers/AdminController.php b/app/Controllers/AdminController.php index 3e3acd5..b56e412 100644 --- a/app/Controllers/AdminController.php +++ b/app/Controllers/AdminController.php @@ -3,6 +3,7 @@ namespace App\Controllers; use App\Database\Migrator; +use App\Web\Theme; use League\Flysystem\FileNotFoundException; use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ServerRequestInterface as Request; @@ -26,7 +27,8 @@ class AdminController extends Controller $settings[$setting->key] = $setting->value; } - $settings['default_user_quota'] = humanFileSize($this->getSetting('default_user_quota', stringToBytes('1G')), 0, true); + $settings['default_user_quota'] = humanFileSize($this->getSetting('default_user_quota', stringToBytes('1G')), 0, + true); return view()->render($response, 'dashboard/system.twig', [ 'usersCount' => $usersCount = $this->database->query('SELECT COUNT(*) AS `count` FROM `users`')->fetch()->count, @@ -77,13 +79,15 @@ class AdminController extends Controller */ public function getThemes(Response $response): Response { - $apiJson = json_decode(file_get_contents('https://bootswatch.com/api/4.json')); + $themes = make(Theme::class)->availableThemes(); $out = []; - $out['Default - Bootstrap 4 default theme'] = self::DEFAULT_THEME_URL; - foreach ($apiJson->themes as $theme) { - $out["{$theme->name} - {$theme->description}"] = $theme->cssMin; + foreach ($themes as $vendor => $list) { + $out["-- {$vendor} --"] = null; + foreach ($list as $name => $url) { + $out[$name] = "{$vendor}|{$url}"; + } } return json($response, $out); diff --git a/app/Controllers/Controller.php b/app/Controllers/Controller.php index 6135c8d..0d6e5c1 100644 --- a/app/Controllers/Controller.php +++ b/app/Controllers/Controller.php @@ -27,8 +27,6 @@ use Psr\Http\Message\ServerRequestInterface as Request; */ abstract class Controller { - protected const DEFAULT_THEME_URL = 'https://bootswatch.com/4/_vendor/bootstrap/dist/css/bootstrap.min.css'; - /** @var Container */ protected $container; diff --git a/app/Controllers/SettingController.php b/app/Controllers/SettingController.php index 207e7ea..bb8d3a6 100644 --- a/app/Controllers/SettingController.php +++ b/app/Controllers/SettingController.php @@ -4,8 +4,11 @@ namespace App\Controllers; use App\Database\Repositories\UserRepository; +use App\Web\Theme; use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ServerRequestInterface as Request; +use Slim\Exception\HttpBadRequestException; +use Slim\Exception\HttpInternalServerErrorException; class SettingController extends Controller { @@ -14,6 +17,7 @@ class SettingController extends Controller * @param Response $response * * @return Response + * @throws HttpInternalServerErrorException */ public function saveSettings(Request $request, Response $response): Response { @@ -64,28 +68,29 @@ class SettingController extends Controller } } - /** * @param Request $request + * @throws HttpInternalServerErrorException */ public function applyTheme(Request $request) { - if (param($request, 'css') !== null) { - if (!is_writable(BASE_DIR.'static/bootstrap/css/bootstrap.min.css')) { - $this->session->alert(lang('cannot_write_file'), 'danger'); - } else { - file_put_contents( - BASE_DIR.'static/bootstrap/css/bootstrap.min.css', - file_get_contents(param($request, 'css')) - ); - } + $css = param($request, 'css'); + if ($css === null) { + return; + } - // if is default, remove setting - if (param($request, 'css') !== self::DEFAULT_THEME_URL) { - $this->updateSetting('css', param($request, 'css')); - } else { - $this->database->query('DELETE FROM `settings` WHERE `key` = \'css\''); - } + if (!is_writable(BASE_DIR.'static/bootstrap/css/bootstrap.min.css')) { + $this->session->alert(lang('cannot_write_file'), 'danger'); + throw new HttpInternalServerErrorException($request); + } + + make(Theme::class)->applyTheme($css); + + // if is default, remove setting + if ($css !== Theme::default()) { + $this->updateSetting('css', $css); + } else { + $this->database->query('DELETE FROM `settings` WHERE `key` = \'css\''); } } @@ -96,15 +101,11 @@ class SettingController extends Controller private function updateSetting($key, $value = null) { if (!$this->database->query('SELECT `value` FROM `settings` WHERE `key` = '.$this->database->getPdo()->quote($key))->fetch()) { - $this->database->query( - 'INSERT INTO `settings`(`key`, `value`) VALUES ('.$this->database->getPdo()->quote($key).', ?)', - $value - ); + $this->database->query('INSERT INTO `settings`(`key`, `value`) VALUES ('.$this->database->getPdo()->quote($key).', ?)', + $value); } else { - $this->database->query( - 'UPDATE `settings` SET `value`=? WHERE `key` = '.$this->database->getPdo()->quote($key), - $value - ); + $this->database->query('UPDATE `settings` SET `value`=? WHERE `key` = '.$this->database->getPdo()->quote($key), + $value); } } } diff --git a/app/Controllers/UpgradeController.php b/app/Controllers/UpgradeController.php index 79be905..ba6d395 100644 --- a/app/Controllers/UpgradeController.php +++ b/app/Controllers/UpgradeController.php @@ -123,8 +123,8 @@ class UpgradeController extends Controller public function checkForUpdates(Request $request, Response $response): Response { $jsonResponse = [ - 'status' => null, - 'message' => null, + 'status' => 'OK', + 'message' => lang('already_latest_version'), 'upgrade' => false, ]; @@ -133,17 +133,18 @@ class UpgradeController extends Controller try { $json = $this->getApiJson(); - $jsonResponse['status'] = 'OK'; foreach ($json as $release) { - if (version_compare($release->tag_name, PLATFORM_VERSION, '>') && ($release->prerelease === $acceptPrerelease)) { + if ( + $release->prerelease === $acceptPrerelease && + version_compare($release->tag_name, PLATFORM_VERSION, '>') && + version_compare($release->tag_name, '4.0.0', '<') + ) { $jsonResponse['message'] = lang('new_version_available', [$release->tag_name]); $jsonResponse['upgrade'] = true; break; } if (version_compare($release->tag_name, PLATFORM_VERSION, '<=')) { - $jsonResponse['message'] = lang('already_latest_version'); - $jsonResponse['upgrade'] = false; break; } } @@ -154,10 +155,10 @@ class UpgradeController extends Controller return json($response, $jsonResponse); } - + /** - * @param Request $request - * @param Response $response + * @param Request $request + * @param Response $response * @return Response * @throws \Twig\Error\LoaderError * @throws \Twig\Error\RuntimeError diff --git a/app/Web/Theme.php b/app/Web/Theme.php new file mode 100644 index 0000000..1aa267f --- /dev/null +++ b/app/Web/Theme.php @@ -0,0 +1,68 @@ +themes as $theme) { + $bootswatch["{$theme->name} - {$theme->description}"] = $theme->cssMin; + } + + $apiJson = json_decode(file_get_contents('https://theme-park.dev/themes.json')); + $base = $apiJson->applications->xbackbone->base_css; + + $themepark = []; + foreach ($apiJson->themes as $name => $urls) { + $themepark[$name] = "{$base},{$urls->url}"; + } + + return [ + 'default' => $default, + 'bootswatch.com' => $bootswatch, + 'theme-park.dev' => $themepark + ]; + } + + /** + * @param string $input + * @return bool + */ + public function applyTheme(string $input): bool + { + [$vendor, $css] = explode('|', $input, 2); + + if ($vendor === 'theme-park.dev') { + [$base, $theme] = explode(',', $css); + $data = file_get_contents(self::DEFAULT_THEME_URL).file_get_contents($base).file_get_contents($theme); + } else { + $data = file_get_contents($css ?? self::DEFAULT_THEME_URL); + } + + return (bool) file_put_contents( + BASE_DIR.'static/bootstrap/css/bootstrap.min.css', + $data + ); + } + + /** + * @return string + */ + public static function default(): string + { + return 'default|'.self::DEFAULT_THEME_URL; + } + +} diff --git a/composer.json b/composer.json index 014835a..c3924fe 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "sergix44/xbackbone", "license": "AGPL-3.0-only", - "version": "3.4.1", + "version": "3.5.0", "description": "A lightweight ShareX PHP backend", "type": "project", "require": { diff --git a/install/index.php b/install/index.php index bedbc11..d561856 100644 --- a/install/index.php +++ b/install/index.php @@ -1,6 +1,6 @@ = 7 && PHP_MINOR_VERSION >= 1) || PHP_MAJOR_VERSION > 7) ?: die('Sorry, PHP 7.1 or above is required to run XBackBone.'); +((PHP_MAJOR_VERSION >= 7 && PHP_MINOR_VERSION >= 2) || PHP_MAJOR_VERSION > 7) ?: die('Sorry, PHP 7.2 or above is required to run XBackBone.'); require __DIR__.'/../vendor/autoload.php'; use App\Database\Migrator; @@ -175,12 +175,12 @@ $app->post('/', function (Request $request, Response $response, \DI\Container $c $storage = $container->get('storage'); // check if the storage is valid - $storageTestFile = 'storage_test.xbackbone.txt'; + $storageTestFile = 'storage_test.txt'; try { try { - $success = $storage->write($storageTestFile, 'XBACKBONE_TEST_FILE'); + $success = $storage->write($storageTestFile, 'TEST_FILE'); } catch (FileExistsException $fileExistsException) { - $success = $storage->update($storageTestFile, 'XBACKBONE_TEST_FILE'); + $success = $storage->update($storageTestFile, 'TEST_FILE'); } if (!$success) { @@ -212,11 +212,8 @@ $app->post('/', function (Request $request, Response $response, \DI\Container $c // re-apply the previous theme if is present $css = $db->query('SELECT `value` FROM `settings` WHERE `key` = \'css\'')->fetch()->value ?? null; - if ($css) { - $content = file_get_contents($css); - if ($content !== false) { - file_put_contents(BASE_DIR.'static/bootstrap/css/bootstrap.min.css', $content); - } + if ($css && strpos($css, '|') !== false) { + $container->make(\App\Web\Theme::class)->applyTheme($css); } // if is upgrading and existing installation, put it out maintenance diff --git a/src/js/app.js b/src/js/app.js index 6a3adb9..a2a2eea 100644 --- a/src/js/app.js +++ b/src/js/app.js @@ -114,6 +114,9 @@ var app = { var opt = document.createElement('option'); opt.value = value; opt.innerHTML = key; + if (value === null) { + opt.disabled = true; + } $themes.append(opt); }); });