Просмотр файла kernel/Authorisation.php

Размер файла: 2.03Kb
<?php

	/*
		Класс: Authorisation
	*/

	class Authorisation
	{
		var $_authorised = false;

		function Authorisation()
		//
		// Проводит авторизацию пользователя по логину/паролю или по данным сессии
		//
		{
			if (isset($_POST['login']) && isset($_POST['password']) && !empty($_POST['login']) && !empty($_POST['password']))
			{
				$login = substr($_POST['login'], 0, 32);
				$password = md5(substr($_POST['password'], 0, 32));
				if (file_exists('data/'.ADMIN_FILE))
				{
					$account = @file('data/'.ADMIN_FILE);
					list($valid_login, $valid_password) = explode('::', str_replace("\n", '', $account[0]));
					if ($login==$valid_login && $password==$valid_password)
					{
						$this->_authorised = true;
						$_SESSION['authorised'] = true;
					} else {
						unset($_SESSION['authorised']);
					}
				} else {
					$stream = fopen('data/'.ADMIN_FILE, 'w');
					flock($stream, LOCK_EX);
					fputs($stream,'admin::'.md5('admin'));
					flock($stream, LOCK_UN);
					fclose($stream);
				}
			} elseif (isset($_SESSION['authorised']) && $_SESSION['authorised'])
			{
				$this->_authorised = true;
			}
		}

		function IsAuth()
		//
		// Авторизован ли пользователь
		//
		{
			return $this->_authorised;
		}

		function SetPassword()
		{
			$_POST['password'] = substr($_POST['password'], 0, 32);
			$_POST['password2'] = substr($_POST['password2'], 0, 32);
			if ($_POST['password'] == $_POST['password2'])
			{
				if (!preg_match('/[^A-Za-z_0-9]/', $_POST['password']))
				{
					$file = @file('../data/'.ADMIN_FILE);
					list($login, $password) = explode('::', str_replace("\n", '', $file[0]));
					$stream = @fopen('../data/'.ADMIN_FILE, 'w');
					flock($stream, LOCK_EX);
					fputs($stream, $login.'::'.md5($_POST['password']));
					flock($stream, LOCK_UN);
					fclose($stream);
					return 0;
				} else return 22;
			} else return 21;
		}

		function ExitUser()
		//
		// Выход
		//
		{
			$this->_authorised = false;
			$_SESSION['authorised'] = false;
		}
	}
?>