<?php
+if (!defined('DBQ_ADMIN')) define('DBQ_ADMIN',true);
+if (!defined('DBQ_TITLE')) define('DBQ_TITLE',basename(__FILE__,'.php'));
-function dbq_run() {
+require_once(dirname(__FILE__).'/../../lib/php/nb.php');
+class DbQ extends nb {
+ public $params = [
+ 'format' => 'human',
+ 'db' => 'ls',
+ 'table' => 'ls',
+ 'action' => 'ls',
+ ];
- $colors = [
+ public $colors = [
'text' => '#ddd',
'background' => '#009B9C',
#'button' => '#444',
- 'button' => '#3C3C3B',
+ 'button' => '#ddd',
+ 'button-backgound' => '#3C3C3B',
];
- if (empty($_SERVER['DOCUMENT_ROOT'])) $_SERVER['DOCUMENT_ROOT'] = dirname(__FILE__).'/html';
- require_once($_SERVER['DOCUMENT_ROOT'].'/../../../lib/php/page.php');
- #
- # Url
- if (empty($_SERVER['HTTP_USER_AGENT'])) $_SERVER['HTTP_USER_AGENT'] = '';
- $params = parse_uri([
- 'format' => ( preg_match('/^\S+\s+.Windows|iPhone|Android|Macintosh/',$_SERVER['HTTP_USER_AGENT']) ? 'html' : 'human' ),
- 'db' => 'ls',
- 'table' => 'ls',
- 'action' => Page::p('action','ls'),
- 'params' => '',
- ]);
- #var_export($params);
- $ext = $params['format'];
- if ($params['format'] == 'html') $params['format'] = 'table';
- if (!$params['db']) not_implemented();
+ public $css = '/default.css';
+ public $ext;
+ public $paths = [];
+
+ # Object
+ public $page;
+ public $table;
+ public $db;
+
+ public function __construct($opt=[]) {
+ #bye($this->params);
+
+ $this->uri_params();
+ $this->params['format'] =
+ preg_match('/^\S+\s+.Windows|iPhone|Android|Macintosh|Linux/',$_SERVER['HTTP_USER_AGENT'])
+ ? 'html'
+ : $this->params['format']
+ ;
+
+ parent::__construct($opt);
+ if (!$this->params['db']) not_implemented();
+
+ if ($this->params['format'] == 'html') $this->params['format'] = 'table';
+ if (empty($this->ext)) $this->ext = $this->params['format'];
+
+ }
+
+ public function page($obj,$meth=null,$head=[],$fct=null) {
+ require_once(NB_ROOT.'/lib/php/page.php');
+
+ list($title,$nav) = title_nav($this->params,$this->ext);
+
+ $this->page = new Page([
+ 'css' => $this->css,
+ 'title' => join(' ',$title),
+ 'nav' => $nav,
+ ]);
+
+ if (!empty($this->colors)) $this->page->css_code = trim('
+body, .button {
+ color: '.$this->colors['text'].';
+ background-color: '.$this->colors['background'].';
+}
+.button {
+ color: '.$this->colors['button'].';
+ border-color: '.$this->colors['button-backgound'].';
+ background-color: '.$this->colors['button-backgound'].';
+}
+table.rows,
+div.rows,
+.menu,
+.block,
+form.edit,
+object, iframe, pre
+{
+ border-color: '.$this->colors['text'].';
+}
+table.rows th, table.rows td { border-color: '.$this->colors['text'].'; }
+');
+
+if (empty($this->_nopage)) {
+ $this->page->content_type($this->page->ext2mime($this->params['format']) ? $this->page->ext2mime($this->params['format']) : 'text/plain');
+ if ($this->page->is('text') and !$this->page->is('html')) $this->page->content_type('text/plain');
+
+ $this->page->headers_no_cache();
+ $this->page->begin();
+}
+
+ #die($obj);
+ #if (is_scalar($obj)) {
+ if (empty($meth)) {
+ echo $obj;
+ } elseif(is_array($meth)) {
+ $this->db->out($meth,$head,['row_parse_post'=>$fct]);
+ } else {
+ $out = $obj->$meth();
+ if (!empty($out)) {
+ $this->db->out($out,$head,['row_parse_post'=>$fct]);
+ }
+ }
+
+ $this->page->end();
+ exit;
+ }
+
+ public function db($connect=[]) {
+
+ if (!empty($connect)) {
+ if (!isset($this->db)) $this->db = $this->db();
+ $this->db->__construct($connect);
+ $this->db->connect();
+ return $this->db;
+ }
+
+ global $DB_CONFS;
+ require_once(NB_ROOT.'/lib/php/db/config.php');
+ $this->db = new Db(['conf'=>$DB_CONFS]);
+ $this->db->format = $this->params['format'];
+ return $this->db;
+ }
+
+ public function table($obj,$meth,$head=[],$fct=null) {
+ $this->table = $this->db->table($this->params['table']);
+ }
+
+ public function title_nav() {
+ $params = $this->params;
+ $ext = $this->ext;
+ $title = array_filter(array_unique(array_slice(array_values($params),1)),function($v){ return($v=='ls' ? '' : $v); });
+ #array_unshift($title,Page::filename2title());
+ array_unshift($title,DBQ_TITLE);
+
+ $path = '/';
+ $nav = [];
+
+ $i = 0;
+ foreach ($title as $k=>$v) {
+
+ if ($i == 0) {
+ $nav[] = [$v,'/'];
+
+ } else {
+ $nav[] = [$v,"$path$v.".$ext];
+ $path .= "$v/";
+
+ }
+
+ $i++;
+
+ }
+
+ return [$title,$nav];
+ }
+
+ public function uri_params() {
+ $values = [];
+
+ if (empty($_SERVER['REQUEST_URI'])) $_SERVER['REQUEST_URI'] = join('/',count($GLOBALS['argv'])>0 ? array_slice($GLOBALS['argv'],1) : []);
+
+ $path = preg_replace('/\?.*$/','',$_SERVER['REQUEST_URI']);
+
+ if (preg_match('/\.(\w+)$/',$path,$m)) {
+ $values[] = $m[1];
+ $path = substr($path,0,strlen($path)-strlen($m[1])-1);
+ } else {
+ $values[] = '';
+ }
+
+ $path = trim($path,'/');
+ if ($path) $values = array_merge($values,explode('/',$path));
+
+ $i=0;
+ $count = count($values);
+ #var_export($values);
+ foreach ($this->params as $p => $default) {
+ if ($i>=$count) break;
+ if ($values[$i]!=='') {
+ if (!preg_match('/^[\w\._-]{2,100}$/',$values[$i])) not_implemented();
+ $this->params[$p] = $values[$i];
+ }
+ $i++;
+ }
+
+ return $this->params;
+ }
+
+
+}
+
+function page_init($params,$ext) {
+ $colors = [
+ 'text' => '#ddd',
+ 'background' => '#009B9C',
+ #'button' => '#444',
+ 'button' => '#ddd',
+ 'button-backgound' => '#3C3C3B',
+ ];
- #
- # Page
list($title,$nav) = title_nav($params,$ext);
- $params['ext'] = $ext;
- #debug($nav);
$Page = new Page([
'css' => '/default.css',
'title' => join(' ',$title),
background-color: '.$colors['background'].';
}
.button {
+ color: '.$colors['button'].';
border-color: '.$colors['button'].';
- background-color: '.$colors['button'].';
+ background-color: '.$colors['button-backgound'].';
}
table.rows,
div.rows,
.menu,
+.block,
form.edit,
object, iframe, pre
{
}
table.rows th, table.rows td { border-color: '.$colors['text'].'; }
');
+
$Page->content_type($Page->ext2mime($params['format']) ? $Page->ext2mime($params['format']) : 'text/plain');
if ($Page->is('text') and !$Page->is('html')) $Page->content_type('text/plain');
+
+ return $Page;
+}
+
+function dbq_run() {
+
+ if (empty($_SERVER['DOCUMENT_ROOT'])) $_SERVER['DOCUMENT_ROOT'] = dirname(__FILE__).'/html';
+ require_once($_SERVER['DOCUMENT_ROOT'].'/../../../lib/php/page.php');
+
+ #
+ # Url
+ if (empty($_SERVER['HTTP_USER_AGENT'])) $_SERVER['HTTP_USER_AGENT'] = '';
+ $params = parse_uri([
+ 'format' => ( preg_match('/^\S+\s+.Windows|iPhone|Android|Macintosh/',$_SERVER['HTTP_USER_AGENT']) ? 'html' : 'human' ),
+ 'db' => 'ls',
+ 'table' => 'ls',
+ 'action' => Page::p('action','ls'),
+ ]);
+ #var_export($params);
+ $ext = $params['format'];
+ if ($params['format'] == 'html') $params['format'] = 'table';
+ if (!$params['db']) not_implemented();
+
+ $DBQ = new DbQ();
+ #$DBQ->params['format'] = $params['format'];
+ $DBQ->_nopage = 1;
+ #
+ # Page
+
+ $Page = page_init($params,$ext);
$Page->headers_no_cache();
$Page->begin();
#
# Db Init
+ #
global $DB_CONFS;
require_once($_SERVER['DOCUMENT_ROOT'].'/../../../lib/php/db/config.php');
$Db = new Db(['conf'=>$DB_CONFS]);
$Db->format = $params['format'];
+ $Db->format = $params['format'];
#
- # Db actions pre conn
- if ($params['db'] == 'help') {
+ # Root actions pre conn
+ #
+ $action = $params['db'];
+ if ($action == 'help') {
#$this->out->rows($format,$rows,$head);
+ if (0) $DBQ->page($DBQ->db(),[
+ [ 'help', 'This help' ],
+ [ 'ls', 'List configured databases' ],
+ [ 'databases', 'List others databases' ],
+ [ 'cryptkey', 'Generate a new encryption key' ],
+ ],['command','description'],function(&$r) use ($Page,$DBQ) {
+ $r['command'] = $Page->tag('a',$r['command'],'href="'.$r['command'].'.'.$DBQ->ext.'"');
+ #return;
+ });
$Db->out([
[ 'help', 'This help' ],
[ 'ls', 'List configured databases' ],
[ 'databases', 'List others databases' ],
[ 'cryptkey', 'Generate a new encryption key' ],
],['command','description'],
- ['row_parse_post'=>function(&$r) use ($Page,$params) {
- $r['command'] = $Page->tag('a',$r['command'],'href="'.$r['command'].'.'.$params['ext'].'"');
+ ['row_parse_post'=>function(&$r) use ($Page,$ext) {
+ $r['command'] = $Page->tag('a',$r['command'],'href="'.$r['command'].'.'.$ext.'"');
}]);
return $Page->end();
- } elseif ($params['db'] == 'ls') {
- $Db->out($Db->ls(),[],['row_parse_post'=>function(&$r) use ($Page,$params) {
- $r['id'] = $Page->tag('a',$r['id'],'href="'.$r['id'].'.'.$params['ext'].'"');
- }]);
- return $Page->end();
+ } elseif ($action == 'ls') {
+ $DBQ->page($DBQ->db(),'ls',[],function(&$r) use ($DBQ) {
+ $r['id'] = $DBQ->page->tag('a',$r['id'],'href="'.$r['id'].'.'.$DBQ->ext.'"');
+ });
+# NB 06.12.16 $Db->out($Db->ls(),[],['row_parse_post'=>function(&$r) use ($Page,$ext) {
+# NB 06.12.16 $r['id'] = $Page->tag('a',$r['id'],'href="'.$r['id'].'.'.$ext.'"');
+# NB 06.12.16 }]);
+# NB 06.12.16 return $Page->end();
+
+ } elseif ($action == 'cryptkey') {
+ $DBQ->page($DBQ->db(),'cryptkey',['key']);
+# NB 06.12.16 $Db->out($Db->cryptkey(),['key']);
+# NB 06.12.16 return $Page->end();
+
+ } elseif ($action == 'phpinfo' and DBQ_ADMIN) {
+ $DBQ->page($Page->phpinfo());
+# NB 06.12.16 echo $Page->phpinfo();
+# NB 06.12.16 return $Page->end();
- } elseif ($params['db'] == 'cryptkey') {
- $Db->out($Db->cryptkey(),['key']);
- return $Page->end();
}
#
# Connections post conn
+ #
if (empty($DB_CONFS[$params['db']])) return $Page->end();
$Db->__construct($DB_CONFS[$params['db']]);
$Db->connect();
+ $DBQ->db($DB_CONFS[$params['db']]);
#
# Db actions
- if ($params['table'] == 'help') {
+ #
+ $action = $params['table'];
+ if ($action == 'help') {
$Db->out([
[ 'help', 'This help' ],
[ 'status', 'Database status' ],
[ 'ls', 'List tables' ],
],['command','description'],
- ['row_parse_post'=>function(&$r) use ($Page,$params) {
- $r['command'] = $Page->tag('a',$r['command'],'href="'.$r['command'].'.'.$params['ext'].'"');
+ ['row_parse_post'=>function(&$r) use ($Page,$ext) {
+ $r['command'] = $Page->tag('a',$r['command'],'href="'.$r['command'].'.'.$ext.'"');
}]);
return $Page->end();
- } elseif ($params['table'] == 'ls') {
- $Db->out($Db->tables_rows(),[],['row_parse_post'=>function(&$r) use ($Page,$params) {
- $r['name'] = $Page->tag('a',$r['name'],'href="'.$params['db'].'/'.$r['name'].'.'.$params['ext'].'"');
+ } elseif ($action == 'ls') {
+ $Db->out($Db->tables_rows(),[],['row_parse_post'=>function(&$r) use ($Page,$params,$ext) {
+ $r['name'] = $Page->tag('a',$r['name'],'href="'.$params['db'].'/'.$r['name'].'.'.$ext.'"');
}]);
return $Page->end();
- } elseif ($params['table'] == 'status') {
+ } elseif ($action == 'status') {
$Db->out($Db->status());
return $Page->end();
- } elseif ($params['table'] == 'databases') {
+ } elseif ($action == 'databases') {
$Db->out($Db->databases());
return $Page->end();
#
# Table Init
+ #
$Table = $Db->table($params['table']);
+ #$DBQ->table = $Table;
if (!$Table) return $Page->end();
#
# Table action
+ #
if ($params['action'] == 'help') {
#$this->out->rows($format,$rows,$head);
$Db->out([
[ 'ls', 'List records' ],
[ 'fields', 'List fields' ],
],['command','description'],
- ['row_parse_post'=>function(&$r) use ($Page,$params) {
- $r['command'] = $Page->tag('a',$r['command'],'href="'.($r['command']=='ls'?'../'.$params['table']:$r['command']).'.'.$params['ext'].'"');
+ ['row_parse_post'=>function(&$r) use ($Page,$params,$ext) {
+ $r['command'] = $Page->tag('a',$r['command'],'href="'.($r['command']=='ls'?'../'.$params['table']:$r['command']).'.'.$ext.'"');
}]);
return $Page->end();
- } elseif ($params['action'] == 'ls') {
- /*
- $Table->row_parse_post = function(&$row) use ($Page,$params) {
- $row['name'] = $Page->tag('a',$row['name'],'href="'.$params['db'].'/'.$row['name'].'.'.$params['ext'].'"');
- };
- */
- $Table->rows();
- return $Page->end();
-
} elseif ($params['action'] == 'status') {
$Db->out($Table->status()+$Table->status(['fields']));
return $Page->end();
$Table->html_edit();
return $Page->end();
+ } elseif ($params['action'] == 'ls') {
+ /*
+ $Table->row_parse_post = function(&$row) use ($Page,$params,$ext) {
+ $row['name'] = $Page->tag('a',$row['name'],'href="'.$params['db'].'/'.$row['name'].'.'.$ext.'"');
+ };
+ */
+ $Table->rows();
+ return $Page->end();
+
}
return false;
function parse_uri($params=[]) {
$values = [];
- if (empty($_SERVER['REQUEST_URI'])) $_SERVER['REQUEST_URI'] = join('&',count($argv)>0 ? array_slice($argv,1) : []);
+ global $argv;
+ if (empty($_SERVER['REQUEST_URI'])) $_SERVER['REQUEST_URI'] = join('/',count($argv)>0 ? array_slice($argv,1) : []);
$path = preg_replace('/\?.*$/','',$_SERVER['REQUEST_URI']);
#var_export($values);
foreach ($params as $p => $default) {
if ($i>=$count) break;
- #echo(">>>>".$values[$i]."\n");
- #if ($values[$i]==='') continue;
if ($values[$i]!=='') {
if (!preg_match('/^[\w\._-]{2,100}$/',$values[$i])) not_implemented();
$params[$p] = $values[$i];
function title_nav($params,$ext) {
$title = array_filter(array_unique(array_slice(array_values($params),1)),function($v){ return($v=='ls' ? '' : $v); });
- array_unshift($title,'Home');
+ #array_unshift($title,Page::filename2title());
+ array_unshift($title,DBQ_TITLE);
+
$path = '/';
$nav = [];
+
$i = 0;
foreach ($title as $k=>$v) {
+
if ($i == 0) {
$nav[] = [$v,'/'];
+
} else {
$nav[] = [$v,"$path$v.".$ext];
$path .= "$v/";
+
}
+
$i++;
+
}
+
return [$title,$nav];
}
-# NB 05.12.16 function row_a(&$v) {
-# NB 05.12.16 $v = Page::tag('a',$r['command'],'href="'.$r['command'].'.'.$params['ext'].'"');
-# NB 05.12.16 }
+
dbq_run();
?>