<?php
require_once $_SERVER['DOCUMENT_ROOT']."/".FOLDER."/classes/database.class.php";
require_once $_SERVER['DOCUMENT_ROOT']."/".FOLDER."/library/session.lib.php";
require_once $_SERVER['DOCUMENT_ROOT']."/".FOLDER."/library/cookie.lib.php";
if (!session_id())
session_start();
class Authentication {
var $db;
function Authentication($db) {
$this->db = $db;
}
function login($name, $password, $remember) {
$sql = "
select
id
from user
where username = ".$this->db->quote($name)."
and password = ".$this->db->quote(md5($password));
$result = $this->db->getResult($sql);
$row = $this->db->getRow($result);
if (!$row)
return FALSE;
session_set("user_id", $row["id"]);
if ($remember == "yes") {
cookie_set("name", $name);
cookie_set("password", md5($password));
}
return TRUE;
}
function isLogged() {
$user_id = session_get("user_id");
if (util_empty($user_id)) {
$name = cookie_get("name");
$password = cookie_get("password");
$sql = "
select
id
from user
where username = ".$this->db->quote($name)."
and password = ".$this->db->quote($password);
$result = $this->db->getResult($sql);
$row = $this->db->getRow($result);
if (!$row)
return FALSE;
session_set("user_id", $row["id"]);
} else {
$sql = "
select
null
from user
where id = ".$this->db->quote($user_id);
$result = $this->db->getResult($sql);
$row = $this->db->getRow($result);
if (!$row)
return FALSE;
}
return TRUE;
}
function logout() {
session_remove("user_id");
cookie_remove("name");
cookie_remove("password");
}
function getUserId() {
return session_get("user_id");
}
}
?>