<?PHP

$mysql_server = $_POST['mysql_server'];
$mysql_username = $_POST['mysql_username'];
$mysql_password = $_POST['mysql_password'];
$mysql_database = $_POST['mysql_database'];

$gcount_username = $_POST['gcount_username'];
$gcount_password = $_POST['gcount_password'];

#Defaults
$mysql_server || $mysql_server = "127.0.0.1";
$mysql_username || $mysql_username = "root";
$mysql_password || $mysql_password = "password";
$mysql_database || $mysql_database = "gcount";
$gcount_username || $gcount_username = "gcount";
$gcount_password || $gcount_password = "count123";

$ok = " (<b><font color=\"green\">Ok</font></b>)<BR>";
$error = "<br><b><font color=\"red\">Error</font></b>";

print "<FONT SIZE=\"+2\"><b>Gcount</b> installation program</FONT><P>\n";

$form .= "<form action=\"$PHP_SELF\" method=\"POST\">
	<table><tr>
		<td><b>Mysql Server:</b></td>
		<td><input type=\"Text\" name=\"mysql_server\" value=\"$mysql_server\"></td>
	</tr>
	<tr>
		<td><b>Mysql Username:</b></td>
		<td><input type=\"Text\" name=\"mysql_username\" value=\"$mysql_username\"></td>
	</tr>
	<tr>
		<td><b>Mysql Password:</b></td>
		<td><input type=\"Password\" name=\"mysql_password\" value=\"$mysql_password\"></td>
	</tr>
	<tr>
		<td><b>Mysql Database:</b></td>
		<td><input type=\"Text\" name=\"mysql_database\" value=\"$mysql_database\"></td>
	</tr></table>

	<br>
	This user will be added with prviledges to access the GCount database.
	<table>
	<tr>
		<td><b>Gcount Username:</b></td>
		<td><input type=\"Text\" name=\"gcount_username\" value=\"$gcount_username\"></td>
	</tr>
	<tr>
		<td><b>Gcount Password:</b></td>
		<td><input type=\"Text\" name=\"gcount_password\" value=\"$gcount_password\"></td>
	</tr>
	</table>
		
	<input type=\"Submit\" value=\"Install\">
</form>";

if ($_SERVER["REQUEST_METHOD"] == "GET") {
	print $form;
}
else {

print "Checking for all the necessary functions";
if (!function_exists("ImageCreateFromPNG")) { 
	print "$error: GD library not compiled into PHP.  Gcount can't function without the GD library (Version 1.8+)";
	die();
	}
else print " $ok";

$cn = @mysql_connect($mysql_server,$mysql_username,$mysql_password);
print "Connecting to the database server";
if (!$cn) { 
	print "$error connecting to MySql server at <b>'$mysql_server'</b><P><code>" . mysql_error() . "</code>\n$form"; 
	die();
	}
else { print $ok; }

print "Attempting to switch to the database";
if (!$message && $cn) { $result = @mysql_select_db($mysql_database); }

if (!$result) { 
	print "$error: switching to the database <b>$mysql_database</b> you must manually create the database first.<P>$form"; 
	die();
	}
else { print $ok; }

$sql1 = "CREATE TABLE webcounter (
	CntCount int(11),
	CntID int(11) NOT NULL auto_increment,
	CntName varchar(25),
	PRIMARY KEY (CntID)
);";

$sql2 = "CREATE TABLE counterlog (
 	logID int(11) NOT NULL auto_increment,
   CntID int(11),
	logHost varchar(50),
	logRefer varchar(250),
	logBrowser varchar(50),
	logBrowserVersion varchar(10),
	logOS varchar(30),
	logOSVersion varchar(10),
	logTimeStamp timestamp(14),
	PRIMARY KEY (logID),
	INDEX counter(CntID)
);";

$sql3 = "GRANT SELECT, INSERT, UPDATE ON $mysql_database.* TO $gcount_username@'%' IDENTIFIED BY '$gcount_password';";
$sql4 = "FLUSH PRIVILEGES;";

print "Creating the first database table";
$rs = @mysql_query($sql1);
if (!$rs) { 
	print "$error: creating the first table...  do you have insert permissions on the database?<br><br><code>" . mysql_error() . "</code>\n<P>\n$form"; 
	die();
}
else { print "$ok"; }

print "Creating the second database table";
$rs = @mysql_query($sql2);
if (!$rs) { 
	print "$error: creating the second table...  do you have insert permissions on the database?<P>$form"; 
	die();
	}
else { print "$ok"; }

print "Creating the user to access the GCount database";
$rs = @mysql_query($sql3);
if (!$rs) { die("$error: Could not create the user to access the database... do you have GRANT permissions?"); }
else { print "$ok"; }

print "Reloading user permissions";
$rs = @mysql_query($sql4);
if (!$rs) { die("Could not reload user permissions"); }
else { print $ok; }

$mysql_config = "mysql.php";

if (!is_writeable($mysql_config)) {
	print "mysql config file is NOT writeable please fix this and rerun this script<br>\n";
	die();
}

$echo = "\"<? \\\$server = '$mysql_server'; \\\$username = '$gcount_username'; \\\$password = '$gcount_password'; \\\$database = '$mysql_database'; ?>\"";

$cmd = "echo $echo > $mysql_config";
#print "\n\n\n\n$cmd\n\n\n\n\n";
system($cmd);

if (chmod($mysql_config,0444)) { print "Changed permissions on $mysql_config to be non-world writable $ok<br>\n"; }
else { print "Error changing permissions on $mysql_config, you should \"<code>chmod ugo-w $mysql_config\"</code><br>\n"; }

#if (unlink("install.php")) { print "Removed install.php<br>"; }
#else { print "Can't remove install.php you should do it manually<br>\n"; }

print "<P>You are now ready to start using <b>Gcount</b>!<br>Click <a href=\"INSTALL\">here</a> for more information on how to use GCount.<br><br><font color=\"red\" size=+1>Please delete install.php now!</font><BR><BR>";

}

?>
