<?
	if ($action=='logout') {
		setcookie ("metoo_cookielogin", "none", time()+3600*24*30, "/");
		setcookie ("metoo_cookiepassword", "none", time()+3600*24*30, "/");
	}
	if (($action=='testlogin') || ($action=='register')) {
		setcookie ("metoo_cookielogin", $login, time()+3600*24*30, "/");
		setcookie ("metoo_cookiepassword", $password, time()+3600*24*30, "/");
	}
	if (($action=='') || ($action=='change') || ($action=='changepass')) {
		$login = $HTTP_COOKIE_VARS["metoo_cookielogin"];
		$password = $HTTP_COOKIE_VARS["metoo_cookiepassword"];
	}
?>
<? include $DOCUMENT_ROOT."/zzzfilelist.inc"; ?>
<? include Inc("functions_general.inc.php"); $dont_inc_general=true; ?>
<?
	$fn=Inc("userlist.csv");
	$fp = fopen ($fn, "r");
	if (filesize($fn)>0) { $userlist = fread($fp, filesize($fn)); }
	fclose($fp);
	Decrunch($userlist);
	$usercell=$cell;
	$userlin=$lin;
	$usercol=$col;

	if ($action=='changepass') {
		if ($newpassword1 <> $newpassword2) {
			$mess=1;
		} else {
			$i=0;
			$found=false;
			do {
				$i++;
				if ($usercell[$i][1]==$login) { $found=true; }
			} while ((!$found) && ($i<$lin));
			$currentpassword = $usercell[$i][2];
			
			if (!$found) {
				$mess=2;
			} else {
				if ($oldpassword <> $currentpassword) {
					$mess=3;
				} else {
					$newpassword1 = Strip($newpassword1);
					$newpassword1 = str_replace("\\", "", $newpassword1);
					$newpassword1 = str_replace("\'", "'", $newpassword1);

					$usercell[$i][2]=$newpassword1;
					$savefile="";
					for ($i=1; $i<=$userlin; $i++) {
						for ($j=1; $j<=$usercol; $j++) {
							$savefile.=$usercell[$i][$j].";";
						}
						$savefile.=chr(10);
					}
	
					$fn=Inc("userlist.csv");
					$fp = fopen ($fn, "w");
					fwrite($fp, $savefile);
					fclose($fp);
					$mess=4;
					
					$password=$newpassword1;
					setcookie ("metoo_cookielogin", $login, time()+3600*24*30, "/");
					setcookie ("metoo_cookiepassword", $password, time()+3600*24*30, "/");
				}
			}
		}
	}
?>

<? $goingtologin=true; include Inc("metootop.inc.php"); ?>
<div align="center">
<?
	$saymess[1]=$loc[$loctag[3]+6][$lang];
	$saymess[2]=$loc[$loctag[3]+7][$lang];
	$saymess[3]=$loc[$loctag[3]+8][$lang];
	$saymess[4]=$loc[$loctag[3]+9][$lang];

	if ($action=='changepass') {
		echo "<br><font class='orangeBOLD'>";
		Message ($saymess[$mess],'');
		echo "</font><br>";
		$action="";
	}
?>
<?
	if ($action=='register') {
		$password = GenPassword();
	}
	$saymess1[1]=$loc[$loctag[3]+10][$lang];
	$saymess1[2]=$loc[$loctag[3]+11][$lang];
	$saymess1[3]=$loc[$loctag[3]+11][$lang];
	$saymess1[4]=$loc[$loctag[3]+5][$lang];
	$saymess2[1]=$loc[$loctag[3]+13][$lang];
	$saymess2[2]=$loc[$loctag[3]+14][$lang]."<br><br>";
	$saymess2[3]=$loc[$loctag[3]+15][$lang]."<br><br>";
	$saymess2[4]="";

	if (($action=='register') || ($action=='change')) {
	
		$fn=Inc("userlist.csv");
		$fp = fopen ($fn, "r");
		if (filesize($fn)>0) { $userlist = fread($fp, filesize($fn)); }
		fclose($fp);
		clearstatcache();
		Decrunch($userlist);
		$usercell=$cell;
		$userlin=$lin;
		$usercol=$col;
		$savefile=$userlist;

		$i=0;
		$found=false;
		do {
			$i++;
			if (($usercell[$i][1]==$login) && ($usercell[$i][1]!="")) { $found=true; }
		} while ((!$found) && ($i<$userlin));
		
		$firstname = Strip($firstname);
		$firstname = str_replace("\\", "", $firstname);
		$firstname = str_replace("\'", "'", $firstname);
		$lastname = Strip($lastname);
		$lastname = str_replace("\\", "", $lastname);
		$lastname = str_replace("\'", "'", $lastname);
		$email = Strip($email);
		$email = str_replace("\\", "", $email);
		$email = str_replace("\'", "'", $email);
		$phone = Strip($phone);
		$phone = str_replace("\\", "", $phone);
		$phone = str_replace("\'", "'", $phone);
		
		if ($found) {
			$usercell[$i][3]=$firstname;
			$usercell[$i][4]=$lastname;
			$usercell[$i][5]=$email;
			$usercell[$i][6]=$phone;

			$usercolmax=0;
			for ($i=1; $i<=$userlin; $i++) {
				for ($j=1; $j<=$usercol; $j++) {
					if ($usercell[$i][$j]!="") {
						if ($j>$usercolmax) { $usercolmax=$j; }
					}
				}
			}
			$usercol=$usercolmax;
			$savefile="";
			for ($i=1; $i<=$userlin; $i++) {
				for ($j=1; $j<=$usercol; $j++) {
					$savefile.=$usercell[$i][$j].";";
				}
				$savefile.=chr(10);
			}

			$mess=1;
		} else {
			$ok = true;
			if ($login == "") {
				$mess=2;
				$ok = false;
			}
			if ($email == "") {
				$mess=3;
				$ok = false;
			}
		
			if ($ok) {
				$date = date ("Y-m-d H:i:s", mktime (date("H"),date("i"),date("s"),date("m"),date("d"),date("Y")));
				$soon  = date ("Y-m-d H:i:s", mktime (date("H"),date("i")+30,date("s"),date("m"),date("d"),date("Y")));
				
				$usercolmax=0;
				for ($i=1; $i<=$userlin; $i++) {
					for ($j=1; $j<=$usercol; $j++) {
						if ($usercell[$i][$j]!="") {
							if ($j>$usercolmax) { $usercolmax=$j; }
						}
					}
				}
				$usercol=$usercolmax;
				$savefile="";
				for ($i=1; $i<=$userlin; $i++) {
					for ($j=1; $j<=$usercol; $j++) {
						$savefile.=$usercell[$i][$j].";";
					}
					$savefile.=chr(10);
				}
				$userlin++;
				$savefile.=$login.";".$password.";".$firstname.";".$lastname.";".$email.";".$phone.";".$date.";";
				if ($userlin==1) { $savefile.="adminMT;"; }
				$savefile.=chr(10);

				mail($email, $loc[$loctag[3]+3][$lang], $loc[$loctag[3]+4][$lang]." : ".$password);
				$mess=4;
				$logged=true;
			}
		}
		$fn=Inc("userlist.csv");
		$fp = fopen ($fn, "w");
		fwrite($fp, $savefile);
		fclose($fp);
		
		echo "<br><font class='orangeBOLD'>";						
		Message($saymess1[$mess], $saymess2[$mess]);
		echo "</font><br>";
		$action="";
	}
?>
<?
	$saymess[1]=$loc[$loctag[3]+17][$lang];
	$saymess[2]=$loc[$loctag[3]+18][$lang];
	$saymess[3]=$loc[$loctag[3]+19][$lang];
	$saymess[4]=$loc[$loctag[3]+20][$lang];

	if (($action=='testlogin') || ($action=='')) {
		$logged=false;
		if ($login == '') {
			$mess = 1;
		} else {
			$i=0;
			$found=false;
			do {
				$i++;
				if ($usercell[$i][1]==$login) { $found=true; }
			} while ((!$found) && ($i<$lin));
			$returnpassword = $usercell[$i][2];
			$firstname = $usercell[$i][3];
			
			if (!$found) {
				$mess = 2;
			} else {
				if ($returnpassword <> $password) {
					$mess = 3;
				} else {
					$mess = 4;
					$logged=true;
				}
			}
		}
		if ($action!='') {
			echo "<br><font class='orangeBOLD'>";						
			Message ($saymess[$mess],'');
			echo "</font><br>";
		}
	}
?>
<font class="jaunePALE">
<?
	$saymess[1]=$loc[$loctag[3]+21][$lang];
	$saymess[2]=$loc[$loctag[3]+22][$lang]." ".$firstname." !<br>";

	if (!$logged) {
		echo $saymess[1];
	} else {
		echo $saymess[2];
	}
?></font>
<br>
<table width=400 cellspacing="10" cellpadding="10" border="1">
<?
	$textforgotpassword=$loc[$loctag[3]+23][$lang];
	$textdontpanic=$loc[$loctag[3]+24][$lang];
	$textlogout=$loc[$loctag[3]+25][$lang];
	$textchangepassword=$loc[$loctag[3]+26][$lang]." :";
	$textnewpassword=$loc[$loctag[3]+27][$lang].":";
	$textnewpasswordagain=$loc[$loctag[3]+28][$lang].":";
	$textnewuser="<b>".$loc[$loctag[3]+29][$lang]." :</b><br>".$loc[$loctag[3]+30][$lang];
	$textchangedetails="<b>".$loc[$loctag[3]+31][$lang]." :</b>";
	$textfirstname=$loc[$loctag[3]+32][$lang].":";
	$textlastname=$loc[$loctag[3]+33][$lang].":";
	$textemail=$loc[$loctag[3]+34][$lang].":";
	$textphone=$loc[$loctag[3]+35][$lang].":";
	$textgregistersubmit=$loc[$loctag[3]+36][$lang]."";
	$textchangesubmit=$loc[$loctag[3]+37][$lang];
	$textexistinguser=$loc[$loctag[3]+38][$lang]." :";
	$textlogin=$loc[$loctag[3]+39][$lang].":";
	$textpassword=$loc[$loctag[3]+40][$lang].":";
	$textloginsubmit=$loc[$loctag[3]+41][$lang];
	
	if (!$logged) {
?>
<tr>
    <td>
	<font class="jaunePALE"><b><? echo $textexistinguser; ?></font></b>
	<form name="form1" action="<? echo IncL("login_metoo.php"); ?>" method="post">
	<div align="center">
	<table cellspacing="2" cellpadding="2" border="0">
	<tr>
	    <td align=right class="jaunePALE"><? echo $textlogin; ?></td>
	    <td><input type="text" name="login"></td>
	</tr>
	<tr>
	    <td align=right class="jaunePALE"><? echo $textpassword; ?></td>
	    <td><input type="password" name="password"></td>
	</tr>
	</table>
	<br>
	<input type='hidden' name='action' value='testlogin'>
	<input value="<? echo $textloginsubmit; ?>" type="submit">
	</div>
	</form>
	</td>
</tr>
<?
	}
?>
<tr>
    <td><font class="jaunePALE"><b>
<?
	if (!$logged) {
		echo $textnewuser;
	} else {
		echo $textchangedetails;
		$i=0;
		$found=false;
		do {
			$i++;
			if ($usercell[$i][1]==$login) { $found=true; }
		} while ((!$found) && ($i<$lin));
		if ($found) {
			$firstname = $usercell[$i][3];
			$lastname = $usercell[$i][4];
			$email = $usercell[$i][5];
			$phone = $usercell[$i][6];
		}
	}
?>
</font></b>
	<form name="form2" action="<? echo IncL("login_metoo.php"); ?>" method="post">
	<div align="center">
	<table cellspacing="2" cellpadding="2" border="0">
<?
	if (!$logged) {
?>
	<tr>
	    <td align=right class="jaunePALE"><? echo $textlogin; ?></td>
	    <td><input type="text" name="login"></td>
	</tr>
<?
	}
?>
	<tr>
	    <td align=right class="jaunePALE"><? echo $textfirstname; ?></td>
	    <td><input type="text" name="firstname"<? if ($logged) { echo " value='".$firstname."'"; }?>></td>
	</tr>
	<tr>
	    <td align=right class="jaunePALE"><? echo $textlastname; ?></td>
	    <td><input type="text" name="lastname"<? if ($logged) { echo " value='".$lastname."'"; }?>></td>
	</tr>
	<tr>
	    <td align=right class="jaunePALE"><? echo $textemail; ?></td>
	    <td><input type="text" name="email"<? if ($logged) { echo " value='".$email."'"; }?>></td>
	</tr>
	<tr>
	    <td align=right class="jaunePALE"><? echo $textphone; ?></td>
	    <td><input type="text" name="phone"<? if ($logged) { echo " value='".$phone."'"; }?>></td>
	</tr>
	</table>
	<br>
<?
	if (!$logged) {
		echo "<input type='hidden' name='action' value='register'>";
		echo "<input value='$textgregistersubmit' type='submit'>";
	} else {
		echo "<input type='hidden' name='action' value='change'>";
		echo "<input value='$textchangesubmit' type='submit'>";
	}
?>
	</div>
	</form>
	</td>
</tr>
<?
	if ($logged) {
?>
<tr>
    <td>
	<font class="jaunePALE"><b><? echo $textchangepassword; ?></font></b>
	<form name="form3" action="<? echo IncL("login_metoo.php"); ?>" method="post">
	<div align="center">
	<table cellspacing="2" cellpadding="2" border="0">
	<tr>
	    <td align=right class="jaunePALE"><? echo $textpassword; ?></td>
	    <td><input type="password" name="oldpassword"></td>
	</tr>
	<tr>
	    <td align=right class="jaunePALE"><? echo $textnewpassword; ?></td>
	    <td><input type="password" name="newpassword1"></td>
	</tr>
	<tr>
	    <td align=right class="jaunePALE"><? echo $textnewpasswordagain; ?></td>
	    <td><input type="password" name="newpassword2"></td>
	</tr>
	</table>
	<br>
	<input type='hidden' name='action' value='changepass'>
	<input value="<? echo $textchangesubmit; ?>" type="submit">
	</div>
	</form>
	</td>
</tr>
<?
	}
?>
<?
	if (!$logged) {
?>
<tr>
    <td>
	<font class="jaunePALE"><b><? echo $textforgotpassword; ?></font></b>
	<form name="form4" action="<? echo IncL("sendpass.php"); ?>" method="post">
	<div align="center">
	<table cellspacing="2" cellpadding="2" border="0">
	<tr>
	    <td align=right class="jaunePALE"><? echo $textlogin; ?></td>
	    <td><input type="text" name="login"></td>
	</tr>
	</table>
	<br>
	<input value="<? echo $textdontpanic; ?>" type="submit">
	</div>
	</form>
	</td>
</tr>
<?
	}
?>
<?
	if ($logged) {
?>
<tr>
    <td>
	<font class="jaunePALE"><b><? echo $textlogout; ?> :</font></b>
	<form name="form5" action="<? echo IncL("login_metoo.php"); ?>" method="post">
	<input type='hidden' name='action' value='logout'>
	<div align="center">
	<input value="<? echo $textlogout; ?>" type="submit">
	</div>
	</form>
	</td>
</tr>
<?
	}
?>
</table>
</div>	

<? include Inc("metoobottom.inc.php"); ?>