source: trunk/www/user.php@ 4

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