From 77a730133cbafd025bd69e498995f2aa722c1a8c Mon Sep 17 00:00:00 2001 From: Nicolas Boisselier Date: Sun, 8 Jan 2017 03:31:31 +0000 Subject: [PATCH] Bed --- bin/dbq | 3 +- etc/profile.d/dbq.sh | 4 ++ etc/profile.d/functions | 2 +- lib/php/db/field.php | 4 +- lib/php/db/table.php | 12 +++-- lib/php/http.php | 2 + lib/php/out.php | 14 +++--- share/db/sys.sql | 30 +++++++++++++ share/db/update.sh | 88 +++++++++++++++++++++++++++++++++---- src/Docker/php/Dockerfile | 1 + www/dbq/dbq.php | 9 ++-- www/dbq/docker-compose.yaml | 3 +- www/dbq/html/default.css | 4 +- 13 files changed, 144 insertions(+), 32 deletions(-) create mode 100644 share/db/sys.sql diff --git a/bin/dbq b/bin/dbq index 72c74ef3..895373e0 100755 --- a/bin/dbq +++ b/bin/dbq @@ -44,9 +44,9 @@ my %CMD_ALIASE = ( # NB 09.04.16 'h' => 'header', ); -## Vim: r!% --curl_help_hash | grep X my @H; my %CURL_OPT = ( +#>_SHELL_REPLACE dbq --curl_help 'compressed|z!' => 1, # Request compressed response (using deflate or gzip) 'dump-header|D=s' => undef, # FILE Write the headers to FILE 'get|G!' => undef, # Send the -d data with a HTTP GET (H) @@ -56,6 +56,7 @@ my %CURL_OPT = ( 'insecure|k!' => undef, # Allow connections to SSL sites without certs (H) 'user-agent|A=s' => undef, # STRING User-Agent to send to server (H) 'user|u=s' => undef, # USER[:PASSWORD] Server user and password +#<_SHELL_REPLACE ); my @CURL_OPT = keys %CURL_OPT; my $CURL_OPT_EXP = join('|',map{ /^(.*?)\|/ ? $1 : $_ } @CURL_OPT); diff --git a/etc/profile.d/dbq.sh b/etc/profile.d/dbq.sh index 21d483e9..e6e6f106 100644 --- a/etc/profile.d/dbq.sh +++ b/etc/profile.d/dbq.sh @@ -11,3 +11,7 @@ dbq2mysql() { dbq2pgsql() { dbq f=txt a=db.dump db.type=pgsql $@ } + +dbq_data_src() { + eval $(dbq t=data_src header=0 format=csv rows.fields=src name=$1) +} diff --git a/etc/profile.d/functions b/etc/profile.d/functions index 6895df0e..00c6a7cf 100644 --- a/etc/profile.d/functions +++ b/etc/profile.d/functions @@ -774,7 +774,7 @@ http_get() { echo "Can't find web client" 1>&2 fi } -alias Get=http_get +# NB 07.01.17 alias Get=http_get http_head() { if which curl 1>/dev/null; then diff --git a/lib/php/db/field.php b/lib/php/db/field.php index ba96d608..6ddc1912 100644 --- a/lib/php/db/field.php +++ b/lib/php/db/field.php @@ -75,14 +75,14 @@ class field extends nb { public function html_edit($value,$type=null) { $size = ($this->size() and is_scalar($this->size())) ? $this->size() : 0; - $tag = ( ($size>$this->textarea_size) or preg_match('/^(text|binary)/i',$this->type) ) ? 'textarea' : 'input'; + $tag = ( ($size>$this->textarea_size) or preg_match('/^(text|binary|blob)/i',$this->type) ) ? 'textarea' : 'input'; return '
' .($this->extras ? ''.$this->out($value).'' : '<'.$tag .' name="'.$this->name.'"' .' id="'.$this->name.'"' - .( $size ? ' size="'.$size.'"' : '') + .( ($size and $tag != 'textarea') ? ' size="'.$size.'"' : '') #.' value="'.$value.'"' .($tag == 'textarea' ? '>'.$value.'' : ' type="' .($type ? $type : $this->html_type()) diff --git a/lib/php/db/table.php b/lib/php/db/table.php index e4c9c39a..e8ca3335 100644 --- a/lib/php/db/table.php +++ b/lib/php/db/table.php @@ -103,11 +103,11 @@ Class Table extends nb { } # NB 22.12.16: TODEL - $this->show_header = - (bool)$this->p('header',$this->show_header) - ; - # TODE FOR out:: + $this->show_header = (bool)$this->p('header',$this->show_header); + + # TODEL WHEN NOT NEEDED ???? $this->pdef('header',$this->show_header); + #bye((int)$this->show_header); } @@ -895,7 +895,11 @@ Class Table extends nb { # Run query # $this->create_temporary(); + + $this->show_header = (bool)$this->p('rows.header',$this->show_header); $this->db()->out->header($this->show_header); + # TODEL WHEN NOT NEEDED ???? + $this->pdef('header',$this->show_header); if ($opt_by_val !== null) $opt = $opt_by_val; diff --git a/lib/php/http.php b/lib/php/http.php index 9b1ff0e6..a6975ed0 100644 --- a/lib/php/http.php +++ b/lib/php/http.php @@ -84,6 +84,8 @@ class Http { '525' => 'SSL Handshake Failed', '526' => 'Invalid SSL Certificate', '527' => 'Railgun Error', +'103' => 'Checkpoint', +'419' => 'I', # [], # Code still in db/table.php ! @@ -99,6 +99,8 @@ Class Out extends Nb { foreach ($r as $k=>$v) { if (!preg_match('/^[a-zA-Z\w_]+$/',$k)) continue; $v = str_replace('"','\\"',$v); + $v = str_replace('$','\\$',$v); + $v = str_replace('`','\\`',$v); $line[] = $o['preff']."$k=\"$v\""; } if ($line) echo join(' ',$line).";\n"; @@ -208,6 +210,7 @@ Class Out extends Nb { foreach ([ 'charset', 'type', + 'header', ] as $k) { if (isset($opt[$k])) self::$k($opt[$k]); unset($opt[$k]); @@ -221,6 +224,8 @@ Class Out extends Nb { } public static function charset($set=null) { if (!empty($set)) self::$charset = $set; return self::$charset; } + public static function type($set=null) { if (!empty($set)) self::$type = $set; return self::$type; } + public static function header($set=null) { if (!empty($set)) self::$header = $set; return self::$header; } public static function types($type=null) { if ($type === '_web_') return array_filter(self::$types,function($v){return !isset($v['_web_']) or (bool)$v['_web_'];}); @@ -228,9 +233,6 @@ Class Out extends Nb { return self::$types; } - public static function type($set=null) { if (!empty($set)) self::$type = $set; return self::$type; } - public static function header($set=null) { if (!empty($set)) self::$header = $set; return self::$header; } - public static function scalar($v) { if ($v === null) return ''; if (is_scalar($v)) return $v; diff --git a/share/db/sys.sql b/share/db/sys.sql new file mode 100644 index 00000000..d86cbb50 --- /dev/null +++ b/share/db/sys.sql @@ -0,0 +1,30 @@ +--DROP TABLE IF EXISTS service; +CREATE TABLE IF NOT EXISTS service ( + name VARCHAR(100), + description VARCHAR(200), + PRIMARY KEY (port,name) +); + +--DROP TABLE IF EXISTS service_port; +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 +); diff --git a/share/db/update.sh b/share/db/update.sh index 6d96a769..8dc00cd4 100755 --- a/share/db/update.sh +++ b/share/db/update.sh @@ -1,23 +1,93 @@ #!/usr/bin/env bash --login -set -e -cd "$(dirname "$0")" +set +e +dir=$(dirname "$0") +tmp=`mktemp -d` + +csv2table() { + local name="$1"; shift + local file="$1"; shift + + cat << EOF +DELETE FROM _var; +DROP TABLE IF EXISTS ${name}_tmp; +CREATE TEMPORARY TABLE ${name}_tmp AS SELECT * FROM $name WHERE 0; + +INSERT INTO _var VALUES('pre',(SELECT count(*) FROM ${name})); + +-- SELECT 'Update $name ('||(SELECT count(*) FROM ${name})||')'; + +BEGIN TRANSACTION; +.import "$file" ${name}_tmp +COMMIT; +INSERT INTO _var VALUES('import',(SELECT count(*) FROM ${name}_tmp)); + +INSERT OR IGNORE INTO $name SELECT * FROM ${name}_tmp; +INSERT INTO _var VALUES('post',(SELECT count(*) FROM ${name})); + +SELECT 'Update sys:$name (' + || ((SELECT v FROM _var WHERE k='post')-(SELECT v FROM _var WHERE k='pre')) + || '/' + ||(SELECT v FROM _var WHERE k='import') +||')' +||' = ('||(SELECT count(*) FROM ${name})||')' +; +EOF +} + +data_src() { + #pwd + + perl -ne 'm,^(\w+)\s+\d+/\w+\s+#\s*(.*?)$, and $_=join(qq|\t|,map(/^\s*(.*?)\s*$/,$1,$2)).qq|\n| and !$h{$_}++ and print' \ + /etc/services \ + > "$tmp/service.csv" + 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" + + 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" + + curl -s http://pgl.yoyo.org/adservers/serverlist.php?hostformat=nohtml \ + > "$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" + +} + ( +cat "$dir/sys.sql" cat << EOF PRAGMA foreign_keys=OFF; -BEGIN TRANSACTION; +.separator \t +CREATE TEMPORARY TABLE _var (k varchar(10),v varhcar(10)); -SELECT 'Update sys.shell_function'; -`shell_functions2sql shell_function` +`data_src` +SELECT 'Update sys:shell_function'; +BEGIN TRANSACTION; +`shell_functions2sql shell_function` COMMIT; + +VACUUM; EOF -) | sqlite3 sys.db +) | sqlite3 "$dir/sys.db" + +#cp -a "$tmp/" /Users/nico/Downloads/commit/ +rm -rf "$tmp" -IFS=$'\n' -for f in $(grep -lrI SHELL_REPLACE $NB_ROOT); do +for f in $(grep -ErlI '^.{1,3}Welcom to php:7.0-fpm-alp EXPOSE 9000 #CMD ["/usr/local/bin/php", "-a"] +#ENTRYPOINT printf "[www]\n\nuser = $USER\n" > /usr/local/etc/php-fpm.d/www.conf.default && /usr/local/sbin/php-fpm ENTRYPOINT /usr/local/sbin/php-fpm diff --git a/www/dbq/dbq.php b/www/dbq/dbq.php index 5a48eb48..bad188c5 100644 --- a/www/dbq/dbq.php +++ b/www/dbq/dbq.php @@ -24,6 +24,7 @@ class DbQ extends nb { public $uri_params; public $param_args_sep = '|'; public $param_exp_value = '[\w\._:-]{2,100}'; + const ACTIONS_NO_TITLE = ['ls','vi']; const PARAM_DB_DEFAULT = 'ls'; public $params = [ 'format' => '', @@ -220,7 +221,7 @@ class DbQ extends nb { if (!$keys) $keys = $all; $keys = array_keys($keys); - $values = explode($this->param_args_sep,$this->params['args']); + $values = $add ? array_fill(0,count($keys),'') : explode($this->param_args_sep,$this->params['args']); $this->table->html_edit(array_combine($keys,$values),$this->table->base.($add ? 'insert/' : 'update/').$this->params['args'],$add); } @@ -296,12 +297,10 @@ class DbQ extends nb { $params = $this->params; $ext = $this->ext; - /* - $title = array_unique(array_slice(array_values($params),1)); - */ $title = array_filter(array_unique(array_slice(array_values($params),1)), function($v){ - return($v==self::PARAM_DB_DEFAULT ? '' : $v); + return(in_array($v,self::ACTIONS_NO_TITLE) ? '' : $v); +# NB 07.01.17 return($v==self::PARAM_DB_DEFAULT ? '' : $v); } ); diff --git a/www/dbq/docker-compose.yaml b/www/dbq/docker-compose.yaml index cbfb0328..e357eabb 100644 --- a/www/dbq/docker-compose.yaml +++ b/www/dbq/docker-compose.yaml @@ -16,7 +16,8 @@ services: - $HOME/.dbq:/etc/dbq - $HOME/ownCloud/var/lib/sqlite:/var/lib/sqlite # NB 16.12.16 network_mode: "host" - entrypoint: sh -c "echo . $NB_ROOT/etc/profile > /etc/profile.d/nb.sh && /usr/local/sbin/php-fpm" + #entrypoint: sh -c "echo . $NB_ROOT/etc/profile > /etc/profile.d/nb.sh && /usr/local/sbin/php-fpm" + entrypoint: sh -c "echo . $NB_ROOT/etc/profile > /etc/profile.d/nb.sh && echo _www:x:50:www-data >> /etc/group && /usr/local/sbin/php-fpm" #entrypoint: "/usr/local/sbin/php-fpm" nginx: diff --git a/www/dbq/html/default.css b/www/dbq/html/default.css index aeafd5ad..f4cc3627 100644 --- a/www/dbq/html/default.css +++ b/www/dbq/html/default.css @@ -54,6 +54,7 @@ input[type=''] { border: solid 1px #ddd; padding: 0.3em 0.4em; } + input[size] { max-width: 70%; } @@ -222,6 +223,3 @@ form.edit .fields .label span { text-align: left; } -form.edit .fields input, form.edit .fields textarea, form.edit .fields select { - width: 65%; -} -- 2.47.3