95 lines
2.2 KiB
PHP
95 lines
2.2 KiB
PHP
<?php
|
|
|
|
class User
|
|
{
|
|
##############################################################################
|
|
# PRIVATE DATA
|
|
private $password, $username, $loggedin, $role;
|
|
private $user_data;
|
|
##############################################################################
|
|
# PUBLIC FUNCTIONS
|
|
public function __construct($site_path)
|
|
{
|
|
$this->user_data = simplexml_load_file($site_path.'data/users/users.xml');
|
|
$this->username = $_SESSION['username'];
|
|
$this->loggedin = $_SESSION['_loggedin'];
|
|
$this->role = $_SESSION['userrole'];
|
|
}
|
|
public function __destruct()
|
|
{
|
|
# save the session data when the object goes out of context
|
|
$_SESSION['username'] = $this->username;
|
|
$_SESSION['_loggedin'] = $this->loggedin;
|
|
$_SESSION['userrole'] = $this->role;
|
|
}
|
|
# see if a user is logged in
|
|
public function user_exists()
|
|
{
|
|
return $this->loggedin;
|
|
}
|
|
# check if the user has a role equal to what is passed
|
|
public function check_role($role_to_check)
|
|
{
|
|
$role_to_check = strtolower($role_to_check);
|
|
if(($role_to_check == $this->role) && $this->loggedin)
|
|
return true;
|
|
else
|
|
return false;
|
|
}
|
|
public function login($username,$password)
|
|
{
|
|
foreach($this->user_data->user as $user)
|
|
{
|
|
if($username == (string)$user->username)
|
|
{
|
|
$hash = sha1($password);
|
|
#print $hash;
|
|
if($hash == (string)$user->password)
|
|
{
|
|
$this->username = $username;
|
|
$this->password = $hash;
|
|
$this->loggedin = true;
|
|
$this->role = (string)$user->role;
|
|
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
# log the user out
|
|
public function logout()
|
|
{
|
|
# delete cookie
|
|
setcookie('PHPSESSID','',time()-3600);
|
|
# delete the session entirely
|
|
session_unset();
|
|
|
|
# reset object vars
|
|
$this->loggedin = false;
|
|
$this->username = NULL;
|
|
$this->password = NULL;
|
|
$this->role = NULL;
|
|
|
|
return true;
|
|
}
|
|
##############################################################################
|
|
# SETTERS/GETTERS
|
|
public function username($new_username = NULL)
|
|
{
|
|
if(!is_null($new_username))
|
|
$this->username = $new_username;
|
|
else
|
|
return $this->username;
|
|
}
|
|
public function role()
|
|
{
|
|
return $this->role;
|
|
}
|
|
##############################################################################
|
|
# PRIVATE FUNCTIONS
|
|
|
|
}
|
|
|
|
?>
|