'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']);
?>
public $id;
public $name;
public $title;
- public $notice;
public $type;
public $tables = [];
public $types = [];
* 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) {
public function tables() {
- if (!isset($this->_tables)) {
+ if (!$this->conf_type('nosql') and !isset($this->_tables)) {
+
#if (!isset($this->tables)) {
$this->_tables = true;
#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;
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;
}
}
public function status() {
-
$status = $new = [];
-#debug(array_keys($this->tables()));
$new = []
+[
'id' => (empty($this->id) ? '' : $this->id),
// Fields
if (isset($opt['fields'])) {
$this->add_fields($opt['fields']);
- $this->_fields = 1;
unset($opt['fields']);
}
*/
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;
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();
}
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
#
#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();
$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
#
}
$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;
} # < 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;
//
$tot = $query->fetch(PDO::FETCH_COLUMN);
$this->tot = $opt['tot'] = $tot;
+ } else {
+ $this->tot = $opt['tot'] = $count;
+
} # < is_html
if ($count === 0 and !$format) {
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') {
$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');
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;
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];
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']);
$i++;
- } while ($entry = ldap_next_entry($conn, $entry));
+ }
if ($fct) return $i;
return $info;
*/
}
+ /// 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() {}
}
# 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 = '<pre>'.NB_EOL.$v.NB_EOL.'</pre>';
+# NB 05.02.18 if (!$is_scalar and strpos($type,'html') !== false) $v = '<pre>'.NB_EOL.$v.NB_EOL.'</pre>';
return $v;
}
}
- $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,
} 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') {
$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;
form.edit,
.menu,
.block,
-iframe, pre
+iframe,
+pre
{
border-radius: 4px;
border-spacing: 0;