From: Nicolas Boisselier Date: Sat, 13 Jul 2019 23:19:10 +0000 (+0100) Subject: /opt/nb/lib/php/nb.php X-Git-Url: https://git.nbdom.net/?a=commitdiff_plain;h=75d8e92cb3b69ffdda20013878b9e2181ed53b58;p=nb.git /opt/nb/lib/php/nb.php --- diff --git a/lib/php/db.php b/lib/php/db.php index 3c5c22a7..3ca7e4e6 100644 --- a/lib/php/db.php +++ b/lib/php/db.php @@ -1301,6 +1301,20 @@ class Db extends nb { return $this->databases; } + /* + * Replace parameters aliases by long name + */ + public static function paliases($aliases,&$changed=[]) { + foreach ($aliases as $short=>$long) { + if (!preg_match('/^\s*$/',self::p($short))) { + self::pset($long,self::p($short)); + $changed[$short] = $long; + } + #if (!preg_match('/^\s*$/',self::p($short))) echo ">$long => short=".self::p($short); + self::pset($short,null); + } + } + public static function pinit(&$changed=[]) { if (!empty(self::$paliases)) self::paliases(self::$paliases,$changed); diff --git a/lib/php/db/table.php b/lib/php/db/table.php index 94c3dc15..47878381 100644 --- a/lib/php/db/table.php +++ b/lib/php/db/table.php @@ -1738,50 +1738,6 @@ Class Table extends nb { return $this->db()->sql_name($value === null ? $this->name : $value); } -# NB 04.02.18 private function _post2sql($post) { -# NB 04.02.18 $keys = []; -# NB 04.02.18 $keys_values = []; -# NB 04.02.18 $fields = []; -# NB 04.02.18 $fields_values = []; -# NB 04.02.18 foreach ($this->fields() as $name => $field) { -# NB 04.02.18 -# NB 04.02.18 if ($field->extras) continue; -# NB 04.02.18 -# NB 04.02.18 $value = $post[$this->ppreff().$name]; -# NB 04.02.18 -# NB 04.02.18 if ($field->key) { -# NB 04.02.18 $keys[] = $name; -# NB 04.02.18 -# NB 04.02.18 if (!isset($post[$this->key_preff.$name])) { -# NB 04.02.18 if ($field->key) $this->bye("Missing `$name`!"); -# NB 04.02.18 continue; -# NB 04.02.18 } -# NB 04.02.18 -# NB 04.02.18 if (isset($post[$this->key_preff.$name])) { -# NB 04.02.18 $keys_values[] = $post[$this->key_preff.$name]; -# NB 04.02.18 -# NB 04.02.18 } else { -# NB 04.02.18 $keys_values[] = $value; -# NB 04.02.18 } -# NB 04.02.18 -# NB 04.02.18 } else { -# NB 04.02.18 -# NB 04.02.18 $fields[] = $name; -# NB 04.02.18 $fields_values[] = $value; -# NB 04.02.18 -# NB 04.02.18 } -# NB 04.02.18 } -# NB 04.02.18 -# NB 04.02.18 return [ -# NB 04.02.18 'keys' => $keys, -# NB 04.02.18 'keys_values' => $keys_values, -# NB 04.02.18 'fields' => $fields, -# NB 04.02.18 'fields_values' => $fields_values, -# NB 04.02.18 'all_keys' => array_combine($keys,$keys_values), -# NB 04.02.18 'all_fields' => array_combine($fields,$fields_values), -# NB 04.02.18 ]; -# NB 04.02.18 } - public function replace($hvalues,&$info=[]) { # NB 12.12.17: When REPLACE is not supported where call a personalized function to modify $sql $fct = $this->db()->conf_type('replace_insert'); diff --git a/lib/php/functions.php b/lib/php/functions.php deleted file mode 100644 index 76a549ca..00000000 --- a/lib/php/functions.php +++ /dev/null @@ -1,267 +0,0 @@ - $v) { -# NB 13.07.19 foreach ($v as $kk => $vv) { -# NB 13.07.19 $new[$kk] = $vv; -# NB 13.07.19 } -# NB 13.07.19 } -# NB 13.07.19 -# NB 13.07.19 $map = $new; -# NB 13.07.19 -# NB 13.07.19 } -# NB 13.07.19 -# NB 13.07.19 return $map; -# NB 13.07.19 } - -function txt2md($txt) { -# NB 28.11.16: TODO rename as Nb::md2html - global $_txt2md; - if ($_txt2md === null) { - require_once(NB_ROOT.'/lib/php/parsedown/Parsedown.php'); - $_txt2md = new Parsedown(); - } - return $_txt2md->text($txt); -} - -function bye($msg='__bye__',$backtrace_deep=0) { - return Nb::bye($msg,$backtrace_deep); -} - -function warn ($msg) { - file_write("php://stderr","$msg\n"); -} - -function err($msg='__err__',$preff='err',$backtrace_deep=0) { - return Nb::err($msg,$preff,$backtrace_deep); -} - -# NB 15.11.17 function err($msg='__err__',$preff='err',$backtrace_deep=0) { -# NB 15.11.17 $msg = is_scalar($msg) ? $msg : print_r($msg,true); -# NB 15.11.17 $preff_msg = $preff ? strtoupper($preff).': ' : ''; -# NB 15.11.17 -# NB 15.11.17 if ($msg !== '__err__' and $backtrace_deep !== false) { -# NB 15.11.17 $msg = trim($preff_msg.$msg).' ' -# NB 15.11.17 .Nb::debug_backtrace_msg(1+$backtrace_deep,NULL,$preff_msg) -# NB 15.11.17 #.Nb::debug_backtrace_msg(1+$backtrace_deep,NULL) -# NB 15.11.17 ; -# NB 15.11.17 -# NB 15.11.17 } else { -# NB 15.11.17 $msg = $preff_msg.$msg; -# NB 15.11.17 -# NB 15.11.17 } -# NB 15.11.17 -# NB 15.11.17 #Nb::msg( !Nb::php_cli() -# NB 15.11.17 Nb::msg( Nb::client_header('Accept','ml') -# NB 15.11.17 ? ''.$msg.''.NB_EOL -# NB 15.11.17 : $msg -# NB 15.11.17 ).NB_EOL; -# NB 15.11.17 -# NB 15.11.17 } - -function debug($msg,$level=0) { - return Nb::debug($msg,$level); -} - -# NB 15.11.17 function debug($msg,$level=0) { -# NB 15.11.17 if ($level and $level>Nb::p('debug')) return; -# NB 15.11.17 #$msg = is_scalar($msg) ? $msg : print_r($msg,true); -# NB 15.11.17 $msg = is_scalar($msg) ? $msg : print_r($msg,true); -# NB 15.11.17 -# NB 15.11.17 if (Nb::client_header('Accept','ml')) { -# NB 15.11.17 $msg = '
'
-# NB 15.11.17       .(isset($_SERVER['HTTP_HOST']) ? htmlentities($msg) : $msg)
-# NB 15.11.17       #.(isset($_SERVER['HTTP_HOST']) ? htmlspecialchars($msg) : $msg)
-# NB 15.11.17     .'
'.NB_EOL; -# NB 15.11.17 -# NB 15.11.17 } else { -# NB 15.11.17 $msg = "DEBUG: $msg\n"; -# NB 15.11.17 -# NB 15.11.17 } -# NB 15.11.17 Nb::msg($msg); -# NB 15.11.17 -# NB 15.11.17 } - -function rtf2txt($rtf) { - $cmd="unrtf --text"; - return cmd2str($cmd,$rtf); -} - -function rtf2html($rtf) { - $cmd="unrtf --html"; - return cmd2str($cmd,$rtf); -} - -function html2pdf($html) { - $cmd="iconv -f utf-8 -t iso-8859-1 -c|htmldoc --encryption --webpage --continuous --no-title --header '...' --footer '...' -"; - $cmd .= " -t pdf -"; - return cmd2str($cmd,$html); -} - -function cmd2str($cmd,$write=null) { - #die(substr($write,0,80)); - $descriptorspec = array( - - // stdin is a pipe that the child will read from - 0 => array("pipe", "r"), - - // stdout is a pipe that the child will write to - 1 => array("pipe", "w"), - - // stderr is a file to write to - 2 => array("pipe", "/dev/null", "w") - ); - - $process = proc_open($cmd, $descriptorspec, $pipes); - - if (!is_resource($process)) return; - - if ($write !== null) fwrite($pipes[0], $write); - // Il est important que vous fermiez les pipes avant d'appeler - // proc_close afin d'éviter un verrouillage. - fclose($pipes[0]); - - $return_content = ''; - while (!feof($pipes[1])) { - $return_content .= fread($pipes[1], 8192); - } - - fclose($pipes[1]); - - $return_value = proc_close($process); - -#die(strlen($write)); - return $return_content; - -} - -function argv2request($args=null) { return Nb::argv2request($args); } - -function html_select_array($data,$opt=[]) { - - $html = ''; - $html .= $opt['blank_lib']; - $html .= ''.NB_EOL; - } - - if (!empty($opt['sort'])) { - if (is_bool($opt['sort'] )) natcasesort($data); - else $opt['sort']($data); - } - - $default = ''; - if (!empty($opt['default_value'])) $default = $opt['default_value']; - - foreach ($data as $d) { - - if (is_array($d)) { - $val = $lib = $d[0]; - if (count($d)>1) $lib = $d[1]; - } else { - $val = $lib = $d; - } - - $lib = htmlentities($lib); - if (!empty($opt['prettyText'])) $lib = prettyText($lib); - - $html .= ''.NB_EOL; - - } - - $html .= ''.NB_EOL; - - return $html; - -} -/* - Add missing functions for compatibility -*/ -if (!function_exists('array_replace_recursive')) { - # http://php.net/manual/en/function.array-replace-recursive.php - function array_replace_recursive($array, $array1) { - function recurse($array, $array1) { - foreach ($array1 as $key => $value) { - - // create new key in $array, if it is empty or not an array - if (!isset($array[$key]) || (isset($array[$key]) && !is_array($array[$key]))) { - $array[$key] = array(); - } - - // overwrite the value in the base array - if (is_array($value)) { - $value = recurse($array[$key], $value); - } - - $array[$key] = $value; - - } - - return $array; - } - - // handle the arguments, merge one by one - $args = func_get_args(); - $array = $args[0]; - - if (!is_array($array)) { - return $array; - } - - for ($i = 1; $i < count($args); $i++) { - if (is_array($args[$i])) { - $array = recurse($array, $args[$i]); - } - } - - return $array; - } -} -?> diff --git a/lib/php/nb.php b/lib/php/nb.php index 546ff96c..0ecfb5a2 100644 --- a/lib/php/nb.php +++ b/lib/php/nb.php @@ -5,27 +5,22 @@ if (!defined('NB_PROD')) define('NB_PROD',false); if (!defined('NB_EOL')) define('NB_EOL',(defined('NB_PROD') and NB_PROD) ? '' : "\n"); if (NB_PROD) unset ($_GET['debug']); -# use _GET instead of _REQUEST -if (!defined('NB_P_GET')) define('NB_P_GET',true); +if (class_exists('Nb')) return; # protect against double include -require_once(NB_ROOT.'/lib/php/functions.php'); - -if (class_exists('NB')) return; # protect against double include - -class NB { +class Nb { #public static function test() { static $i=0; echo ($i++)."\n"; } const ROOT_DIR = NB_ROOT; const NB_ROOT = NB_ROOT; - protected static $content_types_aliases = array( + protected static $content_types_aliases = [ 'text' => 'txt', 'human' => 'txt', 'table' => 'html', 'div' => 'html', - ); + ]; - public function __construct($opt = array()) { + public function __construct($opt = []) { # Deep merge of vars foreach ($opt as $k => $v) { @@ -59,20 +54,6 @@ class NB { foreach (array_keys((array)$this) as $k) { if (isset($this->$k)) unset($this->$k); }; } - /* - * Replace aliases by long name - */ - public static function paliases($aliases,&$changed=[]) { - foreach ($aliases as $short=>$long) { - if (!preg_match('/^\s*$/',self::p($short))) { - self::pset($long,self::p($short)); - $changed[$short] = $long; - } - #if (!preg_match('/^\s*$/',self::p($short))) echo ">$long => short=".self::p($short); - self::pset($short,null); - } - } - /* * Set a default's param value, if not set * Return the value set or existing @@ -86,30 +67,36 @@ class NB { * Function: p * Return a param */ + +# NB 14.07.19 # use _GET instead of _REQUEST +# NB 14.07.19 if (!defined('NB_P_GET')) define('NB_P_GET',true); + public static function ppreff($set_preff=null) { static $preff = ''; if ($set_preff !== null) $preff = $set_preff; return $preff; } - protected static function _p_get($name=null,$default=null) { + public static function p($name=null,$default=null) { +# NB 14.07.19 protected static function _p_get($name=null,$default=null) { if ($name === null) return isset($_GET) ? $_GET : []; $name = self::ppreff() . $name; 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 : []; - $name = self::ppreff() . $name; - return isset($_REQUEST[$name]) ? $_REQUEST[$name] : $default; - } +# NB 14.07.19 public static function p($name=null,$default=null) { +# NB 14.07.19 if(NB_P_GET) return self::_p_get($name,$default); +# NB 14.07.19 if ($name === null) return isset($_REQUEST) ? $_REQUEST : []; +# NB 14.07.19 $name = self::ppreff() . $name; +# NB 14.07.19 return isset($_REQUEST[$name]) ? $_REQUEST[$name] : $default; +# NB 14.07.19 } /* * Function: pset * Set a value for param, delete it if null */ - protected static function _pset_get($name,$value=null) { + public static function pset($name,$value=null) { +# NB 14.07.19 protected static function _pset_get($name,$value=null) { # Brutal ! if (is_array($name)) return ($_GET=$name); @@ -123,21 +110,21 @@ class NB { return ($_GET[$name] = $value); } - public static function pset($name,$value=null) { - if(NB_P_GET) return self::_pset_get($name,$value); - - # Brutal ! - if (is_array($name)) return ($_REQUEST=$name); - - # Delete key - if ($value === null or $value === '') { - unset ($_REQUEST[$name]); - return null; - } - - # Set value - return ($_REQUEST[$name] = $value); - } +# NB 14.07.19 public static function pset($name,$value=null) { +# NB 14.07.19 if(NB_P_GET) return self::_pset_get($name,$value); +# NB 14.07.19 +# NB 14.07.19 # Brutal ! +# NB 14.07.19 if (is_array($name)) return ($_REQUEST=$name); +# NB 14.07.19 +# NB 14.07.19 # Delete key +# NB 14.07.19 if ($value === null or $value === '') { +# NB 14.07.19 unset ($_REQUEST[$name]); +# NB 14.07.19 return null; +# NB 14.07.19 } +# NB 14.07.19 +# NB 14.07.19 # Set value +# NB 14.07.19 return ($_REQUEST[$name] = $value); +# NB 14.07.19 } /* * Function: bye @@ -169,7 +156,6 @@ class NB { } - #self::msg( !self::php_cli() self::msg( self::client_header('Accept','ml') ? 'self::p('debug')) return; #$msg = is_scalar($msg) ? $msg : print_r($msg,true); @@ -984,12 +967,12 @@ class NB { } public static function md2html($txt) { - static $_txt2md = null; - if ($_txt2md === null) { + static $parser = null; + if ($parser === null) { require_once(NB_ROOT.'/lib/php/parsedown/Parsedown.php'); - $_txt2md = new Parsedown(); + $parser = new Parsedown(); } - return $_txt2md->text($txt); + return $parser->text($txt); } public static function minify_js($code) { @@ -1085,20 +1068,153 @@ class NB { } # < Class NB +function prettyText($text) { return Nb::prettyText($text); } +function file_write($file,$data,$mode='w') { return Nb::file_write($file,$data,$mode); } +function bye($msg='__bye__',$backtrace_deep=0) { return Nb::bye($msg,$backtrace_deep); } +function warn ($msg) { Nb::file_write("php://stderr","$msg\n"); } +function err($msg='__err__',$preff='err',$backtrace_deep=0) { return Nb::err($msg,$preff,$backtrace_deep); } +function debug($msg,$level=0) { return Nb::debug($msg,$level); } +function argv2request($args=null) { return Nb::argv2request($args); } + +function rtf2txt($rtf) { + $cmd="unrtf --text"; + return cmd2str($cmd,$rtf); +} + +function rtf2html($rtf) { + $cmd="unrtf --html"; + return cmd2str($cmd,$rtf); +} + +function html2pdf($html) { + $cmd="iconv -f utf-8 -t iso-8859-1 -c|htmldoc --encryption --webpage --continuous --no-title --header '...' --footer '...' -"; + $cmd .= " -t pdf -"; + return cmd2str($cmd,$html); +} + +function cmd2str($cmd,$write=null) { + #die(substr($write,0,80)); + $descriptorspec = array( + + // stdin is a pipe that the child will read from + 0 => array("pipe", "r"), + + // stdout is a pipe that the child will write to + 1 => array("pipe", "w"), + + // stderr is a file to write to + 2 => array("pipe", "/dev/null", "w") + ); + + $process = proc_open($cmd, $descriptorspec, $pipes); + + if (!is_resource($process)) return; + + if ($write !== null) fwrite($pipes[0], $write); + // Il est important que vous fermiez les pipes avant d'appeler + // proc_close afin d'éviter un verrouillage. + fclose($pipes[0]); + + $return_content = ''; + while (!feof($pipes[1])) { + $return_content .= fread($pipes[1], 8192); + } + + fclose($pipes[1]); + + $return_value = proc_close($process); + +#die(strlen($write)); + return $return_content; + +} + +function html_select_array($data,$opt=[]) { + + $html = ''; + $html .= $opt['blank_lib']; + $html .= ''.NB_EOL; + } + + if (!empty($opt['sort'])) { + if (is_bool($opt['sort'] )) natcasesort($data); + else $opt['sort']($data); + } + + $default = ''; + if (!empty($opt['default_value'])) $default = $opt['default_value']; + + foreach ($data as $d) { + + if (is_array($d)) { + $val = $lib = $d[0]; + if (count($d)>1) $lib = $d[1]; + } else { + $val = $lib = $d; + } + + $lib = htmlentities($lib); + if (!empty($opt['prettyText'])) $lib = prettyText($lib); + + $html .= ''.NB_EOL; + + } + + $html .= ''.NB_EOL; + + return $html; + +} /* -return; -if (!count($argv) or (string)$argv[1] != 'test') return; -$value = 'Hello World !'; -#$value = file_get_contents('/etc/hosts'); -$key = hash("MD5", '5587eeb68760aa0ed7d2d1212d0829c3'); //we want a 32 byte binary blob -#substr(hash('sha512',rand()),0,32); -#$key = '5587eeb68760aa0ed7d2d1212d0829c3'; -#$key = '76a553babab7a62b7935d9a10f73777bf57b043c9d0f1fc22ea51dd9484154bc91afafd0f92e773e590ad05ebca9aec3fac11ebada7e517c78d32790e5a8f3ed'; -$enc = nb::encrypt($key,$value); -echo '' - ."key : $key\n" - ."enc : $enc\n" - ."value: ".nb::decrypt($key,$enc) ."\n" -; + Add missing functions for compatibility */ +if (!function_exists('array_replace_recursive')) { + # http://php.net/manual/en/function.array-replace-recursive.php + function array_replace_recursive($array, $array1) { + function recurse($array, $array1) { + foreach ($array1 as $key => $value) { + + // create new key in $array, if it is empty or not an array + if (!isset($array[$key]) || (isset($array[$key]) && !is_array($array[$key]))) { + $array[$key] = array(); + } + + // overwrite the value in the base array + if (is_array($value)) { + $value = recurse($array[$key], $value); + } + + $array[$key] = $value; + + } + + return $array; + } + + // handle the arguments, merge one by one + $args = func_get_args(); + $array = $args[0]; + + if (!is_array($array)) { + return $array; + } + + for ($i = 1; $i < count($args); $i++) { + if (is_array($args[$i])) { + $array = recurse($array, $args[$i]); + } + } + + return $array; + } +} ?>