public $conf = [];
public $row_parse; # Function to call in table.rows()
- # Encription
- public $encrypt = [];
+ # Encryption
+ public $encrypt = [
+# 'secret' => '32_CHAR_KEY',
+# 'tables' => [
+# 'TABLE1' => [
+# 'FIELD1',
+# ...,
+# ],
+# ...,
+# ],
+ ];
# Params
protected static $paliases = [
return $this->tables;
}
- public static function print_header($format) {
+ public static function print_header($format,$value=null) {
if (empty($_SERVER['DOCUMENT_ROOT'])) return null;
+ if ($value!==null) return header("$format: $value");
+
if (!$mime=self::ext2mime($format)) return false;
header('Content-type: '.$mime);
public function out($rows,$head=[]) {
+ if ($this->p('format') == 'null') return true;
// Handle limit
if ($this->p('limit')) {
return $this->databases;
}
- public static function pinit() {
- if (!empty(self::$paliases)) self::paliases(self::$paliases);
+ public static function pinit(&$changed=[]) {
+ if (!empty(self::$paliases)) self::paliases(self::$paliases,$changed);
if ($action=self::p('action') and !empty(self::$action_aliases)) {
foreach (self::$action_aliases as $src => $dest) {
}
# Param - Extract dbname from table
- if (false and preg_match('/^(\w+)\.(.*?)$/',self::p('table'),$m)) {
- self::pset('db',$m[1]);
- self::pset('table',$m[2]);
- }
+# NB 12.11.16 if (false and preg_match('/^(\w+)\.(.*?)$/',self::p('table'),$m)) {
+# NB 12.11.16 self::pset('db',$m[1]);
+# NB 12.11.16 self::pset('table',$m[2]);
+# NB 12.11.16 }
# Select param
if (Db::p('table') and Db::p('select')) {
}
# Set format from client Accept if != html
- Db::pdef('format',out::client_type());
+ if (!Db::p('format')) Db::pdef('format',out::client_type());
}
return $value;
}
+ public function is_encrypt() {
+ if (0
+ or empty($this->db()->encrypt)
+ or empty($this->db()->encrypt['tables'])
+ or empty($this->db()->encrypt['tables'][$this->table->name])
+ )
+ return false;
+ return in_array($this->name,$this->db()->encrypt['tables'][$this->table->name]);
+ }
+
}
?>
)) {
$line = mb_substr($line,mb_strlen($args[0])+1);
array_shift($args); list ($k,$v) = $args;
+ #print_r([$k,$v]);
db_shell_action($k,$v);
}
function db_shell_action($k,$v) {
global $DB_CONFS, $Db, $Table;
Db::pset($k,$v);
- Db::pinit();
+ $changed = [];
+ Db::pinit($changed);
+ if (isset($changed[$k])) $k = $changed[$k];
switch ($k) {
break;
}
+ #bye($Db::p());
+ if ($Db->p('debug') == 'shell') {
+ debug($Db->p());
+ break;
+ }
$Db->action($Db->p('action'),$Table);
break;
break;
default:
- echo "ERR: $k=$v\n";
- Db::pset($k,null);
+ Db::pset($k,$v);
break;
+# default:
+# echo "ERR: $k=$v\n";
+# Db::pset($k,null);
+# break;
+
}
}
?>
if (empty($i['unique']) and empty($i['key'])) $indexes[$i['field']] = 1;
}
+ $count = 0;
foreach ($rows as $row) {
+ $count++;
$row = array_change_key_case($row,CASE_LOWER);
// From config
$row['this'] = $this; # for use
if (isset($conf['map'])) {
+
foreach ($conf['map'] as $from => $to) {
if (!isset($row[$from])) continue;
+
$row[$to] = $row[$from];
unset ($row[$from]);
}
+
}
+
if (isset($conf['fct'])) $conf['fct']($row) ;
$field = array(
echo '<div class="fields">'.NB_EOL;
$count = 0;
if ( $add or ($row = $st->fetch(PDO::FETCH_ASSOC, PDO::FETCH_ORI_NEXT))) {
- $orig = $row;
+ #$orig = $row;
- $this->db()->table_row_decrypt($this,$row);
+ if (!empty($row)) $this->db()->table_row_decrypt($this,$row);
$count ++;
foreach ($fields as $name => $field) {
public function url_sort($name) {
- if (!$this->buttons() or !($f=$this->fields($name)) or !empty($f->dyn)) return self::prettyText($name);
+ if (!$this->buttons() or !($f=$this->fields($name)) or !empty($f->dyn) or $f->is_encrypt()) return self::prettyText($name);
#debug($f);
# See: http://dev.w3.org/html5/html-author/charref
$st->execute();
# Fields
- $fields = array();
+ $fields = [];
if (!DB_TABLE_QUERY_NAME) {
foreach ($this->db()->fields($st) as $f) { $fields[$f->name] = $f; }
$this->fields = $fields;
}
if ($out_conf) {
- out::head($out_conf,$this->fields(),[$row]);
+ out::head($out_conf,array_keys($this->fields()),[$row]);
} else {
+ #bye([$this->fields(),$row]);
echo $this->{"rows_begin_$format"}($this->fields(),$opt);
}
if (self::p('debug')) {
$this->debug($info,1);
- return false;
+ return true;
}
if (!($execute = $query->execute())) {
return $this->out($this->info());
} elseif ($action == 'table.delete') {
- if (!$this->delete($this->p(),$e)) bye($e);
- header('Location: '.$this->url_referer(str_replace('&','&',$this->url_list())));
+ if (!$this->delete($this->p(),$e) and !isset($e['rowCount'])) bye($e);
+ $this->db()->print_header('Location',$this->url_referer(str_replace('&','&',$this->url_list())));
$this->out($e);
return true;
} elseif ($action == 'table.replace') {
- if (!$this->replace($this->p(),$e)) bye($e);
- header('Location: '.$this->url_referer());
+ if (!$this->replace($this->p(),$e) and !isset($e['rowCount'])) bye($e);
+ $this->db()->print_header('Location',$this->url_referer());
$this->out($e);
return true;
} elseif ($action == 'table.insert') {
if (!$this->insert($this->p(),$e)) bye();
- header('Location: '.$this->url_referer());
+ $this->db()->print_header('Location',$this->url_referer());
$this->out($e);
return true;
} elseif ($action == 'table.update') {
#$this->bye($this->p());
if (!$this->update($this->p(),$e)) bye($e);
- header('Location: '.$this->url_referer());
+ $this->db()->print_header('Location',$this->url_referer());
$this->out($e);
return true;
// Criterias
//
$r .= '<div class="criterias">';
- foreach ( array_keys($this->fields()) as $k ) {
+ foreach ( $this->fields() as $k => $f) {
+ if ($f->is_encrypt()) continue;
$v = $this->p($k);
$r .= ''
function err($msg='__err__',$preff='err',$backtrace_deep=0) {
# NB 05.04.16 $msg = is_scalar($msg) ? $msg : print_r($msg,true);
- $msg = is_scalar($msg) ? $msg : var_export($msg,true);
+ $msg = is_scalar($msg) ? $msg : print_r($msg,true);
$preff_msg = $preff ? strtoupper($preff).': ' : '';
if ($msg !== '__err__' and $backtrace_deep !== false) {
$msg = trim($preff_msg.$msg).' '
function debug($msg,$level=0) {
if ($level and $level>nb::p('debug')) return;
#$msg = is_scalar($msg) ? $msg : print_r($msg,true);
- $msg = is_scalar($msg) ? $msg : var_export($msg,true);
+ $msg = is_scalar($msg) ? $msg : print_r($msg,true);
if (nb::client_header('Accept','ml')) {
$msg = '<pre class="debug">'
}
+ #public function __set_state() { }
+
public function __sleep() {
return(get_class_vars(get_class($this))); # get all
# NB 09.04.16 return array_keys((array)$this);
/*
* Replace aliases by long name
*/
- public static function paliases($aliases) {
+ public static function paliases($aliases,&$changed=[]) {
foreach ($aliases as $short=>$long) {
- if (!preg_match('/^\s*$/',self::p($short))) self::pset($long,self::p($short));
+ 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);
}
* Return the value set or existing
*/
public static function pdef($name,$value) {
- if (preg_match('/^\s*$/',self::p($name))) return self::pset($name,$value);
+ if (preg_match('/^\s*$/',self::p($name))) return self::pset($name,is_callable($value) ? $value() : $value);
return self::p($name);
}
public static function php_cli() {
# NB 05.03.16 define('NB_CLI', PHP_SAPI === 'cli');
- return (!isset($_SERVER['SERVER_SOFTWARE']) && (php_sapi_name() == 'cli' || (is_numeric($_SERVER['argc']) && $_SERVER['argc'] > 0)));
+ static $php_cli;
+ if ($php_cli===null or $php_cli==='') $php_cli= (
+ !isset($_SERVER['SERVER_SOFTWARE']) && (php_sapi_name() == 'cli' || (is_numeric($_SERVER['argc']) && $_SERVER['argc'] > 0))
+ );
+ return $php_cli;
}
public static function no_accent($str){
'sql' => array(), # Code still in db/table.php !
+ 'shell' => array(
+ 'preff' => self::p('preff',''),
+ 'suff' => self::p('suff',''),
+ 'row' => function(&$o,&$r) {
+ $line = [];
+ foreach ($r as $k=>$v) {
+ $v = str_replace('"','\\"',$v);
+ $line[] = $o['preff']."$k=\"$v\"".$o['suff'];
+ }
+ if ($line) echo join(' ',$line)."\n";
+ }
+ ),
+
'sh' => array(
'preff' => self::p('preff',""),
'row' => function(&$o,&$r) {