<?php
/*
* @ServerMon v1.1
* @Developed by Maksa988
* @www.Server-Mon.ru
* @Copyright (C) 2014
*/
 if(!defined('SMON'))
	die('Hacking attempt!');
	
class main extends Controller {
	public function __construct() {
		global $db;
		
		$this->header();
		$this->model('users');
		
		if($this->POST('login')):
			$login = $this->POST('sm_login');
			$password = $this->POST('sm_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('2', '1', $_SERVER['REMOTE_ADDR'], $login, md5($password));
						$this->data['success'] = 'Вы успешно авторизировались! <a href="/admin">Обновить</a>';
						return $this->view(array('login/index'), $this->data); //Загружаем шаблон
					else:
						$this->usersModel->addAuth('2', '2', $_SERVER['REMOTE_ADDR'], $login, $password);
						$this->data['error'] = 'Логин или пароль введен неверно!';
					endif;
				else:
					$this->usersModel->addAuth('2', '2', $_SERVER['REMOTE_ADDR'], $login, $password);
					$this->data['error'] = 'Логин или пароль введен неверно!';
				endif;
			else:
				$this->data['error'] = 'Все поля обязательны к заполнению!';
			endif;
		endif;
		
		if(empty($_SESSION['user_id'])):
			return $this->view(array('login/index'), $this->data); //Загружаем шаблон
		endif;
		
		$user = $this->usersModel->getUser($_SESSION['user_id']);
		$this->data['user'] = $user;
		
		if($user['user_group'] != $this->setting('admin_group')):
			$this->data['error'] = 'Вы не имеете доступа!';
			return $this->view(array('login/index'), $this->data); //Загружаем шаблон
		endif;
		
		$this->data['activeSection'] = 'main';
		
		$this->data['version'] = $this->cfg('version');
		$this->data['osinfo'] = php_uname();
		$this->data['phpversion'] = PHP_VERSION;
		$this->data['mysqlversion'] = $db->get_server_info();
		$this->data['zendversion'] = zend_version();
		$this->data['memorylimit'] = ini_get('memory_limit');
		$this->data['postmaxsize'] = ini_get('post_max_size');
		$this->data['uploadmaxsize'] = ini_get('upload_max_size');
		
		$result = $db->query("SHOW TABLE STATUS");
		$this->data['dbsize'] = '0';
		
		while($row = $result->fetch_array()) { 
			$this->data['dbsize'] += $row["Data_length"] + $row["Index_length"];
		}

		if($this->data['dbsize'] < 1024) {
			$this->data['dbsize'] = $this->data['dbsize']." bytes";
		}
		else if($this->data['dbsize'] < 1024000) {
			$this->data['dbsize'] = round(($this->data['dbsize']/1024),1)." Kb";
		}
		else {
			$this->data['dbsize'] = round(($this->data['dbsize']/1024000),1)." MB";
		}
		$votes = $db->query("SELECT COUNT(*) FROM `".$this->cfg('prefix')."vote`");
		$this->data['votes'] = $votes->fetch_array();
		
		return $this->view(array('common/header', 'main/index', 'common/footer'), $this->data); //Загружаем шаблон
	}
}