From c2956a569ecb991a42a385c5e16fa12f526f30d9 Mon Sep 17 00:00:00 2001 From: Nicolas Boisselier Date: Tue, 8 Oct 2024 08:36:48 +0200 Subject: [PATCH] lib/php/db/types/ldap.php --- lib/php/db/field.php | 1 - lib/php/db/types/ldap.php | 23 +++---- lib/php/ldap.php | 1 - www/dbq/dbq.php | 125 +++++++++++++++++++------------------- 4 files changed, 74 insertions(+), 76 deletions(-) diff --git a/lib/php/db/field.php b/lib/php/db/field.php index 8019eb0f..139403fe 100644 --- a/lib/php/db/field.php +++ b/lib/php/db/field.php @@ -139,7 +139,6 @@ class field extends nb { $html = ''; $multi = is_array($values) ? true : false; - #if ($db = $this->db() and $db->type == 'ldap') $multi = true; if ($db = $this->db() and $fct=$db->conf_type('html_edit_multi')) $multi = $fct($this); $suff = $multi ? '[]' : ''; if ($type === null) $type = $this->html_type(); diff --git a/lib/php/db/types/ldap.php b/lib/php/db/types/ldap.php index 354825bd..9e11c8a0 100644 --- a/lib/php/db/types/ldap.php +++ b/lib/php/db/types/ldap.php @@ -3,6 +3,7 @@ if (!class_exists('Db')) { echo "Hi there! I'm just a plugin, not much I can do when called directly.\n"; exit; } +$LDAP_CONN = null; $DB_TYPES['ldap'] = [ 'quote_name' => '', 'quote' => function($v) { return $v; }, @@ -10,25 +11,25 @@ $DB_TYPES['ldap'] = [ '_connect' => function($db) { require_once(NB_ROOT.'/lib/php/ldap.php'); - $db->ldap = new Ldap([ + $LDAP_CONN = new Ldap([ 'host' => $db->host, 'user' => $db->user, 'password' => $db->password, 'base' => $db->name, ]); - return $db->ldap->connect(); + return $LDAP_CONN->connect(); }, 'rows_prepare' => function($table,$sql) { $fields = (empty($table->fields_only) ? array_keys($table->fields()) : $table->fields_only); $table->replace_fields($fields); - $table->db()->ldap->prepare($sql,['attrs'=>$fields]); return $table->db()->ldap; + $LDAP_CONN->prepare($sql,['attrs'=>$fields]); return $LDAP_CONN; }, 'rows_fetch' => function($table) { - $row = $table->db()->ldap->fetch(); + $row = $LDAP_CONN->fetch(); if (!$row) return []; $fields = (empty($table->fields_only) ? array_keys($table->fields()) : $table->fields_only); - $row = $table->db()->ldap->ar_filter_keys($row,$fields); + $row = $LDAP_CONN->ar_filter_keys($row,$fields); $table->fields_only($row,$fields); # reorder by fields return $row; }, @@ -58,12 +59,12 @@ $DB_TYPES['ldap'] = [ foreach ($m[1] as $v) { $row['objectClass'][] = $v; } - $row['dn'] = 'cn=CHANGEME,'.$table->db()->ldap->base(); + $row['dn'] = 'cn=CHANGEME,'.$LDAP_CONN->base(); } else { - $table->db()->ldap->prepare([ + $LDAP_CONN->prepare([ 'base' => $values['dn'], ]); - $row = $table->db()->ldap->fetch(); + $row = $LDAP_CONN->fetch(); } # Sort fields @@ -89,15 +90,15 @@ $DB_TYPES['ldap'] = [ )) unset($values[$k]); } #bye([$dn,$values]); - return (int)$table->db()->ldap->add($dn,$values); + return (int)$LDAP_CONN->add($dn,$values); }, 'delete' => function(&$table,&$values) { $dn = $values['dn']; unset($values['dn']); - return (int)$table->db()->ldap->delete($dn); + return (int)$LDAP_CONN->delete($dn); }, 'update' => function(&$table,&$values) { $dn = $values['dn']; unset($values['dn']); - return (int)$table->db()->ldap->update($dn,$values); + return (int)$LDAP_CONN->update($dn,$values); }, ]; ?> diff --git a/lib/php/ldap.php b/lib/php/ldap.php index bfe82b47..602de3f4 100644 --- a/lib/php/ldap.php +++ b/lib/php/ldap.php @@ -87,7 +87,6 @@ class Ldap { if (!isset($o['limit'])) $o['limit'] = 0; $fct = empty($o['fct']) ? '' : $o['fct']; -# 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 = []; diff --git a/www/dbq/dbq.php b/www/dbq/dbq.php index 6f6dd501..e5a8ec65 100644 --- a/www/dbq/dbq.php +++ b/www/dbq/dbq.php @@ -191,64 +191,64 @@ class DbQ extends nb { } - public function ldap($table='',$attrs=[]) { - $this->deniedUnless($this->perm >= self::ADMIN); - - require_once(NB_ROOT.'/lib/php/ldap.php'); - #debug($GLOBALS['DBQ_LDAP']); - $dn = join(',',$attrs); - $ldap = new Ldap($GLOBALS['DBQ_LDAP']); - - if ($table == 'add') { - $this->deniedUnless($this->perm >= self::WRITE); - $rows = (int)$ldap->add($dn,$_POST); - - } elseif ($table == 'update') { - $this->deniedUnless($this->perm >= self::WRITE); - $rows = (int)$ldap->update($dn,$_POST); - - } elseif ($table == 'rm') { - $this->deniedUnless($this->perm >= self::DELETE); - $rows = (int)$ldap->delete($dn,$_POST); - - } else { // ls - $this->deniedUnless($this->perm >= self::READ); - - $filter = ($table ? $table : ''); - - if (!empty($table) and !strpos($table,'=')) { - $filter = ''; - $attrs = explode(',',$table); - } - - $search = [ - 'filter' => $filter, - 'attrs' => $attrs, - ]; - - if (!$search['filter']) { - if (1 - and !empty($GLOBALS['DBQ_LDAP']['user']) - and preg_match('/^([^,]+),(.*?)$/',$GLOBALS['DBQ_LDAP']['user'],$m) - ) { - $search['filter'] = $m[1]; - $search['base'] = $m[2]; - } else { - $search['filter'] = 'cn='.Http::user(); - } - $search['filter'] = '(&('.$search['filter'].')(userPassword=*))'; - } - $rows = $ldap->search($GLOBALS['DBQ_LDAP'] + $search); - - if ($this->is_html - or ($this->params['format'] == $this->format_cli) - or ($this->params['format'] == 'csv') - ) $rows = $this->array_fill_assoc($rows); - - } - - return $rows; - } + # NB 08.10.24 public function ldap($table='',$attrs=[]) { + # NB 08.10.24 $this->deniedUnless($this->perm >= self::ADMIN); +# NB 08.10.24 + # NB 08.10.24 require_once(NB_ROOT.'/lib/php/ldap.php'); + # NB 08.10.24 #debug($GLOBALS['DBQ_LDAP']); + # NB 08.10.24 $dn = join(',',$attrs); + # NB 08.10.24 $ldap = new Ldap($GLOBALS['DBQ_LDAP']); +# NB 08.10.24 + # NB 08.10.24 if ($table == 'add') { + # NB 08.10.24 $this->deniedUnless($this->perm >= self::WRITE); + # NB 08.10.24 $rows = (int)$ldap->add($dn,$_POST); +# NB 08.10.24 + # NB 08.10.24 } elseif ($table == 'update') { + # NB 08.10.24 $this->deniedUnless($this->perm >= self::WRITE); + # NB 08.10.24 $rows = (int)$ldap->update($dn,$_POST); +# NB 08.10.24 + # NB 08.10.24 } elseif ($table == 'rm') { + # NB 08.10.24 $this->deniedUnless($this->perm >= self::DELETE); + # NB 08.10.24 $rows = (int)$ldap->delete($dn,$_POST); +# NB 08.10.24 + # NB 08.10.24 } else { // ls + # NB 08.10.24 $this->deniedUnless($this->perm >= self::READ); +# NB 08.10.24 + # NB 08.10.24 $filter = ($table ? $table : ''); +# NB 08.10.24 + # NB 08.10.24 if (!empty($table) and !strpos($table,'=')) { + # NB 08.10.24 $filter = ''; + # NB 08.10.24 $attrs = explode(',',$table); + # NB 08.10.24 } +# NB 08.10.24 + # NB 08.10.24 $search = [ + # NB 08.10.24 'filter' => $filter, + # NB 08.10.24 'attrs' => $attrs, + # NB 08.10.24 ]; +# NB 08.10.24 + # NB 08.10.24 if (!$search['filter']) { + # NB 08.10.24 if (1 + # NB 08.10.24 and !empty($GLOBALS['DBQ_LDAP']['user']) + # NB 08.10.24 and preg_match('/^([^,]+),(.*?)$/',$GLOBALS['DBQ_LDAP']['user'],$m) + # NB 08.10.24 ) { + # NB 08.10.24 $search['filter'] = $m[1]; + # NB 08.10.24 $search['base'] = $m[2]; + # NB 08.10.24 } else { + # NB 08.10.24 $search['filter'] = 'cn='.Http::user(); + # NB 08.10.24 } + # NB 08.10.24 $search['filter'] = '(&('.$search['filter'].')(userPassword=*))'; + # NB 08.10.24 } + # NB 08.10.24 $rows = $ldap->search($GLOBALS['DBQ_LDAP'] + $search); +# NB 08.10.24 + # NB 08.10.24 if ($this->is_html + # NB 08.10.24 or ($this->params['format'] == $this->format_cli) + # NB 08.10.24 or ($this->params['format'] == 'csv') + # NB 08.10.24 ) $rows = $this->array_fill_assoc($rows); +# NB 08.10.24 + # NB 08.10.24 } +# NB 08.10.24 + # NB 08.10.24 return $rows; + # NB 08.10.24 } public function page_table_ls($fields=null) { $this->deniedUnless($this->perm >= self::READ); @@ -1233,7 +1233,6 @@ EOF; ( ($this->perm < self::ADMIN) ? [] : [ - # NB 07.02.18 [ 'ldap', 'Connect to domain ldap server [/FILTER/ATTRS' ], [ 'phpinfo', 'Phpinfo' ], [ '_SERVER', 'Dump _SERVER' ], [ '_REQUEST', 'Dump _REQUEST' ], @@ -1280,10 +1279,10 @@ EOF; # NB 14.12.22 $this->deniedUnless($this->perm >= self::ADMIN); # NB 14.12.22 $this->page($this->db->databases()); - } elseif ($action == 'ldap2') { - $table = $this->params['table']; - $attrs = ($this->params['action'] ? explode(',',$this->params['action']) : ['dn','objectClass']); - $this->page($this->ldap($table,$attrs)); + # NB 08.10.24 } elseif ($action == 'ldap2') { + # NB 08.10.24 $table = $this->params['table']; + # NB 08.10.24 $attrs = ($this->params['action'] ? explode(',',$this->params['action']) : ['dn','objectClass']); + # NB 08.10.24 $this->page($this->ldap($table,$attrs)); } elseif ($action == 'phpinfo') { $this->deniedUnless($this->perm >= self::ADMIN); -- 2.47.3