source: trunk/user.php

Last change on this file was 36, checked in by george, 15 years ago
  • Opraveno: Různá chybová hlášení.
  • Upraveno: Zobrazení přímo skriptu a čísla řádku, ve kterém nastala chyba. Funkce již není přímo zobrazována.
  • Property svn:executable set to *
File size: 10.2 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 $TableUser = 'User';
35 var $TableUserOnline = 'UserOnline';
36
37 function Check()
38 {
39 global $Config;
40
41 $SID = session_id();
42 // Lookup user record
43 $Query = $this->Database->select(array('Table' => $this->TableUserOnline, 'Condition' => '`SessionId`="'.$SID.'"'));
44 if($Query->num_rows > 0)
45 {
46 // Refresh time of last access
47 $this->Database->update(array('Table' => $this->TableUserOnline, 'Condition' => '`SessionId`="'.$SID.'"'), array('ActivityTime' => 'NOW()'));
48 } else $this->Database->insert(array('Table' => $this->TableUserOnline), array('SessionId' => $SID, 'User' => 0, 'LoginTime' => 'NOW()', 'ActivityTime' => 'NOW()', 'IpAddress' => GetRemoteAddress(), 'HostName' => gethostbyaddr(GetRemoteAddress())));
49 //echo($this->Database->LastQuery);
50
51 // Zkontroluj přihlášení
52 $Query = $this->Database->select(array('Table' => $this->TableUserOnline, 'Condition' => 'SessionId="'.$SID.'"'));
53 $Row = $Query->fetch_assoc();
54 if($Row['User'] != 0)
55 {
56 $Query = $this->Database->query('SELECT * FROM `'.$this->TableUser.'` WHERE `Id`='.$Row['User']);
57 $this->User = $Query->fetch_assoc();
58 //print_r($this->User);
59 $Result = USER_LOGGED;
60 } else
61 {
62 $Query = $this->Database->select(array('Table' => $this->TableUser, 'Condition' => "`Id`=0"));
63 $this->User = $Query->fetch_assoc();
64 $Result = USER_NOT_LOGGED;
65 }
66
67 // Odeber neaktivní uživatele
68 $DbResult = $this->Database->select(array('Table' => $this->TableUserOnline, 'Columns' => 'User', 'Condition' => '`ActivityTime` < DATE_SUB(NOW(), INTERVAL '.USER_TIMEOUT.' SECOND)'));
69 while($DbRow = $DbResult->fetch_assoc())
70 {
71 $this->Database->delete(array('Table' => $this->TableUserOnline, 'Condition' => '`User`='.$DbRow['User']));
72 //$this->System->Modules['Log']->Add('User', 'Logout');
73 }
74 //$this->LoadPermission($this->User['Role']);
75
76 // Role and permission
77 //$this->LoadRoles();
78 }
79
80 function Register($Nick, $Password, $Password2, $Email, $FirstName, $SecondName)
81 {
82 global $Options, $Config;
83
84 if(($Email == '') || ($Nick == '') || ($Password == '') || ($Password2 == '')) $Result = DATA_MISSING;
85 else if($Password != $Password2) $Result = PASSWORDS_UNMATCHED;
86 else
87 {
88 // Je uživatel registrován?
89 $Query = $this->Database->select(array('Table' => $this->TableUser, 'Condition' => 'Name = "'.$Nick.'"'));
90 if($Query->num_rows > 0) $Result = NICK_USED;
91 else
92 {
93 $Query = $this->Database->select(array('Table' => $this->TableUser, 'Condition' => 'Email = "'.$Email.'"'));
94 if($Query->num_rows > 0) $Result = EMAIL_USED;
95 else
96 {
97 $this->Database->insert(array('Table' => $this->TableUser), array('Name' => $Nick, 'FirstName' => $FirstName, 'SecondName' => $SecondName, 'Password' => $Password, 'Email' => $Email, 'RegistrationTime' => 'NOW()', 'Locked' => 1));
98 $UserId = $this->Database->insert_id;
99
100 $Subject = FromUTF8('Registrace nového účtu', 'iso2');
101 $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='.$Password.'">tento odkaz</a>.'."\n<br> \n\n<br><br>Na tento email neodpovídejte.";
102 $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";
103 mail($Email, $Subject, $Message, $AdditionalHeaders);
104 $Result = USER_REGISTRATED;
105 //$this->System->Modules['Log']->NewRecord('User', 'NewRegistration', $Nick);
106 }
107 }
108 }
109 return($Result);
110 }
111
112 function RegisterConfirm($Id, $Hash)
113 {
114 $DbResult = $this->Database->select(array('Table' => $this->TableUser, 'Columns' => 'Id, Name, Password', 'Condition' => 'Id = '.$Id));
115 if($DbResult->num_rows > 0)
116 {
117 $Row = $DbResult->fetch_assoc();
118 if($Hash == $Row['Password'])
119 {
120 $this->Database->update(array('Table' => $this->TableUser, 'Condition' => 'Id='.$Row['Id']), array('Locked' => 0));
121 $Output = USER_REGISTRATION_CONFIRMED;
122 //$this->System->Modules['Log']->NewRecord('User', 'RegisterConfirm', 'UserName='.$Row['Name']);
123 } else $Output = PASSWORDS_UNMATCHED;
124 } else $Output = USER_NOT_FOUND;
125 return($Output);
126 }
127
128 function Login($Nick, $Password)
129 {
130 $SID = session_id();
131 // Je uživatel registrován?
132 $Query = $this->Database->select(array('Table' => $this->TableUser, 'Condition' => 'UserName="'.$Nick.'"'));
133 if($Query->num_rows > 0)
134 {
135 $Row = $Query->fetch_assoc();
136 if($Row['Password'] != $Password) $Result = BAD_PASSWORD;
137 else if($Row['Locked'] == 1) $Result = ACCOUNT_LOCKED;
138 else
139 {
140 $this->Database->update(array('Table' => $this->TableUser, 'Condition' => 'Id='.$Row['Id']), array('LastLoginTime' => 'NOW()'));
141 $this->Database->update(array('Table' => $this->TableUserOnline, 'Condition' => 'SessionId="'.$SID.'"'), array('User' => $Row['Id'], 'Id' => $Row['Id']));
142 // načtení stavu stromu
143 $Result = USER_LOGGED_IN;
144 //$this->System->Modules['Log']->NewRecord('User', 'Login', 'Nick='.$Nick.',Host='.gethostbyaddr(GetRemoteAddress()));
145 }
146 } else $Result = USER_NOT_REGISTRED;
147 $this->Check();
148 return($Result);
149 }
150
151 function Logout()
152 {
153 global $Config;
154
155 $SID = session_id();
156 $this->Database->update(array('Table' => $this->TableUserOnline, 'Condition' => 'SessionId="'.$SID.'"'), array('User' => 0));
157 //$this->System->Modules['Log']->NewRecord('User', 'Logout', $this->User['Name']);
158 $this->Check();
159 return(USER_LOGGED_OUT);
160 }
161
162 function PasswordRecoveryRequest($Name, $Email)
163 {
164 global $Config;
165
166 $DbResult = $this->Database->select(array('Table' => $this->TableUser, 'Columns' => 'Name, Id, Email, Password', 'Condition' => '`Name`="'.$Name.'" AND `Email`="'.$Email.'"'));
167 if($DbResult->num_rows > 0)
168 {
169 $Row = $DbResult->fetch_assoc();
170 $NewPassword = substr(sha1(strtoupper($Row['Name'])), 0, 7);
171
172 $Subject = 'Obnova hesla';
173 $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.";
174 $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";
175 mail($Row['Email'], $Subject, $Message, $AdditionalHeaders);
176 $Output = USER_PASSWORD_RECOVERY_SUCCESS;
177 //$this->System->Modules['Log']->NewRecord('User', 'PasswordRecoveryRequest', 'UserName='.$Name.',Email='.$Email);
178 } else $Output = USER_PASSWORD_RECOVERY_FAIL;
179 return($Output);
180 }
181
182 function PasswordRecoveryConfirm($Id, $Hash, $NewPassword)
183 {
184 $DbResult = $this->Database->select(array('Table' => $this->TableUser, 'Columns' => 'Id, Name, Password', 'Condition' => 'Id = '.$Id));
185 if($DbResult->num_rows > 0)
186 {
187 $Row = $DbResult->fetch_assoc();
188 $NewPassword2 = substr(sha1(strtoupper($Row['Name'])), 0, 7);
189 if(($NewPassword == $NewPassword2) and ($Hash == $Row['Password']))
190 {
191 $this->Database->update(array('Table' => $this->TableUser, 'Condition' => 'Id='.$Row['Id']), array('Password' => $NewPassword, 'Locked' => 0));
192 $Output = USER_PASSWORD_RECOVERY_CONFIRMED;
193 //$this->System->Modules['Log']->NewRecord('User', 'PasswordRecoveryConfirm', 'UserName='.$Row['Name']);
194 } else $Output = PASSWORDS_UNMATCHED;
195 } else $Output = USER_NOT_FOUND;
196 return($Output);
197 }
198}
199
200?>
Note: See TracBrowser for help on using the repository browser.