This repository has been archived on 2018-10-12. You can view files and clone it, but cannot push or open issues or pull requests.
OpenSQLManager/sys/widgets/db_info_widget.php

636 lines
13 KiB
PHP
Raw Normal View History

<?php
/**
* OpenSQLManager
*
* Free Database manager for Open Source Databases
*
2012-04-20 13:30:27 -04:00
* @package OpenSQLManager
* @author Timothy J. Warren
* @copyright Copyright (c) 2012
* @link https://github.com/aviat4ion/OpenSQLManager
2012-03-28 09:57:57 -04:00
* @license http://philsturgeon.co.uk/code/dbad-license
*/
// --------------------------------------------------------------------------
2012-03-02 18:00:20 -05:00
/**
* Widget for adding / editing database connections
2012-04-20 13:30:27 -04:00
*
* @package OpenSQLManager
* @subpackage Widgets
2012-03-02 18:00:20 -05:00
*/
class DB_Info_Widget extends GtkTable {
2012-03-30 15:22:52 -04:00
/**
* Alias to Settings::get_instance
2012-04-19 21:55:44 -04:00
*
2012-03-30 15:22:52 -04:00
* @var Settings
*/
private $settings;
/**
2012-04-19 12:29:47 -04:00
* Connection name
2012-04-19 21:55:44 -04:00
*
* @var GtkEntry
2012-03-30 15:22:52 -04:00
*/
2012-04-19 12:29:47 -04:00
protected $conn;
/**
* Connection database name
2012-04-19 21:55:44 -04:00
*
* @var GtkEntry
2012-04-19 12:29:47 -04:00
*/
protected $conn_db;
/**
* Connection database type
2012-04-19 21:55:44 -04:00
*
* @var GtkComboBox
2012-04-19 12:29:47 -04:00
*/
protected $dbtype;
/**
* Connection database host
2012-04-19 21:55:44 -04:00
*
* @var GtkEntry
2012-04-19 12:29:47 -04:00
*/
protected $host;
/**
* Connection user name
2012-04-19 21:55:44 -04:00
*
* @var GtkEntry
2012-04-19 12:29:47 -04:00
*/
protected $user;
/**
* Connection password
2012-04-19 21:55:44 -04:00
*
* @var GtkEntry
2012-04-19 12:29:47 -04:00
*/
protected $pass;
/**
* Connection database file
2012-04-19 21:55:44 -04:00
*
* @var GtkFileChooserButton
2012-04-19 12:29:47 -04:00
*/
protected $db_file;
/**
* Connection port
2012-04-19 21:55:44 -04:00
*
* @var GtkEntry
2012-04-19 12:29:47 -04:00
*/
protected $port;
/**
* Reference to last connection name
2012-04-19 21:55:44 -04:00
*
* @var string
2012-04-19 12:29:47 -04:00
*/
protected $old_conn;
2012-03-30 15:22:52 -04:00
/**
2012-04-19 21:55:44 -04:00
* Label for connection name
*
* @var GtkLabel
2012-03-30 15:22:52 -04:00
*/
2012-04-19 12:29:47 -04:00
protected $lblconn;
/**
2012-04-19 21:55:44 -04:00
* Label for connection database name
*
* @var Gtklabel
2012-04-19 12:29:47 -04:00
*/
protected $lblconn_db;
/**
2012-04-19 21:55:44 -04:00
* Label for database type
*
* @var Gtklabel
2012-04-19 12:29:47 -04:00
*/
protected $lbldbtype;
/**
2012-04-19 21:55:44 -04:00
* Label for database host
*
* @var Gtklabel
2012-04-19 12:29:47 -04:00
*/
protected $lblhost;
/**
2012-04-19 21:55:44 -04:00
* Label for database connection user
*
* @var GtkLabel
2012-04-19 12:29:47 -04:00
*/
protected $lbluser;
/**
2012-04-19 21:55:44 -04:00
* Label for database connection password
*
* @var GtkLabel
2012-04-19 12:29:47 -04:00
*/
protected $lblpass;
/**
2012-04-19 21:55:44 -04:00
* Label for database file
*
* @var GtkLabel
2012-04-19 12:29:47 -04:00
*/
protected $lbldb_file;
/**
2012-04-19 21:55:44 -04:00
* Label for database connection port
*
* @var GtkLabel
2012-04-19 12:29:47 -04:00
*/
protected $lblport;
// --------------------------------------------------------------------------
// ! Start of methods
// --------------------------------------------------------------------------
/**
* No params = add, params = edit
*
* @param object $db
2012-04-19 21:55:44 -04:00
* @return void
*/
public function __construct($db=null)
{
parent::__construct();
$this->settings =& Settings::get_instance();
if (is_null($db))
2012-03-28 20:14:10 -04:00
{
$db = new StdClass();
$db->name = '';
2012-03-28 20:14:10 -04:00
$db->host = '';
$db->user = '';
$db->pass = '';
$db->port = '';
$db->conn_db = '';
$db->type = '';
2012-03-30 09:58:41 -04:00
$db->file = NULL;
2012-03-28 20:14:10 -04:00
}
// Set up the form elements, with default values
$this->conn = new GtkEntry();
$this->host = new GtkEntry();
$this->user = new GtkEntry();
$this->pass = new GtkEntry();
$this->port = new GtkEntry();
$this->conn_db = new GtkEntry();
$this->dbtype = GtkComboBox::new_text();
$this->db_file = new GtkFileChooserButton("Select a database file",
Gtk::FILE_CHOOSER_ACTION_OPEN);
// Populate the available database types
$db_types = $this->get_available_dbs();
2012-05-14 13:25:32 -04:00
foreach($db_types as &$t)
{
$this->dbtype->append_text($t);
}
$lower_db_types = array_map('strtolower', $db_types);
// Populate the text fields with default values
$this->conn->set_text($db->name);
2012-03-28 20:14:10 -04:00
$this->host->set_text($db->host);
$this->user->set_text($db->user);
$this->pass->set_text($db->pass);
$this->conn_db->set_text($db->conn_db);
$this->port->set_text($db->port);
// Layout the table
2012-04-11 14:57:38 -04:00
$this->_layout();
// Select the proper db type if editing
if ( ! empty($db->type))
{
2012-03-30 15:22:52 -04:00
// Set the old conn variable for editing
$this->old_conn = $db->name;
$dbtype = strtolower($db->type);
2012-03-30 15:22:52 -04:00
// Set the db type based on the current connection
$this->dbtype->set_active(array_search($dbtype, $lower_db_types));
// Set default path
if ( ! empty($db->file))
{
$this->db_file->set_filename($db->file);
}
2012-04-10 10:47:37 -04:00
// Re-populate the text fields with their actual values
// This seems to work around a PHP-GTK bug...it SHOULD work
// to set them the first time...
$this->conn->set_text($db->name);
$this->host->set_text($db->host);
$this->user->set_text($db->user);
$this->pass->set_text($db->pass);
$this->conn_db->set_text($db->conn_db);
2012-04-10 10:47:37 -04:00
$this->port->set_text($db->port);
}
}
2012-03-30 15:22:52 -04:00
// --------------------------------------------------------------------------
/**
* Set defaults for new database type
2012-03-28 09:57:57 -04:00
*
* @return void
*/
public function change_db()
{
$new_db = $this->dbtype->get_active_text();
// Reset
2012-03-02 10:00:31 -05:00
$this->host->set_text('127.0.0.1');
$this->db_file->set_filename(NULL);
$this->port->show();
$this->lblport->show();
$this->db_file->hide();
$this->lbldb_file->hide();
$this->host->show();
$this->lblhost->show();
$this->user->set_text('');
$this->pass->set_text('');
$this->port->set_text('');
$this->conn_db->set_text('');
$this->conn_db->show();
$this->lblconn_db->show();
switch($new_db)
{
default:
break;
case "MySQL":
$this->user->set_text('root');
$this->port->set_text(3306);
break;
case "PostgreSQL":
$this->user->set_text('postgres');
$this->port->set_text(5432);
break;
case "Firebird":
$this->user->set_text('sysdba');
$this->pass->set_text('masterkey');
$this->lbldb_file->show();
$this->db_file->show();
$this->conn_db->hide();
$this->lblconn_db->hide();
break;
case "ODBC":
$this->lbldb_file->show();
$this->db_file->show();
break;
case "SQLite":
$this->lbldb_file->show();
$this->db_file->show();
$this->port->hide();
$this->lblport->hide();
$this->host->hide();
$this->lblhost->hide();
$this->conn_db->hide();
$this->lblconn_db->hide();
break;
}
}
2012-03-30 15:22:52 -04:00
// --------------------------------------------------------------------------
/**
* Like change_db function, but save current values
2012-03-30 15:22:52 -04:00
*
* @return void
*/
2012-03-30 15:22:52 -04:00
public function set_db()
{
2012-03-30 15:22:52 -04:00
$dbtype = strtolower($this->dbtype->get_active_text());
// Reset
$this->db_file->hide();
$this->lbldb_file->hide();
switch($dbtype)
{
default:
break;
case "firebird":
$this->lbldb_file->show();
$this->db_file->show();
$this->conn_db->hide();
$this->lblconn_db->hide();
break;
case "odbc":
$this->lbldb_file->show();
$this->db_file->show();
break;
case "sqlite":
$this->lbldb_file->show();
$this->db_file->show();
$this->port->hide();
$this->lblport->hide();
$this->host->hide();
$this->lblhost->hide();
$this->conn_db->hide();
$this->lblconn_db->hide();
break;
}
}
2012-03-30 15:22:52 -04:00
// --------------------------------------------------------------------------
/**
* Adds the database to the settings file
2012-04-11 14:57:38 -04:00
*
* @return void
*/
public function db_add()
{
$data = array(
'type' => strtolower($this->dbtype->get_active_text()),
'host' => $this->host->get_text(),
'user' => $this->user->get_text(),
'pass' => $this->pass->get_text(),
'port' => $this->port->get_text(),
'file' => $this->db_file->get_filename(),
2012-03-28 20:14:10 -04:00
'conn_db' => $this->conn_db->get_text(),
'name' => $this->conn->get_text(),
);
2012-04-10 10:47:37 -04:00
$res = $this->settings->add_db($data['name'], $data);
if ($res === FALSE)
2012-04-10 10:47:37 -04:00
{
error("Failed to add database - Connection information invalid");
}
2012-02-28 10:38:13 -05:00
// Pass to connection sidebar to update
Connection_Sidebar::get_instance()->refresh();
2012-03-28 09:57:57 -04:00
2012-03-28 20:14:10 -04:00
// Destroy the parent window
2012-04-10 10:47:37 -04:00
$parent_window = $this->get_parent_window();
2012-03-28 20:14:10 -04:00
$parent_window->destroy();
}
2012-03-30 15:22:52 -04:00
// --------------------------------------------------------------------------
2012-03-28 20:14:10 -04:00
/**
* Edit an existing database connection
2012-04-11 14:57:38 -04:00
*
* @return void
2012-03-28 20:14:10 -04:00
*/
public function db_edit()
{
$data = array(
'type' => strtolower($this->dbtype->get_active_text()),
'host' => $this->host->get_text(),
'user' => $this->user->get_text(),
'pass' => $this->pass->get_text(),
'port' => $this->port->get_text(),
'file' => $this->db_file->get_filename(),
'conn_db' => $this->conn_db->get_text(),
'name' => $this->conn->get_text(),
);
2012-03-30 15:22:52 -04:00
if ($this->settings->edit_db($this->old_conn, $data))
{
// Let the user know the connection has been updated
alert("Changes to database connection have been saved");
}
else
{
error("Error saving changes");
}
2012-03-28 20:14:10 -04:00
// Pass to connection sidebar to update
Connection_Sidebar::get_instance()->refresh();
2012-03-30 09:58:41 -04:00
// Destroy the parent window
2012-04-10 10:47:37 -04:00
$parent_window = $this->get_parent_window();
$parent_window->destroy();
}
2012-03-30 15:22:52 -04:00
// --------------------------------------------------------------------------
/**
* Test a db connection, and display a popup with the result of the test
2012-04-11 14:57:38 -04:00
*
* @return void
*/
public function test_conn()
{
$params = new stdClass();
$params->type = strtolower($this->dbtype->get_active_text());
$params->host = $this->host->get_text();
$params->user = $this->user->get_text();
$params->pass = $this->pass->get_text();
$params->port = $this->port->get_text();
$params->file = $this->db_file->get_filename();
2012-04-02 16:56:55 -04:00
$params->conn_db = $this->conn_db->get_text();
2012-03-02 16:56:05 -05:00
// Return early if a db type isn't selected.
// Better to bail out then crash because of
// silly user input.
2012-03-02 18:00:20 -05:00
if( empty($params->type))
2012-03-02 16:56:05 -05:00
{
2012-04-10 10:47:37 -04:00
error("Failed to connect - Invalid connection settings");
2012-03-02 16:56:05 -05:00
return;
}
2012-03-28 09:57:57 -04:00
// Catch connection exceptions, and
2012-03-07 11:26:45 -05:00
// display the error message to the
2012-03-28 09:57:57 -04:00
// user so they can edit the db
2012-03-07 11:26:45 -05:00
// parameters
try
{
2012-04-02 08:43:27 -04:00
new Query_Builder($params);
}
catch (PDOException $e)
{
error("Error connecting to database: \n\n" . $e->getMessage());
return;
}
2012-03-28 09:57:57 -04:00
// Successful Connection?
// Tell the user!
alert("Successfully Connected.");
}
2012-03-30 15:22:52 -04:00
// --------------------------------------------------------------------------
/**
* Checks what database drivers are available
2012-03-28 09:57:57 -04:00
*
* @return array
*/
public function get_available_dbs()
{
$drivers = array();
// Check if there is pdo support
if( ! function_exists('pdo_drivers'))
{
return FALSE;
}
2012-03-02 10:00:31 -05:00
$pdo_drivers = pdo_drivers();
// Add PDO drivers
2012-05-14 13:25:32 -04:00
foreach($pdo_drivers as &$d)
{
// Skip sqlite2 as opposed to sqlite3
2012-03-02 16:56:05 -05:00
if($d === 'sqlite2' && (in_array('sqlite', $pdo_drivers) || in_array('sqlite3', $pdo_drivers)))
2012-03-02 10:00:31 -05:00
{
continue;
}
// Use the ibase_functions over PDO::Firebird, at least for now
if($d === 'firebird')
{
continue;
}
// Replace default capitalization with something that looks better.
$d = str_replace("sql", "SQL", $d);
$d = str_ireplace("pg", "Postgre", $d);
$d = str_ireplace("odbc", "ODBC", $d);
$d = ucfirst($d);
$drivers[] = $d;
}
// Add firebird support, if exists
2012-03-19 12:20:51 -04:00
if(function_exists('fbird_connect') && ! in_array('firebird', $pdo_drivers))
{
$drivers[] = "Firebird";
}
sort($drivers);
return $drivers;
}
2012-03-30 15:22:52 -04:00
// --------------------------------------------------------------------------
/**
* Simple helper function for adding a row to the GtkTable
2012-03-28 09:57:57 -04:00
*
* @param string $label
* @param string $vname
* @param int &$y1
* @param int &$y2
2012-04-11 14:57:38 -04:00
* @return void
*/
private function _add_row($label, $vname, &$y1, &$y2)
{
$lbl = 'lbl'.$vname;
$this->$lbl = new GtkLabel($label);
$lblalign = new GtkAlignment(0, 0.5, 0, 0);
$lblalign->add($this->$lbl);
$vname =& $this->$vname;
$this->attach($lblalign, 0, 1, ++$y1, ++$y2);
$this->attach($vname, 1, 2, $y1, $y2);
}
2012-04-11 14:57:38 -04:00
// --------------------------------------------------------------------------
/**
* Table layout
*
* @return void
*/
private function _layout()
{
// Reset defaults when changing db types
$this->dbtype->connect_simple("changed", array($this, "change_db"));
//Table attach
//$tbl->attach(left_start, right_stop, top_start, bottom_stop)
// Placeholder vars for y values, so that rows can be
// easily moved
$y1 = -1;
$y2 = 0;
// Connection name
2012-04-19 21:55:44 -04:00
$this->_add_row("Connection name", 'conn', $y1, $y2);
2012-04-11 14:57:38 -04:00
// Database type
2012-04-19 21:55:44 -04:00
$dbtypelbl = new GtkLabel("Database Type");
$typealign = new GtkAlignment(0, 0.5, 0, 0);
$typealign->add($dbtypelbl);
$this->attach($typealign, 0, 1, ++$y1, ++$y2);
$this->attach($this->dbtype, 1, 2, $y1, $y2);
2012-04-11 14:57:38 -04:00
// DB File
2012-04-19 21:55:44 -04:00
$this->_add_row("Database File", 'db_file', $y1, $y2);
2012-04-11 14:57:38 -04:00
// First Db
2012-04-19 21:55:44 -04:00
$this->_add_row("Database Name", 'conn_db', $y1, $y2);
2012-04-11 14:57:38 -04:00
// Host
2012-04-19 21:55:44 -04:00
$this->_add_row("Host", 'host', $y1, $y2);
2012-04-11 14:57:38 -04:00
// Port
2012-04-19 21:55:44 -04:00
$this->_add_row("Port", 'port', $y1, $y2);
2012-04-11 14:57:38 -04:00
// Username
2012-04-19 21:55:44 -04:00
$this->_add_row("User", 'user', $y1, $y2);
2012-04-11 14:57:38 -04:00
// Password
2012-04-19 21:55:44 -04:00
$this->_add_row("Password", 'pass', $y1, $y2);
2012-04-11 14:57:38 -04:00
// Add/Edit connection button
{
$conn_name = $this->conn->get_text();
$caption = (empty($conn_name)) ? 'Add Connection' : 'Update Connection';
$add_button = new GtkButton();
$add_button->set_label($caption);
( ! empty($conn_name))
? $add_button->set_image(GTKImage::new_from_stock(GTK::STOCK_SAVE,
GTK::ICON_SIZE_SMALL_TOOLBAR))
: $add_button->set_image(GTKImage::new_from_stock(GTK::STOCK_ADD,
Gtk::ICON_SIZE_SMALL_TOOLBAR));
$this->attach($add_button, 0, 1, ++$y1, ++$y2);
if ( ! empty($conn_name))
{
$add_button->connect_simple("clicked", array($this, 'db_edit'));
}
else
{
$add_button->connect_simple("clicked", array($this, 'db_add'));
}
}
// Test connection button
{
$test_button = new GtkButton();
$test_button->set_label("Test Connection");
$this->attach($test_button, 1, 2, $y1, $y2);
$test_button->connect_simple("clicked", array($this, 'test_conn'));
}
}
2012-03-28 20:14:10 -04:00
}
// End of db_info_widget.php