formwork/panel/routes.php

261 lines
9.0 KiB
PHP
Raw Normal View History

<?php
2022-11-27 19:30:41 +01:00
use Formwork\Formwork;
2022-12-11 16:48:45 +01:00
use Formwork\Panel\Security\CSRFToken;
2022-11-27 19:30:41 +01:00
use Formwork\Response\JSONResponse;
use Formwork\Response\RedirectResponse;
use Formwork\Utils\FileSystem;
2022-11-27 19:30:41 +01:00
use Formwork\Utils\HTTPRequest;
use Formwork\Utils\Session;
return [
'routes' => [
2022-11-27 21:21:19 +01:00
'panel.index' => [
'path' => '/',
2023-05-20 21:26:49 +02:00
'action' => fn () => new RedirectResponse(Formwork::instance()->panel()->uri('/dashboard/')),
],
2022-12-11 16:48:45 +01:00
2022-11-27 21:21:19 +01:00
'panel.login' => [
'path' => '/login/',
2022-11-27 21:21:19 +01:00
'action' => 'Formwork\\Panel\\Controllers\\AuthenticationController@login',
2023-05-20 21:26:49 +02:00
'methods' => ['GET', 'POST'],
],
2022-12-11 16:48:45 +01:00
2022-11-27 21:21:19 +01:00
'panel.logout' => [
'path' => '/logout/',
2023-05-20 21:26:49 +02:00
'action' => 'Formwork\\Panel\\Controllers\\AuthenticationController@logout',
],
2022-12-11 16:48:45 +01:00
2022-11-27 21:21:19 +01:00
'panel.backup.make' => [
'path' => '/backup/make/',
2022-11-27 21:21:19 +01:00
'action' => 'Formwork\\Panel\\Controllers\\BackupController@make',
'methods' => ['POST'],
2023-05-20 21:26:49 +02:00
'types' => ['XHR'],
],
2022-12-11 16:48:45 +01:00
2022-11-27 21:21:19 +01:00
'panel.backup.download' => [
'path' => '/backup/download/{backup}/',
2022-11-27 21:21:19 +01:00
'action' => 'Formwork\\Panel\\Controllers\\BackupController@download',
2023-05-20 21:26:49 +02:00
'methods' => ['POST'],
],
2022-12-11 16:48:45 +01:00
2022-11-27 21:21:19 +01:00
'panel.cache.clear' => [
'path' => '/cache/clear/',
2022-11-27 21:21:19 +01:00
'action' => 'Formwork\\Panel\\Controllers\\CacheController@clear',
'methods' => ['POST'],
2023-05-20 21:26:49 +02:00
'types' => ['XHR'],
],
2022-12-11 16:48:45 +01:00
2022-11-27 21:21:19 +01:00
'panel.dashboard' => [
'path' => '/dashboard/',
2023-05-20 21:26:49 +02:00
'action' => 'Formwork\Panel\Controllers\DashboardController@index',
],
2022-12-11 16:48:45 +01:00
2022-11-27 21:21:19 +01:00
'panel.options' => [
'path' => '/options/',
2023-05-20 21:26:49 +02:00
'action' => 'Formwork\Panel\Controllers\OptionsController@index',
],
2022-12-11 16:48:45 +01:00
2022-11-27 21:21:19 +01:00
'panel.options.system' => [
'path' => '/options/system/',
2022-11-27 21:21:19 +01:00
'action' => 'Formwork\Panel\Controllers\OptionsController@systemOptions',
2023-05-20 21:26:49 +02:00
'methods' => ['GET', 'POST'],
],
2022-12-11 16:48:45 +01:00
2022-11-27 21:21:19 +01:00
'panel.options.site' => [
'path' => '/options/site/',
2022-11-27 21:21:19 +01:00
'action' => 'Formwork\Panel\Controllers\OptionsController@siteOptions',
2023-05-20 21:26:49 +02:00
'methods' => ['GET', 'POST'],
],
2022-12-11 16:48:45 +01:00
2022-11-27 21:21:19 +01:00
'panel.options.updates' => [
'path' => '/options/updates/',
2023-05-20 21:26:49 +02:00
'action' => 'Formwork\Panel\Controllers\OptionsController@updates',
],
2022-12-11 16:48:45 +01:00
2022-11-27 21:21:19 +01:00
'panel.options.info' => [
'path' => '/options/info/',
2023-05-20 21:26:49 +02:00
'action' => 'Formwork\Panel\Controllers\OptionsController@info',
],
2022-12-11 16:48:45 +01:00
2022-11-27 21:21:19 +01:00
'panel.pages' => [
'path' => '/pages/',
2023-05-20 21:26:49 +02:00
'action' => 'Formwork\Panel\Controllers\PagesController@index',
],
2022-12-11 16:48:45 +01:00
2022-11-27 21:21:19 +01:00
'panel.pages.new' => [
'path' => '/pages/new/',
2022-11-27 21:21:19 +01:00
'action' => 'Formwork\Panel\Controllers\PagesController@create',
2023-05-20 21:26:49 +02:00
'methods' => ['POST'],
],
2022-12-11 16:48:45 +01:00
2022-11-27 21:21:19 +01:00
'panel.pages.edit' => [
'path' => '/pages/{page}/edit/',
2022-11-27 21:21:19 +01:00
'action' => 'Formwork\Panel\Controllers\PagesController@edit',
2023-05-20 21:26:49 +02:00
'methods' => ['GET', 'POST'],
],
2022-12-11 16:48:45 +01:00
2022-11-27 21:21:19 +01:00
'panel.pages.edit.lang' => [
'path' => '/pages/{page}/edit/language/{language}/',
2022-11-27 21:21:19 +01:00
'action' => 'Formwork\Panel\Controllers\PagesController@edit',
2023-05-20 21:26:49 +02:00
'methods' => ['GET', 'POST'],
],
2022-12-11 16:48:45 +01:00
2022-11-27 21:21:19 +01:00
'panel.pages.reorder' => [
'path' => '/pages/reorder/',
2022-11-27 21:21:19 +01:00
'action' => 'Formwork\Panel\Controllers\PagesController@reorder',
'methods' => ['POST'],
2023-05-20 21:26:49 +02:00
'types' => ['XHR'],
],
2022-12-11 16:48:45 +01:00
2022-11-27 21:21:19 +01:00
'panel.pages.uploadfile' => [
'path' => '/pages/{page}/file/upload/',
2022-11-27 21:21:19 +01:00
'action' => 'Formwork\Panel\Controllers\PagesController@uploadFile',
2023-05-20 21:26:49 +02:00
'methods' => ['POST'],
],
2022-12-11 16:48:45 +01:00
2022-11-27 21:21:19 +01:00
'panel.pages.deletefile' => [
'path' => '/pages/{page}/file/{filename}/delete/',
2022-11-27 21:21:19 +01:00
'action' => 'Formwork\Panel\Controllers\PagesController@deleteFile',
2023-05-20 21:26:49 +02:00
'methods' => ['POST'],
],
2022-12-11 16:48:45 +01:00
2022-11-27 21:21:19 +01:00
'panel.pages.delete' => [
'path' => '/pages/{page}/delete/',
2022-11-27 21:21:19 +01:00
'action' => 'Formwork\Panel\Controllers\PagesController@delete',
2023-05-20 21:26:49 +02:00
'methods' => ['POST'],
],
2022-12-11 16:48:45 +01:00
2022-11-27 21:21:19 +01:00
'panel.pages.delete.lang' => [
'path' => '/pages/{page}/delete/language/{language}/',
2022-11-27 21:21:19 +01:00
'action' => 'Formwork\Panel\Controllers\PagesController@delete',
2023-05-20 21:26:49 +02:00
'methods' => ['POST'],
],
2022-12-11 16:48:45 +01:00
2022-11-27 21:21:19 +01:00
'panel.updates.check' => [
'path' => '/updates/check/',
2022-11-27 21:21:19 +01:00
'action' => 'Formwork\Panel\Controllers\UpdatesController@check',
'methods' => ['POST'],
2023-05-20 21:26:49 +02:00
'types' => ['XHR'],
],
2022-12-11 16:48:45 +01:00
2022-11-27 21:21:19 +01:00
'panel.updates.update' => [
'path' => '/updates/update/',
2022-11-27 21:21:19 +01:00
'action' => 'Formwork\Panel\Controllers\UpdatesController@update',
'methods' => ['POST'],
2023-05-20 21:26:49 +02:00
'types' => ['XHR'],
],
2022-12-11 16:48:45 +01:00
2022-11-27 21:21:19 +01:00
'panel.users' => [
'path' => '/users/',
2023-05-20 21:26:49 +02:00
'action' => 'Formwork\Panel\Controllers\UsersController@index',
],
2022-12-11 16:48:45 +01:00
2022-11-27 21:21:19 +01:00
'panel.users.new' => [
'path' => '/users/new/',
2022-11-27 21:21:19 +01:00
'action' => 'Formwork\Panel\Controllers\UsersController@create',
2023-05-20 21:26:49 +02:00
'methods' => ['POST'],
],
2022-12-11 16:48:45 +01:00
2022-11-27 21:21:19 +01:00
'panel.users.delete' => [
'path' => '/users/{user}/delete/',
2022-11-27 21:21:19 +01:00
'action' => 'Formwork\Panel\Controllers\UsersController@delete',
2023-05-20 21:26:49 +02:00
'methods' => ['POST'],
],
2022-12-11 16:48:45 +01:00
2022-11-27 21:21:19 +01:00
'panel.users.profile' => [
'path' => '/users/{user}/profile/',
2022-11-27 21:21:19 +01:00
'action' => 'Formwork\Panel\Controllers\UsersController@profile',
2023-05-20 21:26:49 +02:00
'methods' => ['GET', 'POST'],
2022-11-23 22:50:16 +01:00
],
2022-12-11 16:48:45 +01:00
'panel.register' => [
'path' => '/register/',
'action' => 'Formwork\Panel\Controllers\RegisterController@register',
2023-05-20 21:26:49 +02:00
'methods' => ['GET', 'POST'],
],
2022-12-11 16:48:45 +01:00
2022-11-27 21:21:19 +01:00
'panel.errors.notfound' => [
2022-12-11 16:48:45 +01:00
'path' => '/{route}/',
2023-05-20 21:26:49 +02:00
'action' => 'Formwork\Panel\Controllers\ErrorsController@notFound',
],
],
2022-12-11 16:48:45 +01:00
'filters' => [
2022-12-06 21:26:54 +01:00
'request.validateSize' => [
'action' => static function () {
// Validate HTTP request Content-Length according to `post_max_size` directive
if (HTTPRequest::contentLength() !== null) {
$maxSize = FileSystem::shorthandToBytes(ini_get('post_max_size'));
if (HTTPRequest::contentLength() > $maxSize && $maxSize > 0) {
$panel = Formwork::instance()->panel();
$panel->notify(
2022-12-06 21:26:54 +01:00
Formwork::instance()->translations()->getCurrent()->translate('panel.request.error.postMaxSize'),
'error'
);
return new RedirectResponse($panel->uri());
}
}
},
2023-05-20 21:26:49 +02:00
'methods' => ['POST'],
],
2022-12-06 21:26:54 +01:00
'request.validateCsrf' => [
'action' => static function () {
// Validate CSRF token
try {
2022-11-27 19:30:41 +01:00
CSRFToken::validate();
} catch (RuntimeException $e) {
2022-11-27 19:30:41 +01:00
CSRFToken::destroy();
Session::remove('FORMWORK_USERNAME');
$panel = Formwork::instance()->panel();
$panel->notify(
2022-12-06 21:26:54 +01:00
Formwork::instance()->translations()->getCurrent()->translate('panel.login.suspiciousRequestDetected'),
'warning'
);
2022-11-27 19:30:41 +01:00
if (HTTPRequest::isXHR()) {
return JSONResponse::error('Bad Request: the CSRF token is not valid', 400);
}
return new RedirectResponse($panel->uri('/login/'));
}
},
'methods' => ['POST'],
2023-05-20 21:26:49 +02:00
'types' => ['HTTP', 'XHR'],
],
2022-11-27 21:21:19 +01:00
'panel.register' => [
'action' => static function () {
2022-11-27 21:21:19 +01:00
$panel = Formwork::instance()->panel();
2022-11-27 21:21:19 +01:00
// Register panel if no user exists
if ($panel->users()->isEmpty()) {
2022-11-27 19:30:41 +01:00
if (!HTTPRequest::isLocalhost()) {
return new RedirectResponse(Formwork::instance()->site()->uri());
}
if ($panel->route() !== '/register/') {
return new RedirectResponse($panel->uri('/register/'));
}
}
},
2023-05-20 21:26:49 +02:00
'methods' => ['GET', 'POST'],
],
2022-12-06 21:26:54 +01:00
'panel.redirectToLogin' => [
'action' => static function () {
2022-11-27 21:21:19 +01:00
$panel = Formwork::instance()->panel();
// Redirect to login if no user is logged
if (!$panel->users()->isEmpty() && !$panel->isLoggedIn() && $panel->route() !== '/login/') {
2022-11-27 21:21:19 +01:00
Session::set('FORMWORK_REDIRECT_TO', $panel->route());
return new RedirectResponse($panel->uri('/login/'));
}
2023-05-20 21:26:49 +02:00
},
],
],
];