]> git.nbdom.net Git - nb.git/commitdiff
lib/php/out.php
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Thu, 14 Dec 2017 06:42:39 +0000 (06:42 +0000)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Thu, 14 Dec 2017 06:42:39 +0000 (06:42 +0000)
18 files changed:
lib/php/nb.php
lib/php/out.php
lib/php/out/bin.php [new file with mode: 0644]
lib/php/out/csv.php [new file with mode: 0644]
lib/php/out/cust.php [new file with mode: 0644]
lib/php/out/human.php [new file with mode: 0644]
lib/php/out/json.php [new file with mode: 0644]
lib/php/out/php.php [new file with mode: 0644]
lib/php/out/php_hash.php [new file with mode: 0644]
lib/php/out/php_print_r.php [new file with mode: 0644]
lib/php/out/php_var_dump.php [new file with mode: 0644]
lib/php/out/php_var_export.php [new file with mode: 0644]
lib/php/out/raw.php [new file with mode: 0644]
lib/php/out/sh.php [new file with mode: 0644]
lib/php/out/sql.php [new file with mode: 0644]
lib/php/out/txt.php [new file with mode: 0644]
lib/php/out/yaml.php [new file with mode: 0644]
www/dbq/dbq.php

index f139d6ec6f670f8c19c4519060a2c445a8270c4a..dcd41f29831ee2f75e631242160ab0b30df8a996 100644 (file)
@@ -1064,6 +1064,14 @@ class NB {
     }
   }
 
+       private static function ar_filter_keys($ar,$keys) {
+               $new = [];
+               foreach ($keys as $k) {
+                       if (isset($ar[$k])) $new[$k] = $ar[$k];
+               }
+               return $new;
+       }
+
   public static function ldap_search($o=[]) {
                $host = '127.0.0.1';
                if (preg_match('/^.*?([^\.]+\.[^\.]+)$/',$_SERVER['HTTP_HOST'],$m)) {
@@ -1097,13 +1105,13 @@ class NB {
                }
                $read = ldap_search($connect, $o['base_dn'], $o['filter'], $o['attrs'], $o['limit']) or self::bye(">>Unable to search ldap server<<");
 
+               # ldap_get_entries is shite !! It does not workds with binary datas, put all attrs in lower case 
                $info = [];
                $i = 0;
                $entry = ldap_first_entry($connect, $read);
                do {
 
                        $attributes = ldap_get_attributes($connect, $entry);
-                       #$attributes['count']++;
 
                        for($j=0; $j<$attributes['count']; $j++) {
                                $values = ldap_get_values_len($connect, $entry,$attributes[$j]);
@@ -1115,17 +1123,17 @@ class NB {
                                in_array('dn',!empty($o['attrs']) ? $o['attrs'] : ['dn'])
                        ) $info[$i] = ['dn' => ldap_get_dn($connect,$entry)] + (empty($info[$i]) ? [] : $info[$i]);
 
-                       #$info[$i]['count'] = count($info[$i]);
+                       # Re-order by attrs
+                       if (!empty($o['attrs'])) $info[$i] = self::ar_filter_keys($info[$i],$o['attrs']);
+
                        $i++;
 
                } while ($entry = ldap_next_entry($connect, $entry));
-               #$info['count'] = count($info);
 
                ldap_close($connect);
                #bye($info);
                return $info;
 
-               # NB 13.12.17: ldap_get_entries is shite !! It does not workds with binary datas, put all attrs in lower case 
                $info = ldap_get_entries($connect, $read);
 
                $recs = [];
index 2b762031bb6e6b4d5df1a6dfb2ebddca70a1317e..5f4c91431ef2cea2cfca5c60a68dc616aec2bf74 100644 (file)
@@ -15,100 +15,11 @@ Class Out extends Nb {
 
     self::$types = [
 
-      'sql' => [], # Code still in db/table.php !
 
-      'bin' => [
-        'base64_encode_binary' => false,
-                               'sep' => '',
-                               'eol' => '',
-        'row' => function (&$o,&$row) {
-                                       if (!empty($o['_bin'])) return;
-                                       $o['_bin'] = true;
-                                       foreach ($row as $k=>$v) { echo $v; break; }
-                               },
-                       ],
-
-      'human' => [
-        'sep' => self::p('sep'," | "),
-        'corner' => self::p('corner',"+"),
-        'eol' => self::p('eol',"\n"),
-        'line' => self::p('line',"-"),
-        'head' => 'out_human_head',
-        'row' => 'out_human',
-        'end' => 'out_human_end',
-      ],
-
-      'txt' => [
-        'col' => self::p('col',' : '),
-        'sep' => self::p('sep',"\n"),
-        'eol' => self::p('eol',"--\n"),
-        'init' => function(&$o) { if (self::header()) return; $o['eol'] = ''; },
-      ],
-
-      'csv' => [
-        'sep' => self::punescape('sep',"\t"),
-        'quote' => self::p('quote',''),
-        'quote_escape' => self::punescape('quote_escape','\\'),
-        'eol' => self::punescape('eol',"\n"),
-      ],
-
-      'yaml' => [
-        'enclose' => ["---\n",""],
-        'row' => function (&$o,&$row) {
-          $yaml = '- '.self::yaml_encode($row);
-          $yaml = preg_replace("/^(?!-)/m","  ",$yaml);
-          echo $yaml;
-        }
-      ],
-
-      'json' => [
-        'base64_encode_binary' => true,
-        'enclose' => array('['.self::p('eol',NB_EOL),']'),
-        'row' => function(&$o,&$row) { return self::json_encode($row); },
-        'eol' => self::p('eol',NB_EOL),
-        'rec' => ',',
-      ],
-
-      'sh' => array(
-        'preff' => self::p('preff',""),
-        'row' => function(&$o,&$r) {
-          $line = [];
-          foreach ($r as $k=>$v) {
-            if (!preg_match('/^[a-zA-Z\w_]+$/',$k)) continue;
-            $v = self::scalar($v);
-            $v = str_replace('"','\\"',$v);
-            $v = str_replace('$','\\$',$v);
-            $v = str_replace('`','\\`',$v);
-            $v = str_replace("\r",'',$v);
-            $v = str_replace("\n",'"$\'\\\\n\'"',$v);
-            $line[] = $o['preff']."$k=\"$v\"";
-          }
-          if ($line) echo join(' ',$line).";\n";
-        }
-      ),
-
-      'php' => array(
-        'enclose' => (self::p('enclose') === '0' ? [] : [
-           '<?php'.(NB_EOL ? NB_EOL:' ') . '$VAR = array('
-          ,');' . (NB_EOL ? NB_EOL:' ').'?'.'>'.NB_EOL
-        ]),
-        'eol' => self::p('eol',NB_EOL),
-        'rec' => ',',
-        'row' => function(&$o,&$r) {
-          return var_export($r);
-          echo preg_replace(array(
-            '/(=>\s*)\n\s+/m',
-            '/\),\s*array/m',
-          ),array(
-            '\1',
-            "),".NB_EOL."array"
-          ),var_export($r,true));
-        }
-      ),
-      'php_hash' => [ 'align' => ' => ', 'eol' => ",\n", 'quote' => "'", ],
-      'php_var_dump' => ['row' => function(&$o,&$r) {var_dump($r);},],
-      'php_print_r' => ['row' => function(&$o,&$r) {print_r($r);},],
-      'php_var_export' => ['row' => function(&$o,&$r) {var_export($r);},],
+#<?php
+#if (!class_exists('out')) return;
+#<?php
+#return
 
       'div' => [
         'is_html' => true,
@@ -151,60 +62,12 @@ Class Out extends Nb {
         },
       ],
 
-# NB 14.12.17       'cust' => [
-# NB 14.12.17         '_web_' => false,
-# NB 14.12.17         'cust' => self::p('cust',"CHANGE ME"),
-# NB 14.12.17         'at' => self::p('at','@'),
-# NB 14.12.17         'open' => self::p('open',''),
-# NB 14.12.17         'close' => self::p('close',''),
-# NB 14.12.17 
-# NB 14.12.17         'quote' => self::p('quote',"'"),
-# NB 14.12.17         'quote_escape' => self::p('quote_escape','\\'),
-# NB 14.12.17 
-# NB 14.12.17         'eol' => self::p('eol',"\n"),
-# NB 14.12.17         'sep' => self::p('sep',''),
-# NB 14.12.17         'preff' => self::p('preff',''),
-# NB 14.12.17         'suff' => self::p('suff',''),
-# NB 14.12.17 
-# NB 14.12.17         'end' => function($o) { if (isset($o['count']) and $o['close']) echo $o['close']; },
-# NB 14.12.17         'row' => function(&$o,&$r) {
-# NB 14.12.17             static $count=0;
-# NB 14.12.17             if (!$count) {
-# NB 14.12.17               foreach ($o as $k=>$v) {
-# NB 14.12.17                 if (is_scalar($v)) $o[$k] = str_replace('\n',"\n",$v);
-# NB 14.12.17               }
-# NB 14.12.17               if ($o['open']) echo $o['open'];
-# NB 14.12.17             }
-# NB 14.12.17             $count++; $o['count'] = $count;
-# NB 14.12.17             echo $o['preff']
-# NB 14.12.17               .($count>1 ? $o['sep'] : '')
-# NB 14.12.17               .preg_replace_callback('/'.$o['at'].'([\w_]+)(?::([-\+]?\d+))?'.$o['at'].'/',function($m) use ($o,$r,$count){
-# NB 14.12.17 
-# NB 14.12.17                 #bye($m);
-# NB 14.12.17                 $k = $m[1];
-# NB 14.12.17                 $size = empty($m[2]) ? 0 : (int)$m[2];
-# NB 14.12.17                 $v = $r[$k];
-# NB 14.12.17 
-# NB 14.12.17                 if (!empty($o['quote'])) $v = $o['quote'] .
-# NB 14.12.17                   str_replace($o['quote'],$o['quote_escape'].$o['quote'],$v)
-# NB 14.12.17                 . $o['quote'];
-# NB 14.12.17 
-# NB 14.12.17                 if ($size) {
-# NB 14.12.17                   $align = $size>0 ? STR_PAD_LEFT : STR_PAD_RIGHT;
-# NB 14.12.17                   $size = abs($size);
-# NB 14.12.17                   if ($count>1) $size -= mb_strlen($o['sep'],self::$charset);
-# NB 14.12.17                   $v = self::mb_str_pad($v, $size, ' ',$align);
-# NB 14.12.17                 }
-# NB 14.12.17 
-# NB 14.12.17                 return $v;
-# NB 14.12.17 
-# NB 14.12.17               },$o['cust'])
-# NB 14.12.17               .$o['suff']
-# NB 14.12.17             ;
-# NB 14.12.17         }
-# NB 14.12.17       ],
 
     ];
+               foreach ((array)glob(dirname(__FILE__).'/'.basename(__FILE__,'.php').'/*.php') as $file) {
+                       #debug($file);
+                       self::$types[basename($file,'.php')] = require($file);
+               }
 
     #self::$types['html'] = self::$types['table'];
     self::type(self::client_type());
diff --git a/lib/php/out/bin.php b/lib/php/out/bin.php
new file mode 100644 (file)
index 0000000..d3bad53
--- /dev/null
@@ -0,0 +1,13 @@
+<?php
+if (!class_exists('out')) return;
+return [
+  'base64_encode_binary' => false,
+       'sep' => '',
+       'eol' => '',
+  'row' => function (&$o,&$row) {
+               if (!empty($o['_bin'])) return;
+               $o['_bin'] = true;
+               foreach ($row as $k=>$v) { echo self::scalar($v); break; }
+       },
+];
+?>
diff --git a/lib/php/out/csv.php b/lib/php/out/csv.php
new file mode 100644 (file)
index 0000000..f9b88d6
--- /dev/null
@@ -0,0 +1,9 @@
+<?php
+if (!class_exists('out')) return;
+return [
+  'sep' => self::punescape('sep',"\t"),
+  'quote' => self::p('quote',''),
+  'quote_escape' => self::punescape('quote_escape','\\'),
+  'eol' => self::punescape('eol',"\n"),
+];
+?>
diff --git a/lib/php/out/cust.php b/lib/php/out/cust.php
new file mode 100644 (file)
index 0000000..e84560e
--- /dev/null
@@ -0,0 +1,54 @@
+<?php
+return;
+# NB 14.12.17       'cust' => [
+# NB 14.12.17         '_web_' => false,
+# NB 14.12.17         'cust' => self::p('cust',"CHANGE ME"),
+# NB 14.12.17         'at' => self::p('at','@'),
+# NB 14.12.17         'open' => self::p('open',''),
+# NB 14.12.17         'close' => self::p('close',''),
+# NB 14.12.17 
+# NB 14.12.17         'quote' => self::p('quote',"'"),
+# NB 14.12.17         'quote_escape' => self::p('quote_escape','\\'),
+# NB 14.12.17 
+# NB 14.12.17         'eol' => self::p('eol',"\n"),
+# NB 14.12.17         'sep' => self::p('sep',''),
+# NB 14.12.17         'preff' => self::p('preff',''),
+# NB 14.12.17         'suff' => self::p('suff',''),
+# NB 14.12.17 
+# NB 14.12.17         'end' => function($o) { if (isset($o['count']) and $o['close']) echo $o['close']; },
+# NB 14.12.17         'row' => function(&$o,&$r) {
+# NB 14.12.17             static $count=0;
+# NB 14.12.17             if (!$count) {
+# NB 14.12.17               foreach ($o as $k=>$v) {
+# NB 14.12.17                 if (is_scalar($v)) $o[$k] = str_replace('\n',"\n",$v);
+# NB 14.12.17               }
+# NB 14.12.17               if ($o['open']) echo $o['open'];
+# NB 14.12.17             }
+# NB 14.12.17             $count++; $o['count'] = $count;
+# NB 14.12.17             echo $o['preff']
+# NB 14.12.17               .($count>1 ? $o['sep'] : '')
+# NB 14.12.17               .preg_replace_callback('/'.$o['at'].'([\w_]+)(?::([-\+]?\d+))?'.$o['at'].'/',function($m) use ($o,$r,$count){
+# NB 14.12.17 
+# NB 14.12.17                 #bye($m);
+# NB 14.12.17                 $k = $m[1];
+# NB 14.12.17                 $size = empty($m[2]) ? 0 : (int)$m[2];
+# NB 14.12.17                 $v = $r[$k];
+# NB 14.12.17 
+# NB 14.12.17                 if (!empty($o['quote'])) $v = $o['quote'] .
+# NB 14.12.17                   str_replace($o['quote'],$o['quote_escape'].$o['quote'],$v)
+# NB 14.12.17                 . $o['quote'];
+# NB 14.12.17 
+# NB 14.12.17                 if ($size) {
+# NB 14.12.17                   $align = $size>0 ? STR_PAD_LEFT : STR_PAD_RIGHT;
+# NB 14.12.17                   $size = abs($size);
+# NB 14.12.17                   if ($count>1) $size -= mb_strlen($o['sep'],self::$charset);
+# NB 14.12.17                   $v = self::mb_str_pad($v, $size, ' ',$align);
+# NB 14.12.17                 }
+# NB 14.12.17 
+# NB 14.12.17                 return $v;
+# NB 14.12.17 
+# NB 14.12.17               },$o['cust'])
+# NB 14.12.17               .$o['suff']
+# NB 14.12.17             ;
+# NB 14.12.17         }
+# NB 14.12.17       ],
diff --git a/lib/php/out/human.php b/lib/php/out/human.php
new file mode 100644 (file)
index 0000000..7bb002b
--- /dev/null
@@ -0,0 +1,12 @@
+<?php
+if (!class_exists('out')) return;
+return [
+  'sep' => self::p('sep'," | "),
+  'corner' => self::p('corner',"+"),
+  'eol' => self::p('eol',"\n"),
+  'line' => self::p('line',"-"),
+  'head' => 'out_human_head',
+  'row' => 'out_human',
+  'end' => 'out_human_end',
+];
+?>
diff --git a/lib/php/out/json.php b/lib/php/out/json.php
new file mode 100644 (file)
index 0000000..1ddbcfc
--- /dev/null
@@ -0,0 +1,10 @@
+<?php
+if (!class_exists('out')) return;
+return [
+  'base64_encode_binary' => true,
+  'enclose' => array('['.self::p('eol',NB_EOL),']'),
+  'row' => function(&$o,&$row) { return self::json_encode($row); },
+  'eol' => self::p('eol',NB_EOL),
+  'rec' => ',',
+];
+?>
diff --git a/lib/php/out/php.php b/lib/php/out/php.php
new file mode 100644 (file)
index 0000000..614ec81
--- /dev/null
@@ -0,0 +1,21 @@
+<?php
+if (!class_exists('out')) return;
+return [
+  'enclose' => (self::p('enclose') === '0' ? [] : [
+     '<?php'.(NB_EOL ? NB_EOL:' ') . '$VAR = array('
+    ,');' . (NB_EOL ? NB_EOL:' ').'?'.'>'.NB_EOL
+  ]),
+  'eol' => self::p('eol',NB_EOL),
+  'rec' => ',',
+  'row' => function(&$o,&$r) {
+    return var_export($r);
+    echo preg_replace(array(
+      '/(=>\s*)\n\s+/m',
+      '/\),\s*array/m',
+    ),array(
+      '\1',
+      "),".NB_EOL."array"
+    ),var_export($r,true));
+  }
+];
+?>
diff --git a/lib/php/out/php_hash.php b/lib/php/out/php_hash.php
new file mode 100644 (file)
index 0000000..1911ee3
--- /dev/null
@@ -0,0 +1,3 @@
+<?php
+return [ 'align' => ' => ', 'eol' => ",\n", 'quote' => "'", ];
+?>
diff --git a/lib/php/out/php_print_r.php b/lib/php/out/php_print_r.php
new file mode 100644 (file)
index 0000000..b570e34
--- /dev/null
@@ -0,0 +1,3 @@
+<?php
+return ['row' => function(&$o,&$r) {print_r($r);},];
+?>
diff --git a/lib/php/out/php_var_dump.php b/lib/php/out/php_var_dump.php
new file mode 100644 (file)
index 0000000..ca3918c
--- /dev/null
@@ -0,0 +1,3 @@
+<?php
+return [ 'row' => function(&$o,&$r) {var_dump($r);}];
+?>
diff --git a/lib/php/out/php_var_export.php b/lib/php/out/php_var_export.php
new file mode 100644 (file)
index 0000000..cb62df5
--- /dev/null
@@ -0,0 +1,3 @@
+<?php
+return ['row' => function(&$o,&$r) {var_export($r);},]
+?>
diff --git a/lib/php/out/raw.php b/lib/php/out/raw.php
new file mode 100644 (file)
index 0000000..ec9b29a
--- /dev/null
@@ -0,0 +1,13 @@
+<?php
+if (!class_exists('out')) return;
+return [
+  'base64_encode_binary' => false,
+       'eol' => '',
+       'sep' => self::p('sep','_RAW_'),
+  'init' => function(&$o) { $o['rec'] = $o['sep'].$o['sep']; },
+       #'sep' => '',
+  'row' => function (&$o,&$row) {
+               foreach ($row as $k=>$v) { echo self::scalar($v).$o['sep']; }
+       },
+];
+?>
diff --git a/lib/php/out/sh.php b/lib/php/out/sh.php
new file mode 100644 (file)
index 0000000..ac0dbc7
--- /dev/null
@@ -0,0 +1,20 @@
+<?php
+if (!class_exists('out')) return;
+return [
+  'preff' => self::p('preff',""),
+  'row' => function(&$o,&$r) {
+    $line = [];
+    foreach ($r as $k=>$v) {
+      if (!preg_match('/^[a-zA-Z\w_]+$/',$k)) continue;
+      $v = self::scalar($v);
+      $v = str_replace('"','\\"',$v);
+      $v = str_replace('$','\\$',$v);
+      $v = str_replace('`','\\`',$v);
+      $v = str_replace("\r",'',$v);
+      $v = str_replace("\n",'"$\'\\\\n\'"',$v);
+      $line[] = $o['preff']."$k=\"$v\"";
+    }
+    if ($line) echo join(' ',$line).";\n";
+  }
+];
+?>
diff --git a/lib/php/out/sql.php b/lib/php/out/sql.php
new file mode 100644 (file)
index 0000000..eb273a9
--- /dev/null
@@ -0,0 +1,4 @@
+<?php
+# Code still in db/table.php !
+return [];
+?>
diff --git a/lib/php/out/txt.php b/lib/php/out/txt.php
new file mode 100644 (file)
index 0000000..6f80d1f
--- /dev/null
@@ -0,0 +1,9 @@
+<?php
+if (!class_exists('out')) return;
+return [
+  'col' => self::p('col',' : '),
+  'sep' => self::p('sep',"\n"),
+  'eol' => self::p('eol',"--\n"),
+  'init' => function(&$o) { if (self::header()) return; $o['eol'] = ''; },
+];
+?>
diff --git a/lib/php/out/yaml.php b/lib/php/out/yaml.php
new file mode 100644 (file)
index 0000000..6b6a5c0
--- /dev/null
@@ -0,0 +1,11 @@
+<?php
+if (!class_exists('out')) return;
+return [
+  'enclose' => ["---\n",""],
+  'row' => function (&$o,&$row) {
+    $yaml = '- '.self::yaml_encode($row);
+    $yaml = preg_replace("/^(?!-)/m","  ",$yaml);
+    echo $yaml;
+  }
+];
+?>
index c28467264c09113926bcc1bd31ac4ccea2f2c2b6..cf063485ef1eb3c5742511423ac2f4f644339e67 100644 (file)
@@ -1016,7 +1016,10 @@ EOF;
                        ]);
                        #echo $rows[0]['jpegPhoto']; exit;
                        #$rows = [ 'jpegPhoto' => $rows[0]['jpegPhoto'] ];
-                       if ($this->is_html) $rows = $this->array_fill_assoc($rows);
+                       if ($this->is_html
+                                       or ($this->params['format'] == $this->format_cli)
+                                       or ($this->params['format'] == 'csv')
+                       ) $rows = $this->array_fill_assoc($rows);
                        $this->page($rows);
 
                } elseif ($action == 'phpinfo') {