diff options
author | Paul Arthur <paul.arthur@flowerysong.com> | 2013-05-13 19:25:04 -0400 |
---|---|---|
committer | Paul Arthur <paul.arthur@flowerysong.com> | 2013-05-13 21:11:38 -0400 |
commit | a2fa8d3019aba8a7e5eaee90ddf288ffc76bd663 (patch) | |
tree | 71fb5f806a04f941794612c1a308004b54561f04 /lib | |
parent | 716c50f2e9b4f1a3f7c1cbacb7010b040f8515dc (diff) | |
download | ampache-a2fa8d3019aba8a7e5eaee90ddf288ffc76bd663.tar.gz ampache-a2fa8d3019aba8a7e5eaee90ddf288ffc76bd663.tar.bz2 ampache-a2fa8d3019aba8a7e5eaee90ddf288ffc76bd663.zip |
More cleanup of install, add DB port option
Diffstat (limited to 'lib')
-rw-r--r-- | lib/install.lib.php | 132 |
1 files changed, 50 insertions, 82 deletions
diff --git a/lib/install.lib.php b/lib/install.lib.php index 1d981d45..eea48e26 100644 --- a/lib/install.lib.php +++ b/lib/install.lib.php @@ -107,28 +107,19 @@ function install_check_status($configfile) { /** * install_insert_db - * this function inserts the database using the username/pass/host provided - * and reading the .sql file + * + * Inserts the database using the values from Config. */ -function install_insert_db($username,$password,$hostname,$database,$dbuser=false,$dbpass=false) { - +function install_insert_db($db_user = null, $db_pass = null, $overwrite = false) { + $database = Config::get('database_name'); // Make sure that the database name is valid - $is_valid = preg_match("/([^\d\w\_\-])/",$database,$matches); + $is_valid = preg_match('/([^\d\w\_\-])/', $database, $matches); if (count($matches)) { - Error::add('general', T_('Error: Database name invalid must not be a reserved word, and must be Alphanumeric')); + Error::add('general', T_('Error: Invalid database name.')); return false; } - $data['database_username'] = $username; - $data['database_password'] = $password; - $data['database_hostname'] = $hostname; - $data['database_name'] = $database; - - Config::set_by_array($data, true); - - unset($data); - if (!Dba::check_database()) { Error::add('general', sprintf(T_('Error: Unable to make Database Connection %s'), Dba::error())); return false; @@ -140,41 +131,33 @@ function install_insert_db($username,$password,$hostname,$database,$dbuser=false if ($db_exists && $_POST['existing_db']) { // Rien a faire, we've got the db just blow through } - elseif ($db_exists && !$_POST['overwrite_db']) { + elseif ($db_exists && !$overwrite) { Error::add('general', T_('Error: Database Already exists and Overwrite not checked')); return false; } elseif (!$db_exists) { - $sql = "CREATE DATABASE `" . Dba::escape($database) . "`"; + $sql = 'CREATE DATABASE `' . $database . '`'; if (!Dba::write($sql)) { Error::add('general',sprintf(T_('Error: Unable to Create Database %s'), Dba::error())); return false; } } // if db can't be selected else { - $sql = "DROP DATABASE `" . Dba::escape($database) . "`"; + $sql = 'DROP DATABASE `' . $database . '`'; Dba::write($sql); - $sql = "CREATE DATABASE `" . Dba::escape($database) . "`"; - if (!Dba::write($sql)) { - Error::add('general', sprintf(T_('Error: Unable to Create Database %s'), Dba::error())); - return false; - } + $sql = 'CREATE DATABASE `' . $database . '`'; + if (!Dba::write($sql)) { + Error::add('general', sprintf(T_('Error: Unable to Create Database %s'), Dba::error())); + return false; + } } // end if selected and overwrite Dba::disconnect(); - /* Check and see if we should create a user here */ - if ($_POST['db_user'] == 'create_db_user' || (strlen($dbuser) AND strlen($dbpass))) { - - $db_user = $_POST['db_username'] ? scrub_in($_POST['db_username']) : $dbuser; - $db_pass = $_POST['db_password'] ? $_POST['db_password'] : $dbpass; - - if (!strlen($db_user) || !strlen($db_pass)) { - Error::add('general', T_('Error: Ampache SQL Username or Password missing')); - return false; - } - $sql = "GRANT ALL PRIVILEGES ON " . Dba::escape($database) . ".* TO " . - "'" . Dba::escape($db_user) . "'@'" . Dba::escape($hostname) . "' IDENTIFIED BY '" . Dba::escape($db_pass) . "' WITH GRANT OPTION"; + // Check to see if we should create a user here + if (strlen($db_user) && strlen($db_pass)) { + $sql = 'GRANT ALL PRIVILEGES ON `' . Dba::escape($database) . '`.* TO ' . + "'" . Dba::escape($db_user) . "'@'" . Dba::escape(Config::get('database_hostname')) . "' IDENTIFIED BY '" . Dba::escape($db_pass) . "' WITH GRANT OPTION"; if (!Dba::write($sql)) { Error::add('general', sprintf(T_('Error: Unable to Insert %1$s with permissions to %2$s on %3$s %4$s'), $db_user, $database, $hostname, Dba::error())); @@ -184,76 +167,62 @@ function install_insert_db($username,$password,$hostname,$database,$dbuser=false $sql_file = Config::get('prefix') . '/sql/ampache.sql'; - /* Attempt to insert database */ - $query = fread(fopen($sql_file, "r"), filesize($sql_file)); - $pieces = split_sql($query); - for ($i=0; $i<count($pieces); $i++) { - $pieces[$i] = trim($pieces[$i]); - if(!empty($pieces[$i]) && $pieces[$i] != "#") { - //FIXME: This is for a DB prefix when we get around to it -// $pieces[$i] = str_replace( "#__", $DBPrefix, $pieces[$i]); - if (!$result = Dba::write($pieces[$i])) { - $errors[] = array ( Dba::error(), $pieces[$i] ); - } // end if - } // end if - } // end for - - $sql = "ALTER DATABASE `" . Dba::escape($database) . "` DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci"; - $db_results = Dba::write($sql); - - // If they've picked something other then English update default preferences + $query = fread(fopen($sql_file, 'r'), filesize($sql_file)); + $pieces = split_sql($query); + for ($i=0; $i<count($pieces); $i++) { + $pieces[$i] = trim($pieces[$i]); + if(!empty($pieces[$i]) && $pieces[$i] != '#') { + if (!$result = Dba::write($pieces[$i])) { + $errors[] = array ( Dba::error(), $pieces[$i] ); + } + } + } + + $sql = 'ALTER DATABASE `' . $database . '` DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci'; + $db_results = Dba::write($sql, array(Config::get('database_name'))); + + // If they've picked something other than English update default preferences if (Config::get('lang') != 'en_US') { - $sql = "UPDATE `preference` SET `value`='" . Config::get('lang') . "' WHERE `id`=31"; - $db_results = Dba::write($sql); - $sql = "UPDATE `user_preference` SET `value`='" .Config::get('lang') ."' WHERE `preference`=31"; - $db_results = Dba::write($sql); + // FIXME: 31? I hate magic. + $sql = 'UPDATE `preference` SET `value`= ? WHERE `id` = 31'; + $db_results = Dba::write($sql, array(Config::get('lang'))); + $sql = 'UPDATE `user_preference` SET `value` = ? WHERE `preference` = 31'; + $db_results = Dba::write($sql, array(Config::get('lang'))); } return true; - -} // install_insert_db +} /** * install_create_config - * attempts to write out the config file or offer it as a download + * + * Attempts to write out the config file or offer it as a download. */ -function install_create_config($web_path,$username,$password,$hostname,$database,$download) { +function install_create_config($download = false) { $config_file = Config::get('prefix') . '/config/ampache.cfg.php'; - $data['database_username'] = $username; - $data['database_password'] = $password; - $data['database_hostname'] = $hostname; - $data['database_name'] = $database; - $data['web_path'] = $web_path; - - Config::set_by_array($data, true); - - /* Attempt to make DB connection */ - $dbh = Dba::dbh(); + /* Attempt to make DB connection */ + $dbh = Dba::dbh(); - /* - First Test The Variables they've given us to make - sure that they actually work! - */ // Connect to the DB if(!Dba::check_database()) { Error::add('general', T_("Database Connection Failed Check Hostname, Username and Password")); return false; } - $final = generate_config($data); + $final = generate_config(Config::get_all()); // Make sure the directory is writable OR the empty config file is if (!$download) { if (!check_config_writable()) { - Error::add('general', T_("Config file is not writable")); + Error::add('general', T_('Config file is not writable')); return false; } else { // Given that $final is > 0, we can ignore lazy comparison problems - if (!file_put_contents($config_file,$final)) { - Error::add('general', T_("Error Writing config file")); + if (!file_put_contents($config_file, $final)) { + Error::add('general', T_('Error writing config file')); return false; } } @@ -266,14 +235,13 @@ function install_create_config($web_path,$username,$password,$hostname,$database } return true; - -} // install_create_config +} /** * install_create_account * this creates your initial account and sets up the preferences for the -1 user and you */ -function install_create_account($username,$password,$password2) { +function install_create_account($username, $password, $password2) { if (!strlen($username) OR !strlen($password)) { Error::add('general', T_('No Username/Password specified')); |