diff options
Diffstat (limited to 'installer/installer.php')
-rw-r--r-- | installer/installer.php | 195 |
1 files changed, 35 insertions, 160 deletions
diff --git a/installer/installer.php b/installer/installer.php index 2da3cd02..cf4f41d1 100644 --- a/installer/installer.php +++ b/installer/installer.php @@ -18,131 +18,20 @@ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ class installer { - static function command_line() { - // Set error handlers - set_error_handler(create_function('$errno, $errstr, $errfile, $errline', - 'throw new ErrorException($errstr, 0, $errno, $errfile, $errline);')); - set_exception_handler(array("installer", "print_exception")); - - if (self::already_installed()) { - print "Gallery 3 is already installed.\n"; - return; - } - - $config = self::parse_cli_params(); - try { - self::setup_database($config); - self::setup_var(); - self::install($config); - list ($user, $password) = self::create_admin($config); - print "Successfully installed your Gallery 3!\n"; - print "Log in with:\n username: admin\n password: $password\n"; - } catch (InstallException $e) { - self::display_errors($e->errors()); - } - } - - static function web() { - if (self::already_installed()) { - $state = "already_installed"; - include("install.html.php"); - return; - } - - switch ($step = $_GET["step"]) { - default: - $step = "welcome"; - break; - - case "get_info": - break; - - case "save_info": - $config = array("host" => $_GET["dbhost"], - "user" => $_GET["dbuser"], - "password" => $_GET["dbpass"], - "dbname" => $_GET["dbname"], - "prefix" => "", - "type" => function_exists("mysqli_init") ? "mysqli" : "mysql"); - try { - self::setup_database($config); - self::setup_var(); - self::install($config); - list ($user, $password) = self::create_admin($config); - } catch (Exception $e) { - $step = "database_failure"; - } - break; - } - - include("install.html.php"); - } - static function already_installed() { return file_exists(VARPATH . "database.php"); } - static function parse_cli_params() { - $config = array("host" => "localhost", - "user" => "root", - "password" => "", - "dbname" => "gallery3", - "prefix" => ""); - - if (function_exists("mysqli_init")) { - $config["type"] = "mysqli"; - } else { - $config["type"] = "mysql"; + static function var_writable() { + if (is_writable(VARPATH)) { + return true; } - $argv = $_SERVER["argv"]; - for ($i = 1; $i < count($argv); $i++) { - switch (strtolower($argv[$i])) { - case "-d": - $config["dbname"] = $argv[++$i]; - break; - case "-h": - $config["host"] = $argv[++$i]; - break; - case "-u": - $config["user"] = $argv[++$i]; - break; - case "-p": - $config["password"] = $argv[++$i]; - break; - } + if (@mkdir(VARPATH)) { + return true; } - return $config; - } - - static function setup_database($config) { - $errors = array(); - if (!mysql_connect($config["host"], $config["user"], $config["password"])) { - $errors["Database"] = - "Unable to connect to your database with the credentials provided. Error details:\n" . - mysql_error(); - return; - } - - if (!mysql_select_db($config["dbname"])) { - if (!(mysql_query("CREATE DATABASE {$config['dbname']}") && - mysql_select_db($config["dbname"]))) { - $errors["Database"] = sprintf( - "Database '%s' is not defined and can't be created", - $config["dbname"]); - } - } - - if (empty($errors) && mysql_num_rows(mysql_query("SHOW TABLES FROM {$config['dbname']}"))) { - $errors["Database"] = sprintf( - "Database '%s' exists and has tables in it, continuing may overwrite an existing install", - $config["dbname"]); - } - - if ($errors) { - throw new InstallException($errors); - } + return false; } static function setup_var() { @@ -161,36 +50,48 @@ class installer { } } - static function install($config) { - $errors = array(); + static function create_database_config($config) { + $db_config_file = VARPATH . "database.php"; + ob_start(); + extract($config); + include(DOCROOT . "installer/database_config.php"); + $output = ob_get_clean(); + return file_put_contents($db_config_file, $output) !== false; + } + static function unpack_var() { include(DOCROOT . "installer/init_var.php"); + return true; + } - $buf = ""; - foreach (file("installer/install.sql") as $line) { + static function unpack_sql() { + foreach (file(DOCROOT . "installer/install.sql") as $line) { $buf .= $line; if (preg_match("/;$/", $buf)) { if (!mysql_query($buf)) { - throw new InstallException( - array("Database" => "Unable to install database tables. Error details:\n" . - mysql_error())); - break; + return false; } $buf = ""; } } + return true; + } - $db_config_file = VARPATH . "database.php"; - ob_start(); - extract($config); - include("installer/database_config.php"); - $output = ob_get_clean(); + static function connect($config) { + return mysql_connect($config["host"], $config["user"], $config["password"]); + } - if (!file_put_contents($db_config_file, $output) !== false) { - throw new InstallException(array("Config" => "Unable to create " . VARPATH . "database.php")); + static function select_db($config) { + if (mysql_select_db($config["dbname"])) { + return true; } - system("chmod -R 777 " . VARPATH); + return mysql_query("CREATE DATABASE {$config['dbname']}") && + mysql_select_db($config["dbname"]); + } + + static function db_empty($config) { + return mysql_num_rows(mysql_query("SHOW TABLES FROM {$config['dbname']}")) == 0; } static function create_admin($config) { @@ -214,30 +115,4 @@ class installer { return array("admin", $password); } - - static function print_exception($exception) { - print $exception->getMessage() . "\n"; - print $exception->getTraceAsString(); - } - - static function display_errors($errors) { - print "Errors\n"; - foreach ($errors as $title => $error) { - print "$title\n"; - print " $error\n\n"; - } - } -} - -class InstallException extends Exception { - var $errors; - - function __construct($errors) { - parent::__construct(); - $this->errors = $errors; - } - - function errors() { - return $this->errors; - } -} +}
\ No newline at end of file |