From: Nicolas Boisselier Date: Tue, 27 Dec 2016 22:59:09 +0000 (+0000) Subject: clean up X-Git-Url: https://git.nbdom.net/?a=commitdiff_plain;h=459a8b52b3133bfb43650d08eeda1284efafb674;p=nb.git clean up --- diff --git a/lib/js/nb.js b/lib/js/nb.js index 748738fc..7c413c5f 100644 --- a/lib/js/nb.js +++ b/lib/js/nb.js @@ -239,6 +239,7 @@ function NB(args) { //document.location = url; //if (url == window.location) return false; + f.reset(); window.location = url; return false; } diff --git a/lib/php/db/index.php b/lib/php/db/index.php index ff9f1aa3..36175933 100755 --- a/lib/php/db/index.php +++ b/lib/php/db/index.php @@ -6,7 +6,6 @@ * * lib/php/db/index.php */ -#require_once(realpath(dirname(__FILE__).'/page.php')); return; require_once(realpath(dirname(__FILE__).'/init.php')); if(defined('DB_NO_ACTION') and DB_NO_ACTION) return true; if (!isset($Db)) global $Db, $Table; diff --git a/lib/php/db/init.php b/lib/php/db/init.php index f4279ab4..21688967 100644 --- a/lib/php/db/init.php +++ b/lib/php/db/init.php @@ -1,8 +1,4 @@ diff --git a/lib/php/db/page.php b/lib/php/db/page.php index 1baa64e6..2f69c69e 100644 --- a/lib/php/db/page.php +++ b/lib/php/db/page.php @@ -1,6 +1,6 @@ ($Db->title ? $Db->title : Db::prettyText($Db->name)), diff --git a/lib/php/db/table.php b/lib/php/db/table.php index 14683115..9ab8f849 100644 --- a/lib/php/db/table.php +++ b/lib/php/db/table.php @@ -1,7 +1,7 @@ name,'SELECT ')===0) { - #$GLOBALS['DB_TABLE_QUERY_ID']++; $name = DB_TABLE_QUERY_NAME . $GLOBALS['DB_TABLE_QUERY_ID']; $this->db()->conn->query("CREATE TEMPORARY TABLE ".DB_TABLE_QUERY_NAME." AS $this->name"); $this->name = DB_TABLE_QUERY_NAME; // Virtual Table } elseif (DB_TABLE_QUERY_NAME and !empty($this->sql)) { + if ($this->name) $this->name = DB_TABLE_QUERY_NAME; $this->db()->conn->query("CREATE TEMPORARY TABLE $this->name AS $this->sql"); } elseif (preg_match('/\b(\.import|LOAD DATA|COPY|INSERT|REPLACE|DELETE|TRUNCATE|CREATE|DROP|ALERT)\b/',$this->name)) { @@ -1179,7 +1179,7 @@ Class Table extends nb { Wordpress -----------------------------------------------------------------*/ public function rows_begin_wp() { - require_once(realpath(dirname(__FILE__).'/wp.php')); + require_once(NB_ROOT.'/lib/php/db/wp.php')); $this->_html_table = new html_table($this); return ''; } diff --git a/lib/php/db/types/sqlite.php b/lib/php/db/types/sqlite.php index 8825db23..12b2e271 100644 --- a/lib/php/db/types/sqlite.php +++ b/lib/php/db/types/sqlite.php @@ -89,15 +89,19 @@ $DB_TYPES['sqlite'] = array ( 'table.fields' => array ( 'sql' => 'PRAGMA table_info(\'\')', - 'fct' => create_function('&$r',join('',array( - #'debug("zaza");', - '$r["key"] = $r["pk"] == "0" ? 0 : 1;', - 'if (!empty($r["dflt_value"])) $r["default"] = trim($r["dflt_value"],"\'");', - '$r["null"] = $r["notnull"] == "0" ? 1 : 0;', - '$r["autoincrement"] = preg_match("/[\(,]\s*".$r["name"]." [^\),]+ AUTOINCREMENT/",$r["this"]->sql()) ? 1 : 0;', - '$r["extra"] = preg_match("/[\(,]\s*".$r["name"]." \S+ (COLLATE[^,)]+)/",$r["this"]->sql(),$m) ? trim($m[1]) : "";', - #'debug($m);' - ))), + 'fct' => function(&$r) { + $r["key"] = $r["pk"] == "0" ? 0 : 1; + + if (!empty($r["dflt_value"])) $r["default"] = trim($r["dflt_value"],"'"); + + $r["null"] = $r["notnull"] == "0" ? 1 : 0; + + $sql = $r["this"]->sql(); + $name_exp = preg_quote($r["name"]); + $r["autoincrement"] = preg_match("/[\(,]\s*".$name_exp." [^\),]+ AUTOINCREMENT/",$sql) ? 1 : 0; + + $r["extra"] = preg_match("/[\(,]\s*".$name_exp." \S+ (COLLATE[^,)]+)/",$sql,$m) ? trim($m[1]) : ""; + }, ), 'field.create' => function(&$field) { diff --git a/lib/php/default.php.TODEL b/lib/php/default.php.TODEL deleted file mode 100644 index 4f30e1c6..00000000 --- a/lib/php/default.php.TODEL +++ /dev/null @@ -1,7 +0,0 @@ - diff --git a/lib/php/functions.php b/lib/php/functions.php index b2302481..d63ecbaa 100644 --- a/lib/php/functions.php +++ b/lib/php/functions.php @@ -54,7 +54,7 @@ function txt2md($txt) { # NB 28.11.16: TODO rename as nb::md2html global $_txt2md; if ($_txt2md === null) { - require_once(dirname(__FILE__).'/parsedown/Parsedown.php'); + require_once(NB_ROOT.'/lib/php/parsedown/Parsedown.php'); $_txt2md = new Parsedown(); } return $_txt2md->text($txt); diff --git a/lib/php/page.php b/lib/php/page.php index 26113fe7..188527a9 100644 --- a/lib/php/page.php +++ b/lib/php/page.php @@ -1,6 +1,6 @@ '', - 'db' => self::PARAM_DB_DEFAULT, - 'table' => self::PARAM_DB_DEFAULT, - 'action' => self::PARAM_DB_DEFAULT, + 'db' => '', + 'table' => '', + 'action' => '', 'args' => '', ]; # NB 23.12.16 public $params_deep = []; + # Db + public $limits = []; + public $formats = [ 'html','csv','xml','json','yaml','sh','sql','php' ]; + + # Page public $css = '/default'.(PRODUCTION ? '.min' : '').'.css'; public $js = '/default'.(PRODUCTION ? '.min' : '').'.js'; public $ext; - public $uri; - public $uri_params; - # Object public $page; public $table; @@ -58,11 +58,12 @@ class DbQ extends nb { 'title', 'css', 'js', - 'format_cli_ua_exp' + 'format_html_ua_exp' ] as $k) { $env = 'DBQ_'.strtoupper($k); if (!empty($_SERVER[$env])) $this->$k = $_SERVER[$env]; } + // Envs -> params foreach ([ 'format', @@ -72,8 +73,6 @@ class DbQ extends nb { $env = 'DBQ_PARAM_'.strtoupper($k); if (!empty($_SERVER[$env])) $this->params[$k] = $_SERVER[$env]; } - /* - */ // // Init @@ -82,7 +81,6 @@ class DbQ extends nb { unset($opt['run']); parent::__construct($opt); - if (!$this->params['db']) $this->not_implemented('Db missing'); // // Page @@ -173,6 +171,7 @@ class DbQ extends nb { // Connect if (!empty($connect)) { + if (!$this->params['db']) $this->not_implemented('Params db missing'); if (!isset($this->db)) $this->db = $this->db(); if (!is_array($connect) @@ -182,7 +181,7 @@ class DbQ extends nb { $connect = $this->db->conf[$this->params['db']]; } - if (empty($connect['name'])) $this->not_found('Unknown database'); + if (empty($connect['name'])) $this->not_found('Unknown database: '.$this->params['db']); $this->db->__construct($connect); $this->id = $this->params['db']; @@ -276,9 +275,14 @@ class DbQ extends nb { public function table() { + if (!in_array($this->params['table'],array_keys($this->db->tables()))) { - #$this->bye($this->db->tables()); - $this->not_found('Unknown table'); + + # We Allow SELECT only in admin + if ($this->perm < self::ADMIN or !preg_match('/^SELECT /',$this->params['table'])) { + $this->not_found('Unknown table: '.$this->params['table']); + } + } $this->table = $this->db->table($this->params['table'],[ @@ -332,6 +336,9 @@ class DbQ extends nb { } public function uri_params() { + # + # Parse path, respect params order + # $values = []; $path = ''; $args = ''; @@ -499,7 +506,7 @@ class DbQ extends nb { } else { $format = - (!empty($_SERVER['HTTP_USER_AGENT']) and preg_match($this->format_cli_ua_exp,$_SERVER['HTTP_USER_AGENT'])) + (!empty($_SERVER['HTTP_USER_AGENT']) and preg_match($this->format_html_ua_exp,$_SERVER['HTTP_USER_AGENT'])) ? 'html' : $this->format_cli ; @@ -570,18 +577,9 @@ class DbQ extends nb { return $this->page($this->page->phpinfo($txt)); } - public function run() { - - # - # Db Init - # - $this->db(); - $this->init(); - - # - # Actions pre Db - # + public function run_root() { $action = $this->params['db']; + if (!$action and self::PARAM_DB_DEFAULT) $action = self::PARAM_DB_DEFAULT; #$this->params = [ 'action' => $action ]; if ($action == 'help') { @@ -633,18 +631,13 @@ class DbQ extends nb { $this->page($rows); } + } - # - # Db Connections - # - $this->params['db'] = $action; - $this->db(true); - - # - # Db actions - # + public function run_db() { + $this->db(true); # Db Connections $action = $this->params['table']; - $this->params['table'] = ''; + if (!$action and self::PARAM_DB_DEFAULT) $action = self::PARAM_DB_DEFAULT; +# NB 27.12.16 $this->params['table'] = ''; if ($action == 'help') { $this->page($this->db,[ @@ -678,16 +671,13 @@ class DbQ extends nb { } - # - # Table Init - # - $this->params['table'] = $action; - $this->table(); - - # - # Table action - # + } + + public function run_table() { + $this->table(); # Table init $action = $this->params['action']; + if (!$action and self::PARAM_DB_DEFAULT) $action = self::PARAM_DB_DEFAULT; + #bye([$this->params,$action]); if ($action == 'help') { $this->page($this->table,[ @@ -737,6 +727,30 @@ class DbQ extends nb { $this->not_implemented('Unknown table action'." `$action`"); } + } + + public function run() { + + # + # Db Init + # + $this->db(); + $this->init(); + + # + # Root Actions + # + $this->run_root(); + + # + # Db actions + # + $this->run_db(); + + # + # Table action + # + $this->run_table(); return true; } diff --git a/www/dbq/html/default.css b/www/dbq/html/default.css index b796c96f..91f87f4e 100644 --- a/www/dbq/html/default.css +++ b/www/dbq/html/default.css @@ -36,6 +36,9 @@ li { table { border-collapse: collapse; } td, th { padding: 0.2em 0.7em; } +/* +td form { display: inline-block; } +*/ th a.sort { padding: 0 0.3em; } textarea, diff --git a/www/dbq/html/default.js b/www/dbq/html/default.js index 00e38ec1..0f70bcb0 100644 --- a/www/dbq/html/default.js +++ b/www/dbq/html/default.js @@ -42,6 +42,7 @@ function form_submit_clean(f) { url = (action == "?" ? "" : action) + url; + f.reset(); window.location = url; return false; }