+++ /dev/null
-<?php
-require_once(realpath(dirname(__FILE__).'/nb.php'));
-if (!defined('EOL')) define('EOL',NB_EOL);
-function prettyText($text) { return Nb::prettyText($text); }
-/*
-Vim:
-
-s/array_key_exists(\([^,]\+\),\([^)]\+\))/isset(\2[\1])/g
-
-s/^define(\([^,]\+\)/if (!defined(\1)) define(\1/
-
-*/
-
-function file_write($file,$data,$mode='w') {
- return Nb::file_write($file,$data,$mode);
-
- if (!$ftmp = fopen($file,$mode)) {
- bye("file_write(): Cant open $file in mode $mode");
- return;
- }
-
- if (fwrite($ftmp,$data) === FALSE) {
- bye("file_write(): Cant write $file in mode $mode");
- return;
- }
-
- fclose($ftmp);
- return 1;
-
-}
-
-# NB 13.07.19 function ar_first($ar) { return (is_array($ar) and count($ar)) ? $ar[0] : false; }
-
-# NB 13.07.19 function ar_map($return,$array,$as_hash=false) {
-# NB 13.07.19 $map = array_map(function($a) use($return) { return($return); },$array);
-# NB 13.07.19
-# NB 13.07.19 if ($as_hash) {
-# NB 13.07.19
-# NB 13.07.19 $new = array();
-# NB 13.07.19
-# NB 13.07.19 foreach ($map as $k => $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 ? '<pre'
-# NB 15.11.17 .' class="err'. ( ($preff and $preff!='err') ? " $preff" : '' ).'"'
-# NB 15.11.17 .'>'.$msg.'</pre>'.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 = '<pre class="debug">'
-# 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 .'</pre>'.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 = '<select';
- if (!empty($opt['html'])) $html .= ' '.preg_replace('/^\s+/','',$opt['html']);
- if (!empty($opt['readonly'])) $html.=' disabled="disabled"';
- $html .= '>'.NB_EOL;
-
- if (!empty($opt['blank'])) {
- if (empty($opt['blank_lib'])) $opt['blank_lib'] = $opt['blank'];
- $html .= '<option value="'.$opt['blank_val'].'"'.(strcmp((string)$opt['selected'],$opt['blank_val']) == 0 ?' selected="selected"':'').'>';
- $html .= $opt['blank_lib'];
- $html .= '</option>'.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 .= '<option value="'.($default == $val ? '' : $val).'"'
- .( ($opt['selected']!==NULL and strcmp((string)$opt['selected'],$val)===0) ?' selected="selected"':'')
- .'>'.$lib.'</option>'.NB_EOL;
-
- }
-
- $html .= '</select>'.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;
- }
-}
-?>
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) {
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
* 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);
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
}
- #self::msg( !self::php_cli()
self::msg( self::client_header('Accept','ml')
? '<pre'
.' class="alert-danger err'. ( ($preff and $preff!='err') ? " $preff" : '' ).'"'
* Function: debug
* Does what it says
*/
-# NB 15.11.17 public static function debug($msg,$level=0) {
-# NB 15.11.17 return debug($msg,$level);
-# NB 15.11.17 }
public static function debug($msg,$level=0) {
if ($level and $level>self::p('debug')) return;
#$msg = is_scalar($msg) ? $msg : print_r($msg,true);
}
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) {
} # < 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 = '<select';
+ if (!empty($opt['html'])) $html .= ' '.preg_replace('/^\s+/','',$opt['html']);
+ if (!empty($opt['readonly'])) $html.=' disabled="disabled"';
+ $html .= '>'.NB_EOL;
+
+ if (!empty($opt['blank'])) {
+ if (empty($opt['blank_lib'])) $opt['blank_lib'] = $opt['blank'];
+ $html .= '<option value="'.$opt['blank_val'].'"'.(strcmp((string)$opt['selected'],$opt['blank_val']) == 0 ?' selected="selected"':'').'>';
+ $html .= $opt['blank_lib'];
+ $html .= '</option>'.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 .= '<option value="'.($default == $val ? '' : $val).'"'
+ .( ($opt['selected']!==NULL and strcmp((string)$opt['selected'],$val)===0) ?' selected="selected"':'')
+ .'>'.$lib.'</option>'.NB_EOL;
+
+ }
+
+ $html .= '</select>'.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;
+ }
+}
?>