From 2437d6f896d735f4ea977b56c18e7ff78d0467d6 Mon Sep 17 00:00:00 2001 From: Nicolas Boisselier Date: Fri, 2 Dec 2016 00:55:38 +0000 Subject: [PATCH] nb.rent no more default table --- .gitignore | 4 + etc/dbq/zzz-all.php | 3 + etc/profile.d/docker.sh | 3 +- lib/php/config.php | 5 +- lib/php/db.php | 1 + lib/php/db/config.php | 42 +++++----- lib/php/db/table.php | 153 ++++++++++++++++++++++++++----------- lib/php/nb.php | 7 ++ lib/php/out.php | 11 +++ share/templates/hosts.php | 25 ++++++ share/templates/my.cnf.php | 15 ++-- share/templates/pgpass.php | 15 ++++ 12 files changed, 208 insertions(+), 76 deletions(-) create mode 100644 .gitignore create mode 100644 share/templates/hosts.php create mode 100644 share/templates/pgpass.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..efae21ac --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +*.nogit +*.pyc +.netrwhist +*.swp diff --git a/etc/dbq/zzz-all.php b/etc/dbq/zzz-all.php index abd47f35..fce91a2f 100644 --- a/etc/dbq/zzz-all.php +++ b/etc/dbq/zzz-all.php @@ -82,6 +82,9 @@ foreach ($DBQ as $id => $db) { # NB 21.11.16 conf_merge($DBQ[$fname],$DBQ['all']); } +#if (isset($DBQ['nb']) and isset($DBQ['nb']['default_table'])) + #$DBQ['all']['default_table'] = $DBQ['nb']['default_table'] +#; #debug($DBQ['nb-sqlite']['default_table']); #if (!empty($DBQ['nb-sqlite'])) debug($DBQ['nb-sqlite']); diff --git a/etc/profile.d/docker.sh b/etc/profile.d/docker.sh index 4af53cb3..3cf72499 100644 --- a/etc/profile.d/docker.sh +++ b/etc/profile.d/docker.sh @@ -156,7 +156,8 @@ docker_clean() { local volumes=$(docker volume ls -qf dangling=true 2>/dev/null) [ -n "$volumes" ] && docker volume rm $volumes | sed "s/^/VOLUMES /" - local containers=$(docker ps -f status=dead -f status=exited -f status=created -aq) +# NB 01.12.16 local containers=$(docker ps -f status=dead -f status=exited -f status=created -aq) + local containers=$(docker ps -f status=dead -aq) [ -n "$containers" ] && docker rm -vf $containers | sed "s/^/CONTAINERS /" } diff --git a/lib/php/config.php b/lib/php/config.php index 5e28a570..570d834b 100644 --- a/lib/php/config.php +++ b/lib/php/config.php @@ -7,11 +7,10 @@ */ #define('NB_EOL',''); #define('NB_PROD',true); -require(dirname(__FILE__).'/nb.php'); - if (empty($_SERVER['PROD'])) error_reporting(E_ALL | E_STRICT | E_NOTICE); -ini_set('error_reporting', true); +require(dirname(__FILE__).'/nb.php'); + // Cli if (nb::php_cli()) { argv2request(); diff --git a/lib/php/db.php b/lib/php/db.php index f13b6f49..9f1f6ae5 100644 --- a/lib/php/db.php +++ b/lib/php/db.php @@ -1252,6 +1252,7 @@ class Db extends nb { return $row; } + #public function default_table() { return $this->default_table; } } # < Class ?> diff --git a/lib/php/db/config.php b/lib/php/db/config.php index a5c4f1ba..f7af80e7 100644 --- a/lib/php/db/config.php +++ b/lib/php/db/config.php @@ -1,38 +1,40 @@ p('format'); - if (!$format) bye("Parameter `format` missing!"); + #if (isset($opt['format']) and empty($opt['format'])) { + if (isset($opt['format']) and $opt['format']==='') { + $format = ''; + } else { + + $format = empty($opt['format']) ? $this->p('format') : $opt['format']; + if (!$format) bye("Parameter `format` missing!"); + } list($sql,$where,$limit,$select_count) = $this->rows_sql($opt); $st = $this->db()->conn->prepare($sql);#,[PDO::ATTR_ERRMODE => PDO::ERRMODE_SILENT]); $st->execute(); + if (!empty($opt['query'])) return $st; # # Fields @@ -815,15 +826,18 @@ Class Table extends nb { # $out_conf = null; $opt['use_out'] = false; + if ($this->p('out') or !preg_match('/^('.join('|', - [ 'table','sql','div','wp','_csv','_xml','_json','_yaml' ] # local + [ '','template','table','sql','div','wp','_csv','_xml','_json','_yaml' ] # local ).')$/',$format)) { $opt['use_out'] = true; if (!($out_conf = out::types($format))) $this->bye("Unknow format `$format`"); - out::type($format); - self::$params += array_values(out::types()); + $out_conf = array_merge($opt,$out_conf); + + # Function name should be format + out::type($format); self::$params += array_values(out::types()); if (empty($out_conf['enclose'])) $out_conf['enclose'] = ['','']; debug('Using out module!',3); @@ -865,32 +879,6 @@ Class Table extends nb { $call = null; $call = $this->rows_parsers($row,$opt); - /* - if ($parser) $this->db()->table_row_decrypt($this,$row); - if ($parser and !empty($this->db()->row_parse)) { - $call = $this->db()->row_parse; $call($row); - } - - if ($parser and !empty($this->row_parse_pre)) { - $call = $this->row_parse_pre; $call($row); - } - - if ($opt['is_html'] and !$out_conf) { - foreach ($row as $k=>$v) { - if (!isset($this->extras[$k])) $row[$k] = out::format($v); - } - - } - - if ($parser and !empty($this->row_parse_post)) { - $call = $this->row_parse_post; $call($row); - } - */ - - # - # Close previous - # -# NB 27.09.16 if ($out_conf and $count !== 1) out::row_end($out_conf); # # Fields filter @@ -914,21 +902,17 @@ Class Table extends nb { if ($call) { foreach (array_keys($row) as $name) { -# NB 19.11.16 if (!$this->fields($name)) { if (!isset($fields[$name])) { $fields[$name] = new Field($name); $fields[$name]->dyn = true; -# NB 19.11.16 $this->fields[$name] = $fields[$name]; } } } if ($out_conf) { -# NB 19.11.16 out::head($out_conf,array_keys($this->fields()),[$row]); out::head($out_conf,array_keys($fields),[$row]); } else { -# NB 19.11.16 echo $this->{"rows_begin_$format"}($this->fields(),$opt); echo $this->{"rows_begin_$format"}($fields,$opt); } @@ -949,6 +933,7 @@ Class Table extends nb { if ($out_conf) { out::row($out_conf,$row); + } else { echo $this->{"rows_rec_$format"}($row,$opt); } @@ -996,9 +981,11 @@ Class Table extends nb { } # < is_html - if ($count === 0 and $this->p('header') === 'force') { -# NB 19.11.16 echo $this->{"rows_begin_$format"}($this->fields()); - echo $this->{"rows_begin_$format"}($fields); + if ($count === 0 and ( + $this->p('header') === 'force' + or !$format + )) { + echo $this->{"rows_begin_$format"}($fields,$opt); } if ($out_conf) { @@ -1025,6 +1012,53 @@ Class Table extends nb { return $count; } + /*----------------------------------------------------------------- + No Out + -----------------------------------------------------------------*/ + public function rows_begin_($fields,&$o) { + $o['sep'] = ''; + $o['var'] = [ + 'head' => array_keys($fields), + 'rows' => [], + ]; + } + + public function rows_rec_($row,&$o) { + $o['var']['rows'][] = $row; + } + + public function rows_end_() { + } + + /*----------------------------------------------------------------- + Template + -----------------------------------------------------------------*/ + public function rows_begin_template($fields,&$o) { + if (empty($id) and !( $id=self::p('table.template.id') ) ) self::bye('Wrong parameter!'); + + $id = preg_replace('/[^\w\._-]/','',$id); + + $file = TABLE_TEMPLATE.'/'.$id.'.php'; + if (!is_readable($file)) return false; #self::bye("Wrong id `$id`"); + $o['file'] = $file; + $HEAD = array_keys($fields); + $ROWS = []; + $ROW = []; + require $o['file']; + } + + public function rows_rec_template($row,&$o) { + $HEAD = []; + $ROWS = [$row]; + $ROW = $row; + require $o['file']; + } + + public function rows_end_template() { + unset($this->__file); + unset($this->__fields); + } + /*----------------------------------------------------------------- Wordpress -----------------------------------------------------------------*/ @@ -1526,7 +1560,8 @@ Class Table extends nb { } elseif ($this->p('format') and !preg_match('/^(table|div)$/',$this->p('format'))) { - $this->rows($dummy,['format' => $this->p('format')]); + $opt = ['format' => $this->p('format')]; + $this->rows($opt); return true; } elseif ($action == 'edit') { @@ -1812,5 +1847,31 @@ Class Table extends nb { return $this->status; } + public function template($id=null) { + if (empty($id) and !( $id=self::p('table.template.id') ) ) self::bye('Wrong parameter!'); + + $id = preg_replace('/[^\w\._-]/','',$id); + + $file = TABLE_TEMPLATE.'/'.$id.'.php'; + if (!is_readable($file)) return false; #self::bye("Wrong id `$id`"); + + $opt = [ 'format'=> '' ]; + $this->rows($opt); + + $HEAD = &$opt['var']['head']; + $ROWS = &$opt['var']['rows']; + $ROW = &$opt['var']['rows'][0]; + #$opt['var']['rows']=[]; + + $_REQUEST_BAK = $_REQUEST; + $_REQUEST = array_merge($_REQUEST,$ROW); + + $ex = require $file; + + $_REQUEST = $_REQUEST_BAK; + + #return $ex; + } } # < Class +__table_define(); ?> diff --git a/lib/php/nb.php b/lib/php/nb.php index feb76f1c..b99e1819 100644 --- a/lib/php/nb.php +++ b/lib/php/nb.php @@ -660,15 +660,20 @@ class NB { if ($user === null) $user = ''; if ($key === null) $key = ''; $infos = []; + #bye((array)$infos); + # # Linux standard + # if (function_exists('posix_getuid')) { if ($user==='') $user = posix_getuid(); if (preg_match('/^\d+$/',$user)) $infos = posix_getpwuid($user); else $infos = posix_getpwnam($user); } + # # /etc/passwd + # if (!$infos and $user !== '' and self::osname()!='darwin' @@ -693,7 +698,9 @@ class NB { fclose($handle); } + # # Command + # if (!$infos) { $cmd = $user === '' ? 'id' : sprintf('id %s',$user); $line = []; exec(escapeshellcmd($user === '' ? 'id' : 'id '.escapeshellarg($user)).' 2>/dev/null',$line); diff --git a/lib/php/out.php b/lib/php/out.php index e2ef91eb..f7e3d831 100644 --- a/lib/php/out.php +++ b/lib/php/out.php @@ -112,6 +112,17 @@ Class Out extends Nb { 'eol' => self::p('eol',NB_EOL), 'rec' => ',', 'row' => function(&$o,&$r) { +#bye($o['var_export']); +/* + if (!empty($o['var_export'])) { + if (empty($_GLOBAL['ROWS'])) { + $_GLOBAL['ROWS'] = []; + $o['rec'] = ''; + } + $_GLOBAL['ROWS'][] = $r; + return; + } + */ return var_export($r); echo preg_replace(array( '/(=>\s*)\n\s+/m', diff --git a/share/templates/hosts.php b/share/templates/hosts.php new file mode 100644 index 00000000..47e33df1 --- /dev/null +++ b/share/templates/hosts.php @@ -0,0 +1,25 @@ + diff --git a/share/templates/my.cnf.php b/share/templates/my.cnf.php index 242e931b..5416c0bb 100644 --- a/share/templates/my.cnf.php +++ b/share/templates/my.cnf.php @@ -1,13 +1,16 @@ [mysqld] -max_allowed_packet = +max_allowed_packet = [client] diff --git a/share/templates/pgpass.php b/share/templates/pgpass.php new file mode 100644 index 00000000..c6c78c58 --- /dev/null +++ b/share/templates/pgpass.php @@ -0,0 +1,15 @@ + -- 2.47.3