Files
objects/inc/Main.php
2022-01-24 15:21:08 +01:00

181 lines
4.9 KiB
PHP
Executable File

<?php
namespace Franzz\Objects;
use \Settings;
/**
* Main Class
* @author franzz
* @version 2.3
*/
abstract class Main extends PhpObject
{
//Constants
const SUCCESS = 'success';
const ERROR = 'error';
const UNAUTHORIZED = 'unauthorized';
const NOT_FOUND = 'unknown action';
const NO_DATA = 'No Data';
const ACTIVE = 1;
const INACTIVE = 0;
//Folders
const MASKS_FOLDER = 'masks/';
/**
* DB Handle
* @var Db
*/
protected $oDb;
//Dates format
const URL_DATE_FORMAT = 'Ymd';
const LAYOUT_DATE_FORMAT = 'F \t\h\e jS, Y';
const MYSQL_DATE_FORMAT = 'Y-m-d';
const LAYOUT_TIME_FORMAT = 'G:i';
//Variables
protected $asMasks;
protected $asContext;
/**
* Language Translator
* @var Translator
*/
protected $oLang;
/**
* Main constructor
*/
public function __construct($sProcessPage, $bDb=true, $sFile=__FILE__, $sTimeZone='')
{
parent::__construct($sFile, Settings::DEBUG);
$this->setContext($sProcessPage, $sTimeZone);
//Init objects
if($bDb) {
$asConf = array(
'server' => Settings::DB_SERVER,
'database' => Settings::DB_NAME,
'user' => Settings::DB_LOGIN,
'pass' => Settings::DB_PASS,
'encoding' => Settings::DB_ENC
);
$this->oDb = new Db($asConf, $this->getSqlOptions());
if(in_array($this->oDb->sDbState, array(Db::DB_NO_DATA, Db::DB_NO_TABLE))) $this->install();
}
}
protected abstract function install();
public function getDbBuildScript() {
return $this->oDb->getFullInstallQuery();
}
private function setContext($sProcessPage, $sTimeZone='')
{
//Browser <> PHP <> MySql synchronization
if($sTimeZone=='') $sTimeZone = Settings::TIMEZONE;
date_default_timezone_set($sTimeZone);
ini_set('default_charset', Settings::TEXT_ENC);
header('Content-Type: text/html; charset='.Settings::TEXT_ENC);
mb_internal_encoding(Settings::TEXT_ENC);
mb_http_output(Settings::TEXT_ENC);
mb_language('uni');
mb_regex_encoding(Settings::TEXT_ENC);
$this->asContext['process_page'] = basename($sProcessPage);
$sServerName = array_key_exists('SERVER_NAME', $_SERVER)?$_SERVER['SERVER_NAME']:$_SERVER['PWD'];
$sScheme = array_key_exists('HTTP_X_FORWARDED_PROTO', $_SERVER)?$_SERVER['HTTP_X_FORWARDED_PROTO']:$_SERVER['REQUEST_SCHEME'];
$sAppPath = $sScheme.'://'.str_replace(array('http://', 'https://'), '', $sServerName.dirname($_SERVER['SCRIPT_NAME']));
$this->asContext['serv_name'] = $sAppPath.(mb_substr($sAppPath, -1)!='/'?'/':'');
$this->setMasks();
}
public static function addTimestampToFilePath($sFilePath)
{
//Remove timestamp
$sCleanedFilePath = preg_replace('/(.*)\?[\d]{14}$/', '$1', $sFilePath);
//Add timestamp
return file_exists($sCleanedFilePath)?$sCleanedFilePath.'?'.date("YmdHis", filemtime($sCleanedFilePath)):$sFilePath;
}
public function addUncaughtError($sError)
{
$this->addError('Uncaught errors:'."\n".$sError);
}
/* Building main pages */
public function getMainPage($asGlobalVars=array(), $sMainPage='index', $asMainPageTags=array(), $asCachePages=array())
{
$asDefaultConsts = array('success'=>self::SUCCESS, 'error'=>self::ERROR, 'process_page'=>$this->asContext['process_page']);
$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);
$sPageContent = $oMask->getMask();
$asGlobalVars['consts']['pages'][$sPage] = $sPageContent;
}
}
if(!is_null($this->oLang)) {
$asGlobalVars['consts']['lang_prefix'] = Mask::LANG_PREFIX;
$asGlobalVars['consts']['lang'] = $this->oLang->getTranslations();
}
$oMainMask = new Mask($sMainPage, $this->oLang);
$oMainMask->setTag('GLOBAL_VARS', json_encode($asGlobalVars));
$oMainMask->setTags($asMainPageTags);
return $oMainMask->getMask();
}
protected function getPageContent($sPage)
{
return ToolBox::fixEOL(file_get_contents(self::MASKS_FOLDER.$sPage.'.html'));
}
/* DB structure. See Db::__construct */
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())
{
header('Content-type: application/json');
$asResult = self::getResult($bSuccess, $sDesc, $asVars);
$asResult['result'] = $asResult['result']?self::SUCCESS:self::ERROR;
return json_encode($asResult);
}
public static function getResult($bSuccess, $sDesc, $asVars=array()) {
return array('result'=>$bSuccess, 'desc'=>$sDesc, 'data'=>$asVars);
}
public static function goTo403() {
http_response_code(403);
exit;
}
public static function goTo404() {
http_response_code(404);
exit;
}
}