1 | <?php
|
---|
2 |
|
---|
3 | define('NICK_USED', 'Přezdívka použita!');
|
---|
4 | define('USER_REGISTRATED', 'Uživatel zaregistrován.');
|
---|
5 | define('DATA_MISSING', 'Chybí emailová adresa, přezdívka, nebo některé z hesel!');
|
---|
6 | define('PASSWORDS_UNMATCHED', 'Hesla si neodpovídají!');
|
---|
7 | define('USER_NOT_LOGGED', 'Nejste přihlášen.');
|
---|
8 | define('USER_LOGGED', 'Uživatel přihlášen.');
|
---|
9 | define('USER_NOT_REGISTRED', 'Uživatel neregistrován.');
|
---|
10 | define('USER_ALREADY_LOGGED', 'Uživatel již přihlášen.');
|
---|
11 | define('USER_LOGGED_IN', 'Byl jste přihlášen.');
|
---|
12 | define('USER_LOGGED_OUT', 'Byl jste odhlášen.');
|
---|
13 | define('BAD_PASSWORD', 'Špatné heslo.');
|
---|
14 | define('USER_TIMEOUT', 300); // in seconds
|
---|
15 |
|
---|
16 | class User extends Module
|
---|
17 | {
|
---|
18 | var $Roles = array();
|
---|
19 | var $User = array();
|
---|
20 | var $DefaultRole = 2;
|
---|
21 |
|
---|
22 | function Check()
|
---|
23 | {
|
---|
24 | $SID = session_id();
|
---|
25 |
|
---|
26 | // Lookup user record
|
---|
27 | $Query = $this->Database->select('UserOnline', '*', 'SessionId="'.$SID.'"');
|
---|
28 | if($Query->num_rows > 0)
|
---|
29 | {
|
---|
30 | // Refresh time of last access
|
---|
31 | $this->Database->update('UserOnline', 'SessionId="'.$SID.'"', array('Time' => 'NOW()'));
|
---|
32 | } else $this->Database->insert('UserOnline', array('SessionId' => $SID, 'User' => 1, 'Time' => 'NOW()', 'IpAddress' => (gethostbyaddr(GetRemoteAddress()).' '.GetRemoteAddress())));
|
---|
33 |
|
---|
34 | // Odeber neaktivní uživatele
|
---|
35 | $this->Database->delete('UserOnline', 'Time < DATE_SUB(NOW(), INTERVAL '.USER_TIMEOUT.' SECOND)');
|
---|
36 |
|
---|
37 | // Zkontroluj přihlášení
|
---|
38 | $Query = $this->Database->select('UserOnline', '*', 'SessionId="'.$SID.'"');
|
---|
39 | $Row = $Query->fetch_array();
|
---|
40 | if($Row['User'] != 0)
|
---|
41 | {
|
---|
42 | $Query = $this->Database->select('User', '*', "Id=".$Row['User']."");
|
---|
43 | $this->User = $Query->fetch_array();
|
---|
44 | $Result = USER_LOGGED;
|
---|
45 | } else {
|
---|
46 | $Query = $this->Database->select('User', '*', "Id=1");
|
---|
47 | $this->User = $Query->fetch_array();
|
---|
48 | $Result = USER_NOT_LOGGED;
|
---|
49 | }
|
---|
50 | $this->LoadPermission($this->User['Role']);
|
---|
51 |
|
---|
52 | // Role and permission
|
---|
53 | $this->LoadRoles();
|
---|
54 |
|
---|
55 | }
|
---|
56 |
|
---|
57 | function Register($Nick, $Password, $Password2, $Email, $FullName)
|
---|
58 | {
|
---|
59 | global $Options;
|
---|
60 | if(($Email == '') || ($Nick == '') || ($Password == '') || ($Password2 == '')) $Result = DATA_MISSING;
|
---|
61 | else if($Password != $Password2) $Result = PASSWORDS_UNMATCHED;
|
---|
62 | else
|
---|
63 | {
|
---|
64 | // Je uživatel registrován?
|
---|
65 | $Query = $this->Database->select('User', '*', 'Name = "'.$Nick.'"');
|
---|
66 | if($Query->num_rows() > 0) $Result = NICK_USED;
|
---|
67 | else
|
---|
68 | {
|
---|
69 | $this->Database->insert('User', array('Name' => addslashes($Nick), 'FullName' => addslashes($FullName), 'Password' => addslashes($Password), 'Email' => htmlspecialchars($Email), 'Role' => $this->DefaultRole));
|
---|
70 | $Result = USER_REGISTRATED;
|
---|
71 | }
|
---|
72 | }
|
---|
73 | return($Result);
|
---|
74 | }
|
---|
75 |
|
---|
76 | function Login($Nick, $Password)
|
---|
77 | {
|
---|
78 | $SID = session_id();
|
---|
79 | // Je uživatel registrován?
|
---|
80 | $Query = $this->Database->select('User', '*', 'Name="'.$Nick.'"');
|
---|
81 | if($Query->num_rows > 0)
|
---|
82 | {
|
---|
83 | $Row = $Query->fetch_array();
|
---|
84 | if($Row['Password'] != $Password) $Result = BAD_PASSWORD;
|
---|
85 | else
|
---|
86 | {
|
---|
87 | $this->Database->update('User', 'Id='.$Row['Id'], array('LastLoginTime' => 'NOW()'));
|
---|
88 | $this->Database->update('UserOnline', 'SessionId="'.$SID.'"', array('User' => $Row['Id']));
|
---|
89 | // načtení stavu stromu
|
---|
90 | $Result = USER_LOGGED_IN;
|
---|
91 | }
|
---|
92 | } else $Result = USER_NOT_REGISTRED;
|
---|
93 | return($Result);
|
---|
94 | }
|
---|
95 |
|
---|
96 | function Logout()
|
---|
97 | {
|
---|
98 | $SID = session_id();
|
---|
99 | $this->Database->update('UserOnline', 'SessionId="'.$SID.'"', array('User' => 1));
|
---|
100 | return(USER_LOGGED_OUT);
|
---|
101 | }
|
---|
102 |
|
---|
103 | function LoadRoles()
|
---|
104 | {
|
---|
105 | $this->Roles = array();
|
---|
106 | $DbResult = $this->Database->select('UserRole', '*');
|
---|
107 | while($DbRow = $DbResult->fetch_array())
|
---|
108 | $this->Roles[] = $DbRow;
|
---|
109 | }
|
---|
110 |
|
---|
111 | function LoadPermission($Role)
|
---|
112 | {
|
---|
113 | $this->User['Permission'] = array();
|
---|
114 | $DbResult = $this->Database->query('SELECT `UserRolePermission`.*, `PermissionOperation`.`Description` FROM `UserRolePermission` JOIN `PermissionOperation` ON `PermissionOperation`.`Id` = `UserRolePermission`.`Operation` WHERE `UserRolePermission`.`Role` = '.$Role);
|
---|
115 | if($DbResult->num_rows > 0)
|
---|
116 | while($DbRow = $DbResult->fetch_array())
|
---|
117 | $this->User['Permission'][$DbRow['Operation']] = $DbRow;
|
---|
118 | }
|
---|
119 |
|
---|
120 | function PermissionMatrix()
|
---|
121 | {
|
---|
122 | $Result = array();
|
---|
123 | $DbResult = $this->Database->query('SELECT `UserRolePermission`.*, `PermissionOperation`.`Description`, `UserRole`.`Title` FROM `UserRolePermission` LEFT JOIN `PermissionOperation` ON `PermissionOperation`.`Id` = `UserRolePermission`.`Operation` LEFT JOIN `UserRole` ON `UserRole`.`Id` = `UserRolePermission`.`Role`');
|
---|
124 | while($DbRow = $DbResult->fetch_array())
|
---|
125 | {
|
---|
126 | $Value = '';
|
---|
127 | if($DbRow['Read']) $Value .= 'R';
|
---|
128 | if($DbRow['Write']) $Value .= 'W';
|
---|
129 | $Result[$DbRow['Description']][$DbRow['Title']] = $Value;
|
---|
130 | }
|
---|
131 | return($Result);
|
---|
132 | }
|
---|
133 | }
|
---|
134 |
|
---|
135 | ?>
|
---|