From 34868f6b117755928faa752d2e1d2aa563a57fff Mon Sep 17 00:00:00 2001 From: Nicolas Boisselier Date: Sun, 13 Nov 2016 00:23:24 +0000 Subject: [PATCH] shell, fix bugs --- lib/php/db.php | 32 ++++++++++++++++++++++---------- lib/php/db/field.php | 10 ++++++++++ lib/php/db/shell.php | 18 +++++++++++++++--- lib/php/db/table.php | 34 +++++++++++++++++++++------------- lib/php/functions.php | 4 ++-- lib/php/nb.php | 17 +++++++++++++---- lib/php/out.php | 13 +++++++++++++ 7 files changed, 96 insertions(+), 32 deletions(-) diff --git a/lib/php/db.php b/lib/php/db.php index e9e1d6bb..1d921df2 100644 --- a/lib/php/db.php +++ b/lib/php/db.php @@ -54,8 +54,17 @@ class Db extends nb { public $conf = []; public $row_parse; # Function to call in table.rows() - # Encription - public $encrypt = []; + # Encryption + public $encrypt = [ +# 'secret' => '32_CHAR_KEY', +# 'tables' => [ +# 'TABLE1' => [ +# 'FIELD1', +# ..., +# ], +# ..., +# ], + ]; # Params protected static $paliases = [ @@ -500,9 +509,11 @@ class Db extends nb { return $this->tables; } - public static function print_header($format) { + public static function print_header($format,$value=null) { if (empty($_SERVER['DOCUMENT_ROOT'])) return null; + if ($value!==null) return header("$format: $value"); + if (!$mime=self::ext2mime($format)) return false; header('Content-type: '.$mime); @@ -522,6 +533,7 @@ class Db extends nb { public function out($rows,$head=[]) { + if ($this->p('format') == 'null') return true; // Handle limit if ($this->p('limit')) { @@ -1113,8 +1125,8 @@ class Db extends nb { return $this->databases; } - public static function pinit() { - if (!empty(self::$paliases)) self::paliases(self::$paliases); + public static function pinit(&$changed=[]) { + if (!empty(self::$paliases)) self::paliases(self::$paliases,$changed); if ($action=self::p('action') and !empty(self::$action_aliases)) { foreach (self::$action_aliases as $src => $dest) { @@ -1123,10 +1135,10 @@ class Db extends nb { } # Param - Extract dbname from table - if (false and preg_match('/^(\w+)\.(.*?)$/',self::p('table'),$m)) { - self::pset('db',$m[1]); - self::pset('table',$m[2]); - } +# NB 12.11.16 if (false and preg_match('/^(\w+)\.(.*?)$/',self::p('table'),$m)) { +# NB 12.11.16 self::pset('db',$m[1]); +# NB 12.11.16 self::pset('table',$m[2]); +# NB 12.11.16 } # Select param if (Db::p('table') and Db::p('select')) { @@ -1135,7 +1147,7 @@ class Db extends nb { } # Set format from client Accept if != html - Db::pdef('format',out::client_type()); + if (!Db::p('format')) Db::pdef('format',out::client_type()); } diff --git a/lib/php/db/field.php b/lib/php/db/field.php index 5093319f..e1b14f24 100644 --- a/lib/php/db/field.php +++ b/lib/php/db/field.php @@ -250,5 +250,15 @@ class field extends nb { return $value; } + public function is_encrypt() { + if (0 + or empty($this->db()->encrypt) + or empty($this->db()->encrypt['tables']) + or empty($this->db()->encrypt['tables'][$this->table->name]) + ) + return false; + return in_array($this->name,$this->db()->encrypt['tables'][$this->table->name]); + } + } ?> diff --git a/lib/php/db/shell.php b/lib/php/db/shell.php index a3182043..20d0e28b 100755 --- a/lib/php/db/shell.php +++ b/lib/php/db/shell.php @@ -39,6 +39,7 @@ while($line = fgets(STDIN)){ )) { $line = mb_substr($line,mb_strlen($args[0])+1); array_shift($args); list ($k,$v) = $args; + #print_r([$k,$v]); db_shell_action($k,$v); } @@ -47,7 +48,9 @@ while($line = fgets(STDIN)){ function db_shell_action($k,$v) { global $DB_CONFS, $Db, $Table; Db::pset($k,$v); - Db::pinit(); + $changed = []; + Db::pinit($changed); + if (isset($changed[$k])) $k = $changed[$k]; switch ($k) { @@ -62,6 +65,11 @@ function db_shell_action($k,$v) { break; } + #bye($Db::p()); + if ($Db->p('debug') == 'shell') { + debug($Db->p()); + break; + } $Db->action($Db->p('action'),$Table); break; @@ -79,10 +87,14 @@ function db_shell_action($k,$v) { break; default: - echo "ERR: $k=$v\n"; - Db::pset($k,null); + Db::pset($k,$v); break; +# default: +# echo "ERR: $k=$v\n"; +# Db::pset($k,null); +# break; + } } ?> diff --git a/lib/php/db/table.php b/lib/php/db/table.php index ea6ea092..4cad46f3 100644 --- a/lib/php/db/table.php +++ b/lib/php/db/table.php @@ -286,19 +286,25 @@ Class Table extends nb { if (empty($i['unique']) and empty($i['key'])) $indexes[$i['field']] = 1; } + $count = 0; foreach ($rows as $row) { + $count++; $row = array_change_key_case($row,CASE_LOWER); // From config $row['this'] = $this; # for use if (isset($conf['map'])) { + foreach ($conf['map'] as $from => $to) { if (!isset($row[$from])) continue; + $row[$to] = $row[$from]; unset ($row[$from]); } + } + if (isset($conf['fct'])) $conf['fct']($row) ; $field = array( @@ -407,9 +413,9 @@ Class Table extends nb { echo '
'.NB_EOL; $count = 0; if ( $add or ($row = $st->fetch(PDO::FETCH_ASSOC, PDO::FETCH_ORI_NEXT))) { - $orig = $row; + #$orig = $row; - $this->db()->table_row_decrypt($this,$row); + if (!empty($row)) $this->db()->table_row_decrypt($this,$row); $count ++; foreach ($fields as $name => $field) { @@ -471,7 +477,7 @@ Class Table extends nb { public function url_sort($name) { - if (!$this->buttons() or !($f=$this->fields($name)) or !empty($f->dyn)) return self::prettyText($name); + if (!$this->buttons() or !($f=$this->fields($name)) or !empty($f->dyn) or $f->is_encrypt()) return self::prettyText($name); #debug($f); # See: http://dev.w3.org/html5/html-author/charref @@ -757,7 +763,7 @@ Class Table extends nb { $st->execute(); # Fields - $fields = array(); + $fields = []; if (!DB_TABLE_QUERY_NAME) { foreach ($this->db()->fields($st) as $f) { $fields[$f->name] = $f; } $this->fields = $fields; @@ -855,9 +861,10 @@ Class Table extends nb { } if ($out_conf) { - out::head($out_conf,$this->fields(),[$row]); + out::head($out_conf,array_keys($this->fields()),[$row]); } else { + #bye([$this->fields(),$row]); echo $this->{"rows_begin_$format"}($this->fields(),$opt); } @@ -1243,7 +1250,7 @@ Class Table extends nb { if (self::p('debug')) { $this->debug($info,1); - return false; + return true; } if (!($execute = $query->execute())) { @@ -1422,27 +1429,27 @@ Class Table extends nb { return $this->out($this->info()); } elseif ($action == 'table.delete') { - if (!$this->delete($this->p(),$e)) bye($e); - header('Location: '.$this->url_referer(str_replace('&','&',$this->url_list()))); + if (!$this->delete($this->p(),$e) and !isset($e['rowCount'])) bye($e); + $this->db()->print_header('Location',$this->url_referer(str_replace('&','&',$this->url_list()))); $this->out($e); return true; } elseif ($action == 'table.replace') { - if (!$this->replace($this->p(),$e)) bye($e); - header('Location: '.$this->url_referer()); + if (!$this->replace($this->p(),$e) and !isset($e['rowCount'])) bye($e); + $this->db()->print_header('Location',$this->url_referer()); $this->out($e); return true; } elseif ($action == 'table.insert') { if (!$this->insert($this->p(),$e)) bye(); - header('Location: '.$this->url_referer()); + $this->db()->print_header('Location',$this->url_referer()); $this->out($e); return true; } elseif ($action == 'table.update') { #$this->bye($this->p()); if (!$this->update($this->p(),$e)) bye($e); - header('Location: '.$this->url_referer()); + $this->db()->print_header('Location',$this->url_referer()); $this->out($e); return true; @@ -1604,8 +1611,9 @@ Class Table extends nb { // Criterias // $r .= '
'; - foreach ( array_keys($this->fields()) as $k ) { + foreach ( $this->fields() as $k => $f) { + if ($f->is_encrypt()) continue; $v = $this->p($k); $r .= '' diff --git a/lib/php/functions.php b/lib/php/functions.php index 19f3326e..62cef74b 100644 --- a/lib/php/functions.php +++ b/lib/php/functions.php @@ -78,7 +78,7 @@ function warn ($msg) { function err($msg='__err__',$preff='err',$backtrace_deep=0) { # NB 05.04.16 $msg = is_scalar($msg) ? $msg : print_r($msg,true); - $msg = is_scalar($msg) ? $msg : var_export($msg,true); + $msg = is_scalar($msg) ? $msg : print_r($msg,true); $preff_msg = $preff ? strtoupper($preff).': ' : ''; if ($msg !== '__err__' and $backtrace_deep !== false) { $msg = trim($preff_msg.$msg).' ' @@ -102,7 +102,7 @@ function err($msg='__err__',$preff='err',$backtrace_deep=0) { function debug($msg,$level=0) { if ($level and $level>nb::p('debug')) return; #$msg = is_scalar($msg) ? $msg : print_r($msg,true); - $msg = is_scalar($msg) ? $msg : var_export($msg,true); + $msg = is_scalar($msg) ? $msg : print_r($msg,true); if (nb::client_header('Accept','ml')) { $msg = '
'
diff --git a/lib/php/nb.php b/lib/php/nb.php
index 0e284e61..a04b01a7 100644
--- a/lib/php/nb.php
+++ b/lib/php/nb.php
@@ -48,6 +48,8 @@ class NB {
 
   }
 
+  #public function __set_state() { }
+
   public function __sleep() {
     return(get_class_vars(get_class($this))); # get all
 # NB 09.04.16     return array_keys((array)$this);
@@ -75,9 +77,12 @@ class NB {
   /*
    * Replace aliases by long name
    */
-  public static function paliases($aliases) {
+  public static function paliases($aliases,&$changed=[]) {
     foreach ($aliases as $short=>$long) {
-      if (!preg_match('/^\s*$/',self::p($short))) self::pset($long,self::p($short));
+      if (!preg_match('/^\s*$/',self::p($short))) {
+        self::pset($long,self::p($short));
+        $changed[$short] = $long;
+      }
       #if (!preg_match('/^\s*$/',self::p($short))) echo ">$long => short=".self::p($short);
       self::pset($short,null);
     }
@@ -88,7 +93,7 @@ class NB {
    * Return the value set or existing
    */
   public static function pdef($name,$value) {
-    if (preg_match('/^\s*$/',self::p($name))) return self::pset($name,$value);
+    if (preg_match('/^\s*$/',self::p($name))) return self::pset($name,is_callable($value) ? $value() : $value);
     return self::p($name);
   }
 
@@ -506,7 +511,11 @@ class NB {
 
   public static function php_cli() {
 # NB 05.03.16 define('NB_CLI', PHP_SAPI === 'cli');
-    return (!isset($_SERVER['SERVER_SOFTWARE']) && (php_sapi_name() == 'cli' || (is_numeric($_SERVER['argc']) && $_SERVER['argc'] > 0)));
+    static $php_cli;
+    if ($php_cli===null or $php_cli==='') $php_cli= (
+      !isset($_SERVER['SERVER_SOFTWARE']) && (php_sapi_name() == 'cli' || (is_numeric($_SERVER['argc']) && $_SERVER['argc'] > 0))
+    );
+    return $php_cli;
   }
 
   public static function no_accent($str){
diff --git a/lib/php/out.php b/lib/php/out.php
index 0aba6972..6b76e8a2 100644
--- a/lib/php/out.php
+++ b/lib/php/out.php
@@ -22,6 +22,19 @@ Class Out extends Nb {
 
       'sql' => array(), # Code still in db/table.php !
 
+      'shell' => array(
+        'preff' => self::p('preff',''),
+        'suff' => self::p('suff',''),
+        'row' => function(&$o,&$r) {
+          $line = [];
+          foreach ($r as $k=>$v) {
+            $v = str_replace('"','\\"',$v);
+            $line[] = $o['preff']."$k=\"$v\"".$o['suff'];
+          }
+          if ($line) echo join(' ',$line)."\n";
+        }
+      ),
+
       'sh' => array(
         'preff' => self::p('preff',""),
         'row' => function(&$o,&$r) {
-- 
2.47.3