From 547051bf3f417965c2b7d0200edf6e47b5cde211 Mon Sep 17 00:00:00 2001 From: Nicolas Boisselier Date: Mon, 5 Feb 2018 00:24:32 +0000 Subject: [PATCH] www/dbq/dbq.php --- etc/dbq/ldap.php | 3 + lib/php/db/table.php | 281 +++++++++++++++++++------------------- lib/php/db/types/ldap.php | 1 + www/dbq/dbq.php | 12 +- 4 files changed, 151 insertions(+), 146 deletions(-) diff --git a/etc/dbq/ldap.php b/etc/dbq/ldap.php index 4a147c22..4b8b1ce5 100644 --- a/etc/dbq/ldap.php +++ b/etc/dbq/ldap.php @@ -39,5 +39,8 @@ $GLOBALS['DBQ_LDAP'] = [ 'host' => $host, 'base' => $base, ]; +$DBQ['ldap'] = $GLOBALS['DBQ_LDAP'] + [ + 'type' => 'ldap', +]; #debug($GLOBALS['DBQ_LDAP']); ?> diff --git a/lib/php/db/table.php b/lib/php/db/table.php index 20c3fe33..f3c11a7f 100644 --- a/lib/php/db/table.php +++ b/lib/php/db/table.php @@ -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='&') { + private function url_keys($values=null,$params=[],$preff='?',$sep='&') { 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 '' .''.NB_EOL #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."".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 ''.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 .= ''.NB_EOL; @@ -1575,11 +1574,11 @@ Class Table extends nb { /*----------------------------------------------------------------- Html Div -----------------------------------------------------------------*/ - public function rows_begin_div() { + private function rows_begin_div() { return '
'.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 '
'.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 ? '' : ''.DB_HTML_EDIT.''.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); } diff --git a/lib/php/db/types/ldap.php b/lib/php/db/types/ldap.php index 8445142e..d7feb19c 100644 --- a/lib/php/db/types/ldap.php +++ b/lib/php/db/types/ldap.php @@ -13,6 +13,7 @@ $DB_TYPES['ldap'] = [ 'host' => $this->host, 'user' => $this->user, 'password' => $this->password, + 'base' => $this->base, ]); return $db->ldap->connect(); diff --git a/www/dbq/dbq.php b/www/dbq/dbq.php index 31789597..6fe519e4 100644 --- a/www/dbq/dbq.php +++ b/www/dbq/dbq.php @@ -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'); -- 2.47.3