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',"+"),
'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',''),
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 '';
}
public static function head(&$o,$head,$data=[]) {
- $o['header'] = (self::p('header')==='0') ? false : true;
$o['out_count'] = 0;
# Format $head
if (isset($o['enclose'])) echo $o['enclose'][0];
if (0
- or !$o['header']
+ or !self::header()
or $head === false
#or !isset($o['head'])
) return;
}
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;
}
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,
];
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',
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',
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;
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
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<form.length;i++) {
- e = form.elements[i];
- if (e.style.display == "none") continue;
- if (e.type != "text") continue;
- e.value = ""
- }
-}
-
-function form_submit_clean(f) {
- var i = 0;
- var url = "";
- var action = f.getAttribute("action");
- var method = f.getAttribute("method");
- if (method != "get") return true;
-
- //if (typeof(action) == "undefined") action = "";
-
- for(i=0;i<f.length;i++) {
- var p = f[i];
- if (!p.name) continue;
- if (p.value=="") continue;
- if (p.value=="undefined") continue;
-// NB 02.09.13 url += (url ? "&" : "?") + escape(p.name) + "=" + escape(p.value);
- url += (url ? "&" : "?") + encodeURI(p.name) + "=" + encodeURI(p.value);
- }
-
- url = (action == "?" ? "" : action) + url;
-
- //document.location = url;
-
- //if (url == window.location) return false;
- window.location = url;
- return false;
-}
-
-document.addEventListener("DOMContentLoaded", function() {
-
- for (var e of document.querySelectorAll("form")) {
- e.onsubmit = function() {
- return form_submit_clean(this);
- };
- }
- document.getElementById("table").removeAttribute("name");
- document.getElementById("format").removeAttribute("name");
-
- for (var e of document.querySelectorAll(".menu select.tables, .menu select.format")) {
- e.onchange = function() {
- var format = document.getElementById("format").value;
- if (format == "'.self::HTML_FORMAT.'") format = "html";
- window.location = "./" + document.getElementById("table").value + "." + format;
- return false;
- };
- }
-
- for (var e of document.querySelectorAll(".menu select.limit")) {
- e.onchange = function() {
- this.form.submit();
- return false;
- };
- }
-
- if (document.querySelector(".nav.bottom")) {
- document.querySelector("table.rows").insertAdjacentHTML("beforebegin","<div class=\"nav top\">"+document.querySelector(".nav.bottom").innerHTML+"</div>");
- }
-
-});
+window._dbq = {
+ "HTML_FORMAT": "'.self::HTML_FORMAT.'"
+};
');
$this->page->begin();
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;
}
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;
}
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) )) {
} 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
;
$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() {