<?
	error_reporting(0);
	include("control/auth/users.php");

	session_start();
	session_register("SESSION");

	if (! isset($SESSION)) {
	$SESSION = array();
	}
	
	if($event=='exit') {
	unset ($SESSION["password"]);
	unset ($SESSION["username"]);
	}

	if($enter) { 
	
	$SESSION["username"] = $user;
	$SESSION["password"] = md5($passw);
	
	} 

	$username = $SESSION["username"];
	$password = $SESSION["password"];

$dd = array_search($password, $Users);
?>

<style type=text/css>
input { FONT-FAMILY: MS Sans Serif; FONT-SIZE: 10px; }
select { FONT-FAMILY: MS Sans Serif; FONT-SIZE: 10px; }
a:hover { color: #86869B }
a:visited { color: navy }
a { color: navy }
a:active { color: #ff0000 }
body { FONT-FAMILY: Times New Roman; FONT-SIZE: 13pt; COLOR: #1F1F1F; }
</style>

<? if (empty($password) or $dd !== $username) { ?>
<center>
<form action="<?=$PHP_SELF?>" method="post">
<h3 style="color:red">Аутентификация пользователя</h3>
<table align="center" border="0">
	<tr>
		<td align="center" colspan="2"><b>Пожалуйста, введите логин и пароль</td>
	</tr>
	<tr>
		<td align="right">Login:</td>
		<td><input type="text" name="user" size="22"></td>
	</tr>
	<tr>
		<td align="right">Password:</td>
		<td><input type="password" name="passw" size="22"></td>
	</tr>
	<tr>
		<td align="right"></td>
		<td><input type="submit" value="Send data" name="enter">
<input type="reset" value=" Clear ">
</td>
	</tr>
	<tr>
		<td align="right" colspan="2"><BR><!-- 
<small>This software programming by <a href="http://mouse.org.ru" target="_blank">mouse.org.ru</a> -->
</td>
	</tr>
</table>
</form>
<? 
	die(); 
	} 
?>
<div style="position:absolute;top:63;left:35%" align="center">
<small><b><?=$dd?></b> /
<? if($event!=='chpassw') { ?> 
<a href="<?=$PHP_SELF?>?event=chpassw">
<? } ?>
Сменить пароль</a> / 
<? if($event!=='add') { ?> 
<a href="<?=$PHP_SELF?>?event=add">
<? } ?>
Добавить пользователя</a> / 
<a href="<?=$PHP_SELF?>?event=exit">
Выход</a>
</small>
</div>

<? 
	if($event=='chpassw') { 

	if($Change) { 
	if(md5($Old)!==$password) { print "Неверен текущий пароль"; };
	if($New!==$Re or empty($New)) { 
	print "Новый пароль подтвержден неверно"; };
	if(md5($Old)==$password && $New==$Re && !empty($New)) { 

	$filename = "control/auth/users.php";
	$fd = fopen ($filename, 'r');
	$contents = fread ($fd, filesize ($filename));
	fclose ($fd);

	$Search = '"'.$username.'" => "'.$password.'"';
	$Replace = '"'.$username.'" => "'.md5($New).'"';
	$contents = str_replace($Search,$Replace,$contents);
	$fd = fopen ($filename, 'w');
	fwrite ($fd, $contents);
	fclose ($fd);
	print "Пароль успешно сменен на ".$New; 
	$SESSION["password"] = md5($New);
	};
	}
?>
<table align="center">
<form action="<?=$PHP_SELF?>?event=chpassw" method="post">
	<tr>
		<td><small>Старый пароль:</td>
		<td><small>Новый пароль:</td>
		<td><small>Повторите:</td>
	</tr>
	<tr>
		<td><input type="password" size="20" name="Old"></td>
		<td><input type="password" size="20" name="New"></td>
		<td><input type="password" size="20" name="Re"></td>
		<td><input type="submit" value="Сменить" name="Change"></td>
	</tr>
</form>
</table>
<? } ?>

<? 
	if($event=='add') { 

	if($Add) { 
	if(empty($UserName)) { print "Укажите имя пользователя"; };
	if($Pass!==$Re or empty($Pass)) { 
	print "Пароль подтвержден неверно"; };
	if($Pass==$Re && !empty($UserName)) { 

	$filename = "control/auth/users.php";
	$fd = fopen ($filename, 'r');
	$contents = fread ($fd, filesize ($filename));
	fclose ($fd);

	$S1 = ',"'.$UserName.'" => "'.md5($Pass).'"';
	$contents = str_replace($S1,"",$contents);

	$Search = "// End";
	$Replace = ',"'.$UserName.'" => "'.md5($Pass).'"
// End';
	$contents = str_replace($Search,$Replace,$contents);
	$fd = fopen ($filename, 'w');
	fwrite ($fd, $contents);
	fclose ($fd);
	print "Пользователь ".$UserName." создан. Пароль - ".$Pass; 
	};
	}
?>


<table align="center">
<form action="<?=$PHP_SELF?>?event=add" method="post">
	<tr>
		<td><small>Имя пользователя:</td>
		<td><small>Пароль:</td>
		<td><small>Повторите:</td>
	</tr>
	<tr>
		<td><input type="text" size="20" name="UserName"></td>
		<td><input type="password" size="20" name="Pass"></td>
		<td><input type="password" size="20" name="Re"></td>
		<td><input type="submit" value="Создать" name="Add"></td>
	</tr>
</form>
</table>

<? } ?>










