From: Nicolas Boisselier Date: Tue, 1 Nov 2016 02:01:02 +0000 (+0000) Subject: encrypt X-Git-Url: https://git.nbdom.net/?a=commitdiff_plain;h=e94a69f201c588b7d18bed593dc02c152ad59a71;p=nb.git encrypt --- diff --git a/bin/dbq b/bin/dbq index 6e24ff1e..99467cc4 100755 --- a/bin/dbq +++ b/bin/dbq @@ -103,6 +103,7 @@ get_options(\%Opt,\@CURL_OPT); # # Get URL # +#-e $ROOT_DIR.'/lib/php/db/index.php' && die 'OK'; my $url = ($ENV{$UC_NAME.'_URL'} ? $ENV{$UC_NAME.'_URL'} : ( -e $ROOT_DIR.'/lib/php/db/index.php' ? $ROOT_DIR.'/lib/php/db/index.php' : '/opt/rent/www/index.php' ) ); diff --git a/etc/dbq.php b/etc/dbq.php index 9aa48d27..bdbe6a87 100644 --- a/etc/dbq.php +++ b/etc/dbq.php @@ -10,17 +10,17 @@ $LOCAL_DB = ( # # Search existing DIR_SQLITE # -$DIR_SQLITE = ''; +unset($DIR_SQLITE); foreach ([ - '~nico/ownCloud', - '~nicoadm/ownCloud', + '/home/nico/ownCloud', + '/Users/nico/ownCloud', ] as $dir) { - $dir = nb::untilde($dir); + #$dir = nb::untilde($dir); if (!is_readable($dir)) continue; $DIR_SQLITE = $dir; break; } -if ($DIR_SQLITE) $DIR_SQLITE .= '/var/lib/sqlite'; +if (!empty($DIR_SQLITE)) $DIR_SQLITE .= '/var/lib/sqlite'; # # Includes dbq/*php @@ -41,7 +41,7 @@ foreach ($DBQ as $id => $db) { ) $host_already_exists[$db['host']] = $id; } -if ($DIR_SQLITE) { +if (!empty($DIR_SQLITE)) { foreach (nb::ls_dir($DIR_SQLITE,'\.db$') as $file) { if (strpos($file,'_conflict-')) continue; diff --git a/etc/dbq/nb.php b/etc/dbq/nb.php index 53724cc3..d63441bd 100644 --- a/etc/dbq/nb.php +++ b/etc/dbq/nb.php @@ -1,7 +1,8 @@ 'admin.izideal.vpn', +# NB 01.11.16 macmini router issue 'host' => 'admin.izideal.vpn', + 'host' => '10.8.0.1', 'type' => 'mysql', 'name' => 'nb', '_import' => ['_nico'], diff --git a/etc/dbq/ui.php b/etc/dbq/ui.php index 0ce75b64..6f1f2c72 100644 --- a/etc/dbq/ui.php +++ b/etc/dbq/ui.php @@ -156,7 +156,7 @@ $DBQ['_ui'] = array( ); if ($GLOBALS['web']) { - $DBQ['_ui']['people'] = $DBQ['_ui']['user'] = [ + $DBQ['_ui']['tables']['people'] = $DBQ['_ui']['tables']['user'] = [ 'extras'=> [ 'pic' => '\'\'', ], diff --git a/etc/dbq/wp.php b/etc/dbq/wp.php index d084af96..a1e20788 100644 --- a/etc/dbq/wp.php +++ b/etc/dbq/wp.php @@ -17,9 +17,9 @@ if (false and !empty($GLOBALS['wpdb'])) { $DBQ['wp']['host'] = DB_DIR.'/'.DB_FILE; } -$DBQ['wp-sqlite'] = array ( +if (!empty($DIR_SQLITE) and file_exists("$DIR_SQLITE/wp.db")) $DBQ['wp-sqlite'] = [ 'host' => "$DIR_SQLITE/wp.db", 'type' => 'sqlite', '_import' => 'wp', -); +]; ?> diff --git a/lib/php/db.php b/lib/php/db.php index 173bec36..753ab5ba 100644 --- a/lib/php/db.php +++ b/lib/php/db.php @@ -182,7 +182,7 @@ class Db extends nb { 'name' => 'dbname', 'charset' => 'charset', ] as $k => $v) { - #self::debug($k); + #self::debug([$k,$this->$k]); if ($v=='charset' and $this->type=='pgsql') continue; if (empty($this->$k)) continue; $this->pdo = preg_replace("/\b$v=[^;]*(;|$)/","",$this->pdo); @@ -1127,6 +1127,38 @@ class Db extends nb { return $dbs; } + public function table_row_encrypt(&$table,&$row) { + if (0 + or empty($this->encrypt) + or empty($this->encrypt['secret']) + or empty($this->encrypt['tables']) + or empty($this->encrypt['tables'][$table->name]) + ) return $row; + + foreach ($row as $k => $v) { + if (!in_array($k,$this->encrypt['tables'][$table->name])) continue; + $row[$k] = $this->encrypt($this->encrypt['secret'],$v); + } + + return $row; + } + + public function table_row_decrypt(&$table,&$row) { + if (0 + or empty($this->encrypt) + or empty($this->encrypt['secret']) + or empty($this->encrypt['tables']) + or empty($this->encrypt['tables'][$table->name]) + ) return $row; + + foreach ($row as $k => $v) { + if (!in_array($k,$this->encrypt['tables'][$table->name])) continue; + $row[$k] = $this->decrypt($this->encrypt['secret'],$v); + } + + return $row; + } + } # < Class ?> diff --git a/lib/php/db/field.php b/lib/php/db/field.php index 965c9ffd..5093319f 100644 --- a/lib/php/db/field.php +++ b/lib/php/db/field.php @@ -72,7 +72,7 @@ class field extends nb { if (strpos($this->type,'date') !== false) return 'date'; } - public function html_edit($value) { + public function html_edit($value,$type=null) { $size = ($this->size() and is_scalar($this->size())) ? $this->size() : 0; $tag = ( ($size>$this->textarea_size) or preg_match('/^(text|binary)/i',$this->type) ) ? 'textarea' : 'input'; @@ -84,7 +84,9 @@ class field extends nb { .' id="'.$this->name.'"' .( $size ? ' size="'.$size.'"' : '') #.' value="'.$value.'"' - .($tag == 'textarea' ? '>'.$value.'' : ' type="'.$this->html_type().'" value="'.$value.'" />') + .($tag == 'textarea' ? '>'.$value.'' : ' type="' + .($type ? $type : $this->html_type()) + .'" value="'.$value.'" />') ) .''.NB_EOL; diff --git a/lib/php/db/page.php b/lib/php/db/page.php index b4a87bc6..6a5dbc02 100644 --- a/lib/php/db/page.php +++ b/lib/php/db/page.php @@ -7,6 +7,7 @@ $Page = new Page([ 'call' => array( 'begin', #['out', "Hello World !!!\n"], + function($page){ global $Db, $Table; #$type = $page->mime2ext($page->content_type()); @@ -21,11 +22,11 @@ $Page = new Page([ #bye($type); $page->pdef('format','csv'); } - if ($page->action()) 1; - else return $Db->action($Db->p('action'),$Table); - #else require_once(dirname(__FILE__).'/index.php'); + if (!$page->action()) return $Db->action($Db->p('action'),$Table); }, + function($page){ + #if (!$page->is('html')) return; global $Db; if (!empty($Db)) echo $page->tag('div class="db-infos"',join(' | ',[ $Db->type, @@ -33,6 +34,7 @@ $Page = new Page([ ($Db->name ? $Db->name : null), ])); }, + 'end', ), ]); diff --git a/lib/php/db/table.php b/lib/php/db/table.php index 9ed3b6c1..5beed0cc 100644 --- a/lib/php/db/table.php +++ b/lib/php/db/table.php @@ -406,6 +406,9 @@ Class Table extends nb { echo '
'.NB_EOL; $count = 0; if ( $add or ($row = $st->fetch(PDO::FETCH_ASSOC, PDO::FETCH_ORI_NEXT))) { + $orig = $row; + + $this->db()->table_row_decrypt($this,$row); $count ++; foreach ($fields as $name => $field) { @@ -413,6 +416,7 @@ Class Table extends nb { elseif(!isset($row[$name])) $row[$name] = ''; echo $field->html_edit($row[$name] # NB 18.08.15 (! $add and array_key_exists($name,$row) ) ? $row[$name] : $field->default + #,( $orig[$name] === $row[$name] ? '' : 'password' ) ); } @@ -803,6 +807,7 @@ Class Table extends nb { $count++; $call = null; + $this->db()->table_row_decrypt($this,$row); if ($parse and !empty($this->db()->row_parse)) { $call = $this->db()->row_parse; $call($row); } @@ -1229,7 +1234,11 @@ Class Table extends nb { return false; } - foreach ($fields as $name => $field) $field->bindParam($query,$hvalues[$name],":$name"); + foreach ($fields as $name => $field) { + $row = [ $name => $hvalues[$name] ]; + $this->db()->table_row_encrypt($this,$row); + $field->bindParam($query,$row[$name],":$name"); + } if (self::p('debug')) { $this->debug($info,1); @@ -1305,8 +1314,9 @@ Class Table extends nb { } foreach ($fields as $name) { - #debug("$name: ".$hvalues[$name]); - $field->bindParam($query,$hvalues[$name],":$name"); + $row = [ $name => $hvalues[$name] ]; + $this->db()->table_row_encrypt($this,$row); + $field->bindParam($query,$row[$name],":$name"); } foreach ($keys as $name) { @@ -1356,11 +1366,8 @@ Class Table extends nb { $query = $this->db()->exec($sql); $info['rowCount'] = $query; + return $info['rowCount']; - $info['rowCount'] = $query->rowCount(); - return $info['rowCount']; - # Like insert- NB 05.04.16 -# NB 05.04.16 return $this->db()->exec($sql)->fetchColumn(); } public function out($v,$head=array()) { return $this->db()->out($v,$head); } diff --git a/lib/php/nb.php b/lib/php/nb.php index cae14907..2e70d6c5 100644 --- a/lib/php/nb.php +++ b/lib/php/nb.php @@ -797,7 +797,7 @@ class NB { } # < Class -#return; +return; if (!count($argv) or (string)$argv[1] != 'test') return; $value = 'Hello World !'; #$value = file_get_contents('/etc/hosts'); diff --git a/lib/php/page.php b/lib/php/page.php index 385e30b0..59bda74b 100644 --- a/lib/php/page.php +++ b/lib/php/page.php @@ -300,7 +300,7 @@ class Page extends nb { if (preg_match('/ml$/',self::$content_type)) { self::tag_end(); echo '' . NB_EOL; - echo '' . NB_EOL; + if (preg_match('/html$/',self::$content_type)) echo '' . NB_EOL; } }