<?php
/*
* @ServerMon v1.1
* @Developed by Maksa988
* @www.Server-Mon.ru
* @Copyright (C) 2014
*/
 if(!defined('SMON'))
	die('Hacking attempt!');
	
class user extends Controller {
	public function __construct() {
		$this->header();
		$this->data['activeSection'] = 'login';
		$this->data['navbarName'] = 'Авторизация';

		if(!empty($_SESSION['user_id'])):
			return $this->view(array('common/header', 'user/access', 'common/footer'), $this->data); //Загружаем шаблон
		endif;
		
		$this->model('users');
		
		if($this->POST('auth')):
			$login = $this->POST('login');
			$password = $this->POST('password');
			
			if($login && $password):
				if($this->usersModel->checkUser($login)):
					$user = $this->usersModel->checkUser($login);
					if($user['user_password'] == md5($password)):
						$_SESSION['user_id'] = $user['user_id'];
						$this->usersModel->addAuth('1', '1', $_SERVER['REMOTE_ADDR'], $login, md5($password));
						$this->data['success'] = 'Вы успешно авторизировались!';
					else:
						$this->usersModel->addAuth('1', '2', $_SERVER['REMOTE_ADDR'], $login, $password);
						$this->data['error'] = 'Логин или пароль введен неверно!';
					endif;
				else:
					$this->usersModel->addAuth('1', '2', $_SERVER['REMOTE_ADDR'], $login, $password);
					$this->data['error'] = 'Логин или пароль введен неверно!';
				endif;
			else:
				$this->data['error'] = 'Все поля обязательны к заполнению!';
			endif;
		endif;
		
		return $this->view(array('common/header', 'user/login', 'common/footer'), $this->data); //Загружаем шаблон
	}
}