install.php: improved support for running Laconica in a sub folder and on a non-standard port,

tidyed up configuration file generation, removing duplication from db functions,
This commit is contained in:
Brett Taylor 2009-08-07 14:17:55 +12:00 committed by Brenda Wallace
parent f4f13166f7
commit b0bb1fff2e

View File

@ -180,6 +180,9 @@ function handlePost()
$password = $_POST['password']; $password = $_POST['password'];
$sitename = $_POST['sitename']; $sitename = $_POST['sitename'];
$fancy = !empty($_POST['fancy']); $fancy = !empty($_POST['fancy']);
$server = $_SERVER['HTTP_HOST'];
$path = substr(dirname($_SERVER['PHP_SELF']), 1);
?> ?>
<dl class="system_notice"> <dl class="system_notice">
<dt>Page notice</dt> <dt>Page notice</dt>
@ -219,20 +222,42 @@ function handlePost()
} }
switch($dbtype) { switch($dbtype) {
case 'mysql': mysql_db_installer($host, $database, $username, $password, $sitename, $fancy); case 'mysql':
$db = mysql_db_installer($host, $database, $username, $password);
break; break;
case 'pgsql': pgsql_db_installer($host, $database, $username, $password, $sitename, $fancy); case 'pgsql':
$db = pgsql_db_installer($host, $database, $username, $password);
break; break;
default: default:
} }
if ($path) $path .= '/';
updateStatus("You can visit your <a href='/$path'>new Laconica site</a>."); if (!$db) {
// database connection failed, do not move on to create config file.
return false;
}
updateStatus("Writing config file...");
$res = writeConf($sitename, $server, $path, $fancy, $db);
if (!$res) {
updateStatus("Can't write config file.", true);
showForm();
return;
}
/*
TODO https needs to be considered
*/
$link = "http://".$server.'/'.$path;
updateStatus("Laconica has been installed at $link");
updateStatus("You can visit your <a href='$link'>new Laconica site</a>.");
?> ?>
<?php <?php
} }
function pgsql_db_installer($host, $database, $username, $password, $sitename, $fancy) { function pgsql_db_installer($host, $database, $username, $password) {
$connstring = "dbname=$database host=$host user=$username"; $connstring = "dbname=$database host=$host user=$username";
//No password would mean trust authentication used. //No password would mean trust authentication used.
@ -265,7 +290,7 @@ function pgsql_db_installer($host, $database, $username, $password, $sitename, $
if ($res === false) { if ($res === false) {
updateStatus("Can't run database script.", true); updateStatus("Can't run database script.", true);
showForm(); showForm();
return; return false;
} }
foreach (array('sms_carrier' => 'SMS carrier', foreach (array('sms_carrier' => 'SMS carrier',
'notice_source' => 'notice source', 'notice_source' => 'notice source',
@ -276,29 +301,24 @@ function pgsql_db_installer($host, $database, $username, $password, $sitename, $
if ($res === false) { if ($res === false) {
updateStatus(sprintf("Can't run %d script.", $name), true); updateStatus(sprintf("Can't run %d script.", $name), true);
showForm(); showForm();
return; return false;
} }
} }
pg_query($conn, 'COMMIT'); pg_query($conn, 'COMMIT');
updateStatus("Writing config file...");
if (empty($password)) { if (empty($password)) {
$sqlUrl = "pgsql://$username@$host/$database"; $sqlUrl = "pgsql://$username@$host/$database";
} }
else { else {
$sqlUrl = "pgsql://$username:$password@$host/$database"; $sqlUrl = "pgsql://$username:$password@$host/$database";
} }
$res = writeConf($sitename, $sqlUrl, $fancy, 'pgsql');
if (!$res) {
updateStatus("Can't write config file.", true);
showForm();
return;
}
updateStatus("Done!");
$db = array('type' => 'pgsql', 'database' => $sqlUrl);
return $db;
} }
function mysql_db_installer($host, $database, $username, $password, $sitename, $fancy) { function mysql_db_installer($host, $database, $username, $password) {
updateStatus("Starting installation..."); updateStatus("Starting installation...");
updateStatus("Checking database..."); updateStatus("Checking database...");
@ -306,21 +326,21 @@ function mysql_db_installer($host, $database, $username, $password, $sitename, $
if (!$conn) { if (!$conn) {
updateStatus("Can't connect to server '$host' as '$username'.", true); updateStatus("Can't connect to server '$host' as '$username'.", true);
showForm(); showForm();
return; return false;
} }
updateStatus("Changing to database..."); updateStatus("Changing to database...");
$res = mysql_select_db($database, $conn); $res = mysql_select_db($database, $conn);
if (!$res) { if (!$res) {
updateStatus("Can't change to database.", true); updateStatus("Can't change to database.", true);
showForm(); showForm();
return; return false;
} }
updateStatus("Running database script..."); updateStatus("Running database script...");
$res = runDbScript(INSTALLDIR.'/db/laconica.sql', $conn); $res = runDbScript(INSTALLDIR.'/db/laconica.sql', $conn);
if ($res === false) { if ($res === false) {
updateStatus("Can't run database script.", true); updateStatus("Can't run database script.", true);
showForm(); showForm();
return; return false;
} }
foreach (array('sms_carrier' => 'SMS carrier', foreach (array('sms_carrier' => 'SMS carrier',
'notice_source' => 'notice source', 'notice_source' => 'notice source',
@ -331,31 +351,40 @@ function mysql_db_installer($host, $database, $username, $password, $sitename, $
if ($res === false) { if ($res === false) {
updateStatus(sprintf("Can't run %d script.", $name), true); updateStatus(sprintf("Can't run %d script.", $name), true);
showForm(); showForm();
return; return false;
} }
} }
updateStatus("Writing config file...");
$sqlUrl = "mysqli://$username:$password@$host/$database"; $sqlUrl = "mysqli://$username:$password@$host/$database";
$res = writeConf($sitename, $sqlUrl, $fancy); $db = array('type' => 'mysql', 'database' => $sqlUrl);
if (!$res) { return $db;
updateStatus("Can't write config file.", true);
showForm();
return;
} }
updateStatus("Done!");
} function writeConf($sitename, $server, $path, $fancy, $db)
function writeConf($sitename, $sqlUrl, $fancy, $type='mysql')
{ {
$res = file_put_contents(INSTALLDIR.'/config.php', // assemble configuration file in a string
"<?php\n". $cfg = "<?php\n".
"if (!defined('LACONICA')) { exit(1); }\n\n". "if (!defined('LACONICA')) { exit(1); }\n\n".
"\$config['site']['name'] = \"$sitename\";\n\n".
// site name
"\$config['site']['name'] = '$sitename';\n\n".
// site location
"\$config['site']['server'] = '$server';\n".
"\$config['site']['path'] = '$path'; \n\n".
// checks if fancy URLs are enabled
($fancy ? "\$config['site']['fancy'] = true;\n\n":''). ($fancy ? "\$config['site']['fancy'] = true;\n\n":'').
"\$config['db']['database'] = \"$sqlUrl\";\n\n".
($type == 'pgsql' ? "\$config['db']['quote_identifiers'] = true;\n\n" . // database
"\$config['db']['type'] = \"$type\";\n\n" : ''). "\$config['db']['database'] = '{$db['database']}';\n\n".
"?>"); ($type == 'pgsql' ? "\$config['db']['quote_identifiers'] = true;\n\n":'').
"\$config['db']['type'] = '{$db['type']}';\n\n".
"?>";
// write configuration file out to install directory
$res = file_put_contents(INSTALLDIR.'/config.php', $cfg);
return $res; return $res;
} }
@ -368,10 +397,15 @@ function runDbScript($filename, $conn, $type='mysql')
if (!mb_strlen($stmt)) { if (!mb_strlen($stmt)) {
continue; continue;
} }
if ($type == 'mysql') { switch ($type) {
case 'mysql':
$res = mysql_query($stmt, $conn); $res = mysql_query($stmt, $conn);
} elseif ($type=='pgsql') { break;
case 'pgsql':
$res = pg_query($conn, $stmt); $res = pg_query($conn, $stmt);
break;
default:
updateStatus("runDbScript() error: unknown database type ". $type ." provided.");
} }
if ($res === false) { if ($res === false) {
updateStatus("FAILED SQL: $stmt"); updateStatus("FAILED SQL: $stmt");