From 98145a8acff740dabef304c64814f8385f73ddd8 Mon Sep 17 00:00:00 2001 From: Nicolas Boisselier Date: Tue, 23 Jan 2018 03:53:27 +0000 Subject: [PATCH] www/dbq/dbq.php --- lib/php/ldap.php | 59 ++++++++++++++++++------------------------------ www/dbq/dbq.php | 30 +++++++++++++++++++++--- 2 files changed, 49 insertions(+), 40 deletions(-) diff --git a/lib/php/ldap.php b/lib/php/ldap.php index c545c1c2..b03d7737 100644 --- a/lib/php/ldap.php +++ b/lib/php/ldap.php @@ -124,13 +124,13 @@ class Ldap { public $base; public $user; public $password; - public $search = false; + #public $search = false; private $conn; public function __construct($opt = []) { foreach ($opt as $k => $v) $this->$k = $v; - if (isset($this->search)) return $this->_search($this->search); + #if (isset($this->search)) return $this->_search($this->search); } public function __destruct() { @@ -155,68 +155,53 @@ class Ldap { ldap_close($this->conn); } - public function _search(&$o=[]) { + public function _search($o=[]) { - $connect = $this->conn(); + $conn = $this->conn(); - $read = ldap_search($connect, $o['base'], $o['filter'], $o['attrs'], $o['limit']) or self::bye("Unable to search ldap server"); + if (!isset($o['attrs'])) $o['attrs'] = []; + if (!isset($o['limit'])) $o['limit'] = 0; + if (!isset($o['base'])) $o['base'] = null; + if (!isset($o['filter'])) $o['filter'] = null; + $read = ldap_search($conn, $o['base'], $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); + $entry = ldap_first_entry($conn, $read); if ($entry !== false) do { - $attributes = ldap_get_attributes($connect, $entry); + $attributes = ldap_get_attributes($conn, $entry); for($j=0; $j<$attributes['count']; $j++) { - $values = ldap_get_values_len($connect, $entry,$attributes[$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($connect,$entry)] + (empty($info[$i]) ? [] : $info[$i]); + ) $info[$i] = ['dn' => ldap_get_dn($conn,$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)); + } while ($entry = ldap_next_entry($conn, $entry)); - #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_search - $o['count'] = count($rec); - #return $info; - return $recs; + public static function add($dn,$data) { + $conn = $this->conn(); + return ldap_add($conn, $dn, $data); # or self::bye("Unable to add entry to ldap server"); + } - } # < ldap_search - public static function add($o) { + public static function update($dn,$data) { + $conn = $this->conn(); + return ldap_mod_add($conn, $dn, $data); # or self::bye("Unable to add entry to ldap server"); } } diff --git a/www/dbq/dbq.php b/www/dbq/dbq.php index 47a96e12..0c57feb9 100644 --- a/www/dbq/dbq.php +++ b/www/dbq/dbq.php @@ -178,12 +178,36 @@ class DbQ extends nb { $attrs = explode(',',$this->params['table']); } - $rows = Ldap::search([ - 'bind_dn_preff' => (!empty($_SERVER['DBQ_LDAP_BIND_DN_PREFF']) ? 'ou='.$_SERVER['DBQ_LDAP_BIND_DN_PREFF'] : 'ou=auth'), + # Get server and base from host or ldap.conf + # NB 23.01.18: TODO + if (isset($_SERVER['HTTP_HOST']) and preg_match('/^.*?([^\.]+\.[^\.]+)$/',$_SERVER['HTTP_HOST'],$m)) { + $host = 'ldap.'.$m[1]; + $base = 'dc=' . str_replace('.',',dc=',preg_replace('/^ldap[^.]*./','',$host)); + } else { + $host = '127.0.0.1'; + $base = ''; + foreach (array_unique(glob('~/.ldap.conf')+glob('/etc/ldap/ldap.conf')) as $f) { + if (!is_readable($f)) continue; + foreach (file($f,FILE_SKIP_EMPTY_LINES + FILE_IGNORE_NEW_LINES) as $line) { + $line = trim($line); + if (preg_match('/^BASE\s+([^\s#]+)/i',$line,$m)) $base = $m[1]; + if (preg_match('/^HOST\s+([^\s#]+)/i',$line,$m)) $host = $m[1]; + } + break; + } + } + $preff = (!empty($_SERVER['DBQ_LDAP_BIND_DN_PREFF']) ? 'ou='.$_SERVER['DBQ_LDAP_BIND_DN_PREFF'] : 'ou=auth'); + #$base = $preff . $base; + #$ldap = new Ldap([ ]); + + $search = [ 'filter' => $filter, 'attrs' => $attrs, + ]; + $rows = Ldap::search([ + 'bind_dn_preff' => (!empty($_SERVER['DBQ_LDAP_BIND_DN_PREFF']) ? 'ou='.$_SERVER['DBQ_LDAP_BIND_DN_PREFF'] : 'ou=auth'), 'exit_on_err' => $this->p('err',true), - ]); + ]+$search); #echo $rows[0]['jpegPhoto']; exit; #$rows = [ 'jpegPhoto' => $rows[0]['jpegPhoto'] ]; -- 2.47.3