*****************************************************************************/
require_once(dirname(__FILE__).'/functions.php');
if (!defined('DB_HTML_EDIT')) define('DB_HTML_EDIT','Edit');
-if (!defined('DEBUG')) define('DEBUG',0);
class db {
}
function tables() {
- if (!$this->tables) {
+ if ($this->tables) return $this->tables;
- if ($this->type == 'sqlite') {
- $sql = "SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%' ORDER BY name";
+ if ($this->type == 'sqlite') {
+ $sql = "SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%' ORDER BY name";
- } elseif ($this->type == 'pgsql') {
- $sql = "SELECT table_name FROM information_schema.tables WHERE table_type in('BASE TABLE','LOCAL TEMPORARY') AND table_schema NOT IN ('pg_catalog', 'information_schema')";
+ } elseif ($this->type == 'pgsql') {
+ $sql = "SELECT table_name FROM information_schema.tables WHERE table_type in('BASE TABLE','LOCAL TEMPORARY') AND table_schema NOT IN ('pg_catalog', 'information_schema')";
- } elseif ($this->type == 'mysql') {
- $sql = "SHOW TABLES";
+ } elseif ($this->type == 'mysql') {
+ $sql = "SHOW TABLES";
- } else {
- err('db.tables(): Unknow db type: '.$this->type);
- return array();
- }
-
- $rows = $this->conn->query($sql);
- foreach ($rows as $row) {
- $this->tables[] = current($row);
- }
+ } else {
+ err('db.tables(): Unknow db type: '.$this->type);
+ return array();
+ }
+ $rows = $this->conn->query($sql);
+ foreach ($rows as $row) {
+ $this->tables[] = current($row);
}
+
return $this->tables;
}
function print_header($type) {
- if ($_REQUEST['format']=='csv') {
+ if ($this->p('format')=='csv') {
header('Content-type: text/plain');
- } elseif ($_REQUEST['format']=='yaml') {
+ } elseif ($this->p('format')=='yaml') {
header('Content-type: text/yaml');
- } elseif ($_REQUEST['format']=='xml') {
+ } elseif ($this->p('format')=='xml') {
header('Content-type: text/xml');
} else {
function initParams($tables=null) {
- if (empty($_REQUEST)) $_REQUEST = array();
if ($tables === null) $tables = $this->tables();
#$_REQUEST['table'] = @$_REQUEST['table'] ? preg_replace('/^([\w\d_-]+).*$/','\1',$_REQUEST['table']) : $tables[0];
#$count = $table->rowCount();
$r .= '<option value="'.$table.'"';
#$pretty = prettyText($table);
- $r .= $table == @$_REQUEST['table'] ? ' selected="selected"' : '';
+ $r .= $table == $this->p('table') ? ' selected="selected"' : '';
$r .= '>'.prettyText($table).'</option>'.PHP_EOL;
}
$r .= '</select>'.PHP_EOL;
$r .= '</div>'.PHP_EOL;
+ $r .= '<div id="msg"></div>'.PHP_EOL;
return $r;
#$count = $table->rowCount();
$r .= '<li class="'.$table.'">';
#$pretty = prettyText($table);
- $r .= $table == @$_REQUEST['table'] ? "<span class=\"sel\">$table</span>" : '<a href="?table='.$table.'">'.$table."</a>";
+ $r .= $table == $this->p('table') ? "<span class=\"sel\">$table</span>" : '<a href="?table='.$table.'">'.$table."</a>";
$r .= '</li>'.PHP_EOL;
}
}
+ /*
+ * Function: p
+ * Return a param
+ */
+ function p($name=null,$default=null) {
+ if ($name === null) return isset($_REQUEST) ? $_REQUEST : array();
+ return isset($_REQUEST[$name]) ? $_REQUEST[$name] : $default;
+ }
+
}
class table {
* @param (array)
*/
function page($Page,$param=null) {
- if ($param === null) $param = $_REQUEST;
+ if ($param === null) $param = $this->p();
elseif (!is_array($param)) $param = array('name' => $param);
foreach (array(
#if ($Page->is('html')) $this->form_criterias();
- if (@$_REQUEST['format']) $this->print_header($_REQUEST['format']);
- if (@$_REQUEST['format']) {
- $this->rows(array('format' => @$_REQUEST['format']));
+ if ($this->p('format')) $this->print_header($this->p('format'));
+ if ($this->p('format')) {
+ $this->rows(array('format' => $this->p('format')));
return 0;
}
Rows
*/
if (@$this) $this->rows(array(
- 'format' => @$_REQUEST['format'],
+ 'format' => $this->p('format'),
));
// END
}
function url_edit($values=null,$sep='&') {
- if ($values === null) $values = $_REQUEST;
+ if ($values === null) $values = $this->db->p();
$url_edit = array();
foreach ($this->fields_keys() as $name => $spec) {
$url_edit[] = $name . '=' .urlencode($values[$name]);
}
- return $url_edit ? 'edit/?table='.$_REQUEST['table'].$sep.join($sep,$url_edit) : '';
+ return $url_edit ? 'edit/?table='.$this->db->p('table').$sep.join($sep,$url_edit) : '';
}
}
function html_edit($values = null) {
- if ($values === null) $values = $_REQUEST;
+ if ($values === null) $values = $this->db->p();
if (!is_array($values)) $values = array($values);
$sql = "SELECT *" . $this->select_extras();
echo ''
.'<div class="db buttons">'
.'<input type="submit" name="update"/>'
- .'<input type="text" name="table" value="'.$_REQUEST['table'].'"/>'
+ .'<input type="text" name="table" value="'.$this->db->p('table').'"/>'
.'<input type="reset" />'
.($_SERVER['HTTP_REFERER'] ? '<input type="button" onclick="document.location=document.referrer" value="Cancel" />' : '')
.'</div>'.PHP_EOL
foreach (array_merge( $this->params, array_keys($this->fields()) ) as $f) {
- if (@strcmp($_REQUEST[$f],'')==0) continue;
- $params[$f] = $_REQUEST[$f];
+ if (@strcmp($this->db->p($f),'')==0) continue;
+ $params[$f] = $this->db->p($f);
}
$html = '';
# Asc
- $sel = ( @$_REQUEST['sort']=="$name asc") ? " sel" : "";
+ $sel = ( $this->db->p('sort')=="$name asc") ? " sel" : "";
$html .= '<a title="First In (asc)" class="sort asc'.$sel.'" href="'.$this->url_params("sort","$name asc").'">'
.'<span class="asc">↓</span>'
.'</a>';
$html .= ' ';
# Desc
- $sel = ( @$_REQUEST['sort']=="$name desc") ? " sel" : "";
+ $sel = ( $this->db->p('sort')=="$name desc") ? " sel" : "";
$html .= '<a title="Last In (desc)" class="sort desc'.$sel.'" href="'.$this->url_params("sort","$name desc").'">'
.'<span class="desc">↑</span>'
.'</a>';
'OR',
),array(
'html' => 'name="op"',
- 'selected' => @$_REQUEST['op'],
+ 'selected' => $this->db->p('op'),
));
$criteria[] = html_select_array(array(
array('json','JSON'),
),array(
'html' => 'name="format"',
- 'selected' => @$_REQUEST['format'],
+ 'selected' => $this->db->p('format'),
));
$criteria[] = '<input type="submit" class="button" value="GO"/>';
// Select
//
$sql = "SELECT *" . $this->select_extras();
- $sql .= " FROM $this->name".$this->where_criterias($_REQUEST,@$_REQUEST['op']);
+ $sql .= " FROM $this->name".$this->where_criterias($this->db->p(),$this->db->p('op'));
$this->sql = $sql;
#$this->debug($sql);
$this->debug($sql,1);
$tot = $query->fetch();
#if (!$tot) return;
- if (isset($_REQUEST['sort'])) $sql .= ' ORDER BY '.$_REQUEST['sort'];
+ if ($this->db->p('sort')) $sql .= ' ORDER BY '.$this->db->p('sort');
- if (isset($_REQUEST['limit'])) {
- $limit = $_REQUEST['limit'];
+ if ($this->db->p('limit')) {
+ $limit = $this->db->p('limit');
$sql .= ' LIMIT '.$limit;
} else {
$limit = '';
if ($opt['is_html']) echo '<div align="center" class="nav" id="nav_top"></div>'.PHP_EOL;
$escape = preg_match('/^(table|row|xml)$/',$format) ? true : false;
- if (preg_match('/^(1)?$/',@$_REQUEST['header'])) echo $this->{"rows_begin_$format"}($opt);
+ if (preg_match('/^(1)?$/',$this->db->p('header'))) echo $this->{"rows_begin_$format"}($opt);
$count = 0;
while ($row = $st->fetch(PDO::FETCH_ASSOC, PDO::FETCH_ORI_NEXT)) {
$count_fields++;
/* only if in latin1
- if ($_REQUEST['format'] == 'csv') {
+ if ($this->db->p('format') == 'csv') {
$row[$f] = utf8_encode($row[$f]);
} elseif ($spec['extra']) {
$row[$f] = htmlentities($row[$f]);