<?php
session_start();
include('global.php');
if($_SESSION['amfradmin'] != $ausername || $_SESSION['amfrpassw'] != $apassword) {
session_destroy();
header('Location: login.php');
exit;
}
$link = mysql_connect($host, $dbuser, $dbpass);
echo '<HTML>
<HEAD>
<TITLE>Admin</TITLE>
</HEAD>
<BODY BGCOLOR="white" LINK="blue" ALINK="blue" VLINK="blue">
<CENTER>
<H1>Admin</H1>';
echo '<table border=1><tr><th><A HREF="admin.php">Home</A></th><th><A HREF="admin.php?action=massmail">Mass mail users</A></th><th><A HREF="admin.php?action=keys">Manage keys</A></th><th><A HREF="admin.php?action=domains">Manage domain extensions/subdomains</A></th><th><A HREF="admin.php?action=logs">View logs</A></th><th><A HREF="admin.php?action=config">Update config</A></th></tr></table>';
$action = $_REQUEST['action'];
$aauser = $_REQUEST['aauser'];
$email = $_REQUEST['aaemail'];
$message = $_POST['message'];
$key = $_REQUEST['key'];
$newdomain = $_POST['newdomain'];
$deldomain = $_GET['deldomain'];
$year = $_POST['year'];
$month = $_POST['month'];
$day = $_POST['day'];
if($action == "delete" && $aauser) {
$sql = "DELETE FROM amfr_users WHERE user = '$aauser'";
$result3 = mysql_db_query($dbname, $sql);
$result3 = file_get_contents("http://$cpuser:$cppass@localhost:2086/scripts/killacct?domain=$aauser&user=$aauser&submit-user=Terminate");
echo "$aauser deleted!<BR>";
echo "<A HREF='admin.php'>Back</A>";
}
elseif($action == "config")
{
$confform = <<<CONFIGFORM
<form action="admin.php" method="post">
<table border=1>
<tr>
	<th>WHM Username</th>
</tr>
<tr>
	<td><center>Your WHM username</center></td>
</tr>
<tr>
<td><center>
	<input type="text" name="conf_cpuser" value="$cpuser">
</td></center>
</tr>
<tr>
	<th>WHM Password</th>
</tr>
<tr>
	<td><center>Your WHM password</center></td>
</tr>
<tr>
<td><center>
	<input type="password" name="conf_cppass" value="$cppass">
</center></td>
</tr>
<tr>
	<th>WHM Package</th>
</tr>
<tr>
	<td><center>A package that you have set up in WHM that you want users to have</center></td>
</tr>
<tr>
<td><center>
	<input type="text" name="conf_cppackage" value="$cppackage">
</center></td>
</tr>
<tr>
	<th>Use Signup Keys</th>
</tr>
<tr>
	<td><center>Read readme for more info</center></td>
</tr>
<tr>
<td><center>
CONFIGFORM;
if($skeys == "yes")
{
$confform .= '<select name="conf_skeys"><option value="yes" selected>Yes</option><option value="no">No</option></select>';
}
else
{
$confform .= '<select name="conf_skeys"><option value="yes">Yes</option><option value="no" selected>No</option></select>';
}
$confform .= '
</center></td>
</tr>
';
$confform .= <<<CONFIGFORM2
<tr>
	<th>Use Multiple Signup Keys</th>
</tr>
<tr>
	<td><center>Users can user signup keys unlimited times</center></td>
</tr>
<tr>
<td><center>
CONFIGFORM2;
if($mkeys == "yes")
{
$confform .= '<select name="conf_mkeys"><option value="yes" selected>Yes</option><option value="no">No</option></select>';
}
else
{
$confform .= '<select name="conf_mkeys"><option value="yes">Yes</option><option value="no" selected>No</option></select>';
}
$confform .= '
</center></td>
</tr>
';
$confform .= <<<CONFIGF2
<tr>
	<th>Nameserver 1</th>
</tr>
<tr>
	<td><center>The 1st nameserver to use if a user signs up with a domain</center></td>
</tr>
<tr>
<td><center>
	<input type="text" name="conf_ns1" value="$ns1">
</center></td>
</tr>
<tr>
	<th>Nameserver 2</th>
</tr>
<tr>
	<td><center>The 2nd nameserver to use if a user signs up with a domain</center></td>
</tr>
<tr>
<td><center>
	<input type="text" name="conf_ns2" value="$ns2">
</center></td>
</tr>
<tr>
	<th>Template</th>
</tr>
<tr>
	<td><center>The subdirectory of the templates folder that your template is in</center></td>
</tr>
<tr>
<td><center>
	<input type="text" name="conf_template" value="$template">
</center></td>
</tr>
CONFIGF2;
$confform .= <<<CONFIGFORM3
<tr>
	<th>Use Image Verification</th>
</tr>
<tr>
	<td><center>Make users type in a number from a random image for bot prevention, requires GD</center></td>
</tr>
<tr>
<td><center>
CONFIGFORM3;
if($image == "yes")
{
$confform .= '<select name="conf_image"><option value="yes" selected>Yes</option><option value="no">No</option></select>';
}
else
{
$confform .= '<select name="conf_image"><option value="yes">Yes</option><option value="no" selected>No</option></select>';
}
$confform .= '
</center></td>
</tr>
<tr>
<td>
<input type="hidden" name="action" value="updateconfig">
<center><input type="submit" value="Update Config!"></center>
</td>
</tr>
</table><br>
<a href="admin.php">Back</a>
';
echo $confform;
}
elseif($action == "updateconfig" && $_POST['conf_template'] && $_POST['conf_ns1'] && $_POST['conf_ns2'] && $_POST['conf_cpuser'] && $_POST['conf_cppass'] && $_POST['conf_cppackage'])
{
  amfrsetval('template', $_POST['conf_template']);
  amfrsetval('ns1', $_POST['conf_ns1']);
  amfrsetval('ns2', $_POST['conf_ns2']);
  amfrsetval('cpuser', $_POST['conf_cpuser']);
  amfrsetval('cppass', $_POST['conf_cppass']);
  amfrsetval('cppackage', $_POST['conf_cppackage']);
  amfrsetval('skeys', $_POST['conf_skeys']);
  amfrsetval('mkeys', $_POST['conf_mkeys']);
  amfrsetval('image', $_POST['conf_image']);
echo '<b>Config updated!</b><br><a href="admin.php?action=config">Back</a>';
}
elseif($action == "email" && $email) {
echo '<FORM ACTION="admin.php" METHOD="post">
Message: <TEXTAREA NAME="message" ROWS="15" COLS="80"></TEXTAREA><BR>
<INPUT TYPE="hidden" NAME="aaemail" VALUE="'.$email.'">
<INPUT TYPE="hidden" NAME="action" VALUE="email2">
<INPUT TYPE="submit" VALUE="Send Email">
</FORM>
<A HREF="admin.php">Back</A>';
}
elseif($action == "keys") {
echo '<table border=1><tr><td><A HREF="admin.php?action=newkey">Add new key</A></td><td><A HREF="admin.php?action=listkeys">List keys</A></td><td><A HREF="admin.php?action=delkeys">Delete keys</A></td><td><A HREF="admin.php">Back</A></td></tr></table>';
}
elseif($action == "newkey") {
echo '<FORM ACTION="admin.php" METHOD="post">
New key: <INPUT TYPE="text" NAME="key"><BR>
<INPUT TYPE="hidden" NAME="action" VALUE="newkey2">
<INPUT TYPE="submit" VALUE="Add Key">
</FORM>
<A HREF="admin.php?action=keys">Back</A>';
}
elseif($action == "logs") {
echo '<FORM ACTION="admin.php" METHOD="post">
<B>Please enter the date of the logs that you wish to view. You do not have to fill in all fields.</B><BR>
Month (1-12): <INPUT TYPE="text" NAME="month"> Day(1-31): <INPUT TYPE="text" NAME="day"> Year(e.g. 2005): <INPUT TYPE="text" NAME="year"><BR>
<INPUT TYPE="hidden" NAME="action" VALUE="viewlogs">
<INPUT TYPE="submit" VALUE="View Logs">
</FORM>
<A HREF="admin.php">Back</A>';
}
elseif($action == "viewlogs") {
if($day && $month && $year)
{
$logq = mysql_db_query($dbname, "select * from amfr_logs where day='$day' and month='$month' and year='$year'");
}
elseif(!$day && $month && $year)
{
$logq = mysql_db_query($dbname, "select * from amfr_logs where month='$month' and year='$year'");
}
elseif($day && !$month && $year)
{
$logq = mysql_db_query($dbname, "select * from amfr_logs where day='$day' and year='$year'");
}
elseif($day && $month && !$year)
{
$logq = mysql_db_query($dbname, "select * from amfr_logs where month='$month' and day='$day'");
}
elseif(!$day && !$month && $year)
{
$logq = mysql_db_query($dbname, "select * from amfr_logs where year='$year'");
}
elseif(!$day && $month && !$year)
{
$logq = mysql_db_query($dbname, "select * from amfr_logs where month='$month'");
}
elseif($day && !$month && !$year)
{
$logq = mysql_db_query($dbname, "select * from amfr_logs where day='$day'");
}
else
{
echo '<B>Please fill in at least one field!</B><BR>';
$logdisplay = "no";
}
if($logq && !$logdisplay)
{
while($row = mysql_fetch_assoc($logq))
{
$user = $row['user'];
$logday = $row['day'];
$logyear = $row['year'];
$logmonth = $row['month'];
echo "<b>$user</b> signed up on $logmonth/$logday/$logyear<br>";
}
}
elseif(!$logq && !$logdisplay)
{
echo '<b>No records for the date you entered!</b><br>';
}
echo '<A HREF="admin.php?action=logs">Back</A>';
}
elseif($action == "newkey2" && $key) {
$sql = "INSERT INTO amfr_keys values('$key')";
$result10 = mysql_db_query($dbname, $sql);
echo '<B>Key inserted!</B><BR><A HREF="admin.php?action=keys">Back</A>';
}
elseif($action == "delkey2" && $key) {
$sql = "DELETE FROM amfr_keys WHERE amfrkey='$key'";
$result11 = mysql_db_query($dbname, $sql);
echo '<B>Key deleted!</B><BR><A HREF="admin.php?action=keys">Back</A>';
}
elseif($action == "listkeys") {
if($mkeys == "yes")
{
echo "<B>Keys:</B><BR>";
}
else
{
echo "<B>Unused keys:</B><BR>";
}
$sql = "SELECT * FROM amfr_keys";
$ukeys = mysql_db_query($dbname, $sql);
while ($row = mysql_fetch_assoc($ukeys)) {
$key = $row['amfrkey'];
echo "$key<BR>";
}
if($mkeys == "yes")
{
echo "<BR><B>Key usage:</B><BR>";
}
else
{
echo "<BR><B>Used keys:</B><BR>";
}
$sql = "SELECT * FROM amfr_used_keys";
$dkeys = mysql_db_query($dbname, $sql);
while ($row = mysql_fetch_assoc($dkeys)) {
$key = $row['amfrkey'];
$user = $row['user'];
echo "$key - Used by $user<BR>";
}
echo '<BR><A HREF="admin.php?Action=keys">Back</A>';
}
elseif($action == "delkeys") {
echo "<B>Click on a key to delete it:</B><BR>";
$sql = "SELECT * FROM amfr_keys";
$delkeys = mysql_db_query($dbname, $sql);
while ($row = mysql_fetch_assoc($delkeys)) {
$key = $row['amfrkey'];
echo "<A HREF='admin.php?action=delkey2&key=$key'>$key</A><BR>";
}
echo '<A HREF="admin.php?action=keys">Back</A>';
}
elseif($action == "email2" && $email && $message) {
mail($email, 'Free Hosting Message', $message, 'From: Automailer');
echo "<B>Message sent!</B><BR><A HREF='admin.php'>Back</A>";
}
elseif($action == "domains") {
$query = mysql_db_query($dbname, 'select * from amfr_domains');
while($row = mysql_fetch_assoc($query))
{
$dom = $row['domain'];
echo $dom.' [<a href="admin.php?action=deldomain&deldomain='.$dom.'">Delete</a>]<br>';
}
echo '<FORM ACTION="admin.php" METHOD="post">
Domain (can be entered as "com" for domains or "yourname.com" for subdomains, no "." before domain):<BR>
<INPUT TYPE="text" NAME="newdomain"><BR>
<INPUT TYPE="hidden" NAME="action" VALUE="adddomain">
<INPUT TYPE="submit" VALUE="Add domain extension or subdomain">
</FORM>
<A HREF="admin.php">Back</A>';
}
elseif($action == "adddomain" && $newdomain) {
$query = mysql_db_query($dbname, 'insert into amfr_domains values("'.$newdomain.'")');
echo "<B>Domain added!</B><BR><A HREF='admin.php?action=domains'>Back</A>";
}
elseif($action == "deldomain" && $deldomain) {
$query = mysql_db_query($dbname, 'delete from amfr_domains where domain="'.$newdomain.'"');
echo "<B>Domain deleted!</B><BR><A HREF='admin.php?action=domains'>Back</A>";
}
elseif($action == "massmail") {
echo '<FORM ACTION="admin.php" METHOD="post">
Message: <TEXTAREA NAME="message" ROWS="15" COLS="80"></TEXTAREA><BR>
<INPUT TYPE="hidden" NAME="action" VALUE="mmail">
<INPUT TYPE="submit" VALUE="Send Email">
</FORM>
<A HREF="admin.php">Back</A>';
}
elseif($action == "mmail" && $message) {
$sql = "SELECT * FROM amfr_users";
$result8 = mysql_db_query($dbname, $sql);
while ($row = mysql_fetch_assoc($result8)) {
mail($email, "Free Hosting Message", $message, "From: Automailer");
}
echo "<B>Messages sent!</B><BR><A HREF='admin.php'>Back</A>";
}
else {
echo "<br><b>Users:</b><br>";
echo "<table border=1><tr><th>Username</th><th>Delete</th><th>Email</th></tr>";
$sql = "SELECT * FROM amfr_users";
$result8 = mysql_db_query($dbname, $sql);
while ($row = mysql_fetch_assoc($result8)) {
$user = $row['user'];
$email = $row['email'];
$domain = $row['domain'];
echo "<tr><td><A HREF='http://$domain'>$user</A></td><td><A HREF='admin.php?action=delete&aauser=$user'>Delete</A></td><td><A HREF='admin.php?action=email&aaemail=".urlencode($email)."'>Email ($email)</A></td></tr>";
}
if(mysql_num_rows($result8) == 0)
{
echo '<tr><td colspan=3><b><center>No users!</center></b></td></tr>';
}
echo '</table>';
}
echo "</CENTER></BODY></HTML>";
mysql_close($link);
?>
