From: Nicolas Boisselier Date: Wed, 6 Dec 2017 00:39:01 +0000 (+0000) Subject: lib/php/out.php X-Git-Url: https://git.nbdom.net/?a=commitdiff_plain;h=3aa68441bcf55e3b9e7abd5736411a082eede441;p=nb.git lib/php/out.php --- diff --git a/etc/dbq/000-local.php b/etc/dbq/000-local.php index a0c97f65..1a86220a 100644 --- a/etc/dbq/000-local.php +++ b/etc/dbq/000-local.php @@ -3,11 +3,11 @@ # !!! MANDATORY !!! # $DBQ = [ - 'nb-sys' => [ + 'nb' => [ 'type' => 'sqlite', - 'name' => 'Sys', + 'name' => 'Nb', 'default_table' => 'mime', - 'host' => nb::ROOT_DIR.'/share/db/sys.db', + 'host' => nb::ROOT_DIR.'/share/db/nb.db', ], ]; ?> diff --git a/etc/profile.d/nb.sh b/etc/profile.d/nb.sh index 43d68cc5..d1038e55 100644 --- a/etc/profile.d/nb.sh +++ b/etc/profile.d/nb.sh @@ -1,3 +1,5 @@ +export NB_DB=nb + nb_repos_commit_push() { shell_help "Commit each repos whith a comment" "$@" && return local status diff --git a/etc/vim/source/vars.vim b/etc/vim/source/vars.vim index ecfb0c12..41dee9cd 100644 --- a/etc/vim/source/vars.vim +++ b/etc/vim/source/vars.vim @@ -3,7 +3,7 @@ " " Command to check files per file types " -">SHELL_REPLACE echo "let mimeCheck = {"; dbq db=nb-sys t=mime -cut 1,3 | sort -k 1 | sed -E -e "s/'/''/g" -e "s/^(.*?)\t(.*)$/ \\\,'\1' : '\2'/" -e "0,/,/s/,//" ; echo "\}" +">SHELL_REPLACE echo "let mimeCheck = {"; dbq db=$NB_DB t=mime -cut 1,3 | sort -k 1 | sed -E -e "s/'/''/g" -e "s/^(.*?)\t(.*)$/ \\\,'\1' : '\2'/" -e "0,/,/s/,//" ; echo "\}" let mimeCheck = { \'bash' : 'bash -c' \,'erb' : 'sh -c \"cat | erb -x - | ruby -c\" <' diff --git a/lib/php/http.php b/lib/php/http.php index 03aa1c41..9b5d5cdf 100644 --- a/lib/php/http.php +++ b/lib/php/http.php @@ -1,7 +1,7 @@ SHELL_REPLACE dbq db=nb-sys t=http_status -cut 1,2 | perl -pe "s/'/\\\\'/; s/^(.*?)\t(.*?)$/ '\$1' => '\$2',/" +#>SHELL_REPLACE dbq db=$NB_DB t=http_status -cut 1,2 | perl -pe "s/'/\\\\'/; s/^(.*?)\t(.*?)$/ '\$1' => '\$2',/" '100' => 'Continue', '101' => 'Switching Protocols', '102' => 'Processing', diff --git a/lib/php/mime.php b/lib/php/mime.php index 0cc80ac9..16a48c77 100644 --- a/lib/php/mime.php +++ b/lib/php/mime.php @@ -1,7 +1,7 @@ SHELL_REPLACE dbq db=nb-sys t=mime_type -cut 2,3 | perl -pe "s/'/\\\\'/; s/^(.*?)\t(.*?)$/ '\$1' => '\$2',/" +#>SHELL_REPLACE dbq db=$NB_DB t=mime_type -cut 2,3 | perl -pe "s/'/\\\\'/; s/^(.*?)\t(.*?)$/ '\$1' => '\$2',/" 'application/applixware' => 'aw', 'application/atom+xml' => 'atom xml', 'application/atomcat+xml' => 'atomcat', diff --git a/lib/php/nb.php b/lib/php/nb.php index 0868fbce..33c43c40 100644 --- a/lib/php/nb.php +++ b/lib/php/nb.php @@ -14,12 +14,13 @@ class NB { const ROOT_DIR = NB_ROOT; const NB_ROOT = NB_ROOT; - # Sql: elect type,ext from mime_type where ext in ('json','csv','txt','yaml','xml','html','doc','xls'); protected static $content_types_aliases = array( + #>_SHELL_REPLACE dbq db=$NB_DB table="SELECT type,ext FROM mime_type WHERE ext IN ('json','csv','txt','yaml','xml','html','doc','xls')" 'text' => 'txt', 'human' => 'txt', 'table' => 'html', 'div' => 'html', + #<_SHELL_REPLACE ); protected static $content_types = array( @@ -101,6 +102,7 @@ class NB { if ($name === null) return isset($_GET) ? $_GET : []; return isset($_GET[$name]) ? $_GET[$name] : $default; } + public static function p($name=null,$default=null) { if(NB_P_GET) return self::_p_get($name,$default); if ($name === null) return isset($_REQUEST) ? $_REQUEST : []; diff --git a/lib/php/out.php b/lib/php/out.php index 24bc5a4f..a9150c32 100644 --- a/lib/php/out.php +++ b/lib/php/out.php @@ -27,6 +27,12 @@ Class Out extends Nb { 'end' => 'out_human_end', ], + 'php_hash' => [ + 'align' => ' => ', + 'eol' => ",\n", + 'quote' => "'", + ], + 'cust' => [ '_web_' => false, 'cust' => self::p('cust',"CHANGE ME"), @@ -271,7 +277,7 @@ Class Out extends Nb { echo $call($o,$row); # Internal method - } elseif(is_scalar($o[$k]) and preg_match('/^out_tag|out_human/',$o[$k])) { + } elseif(is_scalar($o[$k]) and preg_match('/^(out_tag|out_human|out_size)/',$o[$k])) { $f = $o[$k]; echo self::$f($o,$row); @@ -288,7 +294,7 @@ Class Out extends Nb { } public static function head(&$o,$head,$data=[]) { - $o['out_count'] = 0; + $o['row_count'] = 0; # Format $head if ($head !== false) { @@ -327,7 +333,7 @@ Class Out extends Nb { ) return; if (self::type_call('head',$o,$head)) { - $o['out_count']++; + $o['row_count']++; if (isset($o['eol'])) echo $o['eol']; } return $head; @@ -336,8 +342,28 @@ Class Out extends Nb { public static function row(&$o,&$row) { - if ($o['out_count'] and isset($o['rec'])) echo $o['rec']; - if (self::type_call('row',$o,$row)) { + if ($o['row_count'] and isset($o['rec'])) echo $o['rec']; + $o['row_count']++; + + # buffering + if (!empty($o['align'])) { + if (!isset($o['sep'])) $o['sep'] = $o['align']; + if (!isset($o['rows'])) $o['rows'] = []; + if (!isset($o['length'])) $o['length'] = []; + + $tmp_row = $row; + foreach ($tmp_row as $k => $v) { + if (!isset($o['length'][$k])) $o['length'][$k] = 0; + if (!empty($o['quote'])) $tmp_row[$k] = $v = self::quote($v,$o['quote'],isset($o['quote_escape']) ? $o['quote_escape'] : null); + $l = mb_strlen($v,self::$charset); + if ($l > $o['length'][$k]) $o['length'][$k] = $l; + } + + $o['rows'][] = $tmp_row; + return true; + + # row() or defined + } elseif (self::type_call('row',$o,$row)) { $ret = true; # Assume html/xml tag style @@ -351,7 +377,6 @@ Class Out extends Nb { } if (isset($o['eol'])) echo $o['eol']; - $o['out_count']++; return $ret; } @@ -363,9 +388,23 @@ Class Out extends Nb { public static function end(&$o) { #if (isset($o['end'])) echo $o['end']($o); + if (!empty($o['align'])) { + foreach ($o['rows'] as $rec) { + $values = []; + + foreach ($rec as $k => $v) { + $v = self::mb_str_pad($v, $o['length'][$k], ' ', STR_PAD_RIGHT); + $values[] = $v; + } + + echo join($o['sep'],$values) . $o['eol']; + } + return; + } + self::type_call('end',$o); if (isset($o['enclose'])) echo $o['enclose'][1]; - unset($o['out_count']); + unset($o['row_count']); } public static function rows_get($type,$data,$head=array()) { @@ -420,6 +459,7 @@ Class Out extends Nb { # NB 27.09.16 if ($count>0) self::row_end($conf); $count++; + self::row($conf,$row); } @@ -512,6 +552,14 @@ Class Out extends Nb { return $v; } + public static function quote($str,$quote='"',$quote_escape='\\') { + if ($quote === null or $quote == true) $quote = '"'; + if ($quote_escape === null) $quote_escape = '\\'; + $str = str_replace($quote,$quote_escape.$quote,$str); + $str = $quote . $str . $quote; + return $str; + } + public static function row_txt(&$o,&$row) { if (!isset($o['col'])) $o['col'] = ''; self::row_parse($row,$o); @@ -711,7 +759,6 @@ Class Out extends Nb { # header if (!$count) echo $sep_line; -# NB 23.11.17 if ($header or $count) echo '' echo '' . ltrim($o['_human']['sep']) . join($o['_human']['sep'],$values) @@ -733,6 +780,34 @@ Class Out extends Nb { } + private static function out_sized(&$o,&$recs) { + $len = []; + + foreach ($recs as $rec) { + + foreach ($rec as $k => $v) { + if (!isset($len[$k])) $len[$k] = 0; + + $l = mb_strlen($v,self::$charset); + + if ($l > $len[$k]) $len[$k] = $l; + } + + } + + foreach ($recs as $rec) { + $values = []; + + foreach ($rec as $k => $v) { + $v = self::mb_str_pad($v, $len[$k], ' ', STR_PAD_RIGHT); + $values[] = $v; + } + + echo join($o['sep'],$values) . $o['eol']; + } + + } + } Out::init() ; # < Class /****************************************************************************/ diff --git a/share/db/0-clean.sql b/share/db/0-clean.sql new file mode 100644 index 00000000..2c08ca57 --- /dev/null +++ b/share/db/0-clean.sql @@ -0,0 +1,19 @@ +-- NB 05.12.17 DROP TABLE IF EXISTS service; +-- NB 25.10.17 CREATE TABLE IF NOT EXISTS service ( +-- NB 25.10.17 name VARCHAR(100), +-- NB 25.10.17 description VARCHAR(200), +-- NB 25.10.17 PRIMARY KEY (port,name) +-- NB 25.10.17 ); + +-- NB 25.10.17 DROP TABLE IF EXISTS service_port; + +-- NB 25.10.17 DROP TABLE IF EXISTS ovh_dedicated; +-- NB 25.10.17 CREATE TABLE ovh_dedicated ( +-- NB 25.10.17 offer VARCHAR(80), +-- NB 25.10.17 proc VARCHAR(150), +-- NB 25.10.17 cpu VARCHAR(150), +-- NB 25.10.17 ram VARCHAR(150), +-- NB 25.10.17 disk VARCHAR(150), +-- NB 25.10.17 option VARCHAR(100), +-- NB 25.10.17 price float(5,2) +-- NB 25.10.17 ); diff --git a/share/db/http_status.sql b/share/db/http_status.sql new file mode 100644 index 00000000..fa699ae3 --- /dev/null +++ b/share/db/http_status.sql @@ -0,0 +1,6 @@ +-- DROP TABLE IF EXISTS http_status; +CREATE TABLE IF NOT EXISTS http_status ( + id int PRIMARY KEY, + name varchar(100), + description varchar(1000) +); diff --git a/share/db/mime.sql b/share/db/mime.sql new file mode 100644 index 00000000..8d3b086f --- /dev/null +++ b/share/db/mime.sql @@ -0,0 +1,5 @@ +CREATE TABLE IF NOT EXISTS mime ( + id varchar(32) NOT NULL PRIMARY KEY, + name varchar(128), + cmd_check varchar(300) +); diff --git a/share/db/mime_type.sql b/share/db/mime_type.sql new file mode 100644 index 00000000..b960fa0c --- /dev/null +++ b/share/db/mime_type.sql @@ -0,0 +1,6 @@ +CREATE TABLE IF NOT EXISTS mime_type ( + name varchar(128), + type varchar(128) NOT NULL DEFAULT '' PRIMARY KEY, + ext varchar(128) +); +CREATE INDEX IF NOT EXISTS mime_type_ext_idx ON mime_type (ext); diff --git a/share/db/nb.db b/share/db/nb.db new file mode 100644 index 00000000..30236da3 Binary files /dev/null and b/share/db/nb.db differ diff --git a/share/db/port.sql b/share/db/port.sql new file mode 100644 index 00000000..eae612e5 --- /dev/null +++ b/share/db/port.sql @@ -0,0 +1,8 @@ +DROP TABLE IF EXISTS port; +CREATE TABLE IF NOT EXISTS port ( + port INT, + proto VARCHAR(3), + name VARCHAR(100), + PRIMARY KEY (port,proto,name), + FOREIGN KEY(name) REFERENCES service(name) +); diff --git a/share/db/shell_function.sql b/share/db/shell_function.sql new file mode 100644 index 00000000..29e57826 --- /dev/null +++ b/share/db/shell_function.sql @@ -0,0 +1,5 @@ +DROP TABLE IF EXISTS shell_function; +CREATE TABLE shell_function ( + name VARCHAR(255) PRIMARY KEY, + code BLOB +); diff --git a/share/db/sys.sql b/share/db/sys.sql deleted file mode 100644 index d46cb976..00000000 --- a/share/db/sys.sql +++ /dev/null @@ -1,51 +0,0 @@ --- NB 05.12.17 DROP TABLE IF EXISTS service; --- NB 25.10.17 CREATE TABLE IF NOT EXISTS service ( --- NB 25.10.17 name VARCHAR(100), --- NB 25.10.17 description VARCHAR(200), --- NB 25.10.17 PRIMARY KEY (port,name) --- NB 25.10.17 ); - --- NB 25.10.17 DROP TABLE IF EXISTS service_port; -DROP TABLE IF EXISTS port; -CREATE TABLE IF NOT EXISTS port ( - port INT, - proto VARCHAR(3), - name VARCHAR(100), - PRIMARY KEY (port,proto,name), - FOREIGN KEY(name) REFERENCES service(name) -); - --- DROP TABLE IF EXISTS http_status; -CREATE TABLE IF NOT EXISTS http_status ( - id int PRIMARY KEY, - name varchar(100), - description varchar(1000) -); - -DROP TABLE IF EXISTS shell_function; -CREATE TABLE shell_function ( - name VARCHAR(255) PRIMARY KEY, - code BLOB -); - --- NB 25.10.17 DROP TABLE IF EXISTS ovh_dedicated; --- NB 25.10.17 CREATE TABLE ovh_dedicated ( --- NB 25.10.17 offer VARCHAR(80), --- NB 25.10.17 proc VARCHAR(150), --- NB 25.10.17 cpu VARCHAR(150), --- NB 25.10.17 ram VARCHAR(150), --- NB 25.10.17 disk VARCHAR(150), --- NB 25.10.17 option VARCHAR(100), --- NB 25.10.17 price float(5,2) --- NB 25.10.17 ); -CREATE TABLE IF NOT EXISTS mime ( - id varchar(32) NOT NULL PRIMARY KEY, - name varchar(128), - cmd_check varchar(300) -); -CREATE TABLE IF NOT EXISTS mime_type ( - name varchar(128), - type varchar(128) NOT NULL DEFAULT '' PRIMARY KEY, - ext varchar(128) -); -CREATE INDEX IF NOT EXISTS mime_type_ext_idx ON mime_type (ext); diff --git a/share/db/update.sh b/share/db/update.sh index a3d521ac..a169d5e7 100755 --- a/share/db/update.sh +++ b/share/db/update.sh @@ -1,6 +1,6 @@ #!/bin/bash --login set +e -dir=$(dirname "$0") +DIR=$(dirname "$0") tmp=`mktemp -d` csv2table() { @@ -39,38 +39,38 @@ data_src() { # NB 25.10.17 perl -ne 'm,^(\w+)\s+\d+/\w+\s+#\s*(.*?)$, and $_=join(qq|\t|,map(/^\s*(.*?)\s*$/,$1,$2)).qq|\n| and !$h{$_}++ and print' \ # NB 25.10.17 /etc/services \ -# NB 25.10.17 > "$tmp/service.csv" -# NB 25.10.17 csv2table service "$tmp/service.csv" +# NB 25.10.17 > "$TMP/service.csv" +# NB 25.10.17 csv2table service "$TMP/service.csv" perl -ne 'm,^(\w+)\s+(\d+)/(\w+)\s+#\s*(.*?)$, and print join(qq|\t|,map(/^\s*(.*?)\s*$/,$2,$3,$1)).qq|\n|' \ /etc/services \ - > "$tmp/port.csv" - csv2table port "$tmp/port.csv" + > "$TMP/port.csv" + csv2table port "$TMP/port.csv" # NB 25.10.17 html-table2csv https://www.ovh.co.uk/dedicated_servers/|perl -ne \ # NB 25.10.17 '/^\w/ or next; @F=split("\t",$_);splice(@F,6,1); $F[6] =~s/ ex.*$//; $F[6] =~s/[^\d\.]//g; print $_ if $_=join("\t",map{$_ eq "-" ? "" : $_} @F[0..6])."\n" and !$dbl{$_}++' \ -# NB 25.10.17 > "$tmp/ovh_dedicated.csv" -# NB 25.10.17 csv2table ovh_dedicated "$tmp/ovh_dedicated.csv" +# NB 25.10.17 > "$TMP/ovh_dedicated.csv" +# NB 25.10.17 csv2table ovh_dedicated "$TMP/ovh_dedicated.csv" return 0 w3m -dump -cols 999999 https://en.wikipedia.org/wiki/List_of_HTTP_status_codes| \ perl -ne 'next unless /^\d\d\d / .. /^ /;s/.\[\d+\]//g; s/^ *//; s/^(\d+) ([\w _-]+).*?\n$/$1\t$2\t/;print' \ - > "$tmp/http_status.csv" - csv2table http_status "$tmp/http_status.csv" + > "$TMP/http_status.csv" + csv2table http_status "$TMP/http_status.csv" curl -s http://pgl.yoyo.org/adservers/serverlist.php?hostformat=nohtml \ - > "$tmp/adservers.csv" - csv2table adservers "$tmp/adservers.csv" + > "$TMP/adservers.csv" + csv2table adservers "$TMP/adservers.csv" curl -s 'http://www.user-agents.org/' | perl -e '$_=join(qq||,<>); print map {s/^\s*(.*?)(?:.nbsp.)?\s*$/$1/;qq|$_\n|} m,([^<]+),g' - > "$tmp/useragent.csv" - csv2table useragent "$tmp/useragent.csv" + > "$TMP/useragent.csv" + csv2table useragent "$TMP/useragent.csv" } ( -cat "$dir/sys.sql" +cat "$DIR/*.sql" cat << EOF PRAGMA foreign_keys=OFF; .separator \t @@ -86,15 +86,12 @@ COMMIT; VACUUM; EOF -) | sqlite3 "$dir/sys.db" +) | sqlite3 "$DIR/nb.db" -#cp -a "$tmp/" /Users/nico/Downloads/commit/ -rm -rf "$tmp" +rm -rf "$TMP" ( -export DBQ_PARAMS="db=nb-sys" - for f in $(grep -ErlI '^.{1,3}