function run_scripts() { global $base_url; ?> <h1>Almost there!</h1> <p>All the files have been updated. Now you need to <b><a href="run_scripts.php?return=<?=$base_url?>/homepage.php?>">click here to update the database</a></b> and the update will be complete.</p> <?php return; // no longer done - too many interactions with old/new Dal code, internationalization etc global $path_prefix; try { include "$path_prefix/web/update/run_scripts.php"; run_update_scripts(); } catch (Exception $e) { echo "<p>Error occurred running database update: ".$e->getMessage()."</p>"; } }
private function dbTest() { global $peepagg_dsn; $params = $this->test_data; if (empty($params['db_user'])) { $this->note("You must supply a database user name", 'error'); return false; } if (empty($params['db_name'])) { $this->note("You must supply a database name", 'error'); return false; } if (empty($params['db_host'])) { $this->note("You must supply a host name", 'error'); return false; } if (empty($params['db_password'])) { $this->note("You may not use a blank password for the MySQL connection", 'error'); return false; } $user_link = @mysql_connect($params['db_host'], $params['db_user'], $params['db_password']); // If the installer wishes to use an existing db instead of creating one. $user_db = FALSE; if ($user_link) { $this->note("Able to connect to the MySQL server at <code>{$params['db_host']}</code> with supplied login details.", 'ok'); // make sure the DB isn't already populated if (!mysql_select_db($params['db_name'], $user_link)) { mysql_close($user_link); $user_link = FALSE; if (!empty($params['mysql_root_username']) && !empty($params['mysql_root_password'])) { // if we have root credentials we will try to create database - so, no exit yet! $this->note("Database <code>{$params['db_name']}</code> does not exist or is inaccessible.", 'info'); } else { $this->note("Database <code>{$params['db_name']}</code> does not exist or is inaccessible.", 'error'); return false; } } else { $sth = $this->run_query("SHOW TABLES", $user_link); if (mysql_num_rows($sth)) { if ($params['create_db'] == "true") { $this->note("The database <code>{$params['db_name']}</code> already contains data. Please wipe it out or recreate the database before installing PeopleAggregator.", 'error'); return false; } else { $this->note("Found database with data. Assuming it is an existing PA install...", "ok"); $user_db = TRUE; } } } } else { if (!empty($params['mysql_root_username'])) { // if we have root credentials we will try to create database - so, no exit yet! $this->note("It is recommended to have a password on the root user's account", 'warn'); } else { $this->note("Unable to connect to the MySQL server using the supplied login details", 'error'); return false; } } if (!$user_link && !$user_db && !empty($params['mysql_root_username'])) { $this->note("Trying administrator login..."); $admin_link = @mysql_connect($params['db_host'], $params['mysql_root_username'], $params['mysql_root_password']); if (!$admin_link) { $this->note("Unable to connect to the MySQL server with the supplied login details or as an administrator", 'error'); return false; } else { $this->note("Able to connect to the MySQL server with the supplied administrator login details - a new database will be created.", 'ok'); } // make sure the db doesn't already exist if (mysql_select_db($params['db_name'], $admin_link)) { $this->note("The database <code>{$params['db_name']}</code> already exists. Please, choose a different name for a new database.", 'error'); return false; } // create it $sql = "CREATE DATABASE " . $this->db_esc($params['db_name']); // $rollback_cmds[] = array("sql", "DROP DATABASE ".$this->db_esc($params['db_name']), $admin_link); $this->run_query($sql, $admin_link); // now grant permissions with successively looser hostnames until // we find one that lets the web server access the database. foreach (array("localhost", "localhost.localdomain", $_SERVER['SERVER_NAME'], gethostbyname($_SERVER['SERVER_NAME']), "%") as $server_host) { $sql = "GRANT ALL ON " . $this->db_esc($params['db_name']) . ".* TO " . $this->db_esc($params['db_user']) . "@" . $this->db_esc($server_host) . " IDENTIFIED BY '" . mysql_real_escape_string($params['db_password']) . "'"; $this->run_query($sql, $admin_link); if (($user_link = mysql_connect($params['db_host'], $params['db_user'], $params['db_password'])) && mysql_select_db($params['db_name'], $user_link)) { $this->note("Successfully logged in to new database using credentials from host {$server_host}", 'ok'); break; } } if (!$user_link) { $this->note("Failed to grant access credentials that would allow the web server to access the database. Please try creating the database manually.", 'error'); return false; } } if (!$user_link) { $this->note("Something went wrong - we should have successfully connected to the DB by now", 'error'); return false; } if (!$user_db) { $this->note("The database was successfully created.", 'ok'); // now set up databases $this->note("Initializing database ... ", 'info'); // Parag Jagdale - 10/16/10: Add ability to specify seed file that contains data to insert if (isset($params['insert_seed_data'])) { // use the PeepAgg structure and seed files if ($sql_file = getShadowedPath("web/install/PeepAgg.structure.mysql")) { if ($this->run_query_file($sql_file, $user_link)) { $this->note("The database structure was successfully created.", 'ok'); define("CURRENT_DB", $params['db_name']); $peepagg_dsn = "mysql://" . $params['db_user'] . ":" . $params['db_password'] . "@" . $params['db_host'] . "/" . $params['db_name']; $this->peepagg_dsn = $peepagg_dsn; $this->admin_exsts = false; } } else { $this->note("File <code>{$sql_file}</code> does not exists.", 'error'); return false; } // run the PeepAgg seed file if ($sql_file = getShadowedPath("web/install/PeepAgg.seed.mysql")) { if ($this->run_query_file($sql_file, $user_link)) { $this->note("The database was successfully inserted with seed data.", 'ok'); $peepagg_dsn = "mysql://" . $params['db_user'] . ":" . $params['db_password'] . "@" . $params['db_host'] . "/" . $params['db_name']; $this->peepagg_dsn = $peepagg_dsn; $this->admin_exsts = false; } } else { $this->note("File <code>{$sql_file}</code> does not exists.", 'error'); return false; } $this->admin_exists = $this->check_admin_exists($user_link); } else { // use the normal PeepAgg.mysql which contains both the structure and basic data to seed all in one file if ($sql_file = getShadowedPath("web/install/PeepAgg.mysql")) { if ($this->run_query_file($sql_file, $user_link)) { $this->note("The database was successfully populated.", 'ok'); define("CURRENT_DB", $params['db_name']); $peepagg_dsn = "mysql://" . $params['db_user'] . ":" . $params['db_password'] . "@" . $params['db_host'] . "/" . $params['db_name']; $this->peepagg_dsn = $peepagg_dsn; $this->admin_exsts = false; } else { $this->note("The installer is unable to execute MySQL queries.", 'error'); return false; } } else { $this->note("File <code>{$fn}</code> does not exists.", 'error'); return false; } } // Parag Jagdale - 10/17/10: end } else { define("CURRENT_DB", $params['db_name']); $peepagg_dsn = "mysql://" . $params['db_user'] . ":" . $params['db_password'] . "@" . $params['db_host'] . "/" . $params['db_name']; $this->peepagg_dsn = $peepagg_dsn; $this->admin_exists = $this->check_admin_exists($user_link); } // now run upgrade scripts $this->note("Running database upgrade script.", 'info'); try { $network_prefix = 'default'; require_once "web/update/run_scripts.php"; run_update_scripts(true); $this->note("The database was successfully upgraded.", 'ok'); } catch (Exception $e) { $this->note("Error updating database: " . $e->getMessage(), 'error'); return false; } return true; }
} flush(); } function run_update_scripts() { global $path_prefix, $base_url; if (!defined("PEEPAGG_UPDATING")) { define("PEEPAGG_UPDATING", 1); } upd_write("Running database upgrade script ..."); $upd = new db_update_page(); $upd->main(); upd_write("Installing default module settings ..."); if (!(include "{$path_prefix}/db/script_module_settings_data.php")) { throw new PAException(GENERAL_SOME_ERROR, "Unable to install default module settings"); } } if (realpath(@$_SERVER['SCRIPT_FILENAME']) == realpath(__FILE__)) { upd_write("Running all scripts to update the system to the latest version."); try { run_update_scripts(); upd_write("It looks like all the scripts ran correctly - your system is now up to date!"); if (@$_SERVER['REQUEST_METHOD']) { echo ' <p><b><a href="' . $base_url . '/homepage.php">Click here to continue back to the (newly updated) homepage!</a></b></p> '; } } catch (Exception $e) { upd_write("AN ERROR OCCURRED: " . $e->getMessage()); } }
function run_scripts() { // global var $_base_url has been removed - please, use PA::$url static variable ?> <h1>Almost there!</h1> <p><?= __('All the files have been updated. Now you need to') ?> <b><a href="run_scripts.php?return=<?= PA::$url.PA_ROUTE_HOME_PAGE?>"><?= __('click here to update the database') ?></a></b> <?= __('and the update will be complete') ?>.</p> <?php return; // no longer done - too many interactions with old/new Dal code, internationalization etc // global var $path_prefix has been removed - please, use PA::$path static variable try { include "web/update/run_scripts.php"; run_update_scripts(); } catch (Exception $e) { echo "<p>".__("Error occurred running database update:")." ".$e->getMessage()."</p>"; } }
function install_peopleaggregator() { global $path_prefix; ?> <h2>Detecting URLs</h2> <ul> <?php // find base url (minus http:// suffix) if (!preg_match("|^(.*?)/config/index.php\$|", $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'], $m)) { dienow("Unable to find base URL"); } $base_url_bare = $m[1]; // make sure the base url is valid if (!can_get_peepagg_txt("http://{$base_url_bare}/peopleaggregator.txt")) { dienow("Unable to guess base URL - I think it should be http://{$base_url_bare}/ but that URL does not seem to work."); } note("Good: we can access the system at http://{$base_url_bare}/"); // try stripping off the first url part (i.e. www.asdf -> asdf) $base_url_suffix = preg_replace("|^[^\\.]+\\.(.*)\$|", "\$1", $base_url_bare); $allow_spawning = FALSE; // check if it doesn't have any dots (e.g. http://colinux/web/config/) // - i.e. not suitable for sharing cookies over domains. if ($base_url_suffix == $base_url_bare) { note("Apparently running on an internal web server - not possible to run multiple networks."); } else { if (preg_match("|^\\d+\\.\\d+\\.\\d+\\.\\d+|", $base_url_bare)) { note("Running with an IP address rather than a domain name - not possible to run multiple networks."); } else { if (can_get_peepagg_txt("http://{$base_url_suffix}/peopleaggregator.txt")) { note("It looks like it is also accessible at <code>http://{$base_url_suffix}/</code>; trying <code>{$base_url_suffix}</code> as the root URL."); } else { note("It is not accessible at <code>http://{$base_url_suffix}/</code>; trying <code>{$base_url_bare}</code> as the root URL."); $base_url_suffix = $base_url_bare; } if (can_get_peepagg_txt("http://some-random-domain.{$base_url_suffix}/peopleaggregator.txt")) { note("It looks like the server is set up to host <code>*.{$base_url_suffix}</code>, so network spawning is possible."); $allow_spawning = TRUE; } else { warn("Wildcard domains do not appear to be enabled (cannot access the root of this install at http://some_random_domain.{$base_url_suffix}/) so network spawning will be disabled."); } } } global $base_url, $domain_suffix; if ($allow_spawning) { $base_url = "http://%network_name%.{$base_url_suffix}"; } else { $base_url = "http://{$base_url_bare}"; } $domain_suffix = preg_replace("|/.*\$|", "", $base_url_suffix); // stash $base_url away as config.inc will modify it $base_url_config = $base_url; note("Base URL: <code>{$base_url}</code>" . ($domain_suffix ? "; domain suffix: <code>{$domain_suffix}</code>" : "")); ?> </ul> <h2>Configuration</h2> <?php $admin_password = get_default("admin_password", ""); $admin_password2 = get_default("admin_password2", ""); $mysql_server = get_default("mysql_server", "localhost"); $mysql_dbname = get_default("mysql_dbname", "peopleaggregator"); $mysql_username = get_default("mysql_username", "peopleaggregator"); $mysql_password = get_default("mysql_password", ""); $mysql_root_username = get_default("mysql_root_username", "root"); $mysql_root_password = get_default("mysql_root_password", ""); $home_network_config = str_replace("%network_name%", "www", $base_url) . "/config/"; ?> <form method="POST" action="<?php echo $home_network_config; ?> #check"> <div class="config"> <p>Some operations (upgrading, and content administration) require an administrator password for access. Please enter an administrator password here.</p> <div class="config_item"><label for="admin_password">Admin password</label> <input type="password" id="admin_password" name="admin_password" value="<?php echo $admin_password; ?> "><?php if (!$admin_password) { echo " ← must not be blank!"; } ?> </div> <div class="config_item"><label for="admin_password">Repeat admin password</label> <input type="password" id="admin_password2" name="admin_password2" value="<?php echo $admin_password2; ?> "><?php if (!$admin_password2) { echo " ← must not be blank!"; } else { if ($admin_password != $admin_password2) { echo " ← must be the same as above!"; } } ?> </div> <p>Enter your database details below.</p> <div class="config_item"><label for="mysql_server">MySQL server</label> <input type="text" name="mysql_server" value="<?php echo $mysql_server; ?> "></div> <div class="config_item"><label for="mysql_dbname">MySQL database name</label> <input type="text" name="mysql_dbname" value="<?php echo $mysql_dbname; ?> "></div> <div class="config_item"><label for="mysql_username">MySQL username</label> <input type="text" name="mysql_username" value="<?php echo $mysql_username; ?> "></div> <div class="config_item"><label for="mysql_password">MySQL password</label> <input type="password" name="mysql_password" value="<?php echo $mysql_password; ?> "><?php if (!$mysql_password) { echo " ← must not be blank!"; } ?> </div> <p>If the database has not been created yet, you can enter your database administrator ("root") login details here to have it created automatically:</p> <div class="config_item"><label for="mysql_root_username">Administrator username</label> <input type="text" name="mysql_root_username" value="<?php echo $mysql_root_username; ?> "></div> <div class="config_item"><label for="mysql_root_password">Administrator password</label> <input type="password" name="mysql_root_password" value="<?php echo $mysql_root_password; ?> "></div> <p><input type="submit" value="Set up PeopleAggregator"></p> </div> </form> <?php // only exec the rest after someone clicks 'setup' if (!$_POST) { exit; } // wrap install process in exception handler so we can roll back $rollback_cmds = array(); try { ?> <h2 id="check">Checking config details</h2> <ul> <?php if (!$admin_password) { focus_field("admin_password"); dienow("You must supply an admin password"); } if ($admin_password != $admin_password2) { focus_field("admin_password"); dienow("Both admin paswords must be the same"); } if (!$mysql_password) { focus_field("mysql_password"); dienow("You may not use a blank password for the MySQL connection"); } $user_link = @mysql_connect($mysql_server, $mysql_username, $mysql_password); if ($user_link) { note("Able to connect to the MySQL server at {$mysql_server} with supplied login details."); // make sure the DB isn't already populated if (!mysql_select_db($mysql_dbname, $user_link)) { note("Database does not exist or is inaccessible"); mysql_close($user_link); $user_link = FALSE; } else { $sth = run_query("SHOW TABLES", $user_link); if (mysql_num_rows($sth)) { throw new Installation_Failure("The database {$mysql_dbname} already contains data. Please wipe it out or recreate the database before installing PeopleAggregator. If PeopleAggregator is already installed here, you will have to create your local_config.php file manually."); } } } else { note("Unable to connect to the MySQL server using the supplied login details"); } if (!$user_link) { note("Trying administrator login..."); $admin_link = @mysql_connect($mysql_server, $mysql_root_username, $mysql_root_password); if (!$admin_link) { dienow("Unable to connect to the MySQL server with the supplied login details or as an administrator"); } note("Able to connect to the MySQL server with the supplied administrator login details - a new database will be created."); // make sure the db doesn't already exist if (mysql_select_db($mysql_dbname, $admin_link)) { throw new Installation_Failure("Database {$mysql_dbname} already exists"); } // create it $sql = "CREATE DATABASE " . db_esc($mysql_dbname); $rollback_cmds[] = array("sql", "DROP DATABASE " . db_esc($mysql_dbname), $admin_link); run_query($sql, $admin_link); // now grant permissions with successively looser hostnames until // we find one that lets the web server access the database. foreach (array("localhost", "localhost.localdomain", $_SERVER['SERVER_NAME'], gethostbyname($_SERVER['SERVER_NAME']), "%") as $server_host) { $sql = "GRANT ALL ON " . db_esc($mysql_dbname) . ".* TO " . db_esc($mysql_username) . "@" . db_esc($server_host) . " IDENTIFIED BY '" . mysql_real_escape_string($mysql_password) . "'"; run_query($sql, $admin_link); if (($user_link = mysql_connect($mysql_server, $mysql_username, $mysql_password)) && mysql_select_db($mysql_dbname, $user_link)) { note("Successfully logged in to new database using credentials from host {$server_host}"); break; } } if (!$user_link) { throw new Installation_Failure("Failed to grant access credentials that would allow the web server to access the database. Please try creating the database manually."); } } if (!$user_link) { throw new Installation_Failure("Something went wrong - we should have successfully connected to the DB by now"); } // set all local_config.php vars global $peepagg_dsn; $peepagg_dsn = "mysql://{$mysql_username}:{$mysql_password}@{$mysql_server}/{$mysql_dbname}"; $logger_logFile = "{$path_prefix}/log/pa.log"; $default_relation_id = 1; // now write out local_config.php $local_config_text = "<" . "?php\n\n// local_config.php: This file contains server-specific settings like\n// the database password, the base URL of this installation, and\n// debugging flags. Anything in default_config.php can be overridden\n// here.\n\n// If you want to change project-specific things like the site name,\n// you can use project_config.php.\n\n// Global defaults, which are shared by all other PeopleAggregator\n// installations, are in default_config.php.\n\n// Database details.\n\$peepagg_dsn = \"{$peepagg_dsn}\";\n\n// URL to the root of the server.\n\$base_url = \"{$base_url_config}\";\n\n// Parent domain part of the URL.\n\$domain_suffix = \"{$domain_suffix}\";\n"; if ($allow_spawning) { $local_config_text .= "\n// Network operation is enabled. To disable, set \$_PA->enable_networks\n// to FALSE. To disable network spawning without deactivating existing\n// networks, set \$_PA->enable_network_spawning to FALSE.\n"; } else { $local_config_text .= "\n// Network operation disabled as wildcard domains are not configured.\n// Comment out the following line to enable network creation (after\n// configuring wildcard DNS, etc).\n\$_PA->enable_networks = FALSE;\n"; } $local_config_text .= "\n// Path to log file (you may wish to change this to /var/log/somewhere/pa.log).\n\$logger_logFile = \"\$path_prefix/log/pa.log\";\n\n// Administration password\n\$admin_password = \"{$admin_password}\";\n\n// When a new user registers on the site, they will automatically be marked as a friend of the user with this ID.\n// (The default is 1, so everyone will be a friend of the first user.)\n\$default_relation_id = {$default_relation_id};\n\n?" . ">\n"; global $config_fn; note("Writing local_config.php"); $rollback_cmds[] = array("delete", $config_fn); if (!file_put_contents($config_fn, $local_config_text)) { throw new Installation_Failure("Unable to write {$config_fn}"); } // define LOCAL_CONFIG_OVERRIDE to tell config.inc to load our new // temporary local_config.php rather than look for it in the global // location define("LOCAL_CONFIG_LOCATION_OVERRIDE", $config_fn); // now set up databases note("Initializing database ... "); run_query_file(dirname(__FILE__) . "/../../db/PeepAgg.mysql", $user_link); note("Running database upgrade script and installing default module settings ... "); try { require_once dirname(__FILE__) . "/../update/run_scripts.php"; run_update_scripts(); } catch (Exception $e) { throw new Installation_Failure("Error updating database or installing default module settings: " . $e->getMessage()); } global $do_auto_update; if (!$do_auto_update) { note("Skipping auto-upgrade preparation as it is disabled for this installation."); } else { note("Preparing system for auto-upgradeability ... "); require_once "Subversion/PAStateStore.php"; try { $store = new Subversion_PAStateStore($path_prefix); $store->initialize(); note("Subversion update root: <code>" . $store->get_repository_root() . "</code>; path: <code>" . $store->get_repository_path() . "</code>; revision: " . $store->get_revision()); } catch (Exception $e) { throw new Installation_Failure("Error preparing auto-upgrade system: " . $e->getMessage()); } } ?> </ul> <h2>All done!</h2> <p class="good">Your database has been initialized and a <code>local_config.php</code> file has been written at <code><?php echo $config_fn; ?> </code>. To finish the installation, please move this file up into the parent of the 'web' directory, then <a href="../">click here</a>.</p> <p>The following command will do this on Linux/UNIX:</p> <p style="margin-left: 5em"><code>mv <?php echo realpath($config_fn); ?> <?php echo $path_prefix; ?> /</code></p> <?php //throw new Installation_Failure("foo"); } catch (Installation_Failure $e) { warn("Installation failed (" . $e->getMessage() . ") - undoing operations"); foreach (array_reverse($rollback_cmds) as $cmd) { switch ($cmd[0]) { case 'sql': list(, $sql, $link) = $cmd; note("DB query: {$sql}"); mysql_query($sql, $link); break; case 'delete': list(, $fn) = $cmd; note("Delete: {$fn}"); unlink($fn); break; default: warn("Unknown rollback command type: " . $cmd[0]); break; } } } }