]> git.nbdom.net Git - nb.git/commitdiff
nb.rent no more default table
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Fri, 2 Dec 2016 00:55:38 +0000 (00:55 +0000)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Fri, 2 Dec 2016 00:55:38 +0000 (00:55 +0000)
12 files changed:
.gitignore [new file with mode: 0644]
etc/dbq/zzz-all.php
etc/profile.d/docker.sh
lib/php/config.php
lib/php/db.php
lib/php/db/config.php
lib/php/db/table.php
lib/php/nb.php
lib/php/out.php
share/templates/hosts.php [new file with mode: 0644]
share/templates/my.cnf.php
share/templates/pgpass.php [new file with mode: 0644]

diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..efae21a
--- /dev/null
@@ -0,0 +1,4 @@
+*.nogit
+*.pyc
+.netrwhist
+*.swp
index abd47f35a5089a4671e16881e42dde61a3de3391..fce91a2f02a65436f7d1174c0a75d9d921052aeb 100644 (file)
@@ -82,6 +82,9 @@ foreach ($DBQ as $id => $db) {
 # NB 21.11.16   conf_merge($DBQ[$fname],$DBQ['all']);
 
 }
+#if (isset($DBQ['nb']) and isset($DBQ['nb']['default_table']))
+  #$DBQ['all']['default_table'] = $DBQ['nb']['default_table']
+#;
 
 #debug($DBQ['nb-sqlite']['default_table']);
 #if (!empty($DBQ['nb-sqlite'])) debug($DBQ['nb-sqlite']);
index 4af53cb37265bd52140acde5df188686647916ac..3cf724991364e3255be042b2ab8e44903bd016f5 100644 (file)
@@ -156,7 +156,8 @@ docker_clean() {
   local volumes=$(docker volume ls -qf dangling=true 2>/dev/null)
   [ -n "$volumes" ] && docker volume rm $volumes | sed "s/^/VOLUMES /"
 
-  local containers=$(docker ps -f status=dead -f status=exited -f status=created -aq)
+# NB 01.12.16   local containers=$(docker ps -f status=dead -f status=exited -f status=created -aq)
+  local containers=$(docker ps -f status=dead -aq)
   [ -n "$containers" ] && docker rm -vf $containers | sed "s/^/CONTAINERS /"
 }
 
index 5e28a57015ddffecbd875b03f1467e844db014e1..570d834b15990cde7d5f64778ad6beaa7b7e180a 100644 (file)
@@ -7,11 +7,10 @@
 */
 #define('NB_EOL','');
 #define('NB_PROD',true);
-require(dirname(__FILE__).'/nb.php');
-
 if (empty($_SERVER['PROD'])) error_reporting(E_ALL | E_STRICT | E_NOTICE);
 
-ini_set('error_reporting', true);
+require(dirname(__FILE__).'/nb.php');
+
 // Cli
 if (nb::php_cli()) {
   argv2request();
index f13b6f496c8d15fbf6a082af9071623e0d024329..9f1f6ae52ef450fbedd15b1a168ea1119ce1dd80 100644 (file)
@@ -1252,6 +1252,7 @@ class Db extends nb {
     return $row;
   }
 
+  #public function default_table() { return $this->default_table; }
 } # < Class
 
 ?>
index a5c4f1ba65bc8a7b4c92b30702c98e93fd142abd..f7af80e744e024d87c8f8750f9072dd963a348a0 100644 (file)
@@ -1,38 +1,40 @@
 <?php
 require_once(realpath(dirname(__FILE__).'/../config.php'));
 require_once(realpath(dirname(__FILE__).'/../db.php'));
-$DB_CONFS = array_merge([]
+if (empty($_SERVER['HOME'])) $_SERVER['HOME'] = '';
+$DB_CONFS = array_merge([],
 
   # ::ROOT_DIR
-  ,[
+  [
     Db::ROOT_DIR.'/etc/dbq.yml',
     Db::ROOT_DIR.'/etc/dbq.yaml',
     Db::ROOT_DIR.'/etc/dbq.php',
-  ]
-  ,(array)glob(Db::ROOT_DIR.'/etc/dbq/*.yml')
-  ,(array)glob(Db::ROOT_DIR.'/etc/dbq/*.yaml')
-  ,(array)glob(Db::ROOT_DIR.'/etc/dbq/*.php')
+  ],
+  (array)glob(Db::ROOT_DIR.'/etc/dbq/*.yml'),
+  (array)glob(Db::ROOT_DIR.'/etc/dbq/*.yaml'),
+  (array)glob(Db::ROOT_DIR.'/etc/dbq/*.php'),
 
   # /etc
-  ,[
+  [
     '/etc/dbq.yml',
     '/etc/dbq.yaml',
     '/etc/dbq.php',
-  ]
-  ,(array)glob('/etc/dbq/*.yml')
-  ,(array)glob('/etc/dbq/*.yaml')
-  ,(array)glob('/etc/dbq/*.php')
+  ],
+  (array)glob('/etc/dbq/*.yml'),
+  (array)glob('/etc/dbq/*.yaml'),
+  (array)glob('/etc/dbq/*.php'),
 
   # ~/
-  ,[
-    '~/.dbq.yml',
-    '~/.dbq.yaml',
-    '~/.dbq.php',
-  ]
-  ,(array)glob($_ENV['HOME'].'/.dbq/*.yml')
-  ,(array)glob($_ENV['HOME'].'/.dbq/*.yaml')
-  ,(array)glob($_ENV['HOME'].'/.dbq/*.php')
-);
+  [
+    $_SERVER['HOME'].'/.dbq.yml',
+    $_SERVER['HOME'].'/.dbq.yaml',
+    $_SERVER['HOME'].'/.dbq.php',
+  ],
+  (array)glob($_SERVER['HOME'].'/.dbq/*.yml'),
+  (array)glob($_SERVER['HOME'].'/.dbq/*.yaml'),
+  (array)glob($_SERVER['HOME'].'/.dbq/*.php'),
+  #] ),
+[]);
 
 #bye($DB_CONFS);
 $DB_CONFS = Db::conf_load($DB_CONFS);
index 1cab6d880268efe99d4537a098c1cfc8790268e6..c51e88b785ab5625fdafdd5ff492e33503e509e8 100644 (file)
@@ -2,16 +2,20 @@
 require_once(realpath(dirname(__FILE__).'/../db.php'));
 require_once(realpath(dirname(__FILE__).'/../db/field.php'));
 require_once(realpath(dirname(__FILE__).'/../out.php'));
+$DB_TABLE_QUERY_ID = 0;
+function __table_define() {
+  define('TABLE_INDENT',NB_EOL ? "\t" : "");
+  define('TABLE_CSV_SEP',Table::p('sep',"\t"));
 
-define('TABLE_INDENT',NB_EOL ? "\t" : "");
-define('TABLE_CSV_SEP',nb::p('sep') ? nb::p('sep') : "\t");
+  if (!defined('TABLE_TEMPLATE')) define('TABLE_TEMPLATE',dirname(__FILE__).'/../../../share/templates');
 
-if (!defined('DB_HTML_EDIT')) define('DB_HTML_EDIT','Edit');
-if (!defined('DB_HTML_DELETE')) define('DB_HTML_DELETE','Delete');
+  if (!defined('DB_HTML_EDIT')) define('DB_HTML_EDIT','Edit');
+  if (!defined('DB_HTML_DELETE')) define('DB_HTML_DELETE','Delete');
 
-# Create a temporary table when table is a SELECT
-if (!defined('DB_TABLE_QUERY_NAME')) define('DB_TABLE_QUERY_NAME','_query_');
-$DB_TABLE_QUERY_ID = 0;
+  # Create a temporary table when table is a SELECT
+  #if (!defined('DB_TABLE_QUERY_NAME'))
+  define('DB_TABLE_QUERY_NAME','_query_');
+}
 
 Class Table extends nb {
 
@@ -780,12 +784,19 @@ Class Table extends nb {
 
     if ($opt_by_val !== null) $opt = $opt_by_val;
 
-    $format = $this->p('format');
-    if (!$format) bye("Parameter `format` missing!");
+    #if (isset($opt['format']) and empty($opt['format'])) {
+    if (isset($opt['format']) and $opt['format']==='') {
+      $format = '';
+    } else {
+
+      $format = empty($opt['format']) ? $this->p('format') : $opt['format'];
+      if (!$format) bye("Parameter `format` missing!");
+    }
 
     list($sql,$where,$limit,$select_count) = $this->rows_sql($opt);
     $st = $this->db()->conn->prepare($sql);#,[PDO::ATTR_ERRMODE => PDO::ERRMODE_SILENT]);
     $st->execute();
+    if (!empty($opt['query'])) return $st;
 
     #
     # Fields
@@ -815,15 +826,18 @@ Class Table extends nb {
     #
     $out_conf = null;
     $opt['use_out'] = false;
+
     if ($this->p('out') or !preg_match('/^('.join('|',
-      [ 'table','sql','div','wp','_csv','_xml','_json','_yaml' ] # local
+      [ '','template','table','sql','div','wp','_csv','_xml','_json','_yaml' ] # local
     ).')$/',$format))
     {
 
       $opt['use_out'] = true;
       if (!($out_conf = out::types($format))) $this->bye("Unknow format `$format`");
-      out::type($format);
-      self::$params += array_values(out::types());
+      $out_conf = array_merge($opt,$out_conf);
+
+      # Function name should be format
+      out::type($format); self::$params += array_values(out::types());
 
       if (empty($out_conf['enclose'])) $out_conf['enclose'] = ['',''];
       debug('Using out module!',3);
@@ -865,32 +879,6 @@ Class Table extends nb {
       $call = null;
 
       $call = $this->rows_parsers($row,$opt);
-      /*
-      if ($parser) $this->db()->table_row_decrypt($this,$row);
-      if ($parser and !empty($this->db()->row_parse)) {
-        $call = $this->db()->row_parse; $call($row);
-      }
-
-      if ($parser and !empty($this->row_parse_pre)) {
-        $call = $this->row_parse_pre; $call($row);
-      }
-
-      if ($opt['is_html'] and !$out_conf) {
-        foreach ($row as $k=>$v) {
-          if (!isset($this->extras[$k])) $row[$k] = out::format($v);
-        }
-
-      }
-
-      if ($parser and !empty($this->row_parse_post)) {
-        $call = $this->row_parse_post; $call($row);
-      }
-      */
-
-      #
-      # Close previous
-      #
-# NB 27.09.16       if ($out_conf and $count !== 1) out::row_end($out_conf);
 
       #
       # Fields filter
@@ -914,21 +902,17 @@ Class Table extends nb {
 
         if ($call) {
           foreach (array_keys($row) as $name) {
-# NB 19.11.16             if (!$this->fields($name)) {
             if (!isset($fields[$name])) {
               $fields[$name] = new Field($name);
               $fields[$name]->dyn = true;
-# NB 19.11.16               $this->fields[$name] = $fields[$name];
             }
           }
         }
 
         if ($out_conf) {
-# NB 19.11.16           out::head($out_conf,array_keys($this->fields()),[$row]);
           out::head($out_conf,array_keys($fields),[$row]);
 
         } else {
-# NB 19.11.16           echo $this->{"rows_begin_$format"}($this->fields(),$opt);
           echo $this->{"rows_begin_$format"}($fields,$opt);
         }
 
@@ -949,6 +933,7 @@ Class Table extends nb {
 
       if ($out_conf) {
         out::row($out_conf,$row);
+
       } else {
         echo $this->{"rows_rec_$format"}($row,$opt);
       }
@@ -996,9 +981,11 @@ Class Table extends nb {
 
     } # < is_html
 
-    if ($count === 0 and $this->p('header') === 'force') {
-# NB 19.11.16       echo $this->{"rows_begin_$format"}($this->fields());
-      echo $this->{"rows_begin_$format"}($fields);
+    if ($count === 0 and (
+      $this->p('header') === 'force'
+      or !$format
+    )) {
+      echo $this->{"rows_begin_$format"}($fields,$opt);
     }
 
     if ($out_conf) {
@@ -1025,6 +1012,53 @@ Class Table extends nb {
     return $count;
   }
 
+  /*-----------------------------------------------------------------
+    No Out
+  -----------------------------------------------------------------*/
+  public function rows_begin_($fields,&$o) {
+    $o['sep'] = '';
+    $o['var'] = [
+      'head' => array_keys($fields),
+      'rows' => [],
+    ];
+  }
+
+  public function rows_rec_($row,&$o) {
+    $o['var']['rows'][] = $row;
+  }
+
+  public function rows_end_() {
+  }
+
+  /*-----------------------------------------------------------------
+    Template
+  -----------------------------------------------------------------*/
+  public function rows_begin_template($fields,&$o) {
+    if (empty($id) and !( $id=self::p('table.template.id') ) ) self::bye('Wrong parameter!');
+
+    $id = preg_replace('/[^\w\._-]/','',$id);
+
+    $file = TABLE_TEMPLATE.'/'.$id.'.php';
+    if (!is_readable($file)) return false; #self::bye("Wrong id `$id`");
+    $o['file'] = $file;
+    $HEAD = array_keys($fields);
+    $ROWS = [];
+    $ROW = [];
+    require $o['file'];
+  }
+
+  public function rows_rec_template($row,&$o) {
+    $HEAD = [];
+    $ROWS = [$row];
+    $ROW = $row;
+    require $o['file'];
+  }
+
+  public function rows_end_template() {
+    unset($this->__file);
+    unset($this->__fields);
+  }
+
   /*-----------------------------------------------------------------
     Wordpress
   -----------------------------------------------------------------*/
@@ -1526,7 +1560,8 @@ Class Table extends nb {
 
     } elseif ($this->p('format') and !preg_match('/^(table|div)$/',$this->p('format'))) {
 
-      $this->rows($dummy,['format' => $this->p('format')]);
+      $opt = ['format' => $this->p('format')];
+      $this->rows($opt);
       return true;
 
     } elseif ($action == 'edit') {
@@ -1812,5 +1847,31 @@ Class Table extends nb {
     return $this->status;
   }
 
+  public function template($id=null) {
+    if (empty($id) and !( $id=self::p('table.template.id') ) ) self::bye('Wrong parameter!');
+
+    $id = preg_replace('/[^\w\._-]/','',$id);
+
+    $file = TABLE_TEMPLATE.'/'.$id.'.php';
+    if (!is_readable($file)) return false; #self::bye("Wrong id `$id`");
+
+    $opt = [ 'format'=> '' ];
+    $this->rows($opt);
+
+    $HEAD = &$opt['var']['head'];
+    $ROWS = &$opt['var']['rows'];
+    $ROW = &$opt['var']['rows'][0];
+    #$opt['var']['rows']=[];
+
+    $_REQUEST_BAK = $_REQUEST;
+    $_REQUEST = array_merge($_REQUEST,$ROW);
+
+    $ex = require $file;
+
+    $_REQUEST = $_REQUEST_BAK;
+
+    #return $ex;
+  }
 } # < Class
+__table_define();
 ?>
index feb76f1c8f18a00ce25bb06e08203791e76b7a18..b99e1819e2a3df69d9b6428e67fb251012b40c42 100644 (file)
@@ -660,15 +660,20 @@ class NB {
     if ($user === null) $user = '';
     if ($key === null) $key = '';
     $infos = [];
+    #bye((array)$infos);
 
+    #
     # Linux standard
+    #
     if (function_exists('posix_getuid')) {
       if ($user==='') $user = posix_getuid();
       if (preg_match('/^\d+$/',$user)) $infos = posix_getpwuid($user);
       else $infos = posix_getpwnam($user);
     }
 
+    #
     # /etc/passwd
+    #
     if (!$infos
       and $user !== ''
       and self::osname()!='darwin'
@@ -693,7 +698,9 @@ class NB {
                        fclose($handle);
     }
 
+    #
     # Command
+    #
     if (!$infos) {
       $cmd = $user === '' ? 'id' : sprintf('id %s',$user);
       $line = []; exec(escapeshellcmd($user === '' ? 'id' : 'id '.escapeshellarg($user)).' 2>/dev/null',$line);
index e2ef91eb77e96df514618be4b2cc0602f965e67a..f7e3d8311668d704c6748f5fd640c9910289b924 100644 (file)
@@ -112,6 +112,17 @@ Class Out extends Nb {
         'eol' => self::p('eol',NB_EOL),
         'rec' => ',',
         'row' => function(&$o,&$r) {
+#bye($o['var_export']);
+/*
+          if (!empty($o['var_export'])) {
+            if (empty($_GLOBAL['ROWS'])) {
+              $_GLOBAL['ROWS'] = [];
+              $o['rec'] = '';
+            }
+            $_GLOBAL['ROWS'][] = $r;
+            return;
+          }
+          */
           return var_export($r);
           echo preg_replace(array(
             '/(=>\s*)\n\s+/m',
diff --git a/share/templates/hosts.php b/share/templates/hosts.php
new file mode 100644 (file)
index 0000000..47e33df
--- /dev/null
@@ -0,0 +1,25 @@
+<?php
+# NB 01.12.16: /etc/hosts
+if (isset($ROWS)) foreach ($ROWS as $r) {
+  $line = [];
+
+  foreach ([
+    'ip',
+    'addr',
+    'address',
+  ] as $k) {
+    if (isset($r[$k])) $line[] = $r[$k];
+  }
+
+  foreach ([
+    'hostname',
+    'host',
+    'name',
+    'id',
+  ] as $k) {
+    if (isset($r[$k])) $line[] = $r[$k];
+  }
+
+  if (count($line)==2) echo join(' ',$line)."\n";
+}
+?>
index 242e931b8d2c5c8ec432434b2a93785b3bc7abc9..5416c0bb0816480920a2b95f97f0de7829f25bc1 100644 (file)
@@ -1,13 +1,16 @@
 <?php
-  $P = $_REQUEST;
-  if (empty($P['max_allowed_packet'])) $P['max_allowed_packet'] = '10M';
+  if (empty($ROW['max_allowed_packet'])) $ROW['max_allowed_packet'] = '64M';
 ?>
 [mysqld]
-max_allowed_packet = <?=$P['max_allowed_packet']."\n"?>
+max_allowed_packet = <?=$ROW['max_allowed_packet']."\n"?>
 
 [client]
 <?php
-if (!empty($P['host'])) echo 'host = '.$P['host']."\n";
-if (!empty($P['user'])) echo 'user = '.$P['user']."\n";
-if (!empty($P['password'])) echo 'password = '.$P['password']."\n";
+foreach ([
+  'host',
+  'user',
+  'password',
+] as $k) {
+if (!empty($ROW[$k])) echo 'host = '.$ROW[$k]."\n";
+}
 ?>
diff --git a/share/templates/pgpass.php b/share/templates/pgpass.php
new file mode 100644 (file)
index 0000000..c6c78c5
--- /dev/null
@@ -0,0 +1,15 @@
+<?php
+/*
+NB 01.12.16
+hostname:port:database:username:password 
+*/
+if (isset($ROWS)) foreach ($ROWS as $r) {
+  echo join(':',[
+    empty($r['hostname']) ? '*' : $r['hostname'],
+    empty($r['port']) ? '*' : $r['port'],
+    empty($r['database']) ? '*' : $r['database'],
+    empty($r['username']) ? '*' : $r['username'],
+    empty($r['password']) ? '*' : $r['password'],
+  ]);
+}
+?>