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

Last change on this file since 347 was 347, checked in by chronos, 13 years ago
  • Upraveno: Záznam operací uživatelů přepracován na systémový modul.
  • Property svn:executable set to *
File size: 14.9 KB
Line 
1<?php
2
3define('LOGIN_USED', 'Přihlašovací jméno již použito.');
4define('NAME_USED', 'Jméno uživatele již použito');
5define('EMAIL_USED', 'Email je již použitý. Použijte jiný email nebo si můžete nechat zaslat nové heslo na email.');
6define('USER_REGISTRATED', 'Uživatel registrován. Na zadanou emailovou adresu byl poslán mail s odkazem pro aktivování účtu.');
7define('USER_REGISTRATION_CONFIRMED', 'Vaše registrace byla potvrzena.');
8define('DATA_MISSING', 'Chybí emailová adresa, přezdívka, nebo některé z hesel.');
9define('PASSWORDS_UNMATCHED', 'Hesla si neodpovídají.');
10define('ACCOUNT_LOCKED', 'Účet je uzamčen. Po registraci je nutné provést aktivaci účtu pomocí odkazu zaslaného v aktivačním emailu.');
11define('USER_NOT_LOGGED', 'Nejste přihlášen.');
12define('USER_LOGGED', 'Uživatel přihlášen.');
13define('USER_NOT_REGISTRED', 'Uživatel neregistrován.');
14define('USER_ALREADY_LOGGED', 'Uživatel již přihlášen.');
15define('USER_LOGGED_IN', 'Byl jste přihlášen.');
16define('USER_LOGGED_OUT', 'Byl jste odhlášen.');
17define('BAD_PASSWORD', 'Špatné heslo.');
18define('USER_NOT_FOUND', 'Uživatel nenalezen.');
19define('USER_PASSWORD_RECOVERY_SUCCESS', 'Přihlašovací údaje byly odeslány na zadanou emailovou adresu.');
20define('USER_PASSWORD_RECOVERY_FAIL', 'Podle zadaných údajů nebyl nalezen žádný uživatel.');
21define('USER_PASSWORD_RECOVERY_CONFIRMED', 'Nové heslo bylo aktivováno.');
22
23define('USER_EVENT_REGISTER', 1);
24define('USER_EVENT_LOGIN', 2);
25define('USER_EVENT_LOGOUT', 3);
26define('USER_EVENT_OPTIONS_CHANGED', 4);
27
28class User extends Model
29{
30 var $Dependencies = array('Log');
31 var $Roles = array();
32 var $User = array();
33 var $DefaultRole = 2;
34 var $AnonymousUserId = 98;
35 var $OnlineStateTimeout = 600; // in seconds
36
37 function __construct($Database, $System)
38 {
39 parent::__construct($Database, $System);
40 $this->Name = 'User';
41 $this->AddPropertyString('Login');
42 $this->AddPropertyString('Name');
43 $this->AddPropertyString('Password');
44 $this->AddPropertyString('Email');
45 $this->AddPropertyString('LastIpAddress');
46 $this->AddPropertyDateTime('LastLoginTime');
47 $this->AddPropertyDateTime('RegistrationTime');
48 $this->AddPropertyOneToMany('User', 'User');
49 $this->AddPropertyBoolean('Locked');
50 $this->AddPropertyInteger('ICQ');
51 $this->AddPropertyString('PhoneNumber');
52 $this->AddPropertyString('InitPassword');
53 }
54
55 function Check()
56 {
57 $SID = session_id();
58 // Lookup user record
59 $Query = $this->Database->select('UserOnline', '*', 'SessionId="'.$SID.'"');
60 if($Query->num_rows > 0)
61 {
62 // Refresh time of last access
63 $this->Database->update('UserOnline', 'SessionId="'.$SID.'"', array('ActivityTime' => 'NOW()'));
64 } else $this->Database->insert('UserOnline', array('SessionId' => $SID, 'User' => $this->AnonymousUserId, 'LoginTime' => 'NOW()', 'ActivityTime' => 'NOW()', 'IpAddress' => GetRemoteAddress(), 'HostName' => gethostbyaddr(GetRemoteAddress()), 'ScriptName' => $_SERVER['PHP_SELF']));
65 //echo($this->Database->LastQuery);
66
67 // Check login
68 $Query = $this->Database->select('UserOnline', '*', 'SessionId="'.$SID.'"');
69 $Row = $Query->fetch_assoc();
70 if($Row['User'] != $this->AnonymousUserId)
71 {
72 $Query = $this->Database->select('User', '*', 'Id='.$Row['User']);
73 $this->User = $Query->fetch_assoc();
74 $Result = USER_LOGGED;
75 } else
76 {
77 $Query = $this->Database->select('User', '*', 'Id='.$this->AnonymousUserId);
78 $this->User = $Query->fetch_assoc();
79 $Result = USER_NOT_LOGGED;
80 }
81
82 // Remove nonactive users
83 $DbResult = $this->Database->select('UserOnline', 'Id, User', 'ActivityTime < DATE_SUB(NOW(), INTERVAL '.$this->OnlineStateTimeout.' SECOND)');
84 while($DbRow = $DbResult->fetch_array())
85 {
86 $this->Database->delete('UserOnline', 'Id='.$DbRow['Id']);
87 if($DbRow['User'] != $this->AnonymousUserId) $this->System->Modules['Log']->NewRecord('User', 'Logout');
88 }
89 //$this->LoadPermission($this->User['Role']);
90
91 // Role and permission
92 //$this->LoadRoles();
93 }
94
95 function Register($Login, $Password, $Password2, $Email, $Name, $PhoneNumber, $ICQ)
96 {
97 global $Options, $Config;
98
99 if(($Email == '') || ($Login == '') || ($Password == '') || ($Password2 == '') || ($Name == '')) $Result = DATA_MISSING;
100 else if($Password != $Password2) $Result = PASSWORDS_UNMATCHED;
101 else
102 {
103 // Je uživatel registrován?
104 $Query = $this->Database->select('User', '*', 'Login = "'.$Login.'"');
105 if($Query->num_rows > 0) $Result = LOGIN_USED;
106 else
107 {
108 $Query = $this->Database->select('User', '*', 'Name = "'.$Name.'"');
109 if($Query->num_rows > 0) $Result = NAME_USED;
110 else
111 {
112 $Query = $this->Database->select('User', '*', 'Email = "'.$Email.'"');
113 if($Query->num_rows > 0) $Result = EMAIL_USED;
114 else
115 {
116 $this->Database->insert('User', array('Name' => $Name, 'Login' => $Login, 'Password' => sha1($Password), 'Email' => $Email, 'RegistrationTime' => 'NOW()', 'Locked' => 1, 'PhoneNumber' => $PhoneNumber, 'ICQ' => $ICQ));
117 $UserId = $this->Database->insert_id;
118 $this->Database->insert('PermissionUserAssignment', array('User' => $UserId, 'GroupOrOperation' => 2, 'Type' => 'Group'));
119
120 $Subject = FromUTF8('Registrace nového účtu', 'iso2');
121 $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.";
122 $AdditionalHeaders = "To: ".$Name." <".$Email.">\n"."From: ".FromUTF8($Config['Web']['Title'], 'iso2')." <noreplay@zdechov.net>\n"."MIME-Version: 1.0\n"."Content-type: text/html; charset=utf-8";
123 mail($Email, $Subject, $Message, $AdditionalHeaders);
124 $Result = USER_REGISTRATED;
125 $this->System->Modules['Log']->NewRecord('User', 'NewRegistration', $Login);
126 }
127 }
128 }
129 }
130 return($Result);
131 }
132
133 function RegisterConfirm($Id, $Hash)
134 {
135 $DbResult = $this->Database->select('User', 'Id, Login, Password', 'Id = '.$Id);
136 if($DbResult->num_rows > 0)
137 {
138 $Row = $DbResult->fetch_array();
139 if($Hash == $Row['Password'])
140 {
141 $this->Database->update('User', 'Id='.$Row['Id'], array('Locked' => 0));
142 $Output = USER_REGISTRATION_CONFIRMED;
143 $this->System->Modules['Log']->NewRecord('User', 'RegisterConfirm', 'Login='.$Row['Login'].', Id='.$Row['Id']);
144 } else $Output = PASSWORDS_UNMATCHED;
145 } else $Output = USER_NOT_FOUND;
146 return($Output);
147 }
148
149 function Login($Login, $Password)
150 {
151 $SID = session_id();
152 $Query = $this->Database->select('User', '*', 'Login="'.$Login.'"');
153 if($Query->num_rows > 0)
154 {
155 $Row = $Query->fetch_assoc();
156 if($Row['Password'] != sha1($Password)) $Result = BAD_PASSWORD;
157 else if($Row['Locked'] == 1) $Result = ACCOUNT_LOCKED;
158 else
159 {
160 $this->Database->update('User', 'Id='.$Row['Id'], array('LastLoginTime' => 'NOW()', 'LastIpAddress' => GetRemoteAddress()));
161 $this->Database->update('UserOnline', 'SessionId="'.$SID.'"', array('User' => $Row['Id']));
162 // načtení stavu stromu
163 $Result = USER_LOGGED_IN;
164 $this->System->Modules['Log']->NewRecord('User', 'Login', 'Login='.$Login.',Host='.gethostbyaddr(GetRemoteAddress()));
165 }
166 } else $Result = USER_NOT_REGISTRED;
167 $this->Check();
168 return($Result);
169 }
170
171 function Logout()
172 {
173 $SID = session_id();
174 $this->Database->update('UserOnline', 'SessionId="'.$SID.'"', array('User' => $this->AnonymousUserId));
175 $this->System->Modules['Log']->NewRecord('User', 'Logout', $this->User['Login']);
176 $this->Check();
177 return(USER_LOGGED_OUT);
178 }
179
180 function LoadRoles()
181 {
182 $this->Roles = array();
183 $DbResult = $this->Database->select('UserRole', '*');
184 while($DbRow = $DbResult->fetch_array())
185 $this->Roles[] = $DbRow;
186 }
187
188 function LoadPermission($Role)
189 {
190 $this->User['Permission'] = array();
191 $DbResult = $this->Database->query('SELECT `UserRolePermission`.*, `PermissionOperation`.`Description` FROM `UserRolePermission` JOIN `PermissionOperation` ON `PermissionOperation`.`Id` = `UserRolePermission`.`Operation` WHERE `UserRolePermission`.`Role` = '.$Role);
192 if($DbResult->num_rows > 0)
193 while($DbRow = $DbResult->fetch_array())
194 $this->User['Permission'][$DbRow['Operation']] = $DbRow;
195 }
196
197 function PermissionMatrix()
198 {
199 $Result = array();
200 $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`');
201 while($DbRow = $DbResult->fetch_array())
202 {
203 $Value = '';
204 if($DbRow['Read']) $Value .= 'R';
205 if($DbRow['Write']) $Value .= 'W';
206 $Result[$DbRow['Description']][$DbRow['Title']] = $Value;
207 }
208 return($Result);
209 }
210
211 function CheckGroupPermission($GroupId, $OperationId)
212 {
213 // Check group-group relation
214 $DbResult = $this->Database->select('PermissionGroupAssignment', '*', '`Group`="'.$GroupId.'" AND `Type`="Group"');
215 while($DbRow = $DbResult->fetch_array())
216 {
217 if($this->CheckGroupPermission($DbRow['GroupOrOperation'], $OperationId) == true) return(true);
218 }
219
220 // Check group-operation relation
221 $DbResult = $this->Database->select('PermissionGroupAssignment', '*', '`Group`="'.$GroupId.'" AND `GroupOrOperation`="'.$OperationId.'" AND `Type`="Operation"');
222 if($DbResult->num_rows > 0) return(true);
223 return(false);
224 }
225
226 function CheckPermission($Module, $Operation, $ItemType = '', $ItemIndex = 0)
227 {
228 $DbResult = $this->Database->select('PermissionOperation', 'Id', '`Module`="'.$Module.'" AND `Item`="'.$ItemType.'" AND `ItemId`='.$ItemIndex.' AND `Operation`="'.$Operation.'"');
229 if($DbResult->num_rows > 0)
230 {
231 $DbRow = $DbResult->fetch_array();
232 $OperationId = $DbRow['Id'];
233
234 // Check user-operation relation
235 $DbResult = $this->Database->select('PermissionUserAssignment', '*', '`User`="'.$this->User['Id'].'" AND `GroupOrOperation`="'.$OperationId.'" AND `Type`="Operation"');
236 if($DbResult->num_rows > 0) return(true);
237
238 // Check user-group relation
239 $DbResult = $this->Database->select('PermissionUserAssignment', 'GroupOrOperation', '`User`="'.$this->User['Id'].'" AND `Type`="Group"');
240 while($DbRow = $DbResult->fetch_array())
241 {
242 if($this->CheckGroupPermission($DbRow['GroupOrOperation'], $OperationId) == true) return(true);
243 }
244 return(false);
245 } else return(false);
246 }
247
248 function PasswordRecoveryRequest($Login, $Email)
249 {
250 global $Config;
251
252 $DbResult = $this->Database->select('User', 'Name, Id, Email, Password', '`Login`="'.$Login.'" AND `Email`="'.$Email.'"');
253 if($DbResult->num_rows > 0)
254 {
255 $Row = $DbResult->fetch_array();
256 $NewPassword = substr(sha1(strtoupper($Row['Login'])), 0, 7);
257
258 $Subject = 'Obnova hesla';
259 $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.";
260 $AdditionalHeaders = "To: ".$Row['Name']." <".$Row['Email'].">\n"."From: ".FromUTF8($Config['Web']['Title'], 'iso2')." <noreplay@zdechov.net>\n"."MIME-Version: 1.0\n"."Content-type: text/html; charset=utf-8";
261 mail($Row['Email'], $Subject, $Message, $AdditionalHeaders);
262 $Output = USER_PASSWORD_RECOVERY_SUCCESS;
263 $this->System->Modules['Log']->NewRecord('User', 'PasswordRecoveryRequest', 'Login='.$Login.',Email='.$Email);
264 } else $Output = USER_PASSWORD_RECOVERY_FAIL;
265 return($Output);
266 }
267
268 function PasswordRecoveryConfirm($Id, $Hash, $NewPassword)
269 {
270 $DbResult = $this->Database->select('User', 'Id, Login, Password', 'Id = '.$Id);
271 if($DbResult->num_rows > 0)
272 {
273 $Row = $DbResult->fetch_array();
274 $NewPassword2 = substr(sha1(strtoupper($Row['Login'])), 0, 7);
275 if(($NewPassword == $NewPassword2) and ($Hash == $Row['Password']))
276 {
277 $this->Database->update('User', 'Id='.$Row['Id'], array('Password' => sha1($NewPassword), 'Locked' => 0));
278 $Output = USER_PASSWORD_RECOVERY_CONFIRMED;
279 $this->System->Modules['Log']->NewRecord('User', 'PasswordRecoveryConfirm', 'Login='.$Row['Login']);
280 } else $Output = PASSWORDS_UNMATCHED;
281 } else $Output = USER_NOT_FOUND;
282 return($Output);
283 }
284}
285
286class UserOnline extends Model
287{
288 function __construct($Database, $System)
289 {
290 parent::__construct($Database, $System);
291 $this->Name = 'UserOnline';
292 $this->AddPropertyOneToMany('User', 'User');
293 $this->AddPropertyDateTime('ActivityTime');
294 $this->AddPropertyDateTime('LoginTime');
295 $this->AddPropertyString('SessionId');
296 $this->AddPropertyString('IpAddress');
297 $this->AddPropertyString('HostName');
298 $this->AddPropertyString('ScriptName');
299 }
300}
301
302class ModuleUser extends Module
303{
304 function __construct($Database, $System)
305 {
306 parent::__construct($Database, $System);
307 $this->Name = 'User';
308 $this->Version = '1.0';
309 $this->Creator = 'Chronos';
310 $this->License = 'GNU/GPL';
311 $this->Description = 'User management';
312 $this->Dependencies = array();
313 $this->Models = array('User', 'UserOnline');
314 }
315
316 function Init()
317 {
318 $this->System->Models['User'] = new User($this->Database, $this->System);
319 }
320
321 function Install()
322 {
323 parent::Install();
324 }
325
326 function UnInstall()
327 {
328 parent::UnInstall();
329 }
330}
331
332
333?>
Note: See TracBrowser for help on using the repository browser.