From f4f868f7f61d86b3f6ceb199e7dd9abe53e4404e Mon Sep 17 00:00:00 2001 From: Nicolas Boisselier Date: Mon, 5 Dec 2016 23:12:40 +0000 Subject: [PATCH] fix Bugs --- etc/dbq/nb.php | 2 +- lib/php/db.php | 93 +++++++++-------- lib/php/out.php | 18 ++-- www/dbq/html/default.css | 6 +- www/dbq/html/index.php | 219 +-------------------------------------- 5 files changed, 67 insertions(+), 271 deletions(-) diff --git a/etc/dbq/nb.php b/etc/dbq/nb.php index db16ae6a..f3de4d08 100644 --- a/etc/dbq/nb.php +++ b/etc/dbq/nb.php @@ -42,7 +42,7 @@ $DBQ['nb'] = [ $r['total'] = sprintf('%d',$r['rent'] * $r['months']); }, - 'row_parse_post' => ((empty($GLOBALS['Db']) or Db::php_cli()) ? null : function(&$r) { + 'row_parse_post' => (Db::php_cli() ? null : function(&$r) { static $templates; if (!isset($templates)) { $templates = $GLOBALS['Db']->rows("SELECT id FROM template",PDO::FETCH_NUM); diff --git a/lib/php/db.php b/lib/php/db.php index 0dabadad..575ef3d9 100644 --- a/lib/php/db.php +++ b/lib/php/db.php @@ -160,14 +160,19 @@ class Db extends nb { # Args into this foreach ($opt as $k=>$v) $this->$k = $v; #parent::__construct($opt); + + # Out Class #if(!is_scalar($opt['type'])) debug($opt['type']); if (empty($this->out)) $this->out = new Out(['charset'=>$this->charset]); -# NB 20.11.16 # id -# NB 20.11.16 if (!empty($this->conf)) { -# NB 20.11.16 foreach ($this->conf as $id=>$conf) { -# NB 20.11.16 } -# NB 20.11.16 } + # Format + # Set format from client Accept if != html + if (!isset($opt['format'])) { + if ($this->out->client_type()) $this->format = $this->out->client_type(); + if (self::p('format')) $this->format = self::p('format'); + # NB 05.12.16: Should be the single point for caling ::p() + #$this->format = self::p('format'); + } # Add formats from module out foreach($this->out->types() as $t=>$v) if (!in_array($t,$this->formats)) $this->formats[] = $t; @@ -549,10 +554,10 @@ class Db extends nb { if ($this->format) { $format = $this->format; - } elseif ($this->p('format') == 'null') { + } elseif ($this->format == 'null') { return true; - } elseif ($format=$this->p('format')) { + } elseif ($format=$this->format) { } else { # NB 04.12.16: TODEL one day ! @@ -580,6 +585,43 @@ class Db extends nb { return true; } + public function tables_rows($o) { + # Options + # Filters + $type = empty($o['type']) ? '' : $o['type']; + $name = empty($o['name']) ? '' : $o['name']; + $engine = empty($o['engine']) ? '' : $o['engine']; + $database = empty($o['database']) ? '' : $o['database']; + $count = empty($o['count']) ? '' : $o['count']; + $fields = empty($o['fields']) ? '' : $o['fields']; + + #var_dump ($this->tables()); +#foreach($this->tables as $t) debug($t->name.'='.$t->type); +#bye($this->tables); + $rows = []; + foreach ($this->tables() as $t) { + #debug($t->name.'='.$t->type); + + if ($name and !$this->str_match($t->name,$name)) continue; + if ($type and !$this->str_match($t->type,$type)) continue; + if ($count and !$this->str_match($t->count,$count)) continue; + if ($engine and !$this->str_match($t->engine,$engine)) continue; + if ($database and isset($t->database) and !$this->str_match($t->database,$database)) continue; + + $row = $t->status(); + if ($count) $row['count'] = $t->status('count'); + #if ($fields) $row['fields'] = $t->status('fields'); + if ($fields) $row['fields'] = join(',',array_keys($t->fields())); + + #debug($t->name.'='.$t->database); + $rows[] = $row; + } + + $rows = self::array_fill_assoc($rows); + usort($rows,function($a,$b) { return strcmp($a['name'],$b['name']); }); + return $rows; + } + public function action($action,$table=null) { #debug(($this->tables)); $actions = explode(',',$action); @@ -622,36 +664,7 @@ class Db extends nb { } elseif ($action == 'db.tables') { - # Options - # Filters - $type = $this->p('type',''); - $name = $this->p('name',''); - $engine = $this->p('engine',''); - $database = $this->p('database',''); - - #var_dump ($this->tables()); -#foreach($this->tables as $t) debug($t->name.'='.$t->type); -#bye($this->tables); - foreach ($this->tables() as $t) { - #debug($t->name.'='.$t->type); - - if (!empty($name) and !$this->str_match($t->name,$name)) continue; - if (!empty($type) and !$this->str_match($t->type,$type)) continue; - if (!empty($count) and !$this->str_match($t->count,$count)) continue; - if (!empty($engine) and !$this->str_match($t->engine,$engine)) continue; - if (!empty($database) and isset($t->database) and !$this->str_match($t->database,$database)) continue; - - $row = $t->status(); - if ($this->p('count')) $row['count'] = $t->status('count'); - #if ($this->p('fields')) $row['fields'] = $t->status('fields'); - if ($this->p('fields')) $row['fields'] = join(',',array_keys($t->fields())); - - #debug($t->name.'='.$t->database); - $rows[] = $row; - } - - $rows = self::array_fill_assoc($rows); - usort($rows,function($a,$b) { return strcmp($a['name'],$b['name']); }); + $rows = $this->tables_rows($this->p()); $return = $this->out($rows); } elseif ($action == 'db.conf') { @@ -1015,9 +1028,6 @@ class Db extends nb { #bye($this->db()->type); foreach ($tables as $t) { - #if ($fct) $t->row_parse_post = $fct; - #$t->row_parse_pre = null; - # DROP / CREATE $create = rtrim($t->create(false),';'); $create = str_replace(';CREATE',";\nCREATE",$create); @@ -1214,9 +1224,6 @@ class Db extends nb { Db::pset('select',''); } - # Set format from client Accept if != html - if (!Db::p('format')) Db::pdef('format',out::client_type()); - } public function ls(&$fields=[]) { diff --git a/lib/php/out.php b/lib/php/out.php index e8aa4a63..d619ebd4 100644 --- a/lib/php/out.php +++ b/lib/php/out.php @@ -442,13 +442,19 @@ Class Out extends Nb { * Function format * format ar variable for scalar output */ - public static function format($v,$type=null,$conf=[]) { + public static function row_parse(&$row,$o=[]) { + foreach ($row as $k => $v) { + $v = out::format($v,null,$o); + } + } + + public static function format($v,$type=null,$o=[]) { if ($type === null) $type = self::type(); $is_scalar = is_scalar($v); $v = self::scalar($v); if ($v==='') $is_scalar = true; - if (empty($conf['no_escape'])) $v = self::escape($v); + if (empty($o['no_escape'])) $v = self::escape($v); if (!$is_scalar and strpos($type,'html') !== false) $v = '
'.NB_EOL.$v.NB_EOL.'
'; return $v; @@ -503,10 +509,11 @@ Class Out extends Nb { public static function row_txt(&$o,&$row) { $header = (self::p('header')==='0') ? false : true; if (!isset($o['col'])) $o['col'] = ''; + self::row_parse($row,$o); foreach ($row as $k => $v) { if (!$header) { - echo self::format($v).$o['sep']; + echo $v.$o['sep']; continue; } @@ -516,7 +523,7 @@ Class Out extends Nb { ,$k ,preg_replace('/\n(\w)/m' ,$o['sep'].str_repeat(' ',(empty($o['head_max_len']) ? mb_strlen($k,self::$charset) : $o['head_max_len']+1)+2).'\1' - ,self::format($v)) + ,$v) ); } } @@ -571,10 +578,9 @@ function out_tag_head($o,&$data) { function out_tag(&$o,&$row) { if (isset($o['tag_enclose'])) echo (NB_EOL ? ' ' : '').'<'.$o['tag_enclose'].'>'.NB_EOL; + out::row_parse($row,$o); foreach ($row as $k => $v) { - $v = out::format($v,null,$o); - # Inside tag, ex: label if (isset($o['tag_key'])) { $v = '<'.$o['tag_key'].'>'.$k.''.$v; diff --git a/www/dbq/html/default.css b/www/dbq/html/default.css index b40665e5..fc12023e 100644 --- a/www/dbq/html/default.css +++ b/www/dbq/html/default.css @@ -18,7 +18,7 @@ h1 { a, body { - color: #cccccc; + color: #ddd; } ul { @@ -95,8 +95,8 @@ table.rows tr:last-child td { border-bottom: none; } text-decoration: none; line-height: 1.5em; cursor: pointer; - background-color: #cccccc; - border: solid 1px #cccccc; + background-color: #ddd; + border: solid 1px #ddd; border-radius: 4px; -webkit-border-radius: 4px; white-space: nowrap; diff --git a/www/dbq/html/index.php b/www/dbq/html/index.php index d7c41e18..d10e321a 100644 --- a/www/dbq/html/index.php +++ b/www/dbq/html/index.php @@ -1,220 +1,3 @@ 'ls', - 'table' => 'ls', - 'action' => 'ls', - 'format' => ( preg_match('/^\S+\s+.Windows|iPhone|Android|Macintosh/',$_SERVER['HTTP_USER_AGENT']) ? 'table' : 'human' ), - ]); - - if (!$params['db']) not_implemented(); - - # - # Page - require_once($_SERVER['DOCUMENT_ROOT'].'/../../../lib/php/page.php'); - Page::pdef('out',1); - $formats = array_keys(out::types()); - foreach ($params as $k=>$v) { - if (!in_array($v,$formats)) continue; - $params['format'] = $v; - break; - } - $Page = new Page([ - 'css' => '/default.css', - ]); - $Page->content_type($Page->ext2mime($params['format']) ? $Page->ext2mime($params['format']) : 'text/plain'); - #$Page->content_type('text/plain'); - #if ($Page->is('text') and !$Page->is('html')) $Page->content_type('text/plain'); - $Page->headers_no_cache(); - $Page->begin(); - - # - # Db Init - global $DB_CONFS; - require_once($_SERVER['DOCUMENT_ROOT'].'/../../../lib/php/db/config.php'); - #if (empty($DB_CONFS[$params['db']])) not_implemented(); - #$Db = new Db($DB_CONFS[$params['db']]+['conf'=>$DB_CONFS]); - $Db = new Db(['conf'=>$DB_CONFS]); - $Db->format = $params['format']; - if (0) $Db->out_parser = function(&$rows) use($Page) { - foreach ($rows as $k=>$row) { - foreach ($row as $kk=>$vv) { - $rows[$k][$kk] = $Page->tag('a',$vv); - break; - } - } - }; - - # - # Db actions pre conn - if ($params['db'] == 'help') { - #$this->out->rows($format,$rows,$head); - $Db->out([ - [ 'help', 'This help' ], - [ 'ls', 'List configured databases' ], - [ 'status', 'Database status' ], - [ 'databases', 'List others databases' ], - ],['command','description']); - return $Page->end(); - - } elseif ($params['db'] == 'ls') { - $Db->out($Db->ls()); - return $Page->end(); - - } - - # - # Connections post conn - if (empty($DB_CONFS[$params['db']])) not_implemented(); - $Db->__construct($DB_CONFS[$params['db']]); - $Db->connect(); - - # - # Db actions - if ($params['table'] == 'help') { - #$this->out->rows($format,$rows,$head); - $Db->out([ - [ 'help', 'This help' ], - [ 'ls', 'List tables' ], - ],['command','description']); - return $Page->end(); - - } elseif ($params['table'] == 'ls') { - $Db->action('db.tables'); - return $Page->end(); - - } elseif ($params['table'] == 'status') { - $Db->out($Db->status()); - return $Page->end(); - - } elseif ($params['table'] == 'databases') { - $Db->out($Db->databases()); - return $Page->end(); - - } - - # - # Table Init - $Table = $Db->table($params['table']); - if (!$Table) not_implemented(); - - # - # Table action - if ($params['action'] == 'help') { - #$this->out->rows($format,$rows,$head); - $Db->out([ - [ 'help', 'This help' ], - [ 'ls', 'List records' ], - [ 'fields', 'List fields' ], - ],['command','description']); - return $Page->end(); - - } elseif ($params['action'] == 'ls') { - $Table->rows(); - return $Page->end(); - - } elseif ($params['action'] == 'status') { - $Db->out($Table->status()+$Table->status(['fields'])); - return $Page->end(); - - } elseif ($params['action'] == 'fields') { - $Table->action('table.fields'); - return $Page->end(); - - } - return false; -} - -function parse_uri($params=[]) { - if (empty($_SERVER['REQUEST_URI'])) $_SERVER['REQUEST_URI'] = join('&',count($argv)>0 ? array_slice($argv,1) : []); - $path = trim(preg_replace('/\?.*$/','',$_SERVER['REQUEST_URI']),'/'); - $values = $path ? explode('/',$path) : []; -#var_export($values); - - $i=0; - $count = count($values); - foreach ($params as $p => $default) { - if ($i>=$count) break; - if (!preg_match('/^[\w\._-]{2,100}$/',$values[$i])) not_implemented(); - $params[$p] = $values[$i]; - $i++; - } - - return $params; -} -run(); -exit; - -$params = [ - 'db', - 'table', - 'action', - 'format', -]; -if (false and strpos($_SERVER['REQUEST_URI'],'?')!==false) { - return not_implemented(); -} -$_SERVER['REQUEST_URI'] = preg_replace('/\?.*$/','',$_SERVER['REQUEST_URI']); -$values = explode('/',trim($_SERVER['REQUEST_URI'],'/')); -require_once($_SERVER['DOCUMENT_ROOT'].'/../../../lib/php/page.php'); -#bye($_REQUEST); - -foreach ($values as $k=>$v) { - #if ($v!=='') debug($params[$k].'='.$v); - if ($v!=='') Page::pdef($params[$k],$v); -} - -Page::pdef('format','human'); -Page::pdef('action','db.ls'); - -require_once($_SERVER['DOCUMENT_ROOT'].'/../../../lib/php/db/init.php'); -#header('Content-type: text/plain'); -$Page = $Db->page(); -$Page->headers_no_cache(); -$Page->call([ -# NB 04.12.16 $Db->page([ -# NB 04.12.16 'call' => [ - 'begin', - #['out', "Hello World !!!\n"], - - function($page) use($Db){ - #debug($Db->p()); - #global $values; debug($values); - }, - function($page) use($Db){ - if (!$page->action()) return $Db->action($Db->p('action'),$Db->table($Db->p('table'))); - }, - - function($page) use($Db){ - if (!$page->is('html')) return; - if (!empty($Db)) echo $page->tag('p class="db-infos"',join(' | ',[ - $Db->type, - $Db->host, - ($Db->name ? $Db->name : null), - ])); - }, - - 'end', -# NB 04.12.16 ], -]); -#require_once($_SERVER['DOCUMENT_ROOT'].'/../../../lib/php/db/page.php'); -# /DB/ACTION/[TABLE]/[FORMAT] -# /ACTION/DB/[TABLE]/[FORMAT] -#debug($_SERVER['REQUEST_URI']); -#debug($_SERVER['QUERY_STRING']); -#debug($_SERVER); - -function not_implemented() { - #return http_response_code(501); - if (empty($_SERVER['SERVER_PROTOCOL'])) $_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.0'; - $msg = '501 Not Implemented'; - header($_SERVER['SERVER_PROTOCOL'].' '.$msg); - echo "$msg\n"; - exit; -} +require_once(dirname(__FILE__).'/../dbq.php'); ?> -- 2.47.3