]> git.nbdom.net Git - nb.git/commitdiff
shell, fix bugs
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Sun, 13 Nov 2016 00:23:24 +0000 (00:23 +0000)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Sun, 13 Nov 2016 00:23:24 +0000 (00:23 +0000)
lib/php/db.php
lib/php/db/field.php
lib/php/db/shell.php
lib/php/db/table.php
lib/php/functions.php
lib/php/nb.php
lib/php/out.php

index e9e1d6bb4156d2aab4e711a3f6a5ce9525507a43..1d921df262660380d59c16d9a762aa03194c881f 100644 (file)
@@ -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());
 
   }
 
index 5093319feddc0d7da9f6e0b02a770b8e450c0c8a..e1b14f248fb9ab5843e7628bf1a516066986d8d6 100644 (file)
@@ -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]);
+  }
+
 }
 ?>
index a318204309f40b84d5e63c3c0f678bb6074093dd..20d0e28b3308b8fd5e544736a6f88c9c82ec5d3d 100755 (executable)
@@ -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;
+
   }
 }
 ?>
index ea6ea09297f5ae8ed62e82be45e566ea59af9d61..4cad46f3929e8ce129004faa871b51f05f421dfc 100644 (file)
@@ -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 '<div class="fields">'.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('&amp;','&',$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('&amp;','&',$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 .= '<div class="criterias">';
-    foreach ( array_keys($this->fields()) as $k ) {
+    foreach ( $this->fields() as $k => $f) {
 
+      if ($f->is_encrypt()) continue;
       $v = $this->p($k);
 
       $r .= ''
index 19f3326e23387ab2bca0bccf0c3f5f0ce0749529..62cef74b8a520905af12a05329a9620660e89712 100644 (file)
@@ -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 = '<pre class="debug">'
index 0e284e61c02c98e79ba3580c94e43740a12adfb7..a04b01a7f06b17db34c47846d63669d6be7feab3 100644 (file)
@@ -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){
index 0aba69720ddc2af4ec3a6b8f1f2b401ceab7aab7..6b76e8a2e0f0d7a27c930908e728fc1f1b5656df 100644 (file)
@@ -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) {