Cleanup code

This commit is contained in:
Giuseppe Criscione 2018-09-11 18:10:18 +02:00
parent 114eae00bf
commit 37e32d8cd4
6 changed files with 95 additions and 67 deletions

View File

@ -88,38 +88,65 @@ class Admin
public function run()
{
if (HTTPRequest::method() === 'POST') {
if (!is_null(HTTPRequest::contentLength())) {
$maxSize = FileSystem::shorthandToBytes(ini_get('post_max_size'));
if (HTTPRequest::contentLength() > $maxSize && $maxSize > 0) {
$this->notify($this->label('request.error.post-max-size'), 'error');
$this->redirectToReferer(302, true);
}
}
try {
CSRFToken::validate();
} catch (RuntimeException $e) {
CSRFToken::destroy();
Session::remove('FORMWORK_USERNAME');
$this->notify($this->label('login.suspicious-request-detected'), 'warning');
if (HTTPRequest::isXHR()) {
JSONResponse::error('Not authorized!', 403)->send();
}
$this->redirect('/login/', 302, true);
}
$this->validateContentLength();
$this->validateCSRFToken();
}
if ($this->users->isEmpty()) {
if ($this->router->request() !== '/') {
$this->redirectToPanel(302, true);
}
$controller = new Controllers\Register();
return $controller->register();
$this->registerAdmin();
}
if (!$this->isLoggedIn() && HTTPRequest::uri() !== '/login/') {
$this->redirect('/login/', 302, true);
}
$this->loadRoutes();
$this->router->dispatch();
if (!$this->router->hasDispatched()) {
$this->errors->notFound();
}
}
protected function validateContentLength()
{
if (!is_null(HTTPRequest::contentLength())) {
$maxSize = FileSystem::shorthandToBytes(ini_get('post_max_size'));
if (HTTPRequest::contentLength() > $maxSize && $maxSize > 0) {
$this->notify($this->label('request.error.post-max-size'), 'error');
$this->redirectToReferer(302, true);
}
}
}
protected function validateCSRFToken()
{
try {
CSRFToken::validate();
} catch (RuntimeException $e) {
CSRFToken::destroy();
Session::remove('FORMWORK_USERNAME');
$this->notify($this->label('login.suspicious-request-detected'), 'warning');
if (HTTPRequest::isXHR()) {
JSONResponse::error('Not authorized!', 403)->send();
}
$this->redirect('/login/', 302, true);
}
}
protected function registerAdmin()
{
if ($this->router->request() !== '/') {
$this->redirectToPanel(302, true);
}
$controller = new Controllers\Register();
return $controller->register();
}
protected function loadRoutes()
{
// Default route
$this->router->add(
'/',
function (RouteParams $params) {
@ -127,6 +154,7 @@ class Admin
}
);
// Authentication
$this->router->add(
array('GET', 'POST'),
'/login/',
@ -137,11 +165,41 @@ class Admin
array(new Controllers\Authentication(), 'logout')
);
// Cache
$this->router->add(
'/dashboard/',
array(new Controllers\Dashboard(), 'run')
'XHR',
'POST',
'/cache/clear/',
array(new Controllers\Cache(), 'clear')
);
// Dashboard
$this->router->add(
'/dashboard/',
array(new Controllers\Dashboard(), 'index')
);
// Options
$this->router->add(
'/options/',
array(new Controllers\Options(), 'index')
);
$this->router->add(
array('GET', 'POST'),
'/options/system/',
array(new Controllers\Options(), 'system')
);
$this->router->add(
array('GET', 'POST'),
'/options/site/',
array(new Controllers\Options(), 'site')
);
$this->router->add(
'/options/info/',
array(new Controllers\Options(), 'info')
);
// Pages
$this->router->add(
'/pages/',
array(new Controllers\Pages(), 'index')
@ -178,28 +236,10 @@ class Admin
array(new Controllers\Pages(), 'delete')
);
$this->router->add(
'/options/',
array(new Controllers\Options(), 'run')
);
$this->router->add(
array('GET', 'POST'),
'/options/system/',
array(new Controllers\Options(), 'system')
);
$this->router->add(
array('GET', 'POST'),
'/options/site/',
array(new Controllers\Options(), 'site')
);
$this->router->add(
'/options/info/',
array(new Controllers\Options(), 'info')
);
// Users
$this->router->add(
'/users/',
array(new Controllers\Users(), 'run')
array(new Controllers\Users(), 'index')
);
$this->router->add(
'POST',
@ -216,19 +256,6 @@ class Admin
'/users/{user}/profile/',
array(new Controllers\Users(), 'profile')
);
$this->router->add(
'XHR',
'POST',
'/cache/clear/',
array(new Controllers\Cache(), 'clear')
);
$this->router->dispatch();
if (!$this->router->hasDispatched()) {
$this->errors->notFound();
}
}
protected function loadLanguages()

View File

@ -9,7 +9,7 @@ use Formwork\Core\Formwork;
class Dashboard extends AbstractController
{
public function run()
public function index()
{
$site = Formwork::instance()->site();
$csrfToken = CSRFToken::get();

View File

@ -7,19 +7,18 @@ use Formwork\Admin\Security\CSRFToken;
use Formwork\Core\Formwork;
use Formwork\Data\DataGetter;
use Formwork\Parsers\YAML;
use Formwork\Router\RouteParams;
use Formwork\Utils\FileSystem;
use Formwork\Utils\Header;
use Formwork\Utils\HTTPRequest;
class Options extends AbstractController
{
public function run(RouteParams $params)
public function index()
{
$this->redirect('/options/system/', 302, true);
}
public function system(RouteParams $params)
public function system()
{
$fields = new Fields(YAML::parseFile(SCHEMES_PATH . 'system.yml'));
@ -56,7 +55,7 @@ class Options extends AbstractController
));
}
public function site(RouteParams $params)
public function site()
{
$fields = new Fields(YAML::parseFile(SCHEMES_PATH . 'site.yml'));
@ -98,7 +97,7 @@ class Options extends AbstractController
));
}
public function info(RouteParams $params)
public function info()
{
$dependencies = $this->getDependencies();

View File

@ -84,7 +84,7 @@ class Pages extends AbstractController
));
}
public function create(RouteParams $params)
public function create()
{
$this->data = new DataGetter(HTTPRequest::postData());

View File

@ -16,7 +16,7 @@ use RuntimeException;
class Users extends AbstractController
{
public function run(RouteParams $params)
public function index()
{
$content = $this->view(
'users.index',
@ -46,7 +46,7 @@ class Users extends AbstractController
));
}
public function create(RouteParams $params)
public function create()
{
$this->data = new DataGetter(HTTPRequest::postData());

View File

@ -36,7 +36,9 @@ class Statistics
);
protected $visitsRegistry;
protected $uniqueVisitsRegistry;
protected $visitorsRegistry;
public function __construct()