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 setDBA($dba) { $_SESSION['driver'] = $dba['driver']; $_SESSION['dbhost'] = $dba['dbhost']; $_SESSION['dbport'] = $dba['dbport']; $_SESSION['dbchar'] = $dba['dbchar']; $_SESSION['dbuser'] = $dba['dbuser']; $_SESSION['dbpass'] = $dba['dbpass']; $_SESSION['dbname'] = $dba['dbname']; $_SESSION['dbpath'] = $dba['dbpath']; $_SESSION['dbfile'] = $dba['dbfile']; } 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", $this->page); printf(''."\n", $src); $src = $this->getFrameSrc("body", $this->page); printf(''."\n", $src); $src = $this->getFrameSrc("foot", $this->page); 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'] : ''; $uri = !empty($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : ''; $uris = explode('?', $uri); //$this->debug(__METHOD__, 'SERVER', $_SERVER, 1); //$this->debug(__METHOD__, 'refs', $refs, 1); if (!empty($uris[1])) { //$this->debug(__METHOD__, '$uris[1]', $uris[1], 1); parse_str($uris[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 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){ $dba = $this->getDBA(); $this->DBO = new $dba['driver']; $status = $this->DBO->connect($dba); //$this->debug(__METHOD__, 'status', $status, 1); $data = $this->DBO->getDatabases($order_by, $order); return($data); } public function getTables($database, $order_by=null, $order=null){ $dba = $this->getDBA(); $dba['dbname'] = $database; $this->DBO = new $dba['driver']; $status = $this->DBO->connect($dba); //$this->debug(__METHOD__, 'status', $status, 1); $data = $this->DBO->getTables($database, $order_by, $order); return($data); } public function getViews($database, $order_by=null, $order=null){ $dba = $this->getDBA(); $dba['dbname'] = $database; $this->DBO = new $dba['driver']; $status = $this->DBO->connect($dba); //$this->debug(__METHOD__, 'status', $status, 1); $data = $this->DBO->getViews($database, $order_by, $order); return($data); } public function getRecords($database, $table, $order_by=null, $order=null){ $dba = $this->getDBA(); $dba['dbname'] = $database; $this->DBO = new $dba['driver']; $status = $this->DBO->connect($dba); //$this->debug(__METHOD__, 'status', $status, 1); $data = $this->DBO->getRecords($database, $table, $order_by, $order); return($data); } public function getRecord($database, $table, $key, $val){ $dba = $this->getDBA(); $dba['dbname'] = $database; $this->DBO = new $dba['driver']; $status = $this->DBO->connect($dba); //$this->debug(__METHOD__, 'status', $status, 1); $data = $this->DBO->getRecord($database, $table, $key, $val); return($data); } 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 getTables111($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 getViews111($driver, $database){ $this->debug(__METHOD__, 'driver', $driver, 1); $this->debug(__METHOD__, 'database', $database, 1); 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=''){ //$this->debug(__METHOD__, 'page', $page, 1); //$this->debug(__METHOD__, 'database', $database, 1); //$this->debug(__METHOD__, 'table', $table, 1); //exit; 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); //printf(''."\n", $order1); $order2 = $order_by == 'tables' && $order != 'DESC' ? 'DESC' : 'ASC'; printf(''."\n", $order2); //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('
#DATABASEDATABASETABLESTABLES
%s%s%s
T O T A L%s
'."\n"); printf('
'."\n"); } public function showTables($database, $tables, $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($tables) || count($tables) == 0) { printf('

Database has no tables!

'."\n"); return(false); } $row=0; $total=0; printf('
'."\n"); printf('

%s

'."\n", $database); 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); //$order3 = $order_by == 'size' && $order != 'DESC' ? 'DESC' : 'ASC'; //printf(''."\n", $database, $order3); //$order4 = $order_by == 'auto' && $order != 'DESC' ? 'DESC' : 'ASC'; //printf(''."\n", $database, $order4); printf(''."\n"); printf(''."\n"); printf(''."\n"); foreach ($tables as $rec) { $row++; printf(''."\n", $database, $rec['table']); printf(''."\n", $row); printf(''."\n", $rec['table']); printf(''."\n", $rec['rows']); //printf(''."\n", $rec['size']); //printf(''."\n", $rec['auto']); printf(''."\n"); $total += intval($rec['rows']); } printf(''."\n"); printf(''."\n"); printf(''."\n"); printf(''."\n"); printf(''."\n", $total); printf(''."\n"); printf(''."\n"); printf('
#TABLESROWSSIZEAUTO
%s%s%s%s%s
T O T A L%s
'."\n"); printf('
'."\n"); } public function showViews($database, $views, $order_by, $order) { //$this->debug(__METHOD__, 'VIEWS', $views, 1); if (!is_array($views) || count($views) == 0) { printf('

Database has no views!

'."\n"); return(false); } $row=0; $total=0; printf('
'."\n"); printf('

%s

'."\n", $database); 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 ($views as $rec) { //$this->debug(__METHOD__, 'rec', $rec, 1); $row++; printf(''."\n", $database, $rec['view']); printf(''."\n", $row); printf(''."\n", $rec['view']); printf(''."\n", $rec['rows']); //printf(''."\n", $rec['size']); //printf(''."\n", $rec['auto']); printf(''."\n"); $total += intval($rec['rows']); } printf(''."\n"); printf(''."\n"); printf(''."\n"); printf(''."\n"); printf(''."\n", $total); printf(''."\n"); printf(''."\n"); printf('
#VIEWSROWS
%s%s%s%s%s
T O T A L%s
'."\n"); printf('
'."\n"); } public function showRecords($database, $table, $records, $order_by, $order) { printf('

%s

'."\n", $table); if (!is_array($records) || count($records) == 0) { printf('

Table has no records!

'."\n"); return(false); } $row=0; printf(''."\n"); foreach ($this->records as $rec) { if ($row == 0){ printf(''."\n"); printf(''."\n"); printf(''."\n"); foreach ($rec as $key => $val) { printf(''."\n", $this->database, $this->table, $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"); } public function showRecord($database, $table, $record, $key, $val) { printf('

RECORD

'."\n"); $row=0; printf('
'."\n"); printf(''."\n", $database); printf(''."\n", $table); printf(''."\n", $key); printf(''."\n", $val); //$this->debug(__METHOD__, 'record[0]', $record[0],1); foreach($record as $rec) { foreach($rec as $key => $val) { printf(''."\n", $key, $val); } } printf(''."\n"); printf(''."\n"); //printf("\n", $caption); $valFlag = 0; foreach($record as $rec) { foreach($rec as $key => $val) { printf(''."\n", ucfirst($key), $key, $val); if (!empty($val)) { $valFlag = 1; } } } printf(''."\n"); printf(''."\n"); if (!empty($valFlag)) { printf(''."\n"); printf(''."\n"); printf(''."\n"); printf(''."\n"); printf(''."\n"); } else { printf(''."\n"); printf(''."\n"); printf(''."\n"); printf(''."\n"); printf(''."\n"); } printf(''."\n"); printf('
%s
 %s 
'."\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"); printf('
'."\n"); printf('
'."\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, $dba, $drivers; public $driver, $dbhost, $dbport, $dbchar, $dbuser, $dbpass, $dbname, $dbpath, $dbfile; public function controller(){ $this->cmd = $this->request('cmd'); $this->driver = $this->request('driver', 'mysql'); $this->dbhost = $this->request('dbhost', 'localhost'); $this->dbport = $this->request('dbport', '3306'); $this->dbchar = $this->request('dbchar', 'utf8mb4'); $this->dbuser = $this->request('dbuser'); $this->dbpass = $this->request('dbpass'); $this->dbname = $this->request('dbname'); $this->dbpath = $this->request('dbpath', '../sqlite'); $this->dbfile = $this->request('dbfile', ''); if ($this->cmd == 'login') { $this->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, ); //$this->debug(__METHOD__, 'dba', $this->dba, 1); $dbo = new $this->driver(); $status = $dbo->connect($this->dba); //$this->debug(__METHOD__, 'status', $status, 1); if ($status === true) { $this->setDBA($this->dba); //$this->debug(__METHOD__, 'driver', $this->driver, 1); if ($this->driver == 'sqlite') { if (!empty($this->dbfile)) { $page = 'tables'; $dbfile = $this->dbfile; $this->redirect($page, $dbfile); } else { $page = 'databases'; $this->redirect($page); } } else { if (!empty($this->dbname)) { $page = 'tables'; $database = $this->dbname; $this->redirect($page, $database); } else { $page = 'databases'; $this->redirect($page); } } } else { //printf('
ERROR2: %s
'."\n", print_r($status)); $this->error(ucwords($status)); session_destroy(); } } else { session_destroy(); } } public function model(){ $this->drivers = $this->getDrivers(); } public function view(){ $this->showHead(); $this->showStatus(); 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') { 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 Databases extends DBone { public $databases, $order_by, $order; public function controller(){ $this->order_by = $this->request('order_by'); $this->order = $this->request('order'); } public function model(){ $this->databases = $this->getDatabases($this->order_by, $this->order); //$this->debug(__METHOD__, 'databases', $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 Operations extends DBone { public $data; public function controller(){ //$this->getRequest(); $this->cmd = !empty($_REQUEST['cmd']) ? $_REQUEST['cmd'] : ''; //$this->debug(__METHOD__, 'REQUEST', $_REQUEST, 1); if ($this->cmd == 'create') { $results = $this->createDatabase($this->database1); $status = $this->getStatus($results, "Database was created", 'databases'); } else if ($this->cmd == 'copy') { $results = $this->copyDatabase($this->database1, $this->database2); $this->debug(__METHOD__, 'results', $results, 1); //exit; $status = $this->getStatus($results, "Database was copied", 'databases'); $this->debug(__METHOD__, 'status', $status, 1); } else if ($this->cmd == 'clone') { $results = $this->cloneDatabase($this->database1, $this->database2); $status = $this->getStatus($results, "Database was cloned", 'databases'); } else if ($this->cmd == 'delete') { $results = $this->deleteDatabase($this->database1); $status = $this->getStatus($results, "Database was deleted", 'databases'); } else if ($this->cmd == 'rename') { $results = $this->renameDatabase($this->database1, $this->database2); $status = $this->getStatus($results, "Database was renamed", 'databases'); } else if ($this->cmd == 'import') { $results = $this->importFile($this->database1); $status = $this->getStatus($results, "Database was imported", 'databases'); } else if ($this->cmd == 'export') { $results = $this->exportFile($this->dbhost, $this->database1); exit; $status = $this->getStatus($results, "Database was exported", 'databases'); } } public function model(){ //$driver = $this->getDriver(); $this->data = $this->getDatabases(); //$this->debug(__METHOD__, 'DATA', $this->data, 1); } public function view(){ $this->showHead(); $this->showStatus(); printf('

OPERATIONS

'."\n"); printf(''."\n"); printf(''."\n"); printf(''."\n"); printf(''."\n"); printf('
'."\n"); printf('
'."\n"); printf('
'."\n"); printf('Create Database'."\n"); printf('
'."\n"); printf(''."\n"); printf('
'."\n"); printf('
'."\n"); printf('
'."\n"); printf('
'."\n"); printf('
'."\n"); printf('Copy Database'."\n"); printf('
'."\n"); printf('
'."\n"); printf(''."\n"); printf('
'."\n"); printf('
'."\n"); printf('
'."\n"); printf('
'."\n"); printf('
'."\n"); printf('Clone Database'."\n"); printf('
'."\n"); printf('
'."\n"); printf(''."\n"); printf('
'."\n"); printf('
'."\n"); printf('
'."\n"); printf('
'."\n"); printf('
'."\n"); printf('Delete Database'."\n"); printf('
'."\n"); printf(''."\n"); printf('
'."\n"); printf('
'."\n"); printf('
'."\n"); printf('
'."\n"); printf('
'."\n"); printf('Rename Database'."\n"); printf('
'."\n"); printf('
'."\n"); printf(''."\n"); printf('
'."\n"); printf('
'."\n"); printf('
'."\n"); printf('
'."\n"); printf('
'."\n"); printf('Import SQL File'."\n"); printf('
'."\n"); printf('
'."\n"); printf(''."\n"); printf('
'."\n"); printf('
'."\n"); printf('
'."\n"); printf('
'."\n"); printf('
'."\n"); printf('Export SQL File'."\n"); printf('
'."\n"); printf(''."\n"); printf('
'."\n"); printf('
'."\n"); printf('
'."\n"); $this->showFoot(); } } class Tables extends DBone { public $database, $tables, $views, $order_by, $order, $order_by2, $order2; public function controller(){ $this->database = $this->request('database'); $this->order_by = $this->request('order_by'); $this->order = $this->request('order'); $this->order_by2 = $this->request('order_by2'); $this->order2 = $this->request('order2'); } public function model(){ $this->tables = $this->getTables($this->database, $this->order_by, $this->order); $this->views = $this->getViews($this->database, $this->order_by2, $this->order2); } public function view(){ $this->showHead(); $this->showStatus(); $this->showTables($this->database, $this->tables, $this->order_by, $this->order); $this->showViews($this->database, $this->views, $this->order_by2, $this->order2); $this->showFoot(); } } class Procedures extends DBone { public function controller(){ $this->getRequest(); $_SESSION['table'] = ''; $this->debug(__METHOD__, 'REQUEST', $_REQUEST); $this->connect($this->database); if ($this->command == 'create') { $this->debug(__METHOD__, 'table', $this->table); $results = $this->createTable($this->table); $status = $this->getStatus($results, "Table `$this->table` was created", 'tables', $this->database); //$status = $this->getStatus($results, "Table was created", 'tables', $this->database); } else if ($this->command == 'copy') { $results = $this->copyTable($this->table, $this->table2); $status = $this->getStatus($results, "Table `$this->table` was copied", 'tables', $this->database); } else if ($this->command == 'clone') { $results = $this->cloneTable($this->table, $this->table2); $status = $this->getStatus($results, "Table `$this->table` was cloned", 'tables', $this->database); } else if ($this->command == 'delete') { $results = $this->deleteTable($this->table); $status = $this->getStatus($results, "Table was deleted", 'tables', $this->database); } else if ($this->command == 'empty') { $results = $this->emptyTable($this->table); $status = $this->getStatus($results, "Table `$this->table` was emptied", 'tables', $this->database); } else if ($this->command == 'rename') { $results = $this->renameTable($this->table, $this->table2); $status = $this->getStatus($results, "Table `$this->table` was renamed", 'tables', $this->database); } else if ($this->command == 'import') { $results = $this->importFile($this->database); $status = $this->getStatus($results, "Table `$this->table` was imported", 'tables', $this->database); } else if ($this->command == 'export') { $results = $this->exportFile($this->dbhost, $this->database, $this->table); $status = $this->getStatus($results, "Table `$this->table` was exported", 'tables', $this->database); } else if ($this->command == 'create_view') { $results = $this->createView($this->view, $this->sql); $status = $this->getStatus($results, "View was created", 'tables', $this->database); } //parent::controller(); } public function model(){ $this->data = $this->getTables($this->driver, $this->database); //printf("
DATA = %s
\n",print_r($this->data)); } public function view(){ $this->showHead(); $this->showStatus(); printf('

PROCEDURES

'."\n"); printf(''."\n"); printf(''."\n"); printf(''."\n"); printf(''."\n"); printf('
'."\n"); //printf('
'."\n", $this->database); printf(''."\n", $this->database); printf('
'."\n"); printf('Create Table'."\n"); printf('
'."\n"); printf(''."\n"); printf('
'."\n"); printf('
'."\n"); printf('
'."\n", $this->database); printf('
'."\n"); printf('Copy Table'."\n"); printf('
'."\n"); printf('
'."\n"); printf(''."\n"); printf('
'."\n"); printf('
'."\n"); printf('
'."\n", $this->database); printf('
'."\n"); printf('Clone Table'."\n"); printf('
'."\n"); printf('
'."\n"); printf(''."\n"); printf('
'."\n"); printf('
'."\n"); //printf('
'."\n", $this->database); printf(''."\n", $this->database); printf('
'."\n"); printf('Delete Table'."\n"); printf('
'."\n"); printf(''."\n"); printf('
'."\n"); printf('
'."\n"); printf('
'."\n", $this->database); printf('
'."\n"); printf('Empty Table'."\n"); printf('
'."\n"); printf(''."\n"); printf('
'."\n"); printf('
'."\n"); printf('
'."\n", $this->database); printf('
'."\n"); printf('Rename Table'."\n"); printf('
'."\n"); printf('
'."\n"); printf(''."\n"); printf('
'."\n"); printf('
'."\n"); printf('
'."\n", $this->database); printf('
'."\n"); printf('Import SQL File'."\n"); printf('
'."\n"); printf(''."\n"); printf('
'."\n"); printf('
'."\n"); printf('
'."\n", $this->database); printf('
'."\n"); printf('Export SQL File'."\n"); printf('
'."\n"); printf(''."\n"); printf('
'."\n"); printf('
'."\n"); $view = "New_View"; $sql = "SELECT * FROM `test`"; printf('
'."\n"); printf(''."\n"); printf(''."\n"); printf(''."\n"); printf(''."\n"); printf(''."\n", $view); printf(''."\n"); printf(''."\n"); printf(''."\n"); printf(''."\n"); printf(''."\n"); printf(''."\n"); printf(''."\n"); printf(''."\n"); printf(''."\n"); printf('
VIEW
SQL'."\n", $sql); printf('
'."\n"); printf(''."\n"); printf('
'."\n"); printf('
'."\n"); printf('
'."\n"); $this->showFoot(); } } class Records extends DBone { public $database, $table, $view, $records, $order_by, $order; public function controller(){ $this->database = $this->request('database'); $this->table = $this->request('table'); $this->view = $this->request('view'); $this->order_by = $this->request('order_by'); $this->order = $this->request('order'); } public function model(){ if (!empty($this->view)) { $this->records = $this->getRecords($this->database, $this->view, $this->order_by, $this->order); } else { $this->records = $this->getRecords($this->database, $this->table, $this->order_by, $this->order); } } public function view(){ $this->showHead(); $this->showStatus(); $this->showRecords($this->database, $this->table, $this->records, $this->order_by, $this->order); $this->showFoot(); } } class Record extends DBone { public $cmd, $database, $table, $key, $val; public function controller(){ $this->cmd = $this->request('cmd'); $this->database = $this->request('database'); $this->table = $this->request('table'); $this->key = $this->request('key'); $this->val = $this->request('val'); //$this->debug(__METHOD__, 'REQUEST', $_REQUEST, 1); if ($this->cmd == 'delete') { $status = $this->deleteRecord($this->database, $this->table, $this->record1); if (!is_string($status)) { $this->notice("Record was deleted!"); $this->redirect('records', $this->database, $this->table); return(true); } else { $this->error($status); } } else if ($this->cmd == 'insert') { $status = $this->insertRecord($this->database, $this->table, $this->record2); if (!is_string($status)) { $this->notice("Record was inserted!"); $this->redirect('records', $this->database, $this->table); return(true); } else { $this->error($status); } } else if ($this->cmd == 'update') { $status = $this->updateRecord($this->database, $this->table, $this->record1, $this->record2); if (!is_string($status)) { $this->notice("Record was updated!"); $this->redirect('records', $this->database, $this->table); return(true); } else { $this->error($status); } } /* if (is_string($status)) { $this->data = array(); $this->data[] = $this->record; } */ } public function model(){ $this->record = $this->getRecord($this->database, $this->table, $this->key, $this->val); //$this->debug(__METHOD__, 'DATABASE', $this->database, 1); //$this->debug(__METHOD__, 'TABLE', $this->table, 1); //$this->debug(__METHOD__, 'KEY', $this->key, 1); //$this->debug(__METHOD__, 'VAL', $this->val, 1); //$this->debug(__METHOD__, 'RECORD', $this->record, 1); /* if (!empty($this->key)) { $sql = "SELECT * FROM `$this->table`"; $sql.= " WHERE `$this->key` = '$this->val'"; $this->debug(__METHOD__, 'sql', $sql); $this->data = $this->DBO->query($sql); $this->debug(__METHOD__, 'data', $this->data); } else { $this->debug(__METHOD__, 'data', $this->data); $data = $this->getColumns($this->database, $this->table); $this->debug(__METHOD__, 'columns', $data); $this->data = array(); foreach($data as $rec) { $this->data[] = array($rec['column'] => ''); } } $this->debug(__METHOD__, 'data', $this->data, 1); */ } public function view(){ $this->showHead(); $this->showStatus(); $this->showRecord($this->database, $this->table, $this->record, $this->key, $this->val); $this->showFoot(); } public function view1(){ printf('

RECORD

'."\n"); $row=0; printf('
'."\n"); foreach($this->data as $rec) { foreach($rec as $key => $val) { printf(''."\n", $key, $val); } } printf(''."\n"); printf(''."\n"); //printf("\n", $caption); $valFlag = 0; foreach($this->data as $rec) { foreach($rec as $key => $val) { printf(''."\n", ucfirst($key), $key, $val); if (!empty($val)) { $valFlag = 1; } } } printf(''."\n"); printf(''."\n"); if (!empty($valFlag)) { printf(''."\n"); printf(''."\n"); printf(''."\n"); printf(''."\n"); printf(''."\n"); } else { printf(''."\n"); printf(''."\n"); printf(''."\n"); printf(''."\n"); printf(''."\n"); } printf(''."\n"); printf('
%s
 %s 
'."\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"); printf('
'."\n"); printf('
'."\n"); printf(''."\n"); } public function view2(){ $this->head(); printf('

NEW RECORD

'."\n"); $row=0; printf('
'."\n"); foreach($this->data as $rec) { foreach($rec as $key => $val) { printf(''."\n", $key, $val); } } printf(''."\n"); printf(''."\n"); //printf("\n", $caption); foreach($this->data as $rec) { foreach($rec as $key => $val) { printf(''."\n", ucfirst($key), $key, $val); } } printf(''."\n"); printf(''."\n"); printf(''."\n"); printf(''."\n"); printf(''."\n"); printf(''."\n"); printf(''."\n"); printf(''."\n"); printf('
%s
 %s 
'."\n"); printf(''."\n"); printf(''."\n"); printf(''."\n"); printf('
'."\n"); printf(''."\n"); printf(''."\n"); printf(''."\n"); printf('
'."\n"); printf('
'."\n"); printf(''."\n"); $this->foot(); } } 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 View1 extends DBone { public function controller(){ $this->getRequest(); $this->connect($this->database); if ($this->command == 'create') { $status = $this->createView($this->view2['view'], $this->view2['sql']); if (!is_string($status)) { $this->notice("View was created!"); $this->redirect('views', $this->database); return(true); } else { $this->error($status); } } else if ($this->command == 'delete') { $status = $this->deleteView($this->view1['view']); if (!is_string($status)) { $this->notice("View was deleted!"); $this->redirect('views', $this->database); return(true); } else { $this->error($status); } } else if ($this->command == 'rename') { $status = $this->renameView($this->view1['view'], $this->view2['view']); if (!is_string($status)) { $this->notice("View was renamed!"); $this->redirect('views', $this->database); return(true); } else { $this->error($status); } } $this->model(); $this->showHead(); $this->showStatus(); $this->view(); $this->showFoot(); } public function model(){ $this->data = $this->getView($this->driver, $this->database, $this->view); $this->debug(__METHOD__, 'data', $this->data); } public function view(){ //$this->head(); //$this->showStatus(); printf('

VIEW

'."\n"); printf('
'."\n"); foreach($this->data as $key => $val) { printf(''."\n", $key, $val); } printf(''."\n"); printf(''."\n"); foreach($this->data as $key => $val) { if ($key == 'sql') { printf(''."\n", $key, $key, $val); } else { printf(''."\n", $key, $key, $val); } } printf(''."\n"); printf(''."\n"); printf(''."\n"); printf(''."\n"); printf(''."\n"); printf('
%s
%s
'."\n"); printf(''."\n"); printf(''."\n"); printf(''."\n"); printf('
'."\n"); printf('
'."\n"); //$this->foot(); } } 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 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 getDatabases222() { try { $sql = "SHOW DATABASES"; //$this->debug(__METHOD__, 'SQL', $sql, 1); $stmt = $this->PDO->query($sql); $databases = $stmt->fetchAll(); //$this->debug(__METHOD__, 'DATABASES', $databases, 1); $this->data = array(); foreach($databases as $arr) { //$this->debug(__METHOD__, 'arr', $arr, 1); $dbname = $arr['Database']; //$this->debug(__METHOD__, 'dbname', $dbname, 1); $sql = "SHOW FULL TABLES FROM `$dbname`"; $sql.= " WHERE `Table_type` != 'VIEW'"; //$this->debug(__METHOD__, 'SQL', $sql, 1); $stmt = $this->PDO->query($sql); $tables = $stmt->fetchAll(); //$this->debug(__METHOD__, 'TABLES', $tables, 1); $data[] = array('database' => $dbname, 'tables' => count($tables)); } } catch(\PDOException $pex) { print "Error: " . $pex->getMessage(); //$status = $pex->errorInfo[2]; $data = $pex->getMessage(); } //$this->debug(__METHOD__, 'DATA', $data, 1); return($data); } function getTables222($database) { try { $sql = "SHOW FULL TABLES FROM `$database`"; $sql.= " WHERE `Table_type` != 'VIEW'"; //$this->debug(__METHOD__, 'SQL', $sql, 1); $stmt = $this->PDO->query($sql); $tables = $stmt->fetchAll(); //$this->debug(__METHOD__, 'TABLES', $tables, 1); $this->data = array(); foreach($tables as $arr) { //$this->debug(__METHOD__, 'arr', $arr, 1); $key = sprintf("Tables_in_%s", $database); $table = $arr[$key]; //$this->debug(__METHOD__, 'table', $table, 1); $sql = "SELECT COUNT(*) AS `records`"; $sql.= " FROM `$table`"; //$this->debug(__METHOD__, 'SQL', $sql, 1); $stmt = $this->PDO->query($sql); $records = $stmt->fetchAll(); //$this->debug(__METHOD__, 'records', $records, 1); $data[] = array('table' => $table, 'rows' => $records[0]['records']); } } catch(\PDOException $pex) { print "Error: " . $pex->getMessage(); //$status = $pex->errorInfo[2]; $data = $pex->getMessage(); } //$this->debug(__METHOD__, 'DATA', $data, 1); return($data); } function getViews222($database) { try { $sql = "SHOW FULL TABLES FROM `$database`"; $sql.= " WHERE `Table_type` = 'VIEW'"; //$this->debug(__METHOD__, 'SQL', $sql, 1); $stmt = $this->PDO->query($sql); $views = $stmt->fetchAll(); //$this->debug(__METHOD__, 'TABLES', $tables, 1); $this->data = array(); foreach($views as $arr) { $this->debug(__METHOD__, 'view', $view, 1); $key = sprintf("Tables_in_%s", $database); $view = $arr[$key]; //$this->debug(__METHOD__, 'table', $table, 1); $sql = "SELECT COUNT(*) AS `records`"; $sql.= " FROM `$view`"; //$this->debug(__METHOD__, 'SQL', $sql, 1); $stmt = $this->PDO->query($sql); $records = $stmt->fetchAll(); $this->debug(__METHOD__, 'records', $records, 1); $data[] = array('table' => $table, 'rows' => $records[0]['records']); } } catch(\PDOException $pex) { print "Error: " . $pex->getMessage(); //$status = $pex->errorInfo[2]; $data = $pex->getMessage(); } $this->debug(__METHOD__, 'DATA', $data, 1); return($data); } 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)); } } } class MySQL extends DBO { public $PDO; 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 getDatabases($order_by=null, $order=null){ $order_by = !empty($order_by) ? $order_by : 'database'; $order = !empty($order) ? $order : 'ASC'; $sql = "SELECT A.SCHEMA_NAME AS `database`, 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.= " AND B.TABLE_TYPE != 'VIEW'"; $sql.= " GROUP BY A.SCHEMA_NAME"; $sql.= " ORDER BY `$order_by` $order"; try { $stmt = $this->PDO->query($sql); $data = $stmt->fetchAll(); } catch(PDOException $pex) { print "Error: " . $pex->getMessage(); $data = $pex->getMessage(); } return($data); } public function getTables($database, $order_by=null, $order=null){ $order_by = !empty($order_by) ? $order_by : 'table'; $order = !empty($order) ? $order : 'ASC'; $sql = "SELECT table_name AS `table`, SUM(table_rows) AS `rows`"; $sql.= " FROM information_schema.tables"; $sql.= " WHERE table_schema = '$database'"; $sql.= " AND table_type != 'VIEW'"; $sql.= " GROUP BY table_name"; $sql.= " ORDER BY `$order_by` $order"; try { $stmt = $this->PDO->query($sql); $data = $stmt->fetchAll(); } catch(PDOException $pex) { print "Error: " . $pex->getMessage(); $data = $pex->getMessage(); } return($data); } function getViews($database) { $data = array(); try { $sql = "SHOW FULL TABLES FROM `$database`"; $sql.= " WHERE `Table_type` = 'VIEW'"; $stmt = $this->PDO->query($sql); $views = $stmt->fetchAll(); foreach($views as $arr) { $key = sprintf("Tables_in_%s", strtolower($database)); $view = $arr[$key]; $sql = "SELECT COUNT(*) AS `records`"; $sql.= " FROM `$view`"; $stmt = $this->PDO->query($sql); $records = $stmt->fetchAll(); $data[] = array('view' => $view, 'rows' => $records[0]['records']); } } catch(\PDOException $pex) { print "Error: " . $pex->getMessage(); $data = $pex->getMessage(); } return($data); } public function getViewsBAD($database, $order_by=null, $order=null){ $order_by = !empty($order_by) ? $order_by : 'view'; $order = !empty($order) ? $order : 'ASC'; $this->debug(__METHOD__, ' database', $database, 1); $sql = "SELECT table_name AS `table`, SUM(table_rows) AS `rows`"; $sql.= " FROM information_schema.tables"; $sql.= " WHERE table_schema = '$database'"; $sql.= " AND table_type = 'VIEW'"; $sql.= " GROUP BY table_name"; //$sql.= " ORDER BY `$order_by` $order"; try { $stmt = $this->PDO->query($sql); $data = $stmt->fetchAll(); } catch(PDOException $pex) { print "Error: " . $pex->getMessage(); //$status = $pex->errorInfo[2]; $data = $pex->getMessage(); } $this->debug(__METHOD__, 'data', $data, 1); return($data); } public function getRecords($database, $table, $order_by=null, $order=null){ $order_by = !empty($order_by) ? $order_by : ''; $order = !empty($order) ? $order : 'ASC'; $sql = "SELECT *"; $sql.= " FROM `$table`"; if (!empty($order_by)) { $sql.= " ORDER BY `$order_by` $order"; } //$this->debug(__METHOD__, 'sql', $sql, 1); try { $stmt = $this->PDO->query($sql); $data = $stmt->fetchAll(); } catch(PDOException $pex) { print "Error: " . $pex->getMessage(); //$status = $pex->errorInfo[2]; $data = $pex->getMessage(); } //$this->debug(__METHOD__, 'data00', $data, 1); return($data); } public function getRecord($database, $table, $key, $val){ $sql = "SELECT *"; $sql.= " FROM `$table`"; $sql.= " WHERE `$key` = '$val'"; //$this->debug(__METHOD__, 'sql', $sql, 1); try { $stmt = $this->PDO->query($sql); $data = $stmt->fetchAll(); } catch(PDOException $pex) { print "Error: " . $pex->getMessage(); //$status = $pex->errorInfo[2]; $data = $pex->getMessage(); } //$this->debug(__METHOD__, 'data00', $data, 1); return($data); } } class SQLite extends DBO { public $PDO, $dbpath, $dbfile; public function connect($dba){ $this->dbpath = !empty($dba['dbpath']) ? $dba['dbpath'] : ''; $this->dbfile = !empty($dba['dbfile']) ? $dba['dbfile'] : ''; if (empty($this->dbfile)) return(true); //if (!is_file($this->dbpath.'/'.$this->dbfile)) return("Invalid DBfile!"); $dsn = "sqlite:$this->dbpath".'/'."$this->dbfile"; //$this->debug(__METHOD__, 'dsn', $dsn, 1); try { $this->PDO = new \PDO($dsn); $status = true; } catch(PDOException $pex) { $status = $pex->errorInfo[2]; //$this->debug(__METHOD__, 'errorinfo', $pex->errorInfo, 1); } return $status; } public function getDatabases($order_by=null, $order=null){ $order_by = !empty($order_by) ? $order_by : 'database'; $order = !empty($order) ? $order : 'ASC'; $data = array(); $exts = array('db', 'sqlite'); $files = scandir($this->dbpath); foreach($files as $file) { if (strpos($file, '.') == 0) continue; $dbname = substr($file, 0, strrpos($file, '.')); $ext = substr(strrchr($file, '.'), 1); if (!in_array($ext, $exts)) continue; $dba = array( 'dbpath' => $this->dbpath, 'dbfile' => $file, ); try { $this->connect($dba); $sql = "SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%';"; $stmt = $this->PDO->query($sql); $recs = $stmt->fetchAll(); $record['database'] = $file; $record['tables'] = count($recs); $data[] = $record; } catch(PDOException $pex) { print "Error: " . $pex->getMessage(); $data = $pex->getMessage(); } } return($data); } public function getTables($database, $order_by=null, $order=null){ $order_by = !empty($order_by) ? $order_by : 'table'; $order = !empty($order) ? $order : 'ASC'; $sql = "SELECT * FROM sqlite_master"; $sql.= " WHERE `type` = 'table'"; $sql.= " AND name NOT LIKE 'sqlite_%'"; $sql.= " ORDER BY `$order_by` $order"; $dba = array( 'dbpath' => $this->dbpath, 'dbfile' => $database, ); //$this->debug(__METHOD__, 'dba', $dba, 1); $this->connect($dba); //$this->debug(__METHOD__, 'sql1', $sql, 1); $stmt = $this->PDO->query($sql); $recs = $stmt->fetchAll(); //$this->debug(__METHOD__, 'recs', $recs, 1); $data = array(); foreach($recs as $rec) { //if ($rec['name'] == 'sqlite_sequence') continue; $table = array(); $table['table'] = $rec['name']; $table['rows'] = $rec['rootpage']; $data[] = $table; } return($data); } public function getRecords($database, $table, $order_by=null, $order=null){ $order_by = !empty($order_by) ? $order_by : ''; $order = !empty($order) ? $order : 'ASC'; $sql = "SELECT *"; $sql.= " FROM `$table`"; if (!empty($order_by)) { $sql.= " ORDER BY `$order_by` $order"; } //$this->debug(__METHOD__, 'sql', $sql, 1); $dba = array( 'dbpath' => $this->dbpath, 'dbfile' => $database, ); $this->connect($dba); $stmt = $this->PDO->query($sql); $recs = $stmt->fetchAll(); try { $stmt = $this->PDO->query($sql); $data = $stmt->fetchAll(); } catch(PDOException $pex) { print "Error: " . $pex->getMessage(); //$status = $pex->errorInfo[2]; $data = $pex->getMessage(); } //$this->debug(__METHOD__, 'data00', $data, 1); return($data); } public function getRecord($database, $table, $key, $val){ $sql = "SELECT *"; $sql.= " FROM `$table`"; $sql.= " WHERE `$key` = '$val'"; //$this->debug(__METHOD__, 'sql', $sql, 1); $dba = array( 'dbpath' => $this->dbpath, 'dbfile' => $database, ); $this->connect($dba); $stmt = $this->PDO->query($sql); $recs = $stmt->fetchAll(); try { $stmt = $this->PDO->query($sql); $data = $stmt->fetchAll(); } catch(PDOException $pex) { print "Error: " . $pex->getMessage(); //$status = $pex->errorInfo[2]; $data = $pex->getMessage(); } //$this->debug(__METHOD__, 'data00', $data, 1); return($data); } } $DBone = new DBone(); $DBone->controller(); $DBone->model(); $DBone->view(); ?>