Change mandatory folder locations & remove obsolete masks
This commit is contained in:
76
inc/Main.php
76
inc/Main.php
@@ -20,9 +20,6 @@ abstract class Main extends PhpObject
|
|||||||
const ACTIVE = 1;
|
const ACTIVE = 1;
|
||||||
const INACTIVE = 0;
|
const INACTIVE = 0;
|
||||||
|
|
||||||
//Folders
|
|
||||||
const MASKS_FOLDER = 'masks/';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* DB Handle
|
* DB Handle
|
||||||
* @var Db
|
* @var Db
|
||||||
@@ -36,10 +33,7 @@ abstract class Main extends PhpObject
|
|||||||
const LAYOUT_TIME_FORMAT = 'G:i';
|
const LAYOUT_TIME_FORMAT = 'G:i';
|
||||||
|
|
||||||
//Variables
|
//Variables
|
||||||
protected $asMasks;
|
|
||||||
protected $asContext;
|
protected $asContext;
|
||||||
protected string $sCsrfToken = '';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Language Translator
|
* Language Translator
|
||||||
* @var Translator
|
* @var Translator
|
||||||
@@ -93,8 +87,6 @@ abstract class Main extends PhpObject
|
|||||||
$sScheme = $_SERVER['HTTP_X_FORWARDED_PROTO'] ?? $_SERVER['REQUEST_SCHEME'] ?? 'https';
|
$sScheme = $_SERVER['HTTP_X_FORWARDED_PROTO'] ?? $_SERVER['REQUEST_SCHEME'] ?? 'https';
|
||||||
$sAppPath = $sScheme.'://'.str_replace(array('http://', 'https://'), '', $sServerName.dirname($_SERVER['SCRIPT_NAME']));
|
$sAppPath = $sScheme.'://'.str_replace(array('http://', 'https://'), '', $sServerName.dirname($_SERVER['SCRIPT_NAME']));
|
||||||
$this->asContext['serv_name'] = $sAppPath.(mb_substr($sAppPath, -1)!='/'?'/':'');
|
$this->asContext['serv_name'] = $sAppPath.(mb_substr($sAppPath, -1)!='/'?'/':'');
|
||||||
|
|
||||||
$this->setMasks();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function addTimestampToFilePath($sFilePath)
|
public static function addTimestampToFilePath($sFilePath)
|
||||||
@@ -106,46 +98,6 @@ abstract class Main extends PhpObject
|
|||||||
return file_exists($sCleanedFilePath)?$sCleanedFilePath.'?'.date("YmdHis", filemtime($sCleanedFilePath)):$sFilePath;
|
return file_exists($sCleanedFilePath)?$sCleanedFilePath.'?'.date("YmdHis", filemtime($sCleanedFilePath)):$sFilePath;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function getCsrfToken() {
|
|
||||||
if($this->sCsrfToken === '') $this->initCsrfToken();
|
|
||||||
return $this->sCsrfToken;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function setCsrfToken() {
|
|
||||||
if(empty($_SESSION['csrf_token'])) $_SESSION['csrf_token'] = bin2hex(random_bytes(32));
|
|
||||||
$this->sCsrfToken = $_SESSION['csrf_token'];
|
|
||||||
}
|
|
||||||
|
|
||||||
private function initCsrfToken() {
|
|
||||||
if(PHP_SAPI === 'cli') return;
|
|
||||||
|
|
||||||
$bCloseSession = false;
|
|
||||||
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_start();
|
|
||||||
$bCloseSession = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->setCsrfToken();
|
|
||||||
if($bCloseSession) session_write_close();
|
|
||||||
}
|
|
||||||
|
|
||||||
public function checkCsrfToken(string $sClientToken) {
|
|
||||||
$sServerToken = $this->getCsrfToken();
|
|
||||||
return PHP_SAPI === 'cli' || ($sServerToken !== '' && is_string($sClientToken) && hash_equals($sServerToken, $sClientToken));
|
|
||||||
}
|
|
||||||
|
|
||||||
public function validateMutationRequest($sAction, $sCsrfToken='') {
|
|
||||||
return
|
|
||||||
PHP_SAPI === 'cli' //Ignore internal cron job
|
|
||||||
||
|
|
||||||
!in_array($sAction, static::MUTATING_ACTIONS, true) //Ignore non-sensitive requests
|
|
||||||
||
|
|
||||||
($_SERVER['REQUEST_METHOD'] ?? '') === 'POST' && $this->checkCsrfToken($sCsrfToken) //Only accept POST requests and valid CSRF token
|
|
||||||
;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function addUncaughtError($sError)
|
public function addUncaughtError($sError)
|
||||||
{
|
{
|
||||||
$this->addError('Uncaught errors:'."\n".$sError);
|
$this->addError('Uncaught errors:'."\n".$sError);
|
||||||
@@ -161,7 +113,7 @@ abstract class Main extends PhpObject
|
|||||||
* @param array $asCachePages Pages to cache in constants
|
* @param array $asCachePages Pages to cache in constants
|
||||||
* @return string HTML Mask
|
* @return string HTML Mask
|
||||||
*/
|
*/
|
||||||
public function getMainPage($asGlobalVars=array(), $sMainPage='index', $asMainPageInfo=array(), $asCachePages=array())
|
public function getMainPage($asGlobalVars=array(), $sMainPage='index', $asMainPageInfo=array())
|
||||||
{
|
{
|
||||||
$asDefaultConsts = array(
|
$asDefaultConsts = array(
|
||||||
'success' => self::SUCCESS,
|
'success' => self::SUCCESS,
|
||||||
@@ -171,16 +123,6 @@ abstract class Main extends PhpObject
|
|||||||
);
|
);
|
||||||
$asGlobalVars['consts'] = array_merge($asDefaultConsts, array_key_exists('consts', $asGlobalVars)?$asGlobalVars['consts']:array());
|
$asGlobalVars['consts'] = array_merge($asDefaultConsts, array_key_exists('consts', $asGlobalVars)?$asGlobalVars['consts']:array());
|
||||||
|
|
||||||
//Masks
|
|
||||||
if(empty($asCachePages)) $asCachePages = array_values($this->asMasks);
|
|
||||||
foreach($asCachePages as $sPage) {
|
|
||||||
if($sPage != $sMainPage) {
|
|
||||||
$oMask = new Mask($sPage, $this->oLang);
|
|
||||||
$oMask->setTags($asDefaultConsts);
|
|
||||||
$asGlobalVars['consts']['pages'][$sPage] = $oMask->getMask();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if(!is_null($this->oLang)) {
|
if(!is_null($this->oLang)) {
|
||||||
$asGlobalVars['consts']['lang_prefix'] = Mask::LANG_PREFIX;
|
$asGlobalVars['consts']['lang_prefix'] = Mask::LANG_PREFIX;
|
||||||
$asGlobalVars['consts']['lang'] = $this->oLang->getTranslations();
|
$asGlobalVars['consts']['lang'] = $this->oLang->getTranslations();
|
||||||
@@ -200,22 +142,10 @@ abstract class Main extends PhpObject
|
|||||||
return $oMainMask->getMask();
|
return $oMainMask->getMask();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function getPageContent($sPage)
|
|
||||||
{
|
|
||||||
return ToolBox::fixEOL(file_get_contents(self::MASKS_FOLDER.$sPage.'.html'));
|
|
||||||
}
|
|
||||||
|
|
||||||
/* DB structure. See Db::__construct */
|
/* DB structure. See Db::__construct */
|
||||||
|
|
||||||
protected abstract function getSqlOptions();
|
protected abstract function getSqlOptions();
|
||||||
|
|
||||||
private function setMasks()
|
|
||||||
{
|
|
||||||
//List all available masks
|
|
||||||
$asMaskPaths = glob(Mask::getMaskFile('*'));
|
|
||||||
$this->asMasks = array_map('basename', $asMaskPaths, array_fill(1, count($asMaskPaths), Mask::MASK_EXT));
|
|
||||||
}
|
|
||||||
|
|
||||||
public static function getJsonResult($bSuccess, $sDesc, $asVars=array())
|
public static function getJsonResult($bSuccess, $sDesc, $asVars=array())
|
||||||
{
|
{
|
||||||
header('Content-type: application/json');
|
header('Content-type: application/json');
|
||||||
@@ -237,8 +167,4 @@ abstract class Main extends PhpObject
|
|||||||
http_response_code(404);
|
http_response_code(404);
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function validatePositiveInt($oValue=0) {
|
|
||||||
return filter_var($oValue, FILTER_VALIDATE_INT, array('options' => array('default' => 0, 'min_range' => 0)));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ class Mask extends PhpObject
|
|||||||
private $oLang;
|
private $oLang;
|
||||||
private $sTimezone;
|
private $sTimezone;
|
||||||
|
|
||||||
const MASK_FOLDER = '../src/masks/';
|
const MASK_FOLDER = '../resources/masks';
|
||||||
const MASK_EXT = '.html';
|
const MASK_EXT = '.html';
|
||||||
const START_TAG = 'START';
|
const START_TAG = 'START';
|
||||||
const END_TAG = 'END';
|
const END_TAG = 'END';
|
||||||
@@ -79,7 +79,7 @@ class Mask extends PhpObject
|
|||||||
|
|
||||||
public static function getMaskFile($sFileName)
|
public static function getMaskFile($sFileName)
|
||||||
{
|
{
|
||||||
return self::MASK_FOLDER.$sFileName.self::MASK_EXT;
|
return self::MASK_FOLDER.'/'.$sFileName.self::MASK_EXT;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function initFile($sFileName)
|
public function initFile($sFileName)
|
||||||
|
|||||||
@@ -58,8 +58,8 @@ class PhpObject
|
|||||||
}
|
}
|
||||||
|
|
||||||
private function setLogPath($sLogPath) {
|
private function setLogPath($sLogPath) {
|
||||||
if($sLogPath == '') $sLogPath = defined('Settings::LOG_FOLDER')?Settings::LOG_FOLDER:dirname(__FILE__);
|
if($sLogPath == '') $sLogPath = defined('Settings::LOG_FOLDER')?Settings::LOG_FOLDER:getcwd();
|
||||||
$this->sLogPath = $sLogPath.DIRECTORY_SEPARATOR.self::LOG_FILENAME;
|
$this->sLogPath = $sLogPath.'/'.self::LOG_FILENAME;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getLogPath()
|
private function getLogPath()
|
||||||
|
|||||||
@@ -33,11 +33,16 @@ class ToolBox
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function fixGlobalVars($argv)
|
public static function getRequest($argv)
|
||||||
{
|
{
|
||||||
//Add CLI arguments
|
$asRequest = array_replace($_GET, $_POST);
|
||||||
if(defined('STDIN')) mb_parse_str(implode('&', array_slice($argv, 1)), $_GET);
|
|
||||||
$_REQUEST = array_merge($_GET, $_REQUEST);
|
if(PHP_SAPI == 'cli' && count($argv) > 1) {
|
||||||
|
mb_parse_str(implode('&', array_slice($argv, 1)), $asCliRequest);
|
||||||
|
$asRequest = array_replace($asRequest, $asCliRequest);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $asRequest;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function array_map_encapsulate($oData, $sChar)
|
public static function array_map_encapsulate($oData, $sChar)
|
||||||
@@ -471,7 +476,7 @@ class ToolBox
|
|||||||
* Return relative time description
|
* Return relative time description
|
||||||
* FIXME shitty implementation of i18n
|
* FIXME shitty implementation of i18n
|
||||||
* @param int $oTime Time (strtotime)
|
* @param int $oTime Time (strtotime)
|
||||||
* @param string $sLang Language (en/fr)
|
* @param string $sLang Language (en/fr/es)
|
||||||
* @return string Relative Time
|
* @return string Relative Time
|
||||||
*/
|
*/
|
||||||
public static function getDateTimeDesc($oTime, $sLang='en')
|
public static function getDateTimeDesc($oTime, $sLang='en')
|
||||||
@@ -488,6 +493,10 @@ class ToolBox
|
|||||||
$asWeekDays = array('lundi', 'mardi', 'mercredi', 'jeudi', 'vendredi', 'samedi', 'dimanche');
|
$asWeekDays = array('lundi', 'mardi', 'mercredi', 'jeudi', 'vendredi', 'samedi', 'dimanche');
|
||||||
$asMonths = array('janvier', 'février', 'mars', 'avril', 'mai', 'juin', 'juillet', 'août', 'septembre', 'octobre', 'novembre', 'décembre');
|
$asMonths = array('janvier', 'février', 'mars', 'avril', 'mai', 'juin', 'juillet', 'août', 'septembre', 'octobre', 'novembre', 'décembre');
|
||||||
break;
|
break;
|
||||||
|
case 'es':
|
||||||
|
$asWeekDays = array('lunes', 'martes', 'miércoles', 'jueves', 'viernes', 'sábado', 'domingo');
|
||||||
|
$asMonths = array('enero', 'febrero', 'marzo', 'abril', 'mayo', 'junio', 'julio', 'agosto', 'septiembre', 'octubre', 'noviembre', 'diciembre');
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
$sSep = '|';
|
$sSep = '|';
|
||||||
@@ -541,6 +550,28 @@ class ToolBox
|
|||||||
elseif($sYear==($sCurYear-1)) $sDesc = 'l\'année dernière';
|
elseif($sYear==($sCurYear-1)) $sDesc = 'l\'année dernière';
|
||||||
else $sDesc = 'en '.$sYear;
|
else $sDesc = 'en '.$sYear;
|
||||||
break;
|
break;
|
||||||
|
case 'es':
|
||||||
|
if($iTimeStamp>$sCurTimeStamp) $sDesc = 'en el futuro';
|
||||||
|
elseif($sCurTimeStamp-$iTimeStamp<60) $sDesc = 'hace unos segundos';
|
||||||
|
elseif($sCurTimeStamp-$iTimeStamp<60*10) $sDesc = 'hace unos minutos';
|
||||||
|
elseif($sCurTimeStamp-$iTimeStamp<60*20) $sDesc = 'hace un cuarto de hora';
|
||||||
|
elseif($sCurTimeStamp-$iTimeStamp<60*50) $sDesc = 'hace media hora';
|
||||||
|
elseif($sCurTimeStamp-$iTimeStamp<60*60*2) $sDesc = 'hace una hora';
|
||||||
|
elseif($sCurTimeStamp-$iTimeStamp<60*60*24 && $sDay==$sCurDay) $sDesc = 'a las '.$sHour.'h';
|
||||||
|
elseif($sCurTimeStamp-$iTimeStamp<60*60*24) $sDesc = 'ayer';
|
||||||
|
elseif($sCurTimeStamp-$iTimeStamp<60*60*24*7 && $sWeek==$sCurWeek) $sDesc = $asWeekDays[$sWeekDay-1];
|
||||||
|
elseif($sCurTimeStamp-$iTimeStamp<60*60*24*7) $sDesc = 'el '.$asWeekDays[$sWeekDay-1].' pasado';
|
||||||
|
elseif($sCurTimeStamp-$iTimeStamp<60*60*24*9) $sDesc = 'hace una semana';
|
||||||
|
elseif($sCurTimeStamp-$iTimeStamp<60*60*24*12) $sDesc = 'hace 10 días';
|
||||||
|
elseif($sCurTimeStamp-$iTimeStamp<60*60*24*16) $sDesc = 'hace 2 semanas';
|
||||||
|
elseif($sCurTimeStamp-$iTimeStamp<60*60*24*23) $sDesc = 'hace 3 semanas';
|
||||||
|
elseif($sCurTimeStamp-$iTimeStamp<60*60*24*31 && $sMonth==$sCurMonth) $sDesc = 'el '.$sDay.' de '.$asMonths[$sMonth-1];
|
||||||
|
elseif($sCurTimeStamp-$iTimeStamp<60*60*24*30*2 && $sMonth==($sCurMonth-1)) $sDesc = 'el mes pasado';
|
||||||
|
elseif($sCurTimeStamp-$iTimeStamp<60*60*24*365 && $sYear==$sCurYear) $sDesc = 'en '.$asMonths[$sMonth-1];
|
||||||
|
elseif($sCurTimeStamp-$iTimeStamp<60*60*24*365) $sDesc = 'en '.$asMonths[$sMonth-1].' de '.$sYear;
|
||||||
|
elseif($sYear==($sCurYear-1)) $sDesc = 'el año pasado';
|
||||||
|
else $sDesc = 'en '.$sYear;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
//return self::mb_ucfirst($sDesc);
|
//return self::mb_ucfirst($sDesc);
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ class Translator extends PhpObject
|
|||||||
private $asLanguages;
|
private $asLanguages;
|
||||||
private $asTranslations; // [lang][key_word] = translation
|
private $asTranslations; // [lang][key_word] = translation
|
||||||
|
|
||||||
const TRANS_FOLDER = '../i18n/';
|
const TRANS_FOLDER = '../resources/lang';
|
||||||
const TRANS_FILE_EXT = '.json';
|
const TRANS_FILE_EXT = '.json';
|
||||||
const TRANS_SEP = '.';
|
const TRANS_SEP = '.';
|
||||||
const DEFAULT_LANG = 'en';
|
const DEFAULT_LANG = 'en';
|
||||||
@@ -143,6 +143,6 @@ class Translator extends PhpObject
|
|||||||
|
|
||||||
private static function getLangPath($sLang)
|
private static function getLangPath($sLang)
|
||||||
{
|
{
|
||||||
return self::TRANS_FOLDER.$sLang.self::TRANS_FILE_EXT;
|
return self::TRANS_FOLDER.'/'.$sLang.self::TRANS_FILE_EXT;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user