Implement lint
This commit is contained in:
+20
-33
@@ -6,9 +6,8 @@ use Franzz\Objects\PhpObject;
|
||||
use Franzz\Objects\ToolBox;
|
||||
|
||||
//TODO Keep only local specificities and move bulk to Franzz\Objects\Controller
|
||||
class Controller extends PhpObject
|
||||
{
|
||||
const MUTATING_ACTIONS = array(
|
||||
class Controller extends PhpObject {
|
||||
private const MUTATING_ACTIONS = [
|
||||
'add_post',
|
||||
'subscribe',
|
||||
'unsubscribe',
|
||||
@@ -21,34 +20,31 @@ class Controller extends PhpObject
|
||||
'admin_set',
|
||||
'admin_create',
|
||||
'admin_delete'
|
||||
);
|
||||
const SESSION_WRITING_ACTIONS = array(
|
||||
];
|
||||
private const SESSION_WRITING_ACTIONS = [
|
||||
'login',
|
||||
'logout'
|
||||
);
|
||||
];
|
||||
|
||||
private Livetrail $oLivetrail;
|
||||
private array $asReq;
|
||||
private string $sCsrfToken = '';
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
public function __construct() {
|
||||
parent::__construct(__CLASS__);
|
||||
}
|
||||
|
||||
private function setReqVal(string $sKey, $oValue, string $sValidation=''): void
|
||||
{
|
||||
private function setReqVal(string $sKey, $oValue, string $sValidation=''): void {
|
||||
$this->asReq[$sKey] = $this->validateValue($sValidation, $oValue);
|
||||
}
|
||||
|
||||
public function handle($sProcessPage, array $argv = array()): string
|
||||
{
|
||||
public function handle($sProcessPage, array $argv = []): string {
|
||||
//Start buffering so warnings/notices can be collected
|
||||
ob_start();
|
||||
|
||||
//Parse variables
|
||||
$asReq = ToolBox::getRequest($argv);
|
||||
$this->asReq = array();
|
||||
$this->asReq = [];
|
||||
$sAction = $asReq['a'] ?? '';
|
||||
$this->setReqVal('t', $asReq['t'] ?? '');
|
||||
$this->setReqVal('name', $asReq['name'] ?? '');
|
||||
@@ -90,8 +86,7 @@ class Controller extends PhpObject
|
||||
return $sResult;
|
||||
}
|
||||
|
||||
private function validateMutationRequest(string $sAction): bool
|
||||
{
|
||||
private function validateMutationRequest(string $sAction): bool {
|
||||
return
|
||||
PHP_SAPI === 'cli'
|
||||
||
|
||||
@@ -101,44 +96,38 @@ class Controller extends PhpObject
|
||||
;
|
||||
}
|
||||
|
||||
private function getCsrfToken(): string
|
||||
{
|
||||
private function getCsrfToken(): string {
|
||||
if($this->sCsrfToken === '') $this->initCsrfToken();
|
||||
return $this->sCsrfToken;
|
||||
}
|
||||
|
||||
private function setCsrfToken(): void
|
||||
{
|
||||
private function setCsrfToken(): void {
|
||||
if(empty($_SESSION['csrf_token'])) $_SESSION['csrf_token'] = bin2hex(random_bytes(32));
|
||||
$this->sCsrfToken = $_SESSION['csrf_token'];
|
||||
}
|
||||
|
||||
private function initCsrfToken(): void
|
||||
{
|
||||
private function initCsrfToken(): void {
|
||||
if(PHP_SAPI === 'cli') return;
|
||||
|
||||
if(session_status() !== PHP_SESSION_ACTIVE) {
|
||||
$bSecure = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') || (($_SERVER['HTTP_X_FORWARDED_PROTO'] ?? '') === 'https');
|
||||
session_set_cookie_params(array('httponly' => true, 'secure' => $bSecure, 'samesite' => 'Lax'));
|
||||
session_set_cookie_params(['httponly' => true, 'secure' => $bSecure, 'samesite' => 'Lax']);
|
||||
session_start();
|
||||
}
|
||||
|
||||
$this->setCsrfToken();
|
||||
}
|
||||
|
||||
private function checkCsrfToken(string $sClientToken): bool
|
||||
{
|
||||
private function checkCsrfToken(string $sClientToken): bool {
|
||||
$sServerToken = $this->getCsrfToken();
|
||||
return PHP_SAPI === 'cli' || ($sServerToken !== '' && is_string($sClientToken) && hash_equals($sServerToken, $sClientToken));
|
||||
}
|
||||
|
||||
private function closeSession(): void
|
||||
{
|
||||
private function closeSession(): void {
|
||||
if(session_status() === PHP_SESSION_ACTIVE) session_write_close();
|
||||
}
|
||||
|
||||
private function dispatch(string $sAction): string
|
||||
{
|
||||
private function dispatch(string $sAction): string {
|
||||
return match($sAction) {
|
||||
'markers' => $this->oLivetrail->getMarkers(),
|
||||
'last_update' => $this->oLivetrail->getLastUpdate(),
|
||||
@@ -154,8 +143,7 @@ class Controller extends PhpObject
|
||||
};
|
||||
}
|
||||
|
||||
private function dispatchAdmin(string $sAction): string
|
||||
{
|
||||
private function dispatchAdmin(string $sAction): string {
|
||||
if(!$this->oLivetrail->checkUserClearance(User::CLEARANCE_ADMIN)) {
|
||||
return Livetrail::getJsonResult(false, Livetrail::NOT_FOUND);
|
||||
}
|
||||
@@ -173,11 +161,10 @@ class Controller extends PhpObject
|
||||
};
|
||||
}
|
||||
|
||||
private static function validateValue(string $sValidation, $oValue=0)
|
||||
{
|
||||
private static function validateValue(string $sValidation, $oValue=0) {
|
||||
return match($sValidation) {
|
||||
'' => $oValue,
|
||||
'positiveInt' => filter_var($oValue, FILTER_VALIDATE_INT, array('options' => array('default' => 0, 'min_range' => 0)))
|
||||
'positiveInt' => filter_var($oValue, FILTER_VALIDATE_INT, ['options' => ['default' => 0, 'min_range' => 0]])
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user