]> git.nbdom.net Git - nb.git/commitdiff
dbq
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Thu, 8 Dec 2016 11:37:07 +0000 (11:37 +0000)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Thu, 8 Dec 2016 11:37:07 +0000 (11:37 +0000)
lib/php/db/table.php
www/dbq/dbq.php

index 33f72326b68fd5d212e9e4c7e5c79181e0108238..23da590a1da0bcac1a4e1194075835ee7b1d4fbe 100644 (file)
@@ -1013,7 +1013,7 @@ Class Table extends nb {
       echo '</div>'.NB_EOL;
     }
 
-    return $count;
+    #return $count;
   }
 
   /*-----------------------------------------------------------------
index a01401aeb30eb8721760a538f71f1395d8db0710..e1b70898415078e7326b8b816cb0d9f5c7460694 100644 (file)
@@ -1,9 +1,10 @@
 <?php
-if (!defined('DBQ_ADMIN')) define('DBQ_ADMIN',true);
-if (!defined('DBQ_TITLE')) define('DBQ_TITLE',basename(__FILE__,'.php'));
-
 require_once(dirname(__FILE__).'/../../lib/php/nb.php');
 class DbQ extends nb {
+  public $run = false;
+  public $title = 'Dbq';
+  public $admin = true;
+
   public $params = [
     'format' => 'human',
     'db' => 'ls',
@@ -14,9 +15,11 @@ class DbQ extends nb {
   public $colors = [
     'text' => '#ddd',
     'background' => '#009B9C',
-    #'button' => '#444',
-    'button' => '#ddd',
-    'button-backgound' => '#3C3C3B',
+
+    #'button' => '#ddd',
+    #'button-backgound' => '#3C3C3B',
+    'button' => '#444',
+    'button-backgound' => '#aaa',
   ];
 
   public $css = '/default.css';
@@ -40,17 +43,14 @@ class DbQ extends nb {
     ;
 
     parent::__construct($opt);
-    if (!$this->params['db']) not_implemented();
+    if (!$this->params['db']) $this->not_implemented();
 
-    if ($this->params['format'] == 'html') $this->params['format'] = 'table';
     if (empty($this->ext)) $this->ext = $this->params['format'];
+    if ($this->params['format'] == 'html') $this->params['format'] = 'table';
 
-  }
-
-  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);
+    list($title,$nav) = $this->title_nav();
 
     $this->page = new Page([
       'css' => $this->css,
@@ -58,6 +58,11 @@ class DbQ extends nb {
       'nav' => $nav,
     ]);
 
+    if ($this->run) $this->run();
+  }
+
+  public function page($obj,$meth=null,$head=[],$fct=null) {
+
     if (!empty($this->colors)) $this->page->css_code = trim('
 body, .button {
   color: '.$this->colors['text'].';
@@ -90,7 +95,9 @@ if (empty($this->_nopage)) {
 
     #die($obj);
     #if (is_scalar($obj)) {
-    if (empty($meth)) {
+    if(is_array($obj)) {
+        $this->db->out($obj,$head,['row_parse_post'=>$fct]);
+    } elseif (empty($meth)) {
       echo $obj;
     } elseif(is_array($meth)) {
         $this->db->out($meth,$head,['row_parse_post'=>$fct]);
@@ -109,6 +116,7 @@ if (empty($this->_nopage)) {
 
     if (!empty($connect)) {
       if (!isset($this->db)) $this->db = $this->db();
+      if (!is_array($connect) and $connect === true) $connect = $this->db->conf[$this->params['db']];
       $this->db->__construct($connect);
       $this->db->connect();
       return $this->db;
@@ -121,16 +129,15 @@ if (empty($this->_nopage)) {
     return $this->db;
   }
 
-  public function table($obj,$meth,$head=[],$fct=null) {
-    $this->table = $this->db->table($this->params['table']);
+  public function table() {
+    $this->table = $this->db->table($this->params['table'],['db'=>$this->db]);
   }
 
   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);
+    if (!empty($this->title)) array_unshift($title,$this->title);
 
     $path = '/';
     $nav = [];
@@ -177,7 +184,7 @@ if (empty($this->_nopage)) {
     foreach ($this->params as $p => $default) {
       if ($i>=$count) break;
       if ($values[$i]!=='') {
-        if (!preg_match('/^[\w\._-]{2,100}$/',$values[$i])) not_implemented();
+        if (!preg_match('/^[\w\._-]{2,100}$/',$values[$i])) $this->not_implemented();
         $this->params[$p] = $values[$i];
       }
       $i++;
@@ -186,295 +193,117 @@ if (empty($this->_nopage)) {
     return $this->params;
   }
 
-
-}
-
-function page_init($params,$ext) {
-  $colors = [
-    'text' => '#ddd',
-    'background' => '#009B9C',
-    #'button' => '#444',
-    'button' => '#ddd',
-    'button-backgound' => '#3C3C3B',
-  ];
-
-  list($title,$nav) = title_nav($params,$ext);
-  $Page = new Page([
-    'css' => '/default.css',
-    'title' => join(' ',$title),
-    'nav' => $nav,
-  ]);
-
-  if (!empty($colors)) $Page->css_code = trim('
-body, .button {
-  color: '.$colors['text'].';
-  background-color: '.$colors['background'].';
-}
-.button {
-  color: '.$colors['button'].';
-  border-color: '.$colors['button'].';
-  background-color: '.$colors['button-backgound'].';
-}
-table.rows,
-div.rows,
-.menu,
-.block,
-form.edit,
-object, iframe, pre
-{
-  border-color: '.$colors['text'].';
-}
-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'];
-
-  #
-  # 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,$ext) {
-      $r['command'] = $Page->tag('a',$r['command'],'href="'.$r['command'].'.'.$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();
-
-  }
-
-  #
-  # 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
-  #
-  $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,$ext) {
-      $r['command'] = $Page->tag('a',$r['command'],'href="'.$r['command'].'.'.$ext.'"');
-    }]);
-    return $Page->end();
-
-  } 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 ($action == 'status') {
-    $Db->out($Db->status());
-    return $Page->end();
-
-  } elseif ($action == 'databases') {
-    $Db->out($Db->databases());
-    return $Page->end();
-
+  public function not_implemented() {
+    #return http_response_code(501);
+    if (empty($_SERVER['SERVER_PROTOCOL'])) $_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.0';
+    $msg = '501 Not Implemented';
+    header($_SERVER['SERVER_PROTOCOL'].' '.$msg);
+    echo "$msg\n";
+    exit;
   }
 
-  #
-  # 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([
-      [ 'help', 'This help' ],
-      [ 'ls', 'List records' ],
-      [ 'fields', 'List fields' ],
-    ],['command','description'],
-    ['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'] == 'status') {
-    $Db->out($Table->status()+$Table->status(['fields']));
-    return $Page->end();
-
-  } elseif ($params['action'] == 'fields') {
-    $Table->out($Table->fields_rows(),[],['row_parse_post'=>function(&$r) use ($Page,$params) {
-    }]);
-    return $Page->end();
-
-  } elseif ($params['action'] == 'edit') {
-    $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();
+  public function run() {
 
-  }
+    #$DBQ = new DbQ();
 
-  return false;
-}
+    #
+    # Db Init
+    #
+    $this->db();
 
-function parse_uri($params=[]) {
-  $values = [];
+    #
+    # Root actions pre conn
+    #
+    $action = $this->params['db'];
+    if ($action == 'help') {
+      $this->page($this->db,[
+        [ 'help', 'This help' ],
+        [ 'ls', 'List configured databases' ],
+        [ 'databases', 'List others databases' ],
+        [ 'cryptkey', 'Generate a new encryption key' ],
+      ],['command','description'],function(&$r) {
+        $r['command'] = $this->page->tag('a',$r['command'],'href="'.$r['command'].'.'.$this->ext.'"');
+      });
 
-  global $argv;
-  if (empty($_SERVER['REQUEST_URI'])) $_SERVER['REQUEST_URI'] = join('/',count($argv)>0 ? array_slice($argv,1) : []);
+    } elseif ($action == 'ls') {
+      $this->page($this->db,'ls',[],function(&$r){
+        $r['id'] = $this->page->tag('a',$r['id'],'href="'.$r['id'].'.'.$this->ext.'"');
+      });
 
-  $path = preg_replace('/\?.*$/','',$_SERVER['REQUEST_URI']);
+    } elseif ($action == 'cryptkey') {
+      $this->page($this->db,'cryptkey',['key']);
 
-  if (preg_match('/\.(\w+)$/',$path,$m)) {
-    #$path = preg_replace('/\.\w+$/','',$path);
-    $values[] = $m[1];
-    $path = substr($path,0,strlen($path)-strlen($m[1])-1);
-  } else {
-    $values[] = '';
-  }
+    } elseif ($action == 'phpinfo' and $this->admin) {
+      $this->page($this->page->phpinfo());
 
-  $path = trim($path,'/');
-  if ($path) $values = array_merge($values,explode('/',$path));
-
-  $i=0;
-  $count = count($values);
-  #var_export($values);
-  foreach ($params as $p => $default) {
-    if ($i>=$count) break;
-    if ($values[$i]!=='') {
-      if (!preg_match('/^[\w\._-]{2,100}$/',$values[$i])) not_implemented();
-      $params[$p] = $values[$i];
     }
-    $i++;
-  }
-
-  return $params;
-}
 
-function not_implemented() {
-  #return http_response_code(501);
-  if (empty($_SERVER['SERVER_PROTOCOL'])) $_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.0';
-  $msg = '501 Not Implemented';
-  header($_SERVER['SERVER_PROTOCOL'].' '.$msg);
-  echo "$msg\n";
-  exit;
-}
-
-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,Page::filename2title());
-  array_unshift($title,DBQ_TITLE);
-
-  $path = '/';
-  $nav = [];
-
-  $i = 0;
-  foreach ($title as $k=>$v) {
+    #
+    # Connections
+    #
+    $this->db(true);
+
+    #
+    # Db actions
+    #
+    $action = $this->params['table'];
+    if ($action == 'help') {
+      $this->page($this->db,[
+        [ 'help', 'This help' ],
+        [ 'status', 'Database status' ],
+        [ 'ls', 'List tables' ],
+      ],['command','description'],function(&$r){
+        $r['command'] = $this->page->tag('a',$r['command'],'href="'.$r['command'].'.'.$this->ext.'"');
+      });
+
+    } elseif ($action == 'ls') {
+      $this->page('',$this->db->tables_rows(),[],function(&$r){
+        $r['name'] = $this->page->tag('a',$r['name'],'href="'.$this->params['db'].'/'.$r['name'].'.'.$this->ext.'"');
+      });
+
+    } elseif ($action == 'status') {
+      $this->page($this->db,'status');
+
+    } elseif ($action == 'databases') {
+      $this->page($this->db,'databases');
 
-    if ($i == 0) {
-      $nav[] = [$v,'/'];
+    }
 
-    } else {
-      $nav[] = [$v,"$path$v.".$ext];
-      $path .= "$v/";
+    #
+    # Table Init
+    #
+    $this->table();
+    
+    #
+    # Table action
+    #
+    if ($this->params['action'] == 'help') {
+      $this->page($this->db(),[
+        [ 'help', 'This help' ],
+        [ 'ls', 'List records' ],
+        [ 'fields', 'List fields' ],
+      ],['command','description'],function(&$r){
+        $r['command'] = $this->page->tag('a',$r['command'],'href="'.$r['command'].'.'.$this->ext.'"');
+      });
+
+    } elseif ($this->params['action'] == 'status') {
+      $this->page($this->table->status()+$this->table->status(['fields']));
+
+    } elseif ($this->params['action'] == 'fields') {
+      $this->page($this->table,'fields_rows');
+
+    } elseif ($this->params['action'] == 'edit') {
+      $this->page($this->table,'html_edit');
+
+    } elseif ($this->params['action'] == 'ls') {
+      $this->page($this->table,'rows');
 
     }
 
-    $i++;
-
+    return false;
   }
 
-  return [$title,$nav];
 }
 
-dbq_run();
+$DBQ = new DbQ(['run'=>true]);
 ?>