diff options
Diffstat (limited to 'installer')
-rw-r--r-- | installer/include.php | 81 | ||||
-rw-r--r-- | installer/index.php | 235 |
2 files changed, 316 insertions, 0 deletions
diff --git a/installer/include.php b/installer/include.php new file mode 100644 index 0000000..7300e90 --- /dev/null +++ b/installer/include.php @@ -0,0 +1,81 @@ +<?php + +function mm_getParam($name, $default='') { + if (isset($_POST[$name])) { + return $_POST[$name]; + } else { + return $default; + } +} + +function mm_configStr($param) { + return "\$db_config['$param'] = \"".$_POST["db_$param"]."\";\n"; +} + +function mm_isSqlConfigured($conf_file) { + if (file_exists($conf_file)) { + global $db_config; + require($conf_file); + if (isset($db_config)) { + unset($db_config); + return true; + } + } + return false; +} + +function mm_mysql_create_db($mysql, $db_name, &$r) { + global $t; + if ($mysql) { + $db_list = mysql_list_dbs($mysql); + $db_array = Array(); + while ($row = mysql_fetch_object($db_list)) { + $db_array[] = $row->Database . ''; + } + $r.=$t->inputP("Existing databases: ".implode(', ',$db_array)); + + if (!in_array($db_name, $db_array)) { + $str.=$t->inputP("Creating database <q>$db_name</q>..."); + $db = mysql_query("CREATE DATABASE $db_name;", $mysql); + if ($db===FALSE) { + $str.=$t->inputP("Database <q>$db_name</q> ". + "could not be created: ". + mysql_error($mysql), true); + return false; + } + } + $r.=$t->inputP("Selecting database <q>$db_name</q>..."); + $db = mysql_select_db($db_name, $mysql); + if (!$db) { + $r.=$t->inputP('Could not select database: ', + mysql_error($mysql), true); + return false; + } + return true; + } else { + return false; + } +} + +function mm_mysql_count_rows_in_table($mysql, $table_name) { + $table=mysql_real_escape_string($table_name); + $query = + "SELECT COUNT(*)\n". + "FROM $table;"; + $total = mysql_query($query, $mysql); + $total = mysql_fetch_array($total); + $total = $total[0]; + return $total; +} + +function mm_mysql_table_exists($mysql, $table_name) { + $table=mysql_real_escape_string($table_name); + $query = + "SELECT COUNT(*)\n". + "FROM information_schema.tables\n". + "WHERE table_name = '$table';"; + $total = mysql_query($query, $mysql); + $total = mysql_fetch_array($total); + $total = $total[0]; + return $total>0; +} diff --git a/installer/index.php b/installer/index.php new file mode 100644 index 0000000..5ea418b --- /dev/null +++ b/installer/index.php @@ -0,0 +1,235 @@ +<?php + +$BASE = dirname(dirname(__FILE__)); +set_include_path(get_include_path() + .PATH_SEPARATOR. "$BASE/src/lib" + .PATH_SEPARATOR. "$BASE/src/ext" + ); +$uri = $_SERVER['REQUEST_URI']; + +require_once('include.php'); + +$conf_file = "$BASE/conf.php"; +if (!mm_isSqlConfigured($conf_file)) { + require_once('Template.class.php'); + $t = new Template($BASE); + + $t->header('Message Manager: Installer'); + + $t->paragraph("First we need to set up the SQL configuration, ". + "then we will set up the first user."); + + $t->openTag('form', array('method'=>'post','action'=>$uri)); + $t->tag('input', array('type'=>'hidden', 'name'=>'try', 'value'=>'t')); + $try = isset($_POST['try']); + + $mysql = false; + if ($try) { + @$mysql = mysql_connect($_POST['db_host'], + $_POST['db_user'], + $_POST['db_password']); + } + + //////////////////////////////////////////////////////////////////////// + + $t->openFieldset("MySQL Authentication", $mysql); + $t->inputText('db_host', 'Hostname', '', + getParam('db_host','localhost'), $mysql); + $t->inputText('db_user', 'Username', '', + getParam('db_user'), $mysql); + $t->inputPassword('db_password', 'Password', '', + getParam('db_password'), $mysql); + if ($try && !$mysql) { + $t->inputP("Could not authenticate: ".mysql_error(), true); + } + $t->closeFieldset(); + + //////////////////////////////////////////////////////////////////////// + + $charset = false; + if ($mysql) { + $charset = mysql_set_charset($_POST['db_charset'], $mysql); + if (!$charset) { + $charset_error = mysql_error($mysql); + } + } + + $db = false; + $db_message = ''; + if ($charset) { + $t->setRet(true); + $db = mm_mysql_db($mysql, $_POST['db_name'], $db_message); + $t->setRet(false); + } + + $db_prefix = $_POST['db_prefix']; + + $table = false; + if ($db) { + $table_exists = mm_mysql_table_exists($mysql,$db_prefix.'auth'); + if (!$table_exists) { + $query = + 'CREATE TABLE '.$db_prefix."auth (\n". + " uid INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,\n". + " name VARCHAR(255),\n". + " hash CHAR(60)\n". + " status INT\n" + ");"; + $table = mysql_query($query); + if (!$table) { + $table_error = mysql_error($mysql); + } + } else { + $table = true; + } + } + + //////////////////////////////////////////////////////////////////////// + + $t->openFieldset("MySQL Settings", $table); + + $t->inputText('db_charset', 'Charset', + "I've heard that you may need to change this if ". + "you use an old version of MySQL. 'utf8' is ". + "generally a good option, though.", + getParam('db_charset','utf8'), $table); + if ($mysql) { + $str = $_POST['db_charset']; + if ($charset) { + $t->inputP("Set charset to <q>$str</q>."); + } else { + $t->inputP("Could not set charset to ". + "<q>$str</q>: ".$charset_error, true); + } + } + + $t->inputText('db_name', 'Database name', '', + getParam('db_name', 'messagemanager'), $table); + echo $db_message; + + $t->inputText('db_prefix', 'Table prefix', + 'Just use simple characters, like [A-Za-z0-9_], '. + 'and keep it short.', + getParam('db_prefix','mm_'), $table); + + if ($db) { + $db_name = '<q>'.$db_prefix.'auth</q>'; + if ($table) { + if ($table_exists) { + $msg="Table $db_name already exists."; + } else { + $msg="Created table $db_name."; + } + } else { + $msg="Could not create table $db_name: ".$table_error; + } + $t->inputP($msg, !$table); + } + + $t->closeFieldset(); + + //////////////////////////////////////////////////////////////////////// + + $fh = false; + if ($table) { + $fh = fopen('conf.php', 'w'); + if ($fh === FALSE) { + $msg="Could not open file <q>conf.php</q> for writing."; + $template->paragraph($msg, true); + } else { + fwrite($fh, '<?php global $db_config;'."\n"); + fwrite($fh, configStr('host')); + fwrite($fh, configStr('user')); + fwrite($fh, configStr('password')); + fwrite($fh, "\n"); + fwrite($fh, configStr('charset')); + fwrite($fh, configStr('name')); + fwrite($fh, configStr('prefix')); + fclose($fh); + } + } + if ($fh) { + $t->closeTag('form'); + $t->openTag('form', array('action'=>$uri)); + $t->tag('input', array('type'=>'submit', + 'value'=>'Cool beans, go to step 2!')); + } else { + $t->tag('input', array('type'=>'submit', 'value'=>'Submit')); + } + $t->closeTag('form'); + $t->footer(); + //////////////////////////////////////////////////////////////////////// +} else { + require_once('MessageManager.class.php'); + $m = new MessageManager($conf_file); + $t = $m->template(); + + $t->header('Message Manager: Installer'); + + $user_count = $m->countUsers(); + + if ($user_count<1) { + $t->openTag('form', array('method'=>'post', 'action'=>$uri)); + $t->tag('input', array('type'=>'hidden', + 'id'=>'try', + 'name'=>'try', + 'value'=>'t')); + $try = isset($_POST['try']); + + $pw = false; + if ($try) { + $pw = ( $_POST['mm_password'] === + $_POST['mm_password_verify'] ); + } + + $admin = false; + if ($pw) { + $user = $_POST['mm_user']; + $password = $_POST['mm_password']; + + $uid = $m->addUser($user, $password); + $admin = $m->setStatus($uid, 2); + if (!$admin) { + $admin_error = mysql_error($mysql); + } + } + + //////////////////////////////////////////////////////////////// + + $t->openFieldset("First Account (administrator)",$admin); + $t->inputText('mm_user', 'Username', + "Must be <= 255 characters.", + getParam('mm_user','root'), $admin); + $t->inputNewPassword('mm_password', 'Password', + ($pw?getParam('mm_password'):''), + $admin); + if ($try && !$pw) { + $msg="Passwords don't match."; + $template->inputP($msg, true); + } + if ($pw) { + $user = "<q>".$_POST['mm_user'].'</q>'; + if ($admin) { + $msg="Created user $user."; + } else { + $msg="Could not create user $user: ". + $admin_error; + } + $t->inputP($msg, !$admin); + } + $t->closeFieldset(); + + //////////////////////////////////////////////////////////////// + + if (!$admin) { + $t->tag('input', array('type'=>'submit', + 'value'=>'Submit')); + } + $t->closeTag('form'); + } else { + $t->paragraph("File conf.php already exists, and there ". + "is at least one user. Return to the ". + "<a href='index.php'>main page</a>."); + } + $t->footer(); +} |