<?

function dispConnection(){


	/////// BEGIN TABLE UP ///////
	if ($_SESSION["user_right"] == 0) {
		$str .=	dispConnectionLogin();
	}
	else {
		$str .=	dispConnectionDisconnect();
	}



	/////// BEGIN SPACE ///////
	$str .= '<table><tr><td></td></tr></table>';


	/////// BEGIN TABLE DOWN ///////
	if ($_SESSION["user_right"] == "ANONYM") {
//		$str .=	dispConnectionRegister();	// HIDE WAITING FORUM
	}
	else if ($_SESSION["user_right"] == "EPI") {
//		$str .=	dispConnectionEpiRegister();	// HIDE WAITING REGISTER PAGE
	}
	else {
//		$str .=	dispConnectionProfile();	// HIDE WAITING EDIT PROFIL
	}


	echo $str;
}



function dispConnectionLogin() { /////// CONNECTION LOGIN ///////

	$str = dispTableBegin("c o n n e x i o n", "30", 1);

	if ($_SERVER['HTTPS'] == "on")
    	$str .= '

    		<form name="saisie" method="post" action="index.php?connect=1&section=Home">
          	 	<tr><td width="40%" class="small" align="center">Login</td></tr>
          		<tr><td width="60%" align="center"><input type="login" name="user_login" size="15" maxlength="15"></td></tr>
    		<tr><td width="40%" class="small" align="center">Pass</td></tr>
    	     	<tr><td width="60%" align="center"><input type="password" name="user_pass" size="15" maxlength="15"></td></tr>
    		<tr><td colspan="2" align="center"><input type="submit" value="Uaimec"><br></td></tr>
    		</form>
    	';
	else
		$str .= "Pour vous connecter au site, <b>vous devez utiliser une connexion SSL</b>!";

	$str .= dispTableEnd();

	return ($str);
}



function dispConnectionRegister() { /////// CONNECTION REGISTER ///////


	$str = dispTableBegin("s ' e n r e g i s t r e r", "30", 1);

	$str .= '

	<tr>
		<td>
			Pour poster des news dans le forum vous devez vous enregistrer ici:
		</td>
	</tr>



	<tr>
		<td colspan="2" align="center">
			<form name="saisie" method="post" action="index.php">
				<input type="submit" value="s\'enregistrer">
				<br>
			</form>
		</td>
	</tr>

';

$str .= dispTableEnd();

return ($str);
}



function dispConnectionEpiRegister() { /////// CONNECTION EPI REGISTER ///////


	$str = dispTableBegin("s ' e n r e g i s t r e r", "30", 1);

	$str .= '

		<tr>
			<td>
				Pour personnaliser votre profil ou emprunter du matos, vous devez vous enregistrer ici:
			</td>
		</tr>

		<tr>
			<td colspan="2" align="center">
				<form name="saisie" method="post" action="index.php">
					<input type="submit" value="s\'enregistrer">
					<br>
				</form>
			</td>
		</tr>
	';

	$str .= dispTableEnd();

	return $str;
}



function dispConnectionProfile() { /////// CONNECTION PROFIL ///////

	$str = dispTableBegin("p r &eacute; f &eacute; r e n c e", "30", 1);

	$str .= '

		<tr>
			<td>
				Pour modifier votre profil:
			</td>
		</tr>

		<tr>
			<td colspan="2" align="center">
				<form name="saisie" method="post" action="index.php">
					<input type="submit" value="Pr&eacute;f&eacute;rence">
					<br>
				</form>
			</td>
		</tr>


	';

	$str .= dispTableEnd();

return $str;
}



function dispConnectionDisconnect() {  /////// CONNECTION PROFIL ///////

	$ID = $_SESSION["user_id"];

	$result = getOneUserById($ID);


	$user = mysql_fetch_array($result);

	$str = dispTableBegin("d &eacute; c o n n e c t e r", "30", 1);

	$str .='
		<form name="saisie" method="post" action="index.php?connect=-1&section=Home">
       		<tr><td width="40%" class="small" align="center"> <b>Salut &#224 toi
		'.$user["user_pseudo"].'
		</b></td></tr>

		<!--
		<tr><td width="40%" class="small" align="center">Status: <b>
		'.get_right_name($user["user_right"]).'
		</b></td></tr>
		<tr><td width="40%" class="small" align="center">Rights: <b>
		'.get_right_name($_SESSION["user_right"]).'
		</b></td></tr>
		-->
		<tr><td width="40%" class="small" align="center">
		'.$user["user_comment"].'
		</td></tr>
		<tr><td></td></tr>
		<tr><td colspan="2" align="center"><input type="submit" value="D&eacute;connecter"><br></td></tr>
		</form>
	';

	$str .= dispTableEnd();

return ($str);
}


// Disconnect user
function userDisconnect(){
  $_SESSION["user_right"] = 0;
  $_SESSION["user_id"] = 0;
}



function checkUser($Check_Login, $Check_Pass){

	$result_login = getOneUserByName($Check_Login);
	$result_pseudo = getOneUserByPseudo($Check_Login);

	// MD5
	$md5_Pass = stripslashes($Check_Pass);
	$md5_Pass = md5($md5_Pass);

	if (mysql_num_rows($result_login)){
		$user = mysql_fetch_array($result_login);

		if ($user["user_login"] == $Check_Login && $user["user_pass"] == $md5_Pass ) {

			$_SESSION["user_right"] = $user["user_right"];
			$_SESSION["user_id"] = $user["user_id"];
			return;
		}
	}
	else if (mysql_num_rows($result_pseudo)){
		$user = mysql_fetch_array($result_pseudo);
		if ($user["user_pseudo"] == $Check_Login && $user["user_pass"] == $md5_Pass ) {
			$_SESSION["user_right"] = $user["user_right"];
			$_SESSION["user_id"] = $user["user_id"];
			return;
		}
	}

	if (yp_check($Check_Login, $Check_Pass)) {
		if ($user["user_login"] == $Check_Login){
			$_SESSION["user_right"] = $user["user_right"];
			$_SESSION["user_id"] = $user["user_id"];
		}
		else {
			if (!check_blacklist($Check_Login)) {
				$_SESSION["user_right"] = 3;
				$_SESSION["user_id"] = 0;
			}
			else {
				$_SESSION["user_right"] = -1;
			}
		}

		return;
	}
}


function yp_check($login, $passwd, $domain = "", $map = "")
{
  /*
   * first of all, if the domain is not specified, we need
   * to retrieve the default yp domain
   */

  //if ($domain == "")
  // {
  //    $domain = yp_get_default_domain();
  //  }

  /*
   * if the map is not specified, we set it to
   * passwd.byname
   */

  if ($map == "")
    {
      $map = "socks";
    }

  /*
   * then we try to find the yp entry corresponding to the
   * login
   */

  $entry = @yp_match($domain, $map, $login);

  /* if the login doesn't exist, yp_match return FALSE */

  if ($entry == FALSE)
    {
      return (FALSE);
    }

  /*
   * next we split the obtained line, we only need the
   * crypted password
   */

  $good_passwd = $entry;


  /*
   * $good_passwd is a crypted password, and with is salt
   * (the 2 first chars), we can crypt the submitted
   * password
   */

  $salt = substr($good_passwd, 0, 2);
  $submitted_passwd = crypt($passwd, $salt);

  /*
   * finally, we have to check if the two crypted
   * passwords match and return the result
   */

  return (strcmp($good_passwd, $submitted_passwd) == 0);
}

function check_blacklist($Check_Login) {  ////// CHECK IF BLACKLISTED //////

	$result = getOneBlacklistByLogin($Check_Login);

	if (mysql_num_rows($result)){
		$blacklist = mysql_fetch_array($result);

		$try =  $blacklist["blacklist_try"] + 1;

		$query = " UPDATE `blacklist_db` ";
		$query .= " SET `blacklist_try` = '$try' ";
		$query .=  " WHERE `blacklist_login` = '$Check_Login' LIMIT 1";
		mysql_query($query)
		    or die("Query failed");

		$result = getAllBlacksite();
		// random un peu porc pour les fake sites.
			$cpt = 0;
			while ($blacksite = mysql_fetch_array($result)) {
				$cpt++;
				$tabsite[$cpt] = $blacksite["blacksite_id"];
			}

			$randsite = rand (1, $cpt);

		$result = getOneBlacksiteById($tabsite[$randsite]);
		$blacksite = mysql_fetch_array($result);

		echo '<META HTTP-EQUIV="REFRESH" CONTENT="0; URL='.$blacksite["blacksite_adress"].'"> '; // automatique redirection
		return (1);
	}

	return (0);
}

function go_random_blacksite() {

		$result = getAllBlacksite();
		// random un peu porc pour les fake sites.
			$cpt = 0;
			while ($blacksite = mysql_fetch_array($result)) {
				$cpt++;
				$tabsite[$cpt] = $blacksite["blacksite_id"];
			}

			$randsite = rand (1, $cpt);

		$result = getOneBlacksiteById($tabsite[$randsite]);
		$blacksite = mysql_fetch_array($result);

		echo '<META HTTP-EQUIV="REFRESH" CONTENT="0; URL='.$blacksite["blacksite_adress"].'"> '; // automatique redirection

}

?>
