removed reuse stuff, it was confusing

fixed mysql databse create to make more logical sense inside code
removed many if($debug) and replaced with throws
This commit is contained in:
Matthew Vale
2015-12-01 10:56:18 +00:00
parent 4ea9c8abc3
commit 2998a7cf7e
2 changed files with 36 additions and 50 deletions
@@ -55,7 +55,6 @@ include "root.php";
public $db_username; public $db_username;
public $db_password; public $db_password;
public $db_create; public $db_create;
public $db_create_reuse_auth;
public $db_create_username; public $db_create_username;
public $db_create_password; public $db_create_password;
@@ -204,10 +203,10 @@ include "root.php";
protected function create_database() { protected function create_database() {
require $this->config_php; require $this->config_php;
$this->write_progress("Creating database as " . $this->db_type); $this->write_progress("Creating database as " . $this->db_type);
if($this->db_create_reuse_auth) if($this->db_create and strlen($this->db_create_username) > 0)
{ {
$this->db_create_username = $this->db_username; $this->db_create_username = $this->db_username;
$this->db_create_password = $this->db_username_password; $this->db_create_password = $this->db_password;
} }
$function = "create_database_" . $this->db_type; $function = "create_database_" . $this->db_type;
$this->$function(); $this->$function();
@@ -368,42 +367,35 @@ include "root.php";
protected function create_database_mysql() { protected function create_database_mysql() {
//database connection //database connection
$connect_string = ''; $connect_string;
$connect_username = $this->db_username; if (strlen($this->db_host) == 0 && strlen($this->db_port) == 0) {
$connect_password = $this->db_password; //if both host and port are empty use the unix socket
if ($this->db_create) { $connect_string = "mysql:host=$this->db_host;unix_socket=/var/run/mysqld/mysqld.sock;";
$connect_username = $this->db_create_username;
$connect_password = $this->db_create_password;
} }
try { elseif (strlen($this->db_port) == 0) {
if (strlen($this->db_host) == 0 && strlen($this->db_port) == 0) { //leave out port if it is empty
//if both host and port are empty use the unix socket $connect_string = "mysql:host=$this->db_host;";
$this->dbh = new PDO("mysql:host=$this->db_host;unix_socket=/var/run/mysqld/mysqld.sock;", $connect_username, $connect_password, array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'));
}
elseif (strlen($this->db_port) == 0) {
//leave out port if it is empty
$this->dbh = new PDO("mysql:host=$this->db_host;", $connect_username, $connect_password, array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'));
}
else {
$this->dbh = new PDO("mysql:host=$this->db_host;port=$this->db_port;", $connect_username, $connect_password, array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'));
}
$this->dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
} }
catch (PDOException $error) { else {
throw new Exception("error connecting to database: " . $error->getMessage() . "\n" . $sql ); $connect_string = "mysql:host=$this->db_host;port=$this->db_port;";
} }
//create the table, user and set the permissions only if the db_create_username was provided //create the table, user and set the permissions only if the db_create_username was provided
if ($this->db_create) { if ($this->db_create) {
try {
$this->dbh = new PDO($connect_string, $this->db_create_username, db_create_password, array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'));
$this->dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
}
catch (PDOException $error) {
throw new Exception("error connecting to database for ccreate: " . $error->getMessage() . "\n" . $sql );
}
//select the mysql database //select the mysql database
try { try {
$this->dbh->query("USE mysql;"); $this->dbh->query("USE mysql;");
} }
catch (PDOException $error) { catch (PDOException $error) {
if ($this->debug) { throw new Exception("error in database: " . $error->getMessage() . "\n" . $sql );
throw new Exception("error connecting to database in order to create: " . $error->getMessage());
}
} }
//create user and set the permissions //create user and set the permissions
@@ -412,9 +404,7 @@ include "root.php";
$this->dbh->query($tmp_sql); $this->dbh->query($tmp_sql);
} }
catch (PDOException $error) { catch (PDOException $error) {
if ($this->debug) { throw new Exception("error in database: " . $error->getMessage() . "\n" . $sql );
print "error: " . $error->getMessage() . "<br/>";
}
} }
//set account to unlimited use //set account to unlimited use
@@ -438,9 +428,7 @@ include "root.php";
} }
} }
catch (PDOException $error) { catch (PDOException $error) {
if ($this->debug) { throw new Exception("error in database: " . $error->getMessage() . "\n" . $sql );
print "error: " . $error->getMessage() . "<br/>";
}
} }
//create the database and set the create user with permissions //create the database and set the create user with permissions
@@ -449,9 +437,7 @@ include "root.php";
$this->dbh->query($tmp_sql); $this->dbh->query($tmp_sql);
} }
catch (PDOException $error) { catch (PDOException $error) {
if ($this->debug) { throw new Exception("error in database: " . $error->getMessage() . "\n" . $sql );
print "error: " . $error->getMessage() . "<br/>";
}
} }
//set user permissions //set user permissions
@@ -459,9 +445,7 @@ include "root.php";
$this->dbh->query("GRANT ALL PRIVILEGES ON ".$this->db_name.".* TO '".$this->db_username."'@'%'; "); $this->dbh->query("GRANT ALL PRIVILEGES ON ".$this->db_name.".* TO '".$this->db_username."'@'%'; ");
} }
catch (PDOException $error) { catch (PDOException $error) {
if ($this->debug) { throw new Exception("error in database: " . $error->getMessage() . "\n" . $sql );
print "error: " . $error->getMessage() . "<br/>";
}
} }
//make the changes active //make the changes active
@@ -470,21 +454,25 @@ include "root.php";
$this->dbh->query($tmp_sql); $this->dbh->query($tmp_sql);
} }
catch (PDOException $error) { catch (PDOException $error) {
if ($this->debug) { throw new Exception("error in database: " . $error->getMessage() . "\n" . $sql );
print "error: " . $error->getMessage() . "<br/>";
}
} }
$this->dbh = null;
} //if (strlen($this->db_create_username) > 0) } //if (strlen($this->db_create_username) > 0)
//select the database //select the database
try {
$this->dbh = new PDO($connect_string, $this->db_username, db_password, array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'));
$this->dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
}
catch (PDOException $error) {
throw new Exception("error connecting to database: " . $error->getMessage() . "\n" . $sql );
}
try { try {
$this->dbh->query("USE ".$this->db_name.";"); $this->dbh->query("USE ".$this->db_name.";");
} }
catch (PDOException $error) { catch (PDOException $error) {
if ($this->debug) { throw new Exception("error in database: " . $error->getMessage() . "\n" . $sql );
print "error: " . $error->getMessage() . "<br/>";
}
} }
//add the database structure //add the database structure
@@ -246,8 +246,6 @@
if($db_create=='1') { echo " checked='checked'"; } if($db_create=='1') { echo " checked='checked'"; }
echo "/>Create the database</label>\n"; echo "/>Create the database</label>\n";
echo " <label class='radio'><input type='checkbox' name='db_create_reuse_auth' value='1'"; echo " <label class='radio'><input type='checkbox' name='db_create_reuse_auth' value='1'";
if($db_create_reuse_auth=='1') { echo " checked='checked'"; }
echo "/>Create database using the username and password above</label>\n";
echo "</td>\n"; echo "</td>\n";
echo "</tr>\n"; echo "</tr>\n";
@@ -259,7 +257,7 @@
echo " <input class='formfld' type='text' name='db_create_username' maxlength='255' value=\"$db_create_username\"><br />\n"; echo " <input class='formfld' type='text' name='db_create_username' maxlength='255' value=\"$db_create_username\"><br />\n";
echo " Optional, this username is used to create the database, a database user and set the permissions. \n"; echo " Optional, this username is used to create the database, a database user and set the permissions. \n";
echo " By default this username is 'pgsql' however it can be any account with permission to add a database, user, and grant permissions. \n"; echo " By default this username is 'pgsql' however it can be any account with permission to add a database, user, and grant permissions. \n";
echo " Leave blank if the user and empty database already exist and you do not want them created. \n"; echo " Leave blank to use the details above. \n";
echo "</td>\n"; echo "</td>\n";
echo "</tr>\n"; echo "</tr>\n";