From 7b81478667d816969468946a247d244dd5111668 Mon Sep 17 00:00:00 2001 From: Sergio Brighenti Date: Thu, 31 Jan 2019 11:48:39 +0100 Subject: [PATCH] Added maintenance mode (close #18) Optimized release zip size --- CHANGELOG.md | 3 +- Gruntfile.js | 36 ++++++++++++++++--- app/Controllers/LoginController.php | 5 +++ app/Controllers/UploadController.php | 5 +++ app/Middleware/AdminMiddleware.php | 2 -- .../CheckForMaintenanceMiddleware.php | 26 ++++++++++++++ app/routes.php | 6 ++-- bootstrap/app.php | 5 +++ composer.lock | 17 ++++----- install/index.php | 1 + resources/lang/en.lang.php | 1 + resources/lang/it.lang.php | 1 + resources/templates/dashboard/system.twig | 2 +- resources/templates/errors/maintenance.twig | 16 +++++++++ 14 files changed, 106 insertions(+), 20 deletions(-) create mode 100644 app/Middleware/CheckForMaintenanceMiddleware.php create mode 100644 resources/templates/errors/maintenance.twig diff --git a/CHANGELOG.md b/CHANGELOG.md index 4b5e005..28168ac 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,9 @@ ## v2.5 + Updated project license to GNU AGPLv3. -+ Added self update feature. ++ **[BETA]** Added self update feature. + Added partial content implementation (stream seeking on chromium based browsers). + Improved video.js alignment with large videos. ++ Optimized output zip release size. ## v2.4.1 + Fixed error message when the file is too large. (#15) diff --git a/Gruntfile.js b/Gruntfile.js index fd249e7..235aabf 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -50,11 +50,37 @@ module.exports = function (grunt) { copy: { main: { files: [ - {expand: true, cwd: 'node_modules/@fortawesome/fontawesome-free', src: ['css/**', 'js/**'], dest: 'static/fontawesome'}, - {expand: true, cwd: 'node_modules/bootstrap/dist', src: ['**'], dest: 'static/bootstrap'}, - {expand: true, cwd: 'node_modules/clipboard/dist', src: ['**'], dest: 'static/clipboardjs'}, - {expand: true, cwd: 'node_modules/video.js/dist', src: ['video.min.js', 'video-js.min.css'], dest: 'static/videojs'}, - {expand: true, cwd: 'node_modules/highlightjs', src: ['styles/**/*', 'highlight.pack.min.js'], dest: 'static/highlightjs'}, + { + expand: true, + cwd: 'node_modules/@fortawesome/fontawesome-free', + src: ['js/all.min.js'], + dest: 'static/fontawesome' + }, + { + expand: true, + cwd: 'node_modules/bootstrap/dist/css', + src: ['bootstrap.min.css'], + dest: 'static/bootstrap/css' + }, + { + expand: true, + cwd: 'node_modules/bootstrap/dist/js', + src: ['bootstrap.bundle.min.js'], + dest: 'static/bootstrap/js' + }, + {expand: true, cwd: 'node_modules/clipboard/dist', src: ['clipboard.min.js'], dest: 'static/clipboardjs'}, + { + expand: true, + cwd: 'node_modules/video.js/dist', + src: ['video.min.js', 'video-js.min.css'], + dest: 'static/videojs' + }, + { + expand: true, + cwd: 'node_modules/highlightjs', + src: ['styles/**/*', 'highlight.pack.min.js'], + dest: 'static/highlightjs' + }, {expand: true, cwd: 'node_modules/jquery/dist', src: ['jquery.min.js'], dest: 'static/jquery'} ], }, diff --git a/app/Controllers/LoginController.php b/app/Controllers/LoginController.php index 25d4871..3a7bcf1 100644 --- a/app/Controllers/LoginController.php +++ b/app/Controllers/LoginController.php @@ -37,6 +37,11 @@ class LoginController extends Controller return redirect($response, 'login'); } + if (isset($this->settings['maintenance']) && $this->settings['maintenance'] && !$result->is_admin) { + $this->session->alert(lang('maintenance_in_progress'), 'info'); + return redirect($response, 'login'); + } + if (!$result->active) { $this->session->alert(lang('account_disabled'), 'danger'); return redirect($response, 'login'); diff --git a/app/Controllers/UploadController.php b/app/Controllers/UploadController.php index 17de610..b331854 100644 --- a/app/Controllers/UploadController.php +++ b/app/Controllers/UploadController.php @@ -26,6 +26,11 @@ class UploadController extends Controller $json = ['message' => null]; + if ($this->settings['maintenance'] && !$this->database->query('SELECT `id`, `is_admin` FROM `users` WHERE `id` = ? LIMIT 1', [$this->session->get('user_id')])->fetch()->is_admin) { + $json['message'] = 'Endpoint under maintenance.'; + return $response->withJson($json, 503); + } + if ($request->getServerParam('CONTENT_LENGTH') > stringToBytes(ini_get('post_max_size'))) { $json['message'] = 'File too large (post_max_size too low).'; return $response->withJson($json, 400); diff --git a/app/Middleware/AdminMiddleware.php b/app/Middleware/AdminMiddleware.php index ab1b84e..b84519f 100644 --- a/app/Middleware/AdminMiddleware.php +++ b/app/Middleware/AdminMiddleware.php @@ -18,9 +18,7 @@ class AdminMiddleware extends Middleware public function __invoke(Request $request, Response $response, callable $next) { if (!$this->database->query('SELECT `id`, `is_admin` FROM `users` WHERE `id` = ? LIMIT 1', [$this->session->get('user_id')])->fetch()->is_admin) { - $this->session->alert('Your account is not admin anymore.', 'danger'); $this->session->set('admin', false); - $this->session->set('redirectTo', (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"); throw new UnauthorizedException(); } diff --git a/app/Middleware/CheckForMaintenanceMiddleware.php b/app/Middleware/CheckForMaintenanceMiddleware.php new file mode 100644 index 0000000..a4280ea --- /dev/null +++ b/app/Middleware/CheckForMaintenanceMiddleware.php @@ -0,0 +1,26 @@ +settings['maintenance']) && $this->settings['maintenance'] && !$this->database->query('SELECT `id`, `is_admin` FROM `users` WHERE `id` = ? LIMIT 1', [$this->session->get('user_id')])->fetch()->is_admin) { + throw new MaintenanceException(); + } + + return $next($request, $response); + } +} \ No newline at end of file diff --git a/app/routes.php b/app/routes.php index c833f08..8a27fa6 100644 --- a/app/routes.php +++ b/app/routes.php @@ -39,7 +39,7 @@ $app->group('', function () { $this->get('/upload/{id}/raw', \App\Controllers\UploadController::class . ':getRawById')->add(\App\Middleware\AdminMiddleware::class)->setName('upload.raw'); $this->post('/upload/{id}/delete', \App\Controllers\UploadController::class . ':delete')->setName('upload.delete'); -})->add(\App\Middleware\AuthMiddleware::class); +})->add(App\Middleware\CheckForMaintenanceMiddleware::class)->add(\App\Middleware\AuthMiddleware::class); $app->get('/', \App\Controllers\DashboardController::class . ':redirects')->setName('root'); $app->get('/login', \App\Controllers\LoginController::class . ':show')->setName('login.show'); @@ -49,7 +49,7 @@ $app->map(['GET', 'POST'], '/logout', \App\Controllers\LoginController::class . $app->post('/upload', \App\Controllers\UploadController::class . ':upload')->setName('upload'); $app->get('/{userCode}/{mediaCode}', \App\Controllers\UploadController::class . ':show')->setName('public'); -$app->get('/{userCode}/{mediaCode}/delete/{token}', \App\Controllers\UploadController::class . ':show')->setName('public.delete.show'); -$app->post('/{userCode}/{mediaCode}/delete/{token}', \App\Controllers\UploadController::class . ':deleteByToken')->setName('public.delete'); +$app->get('/{userCode}/{mediaCode}/delete/{token}', \App\Controllers\UploadController::class . ':show')->setName('public.delete.show')->add(\App\Middleware\CheckForMaintenanceMiddleware::class);; +$app->post('/{userCode}/{mediaCode}/delete/{token}', \App\Controllers\UploadController::class . ':deleteByToken')->setName('public.delete')->add(\App\Middleware\CheckForMaintenanceMiddleware::class);; $app->get('/{userCode}/{mediaCode}/raw', \App\Controllers\UploadController::class . ':showRaw')->setName('public.raw')->setOutputBuffering(false); $app->get('/{userCode}/{mediaCode}/download', \App\Controllers\UploadController::class . ':download')->setName('public.download')->setOutputBuffering(false); \ No newline at end of file diff --git a/bootstrap/app.php b/bootstrap/app.php index 5fba1f0..e2f577b 100644 --- a/bootstrap/app.php +++ b/bootstrap/app.php @@ -22,6 +22,7 @@ $config = array_replace_recursive([ 'base_url' => isset($_SERVER['HTTPS']) ? 'https://' . $_SERVER['HTTP_HOST'] : 'http://' . $_SERVER['HTTP_HOST'], 'storage_dir' => 'storage', 'displayErrorDetails' => false, + 'maintenance' => false, 'db' => [ 'connection' => 'sqlite', 'dsn' => __DIR__ . '/../resources/database/xbackbone.db', @@ -91,6 +92,10 @@ $container['view'] = function ($container) use (&$config) { $container['errorHandler'] = function ($container) { return function (\Slim\Http\Request $request, \Slim\Http\Response $response, $exception) use (&$container) { + if ($exception instanceof \App\Exceptions\MaintenanceException) { + return $container->view->render($response->withStatus(503), 'errors/maintenance.twig'); + } + if ($exception instanceof \App\Exceptions\UnauthorizedException) { return $container->view->render($response->withStatus(403), 'errors/403.twig'); } diff --git a/composer.lock b/composer.lock index 16658b2..401d981 100644 --- a/composer.lock +++ b/composer.lock @@ -1,10 +1,10 @@ { "_readme": [ "This file locks the dependencies of your project to a known state", - "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "content-hash": "3ffe3637bbcca9dc78923aca4ffdbbe6", + "content-hash": "79d2941e9db0718ad1f2766ff912f324", "packages": [ { "name": "container-interop/container-interop", @@ -859,16 +859,16 @@ }, { "name": "twig/twig", - "version": "v2.6.0", + "version": "v2.6.2", "source": { "type": "git", "url": "https://github.com/twigphp/Twig.git", - "reference": "a11dd39f5b6589e14f0ff3b36675d06047c589b1" + "reference": "7d7342c8a4059fefb9b8d07db0cc14007021f9b7" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/twigphp/Twig/zipball/a11dd39f5b6589e14f0ff3b36675d06047c589b1", - "reference": "a11dd39f5b6589e14f0ff3b36675d06047c589b1", + "url": "https://api.github.com/repos/twigphp/Twig/zipball/7d7342c8a4059fefb9b8d07db0cc14007021f9b7", + "reference": "7d7342c8a4059fefb9b8d07db0cc14007021f9b7", "shasum": "" }, "require": { @@ -922,7 +922,7 @@ "keywords": [ "templating" ], - "time": "2018-12-16T10:36:48+00:00" + "time": "2019-01-14T15:00:48+00:00" } ], "packages-dev": [], @@ -935,7 +935,8 @@ "php": ">=7.1", "ext-json": "*", "ext-gd": "*", - "ext-pdo": "*" + "ext-pdo": "*", + "ext-zip": "*" }, "platform-dev": [] } diff --git a/install/index.php b/install/index.php index 641c75b..b257503 100644 --- a/install/index.php +++ b/install/index.php @@ -14,6 +14,7 @@ $config = [ 'base_url' => isset($_SERVER['HTTPS']) ? 'https://' . $_SERVER['HTTP_HOST'] : 'http://' . $_SERVER['HTTP_HOST'], 'storage_dir' => 'storage', 'displayErrorDetails' => true, + 'maintenance' => false, 'db' => [ 'connection' => 'sqlite', 'dsn' => 'resources/database/xbackbone.db', diff --git a/resources/lang/en.lang.php b/resources/lang/en.lang.php index 02cac6c..1f7c637 100644 --- a/resources/lang/en.lang.php +++ b/resources/lang/en.lang.php @@ -93,4 +93,5 @@ return [ 'check_for_updates' => 'Check for updates', 'upgrade' => 'Upgrade', 'updates' => 'Updates', + 'maintenance_in_progress' => 'Platform under maintenance, try again later...', ]; \ No newline at end of file diff --git a/resources/lang/it.lang.php b/resources/lang/it.lang.php index 1f5e029..119ce51 100644 --- a/resources/lang/it.lang.php +++ b/resources/lang/it.lang.php @@ -93,4 +93,5 @@ return [ 'check_for_updates' => 'Controllo aggiornamenti', 'upgrade' => 'Upgrade', 'updates' => 'Aggiornamenti', + 'maintenance_in_progress' => 'Manutenzione in corso, riprova piĆ¹ tardi.', ]; \ No newline at end of file diff --git a/resources/templates/dashboard/system.twig b/resources/templates/dashboard/system.twig index 525a74d..c887ab7 100644 --- a/resources/templates/dashboard/system.twig +++ b/resources/templates/dashboard/system.twig @@ -76,7 +76,7 @@
-
{{ lang('updates') }}
+
{{ lang('updates') }} [BETA]
diff --git a/resources/templates/errors/maintenance.twig b/resources/templates/errors/maintenance.twig new file mode 100644 index 0000000..b30a1a5 --- /dev/null +++ b/resources/templates/errors/maintenance.twig @@ -0,0 +1,16 @@ +{% extends 'base.twig' %} + +{% block title %}Under maintenance{% endblock %} + +{% block content %} +
+
+
+
+

Under maintenance

+

We'll be back very soon! :)

+
+
+
+
+{% endblock %} \ No newline at end of file