From 9d41b6083570e4da05beefbf700152fc331cbc4c Mon Sep 17 00:00:00 2001 From: Nicolas Boisselier Date: Mon, 10 Sep 2018 03:58:55 +0100 Subject: [PATCH] lib/php/nb.php --- lib/php/db.php | 58 +++++++++++++++++++++++-------------------- lib/php/db/table.php | 2 +- lib/php/nb.php | 19 ++++++++------ lib/php/out/table.php | 2 +- www/dbq/dbq.php | 37 ++++++++++++++------------- 5 files changed, 65 insertions(+), 53 deletions(-) diff --git a/lib/php/db.php b/lib/php/db.php index ec64dddb..bec6a503 100644 --- a/lib/php/db.php +++ b/lib/php/db.php @@ -29,10 +29,10 @@ class Db extends nb { # PDO Connection # NB 22.09.16 public static $encoding = 'utf-8'; -# NB 22.09.16: TODO - public static $table_rows_dump = [ - 'praser' => false, - ]; +# NB 10.09.18 # NB 22.09.16: TODO +# NB 10.09.18 public static $table_rows_dump = [ +# NB 10.09.18 'praser' => false, +# NB 10.09.18 ]; public $conn; public $pdo; @@ -54,6 +54,7 @@ class Db extends nb { public $tables = []; public $types = []; public $conf = []; + static $dbq = []; # NB 10.09.18: For internal use in /etc/dbq* files public $attach = []; # for sqlite public $row_parse_pre; # Function to call in table.rows() public $row_parse_post; # Function to call in table.rows() @@ -273,7 +274,8 @@ class Db extends nb { if (!empty($this->$p)) $msg .= " $p=".$this->$p; } - $msg .= "\n ".$e->getMessage(); + #$msg .= "\n ".$e->getMessage(); + $msg .= ": ".$e->getMessage(); #throw new Exception($msg, (int)$e->getCode()); self::bye($msg); } @@ -729,11 +731,11 @@ class Db extends nb { if (empty($files)) return []; # - # Load all files into a $dbs if #files is not a hash + # Load all files into a self::$dbq if #files is not a hash # $DBQ = []; - $dbs = []; +# NB 10.09.18 $dbs = []; $done = []; foreach ((array)$files as $file) { @@ -743,24 +745,24 @@ class Db extends nb { if (!is_readable($file)) continue; if (preg_match('/\.(yaml|yml)$/i',$file) and ($yaml = self::yaml_parse_file($file))) { - $dbs = self::ar_merge($dbs,$yaml); + self::$dbq = self::ar_merge(self::$dbq,$yaml); } elseif (preg_match('/\.php$/i',$file)) { $DBQ = []; require($file); - if (!empty($DBQ)) $dbs = self::ar_merge($dbs,$DBQ); + if (!empty($DBQ)) self::$dbq = self::ar_merge(self::$dbq,$DBQ); } } unset($yaml,$DBQ); - if (!$dbs) return false; + if (!self::$dbq) return false; # # First iteration: Import database conf with key _import # - foreach ($dbs as $id=>$params) { + foreach (self::$dbq as $id=>$params) { $params = (array)$params; foreach ($params as $k => $v) { @@ -769,46 +771,46 @@ class Db extends nb { $import = is_array($v) ? $v : explode(',',$v); foreach ($import as $v) { if ($id == $v) self::bye("Infinite loop: _import $id = $v"); - if (empty($dbs[$v])) continue; + if (empty(self::$dbq[$v])) continue; - foreach ($dbs[$v] as $kk => $vv) { - if (!isset($params[$kk])) $dbs[$id][$kk] = $vv; + foreach (self::$dbq[$v] as $kk => $vv) { + if (!isset($params[$kk])) self::$dbq[$id][$kk] = $vv; } } - unset($dbs[$id][$k]); + unset(self::$dbq[$id][$k]); } } - $default = isset($dbs['_default']) ? $dbs['_default'] : []; + $default = isset(self::$dbq['_default']) ? self::$dbq['_default'] : []; # # Second iteration: Remove db starting with _ # - foreach ($dbs as $db=>$params) { if (preg_match('/^_/',$db)) unset($dbs[$db]); } + foreach (self::$dbq as $db=>$params) { if (preg_match('/^_/',$db)) unset(self::$dbq[$db]); } - if (!$dbs) return false; + if (!self::$dbq) return false; # # Third iteration: Add missing and default # - foreach ($dbs as $db=>$params) { - if (empty($params['name'])) $dbs[$db]['name'] = $db; - if (empty($params['id'])) $dbs[$db]['id'] = $db; + foreach (self::$dbq as $db=>$params) { + if (empty($params['name'])) self::$dbq[$db]['name'] = $db; + if (empty($params['id'])) self::$dbq[$db]['id'] = $db; # Ignore incomplete if (empty($params['type'])) { - unset($dbs[$db]); + unset(self::$dbq[$db]); continue; } - foreach ($default as $k=>$v) if (!isset($params[$k])) $dbs[$db][$k] = $v; + foreach ($default as $k=>$v) if (!isset($params[$k])) self::$dbq[$db][$k] = $v; } # # Sort by `order`, min first # - uasort($dbs,function($a,$b){ + uasort(self::$dbq,function($a,$b){ if (empty($a['order']) and empty($b['order'])) return strcmp($a['id'],$b['id']); $a_ = !empty($a['order']) ? $a['order'] : 9999999; $b_ = !empty($b['order']) ? $b['order'] : 9999999; @@ -818,9 +820,11 @@ class Db extends nb { # # Return # - if (!$dbs) return false; - if ($first !== false) $first = self::ar_first($dbs); - return $dbs; + if (!self::$dbq) return false; + if ($first !== false) $first = self::ar_first(self::$dbq); + + return self::$dbq; + #self::$dbq = array_merge(self::$dbq,$dbs); } /** diff --git a/lib/php/db/table.php b/lib/php/db/table.php index 07a84eae..50d793fe 100644 --- a/lib/php/db/table.php +++ b/lib/php/db/table.php @@ -1611,7 +1611,7 @@ Class Table extends nb { $html = ''; - $html .= ''.NB_EOL; + $html .= '
'.NB_EOL; if ($this->show_header) { $html .= ''.NB_EOL; diff --git a/lib/php/nb.php b/lib/php/nb.php index 167a5f46..889abdc5 100644 --- a/lib/php/nb.php +++ b/lib/php/nb.php @@ -436,11 +436,16 @@ class NB { static $Spyc; if (!isset($Spyc)) { - if (!function_exists('yaml_emit')) { + # NB 10.09.18: Can't trust devian package php-yaml + if (true or !function_exists('yaml_emit')) { require_once(NB_ROOT.'/lib/php/Spyc.php'); - function yaml_emit($data) { return Spyc::YAMLDump($data, false, false, true); } - function yaml_parse_file($file) { return Spyc::YAMLLoad($file); } - function yaml_parse($str) { return Spyc::YAMLLoadString($str); } + function _yaml_emit($data) { return Spyc::YAMLDump($data, false, false, true); } + function _yaml_parse_file($file) { return Spyc::YAMLLoad($file); } + function _yaml_parse($str) { return Spyc::YAMLLoadString($str); } + } else { + function _yaml_emit($data) { return yaml_emit($data, false, false, true); } + function _yaml_parse_file($file) { return yaml_parse_file($file); } + function _yaml_parse($str) { return yaml_parse($str); } } $Spyc = true; @@ -449,7 +454,7 @@ class NB { public static function yaml_parse_file($file) { self::yaml_init(); - return yaml_parse_file($file); + return _yaml_parse_file($file); } /* @@ -457,7 +462,7 @@ class NB { */ public static function yaml_encode($row) { self::yaml_init(); - $yaml = yaml_emit($row); + $yaml = _yaml_emit($row); $yaml = preg_replace('/^---\s*/','',$yaml); $yaml = preg_replace('/\n\.\.\.$/','',$yaml); $yaml = trim($yaml); @@ -467,7 +472,7 @@ class NB { public static function yaml_decode($str) { self::yaml_init(); - return yaml_parse($str); + return _yaml_parse($str); } /* diff --git a/lib/php/out/table.php b/lib/php/out/table.php index 9b562d00..87fc90aa 100644 --- a/lib/php/out/table.php +++ b/lib/php/out/table.php @@ -1,7 +1,7 @@ true, - 'enclose' => array("
".NB_EOL,"
".NB_EOL), + 'enclose' => array("".NB_EOL,"
".NB_EOL), 'tag_enclose' => 'tr class="row"', 'tag_head' => 'th', 'tag' => 'td', diff --git a/www/dbq/dbq.php b/www/dbq/dbq.php index 8f36157f..da5aeca1 100644 --- a/www/dbq/dbq.php +++ b/www/dbq/dbq.php @@ -94,6 +94,7 @@ class DbQ extends nb { public $page; public $table; public $db; + private $conf; public function __construct($opt=[]) { // @@ -267,24 +268,27 @@ class DbQ extends nb { // Write output if ($this->expires and !preg_match('/^(dump|insert|update|replace|rm|vi)$/',$this->params['action'])) $this->page->expires = $this->expires; $this->page->headers_no_cache(); + + // Conf for js or info + $this->conf = [ + 'db.base' => ( empty($this->db) ? '' : $this->db->base ), + 'table.base' => ( empty($this->table) ? '' : $this->table->base ), + 'default.format' => $this->format_html, + 'default.limit' => $this->default_limit, + 'param.format' => $this->params['format'], + 'param.db' => $this->params['db'], + 'param.table' => $this->params['table'], + 'param.action' => $this->params['action'], + 'param.args' => $this->params['args'], + 'param.deep' => $this->params_deep, + 'text.add' => 'Add New', + 'text.clear' => 'Clear', + 'perm' => $this->perm, + 'perms' => $this->perms, + ]; + if (empty($this->_nopage)) { - $this->conf = [ - 'db.base' => ( empty($this->db) ? '' : $this->db->base ), - 'table.base' => ( empty($this->table) ? '' : $this->table->base ), - 'default.format' => $this->format_html, - 'default.limit' => $this->default_limit, - 'param.format' => $this->params['format'], - 'param.db' => $this->params['db'], - 'param.table' => $this->params['table'], - 'param.action' => $this->params['action'], - 'param.args' => $this->params['args'], - 'param.deep' => $this->params_deep, - 'text.add' => 'Add New', - 'text.clear' => 'Clear', - 'perm' => $this->perm, - 'perms' => $this->perms, - ]; $this->page->js_code = 'window._dbq = '.json_encode($this->conf); if ($obj != 'logout') { @@ -1192,7 +1196,6 @@ EOF; } elseif ($action == 'cryptkey') { $this->page($this->cryptkey()); } elseif ($action == 'status') { $this->page($this,'status'); } elseif ($action == 'conf') { $this->page($this,'conf'); - } elseif ($action == 'conf') { $this->page($this,'conf'); } elseif ($action == 'html') { $this->page(Mime::html($arg)); } elseif ($action == 'types') { -- 2.47.3