<?php
	/*****************************************************************************
	IP Reg, a PHP/MySQL IPAM tool
	Copyright (C) 2008 Wietse Warendorff
	
	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.
	
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.
	
	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
	
	For more information, visit http://sourceforge.net/projects/ipreg,
	or contact me at wietsew@users.sourceforge.net
	*****************************************************************************/
	
	// start session with default language
	session_start();
	include('lang/en.php');
	
	// includes
	include("config.php");
	include("dbconnect.php");
	include("functions.php");
	include("yapter.php");	
	
	// check for submit
	if ($_SERVER['REQUEST_METHOD']=="POST" ) {
		if(isset($_POST['user_name']) && isset($_POST['user_pass']) && trim($_POST['user_name']) <> "" && trim($_POST['user_pass']) <> "") {
			$user_name = $_POST['user_name'];
			$result = mysql_query("SELECT user_id, user_pass, user_level, user_displayname, user_mac, user_lang FROM user WHERE user_name='$user_name'") or die(mysql_error());
			
			if(mysql_num_rows($result) > 0) {
				if(!strcmp(md5($_POST['user_pass']), mysql_result($result, 0, "user_pass"))) {
				// all ok, user logged in
					$_SESSION['suser_id'] = mysql_result($result, 0, "user_id");
					$_SESSION['suser_level'] = mysql_result($result, 0, "user_level");
					$_SESSION['suser_displayname'] = mysql_result($result, 0, "user_displayname");
					$_SESSION['suser_mac'] = mysql_result($result, 0, "user_mac");
					$_SESSION['suser_lang'] = mysql_result($result, 0, "user_lang");
					
					header_location("index.php");
				} else {
				// not ok, break session
					$_SESSION = array();
					session_destroy();
				}
				// clear mysql-result
				mysql_free_result($result);
			}
		}
	}
		
	// start output
	// set template
	$tp = new Template("tpl/login.tpl");
	
	// set language variables
	$tp->setvars($lang);
			
	// output
	$tp->parse();
	$tp->spit();
	
	include("footer.php");
?>