source: devel/www/user.php@ 132

Last change on this file since 132 was 132, checked in by george, 16 years ago
  • Property svn:executable set to *
File size: 12.3 KB
Line 
1<?php
2
3define('NICK_USED', 'Přihlašovací jméno již použito.');
4define('EMAIL_USED', 'Email je již použitý.');
5define('USER_REGISTRATED', 'Uživatel zaregistrován.');
6define('USER_REGISTRATION_CONFIRMED', 'Vaše registrace byla potvrzena.');
7define('DATA_MISSING', 'Chybí emailová adresa, přezdívka, nebo některé z hesel.');
8define('PASSWORDS_UNMATCHED', 'Hesla si neodpovídají.');
9define('ACCOUNT_LOCKED', 'Účet uzamčen. Po registraci je nutné provést aktivaci účtu podle zaslaného aktivačního emailu.');
10define('USER_NOT_LOGGED', 'Nejste přihlášen.');
11define('USER_LOGGED', 'Uživatel přihlášen.');
12define('USER_NOT_REGISTRED', 'Uživatel neregistrován.');
13define('USER_ALREADY_LOGGED', 'Uživatel již přihlášen.');
14define('USER_LOGGED_IN', 'Byl jste přihlášen.');
15define('USER_LOGGED_OUT', 'Byl jste odhlášen.');
16define('BAD_PASSWORD', 'Špatné heslo.');
17define('USER_NOT_FOUND', 'Uživatel nenalezen.');
18define('USER_TIMEOUT', 300); // in seconds
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 = 80;
35
36 function Check()
37 {
38 $SID = session_id();
39 // Lookup user record
40 $Query = $this->Database->select('UserOnline', '*', 'SessionId="'.$SID.'"');
41 if($Query->num_rows > 0)
42 {
43 // Refresh time of last access
44 $this->Database->update('UserOnline', 'SessionId="'.$SID.'"', array('ActivityTime' => 'NOW()'));
45 } else $this->Database->insert('UserOnline', array('SessionId' => $SID, 'User' => $this->AnonymousUserId, 'LoginTime' => 'NOW()', 'ActivityTime' => 'NOW()', 'IpAddress' => GetRemoteAddress(), 'HostName' => gethostbyaddr(GetRemoteAddress())));
46 //echo($this->Database->LastQuery);
47
48 // Zkontroluj přihlášení
49 $Query = $this->Database->select('UserOnline', '*', 'SessionId="'.$SID.'"');
50 $Row = $Query->fetch_array();
51 if($Row['User'] != $this->AnonymousUserId)
52 {
53 $Query = $this->Database->select('User', '*', "Id=".$Row['User']."");
54 $this->User = $Query->fetch_array();
55 $Result = USER_LOGGED;
56 } else
57 {
58 $Query = $this->Database->select('User', '*', "Id=".$this->AnonymousUserId);
59 $this->User = $Query->fetch_array();
60 $Result = USER_NOT_LOGGED;
61 }
62
63 // Odeber neaktivní uživatele
64 $DbResult = $this->Database->select('UserOnline', 'Id, User', 'ActivityTime < DATE_SUB(NOW(), INTERVAL '.USER_TIMEOUT.' SECOND)');
65 while($DbRow = $DbResult->fetch_array())
66 {
67 $this->Database->delete('UserOnline', 'Id='.$DbRow['User']);
68 $this->System->Modules['Log']->NewRecord('User', 'Logout');
69 }
70 //$this->LoadPermission($this->User['Role']);
71
72 // Role and permission
73 //$this->LoadRoles();
74 }
75
76 function Register($Nick, $Password, $Password2, $Email, $FirstName, $SecondName)
77 {
78 global $Options, $Config;
79
80 if(($Email == '') || ($Nick == '') || ($Password == '') || ($Password2 == '')) $Result = DATA_MISSING;
81 else if($Password != $Password2) $Result = PASSWORDS_UNMATCHED;
82 else
83 {
84 // Je uživatel registrován?
85 $Query = $this->Database->select('User', '*', 'Name = "'.$Nick.'"');
86 if($Query->num_rows > 0) $Result = NICK_USED;
87 else
88 {
89 $Query = $this->Database->select('User', '*', 'Email = "'.$Email.'"');
90 if($Query->num_rows > 0) $Result = EMAIL_USED;
91 else
92 {
93 $this->Database->insert('User', array('Name' => $Nick, 'FirstName' => $FirstName, 'SecondName' => $SecondName, 'Password' => sha1($Password), 'Email' => $Email, 'RegistrationTime' => 'NOW()', 'Locked' => 1));
94 $UserId = $this->Database->insert_id;
95
96 $Subject = FromUTF8('Registrace nového účtu', 'iso2');
97 $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: ".$Nick."\n<br>Pro dokončení registrace klikněte na ".'<a href="http://'.$Config['Web']['Host'].$Config['Web']['RootFolder'].'/?Action=UserRegisterConfirm&amp;User='.$UserId.'&amp;H='.sha1($Password).'">tento odkaz</a>.'."\n<br> \n\n<br><br>Na tento email neodpovídejte.";
98 $AdditionalHeaders = "To: ".$Nick." <".$Email.">\n"."From: ".FromUTF8($Config['Web']['Title'], 'iso2')." <noreplay@zdechov.net>\n"."MIME-Version: 1.0\n"."Content-type: text/html; charset=utf-8";
99 mail($Email, $Subject, $Message, $AdditionalHeaders);
100 $Result = USER_REGISTRATED;
101 $this->System->Modules['Log']->NewRecord('User', 'NewRegistration', $Nick);
102 }
103 }
104 }
105 return($Result);
106 }
107
108 function RegisterConfirm($Id, $Hash)
109 {
110 $DbResult = $this->Database->select('User', 'Id, Name, Password', 'Id = '.$Id);
111 if($DbResult->num_rows > 0)
112 {
113 $Row = $DbResult->fetch_array();
114 if($Hash == $Row['Password'])
115 {
116 $this->Database->update('User', 'Id='.$Row['Id'], array('Locked' => 0));
117 $Output = USER_REGISTRATION_CONFIRMED;
118 $this->System->Modules['Log']->NewRecord('User', 'RegisterConfirm', 'Username='.$Row['Name']);
119 } else $Output = PASSWORDS_UNMATCHED;
120 } else $Output = USER_NOT_FOUND;
121 return($Output);
122 }
123
124 function Login($Nick, $Password)
125 {
126 $SID = session_id();
127 // Je uživatel registrován?
128 $Query = $this->Database->select('User', '*', 'Name="'.$Nick.'"');
129 if($Query->num_rows > 0)
130 {
131 $Row = $Query->fetch_array();
132 if($Row['Password'] != sha1($Password)) $Result = BAD_PASSWORD;
133 else if($Row['Locked'] == 1) $Result = ACCOUNT_LOCKED;
134 else
135 {
136 $this->Database->update('User', 'Id='.$Row['Id'], array('LastLoginTime' => 'NOW()'));
137 $this->Database->update('UserOnline', 'SessionId="'.$SID.'"', array('User' => $Row['Id']));
138 // načtení stavu stromu
139 $Result = USER_LOGGED_IN;
140 $this->System->Modules['Log']->NewRecord('User', 'Login', 'Nick='.$Nick.',Host='.gethostbyaddr(GetRemoteAddress()));
141 }
142 } else $Result = USER_NOT_REGISTRED;
143 $this->Check();
144 return($Result);
145 }
146
147 function Logout()
148 {
149 $SID = session_id();
150 $this->Database->update('UserOnline', 'SessionId="'.$SID.'"', array('User' => $this->AnonymousUserId));
151 $this->System->Modules['Log']->NewRecord('User', 'Logout', $this->User['Name']);
152 $this->Check();
153 return(USER_LOGGED_OUT);
154 }
155
156 function LoadRoles()
157 {
158 $this->Roles = array();
159 $DbResult = $this->Database->select('UserRole', '*');
160 while($DbRow = $DbResult->fetch_array())
161 $this->Roles[] = $DbRow;
162 }
163
164 function LoadPermission($Role)
165 {
166 $this->User['Permission'] = array();
167 $DbResult = $this->Database->query('SELECT `UserRolePermission`.*, `PermissionOperation`.`Description` FROM `UserRolePermission` JOIN `PermissionOperation` ON `PermissionOperation`.`Id` = `UserRolePermission`.`Operation` WHERE `UserRolePermission`.`Role` = '.$Role);
168 if($DbResult->num_rows > 0)
169 while($DbRow = $DbResult->fetch_array())
170 $this->User['Permission'][$DbRow['Operation']] = $DbRow;
171 }
172
173 function PermissionMatrix()
174 {
175 $Result = array();
176 $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`');
177 while($DbRow = $DbResult->fetch_array())
178 {
179 $Value = '';
180 if($DbRow['Read']) $Value .= 'R';
181 if($DbRow['Write']) $Value .= 'W';
182 $Result[$DbRow['Description']][$DbRow['Title']] = $Value;
183 }
184 return($Result);
185 }
186
187 function CheckGroupPermission($GroupId, $OperationId)
188 {
189 // Check group-group relation
190 $DbResult = $this->Database->select('PermissionGroupAssignment', '*', '`Group`="'.$GroupId.'" AND `Type`="Group"');
191 while($DbRow = $DbResult->fetch_array())
192 {
193 if($this->CheckGroupPermission($DbRow['GroupOrOperation'], $OperationId) == true) return(true);
194 }
195
196 // Check group-operation relation
197 $DbResult = $this->Database->select('PermissionGroupAssignment', '*', '`Group`="'.$GroupId.'" AND `GroupOrOperation`="'.$OperationId.'" AND `Type`="Operation"');
198 if($DbResult->num_rows > 0) return(true);
199 return(false);
200 }
201
202 function CheckPermission($Module, $Operation, $ItemType = '', $ItemIndex = 0)
203 {
204 $DbResult = $this->Database->select('PermissionOperation', 'Id', '`Module`="'.$Module.'" AND `Item`="'.$ItemType.'" AND `ItemId`='.$ItemIndex.' AND `Operation`="'.$Operation.'"');
205 if($DbResult->num_rows > 0)
206 {
207 $DbRow = $DbResult->fetch_array();
208 $OperationId = $DbRow['Id'];
209
210 // Check user-operation relation
211 $DbResult = $this->Database->select('PermissionUserAssignment', '*', '`User`="'.$this->User['Id'].'" AND `GroupOrOperation`="'.$OperationId.'" AND `Type`="Operation"');
212 if($DbResult->num_rows > 0) return(true);
213
214 // Check user-group relation
215 $DbResult = $this->Database->select('PermissionUserAssignment', 'GroupOrOperation', '`User`="'.$this->User['Id'].'" AND `Type`="Group"');
216 while($DbRow = $DbResult->fetch_array())
217 {
218 if($this->CheckGroupPermission($DbRow['GroupOrOperation'], $OperationId) == true) return(true);
219 }
220 return(false);
221 } else return(false);
222 }
223
224 function PasswordRecoveryRequest($Name, $Email)
225 {
226 global $Config;
227
228 $DbResult = $this->Database->select('User', 'Name, Id, Email, Password', '`Name`="'.$Name.'" AND `Email`="'.$Email.'"');
229 if($DbResult->num_rows > 0)
230 {
231 $Row = $DbResult->fetch_array();
232 $NewPassword = substr(sha1(strtoupper($Row['Name'])), 0, 7);
233
234 $Subject = 'Obnova hesla';
235 $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['Name']." je: ".$NewPassword."\n<br>Pro aktivaci tohoto hesla klikněte na ".'<a href="http://'.$Config['Web']['Host'].$Config['Web']['RootFolder'].'/?Action=PasswordRecoveryConfirm&amp;User='.$Row['Id'].'&amp;H='.$Row['Password'].'&amp;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.";
236 $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";
237 mail($Row['Email'], $Subject, $Message, $AdditionalHeaders);
238 $Output = USER_PASSWORD_RECOVERY_SUCCESS;
239 $this->System->Modules['Log']->NewRecord('User', 'PasswordRecoveryRequest', 'Username='.$Name.',Email='.$Email);
240 } else $Output = USER_PASSWORD_RECOVERY_FAIL;
241 return($Output);
242 }
243
244 function PasswordRecoveryConfirm($Id, $Hash, $NewPassword)
245 {
246 $DbResult = $this->Database->select('User', 'Id, Name, Password', 'Id = '.$Id);
247 if($DbResult->num_rows > 0)
248 {
249 $Row = $DbResult->fetch_array();
250 $NewPassword2 = substr(sha1(strtoupper($Row['Name'])), 0, 7);
251 if(($NewPassword == $NewPassword2) and ($Hash == $Row['Password']))
252 {
253 $this->Database->update('User', 'Id='.$Row['Id'], array('Password' => sha1($NewPassword), 'Locked' => 0));
254 $Output = USER_PASSWORD_RECOVERY_CONFIRMED;
255 $this->System->Modules['Log']->NewRecord('User', 'PasswordRecoveryConfirm', 'Username='.$Row['Name']);
256 } else $Output = PASSWORDS_UNMATCHED;
257 } else $Output = USER_NOT_FOUND;
258 return($Output);
259 }
260}
261
262?>
Note: See TracBrowser for help on using the repository browser.