From 2f69760594e2db1e75ec745c6006c3122831bc5e Mon Sep 17 00:00:00 2001 From: Nicolas Boisselier Date: Wed, 7 Sep 2016 02:12:38 +0100 Subject: [PATCH] Bed --- etc/profile.d/aliases | 1 + lib/php/db/index.php | 4 ++ lib/php/db/table.php | 5 +- lib/php/nb.php | 13 +++- lib/php/out.php | 2 +- lib/php/page.php | 138 +++++++++++++++++------------------------- 6 files changed, 77 insertions(+), 86 deletions(-) diff --git a/etc/profile.d/aliases b/etc/profile.d/aliases index 77f5856d..94912f1c 100644 --- a/etc/profile.d/aliases +++ b/etc/profile.d/aliases @@ -18,6 +18,7 @@ alias nmap-ping="nmap -sP -PE -PS443 -PA21,22,23,80,3389" alias arp-list="arp -an" alias pdf2jpegs='gs -dNOPAUSE -sDEVICE=jpeg -dFirstPage=1 -dLastPage=5 -sOutputFile=pdf2jpegs%d.jpg -dJPEGQ=100 -r500 -c quit' alias nb-maildir-make="maildirmake -S Maildir; for d in Spam Trash Sent;do maildirmake -f $d Maildir; done" +alias grep='grep --color=auto' #[ "$UID" = "0" ] && grep -q '^_\?postgres:' /etc/passwd && alias psql='sudo -u postgres psql' diff --git a/lib/php/db/index.php b/lib/php/db/index.php index 02656a59..b6656f48 100755 --- a/lib/php/db/index.php +++ b/lib/php/db/index.php @@ -20,5 +20,9 @@ if(defined('DB_NO_INIT') and DB_NO_INIT) return true; Db::init([Db::ROOT_DIR.'/etc/dbs.yaml',Db::ROOT_DIR.'/etc/dbs.php','/etc/dbs.yaml']); if(defined('DB_NO_ACTION') and DB_NO_ACTION) return true; + +# Happend when include from a function +if (!isset($Db)) global $Db, $Table; + return $Db->action($Db->p('action'),$Table); ?> diff --git a/lib/php/db/table.php b/lib/php/db/table.php index 30b4ef04..5e87274b 100644 --- a/lib/php/db/table.php +++ b/lib/php/db/table.php @@ -1473,7 +1473,10 @@ Class Table extends nb { public function html_row_buttons(&$row) { return array( ''.DB_HTML_EDIT.''.NB_EOL, - ''.DB_HTML_DELETE.''.NB_EOL, + ''.DB_HTML_DELETE.''.NB_EOL, ); } diff --git a/lib/php/nb.php b/lib/php/nb.php index 7e0e8457..4d8c83c2 100644 --- a/lib/php/nb.php +++ b/lib/php/nb.php @@ -33,10 +33,20 @@ class NB { #public static function zaza() { return (is_object($this) ? 'TRUE' : 'FALSE'); } #public static function test() { return 'TEST'; } public function __construct($opt = array()) { + + # Deep merge of vars foreach ($opt as $k => $v) { if ( ! array_key_exists($k,$this) ) self::bye("Unknow param $k = `$v`"); - $this->$k = is_array($v) ? $v : trim($v); + + if ($v === null) continue; + + if (is_array($this->$k)) { + $this->$k = array_merge_recursive((array)$this->$k, is_array($v) ? $v : [$v]); + } else { + $this->$k = $v; + } } + } public function __sleep() { @@ -100,6 +110,7 @@ class NB { * Set a value for param, delete it if null */ public static function bye($msg='',$backtrace_deep=0) { + #throw new Exception($msg); return bye($msg,$backtrace_deep+1); } diff --git a/lib/php/out.php b/lib/php/out.php index 88bece4d..2e146200 100644 --- a/lib/php/out.php +++ b/lib/php/out.php @@ -565,7 +565,7 @@ function out_human_end(&$o) { if (!$count) return; echo $sep_line; - echo "$count Records\n"; + if ($header) echo "$count Records\n"; } # < Functions diff --git a/lib/php/page.php b/lib/php/page.php index 9f97b7ee..f2038b66 100644 --- a/lib/php/page.php +++ b/lib/php/page.php @@ -9,7 +9,7 @@ $Page = new Page(array( #'title' => 'Test', 'call' => array( 'begin', - array('out', 'Hello World !!!'), + ['out', 'Hello World !!!'], #array('out', print_r($_SERVER,true)), 'end', ), @@ -19,7 +19,7 @@ $Page = new Page(array( class Page extends nb { - public $call = array(); + public $call = []; # NB 04.08.15 wtf ??? public $output; protected static $content_type = 'text/html'; @@ -29,44 +29,54 @@ class Page extends nb { public static $expires; # Seconds to cache, no cache if 0, no head if null public $title = ''; - public $css = array(); - public $head = array(); + public $css = []; + public $head = []; public $css_code = ''; - public $js = array(); + public $js = []; public $js_code = ''; public $body_class = ''; public $body_id = ''; public $h1 = ''; + public $sep = ' / '; /* - Create + New */ - public function __construct($opt = array()) { + public function __construct($opt = []) { - //// opt - foreach ($opt as $k => $v) { - if ( ! array_key_exists($k,$this) ) die ("Page->new(): unknow param $k = $v"); - $this->$k = is_array($v) ? $v : trim($v); - } + // Nav + if (isset($opt['nav'])) list($title,$nav) = Page::nav_parse($opt['nav']); + unset($opt['nav']); + + // Content-type + if (isset($opt['content_type'])) $this->content_type($opt['content_type']); + elseif($this->php_cli()) $this->content_type('text/html'); + unset($opt['content_type']); + + // Opt + parent::__construct($opt); // Defaults - if ( empty($this->title) ) $this->title = $this->filename2title(); + if ( empty($this->title) ) $this->title = !empty($title) ? join($this->sep,$title) : $this->filename2title(); + if ( empty($this->h1) ) $this->h1 = !empty($nav) ? join($this->sep,$nav) : $this->title; - //// Default - #print_r($this); + // Call + foreach ($this->to_array($this->call) as $call) { - //// Call - foreach ($this->to_array($this->call) as $v) { + if (is_scalar($call)) { + $this->$call(); - if ( is_array($v) ) { - $k = $v[0]; $v = $v[1]; - $this->$k($v); + } elseif ( is_array($call) ) { + $k = $call[0]; + $this->$k($call[1]); + + } elseif (is_callable($call)) { + $call($this); } else { - $this->$v(); } @@ -82,7 +92,7 @@ class Page extends nb { } public static function to_array($v) { - return (is_array($v) ? $v : array($v)); + return (is_array($v) ? $v : [$v]); } public static function title2filename($title,$content_type='') { @@ -183,8 +193,8 @@ class Page extends nb { return '<' . $tag . ($attrs ? " $attrs" : "") . '>'; } - if (!is_array($content)) $content = array($content); - $tags = array(); + if (!is_array($content)) $content = [$content]; + $tags = []; foreach ($content as $c) { @@ -264,10 +274,10 @@ class Page extends nb { } public static function tag_end($tag=null) { - static $tags = array(); + static $tags = []; if ($tag !== null) return array_unshift($tags,$tag); foreach ($tags as $t) echo "".NB_EOL; - $tags = array(); + $tags = []; return $tags; return join('',self::ar_map('',$tags)); if ($tag === null) return $tags; @@ -405,49 +415,13 @@ class Page extends nb { return preg_replace('/\?.*$/','',$_SERVER['REQUEST_URI']); } - /** - * @copyright (C) 2016 Nicolas Boisselier - * @author Nicolas Boisselier nicolas.boisselier@gmail.com - * Does what the name says - * - * @param datatype1|datatype2 \$paramname description - * @return datatype1|datatype2 description - */ - public static function init($param) { - global $Page; - if (isset($Page)) self::bye("Page.init(): GLOBALS['Page'] already exists !"); - if (!self::is_hash($param)) $param = array('nav' => $param); - - $Page = new self(array( - - # 'head' => array( - # '', - # ), - 'body_class' => 'db', - - 'css' => false ? array() : array( - #'/jquery/jquery.mobile.css', - #'/jquery/jquery-ui.css', - '/css/reset.css', - '/css/block.css', - '/css/button.css', - '/css/db.css', - ), - - 'js' => array( - '/jquery/jquery.js', - '/jquery/jquery-ui.js', - #'/jquery/jquery.mobile.js', - '/js/nb.js', - ), - - )); - if (isset($param['js'])) foreach ($param['js'] as $v) { $Page->js[] = $v; } - if (isset($param['css'])) foreach ($param['css'] as $v) { $Page->css[] = $v; } - - $title = $nav = array(); - - if (isset($param['nav'])) foreach ($param['nav'] as $k => $v) { + public static function files_concat($files) { + + } + + public static function nav_parse($array) { + #$v = &$sep; + foreach ($array as $k => $v) { if ($v === '' or $v === null or empty($v[0])) { continue; } @@ -456,10 +430,10 @@ class Page extends nb { if (count($v)==3) { $title[] = $v[0]; - $nav[] = self::tag('a',$v[1],'href="'.$v[2].'"'); + $nav[] = empty($v[2]) ? $v[1] : self::tag('a',$v[1],'href="'.$v[2].'"'); } else { $title[] = $v[0]; - $nav[] = self::tag('a',$v[0],'href="'.$v[1].'"'); + $nav[] = empty($v[1]) ? $v[0] : self::tag('a',$v[0],'href="'.$v[1].'"'); } } else { @@ -469,16 +443,14 @@ class Page extends nb { } } - - #debug(array($nav,$title)); return; - $Page->h1 = join(' / ',$nav); - - $Page->title = join(' / ',$title); - - return $Page; + return [$title,$nav]; + return [ + 'title' => $title, + 'title' => $title, + ]; } - public function phpinfo() { + public static function phpinfo() { ob_start(); phpinfo(INFO_ALL - INFO_LICENSE); $phpinfo = ob_get_contents(); @@ -506,7 +478,7 @@ class Page extends nb { public function infos() { $out = array( - 'path' => $this->path() + 'path' => self::path() ); foreach (get_class_vars(get_class($this)) as $k=>$v) { if (!empty($this->$k)) $out[$k] = is_scalar($this->$k) ? $this->$k : out::scalar($this->$k); @@ -515,7 +487,7 @@ class Page extends nb { #foreach ($_SERVER as $k=>$v) $out['_SERVER.'.$k] = $v; #foreach ($_ENV as $k=>$v) $out['_ENV.'.$k] = $v; - $out = array_map(function($k, $v) { return array("name"=>$k,"value"=>$v); },array_keys($out),$out); + $out = array_map(function($k, $v) { return ["name"=>$k,"value"=>$v]; },array_keys($out),$out); return $out; } @@ -525,12 +497,12 @@ class Page extends nb { $is_html = out::is_html($format); if ($action == 'page.infos') { - $out = $this->infos(); + $out = self::infos(); out::rows($format,$out); return true; } elseif ($action == 'page.phpinfo' or $action == 'phpinfo') { - echo $this->phpinfo(); return true; + echo self::phpinfo(); return true; } elseif ($r=self::class_action_out($this,$action)) { return $r; -- 2.47.3