<?php
namespace gnomephp\security;
use \gnomephp\doctrine\Doctrine;
/**
* @MappedSuperclass
*/
class SecuritySession{
/**
* The sesion salt key.
* @var string Session salt key.
*/
const SALT = '___%%2(/"#¤(#JsdjWIcas___';
/**
* Expiery time of the session.
* @var int Expiery time in seconds.
*/
const EXPIRE_TIME = 12000;
/**
* @Id @Column(type="integer") @GeneratedValue
*/
protected $id;
/**
* @Column(type="string", length=40)
*/
protected $sessionKey;
/**
* @Column(type="string", length=255)
*/
protected $username;
/**
* @Column(type="datetime")
*/
protected $dateLogin;
/**
* @Column(type="datetime")
*/
protected $dateExpire;
public function __construct($username){
$this->dateLogin = new \DateTime("now");
$this->updateExpire();
$this->setUsername($username);
$this->setSessionKey();
}
static public function getValidSession($session, $ns){
$query = Doctrine::getEM()->createQuery("SELECT s FROM $ns s WHERE s.sessionKey = '".$session."'");
return $query->getOneOrNullResult();
}
static public function deleteSession($session, $ns){
$query = Doctrine::getEM()->createQuery("DELETE FROM $ns s WHERE s.sessionKey = '".$session."'");
return $query->execute();
}
static public function cleanupSessions($ns){
$query = Doctrine::getEM()->createQuery("DELETE FROM $ns s WHERE CURRENT_TIMESTAMP() > s.dateExpire");
return $query->execute();
}
public function updateExpire(){
$d = new \DateTime();
$d->setTimestamp(time() + SecuritySession::EXPIRE_TIME);
$this->dateExpire = $d;
}
public function getId(){
return $this->id;
}
public function getSessionKey(){
return $this->sessionKey;
}
protected function setSessionKey(){
$this->sessionKey = sha1($this->username . SecuritySession::SALT . microtime(true));
}
public function setUsername($username){
$this->username = $username;
}
public function getUsername(){
return $this->username;
}
}