Update and add class test pages, minor updates for CoreLibs

Some code clean up in smarty class (check if $cms object is actually
set)
Logger/Support Debug clean up for some minor logic with debug prefixes
DB IO update debug prefix for log line so we can have a HTML formatted
prefix for echo output
This commit is contained in:
Clemens Schwaighofer
2021-06-28 18:03:59 +09:00
parent 3512fa73ee
commit 76e0c0ac06
27 changed files with 568 additions and 397 deletions

View File

@@ -10,6 +10,10 @@
* - menu creation
* - array vars for smarty
*
* CHANGE PLAN:
* loads DB\IO + Logger and returns one group object
* also checks all missing CONFIG vars from Basic class
*
* PUBLIC VARIABLES
*
* PRIVATE VARIABLES
@@ -224,14 +228,14 @@ class Backend extends \CoreLibs\DB\IO
$PAGES = [];
}
$pages = [];
foreach ($PAGES as $PAGE_CUID => $PAGE_DATA) {
foreach ($PAGES as $PAGE_DATA) {
$pages[] = $PAGE_DATA;
}
// $this->debug('pages', $this->print_ar($pages));
// if flag is 0, then we show all, else, we show only the matching flagges array points
// array is already sorted after correct order
reset($pages);
foreach ($pages as $i => $data) {
foreach ($pages as $data) {
// for ($i = 0, $iMax = count($pages); $i < $iMax; $i ++) {
$show = 0;
// is it visible in the menu & is it online
@@ -329,11 +333,12 @@ class Backend extends \CoreLibs\DB\IO
}
/**
* ONLY USED IN adbTopMenu
* checks if this filename is in the current situation (user id, etc) available
* @param string|null $filename filename
* @return bool true for visible/accessable menu point, false for not
*/
public function adbShowMenuPoint(?string $filename): bool
private function adbShowMenuPoint(?string $filename): bool
{
$enabled = false;
if ($filename === null) {

View File

@@ -30,7 +30,6 @@ class Email
'.*@disney\.ne\.jp$' => 'keitai_softbank_disney', # (kids)
'.*@willcom\.ne\.jp$' => 'keitai_willcom',
'.*@willcom\.com$' => 'keitai_willcom', # new for pdx.ne.jp address
'.*@willcom\.com$' => 'keitai_willcom', # new for pdx.ne.jp address
'.*@wcm\.ne\.jp$' => 'keitai_willcom', # old willcom wcm.ne.jp
'.*@pdx\.ne\.jp$' => 'keitai_willcom_pdx', # old pdx address for willcom
'.*@bandai\.jp$' => 'keitai_willcom_bandai', # willcom paipo! (kids)

View File

@@ -307,7 +307,7 @@ class IO extends \CoreLibs\Basic
/**
* main DB concstructor with auto connection to DB and failure set on failed connection
* @param array $db_config DB configuration array
* @param array $db_config DB configuration array
*/
public function __construct(array $db_config)
{
@@ -317,6 +317,7 @@ class IO extends \CoreLibs\Basic
if (!is_array($db_config)) {
$db_config = [];
}
// TODO: check must set CONSTANTS
// sets the names (for connect/reconnect)
$this->db_name = $db_config['db_name'] ?? '';
$this->db_user = $db_config['db_user'] ?? '';
@@ -396,7 +397,7 @@ class IO extends \CoreLibs\Basic
public function __destruct()
{
$this->__closeDB();
parent::__destruct();
// parent::__destruct();
}
// *************************************************************
@@ -547,7 +548,7 @@ class IO extends \CoreLibs\Basic
if ($prefix) {
$prefix .= '- ';
}
$this->log->debug($debug_id, $prefix.$error_string, true);
$this->log->debug($debug_id, $error_string, true, $prefix);
}
/**
@@ -869,6 +870,24 @@ class IO extends \CoreLibs\Basic
return $this->db_debug;
}
/**
* Switches db debug flag on or off
* OR
* with the optional parameter fix sets debug
* returns current set stats
* @param bool|null $debug Flag to turn debug on off
* @return bool True for debug is on, False for off
*/
public function dbToggleDebug(?bool $debug = null)
{
if ($debug !== null) {
$this->db_debug = $debug;
} else {
$this->db_debug = $this->db_debug ? false : true;
}
return $this->db_debug;
}
/**
* set max query calls, set to --1 to disable loop
* protection. this will generate a warning
@@ -1048,19 +1067,21 @@ class IO extends \CoreLibs\Basic
public function dbInfo(bool $show = true): string
{
$string = '';
$string .= '<b>-DB-info-></b> Connected to db <b>\''.$this->db_name.'\'</b> ';
$string .= 'with schema <b>\''.$this->db_schema.'\'</b> ';
$string .= 'as user <b>\''.$this->db_user.'\'</b> ';
$string .= 'at host <b>\''.$this->db_host.'\'</b> ';
$string .= 'on port <b>\''.$this->db_port.'\'</b> ';
$string .= 'with ssl mode <b>\''.$this->db_ssl.'\'</b><br>';
$string .= '<b>-DB-info-></b> DB IO Class debug output: <b>'.($this->db_debug ? 'Yes' : 'No').'</b>';
$string .= '{b}-DB-info->{/b} Connected to db {b}\''.$this->db_name.'\'{/b} ';
$string .= 'with schema {b}\''.$this->db_schema.'\'{/b} ';
$string .= 'as user {b}\''.$this->db_user.'\'{/b} ';
$string .= 'at host {b}\''.$this->db_host.'\'{/b} ';
$string .= 'on port {b}\''.$this->db_port.'\'{/b} ';
$string .= 'with ssl mode {b}\''.$this->db_ssl.'\'{/b}{br}';
$string .= '{b}-DB-info->{/b} DB IO Class debug output: {b}'.($this->db_debug ? 'Yes' : 'No').'{/b}';
if ($show === true) {
$this->__dbDebug('db', $string, 'dbInfo');
// if debug, remove / change b
$this->__dbDebug('db', str_replace(['{b}', '{/b}', '{br}'], ['', '', ' **** '], $string), 'dbInfo');
} else {
$string = $string.'<br>';
$string = $string.'{br}';
}
return $string;
// for direct print, change to html
return str_replace(['{b}', '{/b}', '{br}'], ['<b>', '</b>', '<br>'], $string);
}
/**
@@ -2125,24 +2146,6 @@ class IO extends \CoreLibs\Basic
return $this->num_rows ?? null;
}
/**
* Switches db debug flag on or off
* OR
* with the optional parameter fix sets debug
* returns current set stats
* @param bool|null $debug Flag to turn debug on off
* @return bool True for debug is on, False for off
*/
public function dbToggleDebug(?bool $debug = null)
{
if ($debug !== null) {
$this->db_debug = $debug;
} else {
$this->db_debug = $this->db_debug ? false : true;
}
return $this->db_debug;
}
// DEPEREACTED CALLS
/**

View File

@@ -45,6 +45,7 @@ class Logging
// debug flags/settings
private $running_uid = ''; // unique ID set on class init and used in logging as prefix
// log file name
private $log_folder = '';
private $log_file_name_ext = 'log'; // use this for date rotate
private $log_max_filesize = 0; // set in kilobytes
private $log_print_file = 'error_msg##LOGID####LEVEL####CLASS####PAGENAME####DATE##';
@@ -60,6 +61,14 @@ class Logging
public function __construct()
{
// check must set constants
if (defined('BASE') && defined('LOG')) {
$this->log_folder = BASE.LOG;
} else {
// fallback + warning
trigger_error('constant BASE or LOG are not defined, fallback to getcwd()', E_USER_WARNING);
$this->log_folder = getcwd().DS;
}
// running time start for script
$this->script_starttime = microtime(true);
// set per run UID for logging
@@ -205,7 +214,7 @@ class Logging
// init output variable
$output = $error_string; // output formated error string to output file
// init base file path
$fn = BASE.LOG.$this->log_print_file.'.'.$this->log_file_name_ext;
$fn = $this->log_folder.$this->log_print_file.'.'.$this->log_file_name_ext;
// log ID prefix settings, if not valid, replace with empty
if (preg_match("/^[A-Za-z0-9]+$/", $this->log_file_id)) {
$rpl_string = '_'.$this->log_file_id;
@@ -434,9 +443,12 @@ class Logging
* @param bool $strip default on false, if set to true,
* all html tags will be stripped and <br> changed to \n
* this is only used for debug output
* @param string $prefix Attach some block before $string. Will not be stripped even
* when strip is true
* if strip is false, recommended to add that to $string
* @return bool True if logged, false if not logged
*/
public function debug(string $level, string $string, bool $strip = false): bool
public function debug(string $level, string $string, bool $strip = false, string $prefix = ''): bool
{
if (!$this->doDebugTrigger('debug', $level)) {
return false;
@@ -463,8 +475,8 @@ class Logging
($strip ?
// find any <br> and replace them with \n
// strip rest of html elements (base only)
preg_replace("/(<\/?)(\w+)([^>]*>)/", '', str_replace('<br>', "\n", $string)) :
$string
preg_replace("/(<\/?)(\w+)([^>]*>)/", '', str_replace('<br>', "\n", $prefix.$string)) :
$prefix.$string
)
)
."\n"
@@ -483,6 +495,8 @@ class Logging
.'[<span style="color: #08b369;">'.$this->page_name.'</span>] '
.'[<span style="color: #0062A2;">'.$this->running_uid.'</span>] '
.'{<span style="font-style: italic; color: #928100;">'.$class.'</span>} - '
// as is prefix, allow HTML
.$prefix
// we replace special HTMLPRE with <pre> entries
.preg_replace("/{##HTMLPRE##}((.|\n)*?){##\/HTMLPRE##}/m", "<pre>\\1</pre>", \CoreLibs\Convert\Html::htmlent($string))
."</div><!--#BR#-->";

View File

@@ -68,6 +68,12 @@ class Support
$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS) ?? [['class' => get_called_class()]];
$class = null;
while ($class === null) {
// if current is
// [function] => debug
// [class] => CoreLibs\Debug\Logging
// then return
// (OUTSIDE) because it was not called from a class method
// or return file name
$class = array_pop($backtrace)['class'] ?? null;
}
return $class ?? '';

View File

@@ -311,7 +311,9 @@ class SmartyExtend extends SmartyBC
private function setSmartyVars($admin_call = false): void
{
global $cms;
$this->mergeCmsSmartyVars($cms);
if (is_object($cms)) {
$this->mergeCmsSmartyVars($cms);
}
// trigger flags
$this->HEADER['USE_PROTOTYPE'] = $this->USE_PROTOTYPE;
@@ -349,16 +351,24 @@ class SmartyExtend extends SmartyBC
// special for admin
if ($admin_call === true) {
// set ACL extra show
$this->DATA['show_ea_extra'] = $cms->acl['show_ea_extra'] ?? false;
$this->DATA['ADMIN'] = !empty($cms->acl['admin']) ? $cms->acl['admin'] : 0;
if (is_object($cms)) {
$this->DATA['show_ea_extra'] = $cms->acl['show_ea_extra'] ?? false;
$this->DATA['ADMIN'] = $cms->acl['admin'] ?? 0;
// top menu
$this->DATA['nav_menu'] = $cms->adbTopMenu() ?? [];
$this->DATA['nav_menu_count'] = is_array($this->DATA['nav_menu']) ? count($this->DATA['nav_menu']) : 0;
// messages = ['msg' =>, 'class' => 'error/warning/...']
$this->DATA['messages'] = $cms->messages ?? [];
} else {
$this->DATA['show_ea_extra'] = false;
$this->DATA['ADMIN'] = 0;
$this->DATA['nav_menu'] = [];
$this->DATA['nav_menu_count'] = 0;
$this->DATA['messages'] = [];
}
// set style sheets
$this->HEADER['STYLESHEET'] = $this->ADMIN_STYLESHEET ? $this->ADMIN_STYLESHEET : ADMIN_STYLESHEET;
$this->HEADER['JAVASCRIPT'] = $this->ADMIN_JAVASCRIPT ? $this->ADMIN_JAVASCRIPT : ADMIN_JAVASCRIPT;
// top menu
$this->DATA['nav_menu'] = $cms->adbTopMenu();
$this->DATA['nav_menu_count'] = is_array($this->DATA['nav_menu']) ? count($this->DATA['nav_menu']) : 0;
// messages = ['msg' =>, 'class' => 'error/warning/...']
$this->DATA['messages'] = $cms->messages ?? [];
// the page name
$this->DATA['page_name'] = $this->page_name;
$this->DATA['table_width'] = $this->PAGE_WIDTH ?? PAGE_WIDTH;