Skip to content

Instantly share code, notes, and snippets.

@withremote
Created January 8, 2015 15:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save withremote/99c0ad36999da43fb127 to your computer and use it in GitHub Desktop.
Save withremote/99c0ad36999da43fb127 to your computer and use it in GitHub Desktop.
CI3 flat file session driver used for testing with Codeception
<?php
/**
* CI3 flat file session driver used for testing with Codeception
*/
/**
* Class CI_Session_codeception
*/
class CI_Session_codeception extends CI_Session_driver
{
/**
* Length of time (in seconds) for sessions to expire
*
* @var int
*/
public $sess_expiration = 7200;
/**
* Interval at which to update session
*
* @var int
*/
public $sess_time_to_update = 300;
/**
* @var
*/
public $userdata;
/**
* Current time
*
* @var int
*/
public $now;
/**
* @var string
*/
protected $sessionStoreDirectory = '';
/**
* @var string
*/
protected $uId = '';
/**
* @var string
*/
protected $sessFileExt = '_session.json';
/**
* Save a session
*/
public function sess_save()
{
// Save current data to storage
$this->userdata['last_updated'] = $this->now;
file_put_contents($this->sessFilePath(), json_encode($this->userdata));
}
/**
* Session file path
* @return string
*/
private function sessFilePath()
{
return $this->sessionStoreDirectory . DIRECTORY_SEPARATOR . $this->uId . $this->sessFileExt;
}
/**
* @param bool $destroy
*/
public function sess_regenerate($destroy = FALSE)
{
if ($destroy) {
$this->sess_destroy();
$this->sessCreate();
} else {
$this->sessUpdate(true);
}
}
/**
* Destroy flat file session
*/
public function sess_destroy()
{
unlink($this->sessFilePath());
$this->userdata = array();
}
/**
* Create session
*/
private function sessCreate()
{
// Initialize userdata
$this->userdata = array(
'session_id' => $this->generateUid(),
'ip_address' => $this->CI->input->ip_address(),
'user_agent' => trim(substr($this->CI->input->user_agent(), 0, 120)),
'last_activity' => $this->now,
);
return file_put_contents($this->sessFilePath(), json_encode($this->userdata));
}
/**
* User ID
* @return string
*/
private function generateUid()
{
return md5($_SERVER['SSH_CLIENT'] . $_SERVER['SSH_CONNECTION'] . implode('-', $_SERVER['argv']));
}
/**
* @param bool $force
* @return bool|int|null
*/
private function sessUpdate($force = false)
{
// We only update the session every five minutes by default (unless forced)
if (!$force && ($this->userdata['last_activity'] + $this->sess_time_to_update) >= $this->now) {
return null;
}
$this->userdata = json_decode($this->sessFilePath(), true);
$this->userdata['last_activity'] = $this->now;
return file_put_contents($this->sessFilePath(), json_encode($this->userdata));
}
/**
*
*/
public function &get_userdata()
{
return $this->userdata;
}
/**
*
*/
protected function initialize()
{
$this->sessionStoreDirectory = dirname(dirname(APPPATH)) . DIRECTORY_SEPARATOR . 'session_store';
if (!file_exists($this->sessionStoreDirectory)) {
mkdir($this->sessionStoreDirectory);
}
$this->uid = $this->generateUid();
$this->now = time();
if (!$this->sessRead()) {
$this->sessCreate();
} else {
$this->sessUpdate();
}
}
/**
* Read session from flat file if it exists
*/
protected function sessRead()
{
$filePath = $this->sessFilePath();
$session = file_exists($filePath);
if (!$session) {
return false;
}
$sessionRead = json_decode(file_get_contents($filePath), true);
// Is the session data we unserialized an array with the correct format?
if (!is_array($sessionRead) OR !isset($sessionRead['session_id'], $sessionRead['ip_address'], $sessionRead['user_agent'], $sessionRead['last_activity'])) {
$this->sess_destroy();
return FALSE;
}
// Is the session current?
if (($sessionRead['last_activity'] + $this->sess_expiration) < $this->now OR $session['last_activity'] > $this->now) {
$this->sess_destroy();
return FALSE;
}
// Session is valid!
$this->userdata = $session;
return TRUE;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment