]> git.nbdom.net Git - nb.git/commitdiff
www/dbq/dbq.php
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Mon, 5 Feb 2018 00:24:32 +0000 (00:24 +0000)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Mon, 5 Feb 2018 00:24:32 +0000 (00:24 +0000)
etc/dbq/ldap.php
lib/php/db/table.php
lib/php/db/types/ldap.php
www/dbq/dbq.php

index 4a147c22d53bf4d979c89cdeabf81743681b705b..4b8b1ce5d7a715e0f862f16b6c3e4987fc27fb55 100644 (file)
@@ -39,5 +39,8 @@ $GLOBALS['DBQ_LDAP'] = [
        'host' => $host,
        'base' => $base,
 ];
+$DBQ['ldap'] = $GLOBALS['DBQ_LDAP'] + [
+               'type' => 'ldap',
+];
 #debug($GLOBALS['DBQ_LDAP']);
 ?>
index 20c3fe339f02f1461b4525957300ac8b99cd57c5..f3c11a7fae71ebaf1b6202b5fc97ae9ae05d1887 100644 (file)
@@ -72,6 +72,13 @@ Class Table extends nb {
                        self::$params = $opt['params']; unset($opt['params']);
                }
 
+               // Fields
+               if (isset($opt['fields'])) {
+                       $this->add_fields($opt['fields']);
+                       $this->_fields = 1;
+                       unset($opt['fields']);
+               }
+
                // Db / Connection
                if (isset($opt['db'])) {
                        $this->db(is_object($opt['db']) ? $opt['db'] : new Db($opt['db']));
@@ -106,7 +113,7 @@ Class Table extends nb {
 
        }
 
-       public function create_temporary_rows() {
+       private function create_temporary_rows() {
                // For static rows
                if(empty($this->rows)) return null;
                if(!empty($this->rows) and empty($this->type)) $this->type = 'rows';
@@ -165,7 +172,7 @@ Class Table extends nb {
         * Create temporary if needed
         *
         */
-       public function create_temporary() {
+       private function create_temporary() {
                if (!empty($this->_create_temporary)) return;
                $this->_create_temporary = true;
 
@@ -355,9 +362,9 @@ Class Table extends nb {
         */
        public function fields($name=null) {
 
-               $this->create_temporary();
-
                if (!isset($this->_fields)) {
+                       $this->create_temporary();
+
                        $this->_fields = true;
 
                        # Extras fields at the end
@@ -437,7 +444,7 @@ Class Table extends nb {
                return $this->fields;
        }
 
-       public function url_keys($values=null,$params=[],$preff='?',$sep='&amp;') {
+       private function url_keys($values=null,$params=[],$preff='?',$sep='&amp;') {
                if ($values === null) $values = $this->p();
                $url = is_array($params) ? $params : [$params];
 
@@ -454,7 +461,7 @@ Class Table extends nb {
                return $url ? $preff.join($sep,$url) : '';
        }
 
-       public function fields_keys_values($values) {
+       private function fields_keys_values($values) {
                $keys = $this->fields_keys();
                if (empty($keys)) $keys = $this->fields();
 
@@ -467,7 +474,7 @@ Class Table extends nb {
                return $ret;
        }
 
-       public function fields_keys(&$others=[]) {
+       private function fields_keys(&$others=[]) {
 
                $fields_keys = [];
 
@@ -515,7 +522,7 @@ Class Table extends nb {
                return html_edit($values,$form_action,true);
        }
 
-       public function sql_edit($values = null,&$add=false) {
+       private function sql_edit($values = null,&$add=false) {
                $where = $this->where($this->fields(),$values);
                if (empty($where)) {
                        $where = ' WHERE 1=0';
@@ -626,7 +633,7 @@ Class Table extends nb {
 
        }
 
-       public function url_list($k='',$v='') {
+       private function url_list($k='',$v='') {
 
                $params = [];
                $fields = ($this->p('action') == 'delete') ? [] : $this->fields();
@@ -654,7 +661,7 @@ Class Table extends nb {
 
        }
 
-       public function url_sort($name) {
+       private function url_sort($name) {
 
                if (!$this->show_url_sort or !($f=$this->field($name)) or !empty($f->dyn) or $f->is_encrypt()) return self::prettyText($name);
                #debug($f);
@@ -717,7 +724,7 @@ Class Table extends nb {
 
        }
 
-       public function where($fields,$hvalues,$need_all_values=false) {
+       private function where($fields,$hvalues,$need_all_values=false) {
 
                // Construct where
                $where = [];
@@ -733,7 +740,7 @@ Class Table extends nb {
 
        }
 
-       public function where_criterias($values,$logic='') {
+       private function where_criterias($values,$logic='') {
                $having = $where = [];
                if (empty($logic)) $logic = 'AND';
 
@@ -847,23 +854,22 @@ Class Table extends nb {
 
        }
 
-# NB 01.02.18  private function add_fields($fields) {
-# NB 01.02.18          foreach ($fields as $k => $v) {
-# NB 01.02.18 
-# NB 01.02.18                  if (is_scalar($v)) {
-# NB 01.02.18                          $this->fields[] = new Field([
-# NB 01.02.18                                  'name' => $k,
-# NB 01.02.18                                  'type' => 'text',
-# NB 01.02.18                                  'table' => $this,
-# NB 01.02.18                          ]);
-# NB 01.02.18 
-# NB 01.02.18                  } else {
-# NB 01.02.18                          $this->fields[$k] = $v;
-# NB 01.02.18 
-# NB 01.02.18                  }
-# NB 01.02.18 
-# NB 01.02.18          }
-# NB 01.02.18  }
+       private function add_fields($fields) {
+               foreach ($fields as $k => $v) {
+                       if (is_scalar($v)) {
+                               $this->fields[$v] = new Field([
+                                       'name' => $v,
+                                       'type' => 'text',
+                                       'table' => $this,
+                               ]);
+
+                       } else {
+                               $this->fields[$k] = $v;
+
+                       }
+               }
+       }
+
 
        private function add_extras($extras) {
                if ($this->p('extras') === '0') return false;
@@ -891,7 +897,7 @@ Class Table extends nb {
 
        }
 
-       public function select_extras() {
+       private function select_extras() {
 
                if (empty($this->extras)) return '';
 
@@ -903,10 +909,10 @@ Class Table extends nb {
                return ','.join(',',$select);
        }
 
-       public function rows_count() {
-               $opt = [ 'count' => 1 ];
-               return $this->db()->row($this->rows_sql($opt));
-       }
+# NB 04.02.18  public function rows_count() {
+# NB 04.02.18          $opt = [ 'count' => 1 ];
+# NB 04.02.18          return $this->db()->row($this->rows_sql($opt));
+# NB 04.02.18  }
 
        public function rows_sql(&$opt=[]) {
 
@@ -1287,25 +1293,20 @@ Class Table extends nb {
        /*-----------------------------------------------------------------
                No Out
        -----------------------------------------------------------------*/
-       public function rows_begin_($fields,&$o) {
+       private function rows_begin_($fields,&$o) {
                $o['sep'] = '';
                $o['var'] = [
                        'head' => array_keys($fields),
                        'rows' => [],
                ];
        }
-
-       public function rows_rec_($row,&$o) {
-               $o['var']['rows'][] = $row;
-       }
-
-       public function rows_end_() {
-       }
+       private function rows_rec_($row,&$o) { $o['var']['rows'][] = $row; }
+       private function rows_end_() { }
 
        /*-----------------------------------------------------------------
                Template
        -----------------------------------------------------------------*/
-       public function rows_begin_template($fields,&$o) {
+       private function rows_begin_template($fields,&$o) {
                $id = $this->idtemplate();
 
                $id = preg_replace('/[^\w\._-]/','',$id);
@@ -1321,7 +1322,7 @@ Class Table extends nb {
 # NB 02.12.16     require $o['file'];
        }
 
-       public function rows_rec_template($row,&$o) {
+       private function rows_rec_template($row,&$o) {
                $i = 0;
                foreach ($row as $k=>$v) {
                        $row[$i] = $v;
@@ -1333,7 +1334,7 @@ Class Table extends nb {
 # NB 02.12.16     $ROW = $row;
        }
 
-       public function rows_end_template() {
+       private function rows_end_template() {
                $HEAD = &$this->__fields;
                $ROWS = &$this->__rows;
                $ROW = &$ROWS[0];;
@@ -1377,12 +1378,12 @@ Class Table extends nb {
        /*-----------------------------------------------------------------
                Sql
        -----------------------------------------------------------------*/
-       public function rows_begin_sql() {
+       private function rows_begin_sql() {
                return '';
                return "\n-- ".$this->name."\n";
        }
 
-       public function rows_rec_sql(&$row,&$opt) {
+       private function rows_rec_sql(&$row,&$opt) {
                $keys = $values = [];
 
                foreach ($row as $k=>$v) {
@@ -1402,19 +1403,19 @@ Class Table extends nb {
                return $sql.NB_EOL;
        }
 
-       public function rows_end_sql() {
+       private function rows_end_sql() {
                return '';
        }
 
        /*-----------------------------------------------------------------
                Text
        -----------------------------------------------------------------*/
-       public function rows_begin_text() {
+       private function rows_begin_text() {
                $this->_row_text = null;
                return '';
        }
 
-       public function rows_rec_text(&$row) {
+       private function rows_rec_text(&$row) {
                $text = '';
                if ($this->_row_text === null) {
                        $this->_row_text = true;
@@ -1424,7 +1425,7 @@ Class Table extends nb {
                return $text.$row."\n";
        }
 
-       public function rows_end_text() {
+       private function rows_end_text() {
                unset($this->_row_text);
                return '';
        }
@@ -1432,7 +1433,7 @@ Class Table extends nb {
        /*-----------------------------------------------------------------
                Json
        -----------------------------------------------------------------*/
-       public function rows_begin_json() {
+       private function rows_begin_json() {
                $this->_row_json = null;
                return '['.NB_EOL;
                return ''
@@ -1441,7 +1442,7 @@ Class Table extends nb {
                .'['.NB_EOL;
        }
 
-       public function rows_rec_json(&$row) {
+       private function rows_rec_json(&$row) {
                if ($this->_row_json === null) {
                        $json = '';
                        $this->_row_json = true;
@@ -1451,7 +1452,7 @@ Class Table extends nb {
                return $json . json_encode($row);
        }
 
-       public function rows_end_json() {
+       private function rows_end_json() {
                unset($this->_row_json);
                return NB_EOL.']'.NB_EOL;
        }
@@ -1459,14 +1460,14 @@ Class Table extends nb {
        /*-----------------------------------------------------------------
                Xml
        -----------------------------------------------------------------*/
-       public function rows_begin_xml() {
+       private 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()->type.'">'.NB_EOL
                ;
        }
 
-       public function rows_rec_xml(&$row) {
+       private function rows_rec_xml(&$row) {
                $xml = '';
                $xml .= TABLE_INDENT."<row>".NB_EOL;
                foreach ($row as $k=>$v) {
@@ -1480,34 +1481,32 @@ Class Table extends nb {
                return $xml;
        }
 
-       public function rows_end_xml() {
+       private function rows_end_xml() {
                return '</rows>'.NB_EOL;
        }
 
-       public function zaza() { return [ ['A','B'], ['a','bb'] ]; }
-
        /*-----------------------------------------------------------------
                Csv
        -----------------------------------------------------------------*/
-       public function rows_begin_csv($fields) {
+       private function rows_begin_csv($fields) {
 
                if (self::p('rows_head_char')!=='') echo self::p('rows_head_char');
                if (!$this->show_header) return '';
                return join(TABLE_CSV_SEP,array_keys($fields))."\n";
        }
 
-       public function rows_rec_csv(&$row) {
+       private function rows_rec_csv(&$row) {
                return str_replace("\n",'|',join(TABLE_CSV_SEP,array_values($row)))."\n";
        }
 
-       public function rows_end_csv() {
+       private function rows_end_csv() {
                return '';
        }
 
        /*-----------------------------------------------------------------
                Html Table
        -----------------------------------------------------------------*/
-       public function rows_begin_table($fields) {
+       private function rows_begin_table($fields) {
 
                $html = '';
 
@@ -1542,7 +1541,7 @@ Class Table extends nb {
                return $html;
        }
 
-       public function rows_rec_table(&$row,&$opt) {
+       private function rows_rec_table(&$row,&$opt) {
 
                $buttons = $this->html_row_buttons($row);
 
@@ -1564,7 +1563,7 @@ Class Table extends nb {
                return $html;
        }
 
-       public function rows_end_table() {
+       private function rows_end_table() {
                unset($this->__rows_table_attr);
                $html = '';
                $html .= '</tbody>'.NB_EOL;
@@ -1575,11 +1574,11 @@ Class Table extends nb {
        /*-----------------------------------------------------------------
                Html Div
        -----------------------------------------------------------------*/
-       public function rows_begin_div() {
+       private function rows_begin_div() {
                return '<div class="rows search-results">'.NB_EOL;
        }
 
-       public function rows_rec_div(&$row) {
+       private function rows_rec_div(&$row) {
 
                $html = '';
 
@@ -1603,14 +1602,14 @@ Class Table extends nb {
                return $html;
        }
 
-       public function rows_end_div() {
+       private function rows_end_div() {
                return '</div>'.NB_EOL;
        }
 
-       public function sql_names($value) {
+       public function sql_names($values) {
 
                $new = [];
-               foreach ($value as $k=>$v) {
+               foreach ($values as $k=>$v) {
                        $new[$k] = $this->sql_name($v);
                }
                return $new;
@@ -1621,49 +1620,49 @@ Class Table extends nb {
                return $this->db()->sql_name($value === null ? $this->name : $value);
        }
 
-       private function _post2sql($post) {
-               $keys = [];
-               $keys_values = [];
-               $fields = [];
-               $fields_values = [];
-               foreach ($this->fields() as $name => $field) {
-
-                       if ($field->extras) continue;
-
-                       $value = $post[$this->ppreff().$name];
-
-                       if ($field->key) {
-                               $keys[] = $name;
-
-                               if (!isset($post[$this->key_preff.$name])) {
-                                       if ($field->key) $this->bye("Missing `$name`!");
-                                       continue;
-                               }
-
-                               if (isset($post[$this->key_preff.$name])) {
-                                       $keys_values[] = $post[$this->key_preff.$name];
-
-                               } else {
-                                       $keys_values[] = $value;
-                               }
-
-                       } else {
-
-                               $fields[] = $name;
-                               $fields_values[] = $value;
-
-                       }
-               }
-
-               return [
-                       'keys' => $keys,
-                       'keys_values' => $keys_values,
-                       'fields' => $fields,
-                       'fields_values' => $fields_values,
-                       'all_keys' => array_combine($keys,$keys_values),
-                       'all_fields' => array_combine($fields,$fields_values),
-               ];
-       }
+# NB 04.02.18  private function _post2sql($post) {
+# NB 04.02.18          $keys = [];
+# NB 04.02.18          $keys_values = [];
+# NB 04.02.18          $fields = [];
+# NB 04.02.18          $fields_values = [];
+# NB 04.02.18          foreach ($this->fields() as $name => $field) {
+# NB 04.02.18 
+# NB 04.02.18                  if ($field->extras) continue;
+# NB 04.02.18 
+# NB 04.02.18                  $value = $post[$this->ppreff().$name];
+# NB 04.02.18 
+# NB 04.02.18                  if ($field->key) {
+# NB 04.02.18                          $keys[] = $name;
+# NB 04.02.18 
+# NB 04.02.18                          if (!isset($post[$this->key_preff.$name])) {
+# NB 04.02.18                                  if ($field->key) $this->bye("Missing `$name`!");
+# NB 04.02.18                                  continue;
+# NB 04.02.18                          }
+# NB 04.02.18 
+# NB 04.02.18                          if (isset($post[$this->key_preff.$name])) {
+# NB 04.02.18                                  $keys_values[] = $post[$this->key_preff.$name];
+# NB 04.02.18 
+# NB 04.02.18                          } else {
+# NB 04.02.18                                  $keys_values[] = $value;
+# NB 04.02.18                          }
+# NB 04.02.18 
+# NB 04.02.18                  } else {
+# NB 04.02.18 
+# NB 04.02.18                          $fields[] = $name;
+# NB 04.02.18                          $fields_values[] = $value;
+# NB 04.02.18 
+# NB 04.02.18                  }
+# NB 04.02.18          }
+# NB 04.02.18 
+# NB 04.02.18          return [
+# NB 04.02.18                  'keys' => $keys,
+# NB 04.02.18                  'keys_values' => $keys_values,
+# NB 04.02.18                  'fields' => $fields,
+# NB 04.02.18                  'fields_values' => $fields_values,
+# NB 04.02.18                  'all_keys' => array_combine($keys,$keys_values),
+# NB 04.02.18                  'all_fields' => array_combine($fields,$fields_values),
+# NB 04.02.18          ];
+# NB 04.02.18  }
 
        public function replace($hvalues,&$info=[]) {
                # NB 12.12.17: When REPLACE is not supported where call a personalized function to modify $sql
@@ -1997,7 +1996,7 @@ Class Table extends nb {
                return false;
        }
 
-       public function html_row_buttons(&$row) {
+       private function html_row_buttons(&$row) {
                if (!$this->show_buttons or empty(self::$params)) return [];
                return [
                        (!DB_HTML_EDIT ? '' : '<a class="edit button" href="'.$this->url_keys($row,'action=edit').'">'.DB_HTML_EDIT.'</a>'.NB_EOL),
@@ -2008,7 +2007,7 @@ Class Table extends nb {
                ];
        }
 
-       public function html_rows_top() {
+       private function html_rows_top() {
                if (self::p('replace') === '0') return '';
                $html = '';
 
@@ -2025,35 +2024,32 @@ Class Table extends nb {
        }
 
        public function count() {
-
                if (isset($this->count)) return $this->count;
-
-               $sql_count = $this->name;
-
-               # We could use it instead of query, but wont be cached ! $this->create_temporary();
-               if ($this->type == 'sql' and $this->name !== DB_TABLE_QUERY_NAME) {
-                       if (!$this->sql) return;
-
-                       $sql_count = "SELECT COUNT(*) FROM ($this->sql) _table_count" ;
-                       #debug([$this->name,$sql_count]);
-# NB 08.08.17       if (preg_match('/SELECT.*SELECT|UNION/i',$this->sql)) {
-# NB 08.08.17         $sql_count = "SELECT COUNT(*) FROM ($this->sql) _table_count" ;
-# NB 08.08.17       } else {
-# NB 08.08.17         $sql_count = "SELECT COUNT(*) FROM ".preg_replace('/^SELECT .*?\s+FROM\s+/','',$this->sql);
-# NB 08.08.17         bye($sql_count);
-# NB 08.08.17       }
-
-               } elseif (preg_match('/^[\w_-]+$/',$sql_count)) {
-                       $sql_count = "SELECT count(*) FROM ".$this->sql_name($sql_count);
-
-               } else {
-                       $sql_count = preg_replace('/ (ORDER|LIMIT) .*?$/s','',$sql_count);
-                       $sql_count = preg_replace('/^SELECT .*FROM/s','SELECT count(*) FROM ',$sql_count);
-
-               }
-
-               $this->count = (int)$this->db()->row($sql_count);
+               $opt = [ 'count' => 1 ];
+               $this->count = $this->db()->row($this->rows_sql($opt));
                return $this->count;
+# NB 04.02.18 
+# NB 04.02.18          if (isset($this->count)) return $this->count;
+# NB 04.02.18 
+# NB 04.02.18          $sql_count = $this->name;
+# NB 04.02.18 
+# NB 04.02.18          # We could use it instead of query, but wont be cached ! $this->create_temporary();
+# NB 04.02.18          if ($this->type == 'sql' and $this->name !== DB_TABLE_QUERY_NAME) {
+# NB 04.02.18                  if (!$this->sql) return;
+# NB 04.02.18 
+# NB 04.02.18                  $sql_count = "SELECT COUNT(*) FROM ($this->sql) _table_count" ;
+# NB 04.02.18 
+# NB 04.02.18          } elseif (preg_match('/^[\w_-]+$/',$sql_count)) {
+# NB 04.02.18                  $sql_count = "SELECT count(*) FROM ".$this->sql_name($sql_count);
+# NB 04.02.18 
+# NB 04.02.18          } else {
+# NB 04.02.18                  $sql_count = preg_replace('/ (ORDER|LIMIT) .*?$/s','',$sql_count);
+# NB 04.02.18                  $sql_count = preg_replace('/^SELECT .*FROM/s','SELECT count(*) FROM ',$sql_count);
+# NB 04.02.18 
+# NB 04.02.18          }
+# NB 04.02.18 
+# NB 04.02.18          $this->count = (int)$this->db()->row($sql_count);
+# NB 04.02.18          return $this->count;
 
        }
 
@@ -2202,7 +2198,6 @@ Class Table extends nb {
 
        public function serialize() {
                $this->fields();
-               #$this->count();
                return serialize($this);
        }
 
index 8445142e40de9f9b66dc8197410f521fbb94e6a6..d7feb19c3d9ce192b67b42d77613b13a9acb8f3b 100644 (file)
@@ -13,6 +13,7 @@ $DB_TYPES['ldap'] = [
                        'host' => $this->host,
                        'user' => $this->user,
                        'password' => $this->password,
+                       'base' => $this->base,
                ]);
 
                return $db->ldap->connect();
index 3178959776e7f66acbba1c8c068f744cc59be78e..6fe519e47513c51204cd286dd074c7ef4cf15366 100644 (file)
@@ -1150,7 +1150,7 @@ EOF;
 
                } elseif ($action == 'ldap') {
                        $table = $this->params['table'];
-                       $attrs = ($this->params['action'] ? explode(',',$this->params['action']) : []);
+                       $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();
@@ -1243,8 +1243,14 @@ EOF;
        }
 
        public function run_table() {
-               $this->table(); # Table init
                $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;
 
                if ($action == 'help') {
@@ -1277,7 +1283,7 @@ EOF;
                        $this->page(preg_replace('/\s*;\s*$/','',$this->table->create(false,$this->params['args'])).";\n");
 
                } elseif ($action == 'count') {
-                       $this->page(['count'=>$this->table->rows_count()]);
+                       $this->page(['count'=>$this->table->count()]);
 
                } elseif ($action == 'fields') {
                        $this->page($this->table,'fields_rows');