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 | |
parent | 716c50f2e9b4f1a3f7c1cbacb7010b040f8515dc (diff) | |
download | ampache-a2fa8d3019aba8a7e5eaee90ddf288ffc76bd663.tar.gz ampache-a2fa8d3019aba8a7e5eaee90ddf288ffc76bd663.tar.bz2 ampache-a2fa8d3019aba8a7e5eaee90ddf288ffc76bd663.zip |
More cleanup of install, add DB port option
-rw-r--r-- | bin/install/install_db.inc | 33 | ||||
-rw-r--r-- | config/ampache.cfg.php.dist | 2 | ||||
-rw-r--r-- | install.php | 42 | ||||
-rw-r--r-- | lib/install.lib.php | 132 | ||||
-rw-r--r-- | templates/show_install.inc.php | 20 | ||||
-rw-r--r-- | templates/show_install_config.inc.php | 4 |
6 files changed, 116 insertions, 117 deletions
diff --git a/bin/install/install_db.inc b/bin/install/install_db.inc index 44b0c955..cae0435d 100644 --- a/bin/install/install_db.inc +++ b/bin/install/install_db.inc @@ -33,10 +33,12 @@ require_once $prefix . '/lib/init-tiny.php'; require_once $prefix . '/lib/install.lib.php'; $options = getopt( - 'h:d:f:p:P:u:U:w:', - array( 'database-user:', + 'h:d:fp:P:u:U:w:', + array( + 'database-user:', 'database-password:', 'database-host:', + 'database-port:', 'database-name:', 'ampache-database-user:', 'ampache-database-password:', @@ -45,22 +47,23 @@ $options = getopt( ) ); -$force = ($options['f'] || $options['force']); +$force = isset($options['f']) || isset($options['force']); $db_user = $options['U'] ?: $options['database-user']; $db_pass = $options['P'] ?: $options['database-password']; $db_host = $options['h'] ?: $options['database-host']; +$db_port = $options['database-port']; $db_name = $options['d'] ?: $options['database-name']; $new_db_user = $options['u'] ?: $options['ampache-database-user']; $new_db_pass = $options['p'] ?: $options['ampache-database-password']; -$webpath = $options['w'] ?: $options ['webpath']; +$web_path = $options['w'] ?: $options ['webpath']; // Make sure we have all the required information if (!$db_user || !$db_pass || !$db_host || !$db_name || !$new_db_user || !$new_db_pass) { usage(); } -// Now let's make sure its not already installed +// Now let's make sure it's not already installed if (!install_check_status($configfile)) { echo "\n", T_('Existing Ampache installation found.'), "\n"; if ($force) { @@ -72,15 +75,29 @@ if (!install_check_status($configfile)) { } } +Config::set_by_array(array( + 'web_path' => $web_path, + 'database_name' => $db_name, + 'database_username' => $db_user, + 'database_password' => $db_pass, + 'database_hostname' => $db_host, + 'database_port' => $db_port +), true); + // Install the database -if (!install_insert_db($db_user, $db_pass, $db_host, $db_name, $new_db_user, $new_db_pass)) { +if (!install_insert_db($new_db_user, $new_db_pass, $force)) { echo T_('Database creation failed'), "\n"; echo Error::get('general'), "\n\n"; exit(1); } +Config::set_by_array(array( + 'database_username' => $new_db_user ?: $db_user, + 'database_password' => $new_db_pass ?: $db_pass +), true); + // Write the config file -if (!install_create_config($webpath, $new_db_user, $new_db_pass, $db_host, $db_name)) { +if (!install_create_config()) { echo T_('Config file creation failed'), "\n"; echo Error::get('general') . "\n\n"; exit(1); @@ -98,6 +115,8 @@ function usage() { echo 'MySQL Admin Password'; echo "\n\t-h, --database-host\t\t\t"; echo 'MySQL Hostname'; + echo "\n\t--database-port\t\t\t"; + echo 'MySQL Database Port'; echo "\n\t-d, --database-name\t\t\t"; echo "MySQL Database Name"; echo "\n\t-u, --ampache-database-user\t"; diff --git a/config/ampache.cfg.php.dist b/config/ampache.cfg.php.dist index ff05b6f5..3c86ac6b 100644 --- a/config/ampache.cfg.php.dist +++ b/config/ampache.cfg.php.dist @@ -32,7 +32,7 @@ database_hostname = localhost ; Port to use when connecting to your database ; DEFAULT: none -; database_port = 3306 +;database_port = 3306 ; Name of your ampache database ; DEFAULT: ampache diff --git a/install.php b/install.php index b322d215..c8f21304 100644 --- a/install.php +++ b/install.php @@ -41,6 +41,16 @@ $username = scrub_in($_REQUEST['local_username']); $password = $_REQUEST['local_pass']; $hostname = scrub_in($_REQUEST['local_host']); $database = scrub_in($_REQUEST['local_db']); +$port = scrub_in($_REQUEST['local_port']); + +Config::set_by_array(array( + 'web_path' => $web_path, + 'database_name' => $database, + 'database_username' => $username, + 'database_password' => $password, + 'database_hostname' => $hostname, + 'database_port' => $port +), true); // Charset and gettext setup $htmllang = $_REQUEST['htmllang']; @@ -76,7 +86,17 @@ unset($safe_dirname); switch ($_REQUEST['action']) { case 'create_db': - if (!install_insert_db($username,$password,$hostname,$database)) { + if ($_POST['db_user'] == 'create_db_user') { + $new_user = scrub_in($_POST['db_username']); + $new_pass = $_POST['db_password']; + } + if (!strlen($new_user) || !strlen($new_pass)) { + Error::add('general', T_('Error: Ampache SQL Username or Password missing')); + require_once 'templates/show_install.inc.php'; + break; + } + + if (!install_insert_db($new_user, $new_pass, $_POST['overwrite_db'])) { require_once 'templates/show_install.inc.php'; break; } @@ -84,27 +104,11 @@ switch ($_REQUEST['action']) { // Now that it's inserted save the lang preference Preference::update('lang', '-1', Config::get('lang')); - header ('Location: ' . $web_path . "/install.php?action=show_create_config&local_db=$database&local_host=$hostname&htmllang=$htmllang&charset=$charset"); + header ('Location: ' . $web_path . "/install.php?action=show_create_config&local_db=$database&local_host=$hostname&local_port=$port&htmllang=$htmllang&charset=$charset"); break; case 'create_config': - // Test and make sure that the values they give us actually work - Config::set_by_array(array( - 'database_username' => $username, - 'database_password' => $password, - 'database_hostname' => $hostname - ), true - ); - if (!Dba::check_database()) { - Error::add('config', T_('Error: Unable to make Database Connection: ') . Dba::error()); - } - - // Was download pressed? $download = (!isset($_POST['write'])); - - if (!Error::occurred()) { - $created_config = install_create_config($web_path,$username,$password,$hostname,$database,$download); - } - + $created_config = install_create_config($download); require_once 'templates/show_install_config.inc.php'; break; case 'show_create_config': 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')); diff --git a/templates/show_install.inc.php b/templates/show_install.inc.php index dcbae665..bea5d0a6 100644 --- a/templates/show_install.inc.php +++ b/templates/show_install.inc.php @@ -36,31 +36,35 @@ require $prefix . '/templates/install_header.inc.php'; <form method="post" action="<?php echo $web_path . "/install.php?action=create_db&htmllang=$htmllang&charset=$charset"; ?>" enctype="multipart/form-data" > <table> <tr> - <td class="align"><?php echo T_("Desired Database Name"); ?></td> + <td class="align"><?php echo T_('Desired Database Name'); ?></td> <td><input type="text" name="local_db" value="ampache" /></td> </tr> <tr> - <td class="align"><?php echo T_("MySQL Hostname"); ?></td> + <td class="align"><?php echo T_('MySQL Hostname'); ?></td> <td><input type="text" name="local_host" value="localhost" /></td> </tr> <tr> - <td class="align"><?php echo T_("MySQL Administrative Username"); ?></td> + <td class="align"><?php echo T_('MySQL port (optional)'); ?></td> + <td><input type="text" name="local_port" /></td> +</tr> +<tr> + <td class="align"><?php echo T_('MySQL Administrative Username'); ?></td> <td><input type="text" name="local_username" value="root" /></td> </tr> <tr> - <td class="align"><?php echo T_("MySQL Administrative Password"); ?></td> + <td class="align"><?php echo T_('MySQL Administrative Password'); ?></td> <td><input type="password" name="local_pass" /></td> </tr> <tr> - <td class="align"><?php echo T_("Create Database User for New Database"); ?>? </td> + <td class="align"><?php echo T_('Create Database User for New Database?'); ?></td> <td><input type="checkbox" value="create_db_user" name="db_user" onclick="flipField('db_username');flipField('db_password');" /></td> </tr> <tr> - <td class="align"><?php echo T_("Ampache Database Username"); ?></td> + <td class="align"><?php echo T_('Ampache Database Username'); ?></td> <td><input type="text" id="db_username" name="db_username" value="ampache" /></td> </tr> <tr> - <td class="align"><?php echo T_("Ampache Database User Password"); ?></td> + <td class="align"><?php echo T_('Ampache Database User Password'); ?></td> <td><input type="password" id="db_password" name="db_password" value="" /></td> </tr> <tr> @@ -73,7 +77,7 @@ require $prefix . '/templates/install_header.inc.php'; </tr> <tr> <td> </td> - <td><input type="submit" value="<?php echo T_("Insert Database"); ?>" /></td> + <td><input type="submit" value="<?php echo T_('Insert Database'); ?>" /></td> </tr> </table> </form> diff --git a/templates/show_install_config.inc.php b/templates/show_install_config.inc.php index ced21e38..2df35ee9 100644 --- a/templates/show_install_config.inc.php +++ b/templates/show_install_config.inc.php @@ -52,6 +52,10 @@ require $prefix . '/templates/install_header.inc.php'; <td class="align"><input type="text" name="local_host" value="<?php echo scrub_out($_REQUEST['local_host']); ?>" /></td> </tr> <tr> + <td class="align"><?php echo T_('MySQL port (optional)'); ?></td> + <td><input type="text" name="local_port" value="<?php echo scrub_out($_REQUEST['local_port']);?>" /></td> +</tr> +<tr> <td class="align"><?php echo T_('MySQL Username'); ?></td> <td class="align"><input type="text" name="local_username" value="<?php echo scrub_out($_REQUEST['local_username']); ?>" /></td> </tr> |