From 95c2dacc3a53170b35692d6b2680eeda1552cb97 Mon Sep 17 00:00:00 2001 From: Nicolas Boisselier Date: Fri, 23 Dec 2016 00:37:43 +0000 Subject: [PATCH] Bed --- lib/php/db.php | 2 +- lib/php/db/table.php | 8 +- lib/php/out.php | 19 ++-- www/dbq/dbq.php | 202 ++++++++++--------------------------------- 4 files changed, 67 insertions(+), 164 deletions(-) diff --git a/lib/php/db.php b/lib/php/db.php index e47ef864..2dc61595 100644 --- a/lib/php/db.php +++ b/lib/php/db.php @@ -176,7 +176,7 @@ class Db extends nb { } # Add formats from module out - foreach($this->out->types() as $t=>$v) if (!in_array($t,$this->formats)) $this->formats[] = $t; + foreach($this->out->types('_web_') as $t=>$v) if (!in_array($t,$this->formats)) $this->formats[] = $t; # Pdo $this->connect_init(); diff --git a/lib/php/db/table.php b/lib/php/db/table.php index d11842c9..d3116fbf 100644 --- a/lib/php/db/table.php +++ b/lib/php/db/table.php @@ -22,7 +22,6 @@ Class Table extends nb { public $name; public $type; # table, view, sql public $sql; -# NB 28.03.16 public $fields_keys; public $replace = []; # replace by javascript public $extras = []; public $row_parse_pre; # Function to call in rows() @@ -96,7 +95,11 @@ Class Table extends nb { foreach ($opt as $k => $v) { $this->$k = $v; } # NB 22.12.16: TODEL - if (!isset($this->show_header)) $this->show_header = (bool)$this->p('header'); + $this->show_header = + (bool)$this->p('header',$this->show_header) + ; + # TODE FOR out:: + $this->pdef('header',$this->show_header); } @@ -887,6 +890,7 @@ Class Table extends nb { # Run query # $this->create_temporary(); + $this->db()->out->header($this->show_header); if ($opt_by_val !== null) $opt = $opt_by_val; diff --git a/lib/php/out.php b/lib/php/out.php index 468c3500..5ce0c058 100644 --- a/lib/php/out.php +++ b/lib/php/out.php @@ -6,10 +6,16 @@ Class Out extends Nb { public static $charset = 'utf-8'; protected static $types; protected static $type; + protected static $header = true; public static function init() { + self::$header = + (bool)self::p('header',self::$header) + ; self::$types = [ + 'sql' => [], # Code still in db/table.php ! + 'human' => [ 'sep' => self::p('sep'," | "), 'corner' => self::p('corner',"+"), @@ -20,9 +26,8 @@ Class Out extends Nb { 'end' => 'out_human_end', ], - 'sql' => [], # Code still in db/table.php ! - 'cust' => [ + '_web_' => false, 'cust' => self::p('cust',"CHANGE ME"), 'at' => self::p('at','@'), 'open' => self::p('open',''), @@ -218,11 +223,13 @@ Class Out extends Nb { public static function charset($set=null) { if (!empty($set)) self::$charset = $set; return self::$charset; } public static function types($type=null) { + if ($type === '_web_') return array_filter(self::$types,function($v){return !isset($v['_web_']) or (bool)$v['_web_'];}); if (!empty($type)) return(empty(self::$types[$type]) ? null : self::$types[$type] ); return self::$types; } public static function type($set=null) { if (!empty($set)) self::$type = $set; return self::$type; } + public static function header($set=null) { if (!empty($set)) self::$header = $set; return self::$header; } public static function scalar($v) { if ($v === null) return ''; @@ -283,7 +290,6 @@ Class Out extends Nb { } public static function head(&$o,$head,$data=[]) { - $o['header'] = (self::p('header')==='0') ? false : true; $o['out_count'] = 0; # Format $head @@ -317,7 +323,7 @@ Class Out extends Nb { if (isset($o['enclose'])) echo $o['enclose'][0]; if (0 - or !$o['header'] + or !self::header() or $head === false #or !isset($o['head']) ) return; @@ -506,12 +512,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) { + if (!self::header()) { echo $v.$o['sep']; continue; } @@ -628,7 +633,7 @@ function out_tag(&$o,&$row) { function out_human_head(&$o,$head) { #$line = "a\tb\tc\t\t\tg"; bye(explode("\t",$line)); - if (out::p('header')==='0') return ''; + if (!out::header()) return ''; $o['_human'] = [ 'head' => 1, ]; diff --git a/www/dbq/dbq.php b/www/dbq/dbq.php index 92a384b7..ede271a1 100644 --- a/www/dbq/dbq.php +++ b/www/dbq/dbq.php @@ -3,40 +3,28 @@ require_once(realpath(dirname(__FILE__).'/../../lib/php/nb.php')); require_once(NB_ROOT.'/lib/php/http.php'); require_once(NB_ROOT.'/lib/php/mime.php'); -# Minimum permission access 0 = readonly -define('DBQ_PERM',(int)(isset($_SERVER['DBQ_PERM']) ? $_SERVER['DBQ_PERM'] : 9)); - -define('DBQ_HUMAN_UA_EXP',(string)(isset($_SERVER['DBQ_HUMAN_UA_EXP']) - ? $_SERVER['DBQ_HUMAN_UA_EXP'] - : '/^\S+\s+.Windows|iPhone|Android|Macintosh|Linux/') -); - -define('DBQ_PARAM_DEFAULT',(string)(isset($_SERVER['DBQ_PARAM_DEFAULT']) ? $_SERVER['DBQ_PARAM_DEFAULT'] : 'ls')); -define('DBQ_PARAM_FORMAT',(string)(isset($_SERVER['DBQ_PARAM_FORMAT']) ? $_SERVER['DBQ_PARAM_FORMAT'] : '')); - -define('DBQ_CSS',(string)(isset($_SERVER['DBQ_CSS']) - ? $_SERVER['DBQ_CSS'] - : '/default.css') -); - -define('DBQ_TITLE',(string)(isset($_SERVER['DBQ_TITLE']) - ? $_SERVER['DBQ_TITLE'] - : 'Dbq') +/* + Default action +*/ +define('DBQ_PARAM_DEFAULT',(string)(isset($_SERVER['DBQ_PARAM_DEFAULT']) + ? $_SERVER['DBQ_PARAM_DEFAULT'] + : 'ls') ); class DbQ extends nb { - - public $perm = DBQ_PERM; + + public $perm = 9; // Minimum permission access 0 = readonly const ADMIN = 9; const DELETE = 4; const WRITE = 3; const VIEW = 1; - public $title = DBQ_TITLE; + public $title = 'Dbq'; public $sep = ' / '; const HTML_FORMAT = 'table'; const CLI_FORMAT = 'human'; + public $human_ua_exp = '/^\S+\s+.Windows|iPhone|Android|Macintosh|Linux/'; public $formats = [ 'table', 'csv', @@ -50,7 +38,7 @@ class DbQ extends nb { const PARAM_EXP = '[\w\._:-]{2,100}'; const PARAM_DB_DEFAULT = DBQ_PARAM_DEFAULT; public $params = [ - 'format' => DBQ_PARAM_FORMAT, + 'format' => '', 'db' => self::PARAM_DB_DEFAULT, 'table' => 'ls', 'action' => 'ls', @@ -60,20 +48,8 @@ class DbQ extends nb { public $run = false; # call run() when __contruct -# NB 10.12.16 public $_colors = [ -# NB 10.12.16 'body' => '#ddd', -# NB 10.12.16 'body-background' => '#009B9C', -# NB 10.12.16 -# NB 10.12.16 #'button' => '#ddd', -# NB 10.12.16 #'button-background' => '#3C3C3B', -# NB 10.12.16 'button' => '#444', -# NB 10.12.16 'button-background' => '#aaa', -# NB 10.12.16 'button-border' => '#888', -# NB 10.12.16 ]; - public $colors = [ - ]; - - public $css = DBQ_CSS; + public $css = '/default.css'; + public $js = '/default.js'; public $ext; public $uri; @@ -85,17 +61,31 @@ class DbQ extends nb { public $db; public function __construct($opt=[]) { - #debug(self::VIEW); - // // Pre defaults values + // Envs -> var + foreach ([ + 'perm', + 'title', + 'css', + 'js', + 'human_ua_exp' + ] as $k) { + $env = 'DBQ_'.strtoupper($k); + if (!empty($_SERVER[$env])) $this->$k = $_SERVER[$env]; + } // Envs -> params foreach ([ + 'format', + 'db', + 'table', ] as $k) { $env = 'DBQ_PARAM_'.strtoupper($k); - if (!empty($_SERVER[$env])) $this->env = $_SERVER[$env]; + if (!empty($_SERVER[$env])) $this->params[$k] = $_SERVER[$env]; } + /* + */ // // Init @@ -115,116 +105,16 @@ class DbQ extends nb { if ($this->run) $this->run(); } - public function page_colors() { - if (!empty($this->colors)) { - if (empty($this->colors['body-border'])) $this->colors['body-border'] = $this->colors['body']; - if (empty($this->colors['button'])) $this->colors['button'] = $this->colors['body']; - if (empty($this->colors['button-background'])) $this->colors['button-background'] = $this->colors['body-background']; - if (empty($this->colors['button-border'])) $this->colors['button-border'] = $this->colors['body-border']; - $this->page->css_code = trim(' -body, .button { - color: '.$this->colors['body'].'; - background-color: '.$this->colors['body-background'].'; -} -table.rows, -div.rows, -.menu, -.block, -form.edit, -object, iframe, pre -{ - border-color: '.$this->colors['body'].'; -} -table.rows th, table.rows td { border-color: '.$this->colors['body-border'].'; } -.button { - color: '.$this->colors['button'].'; - border-color: '.$this->colors['button-border'].'; - background-color: '.$this->colors['button-background'].'; -} -'); - } - } - public function page($obj,$meth=null,$head=[],$fct=null) { if (empty($this->_nopage)) { // Write output - $this->page_colors(); $this->page->headers_no_cache(); + $this->page->js = $this->js; $this->page->js_code = trim(' -function form_submit_clean(form) { - form_clean(form); - form.submit(); -} - -function form_clean(form) { - var e; - for(i=0;i"+document.querySelector(".nav.bottom").innerHTML+""); - } - -}); +window._dbq = { + "HTML_FORMAT": "'.self::HTML_FORMAT.'" +}; '); $this->page->begin(); @@ -474,9 +364,12 @@ document.addEventListener("DOMContentLoaded", function() { public function not_found($admin_msg='') { header('Content-type: text/plain'); $msg = '404 Not Found'; + if (empty($_SERVER['SERVER_PROTOCOL'])) $_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.0'; + header($_SERVER['SERVER_PROTOCOL'].' '.$msg); echo "$msg\n"; + if (!empty($admin_msg) and $this->perm >= self::ADMIN) echo "$admin_msg\n"; exit; } @@ -484,9 +377,12 @@ document.addEventListener("DOMContentLoaded", function() { public function error($admin_msg='') { header('Content-type: text/plain'); $msg = '500 Internal Server Error'; + if (empty($_SERVER['SERVER_PROTOCOL'])) $_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.0'; + header($_SERVER['SERVER_PROTOCOL'].' '.$msg); echo "$msg\n"; + if (!empty($admin_msg) and $this->perm >= self::ADMIN) echo "$admin_msg\n"; exit; } @@ -548,10 +444,7 @@ document.addEventListener("DOMContentLoaded", function() { public function init() { // Format first ! - if (!empty(DBQ_PARAM_FORMAT)) { - $format = DBQ_PARAM_FORMAT; - - } else if ($this->params['format']) { + if ($this->params['format']) { $format = $this->params['format']; } else if ($content_type = self::client_content_type() and ( $format=Mime::toExt($content_type) )) { @@ -559,7 +452,7 @@ document.addEventListener("DOMContentLoaded", function() { } else { $format = - (!empty($_SERVER['HTTP_USER_AGENT']) and preg_match(DBQ_HUMAN_UA_EXP,$_SERVER['HTTP_USER_AGENT'])) + (!empty($_SERVER['HTTP_USER_AGENT']) and preg_match($this->human_ua_exp,$_SERVER['HTTP_USER_AGENT'])) ? 'html' : self::CLI_FORMAT ; @@ -573,17 +466,18 @@ document.addEventListener("DOMContentLoaded", function() { $this->page->title = join($this->sep,$title); $this->page->nav = $nav; - if ($format == 'html') $format = self::HTML_FORMAT; - $this->params['format'] = $format; - $this->db->format = $format; - if (!empty($this->formats)) $this->db->formats = $this->formats; // Then content type if (empty($content_type)) $content_type = Mime::fromExt($format==self::CLI_FORMAT ? 'txt' : $format); - #if (empty($content_type)) $content_type = 'text/plain'; + if (empty($content_type)) $content_type = 'text/plain'; if ($content_type) $this->page->content_type($content_type); + // Affect values to objects + if ($format == 'html') $format = self::HTML_FORMAT; + $this->params['format'] = $format; + $this->db->format = $format; + } public function page_phpinfo() { -- 2.47.3