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 | define('USER_EVENT_REGISTER', 1);
|
---|
17 | define('USER_EVENT_LOGIN', 2);
|
---|
18 | define('USER_EVENT_LOGOUT', 3);
|
---|
19 | define('USER_EVENT_OPTIONS_CHANGED', 4);
|
---|
20 |
|
---|
21 | class User extends Module
|
---|
22 | {
|
---|
23 | var $Dependencies = array('Log');
|
---|
24 | var $Roles = array();
|
---|
25 | var $User = array();
|
---|
26 | var $DefaultRole = 2;
|
---|
27 | var $AnonymousUserId = 1;
|
---|
28 |
|
---|
29 | function Check()
|
---|
30 | {
|
---|
31 | $SID = session_id();
|
---|
32 | // Lookup user record
|
---|
33 | $Query = $this->Database->select('UserOnline', '*', 'SessionId="'.$SID.'"');
|
---|
34 | if($Query->num_rows > 0)
|
---|
35 | {
|
---|
36 | // Refresh time of last access
|
---|
37 | $this->Database->update('UserOnline', 'SessionId="'.$SID.'"', array('Time' => 'NOW()'));
|
---|
38 | } else $this->Database->insert('UserOnline', array('SessionId' => $SID, 'User' => $this->AnonymousUserId, 'Time' => 'NOW()', 'IpAddress' => GetRemoteAddress(), 'HostName' => gethostbyaddr(GetRemoteAddress())));
|
---|
39 | //echo($this->Database->LastQuery);
|
---|
40 |
|
---|
41 | // Odeber neaktivní uživatele
|
---|
42 | $this->Database->delete('UserOnline', 'Time < DATE_SUB(NOW(), INTERVAL '.USER_TIMEOUT.' SECOND)');
|
---|
43 |
|
---|
44 | // Zkontroluj přihlášení
|
---|
45 | $Query = $this->Database->select('UserOnline', '*', 'SessionId="'.$SID.'"');
|
---|
46 | $Row = $Query->fetch_array();
|
---|
47 | if($Row['User'] != $this->AnonymousUserId)
|
---|
48 | {
|
---|
49 | $Query = $this->Database->select('User', '*', "Id=".$Row['User']."");
|
---|
50 | $this->User = $Query->fetch_array();
|
---|
51 | $Result = USER_LOGGED;
|
---|
52 | } else
|
---|
53 | {
|
---|
54 | $Query = $this->Database->select('User', '*', "Id=".$this->AnonymousUserId);
|
---|
55 | $this->User = $Query->fetch_array();
|
---|
56 | $Result = USER_NOT_LOGGED;
|
---|
57 | }
|
---|
58 | //$this->LoadPermission($this->User['Role']);
|
---|
59 |
|
---|
60 | // Role and permission
|
---|
61 | //$this->LoadRoles();
|
---|
62 |
|
---|
63 | }
|
---|
64 |
|
---|
65 | function Register($Nick, $Password, $Password2, $Email, $FirstName, $SecondName)
|
---|
66 | {
|
---|
67 | global $Options;
|
---|
68 | if(($Email == '') || ($Nick == '') || ($Password == '') || ($Password2 == '')) $Result = DATA_MISSING;
|
---|
69 | else if($Password != $Password2) $Result = PASSWORDS_UNMATCHED;
|
---|
70 | else
|
---|
71 | {
|
---|
72 | // Je uživatel registrován?
|
---|
73 | $Query = $this->Database->select('User', '*', 'Name = "'.$Nick.'"');
|
---|
74 | if($Query->num_rows > 0) $Result = NICK_USED;
|
---|
75 | else
|
---|
76 | {
|
---|
77 | $this->Database->insert('User', array('Name' => $Nick, 'FirstName' => $FirstName, 'SecondName' => $SecondName, 'Password' => sha1($Password), 'Email' => $Email, 'RegistrationTime' => 'NOW()'));
|
---|
78 | $Result = USER_REGISTRATED;
|
---|
79 | $this->System->Modules['Log']->NewRecord('User', 'Uživatel registrován', $Nick);
|
---|
80 | }
|
---|
81 | }
|
---|
82 | return($Result);
|
---|
83 | }
|
---|
84 |
|
---|
85 | function Login($Nick, $Password)
|
---|
86 | {
|
---|
87 | $SID = session_id();
|
---|
88 | // Je uživatel registrován?
|
---|
89 | $Query = $this->Database->select('User', '*', 'Name="'.$Nick.'"');
|
---|
90 | if($Query->num_rows > 0)
|
---|
91 | {
|
---|
92 | $Row = $Query->fetch_array();
|
---|
93 | if($Row['Password'] != sha1($Password)) $Result = BAD_PASSWORD;
|
---|
94 | else
|
---|
95 | {
|
---|
96 | $this->Database->update('User', 'Id='.$Row['Id'], array('LastLoginTime' => 'NOW()'));
|
---|
97 | $this->Database->update('UserOnline', 'SessionId="'.$SID.'"', array('User' => $Row['Id']));
|
---|
98 | // načtení stavu stromu
|
---|
99 | $Result = USER_LOGGED_IN;
|
---|
100 | $this->System->Modules['Log']->NewRecord('User', 'Uživatel přihlášen', $Nick);
|
---|
101 | }
|
---|
102 | } else $Result = USER_NOT_REGISTRED;
|
---|
103 | $this->Check();
|
---|
104 | return($Result);
|
---|
105 | }
|
---|
106 |
|
---|
107 | function Logout()
|
---|
108 | {
|
---|
109 | $SID = session_id();
|
---|
110 | $this->Database->update('UserOnline', 'SessionId="'.$SID.'"', array('User' => $this->AnonymousUserId));
|
---|
111 | $this->System->Modules['Log']->NewRecord('User', 'Uživatel odhlášen', $this->User['Name']);
|
---|
112 | $this->Check();
|
---|
113 | return(USER_LOGGED_OUT);
|
---|
114 | }
|
---|
115 |
|
---|
116 | function LoadRoles()
|
---|
117 | {
|
---|
118 | $this->Roles = array();
|
---|
119 | $DbResult = $this->Database->select('UserRole', '*');
|
---|
120 | while($DbRow = $DbResult->fetch_array())
|
---|
121 | $this->Roles[] = $DbRow;
|
---|
122 | }
|
---|
123 |
|
---|
124 | function LoadPermission($Role)
|
---|
125 | {
|
---|
126 | $this->User['Permission'] = array();
|
---|
127 | $DbResult = $this->Database->query('SELECT `UserRolePermission`.*, `PermissionOperation`.`Description` FROM `UserRolePermission` JOIN `PermissionOperation` ON `PermissionOperation`.`Id` = `UserRolePermission`.`Operation` WHERE `UserRolePermission`.`Role` = '.$Role);
|
---|
128 | if($DbResult->num_rows > 0)
|
---|
129 | while($DbRow = $DbResult->fetch_array())
|
---|
130 | $this->User['Permission'][$DbRow['Operation']] = $DbRow;
|
---|
131 | }
|
---|
132 |
|
---|
133 | function PermissionMatrix()
|
---|
134 | {
|
---|
135 | $Result = array();
|
---|
136 | $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`');
|
---|
137 | while($DbRow = $DbResult->fetch_array())
|
---|
138 | {
|
---|
139 | $Value = '';
|
---|
140 | if($DbRow['Read']) $Value .= 'R';
|
---|
141 | if($DbRow['Write']) $Value .= 'W';
|
---|
142 | $Result[$DbRow['Description']][$DbRow['Title']] = $Value;
|
---|
143 | }
|
---|
144 | return($Result);
|
---|
145 | }
|
---|
146 |
|
---|
147 | function CheckGroupPermission($GroupId, $OperationId)
|
---|
148 | {
|
---|
149 | // Check group-group relation
|
---|
150 | $DbResult = $this->Database->select('PermissionGroupAssignment', '*', '`Group`="'.$GroupId.'" AND `Type`="Group"');
|
---|
151 | while($DbRow = $DbResult->fetch_array())
|
---|
152 | {
|
---|
153 | if($this->CheckGroupPermission($DbRow['GroupOrOperation'], $OperationId) == true) return(true);
|
---|
154 | }
|
---|
155 |
|
---|
156 | // Check group-operation relation
|
---|
157 | $DbResult = $this->Database->select('PermissionGroupAssignment', '*', '`Group`="'.$GroupId.'" AND `GroupOrOperation`="'.$OperationId.'" AND `Type`="Operation"');
|
---|
158 | if($DbResult->num_rows > 0) return(true);
|
---|
159 | return(false);
|
---|
160 | }
|
---|
161 |
|
---|
162 | function CheckPermission($Module, $Operation, $ItemType = '', $ItemIndex = 0)
|
---|
163 | {
|
---|
164 | $DbResult = $this->Database->select('PermissionOperation', 'Id', '`Module`="'.$Module.'" AND `Item`="'.$ItemType.'" AND `ItemId`='.$ItemIndex.' AND `Operation`="'.$Operation.'"');
|
---|
165 | if($DbResult->num_rows > 0)
|
---|
166 | {
|
---|
167 | $DbRow = $DbResult->fetch_array();
|
---|
168 | $OperationId = $DbRow['Id'];
|
---|
169 |
|
---|
170 | // Check user-operation relation
|
---|
171 | $DbResult = $this->Database->select('PermissionUserAssignment', '*', '`User`="'.$this->User['Id'].'" AND `GroupOrOperation`="'.$OperationId.'" AND `Type`="Operation"');
|
---|
172 | if($DbResult->num_rows > 0) return(true);
|
---|
173 |
|
---|
174 | // Check user-group relation
|
---|
175 | $DbResult = $this->Database->select('PermissionUserAssignment', 'GroupOrOperation', '`User`="'.$this->User['Id'].'" AND `Type`="Group"');
|
---|
176 | while($DbRow = $DbResult->fetch_array())
|
---|
177 | {
|
---|
178 | if($this->CheckGroupPermission($DbRow['GroupOrOperation'], $OperationId) == true) return(true);
|
---|
179 | }
|
---|
180 | return(false);
|
---|
181 | } else return(false);
|
---|
182 | }
|
---|
183 | }
|
---|
184 |
|
---|
185 | ?>
|
---|