source: trunk/www/Module/User/Model.php

Last change on this file was 95, checked in by chronos, 10 years ago
  • Upraveno: Soubory různých logických částí systému odděleny do aplikačních modulů.
  • Property svn:executable set to *
File size: 9.4 KB
Line 
1<?php
2
3include_once(dirname(__FILE__).'/../../Base/Model.php');
4include_once(dirname(__FILE__).'/../../Base/Mail.php');
5include_once(dirname(__FILE__).'/../Log/Model.php');
6
7class User extends Model
8{
9 var $Dependencies = array('Log');
10 var $Data = array();
11 var $DefaultRole = 2;
12 var $OnlineStateTimeout = 600; // in seconds
13
14 function PasswordHash($Name, $Password)
15 {
16 return(sha1(strtoupper($Name.':'.$Password)));
17 }
18
19 function Check()
20 {
21 $SID = session_id();
22
23 // Remove inactive users
24 $DbResult = $this->Database->select('UserOnline', 'Id, User', 'ActivityTime < DATE_SUB(NOW(), INTERVAL '.$this->OnlineStateTimeout.' SECOND)');
25 while($DbRow = $DbResult->fetch_array())
26 {
27 $this->System->Modules['User']->Data['Id'] = $DbRow['User'];
28 if($DbRow['User'] != $this->Config['Web']['UserAnonymousId']) $this->System->Modules['Log']->NewRecord('User', 'Logout');
29 $this->Database->delete('UserOnline', 'Id='.$DbRow['Id']);
30 }
31
32 // Lookup user record
33 $Query = $this->Database->select('UserOnline', '*', 'SessionId="'.$SID.'"');
34 if($Query->num_rows == 0)
35 $this->Database->insert('UserOnline', array('SessionId' => $SID, 'User' => $this->Config['Web']['UserAnonymousId'], 'LoginTime' => 'NOW()', 'ActivityTime' => 'NOW()', 'IpAddress' => $this->System->GetRemoteAddress(), 'HostName' => gethostbyaddr($this->System->GetRemoteAddress()), 'ScriptName' => $_SERVER['PHP_SELF']));
36 //echo($this->Database->LastQuery);
37
38 // Check login
39 $Query = $this->Database->select('UserOnline', '*', 'SessionId="'.$SID.'"');
40 $Row = $Query->fetch_assoc();
41 if($Row['User'] != $this->Config['Web']['UserAnonymousId'])
42 {
43 $Query = $this->Database->select('User', '*', 'Id='.$Row['User']);
44 $this->Data = $Query->fetch_assoc();
45 $Result = $this->System->Translate('UserLogged');
46 } else
47 {
48 $Query = $this->Database->select('User', '*', 'Id='.$this->Config['Web']['UserAnonymousId']);
49 $this->Data = $Query->fetch_assoc();
50 $Result = $this->System->Translate('UserNotLogged');
51 }
52
53 // Refresh time of last access
54 $this->Database->update('UserOnline', 'SessionId="'.$SID.'"', array('ActivityTime' => 'NOW()'));
55 }
56
57 function Register($Login, $Password, $Password2, $Email, $Name)
58 {
59 if(($Email == '') || ($Login == '') || ($Password == '') || ($Password2 == '') || ($Name == '')) $Result = $this->System->Translate('MissingData');
60 else if($Password != $Password2) $Result = $this->System->Translate('PasswordsUnmatched');
61 else
62 {
63 // Je uživatel registrován?
64 $Query = $this->Database->select('User', '*', 'Login = "'.$Login.'"');
65 if($Query->num_rows > 0) $Result = $this->System->Translate('LoginUsed');
66 else
67 {
68 $Query = $this->Database->select('User', '*', 'Name = "'.$Name.'"');
69 if($Query->num_rows > 0) $Result = $this->System->Translate('NameUsed');
70 else
71 {
72 $Query = $this->Database->select('User', '*', 'Email = "'.$Email.'"');
73 if($Query->num_rows > 0) $Result = $this->System->Translate('EmailUsed');
74 else
75 {
76 $this->Database->insert('User', array('Name' => $Name, 'Login' => $Login, 'Password' => $this->PasswordHash($Login, $Password), 'Email' => $Email, 'RegistrationTime' => 'NOW()', 'Locked' => 1));
77 $UserId = $this->Database->insert_id;
78
79 $Mail = new Mail();
80 $Mail->Content = 'Provedli jste registraci nového účtu na serveru <a href="http://'.$this->Config['Web']['Host'].$this->Config['Web']['RootFolder'].'/">http://'.$this->Config['Web']['Host'].$this->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://'.$this->Config['Web']['Host'].$this->Config['Web']['RootFolder'].'/?Action=UserRegisterConfirm&User='.$UserId.'&H='.$this->PasswordHash($Login, $Password).'">http://'.$this->Config['Web']['Host'].$this->Config['Web']['RootFolder'].'/?Action=UserRegisterConfirm&User='.$UserId.'&H='.$this->PasswordHash($Login, $Password).'</a>.'."\n<br> \n\n<br><br>Na tento email neodpovídejte.";
81 $Mail->Subject = 'Registrace nového účtu';
82 $Mail->RecipientName = $Name;
83 $Mail->RecipientAddress = $Email;
84 $Mail->SenderName = $this->Config['Web']['Title'];
85 $Mail->SenderAddress = 'noreplay@zdechov.net';
86 $Mail->Send();
87
88 $Result = $this->System->Translate('UserRegistrated');
89 $this->System->Modules['Log']->NewRecord('User', 'NewRegistration', $Login);
90 }
91 }
92 }
93 }
94 return($Result);
95 }
96
97 function RegisterConfirm($Id, $Hash)
98 {
99 $DbResult = $this->Database->select('User', 'Id, Login, Password', 'Id = '.$Id);
100 if($DbResult->num_rows > 0)
101 {
102 $Row = $DbResult->fetch_array();
103 if($Hash == $Row['Password'])
104 {
105 $this->Database->update('User', 'Id='.$Row['Id'], array('Locked' => 0));
106 $Output = $this->System->Translate('UserRegistrationConfirmed');
107 $this->System->Modules['Log']->NewRecord('User', 'RegisterConfirm', 'Login='.$Row['Login'].', Id='.$Row['Id']);
108 } else $Output = $this->System->Translate('PasswordsUnmatched');
109 } else $Output = $this->System->Translate('UserNotFound');
110 return($Output);
111 }
112
113 function Login($Login, $Password)
114 {
115 $SID = session_id();
116 $Query = $this->Database->select('User', '*', 'Login="'.$Login.'"');
117 if($Query->num_rows > 0)
118 {
119 $Row = $Query->fetch_assoc();
120 if($Row['Password'] != $this->PasswordHash($Login, $Password)) $Result = $this->System->Translate('BadPassword');
121 else if($Row['Locked'] == 1) $Result = $this->System->Translate('AccountLocked');
122 else
123 {
124 $this->Database->update('User', 'Id='.$Row['Id'], array('LastLoginTime' => 'NOW()', 'LastIpAddress' => $this->System->GetRemoteAddress()));
125 $this->Database->update('UserOnline', 'SessionId="'.$SID.'"', array('User' => $Row['Id']));
126 // načtení stavu stromu
127 $Result = $this->System->Translate('UserLoggedIn');
128 $this->System->Modules['Log']->NewRecord('User', 'Login', 'Login='.$Login.',Host='.gethostbyaddr($this->System->GetRemoteAddress()));
129 }
130 } else $Result = $this->System->Translate('UserNotRegistred');
131 $this->Check();
132 return($Result);
133 }
134
135 function Logout()
136 {
137 $SID = session_id();
138 $this->Database->update('UserOnline', 'SessionId="'.$SID.'"', array('User' => $this->Config['Web']['UserAnonymousId']));
139 $this->System->Modules['Log']->NewRecord('User', 'Logout', $this->Data['Login']);
140 $this->Check();
141 return($this->System->Translate('UserLoggedOut'));
142 }
143
144 function PasswordRecoveryRequest($Login, $Email)
145 {
146 $DbResult = $this->Database->select('User', 'Login, Name, Id, Email, Password', '`Login`="'.$Login.'" AND `Email`="'.$Email.'"');
147 if($DbResult->num_rows > 0)
148 {
149 $Row = $DbResult->fetch_assoc();
150 $NewPassword = substr(sha1(strtoupper($Row['Login'])), 0, 7);
151
152 $Mail = new Mail();
153 $Mail->Subject = 'Obnova hesla';
154 $Mail->Content = 'Požádali jste o zaslání nového hesla na serveru <a href="http://'.
155 $this->Config['Web']['Host'].$this->Config['Web']['RootFolder'].'">http://'.
156 $this->Config['Web']['Host'].$this->Config['Web']['RootFolder']."</a>.<br />\n".
157 "Pokud jste tak neučinili, měli by jste tento email ignorovat.<br /><br />\n\nVaše nové heslo k účtu ".
158 $Row['Login']." je: ".$NewPassword."\n<br>Pro aktivaci tohoto hesla klikněte na ".
159 '<a href="http://'.$this->Config['Web']['Host'].$this->Config['Web']['RootFolder'].
160 '/?Action=PasswordRecoveryConfirm&User='.$Row['Id'].'&H='.$Row['Password'].'&P='.
161 $NewPassword.'">tento odkaz</a>.'."\n<br /> Po přihlášení si prosím změňte heslo na nové.\n\n".
162 "<br><br>Na tento email neodpovídejte.";
163 $Mail->RecipientName = $Row['Name'];
164 $Mail->RecipientAddress = $Row['Email'];
165 $Mail->SenderName = $this->Config['Web']['Title'];
166 $Mail->SenderAddress = 'noreplay@zdechov.net';
167 $Mail->Send();
168
169 $Output = $this->System->Translate('UserPasswordRecoverySuccess');
170 $this->System->Modules['Log']->NewRecord('User', 'PasswordRecoveryRequest', 'Login='.$Login.',Email='.$Email);
171 } else $Output = $this->System->Translate('UserPasswordRecoveryFail');
172 return($Output);
173 }
174
175 function PasswordRecoveryConfirm($Id, $Hash, $NewPassword)
176 {
177 $DbResult = $this->Database->select('User', 'Id, Login, Password', 'Id = '.$Id);
178 if($DbResult->num_rows > 0)
179 {
180 $Row = $DbResult->fetch_array();
181 $NewPassword2 = substr(sha1(strtoupper($Row['Login'])), 0, 7);
182 if(($NewPassword == $NewPassword2) and ($Hash == $Row['Password']))
183 {
184 $this->Database->update('User', 'Id='.$Row['Id'], array('Password' => sha1($NewPassword), 'Locked' => 0));
185 $Output = $this->System->Translate('UserPasswordRecoveryConfirmed');
186 $this->System->Modules['Log']->NewRecord('User', 'PasswordRecoveryConfirm', 'Login='.$Row['Login']);
187 } else $Output = $this->System->Translate('UserPasswordUnmatched');
188 } else $Output = $this->System->Translate('UserNotFound');
189 return($Output);
190 }
191}
Note: See TracBrowser for help on using the repository browser.