--- /dev/null
+<?php
+require_once(realpath(dirname(__FILE__).'/../../lib/php/nb.php'));
+require_once(NB_ROOT.'/lib/php/http.php');
+
+class Ldap extends nb {
+
+ 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
+
+}
return $new;
}
- public static function ldap_search($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::http_user();
-
- if (!isset($o['password'])) $o['password'] = Http::http_pass();
-
- $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");
- }
-
- 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
-
public static function date_microtime($format='Y-m-d H:i:s') {
#return date("Y-m-d H:i:s.u");
$micro_date = microtime();
}
public function page_ldap() {
+ require_once(NB_ROOT.'/lib/php/ldap.php');
#debug($this->params);
$this->deniedUnless($this->perm >= self::READ);
$filter = ($this->params['table'] ? $this->params['table'] : '');
$attrs = explode(',',$this->params['table']);
}
- $rows = $this->ldap_search([
+ $rows = Ldap::search([
'bind_dn_preff' => (!empty($_SERVER['DBQ_LDAP_BIND_DN_PREFF']) ? 'ou='.$_SERVER['DBQ_LDAP_BIND_DN_PREFF'] : 'ou=auth'),
'filter' => $filter,
'attrs' => $attrs,
-table,table.rows{border-collapse:collapse}.button,a,a:visited{text-decoration:none}html{font:90% 'Trebuchet MS',sans-serif;background-color:#EEE}body{display:table;margin-left:auto;margin-right:auto;padding-left:1em;padding-right:1em;color:#444}@keyframes spin{0%{transform:rotate(0)}100%{transform:rotate(360deg)}}.loader{border:16px solid #FFF;border-radius:50%;border-top:16px solid #000;padding:0;margin:0 auto;width:8em;height:8em;animation:spin 2s linear infinite;display:block}body.loader{margin-top:8em}.loader *{display:none!important}h1{margin:0 0 .5em;font-size:180%;padding-bottom:.3em;border-bottom:dashed 1px #444}h1 select{padding:0;border:none;background:inherit;font:inherit}a{color:inherit}.button,div>a,li a,p a,td a{color:#3572B0}.button:hover,a:hover{opacity:.8}ul{padding:0;margin:0}li{list-style:none;padding:0 0 0 .3em}td,th{padding:.2em .7em}th a.sort{float:left;margin-right:.1em}input:not([type]),input[type=text],input[type=password],input[type=date],input[type=email],input[type=url],input[type=''],select,textarea{border-radius:3px;border:1px solid #DDD;padding:.3em .4em;box-shadow:0 1px 1px #EEE}input[size]{max-width:70%}div.row div label:after,form label:after{content:':'}form input,form label,form select{vertical-align:middle;margin:.2em .4em .2em 0}.rows{padding:0}.rows form{display:inline-block}.block,.menu,div.row,form.edit,iframe,pre,table.rows,ul.row{border-radius:4px;border-spacing:0;padding:.5em .2em;margin-bottom:.5em;border:1px solid #DDD;background-color:#FAFAFA;box-shadow:0 1px 2px #EEE}table.rows{border-style:hidden;box-shadow:0 0 0 1px #DDD;table-layout:fixed}table.rows tr:nth-child(even) td{background-color:#FEFEFE}div.row div,table.rows td,table.rows th,ul.row li{border-bottom:solid 1px #DDD}div.row div,table.rows tr:last-child td,ul.row li{border-bottom:none}table.rows td,table.rows th{border-right:dashed 1px #DDD}.rows .count{text-align:right}table.rows th{text-align:left}div.buttons,div.row div.delete,div.row div.view{text-align:center}table.rows td{padding:.3em}table.rows.fixed{width:100%}div.row div label,table.rows.fixed td,table.rows.fixed th{white-space:nowrap;text-overflow:ellipsis;overflow:hidden}table.rows.fixed th{white-space:normal}table.rows td.delete,table.rows td.view{width:1%;white-space:nowrap}table.rows.fixed th.view{width:2em}table.rows.fixed th.delete{width:2.7em}div.rows{clear:both}div.row{float:left;margin-right:4px;min-width:24%}.nofloat{float:none}div.row div{margin:0;padding:.1em .2em;border-bottom:solid 1px #fff}div.row div.view .button{margin-bottom:5px}div.row div.delete .button{margin-top:5px}div.row div:last-child{border-bottom:none;padding-bottom:0}div.row div.delete label,div.row div.view label{display:none}div.row div label,ul.row li label{padding:.1em .5em .1em 0;min-width:20%;display:inline-block}.button{font:inherit;display:inline-block;line-height:1.6em;cursor:pointer;white-space:nowrap;box-sizing:border-box;margin:0 .1em 0 0;padding:0 .3em;border-radius:4px;border:none;background-color:#EEE;background:linear-gradient(to bottom right,#EEE,#DDD);box-shadow:0 1px 1px #BBB}.bottom,.center,.menu,.nav,.rows{clear:both;display:table}.bottom,.buttons,.center,.menu,.nav,.rows,td.button{margin-left:auto;margin-right:auto}div.buttons{margin-top:.5em}form.menu{padding:.5em;background-color:#FAFAFA;max-width:1024px}form.menu .button.add{float:right;margin-top:.2em}form.menu .criterias{border-top:dashed 1px #DDD;margin-top:.3em;padding-top:.3em;clear:both}form.menu .criterias span.label{border-right:solid 1px #fff;border-radius:3px;margin:0 .2em;white-space:nowrap;float:left}form.menu .criterias span.label:last-child{border-right:none}form.menu select:focus{max-width:auto}form.menu select{max-width:20%}form.menu .criterias input[type=text]{width:4em}div.row label,form.edit .fields label{width:25%;text-align:right;display:inline-block;margin-right:1em;vertical-align:top}div.row .label span,form.edit .fields .label span{width:54%;display:inline-block;text-align:left;vertical-align:middle;margin-top:.2em}form.edit .fields input[size],form.edit .fields select,form.edit .fields textarea{width:65%}
+html{font:90% 'Trebuchet MS',sans-serif;background-color:#eee}body{display:table;margin-left:auto;margin-right:auto;padding-left:1em;padding-right:1em;color:#444}@keyframes spin{0%{transform:rotate(0)}100%{transform:rotate(360deg)}}.loader{border:16px solid #fff;border-radius:50%;border-top:16px solid #000;padding:0;margin:0 auto 0 auto;width:8em;height:8em;animation:spin 2s linear infinite;display:block}body.loader{margin-top:8em}.loader *{display:none!important}h1{margin:0 0 .2em 0;font-size:180%;padding-bottom:.3em;margin-bottom:.5em;border-bottom:dashed 1px #444}h1 select{padding:0;border:none;background:inherit;font:inherit}a{color:inherit}div>a,li a,p a,td a{color:#3572b0}a,a:visited{text-decoration:none}.button:hover,a:hover{opacity:.8}ul{padding:0;margin:0}li{list-style:none;padding:0 0 0 .3em}table{border-collapse:collapse}td,th{padding:.2em .7em}th a.sort{float:left;margin-right:.1em}input:not([type]),input[type=''],input[type=date],input[type=email],input[type=password],input[type=text],input[type=url],select,textarea{border-radius:3px;border:solid 1px #ddd;padding:.3em .4em;box-shadow:0 1px 1px #eee}input[size]{max-width:70%}div.row div label:after,form label:after{content:':'}form input,form label,form select{vertical-align:middle;margin:.2em .4em .2em 0}.rows{padding:0}.rows form{display:inline-block}.block,.menu,div.row,form.edit,iframe,pre,table.rows,ul.row{border-radius:4px;border-spacing:0;padding:.5em .2em;margin-bottom:.5em;border:solid 1px #ddd;background-color:#fafafa;box-shadow:0 1px 2px #eee}table.rows{border-style:hidden;border-collapse:collapse;box-shadow:0 0 0 1px #ddd;table-layout:fixed}table.rows tr:nth-child(even) td{background-color:#fefefe}div.row div,table.rows td,table.rows th,ul.row li{border-bottom:solid 1px #ddd}div.row div,table.rows tr:last-child td,ul.row li{border-bottom:none}table.rows td,table.rows th{border-right:dashed 1px #ddd}.rows .count{text-align:right}table.rows th{text-align:left}table.rows td{padding:.3em .3em}table.rows.fixed{width:100%}div.row div label,table.rows.fixed td,table.rows.fixed th{white-space:nowrap;text-overflow:ellipsis;overflow:hidden}table.rows.fixed th{white-space:normal}table.rows td.delete,table.rows td.view{width:1%;white-space:nowrap}table.rows.fixed th.view{width:2em}table.rows.fixed th.delete{width:2.7em}div.rows{clear:both}div.row{float:left;margin-right:4px;min-width:24%}.nofloat{float:none}div.row div{margin:0;padding:.1em .2em;border-bottom:solid 1px #fff}div.row div.view .button{margin-bottom:5px}div.row div.delete .button{margin-top:5px}div.row div:last-child{border-bottom:none;padding-bottom:0}div.row div.delete label,div.row div.view label{display:none}div.row div.delete,div.row div.view{text-align:center}div.row div label,ul.row li label{padding:.1em .5em .1em 0;min-width:20%;display:inline-block}.button{font:inherit;display:inline-block;text-decoration:none;line-height:1.6em;cursor:pointer;white-space:nowrap;box-sizing:border-box;margin:0 .1em 0 0;padding:0 .3em;border-radius:4px;border:none;color:#3572b0;background-color:#eee;background:linear-gradient(to bottom right,#eee,#ddd);box-shadow:0 1px 1px #bbb}.bottom,.center,.menu,.nav,.rows{clear:both;display:table}.bottom,.buttons,.center,.menu,.nav,.rows,td.button{margin-left:auto;margin-right:auto}div.buttons{text-align:center;margin-top:.5em}form.menu{padding:.5em;background-color:#fafafa;max-width:1024px}form.menu .button.add{float:right;margin-top:.2em}form.menu .criterias{border-top:dashed 1px #ddd;margin-top:.3em;padding-top:.3em;clear:both}form.menu .criterias span.label{border-right:solid 1px #fff;border-radius:3px;margin:0 .2em;white-space:nowrap;float:left}form.menu .criterias span.label:last-child{border-right:none}form.menu select:focus{max-width:auto}form.menu select{max-width:20%}form.menu .criterias input[type=text]{width:4em}div.row label,form.edit .fields label{width:25%;text-align:right;display:inline-block;margin-right:1em;vertical-align:top}div.row .label span,form.edit .fields .label span{width:54%;display:inline-block;text-align:left;vertical-align:middle;margin-top:.2em}form.edit .fields input[size],form.edit .fields select,form.edit .fields textarea{width:65%}