]> git.nbdom.net Git - nb.git/commitdiff
clean up
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Tue, 27 Dec 2016 22:59:09 +0000 (22:59 +0000)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Tue, 27 Dec 2016 22:59:09 +0000 (22:59 +0000)
12 files changed:
lib/js/nb.js
lib/php/db/index.php
lib/php/db/init.php
lib/php/db/page.php
lib/php/db/table.php
lib/php/db/types/sqlite.php
lib/php/default.php.TODEL [deleted file]
lib/php/functions.php
lib/php/page.php
www/dbq/dbq.php
www/dbq/html/default.css
www/dbq/html/default.js

index 748738fcba1914bfa4a7bd33ac9bdca0f976594a..7c413c5f0de74bd2ad456e53a2188b889959c887 100644 (file)
@@ -239,6 +239,7 @@ function NB(args) {
     //document.location = url;
 
     //if (url == window.location) return false;
+    f.reset();
     window.location = url;
     return false;
   }
index ff9f1aa332711b86a45a66f56da950e2377d7e73..3617593308eff8b2e980e168463ebcc57e0e3230 100755 (executable)
@@ -6,7 +6,6 @@
 *
 * lib/php/db/index.php
 */
-#require_once(realpath(dirname(__FILE__).'/page.php')); return;
 require_once(realpath(dirname(__FILE__).'/init.php'));
 if(defined('DB_NO_ACTION') and DB_NO_ACTION) return true;
 if (!isset($Db)) global $Db, $Table;
index f4279ab42bf01e26246a2a4bddb98c86a1dbeca1..2168896783faa194915776727f696cf25927a5c0 100644 (file)
@@ -1,8 +1,4 @@
 <?php
-# NB 04.12.16 require_once(realpath(dirname(__FILE__).'/../config.php'));
-# NB 04.12.16 require_once(realpath(dirname(__FILE__).'/../db.php'));
-
 require_once(realpath(dirname(__FILE__).'/config.php'));
-
 Db::init($DB_CONFS);
 ?>
index 1baa64e609408e5745259a42600219cbc586791f..2f69c69ed1589cb7c4252f62933dd9f85f6c8599 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 require_once(dirname(__FILE__).'/init.php');
-require_once(dirname(__FILE__).'/../page.php');
+require_once(NB_ROOT.'/lib/php/page.php');
 
 $Page = new Page([
   'title' => ($Db->title ? $Db->title : Db::prettyText($Db->name)),
index 146831153cdcf3be82ea559f449625f6dcf3eeaf..9ab8f8494b579b352be8114775442df7a5e697dd 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 require_once(realpath(dirname(__FILE__).'/../db.php'));
-require_once(realpath(dirname(__FILE__).'/../db/field.php'));
-require_once(realpath(dirname(__FILE__).'/../out.php'));
+require_once(realpath(NB_ROOT.'/lib/php/db/field.php'));
+require_once(realpath(NB_ROOT.'/lib/php/out.php'));
 $DB_TABLE_QUERY_ID = 0;
 function __table_define() {
   define('TABLE_INDENT',NB_EOL ? "\t" : "");
@@ -123,12 +123,12 @@ Class Table extends nb {
 
     // Name, could be a select
     if (DB_TABLE_QUERY_NAME and stripos($this->name,'SELECT ')===0) {
-      #$GLOBALS['DB_TABLE_QUERY_ID']++; $name = DB_TABLE_QUERY_NAME . $GLOBALS['DB_TABLE_QUERY_ID'];
       $this->db()->conn->query("CREATE TEMPORARY TABLE ".DB_TABLE_QUERY_NAME." AS $this->name");
       $this->name = DB_TABLE_QUERY_NAME;
 
     // Virtual Table
     } elseif (DB_TABLE_QUERY_NAME and !empty($this->sql)) {
+      if ($this->name) $this->name = DB_TABLE_QUERY_NAME;
       $this->db()->conn->query("CREATE TEMPORARY TABLE $this->name AS $this->sql");
 
     } elseif (preg_match('/\b(\.import|LOAD DATA|COPY|INSERT|REPLACE|DELETE|TRUNCATE|CREATE|DROP|ALERT)\b/',$this->name)) {
@@ -1179,7 +1179,7 @@ Class Table extends nb {
     Wordpress
   -----------------------------------------------------------------*/
   public function rows_begin_wp() {
-    require_once(realpath(dirname(__FILE__).'/wp.php'));
+    require_once(NB_ROOT.'/lib/php/db/wp.php'));
     $this->_html_table = new html_table($this);
     return '';
   }
index 8825db23f0dc8b2ec5ce53f1361addd2b0e8673a..12b2e27180498141c71a68741079bf7c3203637c 100644 (file)
@@ -89,15 +89,19 @@ $DB_TYPES['sqlite'] = array (
 
 'table.fields' => array (
   'sql' => 'PRAGMA table_info(\'<T.NAME>\')',
-  'fct' => create_function('&$r',join('',array(
-    #'debug("zaza");',
-    '$r["key"] = $r["pk"] == "0" ? 0 : 1;',
-    'if (!empty($r["dflt_value"])) $r["default"] = trim($r["dflt_value"],"\'");',
-    '$r["null"] = $r["notnull"] == "0" ? 1 : 0;',
-    '$r["autoincrement"] = preg_match("/[\(,]\s*".$r["name"]." [^\),]+ AUTOINCREMENT/",$r["this"]->sql()) ? 1 : 0;',
-    '$r["extra"] = preg_match("/[\(,]\s*".$r["name"]." \S+ (COLLATE[^,)]+)/",$r["this"]->sql(),$m) ? trim($m[1]) : "";',
-    #'debug($m);'
-  ))),
+  'fct' => function(&$r) {
+    $r["key"] = $r["pk"] == "0" ? 0 : 1;
+
+    if (!empty($r["dflt_value"])) $r["default"] = trim($r["dflt_value"],"'");
+
+    $r["null"] = $r["notnull"] == "0" ? 1 : 0;
+
+    $sql = $r["this"]->sql();
+    $name_exp = preg_quote($r["name"]);
+    $r["autoincrement"] = preg_match("/[\(,]\s*".$name_exp." [^\),]+ AUTOINCREMENT/",$sql) ? 1 : 0;
+
+    $r["extra"] = preg_match("/[\(,]\s*".$name_exp." \S+ (COLLATE[^,)]+)/",$sql,$m) ? trim($m[1]) : "";
+  },
 ),
 
 'field.create' => function(&$field) {
diff --git a/lib/php/default.php.TODEL b/lib/php/default.php.TODEL
deleted file mode 100644 (file)
index 4f30e1c..0000000
+++ /dev/null
@@ -1,7 +0,0 @@
-<?php
-if (!defined('DEBUG')) define('DEBUG',(int)@$_REQUEST['debug']);
-ini_set('include_path', realpath(dirname(__FILE__)).':'.ini_get('include_path'));
-
-require_once((dirname(__FILE__).'/functions.php'));
-require_once((dirname(__FILE__).'/page.php'));
-?>
index b23024811f756e46635bd7b2d3e22b101da78213..d63ecbaa10ced0b150d0bb758518c5476eedabba 100644 (file)
@@ -54,7 +54,7 @@ function txt2md($txt) {
 # NB 28.11.16: TODO rename as nb::md2html 
   global $_txt2md;
   if ($_txt2md === null) {
-    require_once(dirname(__FILE__).'/parsedown/Parsedown.php');
+    require_once(NB_ROOT.'/lib/php/parsedown/Parsedown.php');
     $_txt2md = new Parsedown();
   }
   return $_txt2md->text($txt);
index 26113fe701a755131a27de226ba02b7dcdab81a0..188527a9b16a214eee5bd1021a80e246f4ef258a 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 require_once(dirname(__FILE__).'/nb.php');
-require_once(dirname(__FILE__).'/out.php');
+require_once(NB_ROOT.'/lib/php/out.php');
 /*
 if (
   !@$_SERVER['DOCUMENT_ROOT'] and (realpath($argv[0]) == __FILE__)
index 1fc658d5b7b7c13d1f7cad6ae7dcbe5167a643e6..3ecdaf7648436c9f97b3a5aac0fba2dd5196d2c2 100644 (file)
@@ -1,5 +1,5 @@
 <?php
-define(PRODUCTION,empty($_SERVER['PRODUCTION']) ? false : (bool)$_SERVER['PRODUCTION']);
+define('PRODUCTION',empty($_SERVER) or empty($_SERVER['PRODUCTION']) ? false : (bool)$_SERVER['PRODUCTION']);
 
 require_once(realpath(dirname(__FILE__).'/../../lib/php/nb.php'));
 require_once(NB_ROOT.'/lib/php/http.php');
@@ -7,42 +7,42 @@ require_once(NB_ROOT.'/lib/php/mime.php');
 
 class DbQ extends nb {
   
-  public $perm = 9; // Minimum permission access 0 = readonly
   const ADMIN = 9;
   const DELETE = 4;
   const WRITE = 3;
   const VIEW = 1;
+  public $perm = self::ADMIN;
 
   public $title = 'Dbq';
   public $sep_title = ' / ';
 
-  public $format_html = 'table';
   public $format_cli = 'human';
-  public $format_cli_ua_exp = '/^\S+\s+.Windows|iPhone|Android|Macintosh|Linux/';
-  public $limits = [];
-  # to overwrite Db web list
-  public $formats = [ 'html','csv','xml','json','yaml','sh','sql','php' ];
+  public $format_html = 'table';
+  public $format_html_ua_exp = '/^\S+\s+.Windows|iPhone|Android|Macintosh|Linux/';
 
+  public $uri;
+  public $uri_params;
   public $param_args_sep = '|';
   public $param_exp_value = '[\w\._:-]{2,100}';
   const PARAM_DB_DEFAULT = 'ls';
-  #public $action = 'ls';
   public $params = [
     'format' => '',
-    'db' => self::PARAM_DB_DEFAULT,
-    'table' => self::PARAM_DB_DEFAULT,
-    'action' => self::PARAM_DB_DEFAULT,
+    'db' => '',
+    'table' => '',
+    'action' => '',
     'args' => '',
   ];
 # NB 23.12.16   public $params_deep = [];
 
+  # Db
+  public $limits = [];
+  public $formats = [ 'html','csv','xml','json','yaml','sh','sql','php' ];
+
+  # Page
   public $css = '/default'.(PRODUCTION ? '.min' : '').'.css';
   public $js = '/default'.(PRODUCTION ? '.min' : '').'.js';
   public $ext;
 
-  public $uri;
-  public $uri_params;
-
   # Object
   public $page;
   public $table;
@@ -58,11 +58,12 @@ class DbQ extends nb {
       'title',
       'css',
       'js',
-      'format_cli_ua_exp'
+      'format_html_ua_exp'
     ] as $k) {
       $env = 'DBQ_'.strtoupper($k);
       if (!empty($_SERVER[$env])) $this->$k = $_SERVER[$env];
     }
+
     // Envs -> params
     foreach ([
       'format',
@@ -72,8 +73,6 @@ class DbQ extends nb {
       $env = 'DBQ_PARAM_'.strtoupper($k);
       if (!empty($_SERVER[$env])) $this->params[$k] = $_SERVER[$env];
     }
-    /*
-    */
 
     //
     // Init
@@ -82,7 +81,6 @@ class DbQ extends nb {
     unset($opt['run']);
 
     parent::__construct($opt);
-    if (!$this->params['db']) $this->not_implemented('Db missing');
 
     //
     // Page
@@ -173,6 +171,7 @@ class DbQ extends nb {
     // Connect
     if (!empty($connect)) {
 
+      if (!$this->params['db']) $this->not_implemented('Params db missing');
       if (!isset($this->db)) $this->db = $this->db();
 
       if (!is_array($connect)
@@ -182,7 +181,7 @@ class DbQ extends nb {
         $connect = $this->db->conf[$this->params['db']];
       }
 
-      if (empty($connect['name'])) $this->not_found('Unknown database');
+      if (empty($connect['name'])) $this->not_found('Unknown database: '.$this->params['db']);
 
       $this->db->__construct($connect);
       $this->id = $this->params['db'];
@@ -276,9 +275,14 @@ class DbQ extends nb {
 
 
   public function table() {
+
     if (!in_array($this->params['table'],array_keys($this->db->tables()))) {
-      #$this->bye($this->db->tables());
-      $this->not_found('Unknown table');
+
+      # We Allow SELECT only in admin
+      if ($this->perm < self::ADMIN or !preg_match('/^SELECT /',$this->params['table'])) {
+        $this->not_found('Unknown table: '.$this->params['table']);
+      }
+
     }
 
     $this->table = $this->db->table($this->params['table'],[
@@ -332,6 +336,9 @@ class DbQ extends nb {
   }
 
   public function uri_params() {
+    #
+    # Parse path, respect params order
+    #
     $values = [];
     $path = '';
     $args = '';
@@ -499,7 +506,7 @@ class DbQ extends nb {
 
     } else {
       $format = 
-        (!empty($_SERVER['HTTP_USER_AGENT']) and preg_match($this->format_cli_ua_exp,$_SERVER['HTTP_USER_AGENT']))
+        (!empty($_SERVER['HTTP_USER_AGENT']) and preg_match($this->format_html_ua_exp,$_SERVER['HTTP_USER_AGENT']))
           ? 'html'
           : $this->format_cli
       ;
@@ -570,18 +577,9 @@ class DbQ extends nb {
     return $this->page($this->page->phpinfo($txt));
   }
 
-  public function run() {
-
-    #
-    # Db Init
-    #
-    $this->db();
-    $this->init();
-
-    #
-    # Actions pre Db
-    #
+  public function run_root() {
     $action = $this->params['db'];
+    if (!$action and self::PARAM_DB_DEFAULT) $action = self::PARAM_DB_DEFAULT;
     #$this->params = [ 'action' => $action ];
 
     if ($action == 'help') {
@@ -633,18 +631,13 @@ class DbQ extends nb {
       $this->page($rows);
 
     }
+  }
 
-    #
-    # Db Connections
-    #
-    $this->params['db'] = $action;
-    $this->db(true);
-
-    #
-    # Db actions
-    #
+  public function run_db() {
+    $this->db(true); # Db Connections
     $action = $this->params['table'];
-    $this->params['table'] = '';
+    if (!$action and self::PARAM_DB_DEFAULT) $action = self::PARAM_DB_DEFAULT;
+# NB 27.12.16     $this->params['table'] = '';
 
     if ($action == 'help') {
       $this->page($this->db,[
@@ -678,16 +671,13 @@ class DbQ extends nb {
 
     }
 
-    #
-    # Table Init
-    #
-    $this->params['table'] = $action;
-    $this->table();
-    
-    #
-    # Table action
-    #
+  }
+
+  public function run_table() {
+    $this->table(); # Table init
     $action = $this->params['action'];
+    if (!$action and self::PARAM_DB_DEFAULT) $action = self::PARAM_DB_DEFAULT;
+
 #bye([$this->params,$action]);
     if ($action == 'help') {
       $this->page($this->table,[
@@ -737,6 +727,30 @@ class DbQ extends nb {
       $this->not_implemented('Unknown table action'." `$action`");
 
     }
+  }
+
+  public function run() {
+
+    #
+    # Db Init
+    #
+    $this->db();
+    $this->init();
+
+    #
+    # Root Actions
+    #
+    $this->run_root();
+
+    #
+    # Db actions
+    #
+    $this->run_db();
+    
+    #
+    # Table action
+    #
+    $this->run_table();
 
     return true;
   }
index b796c96f725760fa2c45828f1be8c89383dceb1c..91f87f4e0651c682b2cd236fead8d9603e1cf25b 100644 (file)
@@ -36,6 +36,9 @@ li {
 
 table { border-collapse: collapse; }
 td, th { padding: 0.2em 0.7em; }
+/*
+td form { display: inline-block; }
+*/
 th a.sort { padding: 0 0.3em; }
 
 textarea,
index 00e38ec188d67b0a675709252dc7be1c3dfefe22..0f70bcb0275d982e381b23137ef070b320d170de 100644 (file)
@@ -42,6 +42,7 @@ function form_submit_clean(f) {
 
   url = (action == "?" ? "" : action) + url;
 
+  f.reset();
   window.location = url;
   return false;
 }