From 09dfe32eb6b538225686fd6ed0220240010bc574 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Mon, 1 Aug 2011 01:22:36 -0400 Subject: initial commit. Partway through a rewrite. I have some old files I didn't want to entirely delete. --- installer/include.php | 81 +++++++++++++++++ installer/index.php | 235 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 316 insertions(+) create mode 100644 installer/include.php create mode 100644 installer/index.php (limited to 'installer') 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 @@ +Database . ''; + } + $r.=$t->inputP("Existing databases: ".implode(', ',$db_array)); + + if (!in_array($db_name, $db_array)) { + $str.=$t->inputP("Creating database $db_name..."); + $db = mysql_query("CREATE DATABASE $db_name;", $mysql); + if ($db===FALSE) { + $str.=$t->inputP("Database $db_name ". + "could not be created: ". + mysql_error($mysql), true); + return false; + } + } + $r.=$t->inputP("Selecting database $db_name..."); + $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 @@ +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 $str."); + } else { + $t->inputP("Could not set charset to ". + "$str: ".$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 = ''.$db_prefix.'auth'; + 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 conf.php for writing."; + $template->paragraph($msg, true); + } else { + fwrite($fh, '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 = "".$_POST['mm_user'].''; + 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 ". + "main page."); + } + $t->footer(); +} -- cgit v1.2.3