<html>
<head>
<meta name="GENERATOR" content="Microsoft FrontPage 6.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<link href="altauth_style.css" rel="stylesheet" type="text/css">
<title>John's Little Script For Running A Membership Site</title>
<style>
    BODY
    {
      font-family: Verdana;
      font-size: 14px;
    }
    TD
    {
      font-size: 14px;
    }
  </style>
  
</head>

<body rightMargin=0 leftMargin=0 topMargin=0 bottomMargin=0 marginheight="0" marginwidth="0">

<div align="center">
  <table border="0" cellpadding="0" style="border-collapse: collapse" width="100%" id="table1">
    <tr>
      <td>
      <p align="center">
      <img border="0" src="altauth_adminarea_header.jpg" width="829" height="199" alt="John's Little Script"></td>
    </tr>
  </table>
</div>
<div align="center">
  <table border="0" cellpadding="0" style="border-collapse: collapse" width="829" id="table2">
    <tr>
      <td background="altauth_adminarea_bg.jpg">
      <div align="center">
        <table border="0" cellpadding="5" style="border-collapse: collapse" width="686" id="table8">
          <tr>
            <td>


<?


require_once('altauth_lib.php');


$config = array();
$config_keys = array('iprestrict', 'afterlogout', 'afterregister', 'resetpassurl');

$regcodes = array();
$regcodes_file = 'altauth_regcodes.txt';

$html_header = '';
$html_footer = '';

function do_set_config()
{
	global $config;
	global $config_keys;
	
	foreach($config_keys as $key)
	{
		$file = "altauth_$key.txt";
		$param = trim($_REQUEST[$key]);
		
		if (isset($config[$key]) && $param == $config[$key])
			continue;
		
		if ( $param )
		{
			$fh = fopen($file, 'w')
				or die("FAILURE: cannot create/rewrite $file");
			fwrite($fh, $param);
			fclose($fh);
			
			$config[$key] = $param;
		}
		else
		{
			if (file_exists($file))
				unlink($file);
			
			$config[$key] = '';
		}
	}
	
	print '<center><b style="color:blue">Configuration has been updated</b></center>';
}

function do_add_user()
{
	$username = strtolower($_REQUEST['add_user_username']);
	$password = $_REQUEST['add_user_password'];
	
	if ( ! $username || ! $password )
	{
		print '<center><b style="color:red">* Both Username and Password fields are required</b></center>';
		return;
	}
	
	if (has_user($username))
	{
		print "<center><b style='color:red'>* User with username $username is already in the database</b></center>";
	}
	else
	{
		save_user($username, $password);
		$userdb = load_userdb();
		$userdb[$username]['regdate'] = date("Y-m-d G:i:s");
		$userdb[$username]['regcode'] = '';
		save_userdb($userdb);
		print "<center><b style='color:blue'>User $user has been added to the database</b></center>";
		$_REQUEST['add_user_username'] = '';
	}
}

function do_add_regcode()
{
	$code = trim($_REQUEST['add_regcode']);
	
	if ( ! $code ) return;
	
	global $regcodes;
	global $regcodes_file;

	if (in_array($code, $regcodes) )
	{
		print "<center><b style='color:red'>Registration code $code already exists</b></center>";
		return;
	}

	$regcodes[] = $code;
	$fh = fopen($regcodes_file, 'w')
		or die("FAILURE: cannot create/rewrite $file");
	fwrite($fh, join("\n", $regcodes));
	fclose($fh);
	
	print "<center><b style='color:blue'>Registration code $code has been added</b></center>";
}

function do_del_regcode()
{
	$code = $_REQUEST['do_del_regcode'];
	
	global $regcodes;
	global $regcodes_file;
	
	$pos = -1;
	
	for ($i = 0 ; $i < count($regcodes); $i++)
	{
		if ($regcodes[$i] == $code)
		{
			$pos = $i;
			break;
		}
	}
	
	if ($pos >= 0)
	{
		array_splice($regcodes, $pos, 1);
	}
	
	$fh = fopen($regcodes_file, 'w')
		or die("FAILURE: cannot create/rewrite $file");
	fwrite($fh, join("\n", $regcodes));
	fclose($fh);
	
	print "<center><b style='color:blue'>Registration code $code has beed deleted</b></center>";
}

function do_clear_ip($username)
{
	$userdb = load_userdb();
	
	if ( isset($userdb[$username]) )
		$userdb[$username]['ip'] = array();
	
	save_userdb($userdb);
	
	print "<center><b style='color:blue'>Logged IP history for user $username has been cleared</b></center>";
}

function do_change_headfoot()
{
	$files = array( 'html_header' => 'altauth_header.php', 'html_footer' => 'altauth_footer.php' );
	
	foreach($files as $key => $file)
	{
		$param = stripslashes(trim($_REQUEST[$key]));
		
		if (isset($GLOBALS[$key]) && $param == $GLOBALS[$key])
			continue;
		
		if ( $param )
		{
			$fh = fopen($file, 'w')
				or die("FAILURE: cannot create/rewrite $file");
			fwrite($fh, $param);
			fclose($fh);
			
			$GLOBALS[$key] = $param;
		}
		else
		{
			if (file_exists($file))
				unlink($file);
			
			$GLOBALS[$key] = '';
		}
	}
	
	print '<center><b style="color:blue">Header and footer have been updated</b></center>';
}

//---------------------------------------- M A I N --------------------------------------

// check admin authorization
if ( ! is_admin($_COOKIE['altauth_user']) )
{
	print '<br><center><b style="color:red">* This page is available for Administator only!</b></center>';
	print '<br><center><a href="logout.html">Logout and try again</a></center>';
	return;
}

// load config values
foreach ($config_keys as $key)
{
	if (file_exists("altauth_$key.txt"))
		$config[$key] = trim(join('', file("altauth_$key.txt")));
}

// load regcodes
if ( file_exists($regcodes_file) )
{
	$regcodes = split("\n", trim(join('', file($regcodes_file))));
}			

// load html header and footer
if ( file_exists('altauth_header.php') )
{
	$html_header = file_get_contents('altauth_header.php');
}			
if ( file_exists('altauth_footer.php') )
{
	$html_footer = file_get_contents('altauth_footer.php');
}			

// do admin actions
if ( isset($_REQUEST['do_set_config']) )
{
	do_set_config();
}
else if ( isset($_REQUEST['do_add_user']) )
{
	do_add_user();
}
else if ( isset($_REQUEST['do_add_regcode']))
{
	do_add_regcode();
}
else if ( isset($_REQUEST['do_del_regcode']) && $_REQUEST['do_del_regcode'] )
{
	do_del_regcode();
}
else if ( isset($_REQUEST['do_clear_ip']) && $_REQUEST['do_clear_ip'] )
{
	do_clear_ip($_REQUEST['do_clear_ip']);
}
else if ( isset($_REQUEST['do_change_password']) &&  $_REQUEST['do_change_password'])
{
	list($username, $password) = split(':', $_REQUEST['do_change_password']);
	save_user($username, $password);
	print "<center><b style='color:blue'>Password of the user $username has been changed</b></center>";
}
else if ( isset($_REQUEST['do_del_user']) && $_REQUEST['do_del_user'] )
{
	$username = $_REQUEST['do_del_user'];
	delete_user($username);
	print "<center><b style='color:blue'>User $username has been deleted</b></center>";
}
else if ( isset($_REQUEST['do_change_style']) && $_REQUEST['do_change_style'] )
{
	$style = $_REQUEST['do_change_style'];
	copy("altauth_style_$style.css", "altauth_style.css")
		or die("FAILURE: failed to copy altauth_style_$style.css to altauth_style.css");
	print "<center><b style='color:blue'>Style has been changed. If you don't see the change, please press Ctrl-F5 to refresh your browser cache</b></center>";
}
else if ( isset($_REQUEST['do_change_headfoot']))
{
	do_change_headfoot();
}
?>

<br>

<div align="right"><a href="logout.html">Logout</a></div>
<br>

<center>

<form method=post name="ConfigForm" action="<?=$_SERVER[PHP_SELF]?>">
	<table border=0 cellpadding=2 cellspacing=2>
	<tr><td class=headfoot colspan="3">Configuration</td></tr>
	<tr>
		<td class=content>&nbsp;IP restrict</td>
		<td class=content><input type="text" size="5" name="iprestrict" value="<?=isset($config['iprestrict']) ? $config['iprestrict'] : ''?>"></td>
	</tr>
	<tr>
		<td class=content>&nbsp;After logout URL</td>
		<td class=content><input type="text" size="40" name="afterlogout" value="<?=isset($config['afterlogout']) ? $config['afterlogout'] : ''?>"></td>
	</tr>
	<tr>
		<td class=content>&nbsp;After register URL</td>
		<td class=content><input type="text" size="40" name="afterregister" value="<?=isset($config['afterregister']) ? $config['afterregister'] : ''?>"></td>
	</tr>
	<tr>
		<td class=content>&nbsp;Reset password URL</td>
		<td class=content><input type="text" size="40" name="resetpassurl" value="<?=isset($config['resetpassurl']) ? $config['resetpassurl'] : ''?>"></td>
	</tr>
	<tr>
		<td class=headfoot colspan="3"><input class=button type="Submit" name="do_set_config" value="Set"></td>
	</tr>
	</table>  
</form>

<form method=post name="RegisterForm" action="<?=$_SERVER[PHP_SELF]?>">
	<table border=0 cellpadding=2 cellspacing=2>
	<tr><td class=headfoot>Add new user</td></tr>
	<tr><td class=content>
		<table align=center>
		<tr>
			<td class=content>Username</td>
			<td class=content><input type="text" size="20" maxsize="30" name="add_user_username" value="<?=isset($_REQUEST['add_user_username']) ? $_REQUEST['add_user_username'] : '' ?>"></td>
		</tr>
		<tr>
			<td class=content>Password</td>
			<td class=content><input type="text" size="20" maxsize="30" name="add_user_password"></td>
		</tr>
		</table>
	</td></tr>
	<tr>
		<td class=headfoot><input class=button type="Submit" name="do_add_user" value="Add"></td>
	</tr>
	</table>  
</form>

<form method=post name="RegcodesForm" action="<?=$_SERVER[PHP_SELF]?>">
<input type="hidden" name="do_del_regcode">
<table border=0 cellpadding=2 cellspacing=2>
<tr><td class=headfoot colspan="2">Registration codes</td></tr>
<?
	foreach ($regcodes as $code)
	{
		$link = 'http://' . $_SERVER[HTTP_HOST] . preg_replace('/\/[^\/]+$/', '', $_SERVER['PHP_SELF']) . "/altauth_register.php?regcode=" . urlencode($code);
?>
<tr>
	<td class=content width="200px"><b><?=$code?></b></td>
	<td class=content width="10px"><a href="<?=$link?>" target="_blank">link</a>&nbsp;|&nbsp;<a href="#" onClick="if (confirm('Are you sure you want to delete this registration code?')) { RegcodesForm.do_del_regcode.value = '<?=htmlspecialchars($code)?>'; RegcodesForm.submit(); }">delete</a></td>
</tr>
<?
	}
?>
<tr>
	<td class=headfoot align=center colspan="2">
		New code
		<input type="text" size="15" name="add_regcode" value="">
		<input class="button" type="Submit" name="do_add_regcode" value="Add">
	</td>
</tr>
</table>
</form>

<form name="AdminForm" method=post action="<?=$_SERVER[PHP_SELF]?>">
<input type="hidden" name="do_clear_ip">
<input type="hidden" name="do_change_password">
<input type="hidden" name="do_del_user">
<table border=0 cellpadding=0 cellspacing=1>
	<tr>
		<td class=headfoot colspan="6">Users database</td>
	</tr>
	<tr>
		<td class=caption>Username</td>
		<td class=caption>Reg.date</td>
		<td class=caption>Reg.code</td>
		<td class=caption colspan="2">IP Address</td>
		<td class=caption>Actions</td>
	</tr>
	
<?

$passwd_file = locate_passwd_file();
$users = array();

foreach (file($passwd_file) as $line)
{
	list($username) = split(':', $line);
	$users[$username] = '';
}
		
$userdb = load_userdb();

foreach ( array_keys($userdb) as $username)
{
	if ( ! isset($users[$username]) ) continue;
			
	$users[$username]['regdate'] = $userdb[$username]['regdate'];
	$users[$username]['regcode'] = $userdb[$username]['regcode'];
	$users[$username]['ip'] = join('<br>', $userdb[$username]['ip']);
}

ksort($users);
		
foreach (array_keys($users) as $username)
{
?>
	<tr>
		<td class=content>&nbsp;<?=$username?></td>
		<td class=content>&nbsp;<?=$users[$username]['regdate']?></td>
		<td class=content>&nbsp;<?=$users[$username]['regcode']?></td>
		<td class=content><?=$users[$username]['ip']?></td>
		<td class=content valign="middle">
			<? if($users[$username]['ip']) { ?>
			<a href="#" onClick="
				if (confirm('Are you sure you want to clear IP history for this user: <?=$username?>?'))
				{
					AdminForm.do_clear_ip.value = '<?=$username?>'; 
					AdminForm.submit();
				}">
				[x]
			</a>
			<? } ?>
		</td>
		<td class=content>
			<a href="#" onClick="
				if (AdminForm.do_change_password.value = prompt('Please enter a new password this user: <?=$username?>:', ''))
				{
					AdminForm.do_change_password.value = '<?=$username?>:' + AdminForm.do_change_password.value; 
					AdminForm.submit();
				}">
				ch.pass
			</a>&nbsp;&nbsp;
			<?if(! is_admin($username)){?><a href="#" onClick="
				if (confirm('Are you sure you want to remove this user: <?=$username?>?'))
				{
					AdminForm.do_del_user.value = '<?=$username?>'; 
					AdminForm.submit();
				}">
				del.user
			</a><?}?>
		</td>
	</tr>
<?
}
?>
	
	<tr><td class=headfoot align=center colspan=6>&nbsp;</td></tr>
</table>
</form>

<table border=0 cellpadding=2 cellspacing=2>
<tr><td class=headfoot colspan="2">Style</td></tr>
<tr>
	<td class=content>&nbsp;&nbsp;
		<a href="<?=$_SERVER['PHP_SELF']?>?do_change_style=basic">basic</a> |
		<a href="<?=$_SERVER['PHP_SELF']?>?do_change_style=blue">blue</a> |
		<a href="<?=$_SERVER['PHP_SELF']?>?do_change_style=green">green</a> |
		<a href="<?=$_SERVER['PHP_SELF']?>?do_change_style=red">red</a> |
		<a href="<?=$_SERVER['PHP_SELF']?>?do_change_style=orange">orange</a> |
		<a href="<?=$_SERVER['PHP_SELF']?>?do_change_style=grey">grey</a>
		&nbsp;&nbsp;
	</td>
</tr>
</table>
<br>

<form name="HeadfootForm" method=post action="<?=$_SERVER[PHP_SELF]?>">
<table border=0 cellpadding=2 cellspacing=2>
<tr><td class=headfoot colspan="2">HTML header (PHP code allowed)</td></tr>
<tr><td class=content><textarea rows=7 cols=60 name="html_header"><?=htmlspecialchars($html_header)?></textarea></td></tr>
<tr><td class=headfoot colspan="2">HTML footer (PHP code allowed)</td></tr>
<tr><td class=content><textarea rows=7 cols=60 name="html_footer"><?=htmlspecialchars($html_footer)?></textarea></td></tr>
<tr>
	<td class=headfoot align=center>
		<input class="button" type="Submit" name="do_change_headfoot" value="Save header & footer">
	</td>
</tr>
</table>
</form>

</center>
<br>

</td>
          </tr>
        </table>
      </div>

      </td>
    </tr>
    <tr>
      <td>
      <p align="center">
      <img border="0" src="altauth_adminarea_footer.jpg" width="829" height="98"></td>
    </tr>
  </table>
</div>

<?
	print_copyright();
	print_advertisement();
?>

</body>

</html>
