summaryrefslogtreecommitdiffstats
path: root/inc/user.inc.php
blob: 38e57e33149ad8f9fc2d780f6d2f581d80016b7e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php

require_once('inc/session.inc.php');

class User
{
	private static $user = false;

	public static function isLoggedIn()
	{
		return self::$user !== false;
	}

	public static function getName()
	{
		if (self::$user === false) return false;
		return self::$user['fullname'];
	}

	public static function hasPermission($permission)
	{
		if (self::$user === false) return false;
		return (self::$user['permissions'] & Permission::get($permission)) != 0;
	}

	public static function load()
	{
		if (Session::load()) {
			$uid = Session::get('uid');
			if ($uid === false || $uid < 1) self::logout();
			self::$user = Database::queryFirst('SELECT * FROM user WHERE userid = :uid LIMIT 1', array(':uid' => $uid));
			if (self::$user === false) self::logout();
			return true;
		}
		return false;
	}

	public static function login($user, $pass)
	{
		$ret = Database::queryFirst('SELECT userid, passwd FROM user WHERE login = :user LIMIT 1', array(':user' => $user));
		if ($ret === false) return false;
		if (crypt($pass, $ret['passwd']) !== $ret['passwd']) return false;
		Session::create();
		Session::set('uid', $ret['userid']);
		Session::set('token', md5(rand() . time() . rand() . $_SERVER['REMOTE_ADDR'] . rand() . $_SERVER['REMOTE_PORT'] . rand() . $_SERVER['HTTP_USER_AGENT']));
		Session::save();
		return true;
	}

	public static function logout()
	{
		Session::delete();
		Header('Location: ?do=main&fromlogout');
		exit(0);
	}

}