<?php

	//echo md5('huglo729lepage!');

	define('IN_HUGLO', true);
	require_once('./../includes/constants.php');
	require_once(ROOT_PATH . 'includes/db.php');
	require_once(ROOT_PATH . 'cp/includes/sessions.php');
	
	$db	= new db();
	$db->sql_connect(SQL_HOST, SQL_USER, SQL_PASSWD, SQL_BASE);
	
	$error = array();
	
	if(isset($_POST['submit'])) {
		$login = trim($_POST['login']);
		$password = trim($_POST['password']);
		
		if(empty($login)) {
			$error[] = 'Le champ "Login" est vide.';
		}
		if(empty($password)) {
			$error[] = 'Le champ "Password" est vide.';
		}
		
		if(count($error) <= 0) {
			$sql = 'SELECT * FROM ' . USERS_TABLE . ' WHERE users_login = ' . $db->_sql_valide_value($login) . ' AND users_password = ' . $db->_sql_valide_value(md5($password));
			$result = mysql_query($sql);
			
			if(mysql_num_rows($result) != 1) {
				$error[] = 'Le couple Login et Password n\'est pas correct.';
			}
			
			if(count($error) <= 0) {
				$row = mysql_fetch_assoc($result);
				$_SESSION['users_id'] = $row['users_id'];
				$_SESSION['users_login'] = $row['users_login'];

				$sql = 'UPDATE ' . USERS_TABLE . ' SET ' . $db->sql_insert_update('UPDATE', array(
					'users_last'	=>	(int) time(),
					'users_ip'		=>	(string) $_SERVER['REMOTE_ADDR']
				)) . ' WHERE users_id = ' . $row['users_id'];
				mysql_query($sql);
			
				mysql_free_result($result);
				header('Location: histoire.php');
				$db->sql_close();
				exit;
			}
		}
	}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Huglo Lepage Control Panel</title>
<link href="global.css" rel="stylesheet" type="text/css" media="all" />
</head>

<body>
<?php

	if(count($error) > 0) {

?>
<br />
<table class="green">
	<tr>
		<td><?php echo implode($error, '<br />'); ?></td>
	</tr>
</table>
<?php

	}

?>
<h1>Huglo Lepage Control Panel</h1>
<form action="index.php" method="post" name="form">
	<table>
		<tr>
			<td class="bggrey">Login</td>
			<td><input type="text" name="login" id="login" /></td>
		</tr>
		<tr>
			<td class="bggrey">Password</td>
			<td><input type="password" name="password" id="password" /></td>
		</tr>
		<tr>
			<td><input type="submit" name="submit" id="submit" value="Envoyer" class="bouton" /></td>
			<td>&nbsp;</td>
		</tr>
	</table>
</form>
</body>
</html>
<?php

	unset($error);
	$db->sql_close();
	
?>
