]> git.nbdom.net Git - nb.git/commitdiff
/home/nico/.netrc
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Mon, 29 Jan 2018 04:00:45 +0000 (04:00 +0000)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Mon, 29 Jan 2018 04:00:45 +0000 (04:00 +0000)
lib/php/db.php
lib/php/db/table.php
lib/php/db/types/ldap.php [new file with mode: 0644]
lib/php/ldap.php
www/dbq/dbq.php

index 4278c1835a39efb6a645baf1db85f6e99aa6c23c..94cdb07809b5490910c5e239a3763d2c1d37ff0d 100644 (file)
@@ -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;
index f2632ae197a55848931adfe669480107439e77c6..93f018b8e925956abfe02af101afb2a965324a1c 100644 (file)
@@ -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 ''
                        .'<?xml version="1.0" encoding="utf-8"?>'.NB_EOL #<?
-                       .'<rows name="'.$this->name.'" database="'.$this->db()->name.'" database-type="'.$this->db()->conf_type('type').'">'.NB_EOL
+                       .'<rows name="'.$this->name.'" database="'.$this->db()->name.'" database-type="'.$this->db()->type.'">'.NB_EOL
                ;
        }
 
diff --git a/lib/php/db/types/ldap.php b/lib/php/db/types/ldap.php
new file mode 100644 (file)
index 0000000..8445142
--- /dev/null
@@ -0,0 +1,21 @@
+<?php
+if (!class_exists('Db')) {
+       echo "Hi there! I'm just a plugin, not much I can do when called directly.\n";
+       exit;
+}
+$DB_TYPES['ldap'] = [
+       'quote_name' => '',
+       '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();
+       },
+];
+?>
index 3978a799e20b153356eed65168ec3a13f7d68407..d45becb680e89b1d27de81937cbab0d59a14e0e1 100644 (file)
@@ -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,
index 24f443a313a673673627292b208aa7605357bad7..ede626241b4e15e449c16b38af1c4c1703184e69 100644 (file)
@@ -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);