From 60154443bf13cd6bdd66cd9651245b4fbf5da649 Mon Sep 17 00:00:00 2001 From: Nicolas Boisselier Date: Mon, 5 Dec 2016 00:39:07 +0000 Subject: [PATCH] Bed --- lib/php/db.php | 18 ++++- lib/php/db/field.php | 2 +- lib/php/db/init.php | 2 +- lib/php/db/table.php | 10 ++- lib/php/functions.php | 5 ++ lib/php/nb.php | 16 +++-- lib/php/page.php | 4 ++ www/dbq/html/index.php | 148 +++++++++++++++++++++++++++++++++++------ 8 files changed, 174 insertions(+), 31 deletions(-) diff --git a/lib/php/db.php b/lib/php/db.php index 97f0a713..4c9c7443 100644 --- a/lib/php/db.php +++ b/lib/php/db.php @@ -118,6 +118,7 @@ class Db extends nb { public $default_table; public $sort; public $extras; + public $format = 'json'; public $formats = [ 'table','div','csv','xml','json','yaml','sh' ]; public $limits = ['10','20','50','100','500','1000']; @@ -545,7 +546,19 @@ class Db extends nb { public function out($rows,$head=[]) { - if ($this->p('format') == 'null') return true; + if ($this->format) { + $format = $this->format; + + } elseif ($this->p('format') == 'null') { + return true; + + } elseif ($format=$this->p('format')) { + + } else { + # NB 04.12.16: TODEL one day ! + $format = $this->out->php_cli() ? 'csv' : 'table'; + } + // Handle limit if ($this->p('limit')) { @@ -559,7 +572,7 @@ class Db extends nb { } - $this->out->rows($this->p('format',$this->out->php_cli() ? 'csv' : 'table'),$rows,$head); + $this->out->rows($format,$rows,$head); return true; } @@ -818,7 +831,6 @@ class Db extends nb { # Db # $Db = new self(); - if (empty($Db)) return false; # # Params diff --git a/lib/php/db/field.php b/lib/php/db/field.php index e1b14f24..9d543be3 100644 --- a/lib/php/db/field.php +++ b/lib/php/db/field.php @@ -93,7 +93,7 @@ class field extends nb { } public function db() { - if (!empty($this->table) and !empty($this->table->db)) return $this->table->db(); + if (!empty($this->table) and !empty($this->table->db())) return $this->table->db(); global $Db; return ( isset($Db) ? $Db : null ); } diff --git a/lib/php/db/init.php b/lib/php/db/init.php index b3dd1650..f4279ab4 100644 --- a/lib/php/db/init.php +++ b/lib/php/db/init.php @@ -1,5 +1,5 @@ p('format') : $opt['format']; - if (!$format) bye("Parameter `format` missing!"); + if (!$format and $this->db()->format) { + $format = $this->db()->format; + #$this->pset('format',$format); + } + if (!$format) $this->bye("Parameter `format` missing!"); } list($sql,$where,$limit,$select_count) = $this->rows_sql($opt); @@ -1850,6 +1854,8 @@ Class Table extends nb { if (empty($key)) { if (!self::p($k)) continue; + } elseif(is_array($key)) { + if (!in_array($k,$key)) continue; } else { if ($k!=$key) continue; } @@ -1858,7 +1864,7 @@ Class Table extends nb { if (is_array($this->status[$k])) $this->status[$k] = count($this->status[$k]); } - if (!empty($key)) return ( empty($this->status[$key]) ? '' : $this->status[$key] ); + if (!empty($key) and is_scalar($key)) return ( empty($this->status[$key]) ? '' : $this->status[$key] ); return $this->status; } diff --git a/lib/php/functions.php b/lib/php/functions.php index abc71867..b2302481 100644 --- a/lib/php/functions.php +++ b/lib/php/functions.php @@ -62,11 +62,13 @@ function txt2md($txt) { function bye($msg='__bye__',$backtrace_deep=0) { #if ($msg) err($msg,'bye',$backtrace_deep === 0 ? 1 : $backtrace_deep); + if ($msg!=='__bye__') { #throw new Exception($msg); err($msg,'bye',( $backtrace_deep !== false ? (1+-1+$backtrace_deep) : $backtrace_deep )); exit(1); } + #if ($msg) err($msg,'bye',1+$backtrace_deep); exit; #die("Can't exit from BYE!!!"); @@ -79,13 +81,16 @@ function warn ($msg) { function err($msg='__err__',$preff='err',$backtrace_deep=0) { $msg = is_scalar($msg) ? $msg : print_r($msg,true); $preff_msg = $preff ? strtoupper($preff).': ' : ''; + if ($msg !== '__err__' and $backtrace_deep !== false) { $msg = trim($preff_msg.$msg).' ' .nb::debug_backtrace_msg(1+$backtrace_deep,NULL,$preff_msg) #.nb::debug_backtrace_msg(1+$backtrace_deep,NULL) ; + } else { $msg = $preff_msg.$msg; + } #nb::msg( !nb::php_cli() diff --git a/lib/php/nb.php b/lib/php/nb.php index e9a06cd3..85090a28 100644 --- a/lib/php/nb.php +++ b/lib/php/nb.php @@ -17,6 +17,8 @@ class NB { protected static $content_types_aliases = array( 'text' => 'txt', 'human' => 'txt', + 'table' => 'html', + 'div' => 'html', ); protected static $content_types = array( @@ -409,7 +411,9 @@ class NB { E_DEPRECATED => 'E_DEPRECATED ERROR', E_USER_DEPRECATED => 'E_USER_DEPRECATED ERROR', ); - if (isset($call_info['type']) and isset($errorType[$call_info['type']])) $call_info['type'] = $errorType[$call_info['type']].'='.$call_info['type']; + if (isset($call_info['type']) + and isset($errorType[$call_info['type']]) + ) $call_info['type'] = $errorType[$call_info['type']].'='.$call_info['type']; #else ($call_info['type']) $call_info['type'] = $call_info['type']; // Default values @@ -418,17 +422,19 @@ class NB { ; // Get infos - $msg = array(); - foreach (array( + $msg = []; + foreach ([ 'file', 'line', 'function', 'type', 'message', 'code', - ) as $i) { + ] as $i) { - if (array_key_exists($i,$call_info)) $msg[] = "$i=".$call_info[$i]; + if (!array_key_exists($i,$call_info)) continue; + if ($i == 'type' and preg_match('/^\W\W/',$call_info[$i])) continue; + $msg[] = "$i=".$call_info[$i]; } diff --git a/lib/php/page.php b/lib/php/page.php index 2d3f32cd..57b6d0dd 100644 --- a/lib/php/page.php +++ b/lib/php/page.php @@ -179,6 +179,9 @@ class Page extends nb { * Function: tag * Return html code for a tag */ + public static function h1($content='',$attrs='') { return self::tag('h1',$content,$attrs); } + public static function div($content='',$attrs='') { return self::tag('div',$content,$attrs); } + public static function pp($content='',$attrs='') { return self::tag('p',$content,$attrs); } public static function tag($tag,$content='',$attrs='') { static $is_html = null; if ($is_html === null) $is_html = preg_match('/ml$/',self::content_type()) ? true : false; @@ -456,6 +459,7 @@ class Page extends nb { if ($is == 'xhtml') return preg_match('/xhtml$/',self::$content_type); if ($is == 'html') return preg_match('/html$/',self::$content_type); if ($is == 'xml') return preg_match('/xml$/',self::$content_type); + if ($is == 'text') return preg_match('/^text\//',self::$content_type); die("Db->is(): unknow argument '$is'. Accepted values are xhtml, html, xml"); } diff --git a/www/dbq/html/index.php b/www/dbq/html/index.php index 8ac3a3d2..c4c4e2e4 100644 --- a/www/dbq/html/index.php +++ b/www/dbq/html/index.php @@ -1,27 +1,133 @@ '', - 'table' => '', - 'action' => '', - 'format' => '', - ]; +#var_export([$_SERVER['REQUEST_URI']]); +if (empty($_SERVER['SERVER_PROTOCOL'])) $_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.0'; + +function run() { + $params = parse_uri([ + 'db' => 'ls', + 'table' => 'ls', + 'action' => 'ls', + 'format' => 'human', + ]); + + if (!$params['db']) not_implemented(); + + # + # Page + require_once($_SERVER['DOCUMENT_ROOT'].'/../../../lib/php/page.php'); + $Page = new Page(); + $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']; + + # + # Db actions pre conn + if ($params['db'] == 'help') { + #$this->out->rows($format,$rows,$head); + $Db->out([ + [ 'help', 'This help' ], + [ 'ls', 'List 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' ], + ],['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','sql'])); + 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 ($P as $p => $default) { + foreach ($params as $p => $default) { if ($i>=$count) break; - $P[$p] = $values[$i]; + $params[$p] = $values[$i]; $i++; } + return $params; } -#var_dump(parse_uri()); +run(); +exit; $params = [ 'db', @@ -30,11 +136,7 @@ $params = [ 'format', ]; if (false and strpos($_SERVER['REQUEST_URI'],'?')!==false) { - #return http_response_code(501); - $msg = '501 Not Implemented'; - header($_SERVER['SERVER_PROTOCOL'].' '.$msg); - echo "$msg\n"; - return; + return not_implemented(); } $_SERVER['REQUEST_URI'] = preg_replace('/\?.*$/','',$_SERVER['REQUEST_URI']); $values = explode('/',trim($_SERVER['REQUEST_URI'],'/')); @@ -85,4 +187,12 @@ $Page->call([ #debug($_SERVER['REQUEST_URI']); #debug($_SERVER['QUERY_STRING']); #debug($_SERVER); + +function not_implemented() { + #return http_response_code(501); + $msg = '501 Not Implemented'; + header($_SERVER['SERVER_PROTOCOL'].' '.$msg); + echo "$msg\n"; + exit; +} ?> -- 2.47.3