File: D:/HostingSpaces/SBogers59/ferrumbv.nl/wwwroot/kms/app/models/m_login.class.php
<?php
/**
* m_login.class.php
* Created by Komma Mediadesign.
* Author: mike
* Date: 3/20/13
*/
class Login_Model extends Model
{
public function __construct()
{
parent::__construct();
}
/*
* This method runs the login form
* 1. Checks if a user is found.
* 2. Checks if the password is correct.
* 3. Creates a login session.
*/
public function validate()
{
$user = $_POST['user'];
$pass = $_POST['pass'];
if( ! empty($user) && ! empty($pass))
{
/*
* Set data to get from the database
*/
$fields = array('id','user','hash');
$data = array_fill_keys($fields,'');
/*
* Get data from the database
*/
$this->Db->setData($data);
$this->Db->setTableName('kms_admin');
$this->Db->addRule('user',$user);
$this->Db->setScope(0,1);
$result = $this->Db->select();
/*
* Check if user found
*/
if(count($result) > 0)
{
/*
* Create hash from the entered password, and compare with the one in the database
* If they match its a valid login.
*/
$newHash = crypt($pass, $result['hash']);
if($newHash == $result['hash'])
{
$adminString = $result['id'].'_'.md5($result['hash']);
Session::set('admin_string',$adminString);
Session::set('admin_user',$user);
Session::set('login_time',time());
return true;
}
else
{
$this->errors[] = 'User / Pass incorrect';
}
}
else
{
$this->errors[] = 'User / Pass incorrect';
}
}
else
{
$this->errors[] = 'Fill both fields';
}
return false;
}
public function isLoggedIn()
{
/*
* Check if the string is set
*/
if(Session::get('admin_string'))
{
/*
* Check if login time hasn't expired
*/
if(time() < (Session::get('login_time') + 3600)){
Session::set('login_time',time());
/*
* Set data to get from the database
*/
$fields = array('id','hash');
$data = array_fill_keys($fields,'');
/*
* Get data from the database
*/
$this->Db->setData($data);
$this->Db->setTableName('kms_admin');
$result = $this->Db->select();
$result = $this->Db->twoDimensional($result);
foreach($result as $row)
{
$adminString = $row['id'].'_'.md5($row['hash']);
if($adminString == Session::get('admin_string'))
{
return true;
}
}
}
else
{
$this->logOut();
}
}
return false;
}
public function logOut()
{
Session::destroy('admin_string');
Session::destroy('admin_user');
Session::destroy('login_time');
}
}