From f948b9aaaf5490f989d02ccf08d015c7fee1ad8c Mon Sep 17 00:00:00 2001 From: Nicolas Boisselier Date: Tue, 10 Jan 2017 00:51:22 +0000 Subject: [PATCH] Bed --- bin/nb-commit | 5 +- www/dbq/dbq.php | 148 +++++++++++++++++-------------- www/dbq/etc/nginx/default.conf | 6 +- www/dbq/html/default.js | 11 ++- www/dbq/html/default.min.css | 2 +- www/dbq/html/default.min.js | 3 +- www/dbq/{minify.sh => update.sh} | 0 7 files changed, 99 insertions(+), 76 deletions(-) rename www/dbq/{minify.sh => update.sh} (100%) diff --git a/bin/nb-commit b/bin/nb-commit index 15f54cad..688bdcad 100755 --- a/bin/nb-commit +++ b/bin/nb-commit @@ -10,6 +10,9 @@ set -e cd $NB_ROOT || exit +for f in www/*/update.sh; do + [ -x "$f" ] || continue + $f +done share/db/update.sh - which docker 1> /dev/null && docker run --rm -v $NB_ROOT:$NB_ROOT alpine sh -c "(. $NB_ROOT/etc/profile && nb_repos) >/dev/null && echo Docker: OK" diff --git a/www/dbq/dbq.php b/www/dbq/dbq.php index ba318e59..0d48d957 100644 --- a/www/dbq/dbq.php +++ b/www/dbq/dbq.php @@ -33,7 +33,7 @@ class DbQ extends nb { 'action' => '', 'args' => '', ]; -# NB 23.12.16 public $params_deep = []; + public $params_deep = []; # contain index in case if we decide to use a regexp # Db public $limits = []; @@ -44,6 +44,9 @@ class DbQ extends nb { public $js = '/default'.(PRODUCTION ? '.min' : '').'.js'; public $ext; + # Others + public $expode_args = '&'; + # Object public $page; public $table; @@ -53,6 +56,10 @@ class DbQ extends nb { // // Pre defaults values + // + // Init + $this->parse_uri(); + // Envs -> var foreach ([ 'perm', @@ -75,9 +82,6 @@ class DbQ extends nb { if (!empty($_SERVER[$env])) $this->params[$k] = $_SERVER[$env]; } - // - // Init - $this->uri_params(); $run = isset($opt['run']) ? $opt['run'] : false; unset($opt['run']); @@ -95,6 +99,12 @@ class DbQ extends nb { if ($run) $this->run(); } + public function json() { + $rows = []; + foreach ($this->json as $k=>$v) $rows[] = ['name'=>$k,'value'=>$v]; + return $rows; + } + public function limit($set=false) { static $limit=false; if ($set !== false) return ($limit=$set); @@ -116,8 +126,7 @@ class DbQ extends nb { $this->page->headers_no_cache(); $this->page->js = $this->js; - $this->page->js_code = 'window._dbq = ' - . json_encode([ + $this->json = [ 'db.base' => ( empty($this->db) ? '' : $this->db->base ), 'table.base' => ( empty($this->table) ? '' : $this->table->base ), 'default.format' => $this->format_html, @@ -127,8 +136,9 @@ class DbQ extends nb { 'param.table' => $this->params['table'], 'param.action' => $this->params['action'], 'param.args' => $this->params['args'], - ]) - .''; + 'param.deep' => $this->params_deep, + ]; + $this->page->js_code = 'window._dbq = '.json_encode($this->json); list($title,$nav) = $this->title_nav(); $this->page->title = join($this->sep_title,$title); @@ -180,7 +190,7 @@ class DbQ extends nb { $this->db->__construct($connect); $this->id = $this->params['db']; $this->db->connect(); - $this->db->base = '/'.$this->db->id.'/'; + $this->db->base = '/'.$this->db->id; return $this->db; } @@ -205,7 +215,7 @@ class DbQ extends nb { require_once(NB_ROOT.'/lib/php/db/config.php'); } $this->db = new Db(['conf'=>$DB_CONFS]); - $this->db->base = '/'; + $this->db->base = ''; return $this->db; } @@ -222,7 +232,7 @@ class DbQ extends nb { $values = $add ? array_fill(0,count($keys),'') : explode($this->param_args_sep,$this->params['args']); - $this->table->html_edit(array_combine($keys,$values),$this->table->base.($add ? 'insert/' : 'update/').$this->params['args'],$add); + $this->table->html_edit(array_combine($keys,$values),$this->table->base.($add ? 'insert' : 'update').'/'.$this->params['args'],$add); } public function table_rows($fct=null) { @@ -240,11 +250,11 @@ class DbQ extends nb { $args = $GLOBALS['dbq_args']; $r = ['Vew' => join(' ',[ - 'View' + 'View' ]) ] + $r; if ($this->perm < self::DELETE) return; - $rm = $this->table->base.'rm/'.$args; + $rm = $this->table->base.'/rm/'.$args; $r['Delete'] = '' .'
' .'Delete' @@ -289,7 +299,7 @@ class DbQ extends nb { #'params' => [ 'limit' ], ]); - $this->table->base = $this->db->base.$this->table->name.'/'; + $this->table->base = $this->db->base.'/'.$this->table->name; } @@ -300,7 +310,6 @@ class DbQ extends nb { $title = array_filter(array_unique(array_slice(array_values($params),1)), function($v){ return(in_array($v,self::ACTIONS_NO_TITLE) ? '' : $v); -# NB 07.01.17 return($v==self::PARAM_DB_DEFAULT ? '' : $v); } ); @@ -328,7 +337,7 @@ class DbQ extends nb { return [$title,$nav]; } - public function uri_params() { + public function parse_uri() { # # Parse path, respect params order # @@ -340,7 +349,7 @@ class DbQ extends nb { list ($path,$args) = strpos($_SERVER['REQUEST_URI'],'?') ? explode('?',$_SERVER['REQUEST_URI']) : [$_SERVER['REQUEST_URI'],'']; $this->uri = $path; - $this->uri_params = $args; + $this->uri_params = explode($this->expode_args,$args); if (preg_match('/\.(\w+)$/',$path,$m)) { $values[] = $m[1]; @@ -355,8 +364,10 @@ class DbQ extends nb { $i=0; $count = count($values); #var_export($values); + foreach ($this->params as $p => $default) { #if ($i>=$count) break; + if ($i<$count) { # Can be overwrite by get request @@ -377,9 +388,11 @@ class DbQ extends nb { } + $this->params_deep[$p] = $i; } + $i++; -# NB 23.12.16 $this->params_deep[$p] = $i; + } return $this->params; @@ -421,9 +434,44 @@ class DbQ extends nb { exit; } - public function status() { + public function status() { #$this->perm--; $rows = []; + // Client headers + foreach ($this->client_header() as $k=>$v) $rows[] = [ + 'name' => 'client.header.'.$k, + 'value' => ( is_scalar($v) ? $v : json_encode($v) ), + ]; + + // Server + foreach ([ + 'addr', + 'port', + 'software', + ] as $k) { + if ($this->perm < self::ADMIN and $k == 'software') continue; + + $v = 'SERVER_'.strtoupper($k); + $v = isset($_SERVER[$v]) ? $_SERVER[$v] : ''; + if ($v==='') continue; + + $rows[] = [ 'name' => 'server.'.$k, 'value' => $v ]; + } + + // Admin or bye ! + if ($this->perm < self::ADMIN) return $rows; + + /* + // Headers sent + foreach (headers_list() as $v) { + list($k,$v) = explode(': ',$v); + $rows[] = [ + 'name' => 'server.header.'.$k, + 'value' => ( is_scalar($v) ? $v : json_encode($v) ), + ]; + } + */ + // Dbq if ($this->client_content_type()) $rows[] = [ 'name' => 'dbq.client_content_type', @@ -450,45 +498,11 @@ class DbQ extends nb { } } - // Client headers - foreach ([ - 'addr', - ] as $k) { - $v = 'REMORE_'.strtoupper($k); - $v = isset($_SERVER[$v]) ? $_SERVER[$v] : ''; - if ($v==='') continue; - $rows[] = [ 'name' => 'server.'.$k, 'value' => $v ]; - } - foreach ($this->client_header() as $k=>$v) $rows[] = [ - 'name' => 'client.header.'.$k, - 'value' => ( is_scalar($v) ? $v : json_encode($v) ), - ]; - - // Envs - foreach ([ - 'addr', - 'port', - 'software', - ] as $k) { - $v = 'SERVER_'.strtoupper($k); - $v = isset($_SERVER[$v]) ? $_SERVER[$v] : ''; - if ($v==='') continue; - $rows[] = [ 'name' => 'server.'.$k, 'value' => $v ]; - } - - // Server headers - foreach (headers_list() as $v) { - list($k,$v) = explode(': ',$v); - $rows[] = [ - 'name' => 'server.header.'.$k, - 'value' => ( is_scalar($v) ? $v : json_encode($v) ), - ]; - } - return $rows; } - public function init() { + public function run_init() { + $this->db(); // Format first ! if ($this->params['format']) { @@ -592,7 +606,7 @@ class DbQ extends nb { [ 'status', 'Status infos page' ], ],['command','description'],function(&$r) { $r['command'] = $this->page->tag('a',$r['command'],'href="' - .$this->db->base.($r['command'] == self::PARAM_DB_DEFAULT + .$this->db->base.'/'.($r['command'] == self::PARAM_DB_DEFAULT ? '' : $r['command'].'.'.$this->ext ) @@ -603,7 +617,7 @@ class DbQ extends nb { #$this->params += [ 'db' => '', 'table' => '', 'action' => '' ]; #$this->params = array_merge($this->params,[ 'db' => '', 'table' => '', 'action' => '' ]); $this->page($this->db,'ls',[],function(&$r){ - $r['id'] = $this->page->tag('a',$r['id'],'href="'.$this->db->base.$r['id'].'.'.$this->ext.'"'); + $r['id'] = $this->page->tag('a',$r['id'],'href="'.$this->db->base.'/'.$r['id'].'.'.$this->ext.'"'); }); } elseif ($action == 'cryptkey') { @@ -615,6 +629,9 @@ class DbQ extends nb { } elseif ($this->perm < self::ADMIN) { // NOW ONLY FOR ADMIN ! + } elseif ($action == 'json') { + $this->page($this,'json'); + } elseif ($action == 'phpinfo') { #$this->page(['phpinfo'=>$this->page->phpinfo(true)]); #$this->page($this->page->phpinfo()); @@ -647,7 +664,7 @@ class DbQ extends nb { [ 'ls', 'List tables' ], ],['command','description'],function(&$r){ $r['command'] = $this->page->tag('a',$r['command'],'href="' - .$this->db->base.($r['command'] == self::PARAM_DB_DEFAULT + .$this->db->base.'/'.($r['command'] == self::PARAM_DB_DEFAULT ? '' : $r['command'].'.'.$this->ext ) @@ -656,7 +673,7 @@ class DbQ extends nb { } elseif ($action == 'ls') { $this->page('',$this->db->tables_rows(),[],function(&$r){ - $r['name'] = $this->page->tag('a',$r['name'],'href="'.$this->db->base.$r['name'].'.'.$this->ext.'"'); + $r['name'] = $this->page->tag('a',$r['name'],'href="'.$this->db->base.'/'.$r['name'].'.'.$this->ext.'"'); }); } elseif ($action == 'status') { @@ -688,7 +705,7 @@ class DbQ extends nb { [ 'status', 'Status page' ], ],['command','description'],function(&$r){ $r['command'] = $this->page->tag('a',$r['command'],'href="' - .$this->table->base.($r['command'] == self::PARAM_DB_DEFAULT + .$this->table->base.'/'.($r['command'] == self::PARAM_DB_DEFAULT ? '' : $r['command'].'.'.$this->ext ) @@ -712,16 +729,16 @@ class DbQ extends nb { } elseif ($action == 'insert' and $this->perm >= self::WRITE) { if (!$this->table->insert($_POST,$info)) $this->error('insert: '.print_r($info,true)); - header('Location: '.$this->table->base); + header('Location: '.$this->table->base.'/'); } elseif ($action == 'update' and $this->perm >= self::WRITE) { # NB 19.12.16: TODO NOT WORKING with update using replace if (!$this->table->replace($_POST,$info)) $this->error('update: '.print_r($info,true)); - header('Location: '.$this->table->base); + header('Location: '.$this->table->base.'/'); } elseif ($action == 'rm' and $this->perm >= self::DELETE) { if (!$this->table->delete($_POST,$info)) $this->error('rm: '.print_r($info,true)); - header('Location: '.$this->table->base); + header('Location: '.$this->table->base.'/'); } else { @@ -733,10 +750,9 @@ class DbQ extends nb { public function run() { # - # Db Init + # Init # - $this->db(); - $this->init(); + $this->run_init(); # # Root Actions diff --git a/www/dbq/etc/nginx/default.conf b/www/dbq/etc/nginx/default.conf index dbafb366..aa5577b3 100644 --- a/www/dbq/etc/nginx/default.conf +++ b/www/dbq/etc/nginx/default.conf @@ -28,9 +28,7 @@ server { #--------------------------------------------------------------# # Deny - location ~ /\.ht { - deny all; - } + location ~ /\.ht { deny all; } #--------------------------------------------------------------# # serve static files directly @@ -59,7 +57,7 @@ server { fastcgi_param DBQ_CONF_FILE $document_root/../../../etc/dbq/000-local.php; fastcgi_param DBQ_PERM 0; - fastcgi_param DBQ_PARAM_DB ls; +# NB 09.01.17 fastcgi_param DBQ_PARAM_DB ls; fastcgi_param PRODUCTION 1; } diff --git a/www/dbq/html/default.js b/www/dbq/html/default.js index 0f70bcb0..15f9c5d4 100644 --- a/www/dbq/html/default.js +++ b/www/dbq/html/default.js @@ -55,6 +55,8 @@ document.addEventListener("DOMContentLoaded", function() { /* Clean when submit */ document.querySelectorAll("form").forEach(function(e) { e.onsubmit = function() { + var limit = this.elements['limit']; + if (limit.value == window._dbq['default_limit']) limit.removeAttribute("value"); return form_submit_clean(this); }; }); @@ -69,8 +71,8 @@ document.addEventListener("DOMContentLoaded", function() { form.querySelectorAll("form.menu select.format").forEach(function(e) { e.removeAttribute('name'); e.onchange = function() { - this.form.setAttribute('action',this.form.getAttribute('action').replace(/^(.*\/[^\.?\/]+)(\.\w+)?/,'$1.'+this.value)); - this.form.onsubmit(); + this.form.setAttribute('action',window._dbq['table.base']+'.'+this.value); + return this.form.onsubmit(); return false; } }); @@ -79,15 +81,18 @@ document.addEventListener("DOMContentLoaded", function() { form.querySelectorAll("form.menu select.tables").forEach(function(e) { e.removeAttribute('name'); e.onchange = function() { - this.form.setAttribute('action',this.form.getAttribute('action').replace(/^(.*\/)[^\/\.\?]+/,'$1'+this.value)); + this.form.setAttribute('action',window._dbq['db.base']+'/'+this.value+'.html'); return this.form.onsubmit(); } }); /* Limit */ + /* + // NB 10.01.17: TODEL DONE ON SUBMIT form.querySelectorAll("form.menu select.limit option").forEach(function(e) { if (e.value == window._dbq["default.limit"]) e.setAttribute('value',''); }); + */ /* Auto submit */ form.querySelectorAll("form.menu select.limit").forEach(function(e) { diff --git a/www/dbq/html/default.min.css b/www/dbq/html/default.min.css index 6a3cbe59..31acc87d 100644 --- a/www/dbq/html/default.min.css +++ b/www/dbq/html/default.min.css @@ -1 +1 @@ -html{font:90% 'Trebuchet MS',sans-serif;background-color:#eee}body{display:table;margin-left:auto;margin-right:auto;padding-left:1em;padding-right:1em;color:#444}h1{margin:0 0 0.2em 0}a{color:inherit}a,a:visited{text-decoration:none}ul{padding:0;margin:0}li{list-style:none;padding:0 0 0 0.3em}table{border-collapse:collapse}td,th{padding:0.2em 0.7em}th a.sort{padding:0 0.3em}textarea,select,input:not([type]),input[type='text'],input[type='password'],input[type='date'],input[type='email'],input[type='url'],input[type='']{border-radius:3px;border:solid 1px #ddd;padding:0.3em 0.4em}input[size]{max-width:70%}div.row div label:after,form label:after{content:':'}form label,form input,form select{vertical-align:middle;margin:0.2em 0.4em 0.2em 0}table.rows,div.row,form.edit,.menu,.block,object,iframe,pre{border-radius:4px;border-collapse:separate;border-spacing:0;padding:0.5em 0.2em;margin-bottom:0.5em;border:solid 1px #ccc}.rows{padding:0}div.row div,table.rows th,table.rows td{border-bottom:solid 1px #ccc}div.row div,table.rows tr:last-child td{border-bottom:none}div.row div{margin:0}div.row div label{padding:0.1em 0.5em 0.1em 0;min-width:20%;display:inline-block}.menu,.nav,.rows,.buttons,.center{margin-left:auto;margin-right:auto;display:table}.button{font:inherit;display:inline-block;text-decoration:none;line-height:1.5em;cursor:pointer;border-radius:4px;white-space:nowrap;box-sizing:border-box;margin:0;padding:0 0.2em;color:#eee;border:solid 1px #000;background-color:#444}a:hover,.button:hover{opacity:0.8}form.menu{padding:0.5em;background-color:#F9F9F9}form.menu .button.add{float:right}form.menu .criterias{border-top:dashed 1px #ccc;margin-top:0.3em;padding-top:0.3em}form.menu .criterias span.label{border-right:solid 1px #fff;padding:0.2em 0.1em;margin:0.2em 0.2em 0 0;border-radius:3px}form.menu .criterias span.label:last-child{border-right:none}form.menu select:focus{max-width:auto}form.menu select{max-width:15%}form.menu .criterias input[type=text]:focus{width:auto}form.menu .criterias input[type=text]{width:4em}form.edit .fields label{width:25%;text-align:right;display:inline-block;margin-right:1em;vertical-align:top}form.edit .fields .label span{width:54%;display:inline-block;text-align:left}form.edit .fields input,form.edit .fields textarea,form.edit .fields select{width:65%}form.edit .fields label{width:25%;text-align:right;display:inline-block;margin-right:1em;vertical-align:top}form.edit .fields .label span{width:54%;display:inline-block;text-align:left}form.edit .fields input,form.edit .fields textarea,form.edit .fields select{width:65%} \ No newline at end of file +html{font:90% 'Trebuchet MS',sans-serif;background-color:#eee}body{display:table;margin-left:auto;margin-right:auto;padding-left:1em;padding-right:1em;color:#444}h1{margin:0 0 0.2em 0}a{color:inherit}a,a:visited{text-decoration:none}ul{padding:0;margin:0}li{list-style:none;padding:0 0 0 0.3em}table{border-collapse:collapse}td,th{padding:0.2em 0.7em}th a.sort{padding:0 0.3em}textarea,select,input:not([type]),input[type='text'],input[type='password'],input[type='date'],input[type='email'],input[type='url'],input[type='']{border-radius:3px;border:solid 1px #ddd;padding:0.3em 0.4em}input[size]{max-width:70%}div.row div label:after,form label:after{content:':'}form label,form input,form select{vertical-align:middle;margin:0.2em 0.4em 0.2em 0}table.rows,div.row,ul.row,form.edit,.menu,.block,object,iframe,pre{border-radius:4px;border-collapse:separate;border-spacing:0;padding:0.5em 0.2em;margin-bottom:0.5em;border:solid 1px #ccc}.rows{padding:0}ul.row li,div.row div,table.rows th,table.rows td{border-bottom:solid 1px #ccc}ul.row li,div.row div,table.rows tr:last-child td{border-bottom:none}div.row div{margin:0}ul.row li label,div.row div label{padding:0.1em 0.5em 0.1em 0;min-width:20%;display:inline-block}.menu,.nav,.rows,.buttons,.center{margin-left:auto;margin-right:auto;display:table}.button{font:inherit;display:inline-block;text-decoration:none;line-height:1.5em;cursor:pointer;border-radius:4px;white-space:nowrap;box-sizing:border-box;margin:0;padding:0 0.2em;color:#eee;border:solid 1px #000;background-color:#444}a:hover,.button:hover{opacity:0.8}form.menu{padding:0.5em;background-color:#F9F9F9}form.menu .button.add{float:right;margin-top:0.1em}form.menu .criterias{border-top:dashed 1px #ccc;margin-top:0.3em;padding-top:0.3em}form.menu .criterias span.label{border-right:solid 1px #fff;padding:0.2em 0.1em;margin:0.2em 0.2em 0 0;border-radius:3px}form.menu .criterias span.label:last-child{border-right:none}form.menu select:focus{max-width:auto}form.menu select{max-width:20%}form.menu .criterias input[type=text]:focus{width:auto}form.menu .criterias input[type=text]{width:4em}form.edit .fields label{width:25%;text-align:right;display:inline-block;margin-right:1em;vertical-align:top}form.edit .fields .label span{width:54%;display:inline-block;text-align:left}form.edit .fields input,form.edit .fields textarea,form.edit .fields select{width:65%}form.edit .fields label{width:25%;text-align:right;display:inline-block;margin-right:1em;vertical-align:top}form.edit .fields .label span{width:54%;display:inline-block;text-align:left} \ No newline at end of file diff --git a/www/dbq/html/default.min.js b/www/dbq/html/default.min.js index c947a76c..02fa8efc 100644 --- a/www/dbq/html/default.min.js +++ b/www/dbq/html/default.min.js @@ -2,4 +2,5 @@ function form_clean(e){var t for(i=0;i'+document.querySelector(".nav.bottom").innerHTML+""),document.querySelectorAll("form.menu input[type=submit]").forEach(function(e){e.insertAdjacentHTML("afterend",' '+(window._dbq["table.base"]?' ':""))})}) +i.name&&""!=i.value&&"undefined"!=i.value&&(n+=(n?"&":"?")+encodeURI(i.name)+"="+encodeURI(i.value))}return n=("?"==o?"":o)+n,e.reset(),window.location=n,!1}document.addEventListener("DOMContentLoaded",function(){NodeList.prototype.forEach=Array.prototype.forEach,document.querySelectorAll("form").forEach(function(e){e.onsubmit=function(){this.elements.limit +return form_submit_clean(this)}}),document.querySelectorAll("form.menu").forEach(function(e){e.setAttribute("action",window.location.toString().replace(/\?.*$/,"")),e.querySelectorAll("form.menu select.format").forEach(function(e){e.removeAttribute("name"),e.onchange=function(){return this.form.setAttribute("action",window._dbq["table.base"]+"."+this.value),this.form.onsubmit()}}),e.querySelectorAll("form.menu select.tables").forEach(function(e){e.removeAttribute("name"),e.onchange=function(){return this.form.setAttribute("action",window._dbq["db.base"]+"/"+this.value+".html"),this.form.onsubmit()}}),e.querySelectorAll("form.menu select.limit option").forEach(function(e){e.value==window._dbq["default.limit"]&&e.setAttribute("value","")}),e.querySelectorAll("form.menu select.limit").forEach(function(e){e.onchange=function(){return this.form.onsubmit()}})}),document.querySelector(".nav.bottom")&&document.querySelector("table.rows")&&document.querySelector("table.rows").insertAdjacentHTML("beforebegin",'"),document.querySelectorAll("form.menu input[type=submit]").forEach(function(e){e.insertAdjacentHTML("afterend",' '+(window._dbq["table.base"]?' ':""))})}) diff --git a/www/dbq/minify.sh b/www/dbq/update.sh similarity index 100% rename from www/dbq/minify.sh rename to www/dbq/update.sh -- 2.47.3