From ac0c401938c75b373799537c39e26e8c151247fb Mon Sep 17 00:00:00 2001 From: Momchil Bozhinov Date: Sun, 21 Feb 2021 19:26:38 +0200 Subject: [PATCH] * Menu class does not depend on ClonOS --- php/clonos.php | 163 +++++++++++++++++++++-------------------------- php/config.php | 6 +- php/locale.php | 31 +++++++++ php/menu.php | 57 +++++++++-------- php/utils.php | 17 +++++ public/index.php | 43 +++++++------ 6 files changed, 174 insertions(+), 143 deletions(-) create mode 100644 php/locale.php create mode 100644 php/utils.php diff --git a/php/clonos.php b/php/clonos.php index 9c2c648e..c6acf6d0 100644 --- a/php/clonos.php +++ b/php/clonos.php @@ -2,9 +2,10 @@ require_once("cbsd.php"); require_once('config.php'); +require_once('locale.php'); require_once('db.php'); require_once('forms.php'); -require_once('menu.php'); +require_once('utils.php'); class ClonOS { public $server_name=''; @@ -16,15 +17,12 @@ class ClonOS { public $realpath_page=''; public $uri_chunks=array(); public $json_name=''; - public $language='en'; - public $language_file_loaded=false; - public $translate_arr=array(); public $table_templates=array(); public $url_hash=''; public $media_import=''; public $json_req=false; public $sys_vars=array(); - + private $_locale; private $_post=false; private $_db=null; private $_client_ip=''; @@ -62,7 +60,6 @@ class ClonOS { $this->_post=($_SERVER['REQUEST_METHOD']=='POST'); $this->_vars=$_POST; - if(isset($_COOKIE['lang'])) $this->language=$_COOKIE['lang']; $this->workdir=getenv('WORKDIR'); # // /usr/jails $this->environment=getenv('APPLICATION_ENV'); @@ -82,21 +79,11 @@ class ClonOS { $this->server_name=$_SERVER['SERVER_ADDR']; } - if(!empty($uri)){ - $str=str_replace('/index.php','',$uri); - $this->uri_chunks=explode('/',$str); - }else if(isset($this->_vars['path'])){ - $str=trim($this->_vars['path'],'/'); - $this->uri_chunks=explode('/',$str); - } + $this->uri_chunks=Utils::gen_uri_chunks($uri); $this->config=new Config(); - /* determine lang */ - if(!array_key_exists($this->language, $this->config->languages)) $this->language='en'; - include($this->realpath_public.'/lang/'.$this->language.'.php'); - $this->translate_arr=$lang; - unset($lang); + $this->_locale = new Locale($this->realpath_public); $this->_client_ip=$_SERVER['REMOTE_ADDR']; @@ -121,8 +108,6 @@ class ClonOS { $this->_db_tasks=new Db('base','cbsdtaskd'); $this->_db_local=new Db('base','local'); - $this->menu=new Menu($this->config->menu,$this); - if(isset($this->_vars['mode'])) $this->mode=$this->_vars['mode']; if(isset($this->_vars['form_data'])) $this->form=$this->_vars['form_data']; @@ -152,8 +137,6 @@ class ClonOS { exit; } - unset($_POST); - // functions, running without parameters $new_array=array(); $cfunc='ccmd_'.$this->mode; @@ -264,6 +247,11 @@ class ClonOS { } } + function translate($phrase) + { + return $this->_locale->translate($phrase); + } + function ccmd_getJsonPage(){ $included_result_array=false; if(file_exists($this->json_name)){ @@ -291,15 +279,6 @@ class ClonOS { $res=$redis->publish($key,$message); } - function getLang(){ - return $this->language; - } - - function translate($phrase){ - if(isset($this->translate_arr[$phrase])) return $this->translate_arr[$phrase]; - return $phrase; - } - function getTableChunk($table_name='',$tag){ if(empty($table_name)) return false; if(isset($this->table_templates[$table_name][$tag])) return $this->table_templates[$table_name][$tag]; @@ -466,23 +445,23 @@ class ClonOS { $ops_array=$this->_cmd_array; $stat_array=array( - 'jcreate'=>array($this->translate('Creating'),$this->translate('Created')), - 'jstart'=>array($this->translate('Starting'),$this->translate('Launched')), - 'jstop'=>array($this->translate('Stopping'),$this->translate('Stopped')), - 'jrestart'=>array($this->translate('Restarting'),$this->translate('Restarted')), - 'jedit'=>array($this->translate('Saving'),$this->translate('Saved')), - 'jremove'=>array($this->translate('Removing'),$this->translate('Removed')), - 'jexport'=>array($this->translate('Exporting'),$this->translate('Exported')), - 'jimport'=>array($this->translate('Importing'),$this->translate('Imported')), - 'jclone'=>array($this->translate('Cloning'),$this->translate('Cloned')), - 'madd'=>array($this->translate('Installing'),$this->translate('Installed')), + 'jcreate'=>array($this->_locale->translate('Creating'),$this->_locale->translate('Created')), + 'jstart'=>array($this->_locale->translate('Starting'),$this->_locale->translate('Launched')), + 'jstop'=>array($this->_locale->translate('Stopping'),$this->_locale->translate('Stopped')), + 'jrestart'=>array($this->_locale->translate('Restarting'),$this->_locale->translate('Restarted')), + 'jedit'=>array($this->_locale->translate('Saving'),$this->_locale->translate('Saved')), + 'jremove'=>array($this->_locale->translate('Removing'),$this->_locale->translate('Removed')), + 'jexport'=>array($this->_locale->translate('Exporting'),$this->_locale->translate('Exported')), + 'jimport'=>array($this->_locale->translate('Importing'),$this->_locale->translate('Imported')), + 'jclone'=>array($this->_locale->translate('Cloning'),$this->_locale->translate('Cloned')), + 'madd'=>array($this->_locale->translate('Installing'),$this->_locale->translate('Installed')), //'mremove'=>array('Removing','Removed'), - 'sstart'=>array($this->translate('Starting'),$this->translate('Started')), - 'sstop'=>array($this->translate('Stopping'),$this->translate('Stopped')), - 'vm_obtain'=>array($this->translate('Creating'),$this->translate('Created')), - 'srcup'=>array($this->translate('Updating'),$this->translate('Updated')), - 'world'=>array($this->translate('Compiling'),$this->translate('Compiled')), - 'repo'=>array($this->translate('Fetching'),$this->translate('Fetched')), + 'sstart'=>array($this->_locale->translate('Starting'),$this->_locale->translate('Started')), + 'sstop'=>array($this->_locale->translate('Stopping'),$this->_locale->translate('Stopped')), + 'vm_obtain'=>array($this->_locale->translate('Creating'),$this->_locale->translate('Created')), + 'srcup'=>array($this->_locale->translate('Updating'),$this->_locale->translate('Updated')), + 'world'=>array($this->_locale->translate('Compiling'),$this->_locale->translate('Compiled')), + 'repo'=>array($this->_locale->translate('Fetching'),$this->_locale->translate('Fetched')), //'projremove'=>array('Removing','Removed'), ); $stat_array['bcreate']=&$stat_array['jcreate']; @@ -531,7 +510,7 @@ class ClonOS { $obj[$key]['txt_status']=$stat_array[$obj[$key]['operation']][$num]; if($stat['errcode']>0){ $obj[$key]['errmsg']=file_get_contents($stat['logfile']); - $obj[$key]['txt_status']=$this->translate('Error'); + $obj[$key]['txt_status']=$this->_locale->translate('Error'); } //Return the IP of the cloned jail if it was assigned by DHCP @@ -605,14 +584,14 @@ class ClonOS { 'node'=>'local', // TODO: actual data 'ip4_addr'=>str_replace(',',',',$form['ip4_addr']), 'jname'=>$form['jname'], - 'jstatus'=>$this->translate('Cloning'), + 'jstatus'=>$this->_locale->translate('Cloning'), 'icon'=>'spin6 animate-spin', 'desktop'=>' s-on', 'maintenance'=>' maintenance', 'protected'=>'icon-cancel', - 'protitle'=>$this->translate('Delete'), - 'vnc_title'=>$this->translate('Open VNC'), - 'reboot_title'=>$this->translate('Restart jail'), + 'protitle'=>$this->_locale->translate('Delete'), + 'vnc_title'=>$this->_locale->translate('Open VNC'), + 'reboot_title'=>$this->_locale->translate('Restart jail'), ); foreach($vars as $var=>$val) @@ -639,14 +618,14 @@ class ClonOS { 'node'=>'local', 'ip4_addr'=>str_replace(',',',',$jail['ip4_addr']), 'jname'=>$jail['jname'], - 'jstatus'=>$this->translate($stats[$op]), + 'jstatus'=>$this->_locale->translate($stats[$op]), 'icon'=>'spin6 animate-spin', 'desktop'=>' s-on', 'maintenance'=>' maintenance', 'protected'=>($jail['protected']==1)?'icon-lock':'icon-cancel', - 'protitle'=>($jail['protected']==1)?' title="'.$this->translate('Protected jail').'"':' title="'.$this->translate('Delete').'"', - 'vnc_title'=>$this->translate('Open VNC'), - 'reboot_title'=>$this->translate('Restart jail'), + 'protitle'=>($jail['protected']==1)?' title="'.$this->_locale->translate('Protected jail').'"':' title="'.$this->_locale->translate('Delete').'"', + 'vnc_title'=>$this->_locale->translate('Open VNC'), + 'reboot_title'=>$this->_locale->translate('Restart jail'), ); foreach($vars as $var=>$val) @@ -829,14 +808,14 @@ class ClonOS { 'node'=>'local', // TODO: fix actual data! 'ip4_addr'=>str_replace(',',',',$form['ip4_addr']), 'jname'=>$arr['jname'], - 'jstatus'=>$this->translate('Creating'), + 'jstatus'=>$this->_locale->translate('Creating'), 'icon'=>'spin6 animate-spin', 'desktop'=>' s-off', 'maintenance'=>' busy maintenance', 'protected'=>'icon-cancel', - 'protitle'=>$this->translate('Delete'), - 'vnc_title'=>$this->translate('Open VNC'), - 'reboot_title'=>$this->translate('Restart jail'), + 'protitle'=>$this->_locale->translate('Delete'), + 'vnc_title'=>$this->_locale->translate('Open VNC'), + 'reboot_title'=>$this->_locale->translate('Restart jail'), ); foreach($vars as $var=>$val) @@ -882,7 +861,7 @@ class ClonOS { if($err){ $res['error']=true; - $res['error_message']=$this->translate('Jail '.$form['jail_id'].' is not present.'); + $res['error_message']=$this->_locale->translate('Jail '.$form['jail_id'].' is not present.'); $res['jail_id']=$form['jail_id']; $res['reload']=true; return $res; @@ -912,7 +891,7 @@ class ClonOS { if(empty($res['vars'])) $err=true; if($err){ $res['error']=true; - $res['error_message']=$this->translate('Jail '.$form['jail_id'].' is not present.'); + $res['error_message']=$this->_locale->translate('Jail '.$form['jail_id'].' is not present.'); $res['jail_id']=$form['jail_id']; $res['reload']=true; return $res; @@ -942,7 +921,7 @@ class ClonOS { if($err){ $res['error']=true; - $res['error_message']=$this->translate('Jail '.$form['jail_id'].' is not present.'); + $res['error_message']=$this->_locale->translate('Jail '.$form['jail_id'].' is not present.'); $res['jail_id']=$form['jail_id']; $res['reload']=true; return $res; @@ -1037,14 +1016,14 @@ class ClonOS { 'vm_ram'=>$form['vm_ram'], 'vm_cpus'=>$form['vm_cpus'], 'vm_os_type'=>$form['vm_os_type'], - 'jstatus'=>$this->translate('Cloning'), + 'jstatus'=>$this->_locale->translate('Cloning'), 'icon'=>'spin6 animate-spin', 'desktop'=>' s-on', 'maintenance'=>' maintenance', 'protected'=>'icon-cancel', - 'protitle'=>$this->translate('Delete'), - 'vnc_title'=>$this->translate('Open VNC'), - 'reboot_title'=>$this->translate('Restart VM'), + 'protitle'=>$this->_locale->translate('Delete'), + 'vnc_title'=>$this->_locale->translate('Open VNC'), + 'reboot_title'=>$this->_locale->translate('Restart VM'), ); foreach($vars as $var=>$val) @@ -1076,13 +1055,13 @@ class ClonOS { 'vm_ram'=>$this->fileSizeConvert($bhyve['vm_ram']), 'vm_cpus'=>$bhyve['vm_cpus'], 'vm_os_type'=>$bhyve['vm_os_type'], - 'vm_status'=>$this->translate($statuses[$status]), + 'vm_status'=>$this->_locale->translate($statuses[$status]), 'desktop'=>($status==0)?' s-off':' s-on', 'icon'=>($status==0)?'play':'stop', 'protected'=>'icon-cancel', - 'protitle'=>' title="'.$this->translate('Delete').'"', - 'vnc_title'=>$this->translate('Open VNC'), - 'reboot_title'=>$this->translate('Restart bhyve'), + 'protitle'=>' title="'.$this->_locale->translate('Delete').'"', + 'vnc_title'=>$this->_locale->translate('Open VNC'), + 'reboot_title'=>$this->_locale->translate('Restart bhyve'), ); foreach($vars as $var=>$val) @@ -1115,7 +1094,7 @@ class ClonOS { if($err){ $res['error']=true; - $res['error_message']=$this->translate('Jail '.$form['jail_id'].' is not present.'); + $res['error_message']=$this->_locale->translate('Jail '.$form['jail_id'].' is not present.'); $res['jail_id']=$form['jail_id']; $res['reload']=true; return $res; @@ -1163,7 +1142,7 @@ class ClonOS { if($err){ $res['error']=true; - $res['error_message']=$this->translate('Jail '.$form['jail_id'].' is not present.'); // XSS + $res['error_message']=$this->_locale->translate('Jail '.$form['jail_id'].' is not present.'); // XSS $res['jail_id']=$form['jail_id']; // Possible XSS // $res['reload']=true; return $res; @@ -1321,7 +1300,7 @@ class ClonOS { 'nth-num'=>'nth0', // TODO: actual data 'node'=>'local', // TODO: actual data 'jname'=>$arr['jname'], - 'vm_status'=>$this->translate('Creating'), + 'vm_status'=>$this->_locale->translate('Creating'), 'vm_cpus'=>$form['vm_cpus'], 'vm_ram'=>$vm_ram, 'vm_os_type'=>$os_items['type'], //$os_name, @@ -1331,9 +1310,9 @@ class ClonOS { 'desktop'=>' s-off', 'maintenance'=>' maintenance', 'protected'=>'icon-cancel', - 'protitle'=>$this->translate('Delete'), - 'vnc_title'=>$this->translate('Open VNC'), - 'reboot_title'=>$this->translate('Restart VM'), + 'protitle'=>$this->_locale->translate('Delete'), + 'vnc_title'=>$this->_locale->translate('Open VNC'), + 'reboot_title'=>$this->_locale->translate('Restart VM'), ); foreach($vars as $var=>$val) @@ -1424,7 +1403,7 @@ class ClonOS { 'nth-num'=>'nth0', // TODO: actual data 'node'=>'local', // TODO: actual data 'jname'=>$form['vm_name'], - 'vm_status'=>$this->translate('Creating'), + 'vm_status'=>$this->_locale->translate('Creating'), 'vm_cpus'=>$form['vm_cpus'], 'vm_ram'=>$vm_ram, 'vm_os_type'=>$os_type, @@ -1432,9 +1411,9 @@ class ClonOS { 'desktop'=>' s-off', 'maintenance'=>' maintenance', 'protected'=>'icon-cancel', - 'protitle'=>$this->translate('Delete'), - 'vnc_title'=>$this->translate('Open VNC'), - 'reboot_title'=>$this->translate('Restart VM'), + 'protitle'=>$this->_locale->translate('Delete'), + 'vnc_title'=>$this->_locale->translate('Open VNC'), + 'reboot_title'=>$this->_locale->translate('Restart VM'), ); foreach($vars as $var=>$val) @@ -1490,7 +1469,7 @@ class ClonOS { 'keyid'=>$res['lastID'], 'keyname'=>$this->form['keyname'], 'keysrc'=>$this->form['keysrc'], - 'deltitle'=>$this->translate('Delete'), + 'deltitle'=>$this->_locale->translate('Delete'), ); foreach($vars as $var=>$val) @@ -1531,7 +1510,7 @@ class ClonOS { 'netid'=>$res['lastID'], 'netname'=>$form['netname'], 'network'=>$form['network'], - 'deltitle'=>$this->translate('Delete'), + 'deltitle'=>$this->_locale->translate('Delete'), ); foreach($vars as $var=>$val) @@ -1621,8 +1600,8 @@ class ClonOS { 'ver1'=>strlen(intval($res['ver']))$res['rev'], 'date'=>$res['date'], - 'protitle'=>$this->translate('Update'), - 'protitle'=>$this->translate('Delete'), + 'protitle'=>$this->_locale->translate('Update'), + 'protitle'=>$this->_locale->translate('Delete'), ); foreach($vars as $var=>$val) $html_tpl=str_replace('#'.$var.'#',$val,$html_tpl); @@ -1683,7 +1662,7 @@ class ClonOS { $taskId=$res['message']; } - return array('errorMessage'=>'','jail_id'=>'base'.$bid,'taskId'=>$taskId,'html'=>$html,'mode'=>$this->mode,'txt_status'=>$this->translate('Compiling')); + return array('errorMessage'=>'','jail_id'=>'base'.$bid,'taskId'=>$taskId,'html'=>$html,'mode'=>$this->mode,'txt_status'=>$this->_locale->translate('Compiling')); } function fillRepoTr($id,$only_td=false,$bsdsrc=true){ @@ -1722,7 +1701,7 @@ class ClonOS { 'elf'=>$res['elf'], 'date'=>$res['date'], 'maintenance'=>' busy', - 'protitle'=>$this->translate('Delete'), + 'protitle'=>$this->_locale->translate('Delete'), ); foreach($vars as $var=>$val) @@ -1768,7 +1747,7 @@ class ClonOS { 'elf'=>'—', 'date'=>'—', 'maintenance'=>' busy', - 'protitle'=>$this->translate('Delete'), + 'protitle'=>$this->_locale->translate('Delete'), ); foreach($vars as $var=>$val) @@ -1791,7 +1770,7 @@ class ClonOS { $taskId=$res['message']; } - return array('errorMessage'=>'','jail_id'=>'base'.$bid,'taskId'=>$taskId,'html'=>$html,'mode'=>$this->mode,'txt_status'=>$this->translate('Fetching')); + return array('errorMessage'=>'','jail_id'=>'base'.$bid,'taskId'=>$taskId,'html'=>$html,'mode'=>$this->mode,'txt_status'=>$this->_locale->translate('Fetching')); } function ccmd_logLoad(){ @@ -2644,7 +2623,7 @@ class ClonOS { $jres=$this->ccmd_getFreeJname(false,$type); if($jres['error']) return $this->messageError('Something wrong...'); $jname=$jres['freejname']; - $name_comment='* '.$this->translate('Since imported name already exist, we are change it'); + $name_comment='* '.$this->_locale->translate('Since imported name already exist, we are change it'); } } @@ -2746,7 +2725,7 @@ class ClonOS { foreach($quer as $q=>$k){ if(is_numeric($k) && ($k==0 || $k==1)) $k=($k==0)?'no':'yes'; - $html.=''.$this->translate($q).''.$this->translate($k).''; + $html.=''.$this->_locale->translate($q).''.$this->_locale->translate($k).''; } $html.=''; @@ -2792,7 +2771,7 @@ class ClonOS { if($q=='vm_ram') $k=$this->fileSizeConvert($k); if($q=='state_time') $k=date('d.m.Y H:i:s',$k); - $html.=''.$this->translate($q).''.$this->translate($k).''; + $html.=''.$this->_locale->translate($q).''.$this->_locale->translate($k).''; } $html.=''; diff --git a/php/config.php b/php/config.php index edb7ae3b..9f1fe445 100644 --- a/php/config.php +++ b/php/config.php @@ -5,7 +5,7 @@ require_once("cbsd.php"); class Config { /* Список языков, используемых в проекте */ - public $languages=array( + public static $languages=array( 'en'=>'English', 'ru'=>'Russian', ); @@ -20,14 +20,14 @@ class Config 'windows'=>'Windows', ); - public $other_titles=array( + public static $other_titles=array( 'settings'=>'CBSD Settings', 'users'=>'CBSD Users', ); /* Меню проекта */ /* Так же можно использовать подменю (в menu.php есть пример) */ - public $menu=array( + public static $menu=array( 'overview'=>array( 'name'=>'Overview', 'title'=>'Summary Overview', // заголовки лучше делать более полными, чем просто повторение пункта меню diff --git a/php/locale.php b/php/locale.php new file mode 100644 index 00000000..161ab080 --- /dev/null +++ b/php/locale.php @@ -0,0 +1,31 @@ +language=$_COOKIE['lang']; + (!array_key_exists($this->language, Config::languages)) $this->language='en'; + include($realpath_public.'/lang/'.$this->language.'.php'); + $this->translate_arr=$lang; + } + + public function get_lang() + { + return $this->language; + } + + public function get_available_languages() + { + return Config::languages; + } + + public function translate($phrase) + { + if(isset($this->translate_arr[$phrase])) return $this->translate_arr[$phrase]; + return $phrase; + } +} \ No newline at end of file diff --git a/php/menu.php b/php/menu.php index 60fc4015..d623ffb1 100644 --- a/php/menu.php +++ b/php/menu.php @@ -24,7 +24,6 @@ ), */ - class Menu { public $html=array(); @@ -32,30 +31,33 @@ class Menu public $title='Error'; public $path=''; public $first_key=array(); - - function __construct($menu_config=array(),$parent) - { - if(!empty($menu_config)) - { + + function __construct($_REALPATH,$uri) + { + $realpath_public=$_REALPATH.'/public/'; # /usr/home/web/cp/clonos/public/ + $lang = new Locale($realpath_public); + $menu_config = Config::menu; + if(!empty($menu_config)){ reset($menu_config); $this->first_key=key($menu_config); } - - if($parent->environment!='development') - unset($menu_config['sqlite']); - + + if(getenv('APPLICATION_ENV') != 'development'){ + unset($menu_config['sqlite']); + } + $this->html='