source: branches/2/user.php@ 42

Last change on this file since 42 was 42, checked in by george, 16 years ago
  • Opraveno: Sledování odkazovaných adres na bannery.
  • Property svn:executable set to *
File size: 5.1 KB
Line 
1<?php
2
3define('NICK_USED', 'Přezdívka použita!');
4define('USER_REGISTRATED', 'Uživatel zaregistrován.');
5define('DATA_MISSING', 'Chybí emailová adresa, přezdívka, nebo některé z hesel!');
6define('PASSWORDS_UNMATCHED', 'Hesla si neodpovídají!');
7define('USER_NOT_LOGGED', 'Nejste přihlášen.');
8define('USER_LOGGED', 'Uživatel přihlášen.');
9define('USER_NOT_REGISTRED', 'Uživatel neregistrován.');
10define('USER_ALREADY_LOGGED', 'Uživatel již přihlášen.');
11define('USER_LOGGED_IN', 'Byl jste přihlášen.');
12define('USER_LOGGED_OUT', 'Byl jste odhlášen.');
13define('BAD_PASSWORD', 'Špatné heslo.');
14define('USER_TIMEOUT', 300); // in seconds
15
16class User extends Module
17{
18 var $Roles = array();
19 var $User = array();
20 var $DefaultRole = 2;
21 var $AnonymousUserId = 315;
22
23 function Check()
24 {
25 $SID = session_id();
26
27 // Lookup user record
28 $Query = $this->Database->select('UserOnline', '*', 'SessionId="'.$SID.'"');
29 if($Query->num_rows > 0)
30 {
31 // Refresh time of last access
32 $this->Database->update('UserOnline', 'SessionId="'.$SID.'"', array('Time' => 'NOW()'));
33 } else $this->Database->insert('UserOnline', array('SessionId' => $SID, 'User' => $this->AnonymousUserId, 'Time' => 'NOW()', 'HostAddress' => (gethostbyaddr(GetRemoteAddress()).' '.GetRemoteAddress())));
34
35 // Odeber neaktivní uživatele
36 $this->Database->delete('UserOnline', 'Time < DATE_SUB(NOW(), INTERVAL '.USER_TIMEOUT.' SECOND)');
37
38 // Zkontroluj přihlášení
39 $DbResult = $this->Database->select('UserOnline', 'User', 'SessionId="'.$SID.'"');
40 $Row = $DbResult->fetch_assoc();
41 if($Row['User'] != $this->AnonymousUserId)
42 {
43 $Query = $this->Database->select('User', '*', "Id=".$Row['User']."");
44 $this->User = $Query->fetch_array();
45 $Result = USER_LOGGED;
46 } else {
47 $Query = $this->Database->select('User', '*', "Id=".$this->AnonymousUserId);
48 $this->User = $Query->fetch_array();
49 $Result = USER_NOT_LOGGED;
50 }
51 $this->LoadPermission($this->User['Role']);
52
53 // Role and permission
54 $this->LoadRoles();
55
56 }
57
58 function Register($Nick, $Password, $Password2, $Email, $FullName)
59 {
60 global $Options;
61 if(($Email == '') || ($Nick == '') || ($Password == '') || ($Password2 == '')) $Result = DATA_MISSING;
62 else if($Password != $Password2) $Result = PASSWORDS_UNMATCHED;
63 else
64 {
65 // Je uživatel registrován?
66 $Query = $this->Database->select('User', '*', 'Name = "'.$Nick.'"');
67 if($Query->num_rows() > 0) $Result = NICK_USED;
68 else
69 {
70 $this->Database->insert('User', array('Name' => addslashes($Nick), 'FullName' => addslashes($FullName), 'Password' => addslashes($Password), 'Email' => htmlspecialchars($Email), 'Role' => $this->DefaultRole));
71 $Result = USER_REGISTRATED;
72 }
73 }
74 return($Result);
75 }
76
77 function Login($Nick, $Password)
78 {
79 $SID = session_id();
80 // Je uživatel registrován?
81 $Query = $this->Database->select('User', '*', 'Name="'.$Nick.'"');
82 if($Query->num_rows > 0)
83 {
84 $Row = $Query->fetch_array();
85 if($Row['Password'] != $Password) $Result = BAD_PASSWORD;
86 else
87 {
88 $this->Database->update('User', 'Id='.$Row['Id'], array('LastLoginTime' => 'NOW()'));
89 $this->Database->update('UserOnline', 'SessionId="'.$SID.'"', array('User' => $Row['Id']));
90 // načtení stavu stromu
91 $Result = USER_LOGGED_IN;
92 }
93 } else $Result = USER_NOT_REGISTRED;
94 return($Result);
95 }
96
97 function Logout()
98 {
99 $SID = session_id();
100 $this->Database->update('UserOnline', 'SessionId="'.$SID.'"', array('User' => $this->AnonymousUserId));
101 return(USER_LOGGED_OUT);
102 }
103
104 function LoadRoles()
105 {
106 $this->Roles = array();
107 $DbResult = $this->Database->select('UserRole', '*');
108 while($DbRow = $DbResult->fetch_array())
109 $this->Roles[] = $DbRow;
110 }
111
112 function LoadPermission($Role)
113 {
114 $this->User['Permission'] = array();
115 $DbResult = $this->Database->query('SELECT `UserRolePermission`.*, `PermissionOperation`.`Description` FROM `UserRolePermission` JOIN `PermissionOperation` ON `PermissionOperation`.`Id` = `UserRolePermission`.`Operation` WHERE `UserRolePermission`.`Role` = '.$Role);
116 if($DbResult->num_rows > 0)
117 while($DbRow = $DbResult->fetch_array())
118 $this->User['Permission'][$DbRow['Operation']] = $DbRow;
119 }
120
121 function PermissionMatrix()
122 {
123 $Result = array();
124 $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`');
125 while($DbRow = $DbResult->fetch_array())
126 {
127 $Value = '';
128 if($DbRow['Read']) $Value .= 'R';
129 if($DbRow['Write']) $Value .= 'W';
130 $Result[$DbRow['Description']][$DbRow['Title']] = $Value;
131 }
132 return($Result);
133 }
134}
135
136?>
Note: See TracBrowser for help on using the repository browser.