Fixes for the installer. Display error messages on failures, as well as actually check for tables before installing.

This commit is contained in:
jgen 2013-11-28 00:31:09 -05:00
parent 9430ddc374
commit bf4d434c86
2 changed files with 94 additions and 16 deletions

View file

@ -470,6 +470,8 @@ class Database {
* Returns the number of tables present in the current database. * Returns the number of tables present in the current database.
*/ */
public function count_tables() { public function count_tables() {
if(is_null($this->db) || is_null($this->engine)) $this->connect_db();
if($this->engine->name === "mysql") { if($this->engine->name === "mysql") {
return count( return count(
$this->get_all("SHOW TABLES") $this->get_all("SHOW TABLES")

View file

@ -1,8 +1,14 @@
<?php ob_start(); ?> <?php
// TODO: Rewrite the entire installer and make it more readable.
ob_start();
?>
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<!-- <!--
- install.php (c) Shish et all. 2007-2012 - install.php (c) Shish et all. 2007-2013
- -
- Initialise the database, check that folder - Initialise the database, check that folder
- permissions are set properly. - permissions are set properly.
@ -14,7 +20,7 @@
<title>Shimmie Installation</title> <title>Shimmie Installation</title>
<link rel="shortcut icon" href="favicon.ico" /> <link rel="shortcut icon" href="favicon.ico" />
<link rel='stylesheet' href='lib/shimmie.css' type='text/css'> <link rel='stylesheet' href='lib/shimmie.css' type='text/css'>
<script src="lib/jquery-1.7.1.min.js"></script> <script type="text/javascript" src="lib/jquery-1.7.1.min.js"></script>
</head> </head>
<body> <body>
<?php if(false) { ?> <?php if(false) { ?>
@ -48,11 +54,12 @@ require_once __SHIMMIE_ROOT__."core/util.inc.php";
require_once __SHIMMIE_ROOT__."core/exceptions.class.php"; require_once __SHIMMIE_ROOT__."core/exceptions.class.php";
require_once __SHIMMIE_ROOT__."core/database.class.php"; require_once __SHIMMIE_ROOT__."core/database.class.php";
if(is_readable("data/config/shimmie.conf.php")) die("Already installed"); if(is_readable("data/config/shimmie.conf.php")) die("Shimmie is already installed.");
do_install(); do_install();
// utilities {{{ // utilities {{{
// TODO: Can some of these be pushed into "core/util.inc.php" ?
/** /**
* Strips off any kind of slash at the end so as to normalise the path. * Strips off any kind of slash at the end so as to normalise the path.
@ -99,6 +106,7 @@ function eok($name, $value) {
} }
} }
// }}} // }}}
function do_install() { // {{{ function do_install() { // {{{
if(file_exists("data/config/auto_install.conf.php")) { if(file_exists("data/config/auto_install.conf.php")) {
require_once "data/config/auto_install.conf.php"; require_once "data/config/auto_install.conf.php";
@ -116,6 +124,7 @@ function do_install() { // {{{
ask_questions(); ask_questions();
} }
} // }}} } // }}}
function ask_questions() { // {{{ function ask_questions() { // {{{
$warnings = array(); $warnings = array();
$errors = array(); $errors = array();
@ -138,6 +147,7 @@ function ask_questions() { // {{{
if( if(
!in_array("mysql", $drivers) && !in_array("mysql", $drivers) &&
!in_array("pgsql", $drivers) && !in_array("pgsql", $drivers) &&
!in_array("sqlite", $drivers) !in_array("sqlite", $drivers)
) { ) {
$errors[] = " $errors[] = "
@ -221,6 +231,10 @@ function ask_questions() { // {{{
</div> </div>
EOD; EOD;
} // }}} } // }}}
/**
* This is where the install really takes place.
*/
function install_process() { // {{{ function install_process() { // {{{
build_dirs(); build_dirs();
create_tables(); create_tables();
@ -229,15 +243,21 @@ function install_process() { // {{{
header("Location: index.php"); header("Location: index.php");
} // }}} } // }}}
function create_tables() { // {{{ function create_tables() { // {{{
try { try {
$db = new Database(); $db = new Database();
if ( $db->count_tables() > 0 ) { if ( $db->count_tables() > 0 ) {
echo " print <<<EOD
<p>Warning: The Database schema is not empty!</p> <div id="installer">
<h1>Shimmie Installer</h1>
<h3>Warning: The Database schema is not empty!</h3>
<p>Please ensure that the database you are installing Shimmie with is empty before continuing.</p> <p>Please ensure that the database you are installing Shimmie with is empty before continuing.</p>
<p>Once you have emptied the database of any tables, please hit 'refresh' to continue.</p>"; <p>Once you have emptied the database of any tables, please hit 'refresh' to continue.</p>
<br/><br/>
</div>
EOD;
exit; exit;
} }
@ -296,11 +316,35 @@ function create_tables() { // {{{
$db->execute("INSERT INTO config(name, value) VALUES('db_version', 11)"); $db->execute("INSERT INTO config(name, value) VALUES('db_version', 11)");
$db->commit(); $db->commit();
} }
catch(PDOException $e) { catch(PDOException $e)
// FIXME: Make the error message user friendly {
print <<<EOD
<div id="installer">
<h1>Shimmie Installer</h1>
<h3>Database Error:</h3>
<p>An error occured while trying to create the database tables necessary for Shimmie.</p>
<p>Please check and ensure that the database configuration options are all correct.</p>
<br/><br/>
</div>
EOD;
exit($e->getMessage()); exit($e->getMessage());
} }
catch (Exception $e)
{
print <<<EOD
<div id="installer">
<h1>Shimmie Installer</h1>
<h3>Unknown Error:</h3>
<p>An unknown error occured while trying to create the database tables necessary for Shimmie.</p>
<p>Please check the server log files for more information.</p>
<br/><br/>
</div>
EOD;
exit($e->getMessage());
}
} // }}} } // }}}
function insert_defaults() { // {{{ function insert_defaults() { // {{{
try { try {
$db = new Database(); $db = new Database();
@ -313,11 +357,34 @@ function insert_defaults() { // {{{
} }
$db->commit(); $db->commit();
} }
catch(PDOException $e) { catch(PDOException $e)
// FIXME: Make the error message user friendly {
print <<<EOD
<div id="installer">
<h1>Shimmie Installer</h1>
<h3>Database Error:</h3>
<p>An error occured while trying to insert data into the database.</p>
<p>Please check and ensure that the database configuration options are all correct.</p>
<br/><br/>
</div>
EOD;
exit($e->getMessage());
}
catch (Exception $e)
{
print <<<EOD
<div id="installer">
<h1>Shimmie Installer</h1>
<h3>Unknown Error:</h3>
<p>An unknown error occured while trying to insert data into the database.</p>
<p>Please check the server log files for more information.</p>
<br/><br/>
</div>
EOD;
exit($e->getMessage()); exit($e->getMessage());
} }
} // }}} } // }}}
function build_dirs() { // {{{ function build_dirs() { // {{{
// *try* and make default dirs. Ignore any errors -- // *try* and make default dirs. Ignore any errors --
// if something is amiss, we'll tell the user later // if something is amiss, we'll tell the user later
@ -333,16 +400,20 @@ function build_dirs() { // {{{
!is_writable("images") || !is_writable("thumbs") || !is_writable("data") !is_writable("images") || !is_writable("thumbs") || !is_writable("data")
) { ) {
print " print "
<p>Shimmie needs three folders in it's directory, 'images', 'thumbs', and 'data', <div id='installer'>
and they need to be writable by the PHP user.</p> <h1>Shimmie Installer</h1>
<p>If you see this error, if probably means the folders are owned by you, and they need to be <h3>Directory Permissions Error:</h3>
writable by the web server.</p> <p>Shimmie needs to make three folders in it's directory, '<i>images</i>', '<i>thumbs</i>', and '<i>data</i>', and they need to be writable by the PHP user.</p>
<p>If you see this error, if probably means the folders are owned by you, and they need to be writable by the web server.</p>
<p>PHP reports that it is currently running as user: ".$_ENV["USER"]." (". $_SERVER["USER"] .")</p> <p>PHP reports that it is currently running as user: ".$_ENV["USER"]." (". $_SERVER["USER"] .")</p>
<p>Once you have created these folders and / or changed the ownership of the shimmie folder, hit 'refresh' to continue.</p> <p>Once you have created these folders and / or changed the ownership of the shimmie folder, hit 'refresh' to continue.</p>
<br/><br/>
</div>
"; ";
exit; exit;
} }
} // }}} } // }}}
function write_config() { // {{{ function write_config() { // {{{
$file_content = '<' . '?php' . "\n" . $file_content = '<' . '?php' . "\n" .
"define('DATABASE_DSN', '".DATABASE_DSN."');\n" . "define('DATABASE_DSN', '".DATABASE_DSN."');\n" .
@ -355,6 +426,9 @@ function write_config() { // {{{
if(!file_put_contents("data/config/shimmie.conf.php", $file_content)) { if(!file_put_contents("data/config/shimmie.conf.php", $file_content)) {
$h_file_content = htmlentities($file_content); $h_file_content = htmlentities($file_content);
print <<<EOD print <<<EOD
<div id="installer">
<h1>Shimmie Installer</h1>
<h3>File Permissions Error:</h3>
The web server isn't allowed to write to the config file; please copy The web server isn't allowed to write to the config file; please copy
the text below, save it as 'data/config/shimmie.conf.php', and upload it into the shimmie the text below, save it as 'data/config/shimmie.conf.php', and upload it into the shimmie
folder manually. Make sure that when you save it, there is no whitespace folder manually. Make sure that when you save it, there is no whitespace
@ -362,7 +436,9 @@ function write_config() { // {{{
<p><textarea cols="80" rows="2">$file_content</textarea> <p><textarea cols="80" rows="2">$file_content</textarea>
<p>One done, <a href='index.php'>Continue</a> <p>Once done, <a href="index.php">Continue</a>
<br/><br/>
</div>
EOD; EOD;
exit; exit;
} }