1 | <?php
|
---|
2 |
|
---|
3 | // User licence levels
|
---|
4 | define('LICENCE_ANONYMOUS', -1);
|
---|
5 | define('LICENCE_USER', 0);
|
---|
6 | define('LICENCE_MODERATOR', 1);
|
---|
7 | define('LICENCE_ADMIN', 2);
|
---|
8 |
|
---|
9 | class User
|
---|
10 | {
|
---|
11 | var $Id;
|
---|
12 | var $Name;
|
---|
13 | var $Team;
|
---|
14 | var $Role;
|
---|
15 | var $Redirecting;
|
---|
16 | var $Language;
|
---|
17 | var $System;
|
---|
18 |
|
---|
19 | function __construct($System)
|
---|
20 | {
|
---|
21 | $this->System = &$System;
|
---|
22 | if(isset($_SESSION)) $this->Restore();
|
---|
23 | else $this->SetAnonymous();
|
---|
24 | }
|
---|
25 |
|
---|
26 | function __destroy()
|
---|
27 | {
|
---|
28 | if(isset($_SESSION)) $this->Store();
|
---|
29 | }
|
---|
30 |
|
---|
31 | function Login($Name, $Password)
|
---|
32 | {
|
---|
33 | $DbResult = $this->System->Database->query('SELECT `ID` FROM `User` WHERE '.
|
---|
34 | 'LOWER(`Name`) = LOWER("'.$Name.'") AND `Pass` = '.$this->CryptPasswordSQL('"'.$Password.'"', '`Salt`'));
|
---|
35 | if($DbResult->num_rows > 0)
|
---|
36 | {
|
---|
37 | $User = $DbResult->fetch_assoc();
|
---|
38 | $this->Id = $User['ID'];
|
---|
39 | $this->Load();
|
---|
40 | WriteLog('Login: '.$Name, LOG_TYPE_USER);
|
---|
41 | $this->UpdateState();
|
---|
42 | } else $ŧhis->Role = LICENCE_ANONYMOUS;
|
---|
43 | }
|
---|
44 |
|
---|
45 | function Logout()
|
---|
46 | {
|
---|
47 | if($this->Role != LICENCE_ANONYMOUS)
|
---|
48 | $this->System->Database->query('UPDATE `User` SET `LastLogout` = NOW() WHERE `ID` = '.$this->Id);
|
---|
49 | $this->SetAnonymous();
|
---|
50 | }
|
---|
51 |
|
---|
52 | function Load()
|
---|
53 | {
|
---|
54 | $DbResult = $this->System->Database->query('SELECT * FROM `User` WHERE `ID` = '.$this->Id);
|
---|
55 | $User = $DbResult->fetch_assoc();
|
---|
56 | // Security: Password and Salt hash should not be loaded to variables
|
---|
57 | $this->Id = $User['ID'];
|
---|
58 | $this->Team = $User['Team'];
|
---|
59 | $this->Redirecting = $User['Redirecting'];
|
---|
60 | $this->Language = $User['Language'];
|
---|
61 | $this->Name = $User['Name'];
|
---|
62 | $this->Role = $User['GM'];
|
---|
63 | $this->Email = $User['Email'];
|
---|
64 | }
|
---|
65 |
|
---|
66 | function Restore()
|
---|
67 | {
|
---|
68 | if(array_key_exists('UserId', $_SESSION))
|
---|
69 | {
|
---|
70 | $this->Id = $_SESSION['UserId'];
|
---|
71 | if($this->Id != 0)
|
---|
72 | {
|
---|
73 | $this->Load();
|
---|
74 | $this->UpdateState();
|
---|
75 | } else $this->SetAnonymous();
|
---|
76 | } else $this->SetAnonymous();
|
---|
77 | }
|
---|
78 |
|
---|
79 | function Store()
|
---|
80 | {
|
---|
81 | $_SESSION['UserId'] = $this->Id;
|
---|
82 | }
|
---|
83 |
|
---|
84 | function SetAnonymous()
|
---|
85 | {
|
---|
86 | $this->Id = 0;
|
---|
87 | $this->Name = 'anonymous';
|
---|
88 | $this->Role = LICENCE_ANONYMOUS;
|
---|
89 | $this->Language = 1;
|
---|
90 | $this->Redirecting = 1;
|
---|
91 | $this->Team = 0;
|
---|
92 | $this->Email = '';
|
---|
93 | }
|
---|
94 |
|
---|
95 | function Licence($Licence)
|
---|
96 | {
|
---|
97 | if(!isset($_SERVER['REMOTE_ADDR'])) return(true); // Execution from command line
|
---|
98 | else return($this->Role >= $Licence);
|
---|
99 | }
|
---|
100 |
|
---|
101 | function GetPasswordSalt()
|
---|
102 | {
|
---|
103 | return(substr(sha1(mt_rand()), 0, 8));
|
---|
104 | }
|
---|
105 |
|
---|
106 | function CryptPasswordSQL($Password, $Salt)
|
---|
107 | {
|
---|
108 | return('SHA1(CONCAT(SHA1('.$Password.'), '.$Salt.'))');
|
---|
109 | }
|
---|
110 |
|
---|
111 | function UpdateState()
|
---|
112 | {
|
---|
113 | if(array_key_exists('REMOTE_ADDR', $_SERVER))
|
---|
114 | $this->System->Database->query('UPDATE `User` SET `LastIP` = "'.$_SERVER['REMOTE_ADDR'].'", `LastLogin` = NOW() WHERE `ID` = '.$this->Id);
|
---|
115 | }
|
---|
116 | }
|
---|
117 |
|
---|
118 | ?>
|
---|