support for theme-park.dev

This commit is contained in:
SergiX44 2021-08-16 13:55:31 +02:00
parent 6ca60757ae
commit 3d248c8cb4
9 changed files with 129 additions and 50 deletions

View file

@ -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.

View file

@ -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);

View file

@ -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;

View file

@ -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);
}
}
}

View file

@ -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;
}
}
@ -156,8 +157,8 @@ class UpgradeController extends Controller
}
/**
* @param Request $request
* @param Response $response
* @param Request $request
* @param Response $response
* @return Response
* @throws \Twig\Error\LoaderError
* @throws \Twig\Error\RuntimeError

68
app/Web/Theme.php Normal file
View file

@ -0,0 +1,68 @@
<?php
namespace App\Web;
class Theme
{
public const DEFAULT_THEME_URL = 'https://bootswatch.com/4/_vendor/bootstrap/dist/css/bootstrap.min.css';
/**
* @return array
*/
public function availableThemes(): array
{
$apiJson = json_decode(file_get_contents('https://bootswatch.com/api/4.json'));
$default = [];
$default['Default - Bootstrap 4 default theme'] = self::DEFAULT_THEME_URL;
$bootswatch = [];
foreach ($apiJson->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;
}
}

View file

@ -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": {

View file

@ -1,6 +1,6 @@
<?php
((PHP_MAJOR_VERSION >= 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

View file

@ -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);
});
});