From 0cb2343e98a5e8951da0e5e386d31584558381cb Mon Sep 17 00:00:00 2001 From: Momchil Bozhinov Date: Thu, 25 Feb 2021 12:56:25 +0200 Subject: [PATCH 01/12] * PoC --- php/clonos.php | 64 +++++----- php/locale.php | 14 +-- php/menu.php | 91 -------------- public/index.php | 91 ++++++++++---- public/json.php | 27 ++-- templates/menu.html | 11 ++ tpl/Parser.php | 297 ++++++++++++++++++++++++++++++++++++++++++++ tpl/Tpl.php | 105 ++++++++++++++++ 8 files changed, 529 insertions(+), 171 deletions(-) delete mode 100644 php/menu.php create mode 100644 templates/menu.html create mode 100644 tpl/Parser.php create mode 100644 tpl/Tpl.php diff --git a/php/clonos.php b/php/clonos.php index 1c2f28c3..3aa55118 100644 --- a/php/clonos.php +++ b/php/clonos.php @@ -12,7 +12,6 @@ class ClonOS { public $workdir=''; public $environment=''; public $realpath=''; - public $realpath_php=''; public $realpath_public=''; public $realpath_page=''; public $uri_chunks=array(); @@ -56,77 +55,70 @@ class ClonOS { private $_db_jails=null; */ - function __construct($_real_path,$uri_chunks = null){ # /usr/home/web/cp/clonos + function __construct($uri_chunks = null){ - $this->_post=($_SERVER['REQUEST_METHOD']=='POST'); - $this->_vars=$_POST; + $this->_post = ($_SERVER['REQUEST_METHOD']=='POST'); + $this->_vars = $_POST; - $this->workdir=getenv('WORKDIR'); # // /usr/jails - $this->environment=getenv('APPLICATION_ENV'); - $this->realpath=$_real_path.'/'; # /usr/home/web/cp/clonos/ - $this->realpath_php=$_real_path.'/php/'; # /usr/home/web/cp/clonos/php/ - $this->realpath_public=$_real_path.'/public/'; # /usr/home/web/cp/clonos/public/ - $this->media_import=$_real_path.'/media_import/'; + $this->workdir = getenv('WORKDIR'); # // /usr/jails + $this->environment = getenv('APPLICATION_ENV'); + $this->realpath = '../'; # /usr/local/www/clonos/ + $this->realpath_public = '../public/'; # /usr/local/www/clonos/public/ + $this->media_import = '../media_import/'; - if($this->environment=='development'){ - $sentry_file=$this->realpath_php.'sentry.php'; + if($this->environment == 'development'){ + $sentry_file = '../php/sentry.php'; if(file_exists($sentry_file)) include($sentry_file); } if(isset($_SERVER['SERVER_NAME']) && !empty(trim($_SERVER['SERVER_NAME']))){ - $this->server_name=$_SERVER['SERVER_NAME']; + $this->server_name = $_SERVER['SERVER_NAME']; } else { - $this->server_name=$_SERVER['SERVER_ADDR']; + $this->server_name = $_SERVER['SERVER_ADDR']; } if (is_null($uri_chunks)) { # TODO Do we need this ? - $this->uri_chunks=Utils::gen_uri_chunks($uri); + $this->uri_chunks = Utils::gen_uri_chunks($uri); } else { - $this->uri_chunks=$uri_chunks; + $this->uri_chunks = $uri_chunks; } - $this->config=new Config(); - - $this->_locale = new Locale($this->realpath_public); - - $this->_client_ip=$_SERVER['REMOTE_ADDR']; + $this->config = new Config(); + $this->_locale = new Localization($this->realpath_public); + $this->_client_ip = $_SERVER['REMOTE_ADDR']; if(isset($this->_vars['path'])){ //$this->realpath_page=$this->realpath_public.'pages/'.trim($this->_vars['path'],'/').'/'; - $this->realpath_page=$this->realpath_public.'pages/'.$this->uri_chunks[0].'/'; - $this->json_name=$this->realpath_page.'a.json.php'; + $this->realpath_page = $this->realpath_public.'pages/'.$this->uri_chunks[0].'/'; + $this->json_name = $this->realpath_page.'a.json.php'; //echo $this->realpath_page; }else if($_SERVER['REQUEST_URI']){ //$this->realpath_page=$this->realpath_public.'pages/'.trim($_SERVER['REQUEST_URI'],'/').'/'; if(isset($this->uri_chunks[0])){ - $this->realpath_page=$this->realpath_public.'pages/'.$this->uri_chunks[0].'/'; + $this->realpath_page = $this->realpath_public.'pages/'.$this->uri_chunks[0].'/'; } } - if(isset($this->_vars['hash'])) $this->url_hash=preg_replace('/^#/','',$this->_vars['hash']); - -// $this->json_name=$this->realpath_php.'pages' -// $clonos->json_name=$file_path.'a.json.php'; - - if(isset($this->_vars['mode'])) $this->mode=$this->_vars['mode']; - if(isset($this->_vars['form_data'])) $this->form=$this->_vars['form_data']; + if(isset($this->_vars['hash'])) $this->url_hash = preg_replace('/^#/','',$this->_vars['hash']); + if(isset($this->_vars['mode'])) $this->mode = $this->_vars['mode']; + if(isset($this->_vars['form_data'])) $this->form = $this->_vars['form_data']; $ures=$this->userAutologin(); $this->sys_vars['authorized']=false; if($ures!==false){ if(isset($ures['id']) && is_numeric($ures['id']) && $ures['id']>0){ - $this->_user_info=$ures; - $this->_user_info['unregistered']=false; - $this->sys_vars['authorized']=true; + $this->_user_info = $ures; + $this->_user_info['unregistered'] = false; + $this->sys_vars['authorized'] = true; }else{ - $this->_user_info['unregistered']=true; + $this->_user_info['unregistered'] = true; if($this->json_req) exit; } } if($this->_post && isset($this->mode)){ if(isset($this->_user_info['error']) && $this->_user_info['error']){ - if($this->mode!='login'){ + if($this->mode != 'login'){ echo json_encode(array('error'=>true,'unregistered_user'=>true)); exit; } diff --git a/php/locale.php b/php/locale.php index cc6bca2f..a90ec30a 100644 --- a/php/locale.php +++ b/php/locale.php @@ -2,15 +2,15 @@ class Locale { - private $language='en'; - private $translate_arr=array(); + private $language = 'en'; + private $translate_arr = []; - function __construct($realpath_public) + function __construct() { - (isset($_COOKIE['lang'])) AND $this->language=$_COOKIE['lang']; - (!array_key_exists($this->language, Config::$languages)) AND $this->language='en'; - include($realpath_public.'/lang/'.$this->language.'.php'); - $this->translate_arr=$lang; + (isset($_COOKIE['lang'])) AND $this->language = $_COOKIE['lang']; + (!array_key_exists($this->language, Config::$languages)) AND $this->languag = 'en'; + include('../public/lang/'.$this->language.'.php'); + $this->translate_arr = $lang; } public function get_lang() diff --git a/php/menu.php b/php/menu.php deleted file mode 100644 index 3b0fd12a..00000000 --- a/php/menu.php +++ /dev/null @@ -1,91 +0,0 @@ -array( - 'name'=>'Новости', - 'title'=>'Новости сети', - ), - 'connect'=>array( - 'name'=>'Подключение к сети', - 'title'=>'Подключитесь к сети прямо сейчас!', - 'submenu'=>array( - 'map'=>array( - 'name'=>'Зона обслуживания', - 'title'=>'Зона обслуживания абонентов', - ), - 'wifi'=>array( - 'name'=>'Wi-Fi зоны', - 'title'=>'Бесплатные Wi-Fi зоны г. Кириши', - ), - 'docs'=>array( - 'name'=>'Документы', - 'title'=>'Документы' - ) - ) - ), -*/ - -class Menu -{ - public $html=array(); - public $title='Error'; - public $first_key=array(); - - function __construct(Locale $lang, $uri_chunks) - { - $menu_config = Config::$menu; - $this->first_key = array_key_first($menu_config); - - if(getenv('APPLICATION_ENV') != 'development'){ - unset($menu_config['sqlite']); - } - - $this->html=''; - - if($this->title=='Error'){ - $other_titles = Config::$other_titles; - if(isset($other_titles[$qstr])){ - $this->title=$lang->translate($other_titles[$qstr]); - } - } - } -} diff --git a/public/index.php b/public/index.php index 509c1b3c..6251f6a6 100644 --- a/public/index.php +++ b/public/index.php @@ -5,14 +5,46 @@ exit; } -$_real_path=realpath('../'); -$uri=trim($_SERVER['REQUEST_URI'],'/'); -require_once($_real_path.'/php/clonos.php'); -require_once($_real_path.'/php/menu.php'); -$chunks=Utils::gen_uri_chunks($uri); -$clonos=new ClonOS($_real_path, $chunks); -$locale = new Locale($_real_path.'/public/'); # /usr/home/web/cp/clonos/public/ -$menu=new Menu($locale, $chunks); +require_once('../php/clonos.php'); +require_once('../tpl/Tpl.php'); + +function get_title($menu_config, $uri_chunks) +{ + global $locale; + + $title = 'Error'; + $qstr = ''; + if(isset($uri_chunks[0])){ + $qstr = trim($uri_chunks[0],'/'); + } + + foreach($menu_config as $link => $val){ + if($qstr == $link){ + $title = $locale->translate($val['title']); + } + } + + if($title == 'Error'){ + if(isset($menu_config::$other_titles[$qstr])){ + $title = $locale->translate($other_titles[$qstr]); + } + } + + return $title; +} + +$uri = trim($_SERVER['REQUEST_URI'],'/'); +$chunks = Utils::gen_uri_chunks($uri); +$menu_config = Config::$menu; + +$clonos = new ClonOS($chunks); +$locale = new Localization(); + +$translate = function($word) +{ + global $locale; + return $locale->translate($word); +}; if(isset($_GET['upload'])){ include('upload.php'); @@ -25,26 +57,26 @@ exit; } -$lang=$locale->get_lang(); -$_ds=DIRECTORY_SEPARATOR; -$root=trim($_SERVER['DOCUMENT_ROOT'], $_ds); +$lang = $locale->get_lang(); +$_ds = DIRECTORY_SEPARATOR; +$root = trim($_SERVER['DOCUMENT_ROOT'], $_ds); -if(!empty($chunks)) $uri=$chunks[0]; +if(!empty($chunks)) $uri = $chunks[0]; -$file_path=$_ds.$root.$_ds.'pages'.$_ds.$uri.$_ds; -$file_name=$file_path.$lang.'.index.php'; -$json_name=$file_path.'a.json.php'; +$file_path = $_ds.$root.$_ds.'pages'.$_ds.$uri.$_ds; +$file_name = $file_path.$lang.'.index.php'; +$json_name = $file_path.'a.json.php'; if(empty($uri)){ - header('Location: /'.$menu->first_key.'/',true); + header('Location: /'.array_key_first($menu_config).'/',true); exit; } - -error_reporting(E_ALL); -$user_info=$clonos->userAutologin(); +$title = get_title($menu_config, $chunks); + +$user_info = $clonos->userAutologin(); if(!$user_info['error']){ - $user_info_txt="user_id='${user_info['id']}';user_login='${user_info['username']}';"; + $user_info_txt = "user_id='${user_info['id']}';user_login='${user_info['username']}';"; }else{ $user_info['username']='guest'; } @@ -52,7 +84,7 @@ - ClonOS — <?php echo $menu->title; ?> + ClonOS — <?php echo $title; ?> @@ -131,7 +163,20 @@ @@ -151,7 +196,7 @@
- title; ?> +