array( 'driver' => 'mysql', 'dbhost' => 'localhost', 'dbport' => '3306', 'charset' => 'utf8mb4', 'dbuser' => '', 'dbpass' => '', 'available' => array(), 'forbidden' => array('mysql','information_schema','performance_schema','sys'), 'database' => '', 'table' => '', ), 'SQLite' => array( 'driver' => 'sqlite', 'dbpath' => '../sqlite', 'charset' => 'utf8mb4', 'dbuser' => '', 'dbpass' => '', 'available' => array(), 'forbidden' => array('sqlite_sequence'), 'database' => '', 'table' => '', ), ); public $forbidden = array('mysql','information_schema','performance_schema','sys','sqlite_sequence'); public $driver = 'mysql'; public $dbpath = '../sqlite'; public $dbhost = '127.0.0.1'; public $dbport = '3306'; public $charset = 'utf8mb4'; public $dbuser = ''; public $dbpass = ''; public $database = ''; public $table = ''; public $column = ''; public $field = ''; public $key = ''; public $val = ''; public $status = array(); public $notices = array(); public $warnings = array(); public $errors = array(); public $sqls = array(); public function get($var=null){ $val = !empty($this->$var) ? $this->$var : $this; //foreach($this as $key => $val) { // $_SESSION[$key] = $val; //} return($val); } } interface MVC { public function controller(); public function model(); public function view(); } class DBone implements MVC { public $DBO, $frame, $page, $database, $table, $query; public function __construct(){ date_default_timezone_set('America/Los_Angeles'); if (!isset($_SESSION)) session_start(); } public function controller(){ $this->frame = $this->request('frame'); $this->page = $this->request('page'); $this->database = $this->request('database'); $this->table = $this->request('table'); $this->query = $this->request('query'); $this->driver = $this->session('driver'); if (empty($this->page)) { $this->page = 'login'; } /* if (empty($this->page)) { if (!$this->isConnected()) { $this->page = 'login'; } else if (!$this->isLoggedIn()) { $this->page = 'login'; } else { $this->page = 'databases'; } } */ } public function model(){ /* if ($this->frame == 'body') { $this->DBO = new $this->driver(); $dba = array( 'driver' => $this->session('driver', 'mysql'), 'dbhost' => $this->session('dbhost', 'localhost'), 'dbport' => $this->session('dbport', '3306'), 'dbchar' => $this->session('dbchar', 'utf8mb4'), 'dbuser' => $this->session('dbuser'), 'dbpass' => $this->session('dbpass'), 'dbname' => $this->session('dbname'), 'dbpath' => $this->session('dbpath'), 'dbfile' => $this->session('dbfile'), ); $this->status = $this->DBO->connect($dba); } */ } public function view(){ switch($this->frame){ case('head'): $this->showHead(); $this->showHeader(); $this->showFoot(); break; case('body'): $this->showBody(); break; case('foot'): $this->showHead(); $this->showFooter(); $this->showFoot(); break; default: $this->frames(); break; } } public function connect($dba){ $this->debug(__METHOD__, 'dba', $dba, 1); $driver = $dba['driver']; //$this->DBO = new $driver; $this->DBO = new MySQL(); $status = $this->DBO->connect($dba); $this->debug(__METHOD__, 'status', $status, 1); $data = $this->DBO->getDatabases(); $this->debug(__METHOD__, 'data2', $data, 1); //$status = true; return($status); } public function getDBA() { $dba = array( 'driver' => $this->session('driver', 'mysql'), 'dbhost' => $this->session('dbhost', 'localhost'), 'dbport' => $this->session('dbport', '3306'), 'dbchar' => $this->session('dbchar', 'utf8mb4'), 'dbuser' => $this->session('dbuser'), 'dbpass' => $this->session('dbpass'), 'dbname' => $this->session('dbname'), 'dbpath' => $this->session('dbpath'), 'dbfile' => $this->session('dbfile'), ); return($dba); } public function request($var=null, $val=null) { if (empty($var)) { printf('
%s
'."\n", print_r($_REQUEST,1)); } else { $val = isset($_REQUEST[$var]) ? $_REQUEST[$var] : $val; } return($val); } public function session($var=null, $val=null) { if (empty($var)) { printf('
%s
'."\n", print_r($_REQUEST,1)); } else { $val = isset($_SESSION[$var]) ? $_SESSION[$var] : $val; } return($val); } public function frames(){ //$this->debug(__METHOD__, 'SERVER', $_SERVER, 1); printf(''."\n"); printf(''."\n"); printf(''."\n"); printf('DBone'."\n"); printf(''."\n"); printf(''."\n"); printf(''."\n"); printf(''."\n"); printf(''."\n"); printf(''."\n"); printf(''."\n"); printf(''."\n"); printf(''."\n"); printf(''."\n"); printf(''."\n"); $src = $this->getFrameSrc("head", "head"); printf(''."\n", $src); $src = $this->getFrameSrc("body", $this->page); printf(''."\n", $src); $src = $this->getFrameSrc("foot", "foot"); printf(''."\n", $src); printf(''."\n"); printf(''."\n"); } public function getFrameSrc($frame, $page){ $src = sprintf("frame=%s&page=%s", $frame, $page); $ref = !empty($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : ''; $refs = explode('?', $ref); if (!empty($refs[1])) { //$this->debug(__METHOD__, 'refs[1]', $refs[1], 1); parse_str($refs[1], $data); //$this->debug(__METHOD__, 'data', $data, 1); $data['frame'] = $frame; $data['page'] = $page; $src = http_build_query($data); //$this->debug(__METHOD__, 'src', $src, 1); } return($src); } public function showHead(){ printf(''."\n"); printf(''."\n"); printf(''."\n"); printf('DBone'."\n"); printf(''."\n"); printf(''."\n"); printf(''."\n"); printf(''."\n"); printf(''."\n"); printf(''."\n"); printf(''."\n"); printf(''."\n"); printf(''."\n"); printf(''."\n"); printf(''."\n"); printf(''."\n"); printf(''."\n"); //$this->debug(__METHOD__, 'SERVER', $_SERVER, 1); printf('
'."\n"); } public function showHeader(){ printf('
'."\n"); printf('
'."\n", $this->database, $this->table); printf(''."\n"); //printf(''."\n"); printf(''."\n"); if (!$this->isLoggedIn() || empty($this->driver)) { printf(''."\n", 'DBone'); printf(''."\n", $this->query); printf(''."\n"); //printf(''."\n"); printf('
'."\n"); printf('%s'."\n"); } else { $this->page = 'databases'; printf(''."\n", $this->page); printf(''."\n"); } printf('
'."\n"); printf('
'."\n"); printf('
'."\n"); } public function showFooter(){ printf(''."\n"); } public function showFoot(){ //printf('
'."\n"); printf('
'."\n"); //$this->sql(); printf(''."\n"); printf(''."\n"); } public function showStatus(){ //$this->clearStatus(); $this->notice(); $this->warning(); $this->error(); //$this->sql(); $this->clearStatus(); } public function clearStatus(){ //$_SESSION['status'] = array(); $_SESSION['notices'] = array(); $_SESSION['warnings'] = array(); $_SESSION['errors'] = array(); $_SESSION['sqls'] = array(); } public function msg($type, $msg=null){ if (!empty($msg)) { $_SESSION[$type][] = $msg; } else if (!empty($_SESSION[$type])) { $types = array('notices' => 'h4','warnings' => 'h5','errors' => 'h6'); $msgs = $_SESSION[$type]; //printf('
MSGS = %s
'."\n", print_r($msgs)); foreach($msgs as $msg){ printf('<%s>%s'."\n", $types[$type], $msg, $types[$type]); } //$_SESSION[$type] = array(); } } public function notice($msg=null){ $this->msg('notices', $msg); } public function warning($msg=null){ $this->msg('warnings', $msg); } public function error($msg=null){ $this->msg('errors', $msg); } public function sql($sql=null){ if (!empty($sql)) { $_SESSION['sqls'][] = $sql; } else if (!empty($_SESSION['sqls'])) { printf('
'."\n"); foreach($_SESSION['sqls'] as $sql){ printf('%s
'."\n", $sql); } printf('
'."\n"); $_SESSION['sqls'] = array(); } } public function showBody(){ if (class_exists($this->page)) { $Page = new $this->page(); $Page->controller(); $Page->model(); $Page->view(); } else { $this->showHead(); //printf('
'."\n"); printf('

Page Not Found

'."\n"); //printf('
'."\n"); $this->showFoot(); } } public function connect222($dba){ $driver = $dba['driver']; $this->DBO = new $driver; //$this->getSession(); //if ($this->isLoggedIn()){ /* $driver = !empty($_SESSION['driver']) ? $_SESSION['driver'] : ''; $dbpath = !empty($_SESSION['dbpath']) ? $_SESSION['dbpath'] : ''; $dbhost = !empty($_SESSION['dbhost']) ? $_SESSION['dbhost'] : ''; $dbport = !empty($_SESSION['dbport']) ? $_SESSION['dbport'] : ''; $charset = !empty($_SESSION['charset']) ? $_SESSION['charset'] : ''; $dbuser = !empty($_SESSION['dbuser']) ? $_SESSION['dbuser'] : ''; $dbpass = !empty($_SESSION['dbpass']) ? $_SESSION['dbpass'] : ''; */ /* $this->DBO = new DBO(); //printf('
CONNECT DRIVER = %s
'."\n", $driver); //printf('
CONNECT DATABASE = %s
'."\n", $database); //printf('
CONNECT DBPATH = %s
'."\n", $dbpath); if ($this->driver == 'sqlite') { //printf('
DATABASE = %s
'."\n", $database); if (!empty($database)) { $dbhost = $this->dbpath; //printf('
TRY CONNECTING TO = %s
'."\n", $database); $status = $this->DBO->connect($this->driver, $dbhost, $this->dbport, $this->charset, $this->dbuser, $this->dbpass, $database); //printf('
CONNECT STATUS = %s
'."\n", $status); } else { if (is_dir($this->dbpath)) { //printf('
CONNECT DBPATH TRUE = %s
'."\n", $this->dbpath); $status = true; } else { $status = false; } } } else { $status = $this->DBO->connect($this->driver, $this->dbhost, $this->dbport, $this->charset, $this->dbuser, $this->dbpass, $database); //printf('
CONNECT STATUS2 = %s
'."\n", $status); } if ($status !== true) { //printf('
CONNECT ERROR: DATABASE FAILED TO CONNECT!
'."\n"); } else { //printf('
CONNECTED TO DATABASE = %s
'."\n", $database); } */ //return($status); } public function getConnection(){ $this->data['driver'] = !empty($_SESSION['driver']) ? $_SESSION['driver'] : 'mysql'; $this->data['dbhost'] = !empty($_SESSION['dbhost']) ? $_SESSION['dbhost'] : 'localhost'; $this->data['dbport'] = !empty($_SESSION['dbport']) ? $_SESSION['dbport'] : '3306'; $this->data['charset'] = !empty($_SESSION['charset']) ? $_SESSION['charset'] : 'utf8mb4'; $this->data['dbuser'] = !empty($_SESSION['dbuser']) ? $_SESSION['dbuser'] : ''; $this->data['dbpass'] = !empty($_SESSION['dbpass']) ? $_SESSION['dbpass'] : ''; $this->data['database'] = !empty($_SESSION['database']) ? $_SESSION['database'] : ''; $this->data['table'] = !empty($_SESSION['table']) ? $_SESSION['table'] : ''; $this->data['view'] = !empty($_SESSION['view']) ? $_SESSION['view'] : ''; return($data); } public function setConnection($name){ $connection = $_SESSION['connections'][$name]; //printf('
CONNECTION = %s
'."\n", print_r($connection)); foreach($connection as $key => $val){ $_SESSION[$key] = $val; } //printf('
SESSION = %s
'."\n", print_r($_SESSION));exit(); return(true); } public function getDrivers() { $possible_drivers = array ( 'mysql', 'sqlite', 'cubrid', 'firebird', 'ibm', 'informix', 'mssql', 'oci', 'oci8', 'odbc', 'pqsql', 'snowflake', '4d', ); $available_drivers = \PDO::getAvailableDrivers(); foreach($possible_drivers as $driver) { $drivers[$driver] = in_array($driver, $available_drivers) ? 1 : 0; } //printf('
DRIVERS = %s

'."\n", print_r($drivers)); return $drivers; } public function getDatabases($order_by=null, $order=null){ $this->debug(__METHOD__, 'order_by2', $order_by,1); $this->debug(__METHOD__, 'order2', $order,1); $dba = $this->getDBA(); $this->debug(__METHOD__, 'dba2', $dba, 1); $dbo = new $dba['driver']; $status = $dbo->connect($dba); $this->debug(__METHOD__, 'status2', $status, 1); $this->debug(__METHOD__, 'order_by1', $this->order_by,1); $this->debug(__METHOD__, 'order1', $this->order,1); //$this->DBO = new $this->driver(); //$status = $this->DBO->connect($this->database); $databases = $dbo->getDatabases($order_by=null, $order=null); $this->debug(__METHOD__, 'databases2', $databases, 1); return($databases); } public function getDatabases2($driver, $order_by=null, $order=null){ //$this->debug(__METHOD__, 'driver', $driver,1); //$this->debug(__METHOD__, 'order_by', $order_by,1); //$this->debug(__METHOD__, 'order', $order,1); $databases = array(); if (empty($driver)) return($databases); if ($driver == 'mysql') { $this->connect(); $order_by = !empty($order_by) ? $order_by : 'database'; $order = !empty($order) ? $order : 'ASC'; $sql = "SELECT A.SCHEMA_NAME AS `database`"; $sql.= ", COUNT(B.TABLE_NAME) AS `tables`"; $sql.= " FROM information_schema.SCHEMATA A"; $sql.= " LEFT JOIN information_schema.TABLES B"; $sql.= " ON A.SCHEMA_NAME = B.TABLE_SCHEMA"; $sql.= " WHERE A.SCHEMA_NAME NOT IN ('information_schema','mysql','performance_schema','sys')"; $sql.= " GROUP BY A.SCHEMA_NAME"; $sql.= " ORDER BY `$order_by` $order"; //$this->debug(__METHOD__, 'sql1', $sql,1); $databases = $this->DBO->query($sql); //$this->debug(__METHOD__, 'databases', $databases,1); } else if ($driver == 'sqlite') { $files = scandir($this->dbpath); foreach($files as $file) { if (strpos($file, '.') == 0) continue; $database = substr($file, 0, strrpos($file, '.')); $databases[] = $database; } } else { $this->connect(); $sql = "SHOW DATABASES;"; $data = $this->DBO->query($sql); foreach($data as $rec) { $databases[] = $rec['Database']; } } //$this->debug(__METHOD__, 'databases1', $databases,1); if (!empty($this->available)) { $databases = $this->available; } //$this->debug(__METHOD__, 'available', $this->available); //$this->debug(__METHOD__, 'forbidden', $this->forbidden); if (!empty($this->forbidden)) { $dbs = array(); foreach($databases as $database) { if(!in_array($database, $this->forbidden)) { $dbs[] = $database; } } $databases = $dbs; } //$this->debug(__METHOD__, 'databases2', $databases); return($databases); } public function getData($database, $table){ $fields = array(); $this->connect($database); $sql = "SELECT * FROM `$table`"; $this->sql($sql); $recs = $this->DBO->query($sql); $recs = !empty($recs) ? $recs : array(); //printf('
RECS = %s
'."\n", print_r($recs));exit; return($recs); } public function getDriver(){ $driver = !empty($_SESSION['driver']) ? $_SESSION['driver'] : ''; //$this->debug(__METHOD__, 'driver', $driver, 1); return($driver); } public function getFields($database, $table){ $fields = array(); $this->connect($database); $sql = "SELECT * FROM `$table` LIMIT 1"; $this->sql($sql); $recs = $this->DBO->query($sql); $field = array(); foreach($recs[0] as $key => $val){ $fields[] = $key; } return($fields); } public function getColumns($database, $table){ if ($this->driver == 'sqlite') { $sql = "PRAGMA table_info(`$this->table`);"; $this->sql($sql); $this->debug(__METHOD__, 'sql', $sql); $data = $this->DBO->query($sql); $this->debug(__METHOD__, 'data', $data); $columns = array(); //if (!empty($data) || count($data) != 0) { if (is_string($data)) return($data); $columns = array(); foreach($data as $rec) { $column = array(); $column['column'] = $rec['name']; $column['type'] = $rec['type']; $column['key'] = $rec['pk'] == '1' ? 'PRI' : ''; $column['null'] = $rec['notnull'] == '0' ? 'Yes' : 'No'; $columns[] = $column; } } else if ($this->driver == 'mysql') { //$sql = "SHOW COLUMNS FROM `$this->table`"; $sql = "SELECT * FROM information_schema.columns WHERE table_schema = '$this->database' and table_name = '$this->table'"; $this->debug(__METHOD__, 'sql', $sql); $this->sql($sql); $data = $this->DBO->query($sql); $this->debug(__METHOD__, 'data', $data); $columns = array(); //if (!empty($data) || count($data) != 0) { if (is_string($data)) return($data); $columns = array(); foreach($data as $rec) { $column = array(); $column['column'] = $rec['COLUMN_NAME']; $column['type'] = $rec['COLUMN_TYPE']; $types = explode(' ', trim($column['type'])); $column['type'] = $types[0]; //$column['type'] .= !empty($rec['NUMERIC_PRECISION']) ? '('.$rec['NUMERIC_PRECISION'].')' : ''; $column['key'] = $rec['COLUMN_KEY']; $column['extra'] = $rec['EXTRA']; $column['extra'] = str_replace('auto_increment', 'AI', $column['extra']); $column['extra'] = str_replace('DEFAULT_GENERATED', 'DG', $column['extra']); $column['null'] = $rec['IS_NULLABLE'] == 'NO' ? 'No' : 'Yes'; $columns[] = $column; } } else { } return($columns); } public function getColumn($database, $table, $column){ if ($this->driver == 'sqlite') { $sql = "PRAGMA table_info(`$this->table`);"; $this->sql($sql); $this->debug(__METHOD__, 'sql', $sql); $data = $this->DBO->query($sql); $this->debug(__METHOD__, 'data', $data); $columns = array(); //if (!empty($data) || count($data) != 0) { if (is_string($data)) return($data); foreach($data as $rec) { $col = array(); $col['column'] = $rec['name']; $col['type'] = $rec['type']; $col['key'] = $rec['pk'] == '1' ? 'PRI' : ''; $col['null'] = $rec['notnull'] == '0' ? 'Yes' : 'No'; $col['default'] = $rec['dflt_value']; if ($col['column'] == $column) { $column = $col; break; } } } else { //$sql = "SHOW COLUMNS FROM `$this->table`"; //$sql = "SELECT * FROM information_schema.columns WHERE table_schema = '$this->database' and table_name = '$this->table'"; $sql = "SELECT * FROM information_schema.columns WHERE `TABLE_SCHEMA` = '$database' AND `TABLE_NAME` = '$table' AND `COLUMN_NAME` = '$column';"; $this->debug(__METHOD__, 'sql', $sql); $data = $this->DBO->query($sql); $this->debug(__METHOD__, 'data', $data); if (is_string($data)) return($data); $rec = $data[0]; $column = array(); $column['column'] = $rec['COLUMN_NAME']; $column['type'] = $rec['COLUMN_TYPE']; $types = explode(' ', trim($column['type'])); $column['type'] = $types[0]; $column['key'] = $rec['COLUMN_KEY']; $column['attr'] = $rec['COLUMN_TYPE']; $attrs = explode(' ', trim($column['attr'])); $column['attr'] = !empty($types[1]) ? $types[1] : ''; $column['null'] = $rec['IS_NULLABLE'] == 'NO' ? 'No' : 'Yes'; $column['default'] = $rec['COLUMN_DEFAULT']; $column['extra'] = $rec['EXTRA']; $column['collation'] = $rec['COLLATION_NAME']; $column['charset'] = $rec['CHARACTER_SET_NAME']; $column['comment'] = $rec['COLUMN_COMMENT']; } return($column); } public function getIndexes($database, $table){ if ($this->driver == 'sqlite') { $sql = "SELECT * FROM sqlite_master WHERE type = 'index' AND tbl_name = '$table'"; $this->debug(__METHOD__, 'sql', $sql); $data = $this->DBO->query($sql); $this->debug(__METHOD__, 'data', $data); $indexes = array(); foreach($data as $rec) { $index = array(); $index['column'] = $rec['name']; //$index['table'] = $rec['tbl_name']; $index['sql'] = $rec['sql']; $indexes[] = $index; } } else { $sql = "SHOW INDEXES FROM `$table`"; $this->debug(__METHOD__, 'sql', $sql); //$this->sql($sql); $data = $this->DBO->query($sql); $this->debug(__METHOD__, 'data', $data); if (is_string($data)) return($data); $indexes = array(); foreach($data as $rec) { $index = array(); $index['index'] = $rec['Key_name']; $index['column'] = $rec['Column_name']; //$index['table'] = $rec['Table']; $index['type'] = $rec['Index_type']; $index['uniq'] = $rec['Non_unique'] == '0' ? 'Yes' : 'No'; //$index['packed'] = !empty($rec['Packed']) ? 'Yes' : 'No'; //$index['card'] = $rec['Cardinality']; //$index['coll'] = $rec['Collation']; $index['null'] = !empty($rec['Null']) ? 'Yes' : 'No'; //$index['visible'] = $rec['Visible'] == 'YES' ? 'Yes' : 'No'; //$index['comment'] = $rec['Comment']; $indexes[] = $index; } } return($indexes); } public function getIndex($database, $table, $column){ $this->debug(__METHOD__, 'database', $database); $this->debug(__METHOD__, 'table', $table); $this->debug(__METHOD__, 'column', $column); if ($this->driver == 'sqlite') { $sql = "SELECT * FROM sqlite_master WHERE type = 'index' AND tbl_name = '$table'"; $this->debug(__METHOD__, 'sql', $sql); $data = $this->DBO->query($sql); $this->debug(__METHOD__, 'data', $data); $index = array(); foreach($data as $rec){ if ($rec['name'] == $column) { $index = array(); $index['index'] = $rec['name']; $index['sql'] = $rec['sql']; break; } } } else { $sql = "SHOW INDEXES FROM `$table`"; $this->debug(__METHOD__, 'sql', $sql); $data = $this->DBO->query($sql); $this->debug(__METHOD__, 'data', $data); $index = array(); foreach($data as $rec){ if ($rec['Key_name'] == $column) { $index = array(); $index['index'] = $rec['Key_name']; $index['column'] = $rec['Column_name']; //$index['table'] = $rec['Table']; $index['type'] = $rec['Index_type']; $index['uniq'] = $rec['Non_unique'] == '0' ? 'Yes' : 'No'; $index['packed'] = !empty($rec['Packed']) ? 'Yes' : 'No'; $index['card'] = $rec['Cardinality']; $index['coll'] = $rec['Collation']; $index['null'] = !empty($rec['Null']) ? 'Yes' : 'No'; //$index['visible'] = $rec['Visible'] == 'YES' ? 'Yes' : 'No'; $index['comment'] = $rec['Comment']; break; } } } $this->debug(__METHOD__, 'index', $index); return($index); } public function getConnections(){ $Config = new Config(); $connections = $Config->get('connections'); $this->debug(__METHOD__, 'connections', $connections); return($connections); } public function getRequest($var=null, $val=null) { if (empty($var)) { printf('
%s
'."\n", print_r($_REQUEST,1)); } else { $val = isset($_REQUEST[$var]) ? $_REQUEST[$var] : $val; } return($val); } public function getRequest1(){ //parse_str($_REQUEST, $this->request); //$this->debug(__METHOD__, 'request', $this->request); foreach($_REQUEST as $request){ $this->$request = $request; } } public function getRequest2(){ //$this->driver = !empty($_REQUEST['driver']) ? $_REQUEST['driver'] : $_SESSION['driver']; //$this->dbhost = !empty($_REQUEST['dbhost']) ? $_REQUEST['dbhost'] : $_SESSION['dbhost']; //$this->dbport = !empty($_REQUEST['dbport']) ? $_REQUEST['dbport'] : $_SESSION['dbport']; //$this->charset = !empty($_REQUEST['charset']) ? $_REQUEST['charset'] : $_SESSION['charset']; //$this->dbuser = !empty($_REQUEST['dbuser']) ? $_REQUEST['dbuser'] : $_SESSION['dbuser']; //$this->dbpass = !empty($_REQUEST['dbpass']) ? $_REQUEST['dbpass'] : $_SESSION['dbpass']; $driver = !empty($_SESSION['driver']) ? $_SESSION['driver'] : ''; $dbpath = !empty($_SESSION['dbpath']) ? $_SESSION['dbpath'] : ''; $dbhost = !empty($_SESSION['dbhost']) ? $_SESSION['dbhost'] : ''; $dbport = !empty($_SESSION['dbport']) ? $_SESSION['dbport'] : ''; $charset = !empty($_SESSION['charset']) ? $_SESSION['charset'] : ''; $dbuser = !empty($_SESSION['dbuser']) ? $_SESSION['dbuser'] : ''; $dbpass = !empty($_SESSION['dbpass']) ? $_SESSION['dbpass'] : ''; $available = !empty($_SESSION['available']) ? $_SESSION['available'] : ''; $forbidden = !empty($_SESSION['forbidden']) ? $_SESSION['forbidden'] : ''; $this->driver = !empty($_REQUEST['driver']) ? $_REQUEST['driver'] : $driver; $this->dbpath = !empty($_REQUEST['dbpath']) ? $_REQUEST['dbpath'] : $dbpath; $this->dbhost = !empty($_REQUEST['dbhost']) ? $_REQUEST['dbhost'] : $dbhost; $this->dbport = !empty($_REQUEST['dbport']) ? $_REQUEST['dbport'] : $dbport; $this->charset = !empty($_REQUEST['charset']) ? $_REQUEST['charset'] : $charset; $this->dbuser = !empty($_REQUEST['dbuser']) ? $_REQUEST['dbuser'] : $dbuser; $this->dbpass = !empty($_REQUEST['dbpass']) ? $_REQUEST['dbpass'] : $dbpass; $this->available = !empty($_REQUEST['available']) ? $_REQUEST['available'] : $available; $this->forbidden = !empty($_REQUEST['forbidden']) ? $_REQUEST['forbidden'] : $forbidden; $this->frame = !empty($_REQUEST['frame']) ? $_REQUEST['frame'] : ''; $this->page = !empty($_REQUEST['page']) ? $_REQUEST['page'] : ''; $this->query = !empty($_REQUEST['query']) ? $_REQUEST['query'] : ''; $this->command = !empty($_REQUEST['command']) ? $_REQUEST['command'] : ''; $this->cmd = !empty($_REQUEST['cmd']) ? $_REQUEST['cmd'] : ''; $this->sql = !empty($_REQUEST['sql']) ? $_REQUEST['sql'] : ''; $this->connection= !empty($_REQUEST['connection'])? $_REQUEST['connection']: ''; $this->database = !empty($_REQUEST['database']) ? $_REQUEST['database'] : ''; $this->database1 = !empty($_REQUEST['database1']) ? $_REQUEST['database1'] : ''; $this->database2 = !empty($_REQUEST['database2']) ? $_REQUEST['database2'] : ''; $this->table = !empty($_REQUEST['table']) ? $_REQUEST['table'] : ''; $this->table1 = !empty($_REQUEST['table1']) ? $_REQUEST['table1'] : ''; $this->table2 = !empty($_REQUEST['table2']) ? $_REQUEST['table2'] : ''; $this->view = !empty($_REQUEST['view']) ? $_REQUEST['view'] : ''; $this->view1 = !empty($_REQUEST['view1']) ? $_REQUEST['view1'] : ''; $this->view2 = !empty($_REQUEST['view2']) ? $_REQUEST['view2'] : ''; $this->column = !empty($_REQUEST['column']) ? $_REQUEST['column'] : ''; $this->column1 = !empty($_REQUEST['column1']) ? $_REQUEST['column1'] : ''; $this->column2 = !empty($_REQUEST['column2']) ? $_REQUEST['column2'] : ''; $this->columns = !empty($_REQUEST['columns']) ? $_REQUEST['columns'] : ''; $this->columns2 = !empty($_REQUEST['columns2']) ? $_REQUEST['columns2'] : ''; $this->order_by = !empty($_REQUEST['order_by']) ? $_REQUEST['order_by'] : ''; $this->order = !empty($_REQUEST['order']) ? $_REQUEST['order'] : ''; $this->record = !empty($_REQUEST['record']) ? $_REQUEST['record'] : ''; $this->record1 = !empty($_REQUEST['record1']) ? $_REQUEST['record1'] : ''; $this->record2 = !empty($_REQUEST['record2']) ? $_REQUEST['record2'] : ''; $this->index = !empty($_REQUEST['index']) ? $_REQUEST['index'] : ''; $this->index2 = !empty($_REQUEST['index2']) ? $_REQUEST['index2'] : ''; $this->field = !empty($_REQUEST['field']) ? $_REQUEST['field'] : ''; $this->field2 = !empty($_REQUEST['field2']) ? $_REQUEST['field2'] : ''; $this->order = !empty($_REQUEST['order']) ? $_REQUEST['order'] : ''; $this->where = !empty($_REQUEST['where']) ? $_REQUEST['where'] : ''; $this->key = !empty($_REQUEST['key']) ? $_REQUEST['key'] : ''; $this->val = !empty($_REQUEST['val']) ? $_REQUEST['val'] : ''; $this->trigger = !empty($_REQUEST['trigger']) ? $_REQUEST['trigger'] : ''; $this->event = !empty($_REQUEST['event']) ? $_REQUEST['event'] : ''; $this->routine = !empty($_REQUEST['routine']) ? $_REQUEST['routine'] : ''; //$_SESSION['database'] = !empty($_REQUEST['database']) ? $_REQUEST['database'] : ''; //$_SESSION['table'] = !empty($_REQUEST['table']) ? $_REQUEST['table'] : ''; //$_SESSION['view'] = !empty($_REQUEST['view']) ? $_REQUEST['view'] : ''; //$_SESSION['field'] = !empty($_REQUEST['field']) ? $_REQUEST['field'] : ''; } public function getResults($sql, $database=null){ $results = array(); //printf('
getResults.database = %s
'."\n", print_r($database));exit; if (!empty($database)) { $this->connect($database); } else { $this->connect(); } $this->debug(__METHOD__, 'sql', $sql); $results = $this->DBO->query($sql); $this->debug(__METHOD__, 'results', $results); return($results); } public function getSession(){ $this->driver = !empty($_SESSION['driver']) ? $_SESSION['driver'] : ''; $this->dbpath = !empty($_SESSION['dbpath']) ? $_SESSION['dbpath'] : ''; $this->dbhost = !empty($_SESSION['dbhost']) ? $_SESSION['dbhost'] : ''; $this->dbport = !empty($_SESSION['dbport']) ? $_SESSION['dbport'] : ''; $this->charset = !empty($_SESSION['charset']) ? $_SESSION['charset'] : ''; $this->dbuser = !empty($_SESSION['dbuser']) ? $_SESSION['dbuser'] : ''; $this->dbpass = !empty($_SESSION['dbpass']) ? $_SESSION['dbpass'] : ''; //$this->database = !empty($_SESSION['database']) ? $_SESSION['database'] : ''; $this->available = !empty($_SESSION['available']) ? $_SESSION['available'] : array(); $this->forbidden = !empty($_SESSION['forbidden']) ? $_SESSION['forbidden'] : array(); } public function getStatus($results, $notice, $page, $database=null, $table=null){ /* printf('
RESULTS  = %s
'."\n",print_r($results)); printf('
NOTICE   = %s
'."\n",print_r($notice)); printf('
PAGE     = %s
'."\n",print_r($page)); printf('
DATABASE = %s
'."\n",print_r($database)); printf('
TABLE    = %s
'."\n",print_r($table)); */ if (!is_string($results)) { $this->notice($notice); //$this->debug(__METHOD__, 'SESSION1', $_SESSION); //exit; $this->redirect($page, $database, $table); return(true); } else { $this->error($results); } return(false); } public function getRows($driver, $database, $table){ $this->debug(__METHOD__, 'driver', $driver, 1); $this->debug(__METHOD__, 'database', $database, 1); $this->debug(__METHOD__, 'table', $table, 1); //if ($driver == 'sqlite') { //} else { $sql = "SELECT COUNT(*) AS `count` FROM `$table`"; $this->debug(__METHOD__, 'sql', $sql); //printf('
SQL = %s
'."\n",print_r($sql)); $count = $this->DBO->query($sql); //printf('
COUNT = %s
'."\n",print_r($count)); $this->debug(__METHOD__, 'count', $count); $rows = !empty($count[0]['count']) ? $count[0]['count'] : 0; $this->debug(__METHOD__, 'rows', $rows); //} return($rows); } public function getTables($driver, $database){ //$this->debug(__METHOD__, 'driver', $driver, 1); //$this->debug(__METHOD__, 'database', $database, 1); if ($driver == 'mysql') { //$sql = "SELECT table_schema as 'database', COUNT(*) as 'tables'"; //$sql.= " FROM information_schema.tables"; //$sql.= " WHERE table_type = 'BASE TABLE'"; //$sql.= " GROUP BY table_schema"; //$sql.= " ORDER BY tables DESC"; $sql = "SELECT A.TABLE_NAME AS `table`"; $sql.= ", A.TABLE_ROWS AS `rows`"; //$sql = "SELECT *"; $sql.= " FROM information_schema.TABLES A"; $sql.= " WHERE A.TABLE_SCHEMA = '$database'"; //$sql.= " WHERE A.TABLE_SCHEMA NOT IN ('information_schema','mysql','performance_schema','sys')"; //$sql.= " AND A.TABLE_TYPE != 'VIEW'"; //$sql.= " GROUP BY A.SCHEMA_NAME"; //$sql.= " ORDER BY `$order_by` $order"; //$sql.= " ORDER BY A.TABLE_NAME ASC"; $this->connect($database); //$this->debug(__METHOD__, 'sql', $sql, 1); $tables = $this->DBO->query($sql); //$this->debug(__METHOD__, 'data', $tables, 1); } else if ($driver == 'mysql1') { $this->connect($database); $sql = "SELECT IFNULL(table_schema,'Total') 'Database',TableCount"; $sql.= " FROM information_schema.tables"; $sql.= " WHERE table_schema NOT IN ('information_schema','mysql')"; $sql.= " GROUP BY table_schema WITH ROLLUP) A"; $sql.= " ORDER BY TableCount DESC"; $this->debug(__METHOD__, 'sql', $sql,1); $data = $this->DBO->query($sql); $this->debug(__METHOD__, 'data', $data,1); } else if ($driver == 'mysql2') { $this->connect($database); $sql = "SELECT * FROM information_schema.tables"; $sql.= " WHERE TABLE_TYPE = 'BASE TABLE'"; if (!empty($database)) { $sql.= " AND TABLE_SCHEMA = '$database';"; } $this->debug(__METHOD__, 'sql', $sql); $data = $this->DBO->query($sql); $this->debug(__METHOD__, 'data', $data); $tables = array(); foreach($data as $rec){ $table = array(); $table['table'] = $rec['TABLE_NAME']; $table['database'] = $rec['TABLE_SCHEMA']; $table['created'] = $rec['CREATE_TIME']; $table['engine'] = $rec['ENGINE']; $table['collation'] = $rec['TABLE_COLLATION']; $table['auto_increment'] = $rec['AUTO_INCREMENT']; $table['comment'] = $rec['TABLE_COMMENT']; $tables[] = $table; } } else if ($driver == 'sqlite') { $this->connect($database); $sql = "SELECT * FROM sqlite_master WHERE `type` = 'table' AND name NOT LIKE 'sqlite_%'"; $this->sql($sql); //printf('
SQL = %s
'."\n",print_r($sql)); $recs = $this->DBO->query($sql); //printf('
RECS = %s
'."\n",print_r($recs)); $tables = array(); foreach($recs as $rec) { if ($rec['name'] == 'sqlite_sequence') continue; $table = array(); $table['table'] = $rec['name']; $table['sql'] = $rec['sql']; $tables[] = $table; } } else { $sql = "SHOW TABLES FROM `$database`"; $this->debug(__METHOD__, 'sql', $sql); $data = $this->DBO->query($sql); $this->debug(__METHOD__, 'data', $data); $tables = array(); foreach($data as $rec){ $this->debug(__METHOD__, 'rec', $rec); $tables[] = $table; } } $this->debug(__METHOD__, 'tables', $tables); return($tables); } public function getTrigger($driver, $database, $trigger){ $this->debug(__METHOD__, 'driver', $driver); $this->debug(__METHOD__, 'database', $database); //$sql = "SHOW TRIGGERS FROM `$database`"; $sql = "SHOW TRIGGERS FROM `$database` WHERE `Trigger` = '$trigger'"; $this->debug(__METHOD__, 'sql', $sql); $data = $this->DBO->query($sql); $this->debug(__METHOD__, 'data', $data); return($data); } public function getTriggers($driver, $database){ if ($driver == 'sqlite') { $data = array(); } else { $this->debug(__METHOD__, 'driver', $driver); $this->debug(__METHOD__, 'database', $database); $sql = "SHOW TRIGGERS FROM `$database`"; $this->debug(__METHOD__, 'sql', $sql); $data = $this->DBO->query($sql); $this->debug(__METHOD__, 'data', $data); } return($data); } public function getEvent($driver, $database, $event){ $this->debug(__METHOD__, 'driver', $driver); $this->debug(__METHOD__, 'database', $database); $this->debug(__METHOD__, 'event', $event); //$sql = "SHOW EVENTS"; //$sql = "SHOW EVENTS FROM `$database`"; $sql = "SHOW EVENTS FROM `$database` WHERE `Name` = '$event'"; $this->debug(__METHOD__, 'sql', $sql); $data = $this->DBO->query($sql); $this->debug(__METHOD__, 'data', $data); return($data); } public function getEvents($driver, $database){ if ($driver == 'sqlite') { $data = array(); } else { $this->debug(__METHOD__, 'driver', $driver); $this->debug(__METHOD__, 'database', $database); //$sql = "SHOW EVENTS FROM `$database`"; $sql = "SHOW EVENTS"; $this->debug(__METHOD__, 'sql', $sql); $data = $this->DBO->query($sql); $this->debug(__METHOD__, 'data', $data); } return($data); } public function getRoutine($driver, $database, $routine){ $this->debug(__METHOD__, 'driver', $driver); $this->debug(__METHOD__, 'database', $database); $this->debug(__METHOD__, 'routine', $routine); $sql = "SHOW PROCEDURE STATUS WHERE Db = '$database' AND Name = '$routine';"; $this->debug(__METHOD__, 'sql', $sql); $data = $this->DBO->query($sql); $this->debug(__METHOD__, 'data', $data); return($data); } public function getRoutines($driver, $database){ if ($driver == 'sqlite') { $data = array(); } else { $this->debug(__METHOD__, 'driver', $driver); $this->debug(__METHOD__, 'database', $database); //$sql = "SHOW PROCEDURE STATUS FROM `$database`"; $sql = "SHOW PROCEDURE STATUS WHERE Db = '$database';"; $this->debug(__METHOD__, 'sql', $sql); $data = $this->DBO->query($sql); $this->debug(__METHOD__, 'data', $data); } return($data); } public function getView($driver, $database, $view){ $this->debug(__METHOD__, 'driver', $driver); $this->debug(__METHOD__, 'database', $database); $this->debug(__METHOD__, 'view', $view); if ($driver == 'sqlite') { //$sql = "SELECT * FROM sqlite_master WHERE `type` = 'view' AND name NOT LIKE 'sqlite_%'"; $sql = "SELECT * FROM sqlite_master WHERE `type` = 'view'"; $this->debug(__METHOD__, 'sql', $sql); $data = $this->DBO->query($sql); $this->debug(__METHOD__, 'data', $data); $record = array(); foreach($data as $rec) { //if ($rec['name'] == 'sqlite_sequence') continue; $record['view'] = $rec['name']; $record['sql'] = $rec['sql']; } } else { /* $sql = "SELECT * FROM information_schema.views"; $sql.= " WHERE TABLE_SCHEMA = '$database'"; $sql.= " AND TABLE_NAME = '$view'"; */ $sql = "SHOW CREATE TABLE `$database`.`$view`"; $this->debug(__METHOD__, 'sql', $sql); $data = $this->DBO->query($sql); $this->debug(__METHOD__, 'data', $data); $rec = $data[0]; $record = array(); $record['view'] = !empty($rec['View']) ? $rec['View'] : ''; $record['sql'] = !empty($rec['Create View']) ? $rec['Create View'] : ''; } $this->debug(__METHOD__, 'record', $record); return($record); } public function getViews($driver, $database){ $this->debug(__METHOD__, 'driver', $driver); $this->debug(__METHOD__, 'database', $database); if ($driver == 'sqlite') { $sql = "SELECT * FROM sqlite_master WHERE `type` = 'view'"; $this->sql($sql); $this->debug(__METHOD__, 'sql', $sql); $data = $this->DBO->query($sql); $this->debug(__METHOD__, 'data', $data); $views = array(); foreach($data as $rec){ if ($rec['name'] == 'sqlite_sequence') continue; $view = array(); $view['view'] = $rec['name']; $view['sql'] = $rec['sql']; $views[] = $view; } } else { //$sql = "SHOW FULL TABLES WHERE Table_Type LIKE 'VIEW';"; $sql = "SELECT * FROM information_schema.tables"; $sql.= " WHERE TABLE_TYPE = 'VIEW'"; $sql.= " AND TABLE_SCHEMA != 'sys'"; if (!empty($database)) { $sql.= " AND TABLE_SCHEMA = '$database'"; } $sql.= " ORDER BY TABLE_NAME ASC"; $this->debug(__METHOD__, 'sql', $sql); $data = $this->DBO->query($sql); $this->debug(__METHOD__, 'data', $data); $views = array(); foreach($data as $rec){ $view = array(); $view['view'] = $rec['TABLE_NAME']; //$view['database'] = $rec['TABLE_SCHEMA']; $view['created'] = $rec['CREATE_TIME']; $view['comment'] = $rec['TABLE_COMMENT']; $views[] = $view; } } $this->debug(__METHOD__, 'views', $views); return($views); } public function createDatabase($database){ $this->connect(); $sql = sprintf("CREATE DATABASE `%s`;", $database); $this->sql($sql); $status = $this->DBO->exec($sql); return($status); } public function copyDatabase($database1, $database2){ $this->debug(__METHOD__, 'database1', $database1); $this->debug(__METHOD__, 'database2', $database2); $tables = $this->getTables($this->driver, $database1); $this->debug(__METHOD__, 'tables', $tables); foreach($tables as $table){ $sql = sprintf("CREATE TABLE %s.%s AS SELECT * FROM %s.%s;", $database2, $table['table'], $database1, $table['table']); $this->sql($sql); $status = $this->DBO->exec($sql); } return(true); } public function cloneDatabase($database1, $database2){ $tables = $this->getTables($this->driver, $database1); foreach($tables as $table){ $sql = sprintf("CREATE TABLE %s.%s LIKE %s.%s;", $database2, $table['table'], $database1, $table['table']); $this->sql($sql); $status = $this->DBO->exec($sql); } return(true); } public function deleteDatabase($database1){ $this->connect(); $sql = sprintf("DROP DATABASE `%s`;", $database1); $this->sql($sql); $status = $this->DBO->exec($sql); return($status); } public function renameDatabase($database1, $database2){ $this->connect(); $sql = sprintf("CREATE DATABASE `%s`;", $database2); $this->debug(__METHOD__, 'sql', $sql); $this->sql($sql); $status = $this->DBO->exec($sql); $this->debug(__METHOD__, 'status', $status); $sql = sprintf("SHOW DATABASES LIKE '%s';", $database2); //$this->sql($sql); $data = $this->DBO->query($sql); if (!count($data)) { $status = "Failed to create new database!"; //$this->error($status); return($status); } $tables = $this->getTables($this->driver, $database1); foreach($tables as $table){ $sql = sprintf("RENAME TABLE %s.%s TO %s.%s;", $database1, $table['table'], $database2, $table['table']); $this->sql($sql); $status = $this->DBO->exec($sql); if (is_string($status)) { return($status); } } $sql = sprintf("DROP DATABASE `%s`;", $database1); $this->sql($sql); $status = $this->DBO->exec($sql); return($status); } /* CREATE EVENT IF NOT EXISTS test_event_01 ON SCHEDULE AT CURRENT_TIMESTAMP DO INSERT INTO messages(message,created) VALUES('Test MySQL Event 1',NOW()); */ public function createEvent($driver, $database, $event){ $this->debug(__METHOD__, 'driver', $driver); $this->debug(__METHOD__, 'database', $database); $this->debug(__METHOD__, 'event', $event); if ($this->driver == 'sqlite'){ //$sql = sprintf("CREATE INDEX `%s` ON %s (`%s`);", $index, $table, $column); } else { $sql = sprintf("CREATE EVENT `%s` ON SCHEDULE AT '%s' DO %s;", $event['name'], $event['starts'], $event['definition']); } $this->debug(__METHOD__, 'sql', $sql); $status = $this->DBO->exec($sql); $this->debug(__METHOD__, 'status', $status); return($status); } public function deleteEvent($database, $event){ $sql = sprintf("DROP EVENT `%s`;", $event['name']); $this->debug(__METHOD__, 'sql', $sql); $status = $this->DBO->exec($sql); return($status); } public function renameEvent($database, $event1, $event2){ $status = $this->createEvent($database, $event2); if (is_string($status)) return($status); $status = $this->deleteEvent($database, $event1); return($status); } public function createTable($table){ $sql = sprintf("CREATE TABLE `%s` (`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,`name` VARCHAR(80) NOT NULL,`date` DATE);", $table); $this->sql($sql); $status = $this->DBO->exec($sql); return($status); } public function copyTable($table, $table2){ $sql = sprintf("CREATE TABLE `%s` AS SELECT * FROM `%s`;", $table2, $table); $this->sql($sql); $status = $this->DBO->exec($sql); return(true); } public function cloneTable($table, $table2){ $sql = sprintf("CREATE TABLE `%s` LIKE `%s`;", $table2, $table); $this->sql($sql); $status = $this->DBO->exec($sql); return(true); } public function deleteTable($table){ $sql = sprintf("DROP TABLE `%s`;", $table); $this->debug(__METHOD__, 'sql', $sql); $this->sql($sql); $status = $this->DBO->exec($sql); $this->debug(__METHOD__, 'status', $status); return($status); } public function emptyTable($table){ $sql = sprintf("TRUNCATE TABLE `%s`;", $table); $this->sql($sql); $status = $this->DBO->exec($sql); return($status); } public function renameTable($table, $table2){ $sql = sprintf("RENAME TABLE `%s` TO `%s`;", $table, $table2); $this->sql($sql); $status = $this->DBO->exec($sql); return($status); } //CREATE TRIGGER `before_employee_update` BEFORE UPDATE ON `employees` FOR EACH ROW INSERT INTO public function createTrigger($database, $trigger){ $this->debug(__METHOD__, 'database', $database); $this->debug(__METHOD__, 'trigger', $trigger); if ($this->driver == 'sqlite'){ //$sql = sprintf("CREATE INDEX `%s` ON %s (`%s`);", $index, $table, $column); } else { $sql = sprintf("CREATE TRIGGER `%s` %s %s ON `%s` FOR EACH ROW %s;", $trigger['name'], $trigger['timing'], $trigger['event'], $trigger['table'], $trigger['definition']); } $this->debug(__METHOD__, 'sql', $sql); $status = $this->DBO->exec($sql); return($status); } public function deleteTrigger($database, $trigger){ $sql = sprintf("DROP TRIGGER `%s`;", $trigger['name']); $this->debug(__METHOD__, 'sql', $sql); $status = $this->DBO->exec($sql); return($status); } public function renameTrigger($database, $trigger1, $trigger2){ $status = $this->createTrigger($database, $trigger2); if (is_string($status)) return($status); $status = $this->deleteTrigger($database, $trigger1); return($status); } public function createView($view, $select){ $this->debug(__METHOD__, 'view', $view); $sql = sprintf("CREATE"); if ($this->driver == 'mysql') { $sql.= sprintf(" ALGORITHM=UNDEFINED DEFINER=`ronludwig`@`%%` SQL SECURITY DEFINER"); } $sql.= sprintf(" VIEW `%s` AS", $view); $sql.= sprintf(" %s;", $select); $this->debug(__METHOD__, 'sql', $sql); $status = $this->DBO->exec($sql); $this->debug(__METHOD__, 'status', $status); return($status); } public function deleteView($view1){ $this->debug(__METHOD__, 'view1', $view1); $sql = sprintf("DROP VIEW `%s`;", $view1); $this->debug(__METHOD__, 'sql', $sql); $status = $this->DBO->exec($sql); $this->debug(__METHOD__, 'status', $status); return($status); } public function renameView($view1, $view2){ $this->debug(__METHOD__, 'view1', $view1); $this->debug(__METHOD__, 'view2', $view2); if ($this->driver == 'sqlite') { $record = $this->getView($this->driver, $this->database, $view1); $this->debug(__METHOD__, 'RECORD', $record); $sql = $record['sql']; $sql = str_replace($view1, $view2, $sql); $status = $this->createView($view2, $sql); $this->debug(__METHOD__, 'createView_status', $status); if (!is_string($status)) { $status = $this->deleteView($view1); $this->debug(__METHOD__, 'deleteView_status', $status); } } else { $sql = sprintf("RENAME TABLE `%s` TO `%s`;", $view1, $view2); $this->debug(__METHOD__, 'sql', $sql); $status = $this->DBO->exec($sql); $this->debug(__METHOD__, 'status', $status); } return($status); } public function importFile($database){ $this->connect($database); //printf('
DATABASE = %s
'."\n", print_r($database)); //printf('
FILES = %s
'."\n", print_r($_FILES)); $filename = $_FILES["file"]["name"]; $sql = file_get_contents($_FILES["file"]["tmp_name"]); //printf('
SQL = %s
'."\n", print_r($sql)); $status = $this->DBO->exec($sql); //printf('
STATUS = %s
'."\n", print_r($status)); return($status); } public function exportFile($dbhost, $database, $table=null){ //$output = fopen("php://output", "w"); /* printf('
DBHOST   = %s
'."\n", print_r($dbhost)); printf('
DATABASE = %s
'."\n", print_r($database)); printf('
TABLE    = %s
'."\n", print_r($table)); exit; */ $date = date('Y-m-d'); if (!empty($table)) { $filename = sprintf('%s-%s.sql', $table, $date); } else { $filename = sprintf('%s-%s.sql', $database, $date); } header('Content-Type: text/plain; charset=utf-8'); header('Content-Disposition: attachment; filename='.$filename); header('Pragma: no-cache'); header('Expires: 0'); header('Content-Transfer-Encoding: binary'); header('Cache-Control: must-revalidate'); printf('-- DBone SQL Export'."\n"); printf('-- Version: 2.0.1'."\n"); printf('-- Website: http://www.DBone.us'."\n"); printf('--'."\n"); printf('-- Host: %s'."\n", $dbhost); printf('-- Date: %s'."\n", date('Y-m-d')); printf('-- Time: %s'."\n", date('g:i:s')); $this->connect($database); $sql = "SELECT VERSION()"; $data = $this->DBO->query($sql); $server_version = $data[0]['VERSION()']; printf('-- Server version: %s'."\n", $server_version); printf('-- PHP version: %s'."\n", phpversion()); printf("\n"); printf('--'."\n"); printf('-- Database: %s'."\n", $database); printf('--'."\n"); printf("\n"); if (!empty($table)) { $tables = array($table); } else { $tables = $this->getTables($this->driver, $database); } foreach($tables as $table){ $table = $table['table']; printf('--'."\n"); printf('-- Export structure for table: %s'."\n", $table); printf('--'."\n"); printf("\n"); $sql = "SHOW CREATE TABLE `$table`"; $data = $this->DBO->query($sql); $stmt = !empty($data[0]['Create Table']) ? $data[0]['Create Table'] : ''; $stmt = str_replace('CREATE TABLE', 'CREATE TABLE IF NOT EXISTS', $stmt); printf('%s;'."\n", $stmt); printf("\n"); printf('--'."\n"); printf('-- Export data for table: %s'."\n", $table); printf('--'."\n"); printf("\n"); //$fields = $this->getFields($database, $table); //$columns = $this->getColumns($database, $table); //printf('
COLUMNS = %s
'."\n", print_r($columns)); $data = $this->getData($database, $table); $columns = array_keys($data[0]); $fields = "`".implode("`,`", $columns)."`"; foreach($data as $rec){ foreach($rec as $key => $val){ $rec[$key] = addslashes($val); } $values = "'".implode("','", $rec)."'"; printf('INSERT INTO `%s` (%s) VALUES (%s);'."\n", $table, $fields, $values); } } //fclose($output); return(true); } public function addColumn($database, $table, $column, $type, $oldcol){ $sql = sprintf("ALTER TABLE `%s` ADD COLUMN `%s` %s AFTER `%s`;", $table, $column, $type, $oldcol); $this->sql($sql); $status = $this->DBO->exec($sql); return($status); } public function moveColumn($database, $table, $column, $type, $where){ $sql = sprintf("ALTER TABLE `%s` CHANGE COLUMN `%s` `%s` %s %s;", $table, $column, $column, $type, $where); $this->sql($sql); $status = $this->DBO->exec($sql); return($status); } public function changeColumn($database, $table, $column, $type, $oldcol, $null, $key, $default, $extra){ $sql = sprintf("ALTER TABLE `%s` CHANGE COLUMN `%s` `%s` %s", $table, $oldcol, $column, $type); if ($null == 'NO') { $sql.= sprintf(" NOT NULL"); } if (!empty($default)) { $sql.= sprintf(" DEFAULT %s", $default); } $sql.= ';'; $this->sql($sql); $status = $this->DBO->exec($sql); return($status); } public function dropColumn($database, $table, $column){ $sql = sprintf("ALTER TABLE `%s` DROP COLUMN `%s`;", $table, $column); $this->debug(__METHOD__, 'sql', $sql); $this->sql($sql); $status = $this->DBO->exec($sql); $this->debug(__METHOD__, 'status', $status); return($status); } public function modifyColumn($database, $table, $column, $type){ $sql = sprintf("ALTER TABLE `%s` MODIFY COLUMN `%s` %s;", $table, $column, $type); $this->sql($sql); $status = $this->DBO->exec($sql); return($status); } public function renameColumn($database, $table, $column1, $column2){ $sql = sprintf("ALTER TABLE `%s` RENAME COLUMN `%s` TO `%s`;", $table, $column1, $column2); $this->debug(__METHOD__, 'sql', $sql); $this->sql($sql); $status = $this->DBO->exec($sql); $this->debug(__METHOD__, 'status', $status); return($status); } //ALTER TABLE `links` ADD UNIQUE(`title`); //alter table Persion add primary key (persionId,Pname,PMID) public function createIndex($database, $table, $column, $type){ if ($this->driver == 'sqlite'){ $sql = sprintf("CREATE INDEX `%s` ON %s (`%s`);", $index, $table, $column); } else { $sql = sprintf("ALTER TABLE `%s` ADD %s (`%s`);", $table, $type, $column); } $status = $this->DBO->exec($sql); return($status); } //ALTER TABLE `links` DROP INDEX `title`; public function deleteIndex($database, $table, $column){ $sql = sprintf("ALTER TABLE `%s` DROP INDEX `%s`;", $table, $column); $this->sql($sql); $status = $this->DBO->exec($sql); return($status); } //ALTER TABLE `links` DROP INDEX `title`; public function renameIndex($database, $table, $column, $column2){ $sql = sprintf("ALTER TABLE `%s` RENAME INDEX `%s` TO `%s`;", $table, $column, $column2); $this->sql($sql); $status = $this->DBO->exec($sql); return($status); } public function deleteRecord($database, $table, $oldrec){ $conditions = array(); foreach ($oldrec as $key => $val) { $conditions[] = sprintf("`%s` = '%s'", $key, @addslashes($val)); } $condition = implode(' AND ', $conditions); $sql = sprintf("DELETE FROM `%s` WHERE %s;", $table, $condition); $this->sql($sql); $status = $this->DBO->exec($sql); return($status); } public function insertRecord($database, $table, $record){ $rec = array(); foreach($record as $key => $val) { if (!empty($val)) { $rec[$key] = addslashes($val); } } $fields = sprintf("`%s`", implode("`,`", array_keys($rec))); $values = sprintf("'%s'", implode("','", array_values($rec))); $sql = sprintf("INSERT INTO `%s` (%s) VALUES (%s);", $table, $fields, $values); $this->sql($sql); $status = $this->DBO->exec($sql); return($status); } public function updateRecord($database, $table, $record, $record2){ $sets = array(); foreach ($record2 as $key => $val) { if ($record2[$key] !== $record[$key]) { $sets[] = sprintf("`%s` = '%s'", $key, addslashes($val)); } } if (empty($sets)) { return(0); } $set = implode(',', $sets); $conditions = array(); foreach ($record as $key => $val) { $conditions[] = sprintf("`%s` = '%s'", $key, @addslashes($val)); } $condition = implode(' AND ', $conditions); $sql = sprintf("UPDATE `%s` SET %s WHERE %s;", $table, $set, $condition); $this->sql($sql); $status = $this->DBO->exec($sql); return($status); } public function updateRecord3($database, $table, $record, $oldrec){ $i=0; $sets = array(); foreach($record as $key => $val) { if ($i == 0) { $field = $key; $value = $val; } if (empty($val)) { //$sets[] = sprintf("`%s` = NULL", $key); } else { $sets[] = sprintf("`%s` = '%s'", $key, addslashes($val)); } $i++; } $set = implode(',', $sets); $conditions = array(); foreach ($record as $key => $val) { if (empty($val)) { //$conditions[] = sprintf("`%s` = NULL", $key); } else { $conditions[] = sprintf("`%s` = '%s'", $key, @addslashes($val)); } } $condition = implode(' AND ', $conditions); $sql = sprintf("UPDATE `%s` SET %s WHERE `%s` = '%s';", $table, $set, $field, $value); $this->sql($sql); $affected = $this->DBO->exec($sql); return($affected); } public function updateRecord2($database, $table, $record){ $old_record = $this->row($database, $table, $row); $old_record = $old_record[0]; $i=0; $sets = array(); foreach($new_record as $key => $val) { if ($i == 0) { $field = $key; $value = $val; } if (empty($val)) { //$sets[] = sprintf("`%s` = NULL", $key); } else { $sets[] = sprintf("`%s` = '%s'", $key, addslashes($val)); } $i++; } $set = implode(',', $sets); $conditions = array(); foreach ($old_record as $key => $val) { if (empty($val)) { //$conditions[] = sprintf("`%s` = NULL", $key); } else { $conditions[] = sprintf("`%s` = '%s'", $key, @addslashes($val)); } } $condition = implode(' AND ', $conditions); $sql = sprintf("UPDATE `%s` SET %s WHERE %s;", $table, $set, $condition); $this->sql($sql); $affected = $this->DBO->exec($sql); return($affected); } public function isConnected(){ if (!empty($this->DBO)){ return(true); } return(false); } public function isLoggedIn(){ $driver = !empty($_SESSION['driver']) ? $_SESSION['driver'] : ''; $dbpass = !empty($_SESSION['dbpass']) ? $_SESSION['dbpass'] : ''; if ($driver == 'sqlite'){ return(true); } if (!empty($dbpass)){ return(true); } return(false); } public function isDownloadPage(){ $downloads = array('csv','export'); //printf('
PAGE = %s
'."\n", $this->page);exit(); if (in_array($this->page, $downloads)) { return(true); } return(false); } public function isDatabaseSet(){ if (!empty($_SESSION['database'])){ return(true); } return(false); } public function isTableSet(){ if (!empty($_SESSION['table'])){ return(true); } return(false); } public function goback(){ printf(''."\n"); } public function redirect($page, $database='', $table=''){ //printf('
    PAGE = %s
'."\n", print_r($page)); //printf('
DATABASE = %s
'."\n", print_r($database)); //printf('
   TABLE = %s
'."\n", print_r($table)); if (!empty($table)) { $url = sprintf("./?page=%s&database=%s&table=%s", $page, $database, $table); } else if (!empty($database)) { //$url = sprintf("./?page=tables&database=%s", $database); $url = sprintf("./?page=%s&database=%s", $page, $database); } else { $url = sprintf("./?page=databases"); //$url = sprintf("./?page=login"); } //printf('
URL = %s
'."\n", print_r($url)); //header('Location: '.$url); echo ""; } public function redirect3($database, $table){ //printf('
DATABASE = %s
'."\n", print_r($database)); //printf('
   TABLE = %s
'."\n", print_r($table)); if (!empty($table)) { $url = sprintf("./?page=records&database=%s&table=%s", $database, $table); } else if (!empty($database)) { $url = sprintf("./?page=tables&database=%s", $database); } else { $url = sprintf("./?page=databases"); } //printf('
URL = %s
'."\n", print_r($url)); //header('Location: '.$url); echo ""; } public function redirect2($url){ header('Location: '.$url); } public function debug($method, $name, $value, $mode=null) { if (!empty($this->debug) || !empty($mode)) { printf('
%s.%s = %s
'."\n", $method, $name, print_r($value, 1)); } } public function raw($rows) { printf('
'."\n");
		foreach($rows as $row){
			printf("%s\n", print_r($row));
		}
		printf('
'."\n"); } public function showDatabases($data, $order_by, $order) { //$this->debug(__METHOD__, 'data', $data,1); //$this->debug(__METHOD__, '$order_by', $order_by,1); //$this->debug(__METHOD__, '$order', $order,1); if (!is_array($data) || count($data) == 0) { printf('

Host has no databases!

'."\n"); return(false); } $row=0; $total=0; printf('
'."\n"); printf(''."\n"); printf(''."\n"); printf(''."\n"); printf(''."\n"); $order1 = $order_by == 'database' && $order != 'DESC' ? 'DESC' : 'ASC'; printf(''."\n", $order1); $order2 = $order_by == 'tables' && $order != 'DESC' ? 'DESC' : 'ASC'; printf(''."\n", $order2); printf(''."\n"); printf(''."\n"); printf(''."\n"); foreach ($data as $rec) { $row++; printf(''."\n", $rec['database']); printf(''."\n", $row); printf(''."\n", $rec['database']); printf(''."\n", $rec['tables']); printf(''."\n"); $total += intval($rec['tables']); } printf(''."\n"); printf(''."\n"); printf(''."\n"); printf(''."\n"); printf(''."\n", $total); printf(''."\n"); printf(''."\n"); printf('
#DATABASETABLES
%s%s%s
T O T A L%s
'."\n"); printf('
'."\n"); } public function showTables($database, $data, $order_by, $order) { //$this->debug(__METHOD__, 'data', $data,1); //$this->debug(__METHOD__, '$order_by', $order_by,1); //$this->debug(__METHOD__, '$order', $order,1); if (!is_array($data) || count($data) == 0) { printf('

Database has no tables!

'."\n"); return(false); } $row=0; $total=0; printf('
'."\n"); printf(''."\n"); printf(''."\n"); printf(''."\n"); printf(''."\n"); $order1 = $order_by == 'table' && $order != 'DESC' ? 'DESC' : 'ASC'; printf(''."\n", $database, $order1); $order2 = $order_by == 'rows' && $order != 'DESC' ? 'DESC' : 'ASC'; printf(''."\n", $database, $order2); printf(''."\n"); printf(''."\n"); printf(''."\n"); foreach ($data as $rec) { $row++; printf(''."\n", $database, $rec['table']); printf(''."\n", $row); printf(''."\n", $rec['table']); printf(''."\n", $rec['rows']); printf(''."\n"); $total += intval($rec['rows']); } printf(''."\n"); printf(''."\n"); printf(''."\n"); printf(''."\n"); printf(''."\n", $total); printf(''."\n"); printf(''."\n"); printf('
#TABLESROWS
%s%s%s
T O T A L%s
'."\n"); printf('
'."\n"); } public function showData($data, $page, $next, $column, $columns) { $this->debug(__METHOD__, 'data', $data,1); $this->debug(__METHOD__, 'page', $page,1); $this->debug(__METHOD__, 'next', $next,1); $this->debug(__METHOD__, 'column', $column,1); $this->debug(__METHOD__, 'columns', $columns,1); if (!is_array($data)) return(false); $col=0; $row=0; $totals=array(); //printf('
'."\n"); printf(''."\n"); foreach ($data as $rec) { if ($row == 0){ printf(''."\n"); printf(''."\n"); printf(''."\n"); foreach ($rec as $key => $val) { $width = $col == 0 ? ' width="100%"' : ''; $col++; printf('%s'."\n", $width, $page, $key, ($key)); $totals[$key] = 0; } printf(''."\n"); printf(''."\n"); printf(''."\n"); } $row++; $col=0; foreach ($rec as $key => $val) { if($col == 0){ //printf(''."\n", $next, $this->database, $this->table, $column, $val); printf(''."\n", $next, $this->database, $this->table, $column, $val); printf(''."\n", $row); } $col++; if (in_array($key, $columns)) { $totals[$key] += intval($val); printf(''."\n", $val); } else { printf(''."\n", $val); } } printf(''."\n"); } if (!empty($rec)) { printf(''."\n"); printf(''."\n"); printf(''."\n"); //printf(''."\n"); foreach ($rec as $key => $val) { if (in_array($key, $columns) && $totals[$key] > 0) { printf(''."\n"); } } printf(''."\n"); printf(''."\n"); } printf(''."\n"); printf('
#
%s%s%s
 T O T A L%s'."\n", $totals[$key]); } else { printf(' 
'."\n"); //printf('
'."\n"); //printf("
TOTALS = %s
\n",print_r($totals)); } public function show($rows) { //printf("
COUNT = %s
\n",print_r(count($rows))); //printf("
SHOW ROWS = %s
\n",print_r($rows)); $this->debug(__METHOD__, 'rows', $rows); if(!isset($rows[0])){ $recs = array(); //$recs[] = array('key' => 'value'); foreach($rows as $key => $val){ $recs[] = array('key' => $key, 'value' => $val); } $rows = $recs; } //printf("
NEW ROWS = %s
\n",print_r($rows)); //$rows = empty($rows[0]) ? array($rows) : $rows; $bolds = array('bday','ddate','mdate'); $centers = array('age','bdate','bday','byear','lived','mday','mobile','myear','myears','state','year','years'); $rights = array(); $n=0; //printf(''."\n"); printf('
'."\n"); foreach ($rows as $row) { //printf("
%s
\n",print_r($row)); if ($n == 0){ printf("\n"); printf("\n"); foreach ($row as $key => $val) { printf("\n", strtoupper($key)); } printf("\n"); } $n++; printf("\n"); printf("\n", $n); foreach ($row as $key => $val) { printf("\n", print_r($val)); /* if (in_array($key, $bolds)){ printf(''."\n", $val); } else if (in_array($key, $centers)){ printf(''."\n", $val); } else if (in_array($key, $rights)){ printf(''."\n", $val); } else { printf("\n", print_r($val)); } */ } printf("\n"); } printf("
#%s
%s%s%s%s%s%s
\n"); } public function showMenu($items, $page, $database=null) { //printf("
%s
\n",print_r($row)); printf(''."\n"); printf(''."\n"); printf(''."\n"); printf(''."\n"); foreach ($items as $item) { $class = strtolower($item) == strtolower($page) ? 'on' : 'off'; printf(''."\n", $class, $item, $database, strtoupper($item)); } printf(''."\n"); printf(''."\n"); printf('
%s
'."\n"); printf('
'."\n"); } public function showDatabaseMenu($page) { $items = array('database','views','routines','events','triggers'); $this->showMenu($items, $page); } public function showTableMenu($page, $database) { $items = array('table','views'); $this->showMenu($items, $page, $database); } public function showResults($data){ //$this->debug(__METHOD__, 'data', $data); printf(''."\n"); $n=0; foreach ($data as $rec) { if ($n == 0){ printf("\n"); printf("\n"); foreach ($rec as $key => $val) { printf("\n", strtoupper($key)); } printf("\n"); } $n++; printf("\n"); printf("\n", $n); foreach ($rec as $key => $val) { printf("\n", print_r($val, 1)); } printf("\n"); } printf("
#%s
%s%s
\n"); } } class Login extends DBone { public $cmd, $driver, $dbhost, $dbport, $dbchar; public function controller(){ $this->cmd = !empty($_REQUEST['cmd']) ? $_REQUEST['cmd'] : ''; $this->driver = !empty($_REQUEST['driver']) ? $_REQUEST['driver'] : 'mysql'; $this->dbhost = !empty($_REQUEST['dbhost']) ? $_REQUEST['dbhost'] : 'localhost'; $this->dbport = !empty($_REQUEST['dbport']) ? $_REQUEST['dbport'] : '3306'; $this->dbchar = !empty($_REQUEST['dbchar']) ? $_REQUEST['dbchar'] : 'utf8mb4'; $this->dbuser = !empty($_REQUEST['dbuser']) ? $_REQUEST['dbuser'] : ''; $this->dbpass = !empty($_REQUEST['dbpass']) ? $_REQUEST['dbpass'] : ''; $this->dbname = !empty($_REQUEST['dbname']) ? $_REQUEST['dbname'] : ''; $this->dbpath = !empty($_REQUEST['dbpath']) ? $_REQUEST['dbpath'] : '../sqlite'; $this->dbfile = !empty($_REQUEST['dbfile']) ? $_REQUEST['dbfile'] : 'text.db'; if ($this->cmd == 'login') { $dbo = new $this->driver(); $dba = array( 'driver' => $this->driver, 'dbhost' => $this->dbhost, 'dbport' => $this->dbport, 'dbchar' => $this->dbchar, 'dbuser' => $this->dbuser, 'dbpass' => $this->dbpass, 'dbname' => $this->dbname, 'dbpath' => $this->dbpath, 'dbfile' => $this->dbfile, ); $status = $dbo->connect($dba); $this->debug(__METHOD__, 'status', $status, 1); if ($status === true) { $this->getRequest(); $_SESSION['driver'] = !empty($this->driver) ? $this->driver : ''; $_SESSION['dbpath'] = !empty($this->dbpath) ? $this->dbpath : ''; $_SESSION['dbhost'] = !empty($this->dbhost) ? $this->dbhost : ''; $_SESSION['dbport'] = !empty($this->dbport) ? $this->dbport : ''; $_SESSION['charset'] = !empty($this->charset) ? $this->charset : ''; $_SESSION['dbuser'] = !empty($this->dbuser) ? $this->dbuser : ''; $_SESSION['dbpass'] = !empty($this->dbpass) ? $this->dbpass : ''; $_SESSION['database'] = !empty($this->database) ? $this->database : ''; $_SESSION['table'] = !empty($this->table) ? $this->table : ''; $_SESSION['available'] = !empty($this->available) ? $this->available: ''; $_SESSION['forbidden'] = !empty($this->forbidden) ? $this->forbidden: ''; $page = !empty($this->page) ? $this->page : 'databases'; $query = !empty($this->query) ? $this->query : ''; $database = !empty($this->database) ? $this->database : ''; $table = !empty($this->table) ? $this->table : ''; //printf('
PAGE     = %s
'."\n", print_r($page)); //printf('
DATABASE = %s
'."\n", print_r($database)); //printf('
TABLE    = %s
'."\n", print_r($table)); //exit(); $this->redirect($page, $database, $table); } else { printf('
ERROR2: %s
'."\n", print_r($status)); //session_destroy(); } } else { //parent::controller(); //$this->getRequest(); //$this->connect($this->database); //$this->model(); } } public function model(){ $this->drivers = $this->getDrivers(); } public function view(){ $this->showHead(); printf('
'."\n"); printf('

L O G I N

'."\n"); printf('
'."\n"); printf(''."\n"); printf(''."\n"); printf(''."\n"); printf(''."\n"); printf(''."\n"); if ($this->driver == 'sqlite') { $this->dbpath = !empty($this->dbpath) ? $this->dbpath : ''; printf(''."\n"); printf(''."\n"); printf(''."\n", $this->dbpath); printf(''."\n"); printf(''."\n"); printf(''."\n"); printf(''."\n", $this->dbfile); printf(''."\n"); } else { printf(''."\n"); printf(''."\n"); printf(''."\n", $this->dbhost); printf(''."\n"); printf(''."\n"); printf(''."\n"); printf(''."\n", $this->dbport); printf(''."\n"); printf(''."\n"); printf(''."\n"); printf(''."\n", $this->dbchar); printf(''."\n"); printf(''."\n"); printf(''."\n"); printf(''."\n", $this->dbuser); printf(''."\n"); printf(''."\n"); printf(''."\n"); printf(''."\n", $this->dbpass); printf(''."\n"); printf(''."\n"); printf(''."\n"); printf(''."\n", $this->dbname); printf(''."\n"); } printf(''."\n"); printf(''."\n"); printf(''."\n"); printf(''."\n"); printf(''."\n"); printf(''."\n"); printf('
'."\n"); printf(''."\n"); printf('
'."\n"); printf('
'."\n"); printf('
'."\n"); printf('
'."\n"); $this->showFoot(); } } class Logout extends DBone { public function controller(){ $this->getRequest(); } public function model(){ if (isset($_SESSION)) session_destroy(); # CALCULATE HOW LONG LOGGEDIN $accessed = ''; $this->data = array('Accessed' => $accessed); //printf("
DATA = %s
\n",print_r($data)); } public function view(){ $this->showHead(); $this->showStatus(); $n=0; printf('

%s

'."\n", 'Logout'); printf('

%s

'."\n", 'You are now Logged-Out!'); $this->showFoot(); } } class Objects extends DBone { public function controller(){ $this->debug(__METHOD__, 'REQUEST', $_REQUEST); $this->debug(__METHOD__, 'THIS', $this); } public function model(){ } public function view(){ $this->showHead(); $this->showStatus(); $tables = new Tables(); $tables->getRequest(); $tables->model(); $tables->view(); $views = new Views(); $views->getRequest(); $views->model(); $views->view(); if (class_exists('Triggers')) { $triggers = new Triggers(); $triggers->getRequest(); $triggers->model(); $triggers->view(); } if (class_exists('Events')) { $events = new Events(); $events->getRequest(); $events->model(); $events->view(); } if (class_exists('Routines')) { $routines = new Routines(); $routines->getRequest(); $routines->model(); $routines->view(); } $this->showFoot(); } } class Databases extends DBone { public $databases, $order_by, $order; public function controller(){ $this->getRequest(); } public function model(){ $this->databases = $this->getDatabases($this->order_by, $this->order); $this->debug(__METHOD__, 'databases1', $this->databases, 1); /* if ($this->driver == 'sqlite') { //printf('
DIR = %s
'."\n", print_r($this->dbpath)); $files = scandir($this->dbpath); //printf('
FILES = %s
'."\n", print_r($files)); $databases = array(); foreach($files as $file) { if (strpos($file, '.') == 0) continue; $database = substr($file, 0, strrpos($file, ".")); //if (in_array($database, $forbidden)) continue; $this->connect($database); $sql = "SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%'"; //printf('
SQL = %s
'."\n", print_r($sql)); $this->data = $this->DBO->query($sql); //printf('
DATA = %s
'."\n", print_r($this->data)); $tables = count($this->data); $rec['database'] = $database; $rec['tables'] = $tables; $databases[] = $rec; } $this->data = $databases; } else { $databases = $this->getDatabases(); $this->debug(__METHOD__, 'databases', $databases); $this->data = array(); foreach($databases as $database) { $databases = $this->getTables(); //if (in_array($database, $this->forbidden)) continue; //$this->connect($database); $sql = "SHOW TABLES FROM `$database`"; $this->debug(__METHOD__, 'SQL', $sql); $tables = $this->DBO->query($sql); $this->debug(__METHOD__, 'TABLES', $tables); exit; $this->data[] = array('database' => $database, 'tables' => count($tables)); } } */ } public function view(){ $next = 'tables'; $column = 'database'; $columns = array('tables'); $this->showHead(); $this->showStatus(); printf('

DATABASES

'."\n"); $this->showDatabases($this->databases, $this->order_by, $this->order); //$this->showData($this->data, $next, $column, $columns); $this->showFoot(); } } class Tables extends DBone { public $tables, $order_by, $order; public function controller(){ $this->getRequest(); } public function model(){ $this->data = array(); $this->connect($this->database); $this->tables = $this->getTables($this->driver, $this->database); //$this->debug(__METHOD__, 'tables', $this->tables, 1); //$data = $this->getData($this->driver, $this->database, $table); //$this->debug(__METHOD__, 'data', $data, 1); /* foreach($tables as $rec){ $table = $rec['table']; $rows = $this->getRows($this->driver, $this->database, $table); $this->debug(__METHOD__, 'rows', $rows, 1); $row['table'] = $table; $row['rows'] = $rows; $this->data[] = $row; } */ } public function view(){ $this->showHead(); $this->showStatus(); printf('

TABLES

'."\n", $this->database); $page = 'records'; $column = 'table'; $columns = array('rows'); $this->showTables($this->database, $this->tables, $this->order_by, $this->order); $this->showFoot(); } } class Views extends DBone { public function controller(){ $this->getRequest(); } public function model(){ $this->connect($this->database); $data = $this->getViews($this->driver, $this->database); foreach($data as $rec){ $view = $rec['view']; $rows = $this->getRows($this->driver, $this->database, $view); $row['view'] = $view; $row['rows'] = $rows; $this->data[] = $row; } } public function view(){ $this->showHead(); $this->showStatus(); printf('

VIEWS

'."\n", $this->database); $page = 'records'; $column = 'view'; $columns = array('rows'); $this->showData($this->data, $page, $column, $columns); $this->showFoot(); } } class Records extends DBone { public function controller(){ $this->getRequest(); } public function model(){ $this->connect($this->database); $table = !empty($this->table) ? $this->table : $this->view; $sql = "SELECT * FROM `$table`"; if (!empty($this->field)) { $sql.= " ORDER BY `$this->field` $this->order"; } $this->data = $this->DBO->query($sql); } public function view(){ $this->showHead(); $this->showStatus(); printf('

RECORDS

'."\n"); $row=0; printf(''."\n"); foreach ($this->data as $rec) { if ($row == 0){ printf(''."\n"); printf(''."\n"); printf(''."\n"); foreach ($rec as $key => $val) { printf(''."\n", $this->database, $this->table, $this->view, $key, strtoupper($key)); } printf(''."\n"); printf(''."\n"); printf(''."\n"); } $row++; $col=0; foreach ($rec as $key => $val) { if ($col == 0) { $page = 'records'; $primary = 'records'; $secondary = 'record'; $secondary = class_exists($secondary) ? $secondary : $primary; $page2 = $page == $primary ? $secondary : $primary; if (!empty($this->table)) { printf(''."\n", $page2, $this->database, $this->table, $key, $val); } else if (!empty($this->view)) { printf(''."\n", $page2, $this->database, $this->view, $key, $val); } else { printf(''."\n", $page2, $this->database); } printf(''."\n", $row); } $col++; printf(''."\n", $val); } printf(''."\n"); } printf(''."\n"); printf('
#%s
%s%s
'."\n"); $this->showFoot(); } } class Columns extends DBone { public function controller(){ $this->getRequest(); $this->model(); $this->showHead(); $this->showStatus(); $this->view(); $this->showFoot(); } public function model(){ $this->connect($this->database); $this->data = $this->getColumns($this->database, $this->table); $this->debug(__METHOD__, 'DATA', $this->data); } public function view(){ printf('

COLUMNS

'."\n", $this->database, $this->table); $page = 'column'; $column = 'column'; $columns = array(); $this->showData($this->data, $page, $column, $columns); } } class Indexes extends DBone { public function controller(){ $this->getRequest(); $this->model(); $this->showHead(); $this->showStatus(); $this->view(); $this->showFoot(); } public function model(){ $this->connect($this->database); $this->data = $this->getIndexes($this->database, $this->table); $this->debug(__METHOD__, 'DATA', $this->data); } public function view(){ printf('

INDEXES

'."\n", $this->database, $this->table); $n=0; $page = 'index'; $column = 'index'; $columns = array(); $this->showData($this->data, $page, $column, $columns); } } class Search extends DBone { public function controller(){ $this->getRequest(); $databases = array(); $tables = array(); if (!empty($this->database)) { $databases[] = $this->database; } else { $databases = $this->getDatabases(); } $this->debug(__METHOD__, 'DATABASES', $databases); foreach($databases as $database){ $this->debug(__METHOD__, 'DATABASE', $database); $this->connect($database); $this->db = $database; $tables = array(); if (!empty($this->table)) { $tables[] = $this->table; } else { $data = $this->getTables($this->driver, $database); foreach($data as $rec){ $tables[] = $rec['table']; } } $this->debug(__METHOD__, 'TABLES', $tables); if (empty($tables) || count($tables) == 0) continue; foreach($tables as $table){ $this->tb = $table; $this->debug(__METHOD__, 'DATABASE2', $database); $this->debug(__METHOD__, 'TABLE2', $table); $this->model(); $this->view(); $this->tb = ''; } } } public function model(){ //$sql = "SHOW COLUMNS FROM $this->table"; //$sql = "SHOW COLUMNS FROM $this->database.$this->table"; //$sql = "SHOW COLUMNS FROM $this->table FROM $this->database"; //$sql = "USE $this->database; SHOW COLUMNS FROM $this->table;"; //$this->connect($this->database); //printf("
CONNECT = %s
\n",print_r($this->database)); //printf("
TABLE = %s
\n",print_r($this->table)); if ($this->driver == 'sqlite') { //$sql = "SELECT * FROM sqlite_master WHERE tbl_name=`$this->table`"; //$sql = "SELECT * FROM sqlite_master"; $sql = "SELECT * FROM `$this->table` LIMIT 1"; //printf("
SQL = %s
\n",print_r($sql)); $data = $this->DBO->query($sql); //printf("
DATA = %s
\n",print_r($data)); if (count($data) == 0) return; $fields = array(); foreach($data[0] as $key => $val){ $fields[] = $key; } //printf("
FIELDS = %s
\n",print_r($fields)); } else { $this->debug(__METHOD__, 'DATABASE', $this->database); //$this->connect($this->database); //$this->table = $this->table['table']; $sql = "SHOW COLUMNS FROM `$this->tb`;"; $this->debug(__METHOD__, 'SQL', $sql); $data = $this->DBO->query($sql); $this->debug(__METHOD__, 'DATA', $data); if (is_string($data)) return; $fields = array(); foreach($data as $rec){ $fields[] = $rec['Field']; } } //printf("
FIELDS = %s
\n",print_r($fields)); //$tables = implode(',', $fields); //printf("
TABLES = %s
\n",print_r($tables)); //$sql = "SELECT * FROM $this->table"; //$sql.= " WHERE '%$this->query%' IN ($tables)"; $sql = "SELECT * FROM `$this->tb` WHERE "; $n=0; foreach($fields as $field){ if ($n !== 0) $sql.= " OR "; $n++; $sql.= " `$field` LIKE '%$this->query%'"; } $this->debug(__METHOD__, 'SQL', $sql); $this->data = $this->DBO->query($sql); $this->debug(__METHOD__, 'DATA', $this->data); } public function view(){ //printf("
DATABASE = %s
\n",print_r($database)); //printf("
TABLE    = %s
\n",print_r($table)); //printf('
SESSION  = %s
'."\n", print_r($_SESSION)); $n=0; $this->debug(__METHOD__, 'DATA', $this->data); if(empty($this->data) || count($this->data) == 0){ return(false); } printf('

%s.%s

'."\n", $this->db, $this->tb); printf(''."\n"); foreach ($this->data as $rec) { $this->debug(__METHOD__, 'REC', $rec); if ($n == 0){ printf(''."\n"); printf(''."\n"); printf(''."\n"); foreach ($rec as $key => $val) { printf(''."\n", strtoupper($key)); } printf(''."\n"); printf(''."\n"); printf(''."\n"); } $n++; $m=0; foreach ($rec as $key => $val) { if ($m == 0) { $page = class_exists('record') ? 'record' : 'records'; printf(''."\n", $page, $this->db, $this->tb, $key, $val, $n); printf(''."\n", $n); } printf(''."\n", print_r($val, 1)); $m++; } printf(''."\n"); } printf(''."\n"); printf('
#%s
%s%s
'."\n"); printf('

'."\n"); } } class Tools extends DBone { public function controller(){ $this->getRequest(); } public function model(){ //$this->data = array('Columns','Search','Session','Version','Logout'); $this->data = array('Extensions','PhpInfo','Server','Session','SQL','Versions','Logout'); //printf("
DATA = %s
\n",print_r($data)); } public function view(){ $this->showHead(); $this->showStatus(); $n=0; printf('

%s

'."\n", 'Tools'); printf(''."\n"); printf(''."\n"); printf(''."\n"); printf(''."\n"); printf(''."\n"); printf(''."\n"); printf('
'."\n"); printf(''."\n"); printf('
'."\n"); $this->showFoot(); } } class CSV extends DBone { public function controller(){ $this->driver = !empty($_SESSION['driver']) ? $_SESSION['driver'] : ''; $this->database = !empty($_SESSION['database']) ? $_SESSION['database'] : ''; $this->table = !empty($_SESSION['table']) ? $_SESSION['table'] : ''; $this->query = !empty($_REQUEST['query']) ? $_REQUEST['query'] : ''; //printf("
EXPORT DRIVER   = %s
\n",print_r($this->driver)); //printf("
EXPORT DATABASE = %s
\n",print_r($this->database)); //printf("
EXPORT TABLE    = %s
\n",print_r($this->table)); //printf("
EXPORT QUERY    = %s
\n",print_r($this->query)); $this->model(); $this->view(); ### REDIRECT USING JAVASACRIPT //$this->redirect('records', $this->database, $this->table); } public function model(){ $date = date('Y-m-d'); $this->filename = sprintf('%s-%s.csv', $this->table, $date); $this->fields = $this->getFields($this->database, $this->table); $this->data = $this->getData($this->database, $this->table); $sql = "SELECT * from `$this->table`"; //printf("
CSV SQL = %s
\n",print_r($sql)); $this->data = $this->DBO->query($sql); //printf("
CSV DATA = %s
\n",print_r($this->data)); } public function view(){ header('Content-Type: text/csv; charset=utf-8'); header('Content-Disposition: attachment; filename='.$this->filename); header('Pragma: no-cache'); header('Expires: 0'); header('Content-Transfer-Encoding: binary'); header('Cache-Control: must-revalidate'); $output = fopen("php://output", "w"); $header = $this->fields; fputcsv($output, $header); foreach($this->data as $data){ fputcsv($output, $data); } fclose($output); } } class Import extends DBone { public function controller(){ } public function model(){ } public function view(){ } } class Export extends DBone { public function controller(){ $this->driver = !empty($_SESSION['driver']) ? $_SESSION['driver'] : ''; $this->dbhost = !empty($_SESSION['dbhost']) ? $_SESSION['dbhost'] : ''; $this->database = !empty($_SESSION['database']) ? $_SESSION['database'] : ''; $this->table = !empty($_SESSION['table']) ? $_SESSION['table'] : ''; $this->query = !empty($_REQUEST['query']) ? $_REQUEST['query'] : ''; $this->dbhost_flag = 0; //printf("
EXPORT DRIVER   = %s
\n",print_r($this->driver)); //printf("
EXPORT DATABASE = %s
\n",print_r($this->database)); //printf("
EXPORT TABLE    = %s
\n",print_r($this->table)); //printf("
EXPORT QUERY    = %s
\n",print_r($this->query)); $date = date('Y-m-d'); if (!empty($this->table)) { $this->filename = sprintf('%s-%s.sql', $this->table, $date); } else if (!empty($this->database)) { $this->filename = sprintf('%s-%s.sql', $this->database, $date); } else { $this->filename = sprintf('%s-%s.sql', $this->dbhost, $date); } $databases = array(); if (!empty($_SESSION['database'])) { $databases[] = $_SESSION['database']; //printf("
DATABASES1 = %s
\n",print_r($databases)); } else { $this->connect(); $databases = $this->getDatabases(); //printf("
DATABASES2 = %s
\n",print_r($databases)); } //printf("
SEARCH DATABASES = %s
\n",print_r($databases)); $output = fopen("php://output", "w"); foreach($databases as $database){ $this->database_flag = 0; //printf("
DATABASE = %s
\n",print_r($database)); $this->connect($database); //printf("
CONNECT = %s
\n",print_r($this->database)); $this->database = $database; $tables = array(); if (!empty($_SESSION['table'])) { $tables[] = $_SESSION['table']; } else { $tables = $this->getTables($database, $this->driver); } //$tables = $this->getTables($database); //printf("
TABLES = %s
\n",print_r($tables)); if (count($tables) == 0) continue; //$this->database = $database; foreach($tables as $table){ //printf("
SEARCH TABLE = %s
\n",print_r($table)); $this->table = $table; $this->model(); $this->view(); } } fclose($output); } public function model(){ $this->fields = $this->getFields($this->database, $this->table); $this->data = $this->getData($this->database, $this->table); $sql = "SELECT * from `$this->table`"; //printf("
EXPORT SQL = %s
\n",print_r($sql)); $this->data = $this->DBO->query($sql); //printf("
EXPORT DATA = %s
\n",print_r($this->data)); } public function view(){ //$output = fopen("php://output", "w"); if (!$this->dbhost_flag) { $this->exportHeader($this->dbhost); $this->dbhost_flag = 1; } if (!$this->database_flag) { $this->exportDatabase($this->database); $this->database_flag = 1; } printf("\n"); printf('--'."\n"); printf('-- Table structure for table: %s'."\n", $this->table); printf('--'."\n"); printf("\n"); $this->connect($this->database); $sql = "SHOW CREATE TABLE `$this->table`"; $data = $this->DBO->query($sql); //printf('CREATE TABLE: %s'."\n", print_r($data)); $statement = !empty($data[0]['Create Table']) ? $data[0]['Create Table'] : ''; $statement = str_replace('CREATE TABLE', 'CREATE TABLE IF NOT EXISTS', $statement); printf('%s;'."\n", $statement); printf("\n"); printf('--'."\n"); printf('-- Export data for table: %s'."\n", $this->table); printf('--'."\n"); printf("\n"); $fields = "`".implode("`,`", $this->fields)."`"; foreach($this->data as $rec){ $values = "'".implode("','", $rec)."'"; printf('INSERT INTO `%s` (%s) VALUES (%s);'."\n", $this->table, $fields, $values); } //fclose($output); } } class Extensions extends DBone { public function controller(){ $this->getRequest(); $this->model(); $this->showHead(); $this->showStatus(); $this->view(); $this->showFoot(); } public function model(){ //phpinfo(); } public function view(){ printf('

%s

'."\n", 'EXTENSIONS'); printf(''."\n"); printf(''."\n"); printf(''."\n"); printf(''."\n"); printf('
'."\n"); printf('
%s
'."\n", print_r(get_loaded_extensions())); printf('
'."\n"); } } class Pages extends DBone { public function controller(){ parent::controller(); } public function model(){ $this->data = array(); $dir = '../dbone'; $files = array_diff(scandir($dir), array('.', '..', '.DS_Store')); //printf('
FILES = %s
'."\n",print_r($files)); foreach ($files as $file) { $page = substr($file, 0, strrpos($file, ".")); $this->data[] = $page; } //printf('
DATA = %s
'."\n",print_r($this->data)); } public function view(){ printf('

%s

'."\n", 'Pages'); printf(''."\n"); printf(''."\n"); printf(''."\n"); printf(''."\n"); printf(''."\n"); printf(''."\n"); printf(''."\n"); printf(''."\n"); $n=0; foreach ($this->data as $page) { $n++; printf(''."\n"); printf(''."\n", $n); printf(''."\n", $page, $page); printf(''."\n"); } printf(''."\n"); printf('
#PAGE
%s%s
'."\n"); } } class PhpInfo extends DBone { public function controller(){ $this->getRequest(); $this->model(); $this->showHead(); $this->showStatus(); $this->view(); $this->showFoot(); } public function model(){ //phpinfo(); } public function view(){ printf('

%s

'."\n", 'PHP Info'); phpinfo(); } } class Server extends DBone { public function controller(){ $this->getRequest(); $this->model(); $this->showHead(); $this->showStatus(); $this->view(); $this->showFoot(); } public function model(){ //phpinfo(); } public function view(){ printf('

%s

'."\n", 'Server'); printf(''."\n"); printf(''."\n"); printf(''."\n"); printf(''."\n"); printf('
'."\n"); printf('
%s
'."\n",print_r($_SERVER)); printf('
'."\n"); } } class Session extends DBone { public function controller(){ $this->getRequest(); $this->model(); $this->showHead(); $this->showStatus(); $this->view(); $this->showFoot(); } public function model(){ foreach ($_SESSION as $key => $val) { $this->data[] = array('key' => $key, 'val' => $val); } //printf('
DATA = %s
'."\n",print_r($data)); } public function view(){ $n=0; printf('

%s

'."\n", 'Session'); printf(''."\n"); foreach ($this->data as $rec) { if ($n == 0){ printf(''."\n"); printf(''."\n"); printf(''."\n"); foreach ($rec as $key => $val) { printf(''."\n", strtoupper($key)); } printf(''."\n"); printf(''."\n"); printf(''."\n"); } $n++; printf(''."\n"); printf(''."\n", $n); printf(''."\n", $rec['key']); printf(''."\n", print_r($rec['val'])); printf(''."\n"); } printf(''."\n"); printf('
#%s
%s%s%s
'."\n"); } } class Versions extends DBone { public function controller(){ $this->getRequest(); $this->model(); $this->showHead(); $this->showStatus(); $this->view(); $this->showFoot(); } public function model(){ $this->data = array(); $this->data['PHP Version'] = phpversion(); $this->data['MySQL Client'] = mysqli_get_client_version(); $this->connect(); $sql = "SELECT VERSION()"; $data = $this->DBO->query($sql); $this->data['MySQL Server'] = $data[0]['VERSION()']; } public function view(){ printf('

%s

'."\n", 'Versions'); printf(''."\n"); foreach ($this->data as $key => $val) { printf(''."\n"); printf(''."\n", $key); printf(''."\n", $val); printf(''."\n"); } printf('
%s%s
'."\n"); } } class DBO { public $PDO; public function __construct(){} public function connect($dba){ $this->debug(__METHOD__, 'dba', $dba, 1); $driver = !empty($dba['driver']) ? $dba['driver'] : ''; $dbhost = !empty($dba['dbhost']) ? $dba['dbhost'] : ''; $dbport = !empty($dba['dbport']) ? $dba['dbport'] : ''; $dbchar = !empty($dba['dbchar']) ? $dba['dbchar'] : ''; $dbname = !empty($dba['dbname']) ? $dba['dbname'] : ''; $dbuser = !empty($dba['dbuser']) ? $dba['dbuser'] : ''; $dbpass = !empty($dba['dbpass']) ? $dba['dbpass'] : ''; $this->debug(__METHOD__, 'driver', $driver, 1); $dsn = "$driver:host=$dbhost;port=$dbport;charset=$dbchar;dbname=$dbname;"; $dbopts = array( \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION, \PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC, \PDO::ATTR_EMULATE_PREPARES => false, ); try { $this->PDO = new \PDO($dsn, $dbuser, $dbpass, $dbopts); $status = true; } catch(PDOException $pex) { print "Error: " . $pex->getMessage(); //$status = $pex->errorInfo[2]; $status = $pex->getMessage(); } return($status); } public function connect2($dba){ //public function connect($driver, $dbhost, $dbport, $charset, $dbuser, $dbpass, $database){ if (empty($driver)) return(false); if ($driver == 'sqlite') { if (empty($database)) { return(true); } else { //printf('
DBO.dbhost = %s
'."\n", print_r($dbhost)); $dsn = "$driver:$dbhost/$database.db"; //printf('
DBO.dsn = %s
'."\n", print_r($dsn)); } } else { //$dsn = "$driver:host=$dbhost;port=$dbport;"; $dsn = "$driver:host=$dbhost;port=$dbport;charset=$charset;dbname=$database;"; } $options = array( \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION, \PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC, \PDO::ATTR_EMULATE_PREPARES => false, ); try { //printf('
DBO.dsn     = %s
'."\n", print_r($dsn)); //printf('
DBO.dbuser  = %s
'."\n", print_r($dbuser)); //printf('
DBO.dbpass  = %s
'."\n", print_r($dbpass)); //printf('
DBO.options = %s
'."\n", print_r($options)); //$this->PDO = new \PDO($dsn, $dbuser, $dbpass); $this->PDO = new \PDO($dsn, $dbuser, $dbpass, $options); //printf('
DBO.connect.status = OK
'."\n"); $status = true; } catch(PDOException $pex) { printf('
DBO.connect.error = %s
'."\n", print_r($pex->getMessage())); $status = $pex->errorInfo[2]; } return $status; } function exec($sql) { try { $status = $this->PDO->exec($sql); } catch(\PDOException $pex) { $status = $pex->errorInfo[2]; } return $status; } function execute2($sql) { try { $status = $this->PDO->execute($sql); } catch(\PDOException $pex) { $status = $pex->errorInfo[2]; } return $status; } function getlastId() { try { $lastId = $this->PDO->lastInsertId(); } catch(\PDOException $pex) { $status = $pex->errorInfo[2]; } return $lastId; } function prepare($sql) { try { $stmt = $this->PDO->prepare($sql); return($stmt); } catch(\PDOException $pex) { $status = $pex->errorInfo[2]; return($status); } } function query($sql) { if (empty($this->PDO)) return(false); try { //printf('
SQL = %s
'."\n", print_r($sql)); $results = $this->PDO->query($sql); //printf('
RESULTS = %s
'."\n", print_r($results)); $rows = $results->fetchAll(); //printf('
ROWS = %s
'."\n", print_r($rows)); //$rows = $this->PDO->query($sql, PDO::FETCH_ASSOC); return($rows); } catch(\PDOException $pex) { $status = $pex->errorInfo[2]; return($status); } } public function debug($method, $name, $value, $mode=null) { if (!empty($this->debug) || !empty($mode)) { printf('
%s.%s = %s
'."\n", $method, $name, print_r($value, 1)); } } } $DBone = new DBone(); $DBone->controller(); $DBone->model(); $DBone->view(); ?>