]> git.nbdom.net Git - nb.git/commitdiff
Bed
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Sun, 10 Apr 2016 04:46:36 +0000 (05:46 +0100)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Sun, 10 Apr 2016 04:46:36 +0000 (05:46 +0100)
bin/dbq
etc/dbs.php
etc/dbs.yaml
lib/js/nb.js
lib/php/db.php
lib/php/db/table.php
lib/php/db/types/mysql.php
lib/php/functions.php
lib/php/nb.php
lib/php/out.php
lib/php/page.php

diff --git a/bin/dbq b/bin/dbq
index f3e1399fcf177550eb49b22c147309dce0f7bd30..2e00472c6979d3c3baa95b108038de4678606623 100755 (executable)
--- a/bin/dbq
+++ b/bin/dbq
@@ -33,7 +33,7 @@ my %PARAM = (
 my %CMD_ALIASE = (
 # NB 09.04.16   'd' => 'db',
 # NB 09.04.16   't' => 'table',
-# NB 09.04.16   'f' => 'format',
+  'f' => 'format',
 # NB 09.04.16   'l' => 'limit',
 # NB 09.04.16   'a' => 'action',
 # NB 09.04.16   'h' => 'header',
@@ -228,15 +228,15 @@ sub parse_debug {
 }
 
 sub csv2txt {
+  # sprintf missing argument warning message ????
+  # Can't open XXX: No such file or directory
+  no warnings;
+
   my $sep = shift;
   my $noheader = shift;
   my @lines = ();
   my $l;
 
-  # sprintf missing argument warning message ????
-  # Can't open XXX: No such file or directory
-  no warnings;
-
   #open(STDOUT,">&2");
   #
   # Store lines and lengths in memory
@@ -264,8 +264,13 @@ sub csv2txt {
   csv2txt_next:
 
   my @len = ();
+  #warn @{$lines[0]};
+  my $count=0;
        for (@lines) {
     my @F = @$_;
+    last if $count and @F and $F[0] =~/^\r/;
+    $count++;
+
                for (my $i=0;$i<@F;$i++) {
                        $len[$i] = length($F[$i]) unless defined $len[$i];
                        $len[$i] = $l if ( $l = length($F[$i]) ) >= $len[$i];
@@ -289,7 +294,7 @@ sub csv2txt {
     if ($_->[0] =~ s/^\r// and $i) {
       csv_end();
       print "\n";
-      push @lines,$_;
+      unshift @lines,$_;
       goto csv2txt_next;
     }
     printf $format,@$_;
index 5982fa15727910285788e999cc4fbb15d778292f..6facc4d988b42b04bf492eb70d25f325a88ddac9 100644 (file)
@@ -4,28 +4,34 @@
 $CONF = array (
   'nb' => array (
     'host' => 'admin.izideal.vpn',
-    'user' => 'nico',
-    'password' => 'mpc60ax73',
     'type' => 'mysql',
     'name' => 'nb',
+    'user' => 'nico',
+    '_import' => '_nico',
     'default_table' => 'rent',
     'tables' => array(
       'rent' => array(
-        'order_by' => 'start desc',
+        'orderby' => 'start desc, end desc',
         'replace' => array (
           'idtenant' => 'tenant.name',
           'idplace' => 'addr.addr',
         ),
-        'call_rows' => function(&$r) {
-          if (0) $r['rent_year'] = $r['rent'] * (1 +
-            ( date('m',$r['end'])+12*date('Y',strtotime($r['end'])) )
-            - 
-            ( date('m',$r['start'])+12*date('Y',strtotime($r['start'])) )
-          );
+        'row_parse_pre' => function(&$r) {
           #extra: "rent * (1 + ( strftime('%m',end)+12*strftime('%Y',end) ) -  ( strftime('%m',start)+12*strftime('%Y',start) ))"
           #$r['rent_year'] = $r['rent'] * (1 + ( date('m',strtotime($r['end']))+12
           $r['Total'] = $r['rent'] + $r['charge'];
-        }
+          if (1) $r['rent_year'] = sprintf('%.2f',$r['rent'] * (1 +
+            ( date('m',strtotime($r['end'])) + 12 * date('Y',strtotime($r['end'])) )
+            - 
+            ( date('m',strtotime($r['start'])) + 12 * date('Y',strtotime($r['start'])) )
+          ));
+        },
+        'row_parse_post' => function(&$r) {
+          $r['revision'] = join(' ',array(
+            '<a href="template/?id=quittance.html&amp;idplace='.$r['idplace'].'&amp;idtenant='.$r['idtenant'].'&amp;start=2015-04-01">quittance</a>',
+            '<a href="template/?id=revision.txt&amp;idplace='.$r['idplace'].'&amp;idtenant='.$r['idtenant'].'&amp;start=2015-04-01">revision</a>',
+          ));
+        },
       ),
     ),
   ),
index fee137b044507d2ac93e4739879f95ec74ba2a0c..ab9818e132cd8db04c2b53ed8cff8dcff9e56e2c 100644 (file)
@@ -1,3 +1,6 @@
+_nico:
+  user: nico
+
 _mysql:
   type: mysql
 
@@ -9,7 +12,7 @@ wp:
   _import:
     - _nico
     - _mysql
-nb:
+___nb:
   host: admin.izideal.vpn
   _import:
     - _nico
@@ -41,28 +44,6 @@ puppetdb:
         test:
           type: "int"
           extra: 1
-izi:
-  #pdo: 'mysql:host=admin.izideal.com;port=3306;dbname=izi'
-  title: Mysql Izi on Admin
-  default_table: process
-  host: admin.izideal.vpn
-  user: izideal
-  name: izi
-  _import:
-    - _nico
-    - _mysql
-  tables:
-    site:
-      replace:
-        idlang: lang.id
-
-izidev:
-  host: big.cascais.loc
-  title: Mysql Izi on Big
-  #user: nico
-  _import:
-    - izi
-    - _nico
 
 ui:
   pdo: 'sqlite:/opt/semantico/product/releases/sem_ui/db/semantico.db'
@@ -71,7 +52,7 @@ ui:
   default_table: node
   tables:
     node:
-      order_by: "(SELECT value FROM fact WHERE idnode=id AND name='date_install') DESC"
+      orderby: "(SELECT value FROM fact WHERE idnode=id AND name='date_install') DESC"
       replace_:
         iddc: dc.id
   replace:
@@ -107,7 +88,7 @@ _rent:
         id: addr.addr
 
     rent:
-      order_by: "start desc"
+      orderby: "start desc, end desc"
       replace:
         idtenant: tenant.name
         idplace: addr.addr
@@ -121,20 +102,41 @@ _rent:
           #extra: "rent * (1 + ( date_format(end,'%m')+12*date_format(end,'%Y') ) -  ( date_format(start,'%m')+12*date_format(start,'%Y') ))"
         #revision: (SELECT GROUP_CONCAT('<a href="template/?id='||id||'&amp;idplace='||idplace||'&amp;idtenant='||idtenant||'&amp;start='||start||'">'||substr(id,0,instr(id,'.'))||'</a>',' ') FROM template)
 
-# TODEL - NB 10.01.16
-# NB 10.01.16   sort:
-# NB 10.01.16     rent: start desc
-# NB 10.01.16   extras:
-# NB 10.01.16     rent:
-# NB 10.01.16       total:
-# NB 10.01.16         type: "float(8,2)"
-# NB 10.01.16         extra: "ROUND(rent+charge,2)"
-# NB 10.01.16       rent_year:
-# NB 10.01.16         type: "float(8,2)"
-# NB 10.01.16         extra: "rent * (1 + ( strftime('%m',end)+12*strftime('%Y',end) ) -  ( strftime('%m',start)+12*strftime('%Y',start) ))"
-# NB 10.01.16       #revision: (SELECT GROUP_CONCAT('<a href="template/?id='||id||'&amp;idplace='||idplace||'&amp;idtenant='||idtenant||'&amp;start='||start||'">'||substr(id,0,instr(id,'.'))||'</a>',' ') FROM template)
-# NB 10.01.16     template:
-# NB 10.01.16       View: "'<a href=\"/template/'||id||'\">'||id||'</a>'"
+izi:
+  #pdo: 'mysql:host=admin.izideal.com;port=3306;dbname=izi'
+  title: Mysql Izi
+  host: sql.izideal.vpn
+  default_table: process
+  user: izideal
+  name: izi
+  _import:
+    - _mysql
+  tables:
+    site:
+      replace:
+        idlang: lang.id
+
+admin:
+  title: Mysql Izi on Admin
+  host: admin.izideal.vpn
+  _import:
+    - izi
+
+izidev:
+  host: big.cascais.loc
+  title: Mysql Izi on Big
+  #user: nico
+  _import:
+    - izi
+    - _nico
+
+media:
+  host: media.izideal.vpn
+  name: mysql
+  _import:
+    - _mysql
+    - _nico
+
 postfix:
   #pdo: 'mysql:host=media.izideal.vpn;port=3306;dbname=postfix'
   title: Postfix on Izideal
index 1585c40e1295248947b6a402fa30413aaaa0b212..94fcb76840946461bb0fb982b1499c2e215c8762 100644 (file)
@@ -136,7 +136,7 @@ function NB() {
       that.table2div('table.rows');
       return true;
     } else {
-      $('table.rows').addClass('t2d');
+      $('.block table, table.block, table.rows').addClass('t2d');
       $('.t2d-th').each(function(){ $(this).remove(); });
     }
 
index aafd5fce396ba11290c54a4059a8a8003ca52cb4..06cb62afed1fa13baea305fd4da3cc586932c362 100644 (file)
@@ -92,7 +92,8 @@ class Db extends nb {
     #preg_match_all('/[:;](user|username|password)=([^;]*)/',$this->pdo,$m); bye($m);
     if( !$this->type ) return false;
     if ($this->type('use_path')) {
-      $this->name = preg_replace('/^\w+:/','',$this->pdo);
+      $this->file = preg_replace('/^\w+:/','',$this->pdo);
+      $this->host = $this->file;
 
     } else {
 
@@ -109,6 +110,8 @@ class Db extends nb {
       }
       #$this->bye($this->pdo);
 
+      if ($p = $this->p('db.host')) $this->host = $p;
+
       # Add args to pdo
       foreach (array(
         'host' => 'host',
@@ -135,10 +138,10 @@ class Db extends nb {
     if (!$this->type(null)) $this->bye("Unknow type = `".$this->type."`");
 
     # Connect
-    if ($this->type('use_path') and !is_readable($this->name)) {
-      $this->bye("Can't read database file `".$this->name."` type=`$this->type`",false);
-
+    if ($this->type('use_path') and !is_readable($this->file)) {
+      $this->bye("Can't read database file `".$this->file."` type=`$this->type`",false);
     }
+
     try {
       $this->conn = new PDO($this->pdo,$this->user,$this->password,$this->options);
       #$this->conn = new PDO($this->pdo,$this->user,$this->password,$this->pdo_error);
@@ -626,6 +629,10 @@ EOF;
       Table
     */
     if (empty($Db)) return false;
+    if (self::p('action') and
+      !self::p('table') and
+      !preg_match('/^(table\.\w+|rows|insert|edit|delete|update)$/',self::p('action'))
+    ) return true;
     if (!$Db->p('table') and isset($Db->default_table)) $Db->pset('table',$Db->default_table);
     if (!$Db->p('table') and ($v = array_keys($Db->tables()))) $Db->pset('table',$Db->ar_first($v));
 
@@ -690,25 +697,7 @@ EOF;
 
   public function status() {
 
-    if (!($status=$this->type('status'))) return array();
-
-    $new = array();
-    foreach ($status as $sql => $fct) {
-
-      try {
-        $sth = $this->conn->prepare($sql);
-        $sth->execute();
-      } catch(PDOException $e){ 
-        continue;
-      }
-
-      $rows = $sth->fetchAll(PDO::FETCH_BOTH);
-      foreach ($rows as $i=>$r) {
-        if ($v = $fct($r)) $new += $v;
-      }
-      #$status = $new;
-      #debug($rows);
-    }
+    $status = $new = array();
 
     $new = array(
       'host' => $this->host,
@@ -720,7 +709,27 @@ EOF;
       'tables' => count($this->tables()),
     )+$new;
 
-    $status = array();
+    if (($sqls=$this->type('status'))) {
+
+      foreach ($sqls as $sql => $fct) {
+
+        try {
+          $sth = $this->conn->prepare($sql);
+          $sth->execute();
+        } catch(PDOException $e){ 
+          continue;
+        }
+
+        $rows = $sth->fetchAll(PDO::FETCH_BOTH);
+        foreach ($rows as $i=>$r) {
+          if ($v = $fct($r)) $new += $v;
+        }
+        #$sqls = $new;
+        #debug($rows);
+      }
+
+    } # << type.status
+
     #debug($new);
     foreach ($new as $k=>$v) {
       $status[] = array(
@@ -730,21 +739,6 @@ EOF;
     }
 
     return $status;
-    /*
-    */
-
-    return array(
-      'host' => $this->host,
-      'port' => $this->port,
-      'name' => $this->name,
-      'user' => $this->user,
-      #'tables' => count($this->tables()),
-      #'dbs' => count($this->dbs),
-      'tables' => count($this->tables()),
-      #'tables' => join(' ',array_keys($this->tables())),
-      #'dbs' => join(' ',array_values($this->dbs)),
-    )+$status;
-
   }
 
   public function fields($st) {
index c13ffb7ea3864c276c62e76b1d5ca30236938d3e..1a75c10a00d145be5f4fa6aca69d3cf9f8654959 100644 (file)
@@ -12,6 +12,8 @@ define('TABLE_CSV_SEP',nb::p('sep') ? nb::p('sep') : "\t");
 #if (!defined('DB_HTML_BUTTON_ADD'))    define('DB_HTML_BUTTON_ADD','<input type="submit" class="button" value="Add" onclick="this.setAttribute(\'name\',\'edit\')?0:0"/>');
 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_');
 
 Class Table extends nb {
@@ -22,7 +24,7 @@ Class Table extends nb {
 # NB 28.03.16   public $fields_keys;
   public $replace = array(); # replace by javascript
   public $extras = array();
-  public $call_rows; # Function to call in rows()
+  public $row_parse_pre; # Function to call in rows()
   public $count;
   public $engine;
   public $created;
@@ -30,7 +32,6 @@ Class Table extends nb {
   # hidden, sort, ... fields
   public static $params = array( 'db', 'table', 'limit', 'debug', 'action');
 
-  public $orderby = null;
   public $fields;
 
   function __construct($name,$opt=array()) {
@@ -55,27 +56,25 @@ Class Table extends nb {
 
     } elseif (isset($opt['extras'])) {
       $this->add_extras($opt['extras']);
-
-    # Extras should disapear - NB 29.03.16
-# NB 29.03.16       } elseif ($this->db()
-# NB 29.03.16         and isset($this->db()->extras)
-# NB 29.03.16         and isset($this->db()->extras[$this->name])
-# NB 29.03.16       ) {
-# NB 29.03.16         $this->add_extras($this->db()->extras[$this->name]);
-
+      unset($opt['extras']);
     }
-    unset($opt['extras']);
 
     // Connection
 #bye($opt['db']);
     if (isset($opt['db'])) {
       $this->db(is_object($opt['db']) ? $opt['db'] : new Db($opt['db']));
+      unset($opt['db']);
     } elseif(isset($GLOBALS['Db'])) {
       $this->db($GLOBALS['Db']);
     } else {
       $this->db(new Db());
     }
-    unset($opt['db']);
+
+    // Order BY
+# NB 10.04.16     if (isset($opt['orderby'])) {
+# NB 10.04.16       $this->pdef('orderby',$opt['orderby']);
+# NB 10.04.16       unset($opt['orderby']);
+# NB 10.04.16     }
 
     // Add others
 #if (empty($opt)) bye($opt);
@@ -94,9 +93,6 @@ Class Table extends nb {
 
     }
 
-    // Sort
-    if ($this->orderby) $this->pset('orderby',$this->orderby);
-
 # NB 29.03.16     return $this->fields();
   }
 
@@ -341,6 +337,9 @@ Class Table extends nb {
 
   public function url_sort($name) {
 
+    if ($this->p('buttons')==='0' or !($f=$this->fields($name)) or !empty($f->dyn)) return self::prettyText($name);
+    #debug($f);
+
     # See: http://dev.w3.org/html5/html-author/charref
     $html = '';
 
@@ -532,10 +531,9 @@ Class Table extends nb {
     Html Output
   ******************************************************************/
   public function rows(&$opt=array(),$opt_by_val=null) {
+    if (isset($this->orderby)) self::pdef('orderby',$this->orderby);
     if ($opt_by_val !== null) $opt = $opt_by_val;
 
-# NB 17.03.16     $format = ( $this->p('format') ? $this->p('format') : 'table' );
-# NB 19.03.16     $format = $this->pdef('format','table');
     $format = $this->p('format');
     if (!$format) bye("No param format");
     
@@ -618,8 +616,8 @@ Class Table extends nb {
 
       $count++;
 
-      if (!empty($this->call_rows)) {
-        $call = $this->call_rows; $call($row);
+      if (!empty($this->row_parse_pre)) {
+        $call = $this->row_parse_pre; $call($row);
       }
 
       if ($opt['is_html'] and !$out_conf) {
@@ -628,17 +626,21 @@ Class Table extends nb {
         }
       }
 
+      if (!empty($this->row_parse_post)) {
+        $call = $this->row_parse_post; $call($row);
+      }
+
       #
       # Head
       #
       if ($count === 1) {
         if ($opt['is_html']) echo $this->html_rows_top();
 
-        if ($this->call_rows) {
+        if (!empty($this->row_parse_pre) or !empty($this->row_parse_post)) {
           foreach (array_keys($row) as $name) {
             if (!$this->fields($name)) {
               $this->fields[$name] = new Field($name);
-              $this->call = true;
+              $this->fields[$name]->dyn = true;
               #$fields[$name] = $this->fields[$name];
             }
           }
@@ -892,8 +894,7 @@ Class Table extends nb {
 
       foreach ($fields as $name => $f) {
         #$html .= '<th class="'.$f.'">'.($this->p('buttons')==='0' ? $f : $this->url_sort($f)).'</th>'.NB_EOL;
-        $sort = $this->p('buttons')!=='0' and empty($f->call);
-        $html .= '<th class="'.$name.($f->key?' key':'').'">'.(!$sort ? $this->prettyText($name) : $this->url_sort($name)).'</th>'.NB_EOL;
+        $html .= '<th class="'.$name.($f->key?' key':'').'">'.$this->url_sort($name).'</th>'.NB_EOL;
       }
       if ($this->p('buttons')!=='0' and DB_HTML_DELETE) $html .= '<th class="delete"></th>'.NB_EOL;
       $html .= '</tr>'.NB_EOL.'</thead>'.NB_EOL;
@@ -1280,15 +1281,11 @@ Class Table extends nb {
     $r .= self::form_hidden(array('db','table','format','limit','download'));
     $r .= '<input type="hidden" name="download" value="0"/>'; // Embed for no html format
 
-    // Format
+    // Tables - see default.js if you change class
     $r .= '<span class="label">';
-
-  #$criteria[] = html_select_array(ar_map('array($a == "html" ? "" : $a,strtoupper($a))',$this->formats));
-# NB 01.03.16     $r .= '<label for="format">Format</label>'.html_select_array(array(
-# NB 01.03.16       'table','div','csv','xml','json','yaml'
-    $r .= '<label for="format">Format</label>'.html_select_array($this->db()->formats,array(
-      'html'       => 'class="format" name="format" id="format"',
-      'selected'   => $this->p('format'),
+    $r .= '<label for="table">Tables</label>'.html_select_array(array_keys($this->db()->tables()),array(
+      'html'       => 'class="tables" name="table" id="table"',
+      'selected'   => $this->p('table'),
       'prettyText' => true,
     ));
     $r .= '</span>';
@@ -1304,11 +1301,15 @@ Class Table extends nb {
       $r .= '</span>';
     }
 
-    // Tables - see default.js if you change class
+    // Format
     $r .= '<span class="label">';
-    $r .= '<label for="table">Tables</label>'.html_select_array(array_keys($this->db()->tables()),array(
-      'html'       => 'class="tables" name="table" id="table"',
-      'selected'   => $this->p('table'),
+
+  #$criteria[] = html_select_array(ar_map('array($a == "html" ? "" : $a,strtoupper($a))',$this->formats));
+# NB 01.03.16     $r .= '<label for="format">Format</label>'.html_select_array(array(
+# NB 01.03.16       'table','div','csv','xml','json','yaml'
+    $r .= '<label for="format">Format</label>'.html_select_array($this->db()->formats,array(
+      'html'       => 'class="format" name="format" id="format"',
+      'selected'   => $this->p('format'),
       'prettyText' => true,
     ));
     $r .= '</span>';
@@ -1380,7 +1381,6 @@ Class Table extends nb {
 # NB 28.03.16       'replace',
       'extras',
       #'params',
-      'orderby',
       'count',
       'engine',
       'created',
index 00a754d7d0084f6c50ecf8ee84f23f712ef3f6ef..5270796add8b3e6570bfa5565a608db794a8980d 100644 (file)
@@ -13,20 +13,33 @@ $DB_TYPES['mysql'] = array (
 'tables' => 'SELECT TABLE_NAME as name,LOWER(IF(TABLE_TYPE=\'BASE TABLE\',\'TABLE\',TABLE_TYPE)) as type,ENGINE as engine,CREATE_TIME as created FROM information_schema.tables WHERE TABLE_SCHEMA=DATABASE()',
 
 'status' => array (
+  'SHOW MASTER STATUS' => function(&$r) {
+    $row = array();
+    foreach ($r as $k=>$v) {
+      if (preg_match('/[A-Za-z]/',$k) and $v!=='') $row["MASTER.$k"] = $v;
+    }
+    return $row;
+  },
+  'SHOW SLAVE STATUS' => function($r) { $row = array(); foreach (array(
+    'Seconds_Behind_Master',
+    'Master_Host',
+    'Slave_IO_State',
+    'Master_Log_File',
+    'Read_Master_Log_Pos',
+    'Relay_Log_File',
+    'Relay_Log_Pos',
+    'Relay_Master_Log_File',
+    'Master_Server_Id',
+    ) as $k) { if (isset($r[$k])) $row["SLAVE.$k"] = $r[$k]; } return $row;
+  },
   'SHOW GLOBAL STATUS' => create_function('&$r',join('',array(
     'if (preg_match("/(Threads.*|Conn.*|Max_used_connections|Table_.*|Open.*_tables)/",$r["Variable_name"]))'
-      .'return array("GLB.".$r[0]=>$r[1]);'
+      .'return array("GLOBAL.".$r[0]=>$r[1]);'
     #'if (preg_match("/(Threads_connected|Max_used_connections|Table_locks_immediate|Table_locks_waited|Open.*_tables)/",$r[0])) return $r;',
   ))),
   'SHOW VARIABLES' => create_function('&$r',join('',array(
-    'if (preg_match("/^(Table|.*[Cc]onn|Open|Com).*/",$r["Variable_name"]))'
-      .'return array("VAR.".$r[0]=>$r[1]);'
-  ))),
-  'SHOW SLAVE STATUS' => create_function('&$r',join('',array(
-    'foreach ($r as $k=>$v) {',
-      'if (!preg_match("/^(Seconds_Behind_Master)$/",$k)) unset($r[$k]);',
-    '}',
-    'return $r;',
+    'if (preg_match("/^(Table|.*[Cc]onn|Open|Com|hostname).*/",$r["Variable_name"]))'
+      .'return array("VARIABLES.".$r[0]=>$r[1]);'
   ))),
 ),
 
index 411bbb4d6df3594dc74db2c01e9b84fd2ab122ae..727b8ba99878aa5f009ec3b57e20c65782fb9cbe 100644 (file)
@@ -149,10 +149,13 @@ function debug($msg,$level=0) {
 }
 
 function prettyText($text) {
+  $replace = array(
+    '/(\w)(,)(\w)/' => '$1$2 $3',
+    '/_/' => ' ',
+    '/[^\w,]+/' => ' ',
+  );
   return ucwords(
-    preg_replace('/\W+/',' ',
-      str_replace('_',' ',$text)
-    )
+    preg_replace(array_keys($replace),array_values($replace),$text)
   );
 }
 
index 9dab24feb3848cff3c2b26ac3cf29d7dd42c81b7..49baa7b74f3ebcaf4aeb71b8bae30317dd5683ae 100644 (file)
@@ -35,7 +35,12 @@ class NB {
     }
   }
 
-  public function __sleep() { return array_keys((array)$this); }
+  public function __sleep() {
+    return(get_class_vars(get_class($this))); # get all
+# NB 09.04.16     return array_keys((array)$this);
+    #debug((array)$this); # publics + private in a weird way !
+    #debug(get_object_vars($this)); # non static
+  }
   #public function __wakeup() { }
 
   public function __destruct() {
@@ -54,6 +59,16 @@ class NB {
     return isset($_REQUEST[$name]) ? $_REQUEST[$name] : $default;
   }
 
+  /*
+   * Replace aliases by long name
+   */
+  public static function paliases($aliases) {
+    foreach ($aliases as $short=>$long) {
+      DB::pdef($long,Db::p($short));
+      Db::pset($short,null);
+    }
+  }
+
   /*
    * Set a default's param value, if not set
    * Return the value set or existing
@@ -303,13 +318,12 @@ class NB {
 
     $debug = debug_backtrace(); array_shift($debug);
 
-    $tot = count($debug);
-    foreach (array_reverse($debug) as $i=>$call_info) {
+    for ($i=0;$i<$deep;$i++) {
+      array_shift($debug);
+    }
 
+    foreach ($debug as $i=>$call_info) {
       $msg .= ( $one_line ? ($preff !=='' ? $preff : ' | ') : "\n$preff " ) . self::debug_backtrace_info($call_info);
-
-      if ($deep and ($tot-$i) <= $deep+1) break;
-
     }
 
                return $msg;
index d53b177531c3542f5d1634f6593592d2d4041f1d..260a1600cacb35069801c56f36188de854f9335a 100644 (file)
@@ -131,7 +131,6 @@ Class Out extends Nb {
     }
 
     # For sprintf
-#bye($head);
     unset($o['head_max_len']);
 # NB 01.04.16     if (!empty($head)) {
 # NB 01.04.16       $o['head_max_len'] = max(self::ar_map('strlen($a)',$head));
@@ -187,8 +186,8 @@ Class Out extends Nb {
     #bye($row);
     }
 
-    if (!self::is_hash($row)) unset($o['head_max_len']);
-    if (is_array($row) and count($row)>1) $o['head_max_len'] = strlen(count($row));
+# NB 10.04.16     if (!self::is_hash($row)) unset($o['head_max_len']);
+# NB 10.04.16     if (is_array($row) and count($row)>1) $o['head_max_len'] = strlen(count($row));
     #if (!self::is_hash($row)) return out_yaml($row,$o);
 
     # text
@@ -293,6 +292,15 @@ Class Out extends Nb {
     return $v;
   }
 
+  public static function is_html($type=null) {
+    if ($type === null) $type = self::$type;
+    #return self::type($type,'is_html')
+    return (strpos($type,'html') !==false or (
+      !empty(self::$types[$type]['is_html'])
+      #and !empty(self::$types[$type]['is_xml'])
+    ));
+  }
+
   public static function escape($v,$type=null,$charset=null) {
     if ($charset === null) $type = self::$charset;
 
index 564d7ab420a4b94553499ffcb1567916eb659a03..e839c980de31648ca1c31480e78e5d72b81b1523 100644 (file)
@@ -455,19 +455,64 @@ class Page extends nb {
     return $Page;
   }
 
+  public function phpinfo() {
+    ob_start();
+    phpinfo(INFO_ALL - INFO_LICENSE);
+    $phpinfo = ob_get_contents();
+    ob_end_clean();
+
+    # Only tags without attrs
+    $phpinfo = preg_replace('/(<\w+)\s+[^>]+/','$1',$phpinfo);
+
+    # No anchors
+    $phpinfo = preg_replace('@</?a>@','',$phpinfo);
+
+    # Delete header that could break html columns
+    $phpinfo = preg_replace('@<tr><th>.*?</tr>@','',$phpinfo);
+
+    # Add class
+    $phpinfo = str_replace('<table>','<table class="block">',$phpinfo);
+    #$phpinfo = str_replace('<tr>','<tr class="row">',$phpinfo);
+
+    # Pretty h2
+    $phpinfo =  preg_replace_callback ('@(<h2>(?:<[^>]+>)?)([^<]+)@',function($m){return $m[1].prettyText($m[2]);},$phpinfo);
+
+    if (!preg_match_all('@(<h2.*?</table>)@s',$phpinfo,$m)) return '';
+    return join("\n",$m[0]);
+  }
+
+  public function infos() {
+    $out = array(
+      'path' => $this->path()
+    );
+    foreach (get_class_vars(get_class($this)) as $k=>$v) {
+      if (!empty($this->$k)) $out[$k] = is_scalar($this->$k) ? $this->$k : out::scalar($this->$k);
+    }
+    foreach (self::p() as $k=>$v) $out['p.'.$k] = $v;
+    #foreach ($_SERVER as $k=>$v) $out['_SERVER.'.$k] = $v;
+    #foreach ($_ENV as $k=>$v) $out['_ENV.'.$k] = $v;
+
+    $out = array_map(function($k, $v) { return array("name"=>$k,"value"=>$v); },array_keys($out),$out);
+    return $out;
+  }
+
   public function action() {
     $action = self::p('action');
     $format = self::p('format',self::php_cli() ? 'csv' : 'table');
+    $is_html = out::is_html($format);
 
-    if ($action == 'page.info') {
-      $out = array('path' => $this->path());
-      foreach (get_object_vars($this) as $k=>$v) { if (!empty($this->$k)) $out[$k] = $this->$k; }
-      $out['p'] = self::p();
-      $out['_SERVER'] = $_SERVER;
-
-      $out = array_map(function($k, $v) { return array("name"=>$k,"value"=>$v); },array_keys($out),$out);
+    if ($action == 'page.infos') {
+      $out = $this->infos();
       out::rows($format,$out);
       return true;
+
+    } elseif ($action == 'page.phpinfo' or $action == 'phpinfo') {
+      echo $this->phpinfo(); return true;
+
+    } elseif (preg_match('/^page\.(\w+)/',$action,$m)) {
+#bye($this->$m[1]());
+      if ($rows = $this->$m[1]()) return $this->out2($rows,(is_scalar($rows) ? $m[1] : array()));
+
     }
 
     return false;