]> git.nbdom.net Git - nb.git/commitdiff
Bed
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Wed, 7 Dec 2016 00:07:37 +0000 (00:07 +0000)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Wed, 7 Dec 2016 00:07:37 +0000 (00:07 +0000)
lib/php/page.php
www/dbq/dbq.php
www/dbq/html/default.css

index 305cd65cf28b1ef8aff06440c4034d2e0f3466ae..a507719f742ea12ac4048b4bbf2a0ddba1b1c969 100644 (file)
@@ -115,13 +115,12 @@ class Page extends nb {
     return $title;
   }
 
-  public function filename2title() {
+  public static function filename2title() {
     $str = '';
-    #$this->title = $GLOBALS['argv'][0]
     if (isset($_SERVER['REQUEST_URI'])) $str = preg_replace('/\?.*$/','',preg_replace(',^.*?([^/]+)(\/?.*)?$,','\1',$_SERVER['REQUEST_URI']));
     if ($str == '' or $str == '/') $str = preg_replace('@^.*?([^/\.]+)(/index)?(\..*?)$@','\1',$_SERVER['SCRIPT_NAME']);
     $str = preg_replace('@[\'_-]+$@',' ',$str);
-    if ($str == 'index') return 'Home';
+    if ($str == 'index') return;
     return mb_strtoupper(mb_substr($str,0,1)).mb_strtolower(mb_substr($str,1,mb_strlen($str)));
   }
 
@@ -476,6 +475,8 @@ class Page extends nb {
 
   public static function nav_parse($array) {
     #$v = &$sep;
+    $title = [];
+    $nav = [];
     $hash = [];
     foreach ($array as $k => $v) {
       if ($v === '' or $v === null or empty($v[0])) {
@@ -528,6 +529,7 @@ class Page extends nb {
     # Pretty h2
     $phpinfo =  preg_replace_callback ('@(<h2>(?:<[^>]+>)?)([^<]+)@',function($m){return $m[1].prettyText($m[2]);},$phpinfo);
 
+    if (self::php_cli()) return $phpinfo;
     if (!preg_match_all('@(<h2.*?</table>)@s',$phpinfo,$m)) return '';
     return join("\n",$m[0]);
   }
index e9bb5b1f3214b1197a4418e3970d22180246e7d6..a01401aeb30eb8721760a538f71f1395d8db0710 100644 (file)
 <?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),
@@ -44,12 +211,14 @@ body, .button {
   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
 {
@@ -57,74 +226,132 @@ 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();
 
@@ -132,11 +359,14 @@ table.rows th, table.rows td { border-color: '.$colors['text'].'; }
 
   #
   # 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([
@@ -144,20 +374,11 @@ table.rows th, table.rows td { border-color: '.$colors['text'].'; }
       [ '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();
@@ -171,6 +392,15 @@ table.rows th, table.rows td { border-color: '.$colors['text'].'; }
     $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;
@@ -179,7 +409,8 @@ table.rows th, table.rows td { border-color: '.$colors['text'].'; }
 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']);
 
@@ -199,8 +430,6 @@ function parse_uri($params=[]) {
   #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];
@@ -222,23 +451,30 @@ function not_implemented() {
 
 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();
 ?>
index 488b9b449a60f11ab1e9fb3d77569da812e83441..01caf084397984c003bb4bbb9da088fd315feca3 100644 (file)
@@ -65,6 +65,7 @@ table.rows,
 div.rows,
 form.edit,
 .menu,
+.block,
 object, iframe, pre
 {
   border-radius: 2px;