<?php
	/*
	MGB 0.7.x - OpenSource PHP and MySql Guestbook
	Copyright (C) 2004 - 2013 Juergen Grueneisl - http://www.m-gb.org/

	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 2 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, write to the Free Software
	Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

	=============
	login.inc.php
	=============
	*/

	// make sure nobody has direct acces to this script
	if(!defined('ADMINISTRATION')) {
		include ("error.html");
		die();
	}

	require ("../includes/functions.inc.php");

	if(isset($_GET['action']) AND ($_GET['action'] == "logout")) {
		// logout
		if(isset($_SESSION['ID'])) {
			mgb_sql_connect("UPDATE ".$db['prefix']."user SET `logged_out` = '1' WHERE ID=".secure_value($_SESSION['ID'])." LIMIT 1", "Error while logging out.", 0);
			session_unset();
			session_destroy();
			$_SESSION = array();
		}

		$login_is_ok = 0;
		$login_status_text = $lang['logged_out'];
		$login_status_img = "<img src=\"templates/default/images/login.png\" height=\"16\" width=\"16\" title=\"{LANG_LOGIN}\" alt=\"{LANG_LOGIN}\">";
		$_POST['sent'] = 0;
	} else {
		if(isset($_POST['sent']) AND $_POST['sent'] == 1) {
			if(!empty($_POST['username']) AND !empty($_POST['password'])) {
				if(login_ok(secure_value($_POST['username']), "", $_POST['password'])) {
					if(!isset($_SESSION['key'])) {
						generate_key_and_pw(secure_value($_POST['username']), 16, "adminpanel");

						$result = mgb_sql_connect("SELECT ID, user_name, user_key, user_ip, user_is_active, logged_out FROM ".$db['prefix']."user WHERE user_name=".secure_value($_POST['username']), "Error while logging in.", 1);
						$user = @mysql_fetch_array($result);

						$_SESSION['ID'] = $user['ID'];
						$_SESSION['username'] = $user['user_name'];
						$_SESSION['key'] = $user['user_key'];
						$_SESSION['user_is_active'] = $user['user_is_active'];
						$_SESSION['ip'] = $user['user_ip'];

						if($_SESSION['user_is_active'] == 1) {
							$login_status_text = $lang['login_ok'];
							$login_status_img = NULL;
							$login_is_ok = 1;

							if(SID != NULL) { $sid = "?".SID; } else {$sid = NULL; }

							if($user['logged_out'] == 0) {
								$errorcode = 10;
								$refresh = "<meta http-equiv=\"refresh\" content=\"5; URL=admin.php".$sid."\">";
							} else {
								$refresh = "<meta http-equiv=\"refresh\" content=\"2; URL=admin.php".$sid."\">";
							}

							mgb_sql_connect("UPDATE ".$db['prefix']."user SET `logged_in` = '".time()."', `logged_out` = '0' WHERE ID=".secure_value($user['ID'])." LIMIT 1", "Error while logging in.", 0);
						} else {
							session_unset();
							session_destroy();
							$_SESSION = array();

							$login_status_text = $lang['logged_out'];
							$login_status_img = "";
							$login_is_ok = 0;
							$errorcode = 3;

							$refresh = "";
						}
					} else {
						$login_status_text = $lang['logged_out'];
						$login_status_img = "<img src=\"templates/default/images/login.png\" height=\"16\" width=\"16\" title=\"{LANG_LOGIN}\" alt=\"{LANG_LOGIN}\">";
						$login_is_ok = 0;
					}
				} else {
					$login_status_text = $lang['logged_out'];
					$login_status_img = "<img src=\"templates/default/images/login.png\" height=\"16\" width=\"16\" title=\"{LANG_LOGIN}\" alt=\"{LANG_LOGIN}\">";
					$login_is_ok = 0;
					$errorcode = 2;
				}
			} else {
				$login_status_text = $lang['logged_out'];
				$login_status_img = "<img src=\"templates/default/images/login.png\" height=\"16\" width=\"16\" title=\"{LANG_LOGIN}\" alt=\"{LANG_LOGIN}\">";
				$login_is_ok = 0;
				$errorcode = 1;
			}
		} else {
			$login_status_text = $lang['logged_out'];
			$login_status_img = "<img src=\"templates/default/images/login.png\" height=\"16\" width=\"16\" title=\"{LANG_LOGIN}\" alt=\"{LANG_LOGIN}\">";
			$login_is_ok = 0;
		}
	}

	if(!empty($errorcode)) {
		$errormessage = mgb_errormessage($errorcode, $settings['language_path'], "adminpanel");
	} else {
		$content_errormessage = "";
	}

	if(isset($login_is_ok) AND $login_is_ok == 1) {
		$page_include = $content_login_ok;
		$page_include = template("TEMPLATE_ERRORMESSAGE", $content_errormessage, $page_include);
		if(empty($errormessage)) { $errormessage = ""; }
		$page_include = template("ERRORMESSAGE", $errormessage, $page_include);
		$page_include = template("LANG_PLEASE_WAIT", $lang['please_wait'], $page_include);
	} else {
		$page_include = $content_login;
		if(empty($errormessage)) { $errormessage = ""; }
		$page_include = template("TEMPLATE_ERRORMESSAGE", $content_errormessage, $page_include);
		$page_include = template("ERRORMESSAGE", $errormessage, $page_include);
		$page_include = template("LANG_LOGIN_USERNAME", $lang['login_username'], $page_include);
		$page_include = template("LANG_LOGIN_PASSWORD", $lang['login_password'], $page_include);
		$page_include = template("LANG_LOGIN_LOSTPASSWORD", $lang['login_lostpassword'], $page_include);
	}

	$content_scrolling_function = "";
?>