<?php

/**
 * THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 * @package fusionLib
 * @copyright Copyright (c) 2014 - 2017 fusionLib. All rights reserved.
 * @link http://fusionlib.com
 */

/**
 * flSessionRedis Class
 *
 * Class to implement Redis based sessions.
 *
 * @package fusionLib
 */
class flSessionRedis extends flSession {

	/**
	 * The Redis interface object.
	 *
	 * @var Redis
	 */
	private $cache = null;

	/**
	 * Construct and initialize the Redis session object.
	 *
	 * @param flResponse $response The response object to use.
	 * @param string|Redis $server String defining the server:port of the Redis server e.g. localhost:6379 or a Redis object to use.
	 * @param flDb $db The database object.
	 * @param int $life The time in seconds a session should live for, must be greater than 0 or null to use the value of session.gc_maxlifetime.
	 * @param bool $expireCookieOnExit true to expire cookie at the end of the session or false to keep the cookie for the lifetime of the session data.
	 * @param string $cookieName The name of the cookie holding session data.
	 * @param string $cookieDomain The optional domain the cookie is available on, e.g. for all subdomains '.example.com'
	 * @param string $cookiePath The options path the cookie is valid on
	 * @param bool $cookieSecure true to use secure session cookies; else false.
	 * @throws flSessionException
	 */
	function __construct($response, $server, $db, $life = null, $expireCookieOnExit = true, $cookieName = '__sessionid', $cookieDomain = '', $cookiePath = '/', $cookieSecure = false) {
		if($server instanceof Redis)
			$this->cache = $server;
		else {
			// Test if Redis drivers present
			if(extension_loaded('redis'))
				$this->cache = new Redis();
			else
				throw new flSessionException('The Redis extension required by flSessionRedis is not available.');

			// Connect to servers
			$d = explode(':', $server);
			if(!isset($d[1])) $d[1] = 6379;
			if(!$this->cache->connect($d[0], isset($d[1]) ? $d[1] : 6379))
				throw new flSessionException('flSessionRedis failed to connect to the Redis server.');
		}

		parent::__construct($response, $life, $expireCookieOnExit, $cookieName, $cookieDomain, $cookiePath, $cookieSecure);
	}

	/**
	 * Get the inner interface object.
	 *
	 * @return Redis The inner interface object.
	 */
	function _getInnerObject() {
		return $this->cache;
	}

	/**
	 * @inheritdoc
	 */
	public function read($session_id) {
		$r = $this->cache->get('flSession.' . $session_id);
		return $r ? $r : '';
	}

	/**
	 * @inheritdoc
	 */
	public function write($session_id, $session_data) {
		$this->cache->setex('flSession.' . $session_id, $this->expireat, $session_data);
	}

	/**
	 * @inheritdoc
	 */
	public function destroy($session_id) {
		$this->cache->delete('flSession.' . $session_id);
	}

	/**
	 * @inheritdoc
	 */
	public function gc($maxlifetime) {
		// Do nothing as redis will self clean
	}
}
