From 6bd6fba00e2013fa415e062ee37b1c128d398e85 Mon Sep 17 00:00:00 2001 From: Nicolas Boisselier Date: Mon, 5 Feb 2018 16:47:43 +0000 Subject: [PATCH] lib/php/db/table.php --- etc/dbq/ldap.php | 18 ++++++- lib/php/db.php | 17 +++---- lib/php/db/table.php | 41 ++++++++++------ lib/php/db/types/ldap.php | 8 ++++ lib/php/ldap.php | 98 ++++++++++++++++++++++++++++----------- lib/php/out.php | 2 +- www/dbq/dbq.php | 12 ++--- www/dbq/html/default.css | 3 +- 8 files changed, 137 insertions(+), 62 deletions(-) diff --git a/etc/dbq/ldap.php b/etc/dbq/ldap.php index 4b8b1ce5..edebe263 100644 --- a/etc/dbq/ldap.php +++ b/etc/dbq/ldap.php @@ -39,8 +39,22 @@ $GLOBALS['DBQ_LDAP'] = [ 'host' => $host, 'base' => $base, ]; -$DBQ['ldap'] = $GLOBALS['DBQ_LDAP'] + [ - 'type' => 'ldap', +#'(&('.$search['filter'].')(userPassword=*))' +if (preg_match('/^([^,]+),(.*?)$/',$user,$m) +) { + $filter = $m[1]; +} else { + $filter = 'cn='.Http::user(); +} +$filter = '(&('.$filter.')(userPassword=*))'; +$DBQ['ldap2'] = $GLOBALS['DBQ_LDAP'] + [ + 'type' => 'ldap', + 'tables' => [ + $filter => [ + 'fields' => ['dn','objectClass'], + ], + ], + 'default_table' => $filter, ]; #debug($GLOBALS['DBQ_LDAP']); ?> diff --git a/lib/php/db.php b/lib/php/db.php index 94cdb078..59f3b4d2 100644 --- a/lib/php/db.php +++ b/lib/php/db.php @@ -49,7 +49,6 @@ class Db extends nb { public $id; public $name; public $title; - public $notice; public $type; public $tables = []; public $types = []; @@ -370,12 +369,17 @@ class Db extends nb { * Return a table instance */ public function table($name='',$params=[]) { - if ( !is_array($params) ) $this->bye('Usage: table($name,[type=>, sql=>, ...])'); + if (is_array($name)) { + $params = $name; + $name = $params['name']; + } + if (!is_array($params)) $this->bye('Usage: table($name,[type=>, sql=>, ...])'); if (!$name and !empty($this->default_table)) $name = $this->default_table; if (!$name) $this->bye('table(): Missing table'); if (empty($params) or empty($params['db'])) $params['db'] = $this; + $params['name'] = $name; if (empty($this->tables[$name])) { $this->tables[$name] = new Table($name,$params); } elseif ($params) { @@ -504,7 +508,8 @@ class Db extends nb { public function tables() { - if (!isset($this->_tables)) { + if (!$this->conf_type('nosql') and !isset($this->_tables)) { + #if (!isset($this->tables)) { $this->_tables = true; @@ -593,10 +598,8 @@ class Db extends nb { #var_dump ($this->tables()); #foreach($this->tables as $t) debug($t->name.'='.$t->type); -#bye($this->tables); $rows = []; foreach ($this->tables() as $t) { - #debug($t->name.'='.$t->type); if ($name and !$this->str_match($t->name,$name)) continue; if ($type and !$this->str_match($t->type,$type)) continue; @@ -605,11 +608,11 @@ class Db extends nb { if ($database and isset($t->database) and !$this->str_match($t->database,$database)) continue; $row = $t->status(); + if (!$row) $row = ['name' => $t->name]; if ($count) $row['count'] = $t->status('count'); #if ($fields) $row['fields'] = $t->status('fields'); if ($fields) $row['fields'] = join(',',array_keys($t->fields())); - #debug($t->name.'='.$t->database); $rows[] = $row; } @@ -1084,10 +1087,8 @@ class Db extends nb { } public function status() { - $status = $new = []; -#debug(array_keys($this->tables())); $new = [] +[ 'id' => (empty($this->id) ? '' : $this->id), diff --git a/lib/php/db/table.php b/lib/php/db/table.php index f3c11a7f..9be8e218 100644 --- a/lib/php/db/table.php +++ b/lib/php/db/table.php @@ -75,7 +75,6 @@ Class Table extends nb { // Fields if (isset($opt['fields'])) { $this->add_fields($opt['fields']); - $this->_fields = 1; unset($opt['fields']); } @@ -362,7 +361,7 @@ Class Table extends nb { */ public function fields($name=null) { - if (!isset($this->_fields)) { + if (!$this->db()->conf_type('nosql') and!isset($this->_fields)) { $this->create_temporary(); $this->_fields = true; @@ -461,7 +460,7 @@ Class Table extends nb { return $url ? $preff.join($sep,$url) : ''; } - private function fields_keys_values($values) { + public function fields_keys_values($values) { $keys = $this->fields_keys(); if (empty($keys)) $keys = $this->fields(); @@ -1052,11 +1051,6 @@ Class Table extends nb { } public function rows(&$opt=[]) { # ,$opt_by_val=null) { -# NB 30.01.18 if ($opt_by_val !== null) $opt = $opt_by_val; - -# NB 29.01.18 if ($fct = $this->db()->conf_type('_table_rows')) { -# NB 29.01.18 return $fct($opt); -# NB 29.01.18 } # # Db type change # @@ -1086,7 +1080,20 @@ Class Table extends nb { #debug($opt); list($sql,$where,$limit,$select_count) = $this->rows_sql($opt); + $nosql = $this->db()->conf_type('nosql'); + + if ($nosql) { + $sql = $this->name; + $fct = $this->db()->conf_type('prepare'); + if ($fct) $st = $fct($this,$sql); + if (!$fct = $this->db()->conf_type('fetch')) { + $fct = function() use($st) { return $st->fetch(PDO::FETCH_ASSOC, PDO::FETCH_ORI_NEXT); }; + } + #$fct = function() { return false; }; + #$fct = function() use($st) { return $st->fetch(PDO::FETCH_ASSOC, PDO::FETCH_ORI_NEXT); }; + } else { + if ($this->db()->type == 'pgsql') { $sql_orig = $sql; $this->db()->conn->beginTransaction(); @@ -1101,12 +1108,12 @@ Class Table extends nb { $st = $this->db()->conn->prepare($sql);#,[PDO::ATTR_ERRMODE => PDO::ERRMODE_SILENT]); if (empty($cursor)) { - $st->execute(); + $fct = function() use($st) { return $st->fetch(PDO::FETCH_ASSOC, PDO::FETCH_ORI_NEXT); }; } else { - $fct = function() use($st) { return $st->execute(); }; + $fct = function() use($st) { if ($st->execute()) return $st->fetch(PDO::FETCH_ASSOC, PDO::FETCH_ORI_NEXT); }; } +} if (!empty($opt['query'])) return $st; - # # Use the module out when format unknow # @@ -1161,7 +1168,8 @@ Class Table extends nb { } $count = 0; - while ((empty($fct) or $fct()) and $row = $st->fetch(PDO::FETCH_ASSOC, PDO::FETCH_ORI_NEXT)) { + #while ((empty($fct) or $fct()) and $row = $st->fetch(PDO::FETCH_ASSOC, PDO::FETCH_ORI_NEXT)) { + while ($row = $fct($this)) { $count++; $call = null; @@ -1224,14 +1232,14 @@ Class Table extends nb { } # < while rows - $st->closeCursor(); + if (!empty($st)) $st->closeCursor(); if (!empty($cursor)) $cursor->closeCursor(); if (!$count) return; $this->count = $opt['count'] = $count; $this->limit = $opt['limit'] = $limit; - if ($opt['is_html'] or $format=='wp' or !empty($cursor)) { + if (!$nosql and ($opt['is_html'] or $format=='wp' or !empty($cursor))) { if (!empty($sql_orig)) $sql = $sql_orig; // @@ -1262,6 +1270,9 @@ Class Table extends nb { $tot = $query->fetch(PDO::FETCH_COLUMN); $this->tot = $opt['tot'] = $tot; + } else { + $this->tot = $opt['tot'] = $count; + } # < is_html if ($count === 0 and !$format) { @@ -2228,6 +2239,8 @@ Class Table extends nb { public function status($key=null) { + if ($this->db()->conf_type('nosql') and !isset($this->status)) $this->status = []; + if (!isset($this->status)) { if ($this->type == 'sql') { diff --git a/lib/php/db/types/ldap.php b/lib/php/db/types/ldap.php index d7feb19c..6687913d 100644 --- a/lib/php/db/types/ldap.php +++ b/lib/php/db/types/ldap.php @@ -6,6 +6,14 @@ if (!class_exists('Db')) { $DB_TYPES['ldap'] = [ 'quote_name' => '', 'quote' => function($v) { return $v; }, + 'nosql' => true, + #'prepare' => function($sql) { return $this->ldap->prepare($sql); }, + 'prepare' => function($table,$sql) { $table->db()->ldap->prepare($sql); return $table->db()->ldap; }, + 'fetch' => function($table) { + $row = $table->db()->ldap->fetch(PDO::FETCH_ASSOC, PDO::FETCH_ORI_NEXT); + $row = $table->db()->ldap->ar_filter_keys($row,array_keys($table->fields())); + return $row; + }, '_connect' => function($db) { require_once(NB_ROOT.'/lib/php/ldap.php'); diff --git a/lib/php/ldap.php b/lib/php/ldap.php index 222ff8cb..75d00916 100644 --- a/lib/php/ldap.php +++ b/lib/php/ldap.php @@ -3,16 +3,16 @@ require_once(realpath(dirname(__FILE__).'/../../lib/php/nb.php')); require_once(NB_ROOT.'/lib/php/http.php'); class Ldap { - private $conn; private $host = '127.0.0.1'; private $base; private $user; private $password; private $fields; - # NB 28.01.18: TODO - private $row_parse_pre; - private $row_parse_post; + + private $conn; + private $read; + private $entry; public function __construct($opt = []) { foreach ($opt as $k => $v) if ((string)$v !== '') $this->$k = $v; @@ -24,7 +24,7 @@ class Ldap { public static function bye($msg) { die($msg); } - private static function ar_filter_keys($ar,$keys) { + public static function ar_filter_keys($ar,$keys) { $new = []; foreach ($keys as $k) { if (isset($ar[$k])) $new[$k] = $ar[$k]; @@ -48,43 +48,48 @@ class Ldap { ldap_close($this->conn); } -# NB 30.01.18 public function fields($set) { -# NB 30.01.18 if (isset($set)) return $this->fields; -# NB 30.01.18 if (isset($this->fields)) return $this->fields; -# NB 30.01.18 } + private function entry($entry=null,$dn=true) { + $row = []; + + if ($entry === null) $entry = $this->entry; + $conn = $this->connect(); + $attributes = ldap_get_attributes($conn, $entry); + + for($j=0; $j<$attributes['count']; $j++) { + $values = ldap_get_values_len($conn, $entry,$attributes[$j]); + unset($values['count']); + if (isset($values) and count($values) == 1) $values = $values[0]; + $row[$attributes[$j]] = $values; + } + + if ($dn) $row['dn'] = ldap_get_dn($conn,$entry); + + return $row; + } public function search($o=[]) { $conn = $this->connect(); if (!isset($o['dn'])) $o['dn'] = true; - if (!isset($o['base'])) $o['base'] = null; + if (!isset($o['base'])) $o['base'] = $this->base; if (!isset($o['filter'])) $o = array_merge($o,[ 'filter' => 'objectClass=*', 'attrs' => ['dn','objectClass'], 'dn' => true ]); if (!isset($o['attrs'])) $o['attrs'] = []; if (!is_array($o['attrs'])) $o['attrs'] = [$o['attrs']]; if (!isset($o['limit'])) $o['limit'] = 0; $fct = empty($o['fct']) ? '' : $o['fct']; - $read = ldap_search($conn, $o['base'], $o['filter'], $o['attrs'], 0, $o['limit']) or self::bye("Unable to search ldap server"); +# NB 05.02.18 $read = ldap_search($conn, $o['base'], $o['filter'], $o['attrs'], 0, $o['limit']) or self::bye("Unable to search ldap server"); # ldap_get_entries is shite !! It does not workds with binary datas, put all attrs in lower case $info = []; $i = 0; - $entry = ldap_first_entry($conn, $read); - if ($entry !== false) do { - - $attributes = ldap_get_attributes($conn, $entry); - - for($j=0; $j<$attributes['count']; $j++) { - $values = ldap_get_values_len($conn, $entry,$attributes[$j]); - unset($values['count']); - if (isset($values) and count($values) == 1) $values = $values[0]; - $info[$i][$attributes[$j]] = $values; - } - - if (!empty($o['dn']) and - in_array('dn',!empty($o['attrs']) ? $o['attrs'] : ['dn']) - ) $info[$i] = ['dn' => ldap_get_dn($conn,$entry)] + (empty($info[$i]) ? [] : $info[$i]); + $dn = (bool)(!empty($o['dn']) and in_array('dn',!empty($o['attrs']) ? $o['attrs'] : ['dn'])); +# NB 05.02.18 while ($entry = $this->fetch()) { +# NB 05.02.18 $info[$i] = $this->entry($entry,$dn); + if ($this->prepare($o)) while ($row = $this->fetch($dn)) { + $info[$i] = $row; + #bye($info[$i]); # Re-order by attrs if (!empty($o['attrs']) and !empty($info[$i])) $info[$i] = self::ar_filter_keys($info[$i],$o['attrs']); @@ -96,7 +101,7 @@ class Ldap { $i++; - } while ($entry = ldap_next_entry($conn, $entry)); + } if ($fct) return $i; return $info; @@ -127,4 +132,43 @@ class Ldap { */ } + /// PDO compatible + public function setAttribute($k,$v) { } + + public function prepare($sql,$o=[]) { + if (is_array($sql)) { + $o = $sql; + $sql = $o['filter']; + } + + if (!isset($o['base'])) $o['base'] = $this->base; + #if (!isset($o['filter'])) $o = array_merge($o,[ 'filter' => 'objectClass=*', 'attrs' => ['dn','objectClass'], 'dn' => true ]); + $o['filter'] = $sql; + if (!isset($o['attrs'])) $o['attrs'] = []; + if (!is_array($o['attrs'])) $o['attrs'] = [$o['attrs']]; + if (!isset($o['limit'])) $o['limit'] = 0; + + $conn = $this->connect(); + $this->read = ldap_search($conn, $o['base'], $o['filter'], $o['attrs'], 0, $o['limit']) or self::bye("Unable to search ldap server"); + + $this->fetch(null); + return $this->read; + } + + public function fetch($type='',$opt='') { + static $count = 0; + if ($type === null) { + $count = 0; + return true; + } + $conn = $this->connect(); + $this->entry = ( $count ? ldap_next_entry($conn, $this->entry) : ldap_first_entry($conn, $this->read)); + if (!$this->entry) return null; + $count++; + + $dn = ($type === false ? false : true); + return $this->entry($this->entry,$dn); + } + + public function closeCursor() {} } diff --git a/lib/php/out.php b/lib/php/out.php index f8296615..439fe570 100644 --- a/lib/php/out.php +++ b/lib/php/out.php @@ -326,7 +326,7 @@ Class Out extends Nb { # NB 07.01.18 if (empty($o['no_escape'])) $v = self::escape($v); if (!empty($o['escape'])) $v = self::escape($v); - if (!$is_scalar and strpos($type,'html') !== false) $v = '
'.NB_EOL.$v.NB_EOL.'
'; +# NB 05.02.18 if (!$is_scalar and strpos($type,'html') !== false) $v = '
'.NB_EOL.$v.NB_EOL.'
'; return $v; } diff --git a/www/dbq/dbq.php b/www/dbq/dbq.php index 6fe519e4..f6ef67ff 100644 --- a/www/dbq/dbq.php +++ b/www/dbq/dbq.php @@ -607,7 +607,9 @@ class DbQ extends nb { } - $this->table = $this->db->table($this->params['table'],array_merge([ + $this->table = $this->db->table(array_merge([ + 'name' => $this->params['table'], + 'db' => $this->db, 'db' => $this->db, 'show_hidden_params' => false, 'show_buttons' => false, @@ -1151,10 +1153,6 @@ EOF; } elseif ($action == 'ldap') { $table = $this->params['table']; $attrs = ($this->params['action'] ? explode(',',$this->params['action']) : ['dn','objectClass']); - if (false and $this->params['format'] == $this->format_html) { - $this->params['format'] = 'div'; - $this->run_init(); - } $this->page($this->ldap($table,$attrs)); } elseif ($action == 'phpinfo') { @@ -1246,10 +1244,6 @@ EOF; $action = $this->params['action']; $param = []; - if ($this->db->type == 'ldap') { - $param['fields'] = ($action ? explode(',',$action) : ['dn','objectClass']); - debug($param); - } $this->table($param); # Table init if (!$action and self::PARAM_DB_DEFAULT) $action = self::PARAM_DB_DEFAULT; diff --git a/www/dbq/html/default.css b/www/dbq/html/default.css index 31d8a69e..50b8a6ec 100644 --- a/www/dbq/html/default.css +++ b/www/dbq/html/default.css @@ -125,7 +125,8 @@ ul.row, form.edit, .menu, .block, -iframe, pre +iframe, +pre { border-radius: 4px; border-spacing: 0; -- 2.47.3