Database->select('UserOnline', 'Id, User', 'ActivityTime < DATE_SUB(NOW(), INTERVAL '.$this->OnlineStateTimeout.' SECOND)');
while($DbRow = $DbResult->fetch_array())
{
$this->System->Modules['User']->Data['Id'] = $DbRow['User'];
if($DbRow['User'] != $this->AnonymousUserId) $this->System->Modules['Log']->NewRecord('User', 'Logout');
$this->Database->delete('UserOnline', 'Id='.$DbRow['Id']);
}
// Lookup user record
$Query = $this->Database->select('UserOnline', '*', 'SessionId="'.$SID.'"');
if($Query->num_rows == 0)
$this->Database->insert('UserOnline', array('SessionId' => $SID, 'User' => $this->AnonymousUserId, 'LoginTime' => 'NOW()', 'ActivityTime' => 'NOW()', 'IpAddress' => GetRemoteAddress(), 'HostName' => gethostbyaddr(GetRemoteAddress()), 'ScriptName' => $_SERVER['PHP_SELF']));
//echo($this->Database->LastQuery);
// Check login
$Query = $this->Database->select('UserOnline', '*', 'SessionId="'.$SID.'"');
$Row = $Query->fetch_assoc();
if($Row['User'] != $this->AnonymousUserId)
{
$Query = $this->Database->select('User', '*', 'Id='.$Row['User']);
$this->Data = $Query->fetch_assoc();
$Result = $this->System->Translate('UserLogged');
} else
{
$Query = $this->Database->select('User', '*', 'Id='.$this->AnonymousUserId);
$this->Data = $Query->fetch_assoc();
$Result = $this->System->Translate('UserNotLogged');
}
// Refresh time of last access
$this->Database->update('UserOnline', 'SessionId="'.$SID.'"', array('ActivityTime' => 'NOW()'));
//$this->LoadPermission($this->Data['Role']);
// Role and permission
//$this->LoadRoles();
}
function Register($Login, $Password, $Password2, $Email, $Name)
{
global $Options, $Config;
if(($Email == '') || ($Login == '') || ($Password == '') || ($Password2 == '') || ($Name == '')) $Result = $this->System->Translate('MissingData');
else if($Password != $Password2) $Result = $this->System->Translate('PasswordsUnmatched');
else
{
// Je uživatel registrován?
$Query = $this->Database->select('User', '*', 'Login = "'.$Login.'"');
if($Query->num_rows > 0) $Result = $this->System->Translate('LoginUsed');
else
{
$Query = $this->Database->select('User', '*', 'Name = "'.$Name.'"');
if($Query->num_rows > 0) $Result = $this->System->Translate('NameUsed');
else
{
$Query = $this->Database->select('User', '*', 'Email = "'.$Email.'"');
if($Query->num_rows > 0) $Result = $this->System->Translate('EmailUsed');
else
{
$this->Database->insert('User', array('Name' => $Name, 'Login' => $Login, 'Password' => $this->PasswordHash($Login, $Password), 'Email' => $Email, 'RegistrationTime' => 'NOW()', 'Locked' => 1, 'Role' => 2));
$UserId = $this->Database->insert_id;
$Subject = FromUTF8('Registrace nového účtu', 'iso2');
$Message = 'Provedli jste registraci nového účtu na serveru http://'.$Config['System']['Host'].$Config['System']['RootFolder']."/.
\nPokud jste tak neučinili, měli by jste tento email ignorovat.
\n\nVáš účet je: ".$Login."\n
Pro dokončení registrace klikněte na tento odkaz: ".'http://'.$Config['System']['Host'].$Config['System']['RootFolder'].'/?Action=UserRegisterConfirm&User='.$UserId.'&H='.$this->PasswordHash($Login, $Password).'.'."\n
\n\n
Na tento email neodpovídejte.";
$AdditionalHeaders = "To: ".$Name." <".$Email.">\n"."From: ".FromUTF8($Config['System']['Title'], 'iso2')." \n"."MIME-Version: 1.0\n"."Content-type: text/html; charset=utf-8";
mail($Email, $Subject, $Message, $AdditionalHeaders);
$Result = $this->System->Translate('UserRegistrated');
$this->System->Modules['Log']->NewRecord('User', 'NewRegistration', $Login);
}
}
}
}
return($Result);
}
function RegisterConfirm($Id, $Hash)
{
$DbResult = $this->Database->select('User', 'Id, Login, Password', 'Id = '.$Id);
if($DbResult->num_rows > 0)
{
$Row = $DbResult->fetch_array();
if($Hash == $Row['Password'])
{
$this->Database->update('User', 'Id='.$Row['Id'], array('Locked' => 0));
$Output = $this->System->Translate('UserRegistrationConfirmed');
$this->System->Modules['Log']->NewRecord('User', 'RegisterConfirm', 'Login='.$Row['Login'].', Id='.$Row['Id']);
} else $Output = $this->System->Translate('PasswordsUnmatched');
} else $Output = $this->System->Translate('UserNotFound');
return($Output);
}
function Login($Login, $Password)
{
$SID = session_id();
$Query = $this->Database->select('User', '*', 'Login="'.$Login.'"');
if($Query->num_rows > 0)
{
$Row = $Query->fetch_assoc();
if($Row['Password'] != $this->PasswordHash($Login, $Password)) $Result = $this->System->Translate('BadPassword');
else if($Row['Locked'] == 1) $Result = $this->System->Translate('AccountLocked');
else
{
$this->Database->update('User', 'Id='.$Row['Id'], array('LastLoginTime' => 'NOW()', 'LastIpAddress' => GetRemoteAddress()));
$this->Database->update('UserOnline', 'SessionId="'.$SID.'"', array('User' => $Row['Id']));
// načtení stavu stromu
$Result = $this->System->Translate('UserLoggedIn');
$this->System->Modules['Log']->NewRecord('User', 'Login', 'Login='.$Login.',Host='.gethostbyaddr(GetRemoteAddress()));
}
} else $Result = $this->System->Translate('UserNotRegistred');
$this->Check();
return($Result);
}
function Logout()
{
$SID = session_id();
$this->Database->update('UserOnline', 'SessionId="'.$SID.'"', array('User' => $this->AnonymousUserId));
$this->System->Modules['Log']->NewRecord('User', 'Logout', $this->Data['Login']);
$this->Check();
return($this->System->Translate('UserLoggedOut'));
}
function LoadRoles()
{
$this->Roles = array();
$DbResult = $this->Database->select('UserRole', '*');
while($DbRow = $DbResult->fetch_array())
$this->Roles[] = $DbRow;
}
function LoadPermission($Role)
{
$this->Data['Permission'] = array();
$DbResult = $this->Database->query('SELECT `UserRolePermission`.*, `PermissionOperation`.`Description` FROM `UserRolePermission` JOIN `PermissionOperation` ON `PermissionOperation`.`Id` = `UserRolePermission`.`Operation` WHERE `UserRolePermission`.`Role` = '.$Role);
if($DbResult->num_rows > 0)
while($DbRow = $DbResult->fetch_array())
$this->Data['Permission'][$DbRow['Operation']] = $DbRow;
}
function PermissionMatrix()
{
$Result = array();
$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`');
while($DbRow = $DbResult->fetch_array())
{
$Value = '';
if($DbRow['Read']) $Value .= 'R';
if($DbRow['Write']) $Value .= 'W';
$Result[$DbRow['Description']][$DbRow['Title']] = $Value;
}
return($Result);
}
function CheckGroupPermission($GroupId, $OperationId)
{
// Check group-group relation
$DbResult = $this->Database->select('PermissionGroupAssignment', '*', '`Group`="'.$GroupId.'" AND `Type`="Group"');
while($DbRow = $DbResult->fetch_array())
{
if($this->CheckGroupPermission($DbRow['GroupOrOperation'], $OperationId) == true) return(true);
}
// Check group-operation relation
$DbResult = $this->Database->select('PermissionGroupAssignment', '*', '`Group`="'.$GroupId.'" AND `GroupOrOperation`="'.$OperationId.'" AND `Type`="Operation"');
if($DbResult->num_rows > 0) return(true);
return(false);
}
function CheckPermission($Module, $Operation, $ItemType = '', $ItemIndex = 0)
{
$DbResult = $this->Database->select('PermissionOperation', 'Id', '`Module`="'.$Module.'" AND `Item`="'.$ItemType.'" AND `ItemId`='.$ItemIndex.' AND `Operation`="'.$Operation.'"');
if($DbResult->num_rows > 0)
{
$DbRow = $DbResult->fetch_array();
$OperationId = $DbRow['Id'];
// Check user-operation relation
$DbResult = $this->Database->select('PermissionUserAssignment', '*', '`User`="'.$this->Data['Id'].'" AND `GroupOrOperation`="'.$OperationId.'" AND `Type`="Operation"');
if($DbResult->num_rows > 0) return(true);
// Check user-group relation
$DbResult = $this->Database->select('PermissionUserAssignment', 'GroupOrOperation', '`User`="'.$this->Data['Id'].'" AND `Type`="Group"');
while($DbRow = $DbResult->fetch_array())
{
if($this->CheckGroupPermission($DbRow['GroupOrOperation'], $OperationId) == true) return(true);
}
return(false);
} else return(false);
}
function PasswordRecoveryRequest($Login, $Email)
{
global $Config;
$DbResult = $this->Database->select('User', 'Login, Name, Id, Email, Password', '`Login`="'.$Login.'" AND `Email`="'.$Email.'"');
if($DbResult->num_rows > 0)
{
$Row = $DbResult->fetch_assoc();
$NewPassword = substr(sha1(strtoupper($Row['Login'])), 0, 7);
$Subject = 'Obnova hesla';
$Message = 'Požádali jste o zaslání nového hesla na serveru http://'.$Config['System']['Host'].$Config['System']['RootFolder'].".
\nPokud jste tak neučinili, měli by jste tento email ignorovat.
\n\nVaše nové heslo k účtu ".$Row['Login']." je: ".$NewPassword."\n
Pro aktivaci tohoto hesla klikněte na ".'tento odkaz.'."\n
Po přihlášení si prosím změňte heslo na nové.\n\n
Na tento email neodpovídejte.";
$AdditionalHeaders = "To: ".$Row['Name']." <".$Row['Email'].">\n"."From: ".FromUTF8($Config['System']['Title'], 'iso2')." \n"."MIME-Version: 1.0\n"."Content-type: text/html; charset=utf-8";
mail($Row['Email'], $Subject, $Message, $AdditionalHeaders);
$Output = $this->System->Translate('UserPasswordRecoverySuccess');
$this->System->Modules['Log']->NewRecord('User', 'PasswordRecoveryRequest', 'Login='.$Login.',Email='.$Email);
} else $Output = $this->System->Translate('UserPasswordRecoveryFail');
return($Output);
}
function PasswordRecoveryConfirm($Id, $Hash, $NewPassword)
{
$DbResult = $this->Database->select('User', 'Id, Login, Password', 'Id = '.$Id);
if($DbResult->num_rows > 0)
{
$Row = $DbResult->fetch_array();
$NewPassword2 = substr(sha1(strtoupper($Row['Login'])), 0, 7);
if(($NewPassword == $NewPassword2) and ($Hash == $Row['Password']))
{
$this->Database->update('User', 'Id='.$Row['Id'], array('Password' => sha1($NewPassword), 'Locked' => 0));
$Output = $this->System->Translate('UserPasswordRecoveryConfirmed');
$this->System->Modules['Log']->NewRecord('User', 'PasswordRecoveryConfirm', 'Login='.$Row['Login']);
} else $Output = $this->System->Translate('UserPasswordUnmatched');
} else $Output = $this->System->Translate('UserNotFound');
return($Output);
}
function ServerCount()
{
$DbResult = $this->Database->query('SELECT COUNT(*) FROM Server WHERE User='.$this->Data['Id']);
$DbRow = $DbResult->fetch_row();
return($DbRow[0]);
}
function RealmCount()
{
$Total = 0;
$DbResult = $this->Database->query('SELECT Id FROM Server WHERE User='.$this->User['Id']);
while($DbRow = $DbResult->fetch_assoc())
{
$Server = new Server($this->Database, $DbRow['Id']);
$Total += $Server->RealmCount();
}
return($Total);
}
}