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;
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
# 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