<commit_before><?php
/**
 * Fuel
 *
 * Fuel is a fast, lightweight, community driven PHP5 framework.
 *
 * @package		Fuel
 * @version		1.0
 * @author		Harro "WanWizard" Verton
 * @license		MIT License
 * @copyright	2010 <NAME>
 * @link		http://fuelphp.com
 */

namespace Fuel\Core;



abstract class Session_Driver {

	/*
	 * @var	session class configuration
	 */
	protected $config = array();

	/*
	 * @var	session indentification keys
	 */
	protected $keys = array();

	/*
	 * @var	session variable data
	 */
	protected $data = array();

	/*
	 * @var	session flash data
	 */
	protected $flash = array();

	/*
	 * @var	session time object
	 */
	protected $time = null;

	// --------------------------------------------------------------------
	// abstract methods
	// --------------------------------------------------------------------

	/**
	 * create a new session
	 *
	 * @access	public
	 * @return	void
	 */
	abstract function create();

	// --------------------------------------------------------------------

	/**
	 * destroy the current session
	 *
	 * @access	public
	 * @return	void
	 */
	abstract function destroy();

	// --------------------------------------------------------------------
	// generic driver methods
	// --------------------------------------------------------------------

	/**
	 * read the session
	 *
	 * @access	public
	 * @return	void
	 */
	public function read()
	{
		// auto expire flash variables if needed
		if ($this->config['flash_auto_expire'] === true)
		{
			foreach($this->flash as $key => $value)
			{
				$this->flash[$key]['state'] = 'old';
			}
		}
	}

	// --------------------------------------------------------------------

	/**
	 * write the session
	 *
	 * @access	public
	 * @return	void
	 */
	public function write()
	{
		$this->_cleanup_flash();
	}

	// --------------------------------------------------------------------

	/**
	 * generic driver initialisation
	 *
	 * @access	public
	 * @return	void
	 */
	public function init()
	{
		// get a time object
		$this->time = \Date::time();
	}

	// --------------------------------------------------------------------

	/**
	 * set session variables
	 *
	 * @param	string	name of the variable to set
	 * @param	mixed	value
	 * @access	public
	 * @return	void
	 */
	public function set($name, $value)
	{
		$this->data[$name] = $value;

		// need to auto-update the session?
		if ($this->config['write_on_set'] === true)
		{
			$this->write();
		}
	}

	// --------------------------------------------------------------------

	/**
	 * get session variables
	 *
	 * @access	public
	 * @param	string	name of the variable to get
	 * @param	mixed	default value to return if the variable does not exist
	 * @return	mixed
	 */
	public function get($name, $default = null)
	{
		if (is_null($name))
		{
			return $this->data;
		}
		elseif (isset($this->data[$name]))
		{
			return $this->data[$name];
		}

		if (strpos($name, '.') !== false)
		{
			$parts = explode('.', $name);

			switch (count($parts))
			{
				case 2:
					if (isset($this->data[$parts[0]][$parts[1]]))
					{
						return $this->data[$parts[0]][$parts[1]];
					}
				break;

				case 3:
					if (isset($this->data[$parts[0]][$parts[1]][$parts[2]]))
					{
						return $this->data[$parts[0]][$parts[1]][$parts[2]];
					}
				break;

				case 4:
					if (isset($this->data[$parts[0]][$parts[1]][$parts[2]][$parts[3]]))
					{
						return $this->data[$parts[0]][$parts[1]][$parts[2]][$parts[3]];
					}
				break;

				default:
					$return = false;
					foreach ($parts as $part)
					{
						if ($return === false and isset($this->data[$part]))
						{
							$return = $this->data[$part];
						}
						elseif (isset($return[$part]))
						{
							$return = $return[$part];
						}
						else
						{
							return $default;
						}
					}
					return $return;
				break;
			}
		}

		return $default;
	}

	// --------------------------------------------------------------------

	/**
	 * get session key variables
	 *
	 * @access	public
	 * @param	string	name of the variable to get, default is 'session_id'
	 * @return	mixed	contents of the requested variable, or false if not found
	 */
	public function key($name)
	{
		return isset($this->keys[$name]) ? $this->keys[$name] : false;
	}

	// --------------------------------------------------------------------

	/**
	 * delete session variables
	 *
	 * @param	string	name of the variable to delete
	 * @param	mixed	value
	 * @access	public
	 * @return	void
	 */
	public function delete($name)
	{
		if (isset($this->data[$name]))
		{
			unset($this->data[$name]);
		}

		if (strpos($name, '.') !== false)
		{
			$parts = explode('.', $name);

			switch (count($parts))
			{
				case 2:
					if (isset($this->data[$parts[0]][$parts[1]]))
					{
						unset($this->data[$parts[0]][$parts[1]]);
					}
				break;

				case 3:
					if (isset($this->data[$parts[0]][$parts[1]][$parts[2]]))
					{
						unset($this->data[$parts[0]][$parts[1]][$parts[2]]);
					}
				break;

				case 4:
					if (isset($this->data[$parts[0]][$parts[1]][$parts[2]][$parts[3]]))
					{
						unset($this->data[$parts[0]][$parts[1]][$parts[2]][$parts[3]]);
					}
				break;

				default:
					$return = false;
					foreach ($parts as $part)
					{
						if ($return === false and isset($this->data[$part]))
						{
							$return =& $this->data[$part];
						}
						elseif (isset($return[$part]))
						{
							$return =& $return[$part];
						}
					}
					if ($return !== false) unset($return);
				break;
			}
		}

		// need to auto-update the session?
		if ($this->config['write_on_set'] === true)
		{
			$this->write();
		}
	}

	// --------------------------------------------------------------------

	/**
	 * force a session_id rotation
	 *
	 * @access	public
	 * @param	boolean, if true, force a session id rotation
	 * @return  void
	 */
	public function rotate($force = true)
	{
		// existing session. need to rotate the session id?
		if ($this->config['rotation_time'] &&
			($force or $this->keys['created'] + $this->config['rotation_time'] <= $this->time->get_timestamp()))
		{

			// generate a new session id, and update the create timestamp
			$this->keys['previous_id']	= $this->keys['session_id'];
			$this->keys['session_id']	= $this->_new_session_id();
			$this->keys['created'] 		= $this->time->get_timestamp();
			$this->keys['updated']		= $this->keys['created'];
		}

	}

	// --------------------------------------------------------------------

	/**
	 * set session flash variables
	 *
	 * @param	string	name of the variable to set
	 * @param	mixed	value
	 * @access	public
	 * @return	void
	 */
	public function set_flash($name, $value)
	{
		$this->flash[$this->config['flash_id'].'::'.$name] = array('state' => 'new', 'value' => $value);

		// need to auto-update the session?
		if ($this->config['write_on_set'] === true)
		{
			$this->write();
		}
	}

	// --------------------------------------------------------------------

	/**
	 * get session flash variables
	 *
	 * @access	public
	 * @param	string	name of the variable to get
	 * @param	mixed	default value to return if the variable does not exist
	 * @return	mixed
	 */
	public function get_flash($name, $default = null)
	{
		if (is_null($name))
		{
			$default = array();
			foreach($this->flash as $key => $value)
			{
				$key = substr($key, strpos($key, '::')+2);
				$default[$key] = $value;
			}
		}
		elseif (isset($this->flash[$this->config['flash_id'].'::'.$name]))
		{
			$this->flash[$this->config['flash_id'].'::'.$name]['state'] = 'old';
			$default = $this->flash[$this->config['flash_id'].'::'.$name]['value'];

			// need to auto-update the session?
			if ($this->config['write_on_set'] === true)
			{
				$this->write();
			}
		}

		return $default;
	}

	// --------------------------------------------------------------------

	/**
	 * keep session flash variables
	 *
	 * @access	public
	 * @param	string	name of the variable to keep
	 * @return	void
	 */
	public function keep_flash($name)
	{
		if (is_null($name))
		{
			foreach($this->flash as $key => $value)
			{
				$this->flash[$key]['state'] = 'new';
			}
		}
		elseif (isset($this->flash[$this->config['flash_id'].'::'.$name]))
		{
			$this->flash[$this->config['flash_id'].'::'.$name]['state'] = 'new';
		}

		// need to auto-update the session?
		if ($this->config['write_on_set'] === true)
		{
			$this->write();
		}
	}

	// --------------------------------------------------------------------

	/**
	 * delete session flash variables
	 *
	 * @param	string	name of the variable to delete
	 * @param	mixed	value
	 * @access	public
	 * @return	void
	 */
	public function delete_flash($name)
	{
		if (is_null($name))
		{
			$this->flash = array();
		}
		elseif (isset($this->flash[$this->config['flash_id'].'::'.$name]))
		{
			unset($this->flash[$this->config['flash_id'].'::'.$name]);
		}

		// need to auto-update the session?
		if ($this->config['write_on_set'] === true)
		{
			$this->write();
		}
	}

	// --------------------------------------------------------------------

	/**
	 * set the session flash id
	 *
	 * @param	string	name of the id to set
	 * @access	public
	 * @return	void
	 */
	public function set_flash_id($name)
	{
		$this->config['flash_id'] = (string) $name;
	}

	// --------------------------------------------------------------------

	/**
	 * get the current session flash id
	 *
	 * @access	public
	 * @return	string	name of the flash id
	 */
	public function get_flash_id($name)
	{
		return $this->config['flash_id'];
	}

	// --------------------------------------------------------------------

	/**
	 * get a runtime config value
	 *
	 * @param	string	name of the config variable to get
	 * @access	public
	 * @return  mixed
	 */
	public function get_config($name)
	{
		return isset($this->config[$name]) ? $this->config[$name] : null;
	}

	// --------------------------------------------------------------------

	/**
	 * removes flash variables marked as old
	 *
	 * @access	private
	 * @return  void
	 */
	protected function _cleanup_flash()
	{
var_dump($this->flash);
		foreach($this->flash as $key => $value)
		{
			if ($value['state'] === 'old')
			{
				unset($this->flash[$key]);
			}
		}
	}
<commit_msg>removed some left-over debug code<commit_after><?php
/**
 * Fuel
 *
 * Fuel is a fast, lightweight, community driven PHP5 framework.
 *
 * @package		Fuel
 * @version		1.0
 * @author		Harro "WanWizard" Verton
 * @license		MIT License
 * @copyright	2010 <NAME>
 * @link		http://fuelphp.com
 */

namespace Fuel\Core;



abstract class Session_Driver {

	/*
	 * @var	session class configuration
	 */
	protected $config = array();

	/*
	 * @var	session indentification keys
	 */
	protected $keys = array();

	/*
	 * @var	session variable data
	 */
	protected $data = array();

	/*
	 * @var	session flash data
	 */
	protected $flash = array();

	/*
	 * @var	session time object
	 */
	protected $time = null;

	// --------------------------------------------------------------------
	// abstract methods
	// --------------------------------------------------------------------

	/**
	 * create a new session
	 *
	 * @access	public
	 * @return	void
	 */
	abstract function create();

	// --------------------------------------------------------------------

	/**
	 * destroy the current session
	 *
	 * @access	public
	 * @return	void
	 */
	abstract function destroy();

	// --------------------------------------------------------------------
	// generic driver methods
	// --------------------------------------------------------------------

	/**
	 * read the session
	 *
	 * @access	public
	 * @return	void
	 */
	public function read()
	{
		// auto expire flash variables if needed
		if ($this->config['flash_auto_expire'] === true)
		{
			foreach($this->flash as $key => $value)
			{
				$this->flash[$key]['state'] = 'old';
			}
		}
	}

	// --------------------------------------------------------------------

	/**
	 * write the session
	 *
	 * @access	public
	 * @return	void
	 */
	public function write()
	{
		$this->_cleanup_flash();
	}

	// --------------------------------------------------------------------

	/**
	 * generic driver initialisation
	 *
	 * @access	public
	 * @return	void
	 */
	public function init()
	{
		// get a time object
		$this->time = \Date::time();
	}

	// --------------------------------------------------------------------

	/**
	 * set session variables
	 *
	 * @param	string	name of the variable to set
	 * @param	mixed	value
	 * @access	public
	 * @return	void
	 */
	public function set($name, $value)
	{
		$this->data[$name] = $value;

		// need to auto-update the session?
		if ($this->config['write_on_set'] === true)
		{
			$this->write();
		}
	}

	// --------------------------------------------------------------------

	/**
	 * get session variables
	 *
	 * @access	public
	 * @param	string	name of the variable to get
	 * @param	mixed	default value to return if the variable does not exist
	 * @return	mixed
	 */
	public function get($name, $default = null)
	{
		if (is_null($name))
		{
			return $this->data;
		}
		elseif (isset($this->data[$name]))
		{
			return $this->data[$name];
		}

		if (strpos($name, '.') !== false)
		{
			$parts = explode('.', $name);

			switch (count($parts))
			{
				case 2:
					if (isset($this->data[$parts[0]][$parts[1]]))
					{
						return $this->data[$parts[0]][$parts[1]];
					}
				break;

				case 3:
					if (isset($this->data[$parts[0]][$parts[1]][$parts[2]]))
					{
						return $this->data[$parts[0]][$parts[1]][$parts[2]];
					}
				break;

				case 4:
					if (isset($this->data[$parts[0]][$parts[1]][$parts[2]][$parts[3]]))
					{
						return $this->data[$parts[0]][$parts[1]][$parts[2]][$parts[3]];
					}
				break;

				default:
					$return = false;
					foreach ($parts as $part)
					{
						if ($return === false and isset($this->data[$part]))
						{
							$return = $this->data[$part];
						}
						elseif (isset($return[$part]))
						{
							$return = $return[$part];
						}
						else
						{
							return $default;
						}
					}
					return $return;
				break;
			}
		}

		return $default;
	}

	// --------------------------------------------------------------------

	/**
	 * get session key variables
	 *
	 * @access	public
	 * @param	string	name of the variable to get, default is 'session_id'
	 * @return	mixed	contents of the requested variable, or false if not found
	 */
	public function key($name)
	{
		return isset($this->keys[$name]) ? $this->keys[$name] : false;
	}

	// --------------------------------------------------------------------

	/**
	 * delete session variables
	 *
	 * @param	string	name of the variable to delete
	 * @param	mixed	value
	 * @access	public
	 * @return	void
	 */
	public function delete($name)
	{
		if (isset($this->data[$name]))
		{
			unset($this->data[$name]);
		}

		if (strpos($name, '.') !== false)
		{
			$parts = explode('.', $name);

			switch (count($parts))
			{
				case 2:
					if (isset($this->data[$parts[0]][$parts[1]]))
					{
						unset($this->data[$parts[0]][$parts[1]]);
					}
				break;

				case 3:
					if (isset($this->data[$parts[0]][$parts[1]][$parts[2]]))
					{
						unset($this->data[$parts[0]][$parts[1]][$parts[2]]);
					}
				break;

				case 4:
					if (isset($this->data[$parts[0]][$parts[1]][$parts[2]][$parts[3]]))
					{
						unset($this->data[$parts[0]][$parts[1]][$parts[2]][$parts[3]]);
					}
				break;

				default:
					$return = false;
					foreach ($parts as $part)
					{
						if ($return === false and isset($this->data[$part]))
						{
							$return =& $this->data[$part];
						}
						elseif (isset($return[$part]))
						{
							$return =& $return[$part];
						}
					}
					if ($return !== false) unset($return);
				break;
			}
		}

		// need to auto-update the session?
		if ($this->config['write_on_set'] === true)
		{
			$this->write();
		}
	}

	// --------------------------------------------------------------------

	/**
	 * force a session_id rotation
	 *
	 * @access	public
	 * @param	boolean, if true, force a session id rotation
	 * @return  void
	 */
	public function rotate($force = true)
	{
		// existing session. need to rotate the session id?
		if ($this->config['rotation_time'] &&
			($force or $this->keys['created'] + $this->config['rotation_time'] <= $this->time->get_timestamp()))
		{

			// generate a new session id, and update the create timestamp
			$this->keys['previous_id']	= $this->keys['session_id'];
			$this->keys['session_id']	= $this->_new_session_id();
			$this->keys['created'] 		= $this->time->get_timestamp();
			$this->keys['updated']		= $this->keys['created'];
		}

	}

	// --------------------------------------------------------------------

	/**
	 * set session flash variables
	 *
	 * @param	string	name of the variable to set
	 * @param	mixed	value
	 * @access	public
	 * @return	void
	 */
	public function set_flash($name, $value)
	{
		$this->flash[$this->config['flash_id'].'::'.$name] = array('state' => 'new', 'value' => $value);

		// need to auto-update the session?
		if ($this->config['write_on_set'] === true)
		{
			$this->write();
		}
	}

	// --------------------------------------------------------------------

	/**
	 * get session flash variables
	 *
	 * @access	public
	 * @param	string	name of the variable to get
	 * @param	mixed	default value to return if the variable does not exist
	 * @return	mixed
	 */
	public function get_flash($name, $default = null)
	{
		if (is_null($name))
		{
			$default = array();
			foreach($this->flash as $key => $value)
			{
				$key = substr($key, strpos($key, '::')+2);
				$default[$key] = $value;
			}
		}
		elseif (isset($this->flash[$this->config['flash_id'].'::'.$name]))
		{
			$this->flash[$this->config['flash_id'].'::'.$name]['state'] = 'old';
			$default = $this->flash[$this->config['flash_id'].'::'.$name]['value'];

			// need to auto-update the session?
			if ($this->config['write_on_set'] === true)
			{
				$this->write();
			}
		}

		return $default;
	}

	// --------------------------------------------------------------------

	/**
	 * keep session flash variables
	 *
	 * @access	public
	 * @param	string	name of the variable to keep
	 * @return	void
	 */
	public function keep_flash($name)
	{
		if (is_null($name))
		{
			foreach($this->flash as $key => $value)
			{
				$this->flash[$key]['state'] = 'new';
			}
		}
		elseif (isset($this->flash[$this->config['flash_id'].'::'.$name]))
		{
			$this->flash[$this->config['flash_id'].'::'.$name]['state'] = 'new';
		}

		// need to auto-update the session?
		if ($this->config['write_on_set'] === true)
		{
			$this->write();
		}
	}

	// --------------------------------------------------------------------

	/**
	 * delete session flash variables
	 *
	 * @param	string	name of the variable to delete
	 * @param	mixed	value
	 * @access	public
	 * @return	void
	 */
	public function delete_flash($name)
	{
		if (is_null($name))
		{
			$this->flash = array();
		}
		elseif (isset($this->flash[$this->config['flash_id'].'::'.$name]))
		{
			unset($this->flash[$this->config['flash_id'].'::'.$name]);
		}

		// need to auto-update the session?
		if ($this->config['write_on_set'] === true)
		{
			$this->write();
		}
	}

	// --------------------------------------------------------------------

	/**
	 * set the session flash id
	 *
	 * @param	string	name of the id to set
	 * @access	public
	 * @return	void
	 */
	public function set_flash_id($name)
	{
		$this->config['flash_id'] = (string) $name;
	}

	// --------------------------------------------------------------------

	/**
	 * get the current session flash id
	 *
	 * @access	public
	 * @return	string	name of the flash id
	 */
	public function get_flash_id($name)
	{
		return $this->config['flash_id'];
	}

	// --------------------------------------------------------------------

	/**
	 * get a runtime config value
	 *
	 * @param	string	name of the config variable to get
	 * @access	public
	 * @return  mixed
	 */
	public function get_config($name)
	{
		return isset($this->config[$name]) ? $this->config[$name] : null;
	}

	// --------------------------------------------------------------------

	/**
	 * removes flash variables marked as old
	 *
	 * @access	private
	 * @return  void
	 */
	protected function _cleanup_flash()
	{
		foreach($this->flash as $key => $value)
		{
			if ($value['state'] === 'old')
			{
				unset($this->flash[$key]);
			}
		}
	}

{'fraction_non_alphanumeric': 0.29998040944264864, 'fraction_numerical': 0.004897639337839162, 'mean_word_length': 1.7059369202226344, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 180, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3501715', 'n_tokens_mistral': 7550, 'n_tokens_neox': 6216, 'n_words': 1433}