]> git.nbdom.net Git - nb.git/commitdiff
Bed
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Tue, 6 Dec 2016 01:05:13 +0000 (01:05 +0000)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Tue, 6 Dec 2016 01:05:13 +0000 (01:05 +0000)
www/dbq/dbq.php [new file with mode: 0644]

diff --git a/www/dbq/dbq.php b/www/dbq/dbq.php
new file mode 100644 (file)
index 0000000..0952331
--- /dev/null
@@ -0,0 +1,200 @@
+<?php
+
+function dbq_run() {
+
+  #
+  # 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' => 'help',
+    'table' => 'ls',
+    'action' => 'ls',
+  ]);
+  #var_export($params);
+  $ext = $params['format'];
+  if ($params['format'] == 'html') $params['format'] = 'table';
+
+  if (!$params['db']) not_implemented();
+
+  #
+  # Page
+  if (empty($_SERVER['DOCUMENT_ROOT'])) $_SERVER['DOCUMENT_ROOT'] = dirname(__FILE__).'/html';
+  require_once($_SERVER['DOCUMENT_ROOT'].'/../../../lib/php/page.php');
+  #Page::pdef('out',1);
+  $Page = new Page([
+    'css' => '/default.css',
+    'title' => join(' ',array_unique(array_slice(array_values($params),1))),
+  ]);
+  $params['ext'] = $ext;
+
+  $Page->content_type($Page->ext2mime($params['format']) ? $Page->ext2mime($params['format']) : 'text/plain');
+  #$Page->content_type('text/plain');
+  if ($Page->is('text') and !$Page->is('html')) $Page->content_type('text/plain');
+  #$Page->title = array_values($params)[1];
+  $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 actions pre conn
+  if ($params['db'] == 'help') {
+    #$this->out->rows($format,$rows,$head);
+    $Db->out([
+      [ 'help', 'This help' ],
+      [ 'ls', 'List configured databases' ],
+      [ 'status', 'Database status' ],
+      [ 'databases', 'List others databases' ],
+    ],['command','description'],
+    ['row_parse_post'=>function(&$r) use ($Page,$params) {
+      $r['command'] = $Page->tag('a',$r['command'],'href="'.$r['command'].'.'.$params['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();
+
+  }
+
+  #
+  # Connections post conn
+  if (empty($DB_CONFS[$params['db']])) return $Page->end();
+  $Db->__construct($DB_CONFS[$params['db']]);
+  $Db->connect();
+
+  #
+  # Db actions
+  if ($params['table'] == 'help') {
+    #$this->out->rows($format,$rows,$head);
+    $Db->out([
+      [ 'help', 'This help' ],
+      [ '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'].'"');
+    }]);
+    return $Page->end();
+
+  } elseif ($params['table'] == 'ls') {
+    /*
+    bye('zaza');
+    $Table->row_parse_post = function(&$row) use($Page) {
+      debug($row);
+      $row['name'] = '';
+        #$Page->tag('a',$row['name']);
+    };
+    */
+    #$Db->action('db.tables');
+    $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'].'"');
+    }]);
+    return $Page->end();
+
+  } elseif ($params['table'] == 'status') {
+    $Db->out($Db->status());
+    return $Page->end();
+
+  } elseif ($params['table'] == 'databases') {
+    $Db->out($Db->databases());
+    return $Page->end();
+
+  }
+
+  #
+  # Table Init
+  $Table = $Db->table($params['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) {
+      $r['command'] = $Page->tag('a',$r['command'],'href="'.$r['command'].'.'.$params['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();
+
+  } elseif ($params['action'] == 'fields') {
+    $Table->out($Table->fields_rows(),[],['row_parse_post'=>function(&$r) use ($Page,$params) {
+    }]);
+    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) : []);
+
+  $path = preg_replace('/\?.*$/','',$_SERVER['REQUEST_URI']);
+
+  if (preg_match('/\.(\w+)$/',$_SERVER['REQUEST_URI'],$m)) {
+    #$path = preg_replace('/\.\w+$/','',$_SERVER['REQUEST_URI']);
+    $values[] = $m[1];
+    $path = substr($_SERVER['REQUEST_URI'],0,strlen($_SERVER['REQUEST_URI'])-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 ($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];
+    }
+    $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;
+}
+# 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();
+?>