bludit/kernel/login.class.php

73 lines
1.3 KiB
PHP
Raw Normal View History

2015-03-27 02:00:01 +01:00
<?php defined('BLUDIT') or die('Bludit CMS.');
2015-03-08 18:02:59 +01:00
class Login {
2015-03-27 02:00:01 +01:00
private $dbUsers;
2015-03-08 18:02:59 +01:00
2015-03-27 02:00:01 +01:00
function __construct($dbUsers)
2015-03-08 18:02:59 +01:00
{
2015-03-27 02:00:01 +01:00
$this->dbUsers = $dbUsers;
2015-03-08 18:02:59 +01:00
}
2015-03-27 02:00:01 +01:00
public function setLogin($username)
2015-03-08 18:02:59 +01:00
{
2015-03-27 02:00:01 +01:00
Session::set('username', $username);
Session::set('fingerPrint', $this->fingerPrint());
Session::set('sessionTime', time());
2015-03-08 18:02:59 +01:00
}
2015-03-27 02:00:01 +01:00
public function isLogged()
2015-03-08 18:02:59 +01:00
{
2015-03-27 02:00:01 +01:00
if(Session::get('fingerPrint')===$this->fingerPrint())
2015-03-08 18:02:59 +01:00
{
2015-03-27 02:00:01 +01:00
if(!empty(Session::get('username'))) {
return true;
2015-03-08 18:02:59 +01:00
}
}
return false;
}
2015-03-27 02:00:01 +01:00
public function verifyUser($username, $password)
2015-03-08 18:02:59 +01:00
{
2015-03-27 02:00:01 +01:00
if(empty(trim($username)) || empty(trim($password)))
2015-03-08 18:02:59 +01:00
return false;
2015-03-27 02:00:01 +01:00
$user = $this->dbUsers->get($username);
if($user==false)
2015-03-08 18:02:59 +01:00
return false;
2015-03-27 02:00:01 +01:00
$passwordHash = sha1($password.$user['salt']);
if($passwordHash === $user['password'])
2015-03-08 18:02:59 +01:00
{
2015-03-27 02:00:01 +01:00
$this->setLogin($username);
2015-03-08 18:02:59 +01:00
2015-03-27 02:00:01 +01:00
return true;
2015-03-08 18:02:59 +01:00
}
2015-03-27 02:00:01 +01:00
return false;
2015-03-08 18:02:59 +01:00
}
2015-03-27 02:00:01 +01:00
private function fingerPrint($random=false)
2015-03-08 18:02:59 +01:00
{
// User agent
$agent = getenv('HTTP_USER_AGENT');
if(empty($agent))
2015-03-27 02:00:01 +01:00
$agent = 'Bludit/1.0 (Mr Nibbler Protocol)';
2015-03-08 18:02:59 +01:00
// User IP
if(getenv('HTTP_X_FORWARDED_FOR'))
$ip = getenv('HTTP_X_FORWARDED_FOR');
elseif(getenv('HTTP_CLIENT_IP'))
$ip = getenv('HTTP_CLIENT_IP');
else
$ip = getenv('REMOTE_ADDR');
2015-03-27 02:00:01 +01:00
if($random)
return sha1(mt_rand().$agent.$ip);
2015-03-08 18:02:59 +01:00
return sha1($agent.$ip);
}
2015-03-27 02:00:01 +01:00
}