source: trunk/Modules/User/User.php@ 376

Last change on this file since 376 was 376, checked in by chronos, 13 years ago
  • Property svn:executable set to *
File size: 16.7 KB
Line 
1<?php
2
3include(dirname(__FILE__).'/UserList.php');
4include(dirname(__FILE__).'/UserPermission.php');
5
6define('LOGIN_USED', 'Přihlašovací jméno již použito.');
7define('NAME_USED', 'Jméno uživatele již použito');
8define('EMAIL_USED', 'Email je již použitý. Použijte jiný email nebo si můžete nechat zaslat nové heslo na email.');
9define('USER_REGISTRATED', 'Uživatel registrován. Na zadanou emailovou adresu byl poslán mail s odkazem pro aktivování účtu.');
10define('USER_REGISTRATION_CONFIRMED', 'Vaše registrace byla potvrzena.');
11define('DATA_MISSING', 'Chybí emailová adresa, přezdívka, nebo některé z hesel.');
12define('PASSWORDS_UNMATCHED', 'Hesla si neodpovídají.');
13define('ACCOUNT_LOCKED', 'Účet je uzamčen. Po registraci je nutné provést aktivaci účtu pomocí odkazu zaslaného v aktivačním emailu.');
14define('USER_NOT_LOGGED', 'Nejste přihlášen.');
15define('USER_LOGGED', 'Uživatel přihlášen.');
16define('USER_NOT_REGISTRED', 'Uživatel neregistrován.');
17define('USER_ALREADY_LOGGED', 'Uživatel již přihlášen.');
18define('USER_LOGGED_IN', 'Byl jste přihlášen.');
19define('USER_LOGGED_OUT', 'Byl jste odhlášen.');
20define('BAD_PASSWORD', 'Špatné heslo.');
21define('USER_NOT_FOUND', 'Uživatel nenalezen.');
22define('USER_PASSWORD_RECOVERY_SUCCESS', 'Přihlašovací údaje byly odeslány na zadanou emailovou adresu.');
23define('USER_PASSWORD_RECOVERY_FAIL', 'Podle zadaných údajů nebyl nalezen žádný uživatel.');
24define('USER_PASSWORD_RECOVERY_CONFIRMED', 'Nové heslo bylo aktivováno.');
25
26define('USER_EVENT_REGISTER', 1);
27define('USER_EVENT_LOGIN', 2);
28define('USER_EVENT_LOGOUT', 3);
29define('USER_EVENT_OPTIONS_CHANGED', 4);
30
31class User extends Model
32{
33 var $Dependencies = array('Log');
34 var $Roles = array();
35 var $User = array();
36 var $DefaultRole = 2;
37 var $AnonymousUserId = 98;
38 var $OnlineStateTimeout = 600; // in seconds
39 var $PermissionCache = array();
40
41 function __construct($Database, $System)
42 {
43 parent::__construct($Database, $System);
44 $this->Name = 'User';
45 $this->AddPropertyString('Login');
46 $this->AddPropertyString('Name');
47 $this->AddPropertyString('Password');
48 $this->AddPropertyString('Email');
49 $this->AddPropertyString('LastIpAddress');
50 $this->AddPropertyDateTime('LastLoginTime');
51 $this->AddPropertyDateTime('RegistrationTime');
52 $this->AddPropertyOneToMany('User', 'User');
53 $this->AddPropertyBoolean('Locked');
54 $this->AddPropertyInteger('ICQ');
55 $this->AddPropertyString('PhoneNumber');
56 $this->AddPropertyString('InitPassword');
57 }
58
59 function Check()
60 {
61 $SID = session_id();
62 // Lookup user record
63 $Query = $this->Database->select('UserOnline', '*', 'SessionId="'.$SID.'"');
64 if($Query->num_rows > 0)
65 {
66 // Refresh time of last access
67 $this->Database->update('UserOnline', 'SessionId="'.$SID.'"', array('ActivityTime' => 'NOW()'));
68 } else $this->Database->insert('UserOnline', array('SessionId' => $SID, 'User' => $this->AnonymousUserId, 'LoginTime' => 'NOW()', 'ActivityTime' => 'NOW()', 'IpAddress' => GetRemoteAddress(), 'HostName' => gethostbyaddr(GetRemoteAddress()), 'ScriptName' => $_SERVER['PHP_SELF']));
69 //echo($this->Database->LastQuery);
70
71 // Check login
72 $Query = $this->Database->select('UserOnline', '*', 'SessionId="'.$SID.'"');
73 $Row = $Query->fetch_assoc();
74 if($Row['User'] != $this->AnonymousUserId)
75 {
76 $Query = $this->Database->select('User', '*', 'Id='.$Row['User']);
77 $this->User = $Query->fetch_assoc();
78 $Result = USER_LOGGED;
79 } else
80 {
81 $Query = $this->Database->select('User', '*', 'Id='.$this->AnonymousUserId);
82 $this->User = $Query->fetch_assoc();
83 $Result = USER_NOT_LOGGED;
84 }
85
86 // Remove nonactive users
87 $DbResult = $this->Database->select('UserOnline', 'Id, User', 'ActivityTime < DATE_SUB(NOW(), INTERVAL '.$this->OnlineStateTimeout.' SECOND)');
88 while($DbRow = $DbResult->fetch_array())
89 {
90 $this->Database->delete('UserOnline', 'Id='.$DbRow['Id']);
91 if($DbRow['User'] != $this->AnonymousUserId) $this->System->Modules['Log']->NewRecord('User', 'Logout');
92 }
93 //$this->LoadPermission($this->User['Role']);
94
95 // Role and permission
96 //$this->LoadRoles();
97 }
98
99 function Register($Login, $Password, $Password2, $Email, $Name, $PhoneNumber, $ICQ)
100 {
101 global $Config;
102
103 if(($Email == '') || ($Login == '') || ($Password == '') || ($Password2 == '') || ($Name == '')) $Result = DATA_MISSING;
104 else if($Password != $Password2) $Result = PASSWORDS_UNMATCHED;
105 else
106 {
107 // Je uživatel registrován?
108 $Query = $this->Database->select('User', '*', 'Login = "'.$Login.'"');
109 if($Query->num_rows > 0) $Result = LOGIN_USED;
110 else
111 {
112 $Query = $this->Database->select('User', '*', 'Name = "'.$Name.'"');
113 if($Query->num_rows > 0) $Result = NAME_USED;
114 else
115 {
116 $Query = $this->Database->select('User', '*', 'Email = "'.$Email.'"');
117 if($Query->num_rows > 0) $Result = EMAIL_USED;
118 else
119 {
120 $this->Database->insert('User', array('Name' => $Name, 'Login' => $Login, 'Password' => sha1($Password), 'Email' => $Email, 'RegistrationTime' => 'NOW()', 'Locked' => 1, 'PhoneNumber' => $PhoneNumber, 'ICQ' => $ICQ));
121 $UserId = $this->Database->insert_id;
122 $this->Database->insert('PermissionUserAssignment', array('User' => $UserId, 'GroupOrOperation' => 2, 'Type' => 'Group'));
123
124 $Mail = new Mail();
125 $Mail->Subject = 'Registrace nového účtu';
126 $Mail->Message = 'Provedli jste registraci nového účtu na serveru <a href="http://'.$Config['Web']['Host'].$Config['Web']['RootFolder'].'">http://'.$Config['Web']['Host'].$Config['Web']['RootFolder']."</a>.<br>\nPokud jste tak neučinili, měli by jste tento email ignorovat.<br><br>\n\nVáš účet je: ".$Login."\n<br>Pro dokončení registrace klikněte na tento odkaz: ".'<a href="http://'.$Config['Web']['Host'].$Config['Web']['RootFolder'].'/?Action=UserRegisterConfirm&User='.$UserId.'&H='.sha1($Password).'">http://'.$Config['Web']['Host'].$Config['Web']['RootFolder'].'/?Action=UserRegisterConfirm&User='.$UserId.'&H='.sha1($Password).'</a>.'."\n<br> \n\n<br><br>Na tento email neodpovídejte.";
127 $Mail->RecipientName = $Name;
128 $Mail->RecipientAddress = $Email;
129 $Mail->SenderName = $Config['Web']['Title'];
130 $Mail->SenderAddress = 'noreplay@zdechov.net';
131 $Mail->Send();
132 $Result = USER_REGISTRATED;
133 $this->System->Modules['Log']->NewRecord('User', 'NewRegistration', $Login);
134 }
135 }
136 }
137 }
138 return($Result);
139 }
140
141 function RegisterConfirm($Id, $Hash)
142 {
143 $DbResult = $this->Database->select('User', 'Id, Login, Password', 'Id = '.$Id);
144 if($DbResult->num_rows > 0)
145 {
146 $Row = $DbResult->fetch_array();
147 if($Hash == $Row['Password'])
148 {
149 $this->Database->update('User', 'Id='.$Row['Id'], array('Locked' => 0));
150 $Output = USER_REGISTRATION_CONFIRMED;
151 $this->System->Modules['Log']->NewRecord('User', 'RegisterConfirm', 'Login='.$Row['Login'].', Id='.$Row['Id']);
152 } else $Output = PASSWORDS_UNMATCHED;
153 } else $Output = USER_NOT_FOUND;
154 return($Output);
155 }
156
157 function Login($Login, $Password)
158 {
159 $SID = session_id();
160 $Query = $this->Database->select('User', '*', 'Login="'.$Login.'"');
161 if($Query->num_rows > 0)
162 {
163 $Row = $Query->fetch_assoc();
164 if($Row['Password'] != sha1($Password)) $Result = BAD_PASSWORD;
165 else if($Row['Locked'] == 1) $Result = ACCOUNT_LOCKED;
166 else
167 {
168 $this->Database->update('User', 'Id='.$Row['Id'], array('LastLoginTime' => 'NOW()', 'LastIpAddress' => GetRemoteAddress()));
169 $this->Database->update('UserOnline', 'SessionId="'.$SID.'"', array('User' => $Row['Id']));
170 // načtení stavu stromu
171 $Result = USER_LOGGED_IN;
172 $this->System->Modules['Log']->NewRecord('User', 'Login', 'Login='.$Login.',Host='.gethostbyaddr(GetRemoteAddress()));
173 }
174 } else $Result = USER_NOT_REGISTRED;
175 $this->Check();
176 return($Result);
177 }
178
179 function Logout()
180 {
181 $SID = session_id();
182 $this->Database->update('UserOnline', 'SessionId="'.$SID.'"', array('User' => $this->AnonymousUserId));
183 $this->System->Modules['Log']->NewRecord('User', 'Logout', $this->User['Login']);
184 $this->Check();
185 return(USER_LOGGED_OUT);
186 }
187
188 function CheckGroupPermission($GroupId, $OperationId)
189 {
190 // Check group-group relation
191 $DbResult = $this->Database->select('PermissionGroupAssignment', '*', '`Group`="'.$GroupId.'"');
192 while($DbRow = $DbResult->fetch_array())
193 {
194 if($this->CheckGroupPermission($DbRow['AssignedGroup'], $OperationId) == true) return(true);
195 }
196
197 // Check group-operation relation
198 $DbResult = $this->Database->select('PermissionGroupAssignment', '*', '`Group`="'.$GroupId.'" AND `AssignedOperation`="'.$OperationId.'"');
199 if($DbResult->num_rows > 0) return(true);
200 return(false);
201 }
202
203 function CheckPermission($Module, $Operation, $ItemType = '', $ItemIndex = 0)
204 {
205 // First try to check cache
206 if(in_array(array($Module, $Operation, $ItemType, $ItemType), $this->PermissionCache))
207 {
208 $OperationId = array_search(array($Module, $Operation, $ItemType, $ItemType), $this->PermissionCache);
209 $PermissionExists = is_numeric($OperationId);
210 } else
211 {
212 // If no permission combination exists in cache, do new check of database items
213 $DbResult = $this->Database->select('PermissionOperation', 'Id', '`Module`="'.$Module.'" AND `Item`="'.$ItemType.'" AND `ItemId`='.$ItemIndex.' AND `Operation`="'.$Operation.'"');
214 if($DbResult->num_rows > 0)
215 {
216 $DbRow = $DbResult->fetch_array();
217 $OperationId = $DbRow['Id'];
218 $this->PermissionCache[$DbRow['Id']] = array($Module, $Operation, $ItemType, $ItemType);
219 $PermissionExists = true;
220 } else
221 {
222 $this->PermissionCache[$DbRow['Id'].'_'] = array($Module, $Operation, $ItemType, $ItemType);
223 $PermissionExists = false;
224 }
225 }
226
227 if($PermissionExists)
228 {
229 // Check user-operation relation
230 $DbResult = $this->Database->select('PermissionUserAssignment', '*', '`User`="'.$this->User['Id'].'" AND `AssignedOperation`="'.$OperationId.'"');
231 if($DbResult->num_rows > 0) return(true);
232
233 // Check user-group relation
234 $DbResult = $this->Database->select('PermissionUserAssignment', 'AssignedGroup', '`User`="'.$this->User['Id'].'"');
235 while($DbRow = $DbResult->fetch_array())
236 {
237 if($this->CheckGroupPermission($DbRow['AssignedGroup'], $OperationId) == true) return(true);
238 }
239 return(false);
240 } else return(false);
241 }
242
243 function PasswordRecoveryRequest($Login, $Email)
244 {
245 global $Config;
246
247 $DbResult = $this->Database->select('User', 'Name, Id, Email, Password', '`Login`="'.$Login.'" AND `Email`="'.$Email.'"');
248 if($DbResult->num_rows > 0)
249 {
250 $Row = $DbResult->fetch_array();
251 $NewPassword = substr(sha1(strtoupper($Row['Login'])), 0, 7);
252
253 $Mail = new Mail();
254 $Mail->Subject = 'Obnova hesla';
255 $Mail->Message = 'Požádali jste o zaslání nového hesla na serveru <a href="http://'.$Config['Web']['Host'].$Config['Web']['RootFolder'].'">http://'.$Config['Web']['Host'].$Config['Web']['RootFolder']."</a>.<br />\nPokud jste tak neučinili, měli by jste tento email ignorovat.<br /><br />\n\nVaše nové heslo k účtu ".$Row['Login']." je: ".$NewPassword."\n<br>Pro aktivaci tohoto hesla klikněte na ".'<a href="http://'.$Config['Web']['Host'].$Config['Web']['RootFolder'].'/?Action=PasswordRecoveryConfirm&User='.$Row['Id'].'&H='.$Row['Password'].'&P='.$NewPassword.'">tento odkaz</a>.'."\n<br /> Po přihlášení si prosím změňte heslo na nové.\n\n<br><br>Na tento email neodpovídejte.";
256 $Mail->RecipientAddress = $Row['Email'];
257 $Mail->RecipientName = $Row['Name'];
258 $Mail->SenderAddress = 'noreplay@zdechov.net';
259 $Mail->SenderName = $Config['Web']['Title'];
260 $Mail->Send();
261 $Output = USER_PASSWORD_RECOVERY_SUCCESS;
262 $this->System->Modules['Log']->NewRecord('User', 'PasswordRecoveryRequest', 'Login='.$Login.',Email='.$Email);
263 } else $Output = USER_PASSWORD_RECOVERY_FAIL;
264 return($Output);
265 }
266
267 function PasswordRecoveryConfirm($Id, $Hash, $NewPassword)
268 {
269 $DbResult = $this->Database->select('User', 'Id, Login, Password', 'Id = '.$Id);
270 if($DbResult->num_rows > 0)
271 {
272 $Row = $DbResult->fetch_array();
273 $NewPassword2 = substr(sha1(strtoupper($Row['Login'])), 0, 7);
274 if(($NewPassword == $NewPassword2) and ($Hash == $Row['Password']))
275 {
276 $this->Database->update('User', 'Id='.$Row['Id'], array('Password' => sha1($NewPassword), 'Locked' => 0));
277 $Output = USER_PASSWORD_RECOVERY_CONFIRMED;
278 $this->System->Modules['Log']->NewRecord('User', 'PasswordRecoveryConfirm', 'Login='.$Row['Login']);
279 } else $Output = PASSWORDS_UNMATCHED;
280 } else $Output = USER_NOT_FOUND;
281 return($Output);
282 }
283}
284
285class UserOnline extends Model
286{
287 function __construct($Database, $System)
288 {
289 parent::__construct($Database, $System);
290 $this->Name = 'UserOnline';
291 $this->AddPropertyOneToMany('User', 'User');
292 $this->AddPropertyDateTime('ActivityTime');
293 $this->AddPropertyDateTime('LoginTime');
294 $this->AddPropertyString('SessionId');
295 $this->AddPropertyString('IpAddress');
296 $this->AddPropertyString('HostName');
297 $this->AddPropertyString('ScriptName');
298 }
299}
300
301class UserOptionsView extends ViewForm
302{
303 function __construct($Database)
304 {
305 parent::__construct($Database);
306 $this->Name = 'UserOptions';
307 $this->Title = 'Nastavení uživatele';
308 $this->ModelName = 'User';
309 $this->AddItemString('Login', 'Přihlašovací jméno', '');
310 $this->AddItemPassword('Password', 'Heslo', '');
311 $this->AddItemString('Name', 'Zobrazované jméno', '');
312 $this->AddItemString('Email', 'E-mail', '');
313 $this->AddItemString('PhoneNumber', 'Telefon', '');
314 $this->AddItemString('ICQ', 'ICQ', '');
315 }
316}
317
318class UserRegisterView extends ViewForm
319{
320 function __construct($Database)
321 {
322 parent::__construct($Database);
323 $this->Name = 'UserRegister';
324 $this->Title = 'Registrace uživatele';
325 $this->SubmitText = 'Registrovat';
326 $this->ModelName = 'User';
327 $this->AddItemString('Login', 'Přihlašovací jméno', '');
328 $this->AddItemPassword('Password', 'Heslo', '');
329 $this->AddItemPassword('Password2', 'Potvrzení hesla', '');
330 $this->AddItemString('Name', 'Zobrazované jméno', '');
331 $this->AddItemString('Email', 'E-mail', '');
332 $this->AddItemString('PhoneNumber', 'Telefon', '');
333 $this->AddItemString('ICQ', 'ICQ', '');
334 }
335}
336
337class PasswordRecoveryView extends ViewForm
338{
339 function __construct($Database)
340 {
341 parent::__construct($Database);
342 $this->Name = 'PasswordRecovery';
343 $this->Title = 'Obnova hesla';
344 $this->SubmitText = 'Obnovit';
345 $this->AddItemString('Name', 'Přihlašovací jméno', '');
346 $this->AddItemString('Email', 'E-mail', '');
347 }
348}
349
350class UserLoginView extends ViewForm
351{
352 function __construct($Database)
353 {
354 parent::__construct($Database);
355 $this->Name = 'UserLogin';
356 $this->Title = 'Přihlášení uživatele';
357 $this->SubmitText = 'Přihlásit';
358 $this->AddItemString('Username', 'Přihlašovací jméno', '');
359 $this->AddItemPassword('Password', 'Heslo', '');
360 }
361}
362
363class ModuleUser extends Module
364{
365 function __construct($Database, $System)
366 {
367 parent::__construct($Database, $System);
368 $this->Name = 'User';
369 $this->Version = '1.0';
370 $this->Creator = 'Chronos';
371 $this->License = 'GNU/GPL';
372 $this->Description = 'User management';
373 $this->Dependencies = array();
374 $this->Models = array('User', 'UserOnline');
375 $this->Views = array('UserLogin', 'PasswordRecove', 'UserRegister', 'UserOptions',
376 'PermissionGroup', 'PermissionOperation', 'PermissionGroupAssignement',
377 'PermissionUserAssignment');
378 }
379
380 function Init()
381 {
382 $this->System->Models['User'] = new User($this->Database, $this->System);
383 $this->System->Pages['uzivatele'] = 'UserListPage';
384 if(isset($_SERVER['REMOTE_ADDR'])) $this->System->Models['User']->Check();
385 }
386
387 function Install()
388 {
389 parent::Install();
390 }
391
392 function UnInstall()
393 {
394 parent::UnInstall();
395 }
396}
397
398
399?>
Note: See TracBrowser for help on using the repository browser.