From 6725133e58af13d8571afb928bd6c1eb192c2d8d Mon Sep 17 00:00:00 2001 From: Nicolas Boisselier Date: Thu, 25 Jan 2018 02:39:33 +0000 Subject: [PATCH] www/dbq/dbq.php --- etc/profile.d/functions | 2 ++ lib/php/ldap.php | 33 ++++++++++++------------- www/dbq/dbq.php | 53 +++++++++++++++++++++++++---------------- 3 files changed, 51 insertions(+), 37 deletions(-) diff --git a/etc/profile.d/functions b/etc/profile.d/functions index 51051191..267bb79a 100644 --- a/etc/profile.d/functions +++ b/etc/profile.d/functions @@ -794,6 +794,7 @@ http_get() { else echo "Can't find web client" 1>&2 + return 1 fi } @@ -809,6 +810,7 @@ http_head() { else echo "Can't find web client" 1>&2 + return 1 fi } diff --git a/lib/php/ldap.php b/lib/php/ldap.php index 0318f787..632b3f49 100644 --- a/lib/php/ldap.php +++ b/lib/php/ldap.php @@ -4,7 +4,7 @@ require_once(NB_ROOT.'/lib/php/http.php'); class Ldap { - public static function connect(&$o=[]) { + public static function _connect(&$o=[]) { $host = '127.0.0.1'; if (preg_match('/^.*?([^\.]+\.[^\.]+)$/',$_SERVER['HTTP_HOST'],$m)) { $host = 'ldap.'.$m[1]; @@ -45,9 +45,9 @@ class Ldap { return $connect; } - public static function search($o=[]) { + public static function _search($o=[]) { - $connect = self::connect($o); + $connect = self::_connect($o); if (empty($o['filter'])) { $o['filter'] = "(&(userpassword=*))"; @@ -112,6 +112,12 @@ class Ldap { } # < ldap_search + private $conn; + private $host = '127.0.0.1'; + private $base; + private $user; + private $password; + private static function ar_filter_keys($ar,$keys) { $new = []; foreach ($keys as $k) { @@ -120,14 +126,6 @@ class Ldap { return $new; } - public $host = '127.0.0.1'; - public $base; - public $user; - public $password; - #public $search = false; - - private $conn; - public function __construct($opt = []) { foreach ($opt as $k => $v) if ((string)$v !== '') $this->$k = $v; } @@ -154,7 +152,7 @@ class Ldap { ldap_close($this->conn); } - public function _search($o=[]) { + public function search($o=[]) { $conn = $this->conn(); @@ -193,14 +191,17 @@ class Ldap { } # < ldap_search - public static function add($dn,$data) { + public function add($dn,$data) { $conn = $this->conn(); return ldap_add($conn, $dn, $data); # or self::bye("Unable to add entry to ldap server"); } - 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"); + public function update($dn,$data) { + return ldap_mod_add($this->conn(), $dn, $data); # or self::bye("Unable to add entry to ldap server"); + } + + public function delete($dn) { + return ldap_delete($this->conn(), $dn); # or self::bye("Unable to add entry to ldap server"); } } diff --git a/www/dbq/dbq.php b/www/dbq/dbq.php index 54ef072f..c6faf4c8 100644 --- a/www/dbq/dbq.php +++ b/www/dbq/dbq.php @@ -170,8 +170,28 @@ class DbQ extends nb { } public function page_ldap() { - require_once(NB_ROOT.'/lib/php/ldap.php'); $this->deniedUnless($this->perm >= self::READ); + + require_once(NB_ROOT.'/lib/php/ldap.php'); + #debug($GLOBALS['DBQ_LDAP']); + $ldap = new Ldap($GLOBALS['DBQ_LDAP']); + + if ($this->params['table'] == 'add') { + debug($this->params); + $rows = (int)$ldap->add($this->params['action'],$_POST); + bye($_POST); + $this->page($rows); + + } elseif ($this->params['table'] == 'update') { + $rows = (int)$ldap->update($this->params['action'],$_POST); + $this->page($rows); + + } elseif ($this->params['table'] == 'rm') { + $rows = (int)$ldap->delete($this->params['action'],$_POST); + $this->page($rows); + + } + $filter = ($this->params['table'] ? $this->params['table'] : ''); $attrs = ($this->params['action'] ? explode(',',$this->params['action']) : []); @@ -185,24 +205,15 @@ class DbQ extends nb { 'attrs' => $attrs, ]; - if (true and !empty($GLOBALS['DBQ_LDAP'])) { - #debug($GLOBALS['DBQ_LDAP']); - $ldap = new Ldap($GLOBALS['DBQ_LDAP']); - if (!$search['filter']) { - if (preg_match('/^([^,]+),(.*?)$/',$GLOBALS['DBQ_LDAP']['user'],$m)) { - $search['filter'] = $m[1]; - $search['base'] = $m[2]; - } else { - $search['filter'] = 'cn='.Http::user(); - } + if (!$search['filter']) { + if (preg_match('/^([^,]+),(.*?)$/',$GLOBALS['DBQ_LDAP']['user'],$m)) { + $search['filter'] = $m[1]; + $search['base'] = $m[2]; + } else { + $search['filter'] = 'cn='.Http::user(); } - $rows = $ldap->_search($GLOBALS['DBQ_LDAP'] + $search); - } else { - $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); } + $rows = $ldap->search($GLOBALS['DBQ_LDAP'] + $search); #echo $rows[0]['jpegPhoto']; exit; #$rows = [ 'jpegPhoto' => $rows[0]['jpegPhoto'] ]; @@ -210,6 +221,7 @@ class DbQ extends nb { or ($this->params['format'] == $this->format_cli) or ($this->params['format'] == 'csv') ) $rows = $this->array_fill_assoc($rows); + $this->page($rows); } @@ -855,11 +867,10 @@ EOF; 'value' => $this->params['format'], ]; - $user = ''; - if (true or $user = $this->http->user()) { - if ($user) $rows[] = [ 'name' => 'dbq.user.name', 'value' => $user ]; - $rows[] = [ 'name' => 'dbq.user.perm', 'value' => $this->perm2h() ]; + if ($user = $this->http->user()) { + $rows[] = [ 'name' => 'dbq.user.name', 'value' => $user ]; } + $rows[] = [ 'name' => 'dbq.user.perm', 'value' => $this->perm2h() ]; // Machine if ($this->perm >= self::ADMIN and function_exists('posix_uname')) { -- 2.47.3