]> git.nbdom.net Git - nb.git/commitdiff
lib/php/db/table.php
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Mon, 5 Feb 2018 16:47:43 +0000 (16:47 +0000)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Mon, 5 Feb 2018 16:47:43 +0000 (16:47 +0000)
etc/dbq/ldap.php
lib/php/db.php
lib/php/db/table.php
lib/php/db/types/ldap.php
lib/php/ldap.php
lib/php/out.php
www/dbq/dbq.php
www/dbq/html/default.css

index 4b8b1ce5d7a715e0f862f16b6c3e4987fc27fb55..edebe26344aee907558fe1af9708026bc6d91a4f 100644 (file)
@@ -39,8 +39,22 @@ $GLOBALS['DBQ_LDAP'] = [
        'host' => $host,
        'base' => $base,
 ];
-$DBQ['ldap'] = $GLOBALS['DBQ_LDAP'] + [
-               'type' => 'ldap',
+#'(&('.$search['filter'].')(userPassword=*))'
+if (preg_match('/^([^,]+),(.*?)$/',$user,$m)
+) {
+       $filter = $m[1];
+} else {
+       $filter = 'cn='.Http::user();
+}
+$filter = '(&('.$filter.')(userPassword=*))';
+$DBQ['ldap2'] = $GLOBALS['DBQ_LDAP'] + [
+       'type' => 'ldap',
+       'tables' => [
+               $filter => [
+                       'fields' => ['dn','objectClass'],
+               ],
+       ],
+       'default_table' => $filter,
 ];
 #debug($GLOBALS['DBQ_LDAP']);
 ?>
index 94cdb07809b5490910c5e239a3763d2c1d37ff0d..59f3b4d27340dd39f2fec14dbb2d020e2a8ff6d5 100644 (file)
@@ -49,7 +49,6 @@ class Db extends nb {
        public $id;
        public $name;
        public $title;
-       public $notice;
        public $type;
        public $tables = [];
        public $types = [];
@@ -370,12 +369,17 @@ class Db extends nb {
        * Return a table instance
        */
        public function table($name='',$params=[]) {
-               if ( !is_array($params) ) $this->bye('Usage: table($name,[type=>, sql=>, ...])');
+               if (is_array($name)) {
+                       $params = $name;
+                       $name = $params['name'];
+               }
+               if (!is_array($params)) $this->bye('Usage: table($name,[type=>, sql=>, ...])');
                if (!$name and !empty($this->default_table)) $name = $this->default_table;
                if (!$name) $this->bye('table(): Missing table');
 
                if (empty($params) or empty($params['db'])) $params['db'] = $this;
 
+               $params['name'] = $name;
                if (empty($this->tables[$name])) {
                        $this->tables[$name] = new Table($name,$params);
                } elseif ($params) {
@@ -504,7 +508,8 @@ class Db extends nb {
 
        public function tables() {
 
-               if (!isset($this->_tables)) {
+               if (!$this->conf_type('nosql') and !isset($this->_tables)) {
+               
                #if (!isset($this->tables)) {
                        $this->_tables = true;
 
@@ -593,10 +598,8 @@ class Db extends nb {
 
                #var_dump ($this->tables());
 #foreach($this->tables as $t) debug($t->name.'='.$t->type);
-#bye($this->tables);
                $rows = [];
                foreach ($this->tables() as $t) {
-                       #debug($t->name.'='.$t->type);
 
                        if ($name and !$this->str_match($t->name,$name)) continue;
                        if ($type and !$this->str_match($t->type,$type)) continue;
@@ -605,11 +608,11 @@ class Db extends nb {
                        if ($database and isset($t->database) and !$this->str_match($t->database,$database)) continue;
 
                        $row = $t->status();
+                       if (!$row) $row = ['name' => $t->name];
                        if ($count) $row['count'] = $t->status('count');
                        #if ($fields) $row['fields'] = $t->status('fields');
                        if ($fields) $row['fields'] = join(',',array_keys($t->fields()));
 
-                       #debug($t->name.'='.$t->database);
                        $rows[] = $row;
                }
 
@@ -1084,10 +1087,8 @@ class Db extends nb {
        }
 
        public function status() {
-
                $status = $new = [];
 
-#debug(array_keys($this->tables()));
                $new = []
                +[
                        'id' => (empty($this->id) ? '' : $this->id),
index f3c11a7fae71ebaf1b6202b5fc97ae9ae05d1887..9be8e2180845d4ce68dc270521c2fb1bdb994261 100644 (file)
@@ -75,7 +75,6 @@ Class Table extends nb {
                // Fields
                if (isset($opt['fields'])) {
                        $this->add_fields($opt['fields']);
-                       $this->_fields = 1;
                        unset($opt['fields']);
                }
 
@@ -362,7 +361,7 @@ Class Table extends nb {
         */
        public function fields($name=null) {
 
-               if (!isset($this->_fields)) {
+               if (!$this->db()->conf_type('nosql') and!isset($this->_fields)) {
                        $this->create_temporary();
 
                        $this->_fields = true;
@@ -461,7 +460,7 @@ Class Table extends nb {
                return $url ? $preff.join($sep,$url) : '';
        }
 
-       private function fields_keys_values($values) {
+       public function fields_keys_values($values) {
                $keys = $this->fields_keys();
                if (empty($keys)) $keys = $this->fields();
 
@@ -1052,11 +1051,6 @@ Class Table extends nb {
        }
 
        public function rows(&$opt=[]) { # ,$opt_by_val=null) {
-# NB 30.01.18          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
                #
@@ -1086,7 +1080,20 @@ Class Table extends nb {
 
 #debug($opt);
                list($sql,$where,$limit,$select_count) = $this->rows_sql($opt);
+               $nosql = $this->db()->conf_type('nosql');
+
+       if ($nosql) {
+               $sql = $this->name;
+               $fct = $this->db()->conf_type('prepare');
+               if ($fct) $st = $fct($this,$sql);
+               if (!$fct = $this->db()->conf_type('fetch')) {
+                       $fct = function() use($st) { return $st->fetch(PDO::FETCH_ASSOC, PDO::FETCH_ORI_NEXT); };
+               }
+               #$fct = function() { return false; };
+               #$fct = function() use($st) { return $st->fetch(PDO::FETCH_ASSOC, PDO::FETCH_ORI_NEXT); };
 
+       } else {
+       
                if ($this->db()->type == 'pgsql') {
                        $sql_orig = $sql;
                        $this->db()->conn->beginTransaction();
@@ -1101,12 +1108,12 @@ Class Table extends nb {
 
                $st = $this->db()->conn->prepare($sql);#,[PDO::ATTR_ERRMODE => PDO::ERRMODE_SILENT]);
                if (empty($cursor)) {
-                       $st->execute();
+                       $fct = function() use($st) { return $st->fetch(PDO::FETCH_ASSOC, PDO::FETCH_ORI_NEXT); };
                } else {
-                       $fct = function() use($st) { return $st->execute(); };
+                       $fct = function() use($st) { if ($st->execute()) return $st->fetch(PDO::FETCH_ASSOC, PDO::FETCH_ORI_NEXT); };
                }
+}
                if (!empty($opt['query'])) return $st;
-
                #
                # Use the module out when format unknow
                #
@@ -1161,7 +1168,8 @@ Class Table extends nb {
                }
 
                $count = 0;
-               while ((empty($fct) or $fct()) and $row = $st->fetch(PDO::FETCH_ASSOC, PDO::FETCH_ORI_NEXT)) {
+               #while ((empty($fct) or $fct()) and $row = $st->fetch(PDO::FETCH_ASSOC, PDO::FETCH_ORI_NEXT)) {
+               while ($row = $fct($this)) {
 
                        $count++;
                        $call = null;
@@ -1224,14 +1232,14 @@ Class Table extends nb {
 
                } # < while rows
 
-               $st->closeCursor();
+               if (!empty($st)) $st->closeCursor();
                if (!empty($cursor)) $cursor->closeCursor();
                if (!$count) return;
 
                $this->count = $opt['count'] = $count;
                $this->limit = $opt['limit'] = $limit;
 
-               if ($opt['is_html'] or $format=='wp' or !empty($cursor)) {
+               if (!$nosql and ($opt['is_html'] or $format=='wp' or !empty($cursor))) {
 
                        if (!empty($sql_orig)) $sql = $sql_orig;
                        //
@@ -1262,6 +1270,9 @@ Class Table extends nb {
                        $tot = $query->fetch(PDO::FETCH_COLUMN);
                        $this->tot = $opt['tot'] = $tot;
 
+               } else {
+                       $this->tot = $opt['tot'] = $count;
+
                } # < is_html
 
                if ($count === 0 and !$format) {
@@ -2228,6 +2239,8 @@ Class Table extends nb {
 
        public function status($key=null) {
 
+               if ($this->db()->conf_type('nosql') and !isset($this->status)) $this->status = [];
+
                if (!isset($this->status)) {
 
                        if ($this->type == 'sql') {
index d7feb19c3d9ce192b67b42d77613b13a9acb8f3b..6687913df9f187de667f4e3edfd0abba8973078d 100644 (file)
@@ -6,6 +6,14 @@ if (!class_exists('Db')) {
 $DB_TYPES['ldap'] = [
        'quote_name' => '',
        'quote' => function($v) { return $v; },
+       'nosql' => true,
+       #'prepare' => function($sql) { return $this->ldap->prepare($sql); },
+       'prepare' => function($table,$sql) { $table->db()->ldap->prepare($sql); return $table->db()->ldap; },
+       'fetch' => function($table) {
+               $row = $table->db()->ldap->fetch(PDO::FETCH_ASSOC, PDO::FETCH_ORI_NEXT);
+               $row = $table->db()->ldap->ar_filter_keys($row,array_keys($table->fields()));
+               return $row;
+       },
        '_connect' => function($db) {
                require_once(NB_ROOT.'/lib/php/ldap.php');
 
index 222ff8cbcf6fa398190fa2078c83a364c3ff0718..75d00916000f8c33fe6bad7074c8dda52312da8a 100644 (file)
@@ -3,16 +3,16 @@ require_once(realpath(dirname(__FILE__).'/../../lib/php/nb.php'));
 require_once(NB_ROOT.'/lib/php/http.php');
 
 class Ldap {
-       private $conn;
        private $host = '127.0.0.1';
        private $base;
        private $user;
        private $password;
 
        private $fields;
-       # NB 28.01.18: TODO 
-       private $row_parse_pre;
-       private $row_parse_post;
+
+       private $conn;
+       private $read;
+       private $entry;
 
   public function __construct($opt = []) {
                foreach ($opt as $k => $v) if ((string)$v !== '') $this->$k = $v;
@@ -24,7 +24,7 @@ class Ldap {
 
        public static function bye($msg) { die($msg); }
 
-       private static function ar_filter_keys($ar,$keys) {
+       public static function ar_filter_keys($ar,$keys) {
                $new = [];
                foreach ($keys as $k) {
                        if (isset($ar[$k])) $new[$k] = $ar[$k];
@@ -48,43 +48,48 @@ class Ldap {
                ldap_close($this->conn);
        }
 
-# NB 30.01.18  public function fields($set) {
-# NB 30.01.18          if (isset($set)) return $this->fields;
-# NB 30.01.18          if (isset($this->fields)) return $this->fields;
-# NB 30.01.18  }
+       private function entry($entry=null,$dn=true) {
+               $row = [];
+
+               if ($entry === null) $entry = $this->entry;
+               $conn = $this->connect();
+               $attributes = ldap_get_attributes($conn, $entry);
+
+               for($j=0; $j<$attributes['count']; $j++) {
+                       $values = ldap_get_values_len($conn, $entry,$attributes[$j]);
+                       unset($values['count']);
+                       if (isset($values) and count($values) == 1) $values = $values[0];
+                       $row[$attributes[$j]] = $values;
+               }
+
+               if ($dn) $row['dn'] = ldap_get_dn($conn,$entry);
+
+               return $row;
+       }
 
        public function search($o=[]) {
 
                $conn = $this->connect();
 
                if (!isset($o['dn'])) $o['dn'] = true;
-               if (!isset($o['base'])) $o['base'] = null;
+               if (!isset($o['base'])) $o['base'] = $this->base;
                if (!isset($o['filter'])) $o = array_merge($o,[ 'filter' => 'objectClass=*', 'attrs' => ['dn','objectClass'], 'dn' => true ]);
                if (!isset($o['attrs'])) $o['attrs'] = [];
                if (!is_array($o['attrs'])) $o['attrs'] = [$o['attrs']];
                if (!isset($o['limit'])) $o['limit'] = 0;
                $fct = empty($o['fct']) ? '' : $o['fct'];
 
-               $read = ldap_search($conn, $o['base'], $o['filter'], $o['attrs'], 0, $o['limit']) or self::bye("Unable to search ldap server");
+# 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 = [];
                $i = 0;
-               $entry = ldap_first_entry($conn, $read);
-               if ($entry !== false) do {
-
-                       $attributes = ldap_get_attributes($conn, $entry);
-
-                       for($j=0; $j<$attributes['count']; $j++) {
-                               $values = ldap_get_values_len($conn, $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($conn,$entry)] + (empty($info[$i]) ? [] : $info[$i]);
+               $dn =   (bool)(!empty($o['dn']) and in_array('dn',!empty($o['attrs']) ? $o['attrs'] : ['dn']));
+# NB 05.02.18          while ($entry = $this->fetch()) {
+# NB 05.02.18                  $info[$i] = $this->entry($entry,$dn);
+               if ($this->prepare($o)) while ($row = $this->fetch($dn)) {
+                       $info[$i] = $row;
+                       #bye($info[$i]);
 
                        # Re-order by attrs
                        if (!empty($o['attrs']) and !empty($info[$i])) $info[$i] = self::ar_filter_keys($info[$i],$o['attrs']);
@@ -96,7 +101,7 @@ class Ldap {
 
                        $i++;
 
-               } while ($entry = ldap_next_entry($conn, $entry));
+               }
 
                if ($fct) return $i;
                return $info;
@@ -127,4 +132,43 @@ class Ldap {
                 */
        }
 
+       /// PDO compatible
+       public function setAttribute($k,$v) { }
+
+       public function prepare($sql,$o=[]) {
+               if (is_array($sql)) {
+                       $o = $sql;
+                       $sql = $o['filter'];
+               }
+
+               if (!isset($o['base'])) $o['base'] = $this->base;
+               #if (!isset($o['filter'])) $o = array_merge($o,[ 'filter' => 'objectClass=*', 'attrs' => ['dn','objectClass'], 'dn' => true ]);
+               $o['filter'] = $sql;
+               if (!isset($o['attrs'])) $o['attrs'] = [];
+               if (!is_array($o['attrs'])) $o['attrs'] = [$o['attrs']];
+               if (!isset($o['limit'])) $o['limit'] = 0;
+
+               $conn = $this->connect();
+               $this->read = ldap_search($conn, $o['base'], $o['filter'], $o['attrs'], 0, $o['limit']) or self::bye("Unable to search ldap server");
+
+               $this->fetch(null);
+               return $this->read;
+       }
+
+       public function fetch($type='',$opt='') {
+               static $count = 0;
+               if ($type === null) {
+                       $count = 0;
+                       return true;
+               }
+               $conn = $this->connect();
+               $this->entry = ( $count ? ldap_next_entry($conn, $this->entry) : ldap_first_entry($conn, $this->read));
+               if (!$this->entry) return null;
+               $count++;
+
+               $dn = ($type === false ? false : true);
+               return $this->entry($this->entry,$dn);
+       }
+
+       public function closeCursor() {}
 }
index f8296615873f0b09bf1c4a6748a52c575d0a8064..439fe570d2556e69c04e25880ea9cacf37a76ffe 100644 (file)
@@ -326,7 +326,7 @@ Class Out extends Nb {
 # NB 07.01.18     if (empty($o['no_escape'])) $v = self::escape($v);
     if (!empty($o['escape'])) $v = self::escape($v);
 
-    if (!$is_scalar and strpos($type,'html') !== false) $v = '<pre>'.NB_EOL.$v.NB_EOL.'</pre>';
+# NB 05.02.18     if (!$is_scalar and strpos($type,'html') !== false) $v = '<pre>'.NB_EOL.$v.NB_EOL.'</pre>';
     return $v;
   }
 
index 6fe519e47513c51204cd286dd074c7ef4cf15366..f6ef67ffe7bbe66e6a51f9a0871faa9f667f08b5 100644 (file)
@@ -607,7 +607,9 @@ class DbQ extends nb {
 
                }
 
-               $this->table = $this->db->table($this->params['table'],array_merge([
+               $this->table = $this->db->table(array_merge([
+                       'name' => $this->params['table'],
+                       'db' => $this->db,
                        'db' => $this->db,
                        'show_hidden_params' => false,
                        'show_buttons' => false,
@@ -1151,10 +1153,6 @@ EOF;
                } elseif ($action == 'ldap') {
                        $table = $this->params['table'];
                        $attrs = ($this->params['action'] ? explode(',',$this->params['action']) : ['dn','objectClass']);
-                       if (false and $this->params['format'] == $this->format_html) {
-                               $this->params['format'] = 'div';
-                               $this->run_init();
-                       }
                        $this->page($this->ldap($table,$attrs));
 
                } elseif ($action == 'phpinfo') {
@@ -1246,10 +1244,6 @@ EOF;
                $action = $this->params['action'];
 
                $param = [];
-               if ($this->db->type == 'ldap') {
-                       $param['fields'] = ($action ? explode(',',$action) : ['dn','objectClass']);
-                       debug($param);
-               }
                $this->table($param); # Table init
                if (!$action and self::PARAM_DB_DEFAULT) $action = self::PARAM_DB_DEFAULT;
 
index 31d8a69e06a3b828e4d792687e2d056aa4d1133a..50b8a6eccf2514c6d1917193eeb28d05a48c338c 100644 (file)
@@ -125,7 +125,8 @@ ul.row,
 form.edit,
 .menu,
 .block,
-iframe, pre
+iframe,
+pre
 {
        border-radius: 4px;
        border-spacing: 0;