diff options
author | Tim Almdal <tnalmdal@shaw.ca> | 2009-01-14 03:38:51 +0000 |
---|---|---|
committer | Tim Almdal <tnalmdal@shaw.ca> | 2009-01-14 03:38:51 +0000 |
commit | 02af2d8b7639fdc18fba3d69a4ca0a3f5c92b948 (patch) | |
tree | ab8a0ebaa6c6223f8ae3b9717d9a3942d0980b91 | |
parent | 383ec9c22cde58a15d48ddad18377d4f99348e91 (diff) |
The installer. At this point, the core installs, the user module defines groups and users, but dies when attempting to create default permissions
-rw-r--r-- | index.php | 4 | ||||
-rw-r--r-- | installer/helpers/installer.php | 233 | ||||
-rw-r--r-- | installer/install.php | 27 | ||||
-rw-r--r-- | installer/views/database.php.php | 44 |
4 files changed, 276 insertions, 32 deletions
@@ -45,9 +45,9 @@ define('MODPATH', strtr(realpath('modules') . '/', DIRECTORY_SEPARATOR, '/')); define('THEMEPATH', strtr(realpath('themes') . '/', DIRECTORY_SEPARATOR, '/')); define('SYSPATH', strtr(realpath('kohana') . '/', DIRECTORY_SEPARATOR, '/')); -if (!file_exists('var')) { +if (!file_exists('var/installed')) { // Run the installer - header("Location: installer/install.php"); + header("Location: installer/index.php"); } // Force a test run if we're in command line mode. diff --git a/installer/helpers/installer.php b/installer/helpers/installer.php index 640e4335..5eaaa79d 100644 --- a/installer/helpers/installer.php +++ b/installer/helpers/installer.php @@ -237,7 +237,7 @@ class installer { self::$config[$key] = $value; $class = "Install_{$value}_Driver"; if (!class_exists($class)) { - require_once(DOCROOT . "/installer/libraries/$class" . EXT); + require_once(DOCROOT . "installer/libraries/$class" . EXT); } } if ($key == "modules") { @@ -322,30 +322,231 @@ class installer { "error" => true); } self::$messages[] = $section; - return !$writable; + return $writable; } - - private static function _render($view) { + + public static function setup_kohana() { + define('KOHANA_VERSION', '2.3'); + define('KOHANA_CODENAME', 'accipiter'); + + // Test of Kohana is running in Windows + define('KOHANA_IS_WIN', DIRECTORY_SEPARATOR === '\\'); + + // Load core files + require SYSPATH.'core/utf8'.EXT; + require SYSPATH.'core/Event'.EXT; + require SYSPATH.'core/Kohana'.EXT; + + // Define Kohana error constant + define('E_KOHANA', 42); + + // Define 404 error constant + define('E_PAGE_NOT_FOUND', 43); + + // Define database error constant + define('E_DATABASE_ERROR', 44); + + // Set autoloader + spl_autoload_register(array('Kohana', 'auto_load')); + + Kohana::config("locale.language"); + } + + public static function install() { + ob_start(); + $step = 0; + $modules[] = array(); + try { + while ($step >= 0) { + switch ($step) { + case 0: + if (file_exists("var")) { + $step = 1; + } else if (mkdir("var", 0774)) { + print "'var' directory created\n"; + $step = 1; + } else { + $step = -1; + print "'var' directory was not created\n"; + } + break; + case 1: + $db_config = realpath("var/database.php"); + $data = array("type" => strtolower(self::$config["type"]), + "user" => self::$config["user"], + "password" => self::$config["password"], + "host" => self::$config["host"], + "database" => self::$config["dbname"], + "prefix" => self::$config["prefix"]); + + $config = self::_render("installer/views/database.php", $data); + if (file_put_contents($db_config, $config) !== false) { + print "'var/database.php' created\n"; + $step = 2; + } else { + print "'var/database.php' was not created\n"; + $step = -1; + } + break; + case 2: + foreach (array_keys(self::$config["modules"]) as $module_name) { + self::_module_install($module_name); + $version = module::get_version($module_name); + $modules[] = "$module_name: $version"; + } + $step = 3; + break; + case 3: + if (file_put_contents("var/installed", implode("\n", $modules))) { + print "Gallery3 installed\n"; + } else { + print "Unable to write 'var/installed'"; + } + $step = -1; + } + } + + } catch (Exception $e) { + self::print_exception($e); + } + $return = ob_get_contents(); + ob_clean(); + return $return; + } + + public static function print_exception($exception) { + // Beautify backtrace + try { + $trace = self::_backtrace($exception); + } catch(Exception $e) { + print_r($e); + } + + $type = get_class($exception); + $message = $exception->getMessage(); + $file = $exception->getFile(); + $line = $exception->getLine(); + + print "$type Occurred: $message \nin {$file}[$line]\n$trace"; + // Turn off error reporting + error_reporting(0); + } + + /** + * Install a module. + */ + private static function _module_install($module_name) { + $installer_class = "{$module_name}_installer"; + print "$installer_class install (initial)\n"; + if ($module_name != "core") { + require_once(DOCROOT . "modules/${module_name}/helpers/{$installer_class}.php"); + } else { + require_once(DOCROOT . "core/helpers/core_installer.php"); + } + + $core_config = Kohana::config_load("core"); + $kohana_modules = $core_config["modules"]; + $kohana_modules[] = MODPATH . $module_name; + Kohana::config_set("core.modules", $kohana_modules); + + + call_user_func(array($installer_class, "install")); + + //if (method_exists($installer_class, "install")) { + // call_user_func_array(array($installer_class, "install"), array()); + //} + print "Installed module $module_name\n"; + } + + private static function _render($view, $data=null) { if ($view == '') return; // Buffering on ob_start(); - try - { - // Views are straight HTML pages with embedded PHP, so importing them - // this way insures that $this can be accessed as if the user was in - // the controller, which gives the easiest access to libraries in views - include realpath($view . EXT); - } - catch (Exception $e) - { - // Display the exception using its internal __toString method - echo $e; - } + try { + // Views are straight HTML pages with embedded PHP, so importing them + // this way insures that $this can be accessed as if the user was in + // the controller, which gives the easiest access to libraries in views + include realpath($view . EXT); + } catch (Exception $e) { + // Display the exception using its internal __toString method + echo $e; + } // Fetch the output and close the buffer return ob_get_clean(); } + + /** + * Displays nice backtrace information. + * @see http://php.net/debug_backtrace + * + * @param array backtrace generated by an exception or debug_backtrace + * @return string + */ + public static function _backtrace($exception) { + $trace = $exception->getTrace(); + if ( ! is_array($trace)) { + return; + } + + // Final output + $output = array(); + $cli = PHP_SAPI == "cli"; + + $args = array(); + // Remove the first entry of debug_backtrace(), it is the exception_handler call + if ($exception instanceof ErrorException) { + $last = array_shift($trace); + $args = !empty($last["args"]) ? $last["args"] : $args; + } + + foreach ($trace as $entry) { + $temp = $cli ? "" : "<li>"; + + if (isset($entry["file"])) { + $format = $cli ? "%s[%s]" : "<tt>%s <strong>[%s]:</strong></tt>"; + $temp .= sprintf($format, preg_replace("!^".preg_quote(DOCROOT)."!", "", + $entry["file"]), $entry["line"]); + } + + $temp .= $cli ? "\n\t" : "<pre>"; + + if (isset($entry["class"])) { + // Add class and call type + $temp .= $entry["class"].$entry["type"]; + } + + // Add function + $temp .= $entry["function"]."("; + + // Add function args + if (isset($entry["args"]) AND is_array($entry["args"])) { + // Separator starts as nothing + $sep = ""; + + while ($arg = array_shift($args)) { + if (is_string($arg) AND is_file($arg)) { + // Remove docroot from filename + $arg = preg_replace("!^".preg_quote(DOCROOT)."!", "", $arg); + } + + $temp .= $sep . ($cli ? print_r($arg, TRUE) : html::specialchars(print_r($arg, TRUE))); + + // Change separator to a comma + $sep = ", "; + } + $args = $entry["args"]; + } + + $temp .= ")" . ($cli ? "\n" : "</pre></li>"); + + $output[] = $temp; + } + + $output = implode("\n", $output); + return $cli ? $output : "<ul class=\"backtrace\">" . $output . "</ul>"; + } }
\ No newline at end of file diff --git a/installer/install.php b/installer/install.php index 9802bbd2..2cf5d76d 100644 --- a/installer/install.php +++ b/installer/install.php @@ -36,46 +36,43 @@ */ function exception_handler($exception) { - $code = $exception->getCode(); - $type = get_class($exception); - $message = $exception->getMessage(); - $file = $exception->getFile(); - $line = $exception->getLine(); - - var_dump($exception); - // Turn off error reporting - error_reporting(0); + installer::print_exception($exception); exit; } -if (PHP_SAPI != 'cli') { +if (PHP_SAPI != "cli") { $redirect = str_replace("install.php", "index.php", $_SERVER["REQUEST_URI"]); header("Location: $redirect"); return; } -if (file_exists('var')) { +if (file_exists("var/installed")) { dir("Gallery3 is already installed... exiting"); } array_shift($argv); // remove the script name from the arguments -define("DOCROOT", dirname(dirname(__FILE__))); +define("DOCROOT", dirname(dirname(__FILE__)) . "/"); chdir(DOCROOT); define('APPPATH', strtr(realpath('core') . '/', DIRECTORY_SEPARATOR, '/')); define('MODPATH', strtr(realpath('modules') . '/', DIRECTORY_SEPARATOR, '/')); define('THEMEPATH', strtr(realpath('themes') . '/', DIRECTORY_SEPARATOR, '/')); define('SYSPATH', strtr(realpath('kohana') . '/', DIRECTORY_SEPARATOR, '/')); +define('VARPATH', strtr(realpath('var') . '/', DIRECTORY_SEPARATOR, '/')); +define('TEST_MODE', 0); define('EXT', ".php"); +$_SERVER["HTTP_USER_AGENT"] = phpversion(); +date_default_timezone_set('America/Los_Angeles'); + set_error_handler(create_function('$errno, $errstr, $errfile, $errline', 'throw new ErrorException($errstr, 0, $errno, $errfile, $errline);')); // Set exception handler set_exception_handler('exception_handler'); -include DOCROOT . "/installer/helpers/installer.php"; +include DOCROOT . "installer/helpers/installer.php"; // @todo Log the results of failed call if (!installer::environment_check()) { @@ -90,6 +87,7 @@ $config_valid = true; try { $config_valid = installer::check_database_authorization(); } catch (Exception $e) { + installer::print_exception($e); die("Specifed User does not have sufficient authority to install Gallery3\n"); } @@ -98,7 +96,8 @@ $config_valid &= installer::check_docroot_writable(); installer::display_requirements(!$config_valid); if ($config_valid) { - // @todo do the install + installer::setup_kohana(); + print installer::install(); } diff --git a/installer/views/database.php.php b/installer/views/database.php.php new file mode 100644 index 00000000..42268422 --- /dev/null +++ b/installer/views/database.php.php @@ -0,0 +1,44 @@ +<?php defined('SYSPATH') OR die('No direct access allowed.'); +print "<?php defined('SYSPATH') OR die('No direct access allowed.');\n"; +print "/**\n"; +print " * @package Database\n"; +print " *\n"; +print " * Database connection settings, defined as arrays, or \"groups\". If no group\n"; +print " * name is used when loading the database library, the group named \"default\"\n"; +print " * will be used.\n"; +print " *\n"; +print " * Each group can be connected to independently, and multiple groups can be\n"; +print " * connected at once.\n"; +print " *\n"; +print " * Group Options:\n"; +print " * benchmark - Enable or disable database benchmarking\n"; +print " * persistent - Enable or disable a persistent connection\n"; +print " * connection - Array of connection specific parameters; alternatively,\n"; +print " * you can use a DSN though it is not as fast and certain\n"; +print " * characters could create problems (like an '@' character\n"; +print " * in a password):\n"; +print " * 'connection' => 'mysql://dbuser:secret@localhost/kohana'\n"; +print " * character_set - Database character set\n"; +print " * table_prefix - Database table prefix\n"; +print " * object - Enable or disable object results\n"; +print " * cache - Enable or disable query caching\n"; +print " * escape - Enable automatic query builder escaping\n"; +print " */\n"; +print "\$config['default'] = array(\n"; +print " 'benchmark' => FALSE,\n"; +print " 'persistent' => FALSE,\n"; +print " 'connection' => array(\n"; +print " 'type' => '{$data['type']}',\n"; +print " 'user' => '{$data['user']}',\n"; +print " 'pass' => '{$data['password']}',\n"; +print " 'host' => '{$data['host']}',\n"; +print " 'port' => FALSE,\n"; +print " 'socket' => FALSE,\n"; +print " 'database' => '{$data['database']}'\n"; +print " ),\n"; +print " 'character_set' => 'utf8',\n"; +print " 'table_prefix' => '{$data['prefix']}',\n"; +print " 'object' => TRUE,\n"; +print " 'cache' => FALSE,\n"; +print " 'escape' => TRUE\n"; +print ");\n"; |