<?php
// Tests/edits database config file

error_reporting (E_ALL ^ E_NOTICE);

function p_line($line) {
	echo '<p>'.$line."</p>\n";
}

function web_line($line = '') {
	echo $line."<br />\n";
}


// ------------------------------------------------------------------


// Test for data
if (isset ($_GET['del'])) {
	@unlink ($_SERVER['PHP_SELF']);

	if (file_exists ($_SERVER['PHP_SELF'])) {
		web_line ('Could not delete file.  Delete manually via FTP');
	} else {
		web_line ('Site secured');
	}
	exit();
}


if (isset ($_POST['write'])) {
	$db_configuration = <<<EOF
<?php // Include only

	\$dbhost = '{$_POST['dbhost']}';
	\$dbuser = '{$_POST['dbuser']}';
	\$dbpass = '{$_POST['dbpass']}';
	\$dbname = '{$_POST['dbname']}';
	\$OekakiPoteto_Prefix = '{$_POST['dbprefix']}';
	\$OekakiPoteto_MemberPrefix = '{$_POST['OekakiPoteto_MemberPrefix']}';

	\$dbconn = mysql_connect(\$dbhost, \$dbuser, \$dbpass) or die('Could not connect to the MySQL database.');
	\$mySQLDB = mysql_select_db(\$dbname);

?>
EOF;

	$fp = @fopen ('dbconn.php', 'w');
	if ($fp) {
		fwrite ($fp, $db_configuration);
		fclose ($fp);
		web_line('Database config file written.  <a href="'.$_SERVER['PHP_SELF'].'">Click here to test</a>.');
	} else {
		web_line('Database config file is locked -- file not written.');
	}

	exit();
}


// ------------------------------------------------------------------


echo <<<EOF
<html>
<body>

EOF;

// Import and evaluate dbconn.php file
{
	$eval_source = @file_get_contents ('dbconn.php');

	if (!$eval_source) {
		web_line ("Cannot open dbconn.php file!");
		exit();
	}

	$eval_source = str_replace ('<'.'?php', '<'.'?', $eval_source);
	$eval_source = preg_replace ('/\sor die.*;/', ';', $eval_source);

	ob_start();
	$eval_result = eval ('?'.'>'.trim ($eval_source).'<'.'?');
	$eval_output = ob_get_contents();
	ob_clean();

	if (!empty ($eval_output)) {
		web_line ("<strong>Database connection failed!</strong>");
		web_line ("<strong>Output:</strong>");
		echo ("<div style=\"background-color: yellow;\">{$eval_output}</div></strong>\n");
	} elseif ($dbconn && $mySQLDB) {
		web_line ("<strong>Database connection OK!</strong>");
		web_line ("<strong>Remember to delete this file for security reasons!</strong>");
		web_line ("<a href=\"{$_SERVER['PHP_SELF']}?del=1\">Click here to delete</a>.");
	} elseif (!$mySQLDB) {
		web_line ("<strong>Database Name &ldquo;{$dbname}&rdquo; not usable!</strong>");
	}
}


// Output editor
echo <<<EOF

	<hr />

	<p>Submit this form to update the database</p>

	<form name="form1" method="post" action="{$_SERVER['PHP_SELF']}">
		<input type="hidden" name="write" value="1" />

		<table class="infomain">
		<tr>
		<td class="infoask" valign="top">
			Host (usually &ldquo;localhost&rdquo;):
		</td>
		<td class="infoenter" valign="top">
			<input type="text" name="dbhost" size="40" value="{$dbhost}" />
		</td>

		<tr>
		<td class="infoask" valign="top">
			Database Name:
		</td>
		<td class="infoenter" valign="top">
			<input type="text" name="dbname" size="40" value="{$dbname}" />
		</td>
		</tr>

		<tr>
		<td class="infoask" valign="top">
			Database Username:
		</td>
		<td>
			<input type="text" name="dbuser" size="40" value="{$dbuser}" />
		<td class="infoenter" valign="top">
		</tr>

		<tr>
		<td class="infoask" valign="top">
			Database password:
		</td>
		<td class="infoenter" valign="top">
			<input type="text" name="dbpass" value="{$dbpass}" size="40" />
		</tr>

		<tr>
		<td class="infoask" valign="top">
			Main Table Prefix:
		</td>
		<td class="infoenter" valign="top">
			<input type="text" name="dbprefix" size="40" value="{$OekakiPoteto_Prefix}" />
		</td>
		</tr>

		<tr>
		<td class="infoask" valign="top">
			Member Table Prefix:
		</td>
		<td class="infoenter" valign="top">
			<input name="OekakiPoteto_MemberPrefix" type="text" id="OekakiPoteto_MemberPrefix" value="{$OekakiPoteto_MemberPrefix}" size="40" />
		</td>
		</tr>
		</table>

		<input type="submit" name="submit" value="submit" />
	</form>
</body>
<html>
EOF;


?>