source: trunk/www/user.php@ 20

Last change on this file since 20 was 20, checked in by george, 16 years ago
  • Přidáno: Generování souboru realmlist.wtf.
  • Upraveno: Uchování portu realmd a worldd v databází namísto výpočtu z Id serveru.
  • Opraveno: Zobrazení hodnot dle skriptů typů v obecné tabulce.
  • Property svn:executable set to *
File size: 13.1 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_BAD_ROLE', 'Nemáte dostatečná oprávnění');
24
25define('USER_EVENT_REGISTER', 1);
26define('USER_EVENT_LOGIN', 2);
27define('USER_EVENT_LOGOUT', 3);
28define('USER_EVENT_OPTIONS_CHANGED', 4);
29
30define('USER_ROLE_ANONYMOUS', 1);
31define('USER_ROLE_USER', 2);
32define('USER_ROLE_ADMINISTRATOR', 3);
33
34class User extends Module
35{
36 var $Dependencies = array('Log');
37 var $Roles = array();
38 var $User = array();
39 var $DefaultRole = 2;
40 var $AnonymousUserId = 1;
41 var $OnlineStateTimeout = 600; // in seconds
42
43 function Check()
44 {
45 $SID = session_id();
46 // Lookup user record
47 $Query = $this->Database->select('UserOnline', '*', 'SessionId="'.$SID.'"');
48 if($Query->num_rows > 0)
49 {
50 // Refresh time of last access
51 $this->Database->update('UserOnline', 'SessionId="'.$SID.'"', array('ActivityTime' => 'NOW()'));
52 } else $this->Database->insert('UserOnline', array('SessionId' => $SID, 'User' => $this->AnonymousUserId, 'LoginTime' => 'NOW()', 'ActivityTime' => 'NOW()', 'IpAddress' => GetRemoteAddress(), 'HostName' => gethostbyaddr(GetRemoteAddress()), 'ScriptName' => $_SERVER['PHP_SELF']));
53 //echo($this->Database->LastQuery);
54
55 // Check login
56 $Query = $this->Database->select('UserOnline', '*', 'SessionId="'.$SID.'"');
57 $Row = $Query->fetch_assoc();
58 if($Row['User'] != $this->AnonymousUserId)
59 {
60 $Query = $this->Database->select('User', '*', 'Id='.$Row['User']);
61 $this->User = $Query->fetch_assoc();
62 $Result = USER_LOGGED;
63 } else
64 {
65 $Query = $this->Database->select('User', '*', 'Id='.$this->AnonymousUserId);
66 $this->User = $Query->fetch_assoc();
67 $Result = USER_NOT_LOGGED;
68 }
69
70 // Remove nonactive users
71 $DbResult = $this->Database->select('UserOnline', 'Id, User', 'ActivityTime < DATE_SUB(NOW(), INTERVAL '.$this->OnlineStateTimeout.' SECOND)');
72 while($DbRow = $DbResult->fetch_array())
73 {
74 $this->Database->delete('UserOnline', 'Id='.$DbRow['Id']);
75 if($DbRow['User'] != $this->AnonymousUserId) $this->System->Modules['Log']->NewRecord('User', 'Logout');
76 }
77 //$this->LoadPermission($this->User['Role']);
78
79 // Role and permission
80 //$this->LoadRoles();
81 }
82
83 function Register($Login, $Password, $Password2, $Email, $Name)
84 {
85 global $Options, $Config;
86
87 if(($Email == '') || ($Login == '') || ($Password == '') || ($Password2 == '') || ($Name == '')) $Result = DATA_MISSING;
88 else if($Password != $Password2) $Result = PASSWORDS_UNMATCHED;
89 else
90 {
91 // Je uživatel registrován?
92 $Query = $this->Database->select('User', '*', 'Login = "'.$Login.'"');
93 if($Query->num_rows > 0) $Result = LOGIN_USED;
94 else
95 {
96 $Query = $this->Database->select('User', '*', 'Name = "'.$Name.'"');
97 if($Query->num_rows > 0) $Result = NAME_USED;
98 else
99 {
100 $Query = $this->Database->select('User', '*', 'Email = "'.$Email.'"');
101 if($Query->num_rows > 0) $Result = EMAIL_USED;
102 else
103 {
104 $this->Database->insert('User', array('Name' => $Name, 'Login' => $Login, 'Password' => sha1($Password), 'Email' => $Email, 'RegistrationTime' => 'NOW()', 'Locked' => 1, 'Role' => 2));
105 $UserId = $this->Database->insert_id;
106
107 $Subject = FromUTF8('Registrace nového účtu', 'iso2');
108 $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.";
109 $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";
110 mail($Email, $Subject, $Message, $AdditionalHeaders);
111 $Result = USER_REGISTRATED;
112 $this->System->Modules['Log']->NewRecord('User', 'NewRegistration', $Login);
113 }
114 }
115 }
116 }
117 return($Result);
118 }
119
120 function RegisterConfirm($Id, $Hash)
121 {
122 $DbResult = $this->Database->select('User', 'Id, Login, Password', 'Id = '.$Id);
123 if($DbResult->num_rows > 0)
124 {
125 $Row = $DbResult->fetch_array();
126 if($Hash == $Row['Password'])
127 {
128 $this->Database->update('User', 'Id='.$Row['Id'], array('Locked' => 0));
129 $Output = USER_REGISTRATION_CONFIRMED;
130 $this->System->Modules['Log']->NewRecord('User', 'RegisterConfirm', 'Login='.$Row['Login'].', Id='.$Row['Id']);
131 } else $Output = PASSWORDS_UNMATCHED;
132 } else $Output = USER_NOT_FOUND;
133 return($Output);
134 }
135
136 function Login($Login, $Password)
137 {
138 $SID = session_id();
139 $Query = $this->Database->select('User', '*', 'Login="'.$Login.'"');
140 if($Query->num_rows > 0)
141 {
142 $Row = $Query->fetch_assoc();
143 if($Row['Password'] != $Password) $Result = BAD_PASSWORD;
144 else if($Row['Locked'] == 1) $Result = ACCOUNT_LOCKED;
145 else
146 {
147 $this->Database->update('User', 'Id='.$Row['Id'], array('LastLoginTime' => 'NOW()', 'LastIpAddress' => GetRemoteAddress()));
148 $this->Database->update('UserOnline', 'SessionId="'.$SID.'"', array('User' => $Row['Id']));
149 // načtení stavu stromu
150 $Result = USER_LOGGED_IN;
151 $this->System->Modules['Log']->NewRecord('User', 'Login', 'Login='.$Login.',Host='.gethostbyaddr(GetRemoteAddress()));
152 }
153 } else $Result = USER_NOT_REGISTRED;
154 $this->Check();
155 return($Result);
156 }
157
158 function Logout()
159 {
160 $SID = session_id();
161 $this->Database->update('UserOnline', 'SessionId="'.$SID.'"', array('User' => $this->AnonymousUserId));
162 $this->System->Modules['Log']->NewRecord('User', 'Logout', $this->User['Login']);
163 $this->Check();
164 return(USER_LOGGED_OUT);
165 }
166
167 function LoadRoles()
168 {
169 $this->Roles = array();
170 $DbResult = $this->Database->select('UserRole', '*');
171 while($DbRow = $DbResult->fetch_array())
172 $this->Roles[] = $DbRow;
173 }
174
175 function LoadPermission($Role)
176 {
177 $this->User['Permission'] = array();
178 $DbResult = $this->Database->query('SELECT `UserRolePermission`.*, `PermissionOperation`.`Description` FROM `UserRolePermission` JOIN `PermissionOperation` ON `PermissionOperation`.`Id` = `UserRolePermission`.`Operation` WHERE `UserRolePermission`.`Role` = '.$Role);
179 if($DbResult->num_rows > 0)
180 while($DbRow = $DbResult->fetch_array())
181 $this->User['Permission'][$DbRow['Operation']] = $DbRow;
182 }
183
184 function PermissionMatrix()
185 {
186 $Result = array();
187 $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`');
188 while($DbRow = $DbResult->fetch_array())
189 {
190 $Value = '';
191 if($DbRow['Read']) $Value .= 'R';
192 if($DbRow['Write']) $Value .= 'W';
193 $Result[$DbRow['Description']][$DbRow['Title']] = $Value;
194 }
195 return($Result);
196 }
197
198 function CheckGroupPermission($GroupId, $OperationId)
199 {
200 // Check group-group relation
201 $DbResult = $this->Database->select('PermissionGroupAssignment', '*', '`Group`="'.$GroupId.'" AND `Type`="Group"');
202 while($DbRow = $DbResult->fetch_array())
203 {
204 if($this->CheckGroupPermission($DbRow['GroupOrOperation'], $OperationId) == true) return(true);
205 }
206
207 // Check group-operation relation
208 $DbResult = $this->Database->select('PermissionGroupAssignment', '*', '`Group`="'.$GroupId.'" AND `GroupOrOperation`="'.$OperationId.'" AND `Type`="Operation"');
209 if($DbResult->num_rows > 0) return(true);
210 return(false);
211 }
212
213 function CheckPermission($Module, $Operation, $ItemType = '', $ItemIndex = 0)
214 {
215 $DbResult = $this->Database->select('PermissionOperation', 'Id', '`Module`="'.$Module.'" AND `Item`="'.$ItemType.'" AND `ItemId`='.$ItemIndex.' AND `Operation`="'.$Operation.'"');
216 if($DbResult->num_rows > 0)
217 {
218 $DbRow = $DbResult->fetch_array();
219 $OperationId = $DbRow['Id'];
220
221 // Check user-operation relation
222 $DbResult = $this->Database->select('PermissionUserAssignment', '*', '`User`="'.$this->User['Id'].'" AND `GroupOrOperation`="'.$OperationId.'" AND `Type`="Operation"');
223 if($DbResult->num_rows > 0) return(true);
224
225 // Check user-group relation
226 $DbResult = $this->Database->select('PermissionUserAssignment', 'GroupOrOperation', '`User`="'.$this->User['Id'].'" AND `Type`="Group"');
227 while($DbRow = $DbResult->fetch_array())
228 {
229 if($this->CheckGroupPermission($DbRow['GroupOrOperation'], $OperationId) == true) return(true);
230 }
231 return(false);
232 } else return(false);
233 }
234
235 function PasswordRecoveryRequest($Login, $Email)
236 {
237 global $Config;
238
239 $DbResult = $this->Database->select('User', 'Login, Name, Id, Email, Password', '`Login`="'.$Login.'" AND `Email`="'.$Email.'"');
240 if($DbResult->num_rows > 0)
241 {
242 $Row = $DbResult->fetch_assoc();
243 $NewPassword = substr(sha1(strtoupper($Row['Login'])), 0, 7);
244
245 $Subject = 'Obnova hesla';
246 $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.";
247 $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";
248 mail($Row['Email'], $Subject, $Message, $AdditionalHeaders);
249 $Output = USER_PASSWORD_RECOVERY_SUCCESS;
250 $this->System->Modules['Log']->NewRecord('User', 'PasswordRecoveryRequest', 'Login='.$Login.',Email='.$Email);
251 } else $Output = USER_PASSWORD_RECOVERY_FAIL;
252 return($Output);
253 }
254
255 function PasswordRecoveryConfirm($Id, $Hash, $NewPassword)
256 {
257 $DbResult = $this->Database->select('User', 'Id, Login, Password', 'Id = '.$Id);
258 if($DbResult->num_rows > 0)
259 {
260 $Row = $DbResult->fetch_array();
261 $NewPassword2 = substr(sha1(strtoupper($Row['Login'])), 0, 7);
262 if(($NewPassword == $NewPassword2) and ($Hash == $Row['Password']))
263 {
264 $this->Database->update('User', 'Id='.$Row['Id'], array('Password' => sha1($NewPassword), 'Locked' => 0));
265 $Output = USER_PASSWORD_RECOVERY_CONFIRMED;
266 $this->System->Modules['Log']->NewRecord('User', 'PasswordRecoveryConfirm', 'Login='.$Row['Login']);
267 } else $Output = PASSWORDS_UNMATCHED;
268 } else $Output = USER_NOT_FOUND;
269 return($Output);
270 }
271}
272
273?>
Note: See TracBrowser for help on using the repository browser.