<?php
	session_start();
	if( !isset($_SESSION["install"]) )
	{
		$_SESSION["install"]=array();
		$_SESSION["install"]["step"] = 1;
	}

	if( $_SESSION["install"]["step"] > 1 )
	{
		$db = mysql_connect(
			$_SESSION["install"]["db_host"],
			$_SESSION["install"]["db_user"],
			$_SESSION["install"]["db_passwd"]
		);
		if( !$db )
		{
			echo "Error connecting: ".mysql_error();
			echo "<br><br><a href=\"install.php\">Try again</a>";
			$_SESSION["install"]["step"] = 1;
		}
	}

	if( $_SESSION["install"]["step"] > 2 )
	{
		if( !mysql_select_db( $_SESSION["install"]["db_database"], $db ) )
		{
			echo "You don't have permission to use this database.<br>".mysql_error();
			echo "<br><br><a href=\"install.php\">Try again</a>";
			$_SESSION["install"]["step"] = 1;
			return;
		}
	}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
	<title>Installing phpWebThings</title>
	<style type="text/css">	BODY {
		background : White;
		font-family : Verdana, Geneva, Arial, Helvetica, sans-serif;
		font-size : 12px;
		color : #2F4F4F;
	}

	TD {
		background : #F8F8FF;
		font-family : Verdana, Geneva, Arial, Helvetica, sans-serif;
		font-size : 12px;
		color : #2F4F4F;
	}

	INPUT {
		background : #F5F5F5;
		font-family : Verdana, Geneva, Arial, Helvetica, sans-serif;
		font-size : 12px;
		color : #2F4F4F;
	}

	TEXTAREA {
		background : #F5F5F5;
		font-family : Verdana, Geneva, Arial, Helvetica, sans-serif;
		font-size : 12px;
		color : #2F4F4F;
	}
</style>
</head>
<body bgcolor="#FFFFFF"><br><br>
<table width="500" align="center" cellpadding="4" cellspacing="0">
<tr><td><img src="images/logo.gif" width="275" height="60" alt="" border="0"><br></td></tr>
<?php
if( isset($_POST["db_submit"]) ) process_step1();
else if( isset($_POST["db2_submit"]) ) process_step2();
else if( isset($_POST["db3_submit"]) ) process_step3();
else draw_step1();
?>
<tr><td align="right"><a href="http://www.phpwebthings.org">phpWebThings</a></td></tr>
</table>

</body>
</html>
<?php
function draw_step1()
{
?>
<tr><td>Database Information:<br>
<i>Fill the fields (MySQL login) so the install script can login into database</i><br></td></tr>
<form action="install.php" method="post">
<input type="hidden" name="db_submit" value="1">
<tr><td>User:<br><input type="text" name="db_user" size="32" maxlength="128"></td></tr>
<tr><td>Password:<br><input type="password" name="db_passwd" size="32" maxlength="128"></td></tr>
<tr><td>Host:<br><input type="text" name="db_host" value="localhost" size="32" maxlength="128"></td></tr>
<tr><td align="right"><input type="submit" name="db_button" value="Next Step"></td></tr>
</form>
<?php
}

function process_step1()
{
	global $db;
	echo "<tr><td>";
	$db = mysql_connect( $_POST["db_host"], $_POST["db_user"], $_POST["db_passwd"] );
	if( !$db )
	{
		echo "Error connecting: ".mysql_error();
		echo "<br><br><a href=\"install.php\">Try again</a>";
		echo "</td></tr>";
		$_SESSION["install"]["step"] = 1;
		return;
	}
	$_SESSION["install"]["db_host"] = $_POST["db_host"];
	$_SESSION["install"]["db_user"] = $_POST["db_user"];
	$_SESSION["install"]["db_passwd"] = $_POST["db_passwd"];
	$ret = mysql_query( "SHOW DATABASES" );
	$dbs = array();
	while( $row = mysql_fetch_array($ret) )
	{
		$dbs[] = $row[0];
	}
	mysql_free_result($ret);
	$_SESSION["install"]["step"] = 2;
	draw_step2($dbs);
}

function draw_step2(&$dbs)
{
?>
<tr><td>Database Information:<br>
<i>Select the database where you want to create the tables and the table prefix for the tables</i><br></td></tr>
<form action="install.php" method="post">
<input type="hidden" name="db2_submit" value="1">
<?php
if( !empty($dbs) )
{
?>
<tr><td>Database:<br><select name="db2_database"><?php
reset($dbs);
while( $row = each($dbs) )
{
	echo "<option value=\"{$row[1]}\">{$row[1]}</option>";
}
?></select></td></tr>
<?php
} else {
?>
<tr><td>Database:<br><input type="text" name="db2_database" size=32 maxlength=128></td></tr>
<?php
}
?>
<tr><td>Table Prefix:<br><input type="text" name="db2_prefix" value="wt" size="10" maxlength="10"></td></tr>
<tr><td align="right"><input type="submit" name="db2_button" value="Next Step"></td></tr>
</form>
<?php
}

function process_step2()
{
	global $db;
	echo "<tr><td>";
	if( !mysql_select_db( $_POST["db2_database"], $db ) )
	{
		echo "You don't have permission to use this database.<br>".mysql_error();
		echo "<br><br><a href=\"install.php\">Try again</a>";
		echo "</td></tr>";
		$_SESSION["install"]["step"] = 1;
		return;
	}
	$_SESSION["install"]["db_prefix"] = $_POST["db2_prefix"];
	$_SESSION["install"]["db_database"] = $_POST["db2_database"];
	$fd = fopen( "all_tables_mysql.sql", "r" );
	while( !feof($fd) )
	{
		$buffer = trim(fgets($fd, 4096));
		if( empty($buffer) ) continue;
		$buffer = str_replace( "%prefix%", $_POST["db2_prefix"], $buffer );
		$ret = mysql_query( $buffer, $db );
		if( !$ret )
		{
			echo "Error creating tables:<br>".mysql_error();
			echo "<br><br><a href=\"install.php\">Try again</a>";
			echo "</td></tr>";
			$_SESSION["install"]["step"] = 1;
			return;
		}
	}
	fclose( $fd );
	echo "tables created<br>";
	$_SESSION["install"]["step"] = 3;
	draw_step3();
}

function draw_step3()
{
?>
<tr><td>Administrator Information:<br>
<i>Now, inform the fields about the administrator. Don't forget the loing and password you will inform here. <u>Case matters</u>.</i><br></td></tr>
<form action="install.php" method="post">
<input type="hidden" name="db3_submit" value="1">
<tr><td>Login:<br><input type="text" name="db3_login" size="32" maxlength="20"></td></tr>
<tr><td>Password:<br><input type="text" name="db3_passwd" size="32" maxlength="128"></td></tr>
<tr><td>Real Name:<br><input type="text" name="db3_name" size="32" maxlength="40"></td></tr>
<tr><td>e-mail:<br><input type="text" name="db3_email" size="32" maxlength="50"></td></tr>
<tr><td align="right"><input type="submit" name="db3_button" value="Next Step"></td></tr>
</form>
<?php
}

function process_step3()
{
	global $db;
	echo "<tr><td>";
	$db_login = addslashes(delmagic($_POST["db3_login"]));
	$db_passwd = addslashes(delmagic($_POST["db3_passwd"]));
	$db_name = addslashes(delmagic($_POST["db3_name"]));
	$db_email = addslashes(delmagic($_POST["db3_email"]));

	mt_srand(make_seed());
	$session = md5( mt_rand( 0, mt_getrandmax() ) );
	$stmt = "INSERT INTO {$_SESSION["install"]["db_prefix"]}_users values ( 1, '$db_login', MD5('$db_passwd'), 'admin', '$db_name', '$db_email', '', '', NULL, 'Y', 'Y', '', NULL, NULL, NULL, NULL, NULL, '$session', 'Y', NULL, 0, 0, 0, 0, NOW(), NOW(), NOW(), 0, NULL, NULL, NULL)";
	if( !mysql_query( $stmt, $db ) )
	{
		echo "Error creating administrator.<br>".mysql_error();
		echo "<br><br><a href=\"install.php\">Try again</a>";
		echo "</td></tr>";
		$_SESSION["install"]["step"] = 1;
		return;
	}
	echo "Administrator account created.<br>";
	echo "</td></tr>";
	$_SESSION["install"]["step"] = 3;
	create_wtconfig();
}

function create_wtconfig()
{
	echo "<tr><td>";
	$wtconfig = "<?php\n// Database configuration\n\$config[\"dbtype\"] = \"mysql\";  //mysql/pgsql\n\$config[\"host\"] = \"{$_SESSION["install"]["db_host"]}\";\n\$config[\"user\"] = \"{$_SESSION["install"]["db_user"]}\";\n\$config[\"password\"] = \"{$_SESSION["install"]["db_passwd"]}\";\n\$config[\"database\"] = \"{$_SESSION["install"]["db_database"]}\";\n\$config[\"prefix\"] = \"{$_SESSION["install"]["db_prefix"]}\";\n?>";
	echo "Creating wt_config.php with this info:<br>";
	echo "<textarea rows=\"7\" cols=\"60\">".htmlspecialchars($wtconfig)."</textarea><br>";
	$fd = fopen( "wt_config.php", "w" );
	if( !$fd )
	{
		echo "An error ocurred tryeing to create file wt_config.php, edit it and paste this into it.";
	} else {
		fwrite( $fd, $wtconfig );
		fclose( $fd );
	}
	echo "<b>Installation almost complete! Now, remove the file 'install.php', secure the file 'wt_config.php' if needed, login with the login and password you created and enter WebAdmin to configure the modules.<br><br>I hope that you liked!<br>See ya!<br>Paulo Assis<br>http://www.phpwebthings.org</b>";
	echo "</td></tr>";
}

function delmagic( $value )
{
    // this function removes backslashes ig magic_quotes_gpc is on
    if( get_magic_quotes_gpc() ) return stripslashes( $value );
    else return $value;
}

function make_seed()
{
	list($usec, $sec) = explode(' ', microtime());
	return (float) $sec + ((float) $usec * 100000);
}

?>
