From 2af93e5fe5299ebd77f8175181f9777d6183d37f Mon Sep 17 00:00:00 2001 From: Nicolas Boisselier Date: Mon, 29 Jan 2018 04:00:45 +0000 Subject: [PATCH] /home/nico/.netrc --- lib/php/db.php | 52 ++++++++++++++++++++++----------------- lib/php/db/table.php | 10 +++++--- lib/php/db/types/ldap.php | 21 ++++++++++++++++ lib/php/ldap.php | 14 ++++++----- www/dbq/dbq.php | 7 +++--- 5 files changed, 68 insertions(+), 36 deletions(-) create mode 100644 lib/php/db/types/ldap.php diff --git a/lib/php/db.php b/lib/php/db.php index 4278c183..94cdb078 100644 --- a/lib/php/db.php +++ b/lib/php/db.php @@ -253,36 +253,42 @@ class Db extends nb { $this->conf_type_load(); # Connect - try { - $this->conn = new PDO($this->pdo,$this->user,$this->password,$this->options); - #if (isset($this->pdo_error)) $this->conn->setAttribute($this->pdo_error[0], $this->pdo_error[1]); + if ($fct = $this->conf_type('_connect')) { + $this->conn = $fct($this); - } catch (PDOException $e) { - $msg = 'Connection failed:'; + } else { - foreach(['name','host','file'] as $p) { - if (!empty($this->$p)) $msg .= " $p=".$this->$p; - } + try { + $this->conn = new PDO($this->pdo,$this->user,$this->password,$this->options); + #if (isset($this->pdo_error)) $this->conn->setAttribute($this->pdo_error[0], $this->pdo_error[1]); - $msg .= "\n ".$e->getMessage(); - #throw new Exception($msg, (int)$e->getCode()); - self::bye($msg); - } + } catch (PDOException $e) { + $msg = 'Connection failed:'; - if (empty($this->conn)) { - $this->bye("Connection failed: ".$this->pdo_info()); - return false; - } + foreach(['name','host','file'] as $p) { + if (!empty($this->$p)) $msg .= " $p=".$this->$p; + } - # Create functions - foreach ((array)$this->conf_type('sqliteCreateFunction') as $name => $fct) { - if (is_array($fct)) { - $this->conn->sqliteCreateFunction($name,$fct[0],$fct[1]); - } else { - $this->conn->sqliteCreateFunction($name,$fct); + $msg .= "\n ".$e->getMessage(); + #throw new Exception($msg, (int)$e->getCode()); + self::bye($msg); + } + + if (empty($this->conn)) { + $this->bye("Connection failed: ".$this->pdo_info()); + return false; } - } + # Create functions + foreach ((array)$this->conf_type('sqliteCreateFunction') as $name => $fct) { + if (is_array($fct)) { + $this->conn->sqliteCreateFunction($name,$fct[0],$fct[1]); + } else { + $this->conn->sqliteCreateFunction($name,$fct); + } + } + + } $this->method('connect'); return true; diff --git a/lib/php/db/table.php b/lib/php/db/table.php index f2632ae1..93f018b8 100644 --- a/lib/php/db/table.php +++ b/lib/php/db/table.php @@ -6,7 +6,7 @@ $DB_TABLE_QUERY_ID = 0; // >> Define define('TABLE_INDENT',NB_EOL ? "\t" : ""); -define('TABLE_CSV_SEP',Table::p('sep',"\t")); +define('TABLE_CSV_SEP',Db::p('sep',"\t")); if (!defined('TABLE_TEMPLATE')) define('TABLE_TEMPLATE',dirname(__FILE__).'/../../../share/templates'); @@ -1034,7 +1034,11 @@ Class Table extends nb { } public function rows(&$opt=[],$opt_by_val=null) { + if ($opt_by_val !== null) $opt = $opt_by_val; +# NB 29.01.18 if ($fct = $this->db()->conf_type('_table_rows')) { +# NB 29.01.18 return $fct($opt); +# NB 29.01.18 } # # Db type change # @@ -1053,8 +1057,6 @@ Class Table extends nb { # $this->create_temporary(); - if ($opt_by_val !== null) $opt = $opt_by_val; - if (isset($opt['format']) and $opt['format']==='') { $format = ''; } else { @@ -1453,7 +1455,7 @@ Class Table extends nb { public function rows_begin_xml() { return '' .''.NB_EOL #name.'" database="'.$this->db()->name.'" database-type="'.$this->db()->conf_type('type').'">'.NB_EOL + .''.NB_EOL ; } diff --git a/lib/php/db/types/ldap.php b/lib/php/db/types/ldap.php new file mode 100644 index 00000000..8445142e --- /dev/null +++ b/lib/php/db/types/ldap.php @@ -0,0 +1,21 @@ + '', + 'quote' => function($v) { return $v; }, + '_connect' => function($db) { + require_once(NB_ROOT.'/lib/php/ldap.php'); + + $db->ldap = new Ldap([ + 'host' => $this->host, + 'user' => $this->user, + 'password' => $this->password, + ]); + + return $db->ldap->connect(); + }, +]; +?> diff --git a/lib/php/ldap.php b/lib/php/ldap.php index 3978a799..d45becb6 100644 --- a/lib/php/ldap.php +++ b/lib/php/ldap.php @@ -30,7 +30,7 @@ class Ldap { return $new; } - public function conn($exit=true) { + public function connect($exit=true) { if ($this->conn) return $this->conn; if (!$this->conn = @ldap_connect($this->host)) return self::bye("Could not connect to LDAP server"); @@ -48,7 +48,7 @@ class Ldap { public function search($o=[]) { - $conn = $this->conn(); + $conn = $this->connect(); if (!isset($o['dn'])) $o['dn'] = true; if (!isset($o['base'])) $o['base'] = null; @@ -96,17 +96,19 @@ class Ldap { } # < ldap_search public function add($dn,$data) { - $conn = $this->conn(); + $conn = $this->connect(); + debug($dn); return ldap_add($conn, $dn . ',' . $this->base, $data); +# NB 29.01.18 return ldap_mod_add($conn, $dn . ',' . $this->base, $data); } public function update($dn,$data) { -# NB 28.01.18 return ldap_mod_add($this->conn(), $dn . ',' . $this->base, $data); - return ldap_mod_replace($this->conn(), $dn . ',' . $this->base, $data); +# NB 28.01.18 return ldap_mod_add($this->connect(), $dn . ',' . $this->base, $data); + return ldap_mod_replace($this->connect(), $dn . ',' . $this->base, $data); } public function delete($dn) { - return ldap_delete($this->conn(), $dn); + return ldap_delete($this->connect(), $dn); /* foreach ($ldap->search([ 'base' => $this->user, diff --git a/www/dbq/dbq.php b/www/dbq/dbq.php index 24f443a3..ede62624 100644 --- a/www/dbq/dbq.php +++ b/www/dbq/dbq.php @@ -177,19 +177,20 @@ class DbQ extends nb { 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($attrs,$_POST); + $rows = (int)$ldap->add($dn,$_POST); } elseif ($table == 'update') { $this->deniedUnless($this->perm >= self::WRITE); - $rows = (int)$ldap->update($attrs,$_POST); + $rows = (int)$ldap->update($dn,$_POST); } elseif ($table == 'rm') { $this->deniedUnless($this->perm >= self::DELETE); - $rows = (int)$ldap->delete($attrs,$_POST); + $rows = (int)$ldap->delete($dn,$_POST); } else { // ls $this->deniedUnless($this->perm >= self::READ); -- 2.47.3