From: Nicolas Boisselier Date: Sun, 28 Jan 2018 04:32:31 +0000 (+0000) Subject: www/dbq/dbq.php X-Git-Url: https://git.nbdom.net/?a=commitdiff_plain;h=8fc013ef3fd2e2405b8ea757a4e6aee40e269bfd;p=nb.git www/dbq/dbq.php --- diff --git a/lib/php/ldap.php b/lib/php/ldap.php index 16e03c67..3978a799 100644 --- a/lib/php/ldap.php +++ b/lib/php/ldap.php @@ -3,122 +3,14 @@ require_once(realpath(dirname(__FILE__).'/../../lib/php/nb.php')); require_once(NB_ROOT.'/lib/php/http.php'); class Ldap { - -/* - public static function _connect(&$o=[]) { - $host = '127.0.0.1'; - if (preg_match('/^.*?([^\.]+\.[^\.]+)$/',$_SERVER['HTTP_HOST'],$m)) { - $host = 'ldap.'.$m[1]; - } - if (!isset($o['host'])) $o['host'] = $host; - if (!isset($o['base_dn'])) $o['base_dn'] = 'dc=' . str_replace('.',',dc=',preg_replace('/^ldap[^.]*./','',$host)); - - if (!isset($o['user'])) $o['user'] = Http::user(); - - if (!isset($o['password'])) $o['password'] = Http::password(); - - $o['bind_dn_preff'] = isset($o['bind_dn_preff']) ? ','.$o['bind_dn_preff'].',' : ''; - - if (!isset($o['bind_dn'])) $o['bind_dn'] = 'cn=' . $o['user'] . $o['bind_dn_preff'] . $o['base_dn']; - - if (empty($o['attrs'])) $o['attrs'] = []; - - if (!isset($o['limit'])) $o['limit'] = 0; - if (!isset($o['dn'])) $o['dn'] = true; - #return [$o]; - - if (!isset($o['exit_on_err'])) $o['exit_on_err'] = true; - #try { $connect = ldap_connect($o['host']); } catch (Exception $e) {}; if (!$connect) { - if (!$connect = @ldap_connect($o['host'])) { - if (empty($o['exit_on_err'])) return []; - self::bye("Could not connect to LDAP server"); - } - - ldap_set_option($connect, LDAP_OPT_PROTOCOL_VERSION, 3); - ldap_set_option($connect, LDAP_OPT_REFERRALS, 0); - - #try { $bind = ldap_bind($connect, $o['bind_dn'], $o['password']); } catch (Exception $e) {}; - if (!$bind = @ldap_bind($connect, $o['bind_dn'], $o['password'])) { - if (empty($o['exit_on_err'])) return []; - self::bye("Could not bind to LDAP server"); - } - - return $connect; - } - - public static function _search($o=[]) { - - $connect = self::_connect($o); - - if (empty($o['filter'])) { - $o['filter'] = "(&(userpassword=*))"; - $o['base_dn'] = $o['bind_dn']; - } - $read = ldap_search($connect, $o['base_dn'], $o['filter'], $o['attrs'], $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($connect, $read); - if ($entry !== false) do { - - $attributes = ldap_get_attributes($connect, $entry); - - for($j=0; $j<$attributes['count']; $j++) { - $values = ldap_get_values_len($connect, $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($connect,$entry)] + (empty($info[$i]) ? [] : $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($connect, $entry)); - - ldap_close($connect); - #bye($info); - return $info; - - $info = ldap_get_entries($connect, $read); - - $recs = []; - #debug($info); - for ($r=0; $r<$info["count"]; $r++){ - $rec = $info[$r]; - - for ($i=0; $i<$rec["count"]; $i++){ - # Fields - $key = $info[$r][$i]; - if (!empty($o['attrs']) and !in_array($key,$o['attrs'])) continue; - $recs[$r][$key] = []; - - # Values - if (isset($rec[$key]['count'])) for ($j=0; $j<$rec[$key]['count']; $j++){ - $recs[$r][$key][] = $rec[$key][$j]; - } - if (count($recs[$r][$key]) == 1) $recs[$r][$key] = $recs[$r][$key][0]; - - } - } - - ldap_close($connect); - #return $info; - return $recs; - - } # < ldap_search - - */ private $conn; private $host = '127.0.0.1'; private $base; private $user; private $password; + # NB 28.01.18: TODO + private $row_parse_pre; + private $row_parse_post; public function __construct($opt = []) { foreach ($opt as $k => $v) if ((string)$v !== '') $this->$k = $v; @@ -164,6 +56,8 @@ class Ldap { 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"); # ldap_get_entries is shite !! It does not workds with binary datas, put all attrs in lower case @@ -187,10 +81,16 @@ class Ldap { # Re-order by attrs if (!empty($o['attrs']) and !empty($info[$i])) $info[$i] = self::ar_filter_keys($info[$i],$o['attrs']); + if ($fct) { + $fct($info[$i]); + unset($info[$i]); + } + $i++; } while ($entry = ldap_next_entry($conn, $entry)); + if ($fct) return $i; return $info; } # < ldap_search diff --git a/www/dbq/dbq.php b/www/dbq/dbq.php index a94e299f..24f443a3 100644 --- a/www/dbq/dbq.php +++ b/www/dbq/dbq.php @@ -285,7 +285,7 @@ class DbQ extends nb { $this->page->body_class = '' .(empty($this->params['db']) ? '' : ' db-'.$this->params['db']) .(empty($this->params['table']) ? '' : ' table-'.$this->params['table']) - .( (empty($this->db) or empty($this->table)) ? '' : ' h1-to-select' ) + .( (empty($this->db) or empty($this->table)) ? '' : ' no-db' ) .(empty($this->params['action']) ? ' action-'.self::PARAM_DB_DEFAULT : ' action-'.($this->params['action'] == 'add' ? 'vi' : $this->params['action'])) ; @@ -530,7 +530,6 @@ class DbQ extends nb { }, ] : []; - #$this->table->rows($opt); $this->db()->__construct($opt); $this->table->rows(); unset($GLOBALS['dbq_args']); diff --git a/www/dbq/html/default.js b/www/dbq/html/default.js index 71ef3f13..44dd7a05 100644 --- a/www/dbq/html/default.js +++ b/www/dbq/html/default.js @@ -133,7 +133,7 @@ document.addEventListener("DOMContentLoaded", function() { document.querySelector("th.delete").appendChild(a); } - if (document.querySelector("body.h1-to-select")) document.querySelectorAll("body > h1 a.db, body > h1 a.table").forEach(function(e) { + if (document.querySelector("body.no-db")) document.querySelectorAll("body > h1 a.db, body > h1 a.table").forEach(function(e) { var base = window._dbq[e.className+'.base'].replace(/\/[^\/]+$/,''); var url = base + (base == '' ? '/ls' : '') + '.json'; console.log(url);