From 30daf00bcebc9bb3c67477dfab8ef08f675d9eb4 Mon Sep 17 00:00:00 2001 From: Nicolas Boisselier Date: Thu, 14 Dec 2017 06:42:39 +0000 Subject: [PATCH] lib/php/out.php --- lib/php/nb.php | 16 +++- lib/php/out.php | 153 ++------------------------------- lib/php/out/bin.php | 13 +++ lib/php/out/csv.php | 9 ++ lib/php/out/cust.php | 54 ++++++++++++ lib/php/out/human.php | 12 +++ lib/php/out/json.php | 10 +++ lib/php/out/php.php | 21 +++++ lib/php/out/php_hash.php | 3 + lib/php/out/php_print_r.php | 3 + lib/php/out/php_var_dump.php | 3 + lib/php/out/php_var_export.php | 3 + lib/php/out/raw.php | 13 +++ lib/php/out/sh.php | 20 +++++ lib/php/out/sql.php | 4 + lib/php/out/txt.php | 9 ++ lib/php/out/yaml.php | 11 +++ www/dbq/dbq.php | 5 +- 18 files changed, 212 insertions(+), 150 deletions(-) create mode 100644 lib/php/out/bin.php create mode 100644 lib/php/out/csv.php create mode 100644 lib/php/out/cust.php create mode 100644 lib/php/out/human.php create mode 100644 lib/php/out/json.php create mode 100644 lib/php/out/php.php create mode 100644 lib/php/out/php_hash.php create mode 100644 lib/php/out/php_print_r.php create mode 100644 lib/php/out/php_var_dump.php create mode 100644 lib/php/out/php_var_export.php create mode 100644 lib/php/out/raw.php create mode 100644 lib/php/out/sh.php create mode 100644 lib/php/out/sql.php create mode 100644 lib/php/out/txt.php create mode 100644 lib/php/out/yaml.php diff --git a/lib/php/nb.php b/lib/php/nb.php index f139d6ec..dcd41f29 100644 --- a/lib/php/nb.php +++ b/lib/php/nb.php @@ -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 = []; diff --git a/lib/php/out.php b/lib/php/out.php index 2b762031..5f4c9143 100644 --- a/lib/php/out.php +++ b/lib/php/out.php @@ -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' ? [] : [ - ''.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);},], +# [ '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 index 00000000..d3bad53b --- /dev/null +++ b/lib/php/out/bin.php @@ -0,0 +1,13 @@ + 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 index 00000000..f9b88d61 --- /dev/null +++ b/lib/php/out/csv.php @@ -0,0 +1,9 @@ + 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 index 00000000..e84560e0 --- /dev/null +++ b/lib/php/out/cust.php @@ -0,0 +1,54 @@ + [ +# 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 index 00000000..7bb002b7 --- /dev/null +++ b/lib/php/out/human.php @@ -0,0 +1,12 @@ + 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 index 00000000..1ddbcfc2 --- /dev/null +++ b/lib/php/out/json.php @@ -0,0 +1,10 @@ + 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 index 00000000..614ec810 --- /dev/null +++ b/lib/php/out/php.php @@ -0,0 +1,21 @@ + (self::p('enclose') === '0' ? [] : [ + ''.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 index 00000000..1911ee3e --- /dev/null +++ b/lib/php/out/php_hash.php @@ -0,0 +1,3 @@ + ' => ', '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 index 00000000..b570e343 --- /dev/null +++ b/lib/php/out/php_print_r.php @@ -0,0 +1,3 @@ + 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 index 00000000..ca3918c7 --- /dev/null +++ b/lib/php/out/php_var_dump.php @@ -0,0 +1,3 @@ + 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 index 00000000..cb62df50 --- /dev/null +++ b/lib/php/out/php_var_export.php @@ -0,0 +1,3 @@ + function(&$o,&$r) {var_export($r);},] +?> diff --git a/lib/php/out/raw.php b/lib/php/out/raw.php new file mode 100644 index 00000000..ec9b29ae --- /dev/null +++ b/lib/php/out/raw.php @@ -0,0 +1,13 @@ + 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 index 00000000..ac0dbc76 --- /dev/null +++ b/lib/php/out/sh.php @@ -0,0 +1,20 @@ + 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 index 00000000..eb273a91 --- /dev/null +++ b/lib/php/out/sql.php @@ -0,0 +1,4 @@ + diff --git a/lib/php/out/txt.php b/lib/php/out/txt.php new file mode 100644 index 00000000..6f80d1f3 --- /dev/null +++ b/lib/php/out/txt.php @@ -0,0 +1,9 @@ + 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 index 00000000..6b6a5c00 --- /dev/null +++ b/lib/php/out/yaml.php @@ -0,0 +1,11 @@ + ["---\n",""], + 'row' => function (&$o,&$row) { + $yaml = '- '.self::yaml_encode($row); + $yaml = preg_replace("/^(?!-)/m"," ",$yaml); + echo $yaml; + } +]; +?> diff --git a/www/dbq/dbq.php b/www/dbq/dbq.php index c2846726..cf063485 100644 --- a/www/dbq/dbq.php +++ b/www/dbq/dbq.php @@ -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') { -- 2.47.3