<?php
require_once(realpath(dirname(__FILE__).'/../db.php'));
-require_once(realpath(dirname(__FILE__).'/../db/field.php'));
-require_once(realpath(dirname(__FILE__).'/../out.php'));
+require_once(realpath(NB_ROOT.'/lib/php/db/field.php'));
+require_once(realpath(NB_ROOT.'/lib/php/out.php'));
$DB_TABLE_QUERY_ID = 0;
function __table_define() {
define('TABLE_INDENT',NB_EOL ? "\t" : "");
// Name, could be a select
if (DB_TABLE_QUERY_NAME and stripos($this->name,'SELECT ')===0) {
- #$GLOBALS['DB_TABLE_QUERY_ID']++; $name = DB_TABLE_QUERY_NAME . $GLOBALS['DB_TABLE_QUERY_ID'];
$this->db()->conn->query("CREATE TEMPORARY TABLE ".DB_TABLE_QUERY_NAME." AS $this->name");
$this->name = DB_TABLE_QUERY_NAME;
// Virtual Table
} elseif (DB_TABLE_QUERY_NAME and !empty($this->sql)) {
+ if ($this->name) $this->name = DB_TABLE_QUERY_NAME;
$this->db()->conn->query("CREATE TEMPORARY TABLE $this->name AS $this->sql");
} elseif (preg_match('/\b(\.import|LOAD DATA|COPY|INSERT|REPLACE|DELETE|TRUNCATE|CREATE|DROP|ALERT)\b/',$this->name)) {
Wordpress
-----------------------------------------------------------------*/
public function rows_begin_wp() {
- require_once(realpath(dirname(__FILE__).'/wp.php'));
+ require_once(NB_ROOT.'/lib/php/db/wp.php'));
$this->_html_table = new html_table($this);
return '';
}
'table.fields' => array (
'sql' => 'PRAGMA table_info(\'<T.NAME>\')',
- 'fct' => create_function('&$r',join('',array(
- #'debug("zaza");',
- '$r["key"] = $r["pk"] == "0" ? 0 : 1;',
- 'if (!empty($r["dflt_value"])) $r["default"] = trim($r["dflt_value"],"\'");',
- '$r["null"] = $r["notnull"] == "0" ? 1 : 0;',
- '$r["autoincrement"] = preg_match("/[\(,]\s*".$r["name"]." [^\),]+ AUTOINCREMENT/",$r["this"]->sql()) ? 1 : 0;',
- '$r["extra"] = preg_match("/[\(,]\s*".$r["name"]." \S+ (COLLATE[^,)]+)/",$r["this"]->sql(),$m) ? trim($m[1]) : "";',
- #'debug($m);'
- ))),
+ 'fct' => function(&$r) {
+ $r["key"] = $r["pk"] == "0" ? 0 : 1;
+
+ if (!empty($r["dflt_value"])) $r["default"] = trim($r["dflt_value"],"'");
+
+ $r["null"] = $r["notnull"] == "0" ? 1 : 0;
+
+ $sql = $r["this"]->sql();
+ $name_exp = preg_quote($r["name"]);
+ $r["autoincrement"] = preg_match("/[\(,]\s*".$name_exp." [^\),]+ AUTOINCREMENT/",$sql) ? 1 : 0;
+
+ $r["extra"] = preg_match("/[\(,]\s*".$name_exp." \S+ (COLLATE[^,)]+)/",$sql,$m) ? trim($m[1]) : "";
+ },
),
'field.create' => function(&$field) {
<?php
-define(PRODUCTION,empty($_SERVER['PRODUCTION']) ? false : (bool)$_SERVER['PRODUCTION']);
+define('PRODUCTION',empty($_SERVER) or empty($_SERVER['PRODUCTION']) ? false : (bool)$_SERVER['PRODUCTION']);
require_once(realpath(dirname(__FILE__).'/../../lib/php/nb.php'));
require_once(NB_ROOT.'/lib/php/http.php');
class DbQ extends nb {
- public $perm = 9; // Minimum permission access 0 = readonly
const ADMIN = 9;
const DELETE = 4;
const WRITE = 3;
const VIEW = 1;
+ public $perm = self::ADMIN;
public $title = 'Dbq';
public $sep_title = ' / ';
- public $format_html = 'table';
public $format_cli = 'human';
- public $format_cli_ua_exp = '/^\S+\s+.Windows|iPhone|Android|Macintosh|Linux/';
- public $limits = [];
- # to overwrite Db web list
- public $formats = [ 'html','csv','xml','json','yaml','sh','sql','php' ];
+ public $format_html = 'table';
+ public $format_html_ua_exp = '/^\S+\s+.Windows|iPhone|Android|Macintosh|Linux/';
+ public $uri;
+ public $uri_params;
public $param_args_sep = '|';
public $param_exp_value = '[\w\._:-]{2,100}';
const PARAM_DB_DEFAULT = 'ls';
- #public $action = 'ls';
public $params = [
'format' => '',
- 'db' => self::PARAM_DB_DEFAULT,
- 'table' => self::PARAM_DB_DEFAULT,
- 'action' => self::PARAM_DB_DEFAULT,
+ 'db' => '',
+ 'table' => '',
+ 'action' => '',
'args' => '',
];
# NB 23.12.16 public $params_deep = [];
+ # Db
+ public $limits = [];
+ public $formats = [ 'html','csv','xml','json','yaml','sh','sql','php' ];
+
+ # Page
public $css = '/default'.(PRODUCTION ? '.min' : '').'.css';
public $js = '/default'.(PRODUCTION ? '.min' : '').'.js';
public $ext;
- public $uri;
- public $uri_params;
-
# Object
public $page;
public $table;
'title',
'css',
'js',
- 'format_cli_ua_exp'
+ 'format_html_ua_exp'
] as $k) {
$env = 'DBQ_'.strtoupper($k);
if (!empty($_SERVER[$env])) $this->$k = $_SERVER[$env];
}
+
// Envs -> params
foreach ([
'format',
$env = 'DBQ_PARAM_'.strtoupper($k);
if (!empty($_SERVER[$env])) $this->params[$k] = $_SERVER[$env];
}
- /*
- */
//
// Init
unset($opt['run']);
parent::__construct($opt);
- if (!$this->params['db']) $this->not_implemented('Db missing');
//
// Page
// Connect
if (!empty($connect)) {
+ if (!$this->params['db']) $this->not_implemented('Params db missing');
if (!isset($this->db)) $this->db = $this->db();
if (!is_array($connect)
$connect = $this->db->conf[$this->params['db']];
}
- if (empty($connect['name'])) $this->not_found('Unknown database');
+ if (empty($connect['name'])) $this->not_found('Unknown database: '.$this->params['db']);
$this->db->__construct($connect);
$this->id = $this->params['db'];
public function table() {
+
if (!in_array($this->params['table'],array_keys($this->db->tables()))) {
- #$this->bye($this->db->tables());
- $this->not_found('Unknown table');
+
+ # We Allow SELECT only in admin
+ if ($this->perm < self::ADMIN or !preg_match('/^SELECT /',$this->params['table'])) {
+ $this->not_found('Unknown table: '.$this->params['table']);
+ }
+
}
$this->table = $this->db->table($this->params['table'],[
}
public function uri_params() {
+ #
+ # Parse path, respect params order
+ #
$values = [];
$path = '';
$args = '';
} else {
$format =
- (!empty($_SERVER['HTTP_USER_AGENT']) and preg_match($this->format_cli_ua_exp,$_SERVER['HTTP_USER_AGENT']))
+ (!empty($_SERVER['HTTP_USER_AGENT']) and preg_match($this->format_html_ua_exp,$_SERVER['HTTP_USER_AGENT']))
? 'html'
: $this->format_cli
;
return $this->page($this->page->phpinfo($txt));
}
- public function run() {
-
- #
- # Db Init
- #
- $this->db();
- $this->init();
-
- #
- # Actions pre Db
- #
+ public function run_root() {
$action = $this->params['db'];
+ if (!$action and self::PARAM_DB_DEFAULT) $action = self::PARAM_DB_DEFAULT;
#$this->params = [ 'action' => $action ];
if ($action == 'help') {
$this->page($rows);
}
+ }
- #
- # Db Connections
- #
- $this->params['db'] = $action;
- $this->db(true);
-
- #
- # Db actions
- #
+ public function run_db() {
+ $this->db(true); # Db Connections
$action = $this->params['table'];
- $this->params['table'] = '';
+ if (!$action and self::PARAM_DB_DEFAULT) $action = self::PARAM_DB_DEFAULT;
+# NB 27.12.16 $this->params['table'] = '';
if ($action == 'help') {
$this->page($this->db,[
}
- #
- # Table Init
- #
- $this->params['table'] = $action;
- $this->table();
-
- #
- # Table action
- #
+ }
+
+ public function run_table() {
+ $this->table(); # Table init
$action = $this->params['action'];
+ if (!$action and self::PARAM_DB_DEFAULT) $action = self::PARAM_DB_DEFAULT;
+
#bye([$this->params,$action]);
if ($action == 'help') {
$this->page($this->table,[
$this->not_implemented('Unknown table action'." `$action`");
}
+ }
+
+ public function run() {
+
+ #
+ # Db Init
+ #
+ $this->db();
+ $this->init();
+
+ #
+ # Root Actions
+ #
+ $this->run_root();
+
+ #
+ # Db actions
+ #
+ $this->run_db();
+
+ #
+ # Table action
+ #
+ $this->run_table();
return true;
}