<commit_before><?php defined('SYSPATH') or die('No direct script access.');
/**
 * Base session class.
 *
 * @package    Kohana
 * @author     Kohana Team
 * @copyright  (c) 2008-2009 Kohana Team
 * @license    http://kohanaphp.com/license.html
 */
abstract class Session_Core {

	// Session instances
	protected static $instances = array();

	/**
	 * Creates a singleton session of the given type.
	 *
	 * @param   string   session type (native, cookie, etc)
	 * @param   boolean  bind $_SESSION to the internal array
	 * @return  Session
	 */
	public static function instance($type = 'native', $bind = FALSE)
	{
		if ( ! isset(Session::$instances[$type]))
		{
			// Load the configuration for this type
			$config = Kohana::config('session')->get($type);

			// Set the session class name
			$class = 'Session_'.ucfirst($type);

			// Create a new session instance
			Session::$instances[$type] = new $class($config);

			if ($bind === TRUE)
			{
				if ( ! isset($_SESSION))
				{
					// Create the $_SESSION global
					$GLOBALS['_SESSION'] = array();
				}

				// Bind to the global $_SESSION array
				$_SESSION =& Session::$instances[$type]->data();
			}
		}

		return Session::$instances[$type];
	}

	// Cookie name
	protected $_name = 'session';

	// Cookie lifetime
	protected $_lifetime  = 0;

	// Encrypt session data?
	protected $_encrypted = FALSE;

	// Session data
	protected $_data = array();

	/**
	 * Overloads the name, lifetime, and encrypted session settings.
	 *
	 * @param   array  configuration
	 * @return  void
	 */
	protected function __construct(array $config = NULL)
	{
		if (isset($config['name']))
		{
			// Cookie name to store the session id in
			$this->_name = (string) $config['name'];
		}

		if (isset($config['lifetime']))
		{
			// Cookie lifetime
			$this->_lifetime = (int) $config['lifetime'];
		}

		if (isset($config['encrypted']))
		{
			// Enable or disable encryption of data
			$this->_encrypted = (bool) $config['encrypted'];
		}

		// Load the session
		$this->read();
	}

	/**
	 * Session object is rendered to a serialized string.
	 *
	 * @return  string
	 */
	public function __toString()
	{
		// Serialize the data array
		$data = serialize($this->_data);

		if ($this->_encrypted === TRUE)
		{
			// @todo: encrypt the data here
		}
		else
		{
			// Obfuscate the data with base64 encoding
			$data = base64_encode($data);
		}

		return $data;
	}

	/**
	 * Returns the current session array.
	 *
	 * @return  array
	 */
	public function & as_array()
	{
		return $this->_data;
	}

	/**
	 * Get a variable from the session array.
	 *
	 * @param   string   variable name
	 * @param   mixed    default value to return
	 * @return  mixed
	 */
	public function get($key, $default = NULL)
	{
		return array_key_exists($key, $this->_data) ? $this->_data[$key] : $default;
	}

	/**
	 * Set a variable in the session array.
	 *
	 * @param   string   variable name
	 * @param   mixed    value
	 * @return  Session
	 */
	public function set($key, $value)
	{
		$this->_data[$key] = $value;

		return $this;
	}

	/**
	 * Removes a variable in the session array.
	 *
	 * @param   string  variable name
	 * @return  Session
	 */
	public function delete($key)
	{
		unset($this->_data[$key]);

		return $this;
	}

	/**
	 * Loads the session data.
	 * 
	 * @return  void
	 */
	public function read()
	{
		if (is_string($data = $this->_read()))
		{
			try
			{
				if ($this->_encrypted === TRUE)
				{
					// @todo: decrypt the data here
				}
				else
				{
					// Decode the base64 encoded data
					$data = base64_decode($data);
				}
<commit_msg>Added encryption to Session reading and writing<commit_after><?php defined('SYSPATH') or die('No direct script access.');
/**
 * Base session class.
 *
 * @package    Kohana
 * @author     Kohana Team
 * @copyright  (c) 2008-2009 Kohana Team
 * @license    http://kohanaphp.com/license.html
 */
abstract class Session_Core {

	// Session instances
	protected static $instances = array();

	/**
	 * Creates a singleton session of the given type.
	 *
	 * @param   string   session type (native, cookie, etc)
	 * @param   boolean  bind $_SESSION to the internal array
	 * @return  Session
	 */
	public static function instance($type = 'native', $bind = FALSE)
	{
		if ( ! isset(Session::$instances[$type]))
		{
			// Load the configuration for this type
			$config = Kohana::config('session')->get($type);

			// Set the session class name
			$class = 'Session_'.ucfirst($type);

			// Create a new session instance
			Session::$instances[$type] = new $class($config);

			if ($bind === TRUE)
			{
				if ( ! isset($_SESSION))
				{
					// Create the $_SESSION global
					$GLOBALS['_SESSION'] = array();
				}

				// Bind to the global $_SESSION array
				$_SESSION =& Session::$instances[$type]->data();
			}
		}

		return Session::$instances[$type];
	}

	// Cookie name
	protected $_name = 'session';

	// Cookie lifetime
	protected $_lifetime  = 0;

	// Encrypt session data?
	protected $_encrypted = FALSE;

	// Session data
	protected $_data = array();

	/**
	 * Overloads the name, lifetime, and encrypted session settings.
	 *
	 * @param   array  configuration
	 * @return  void
	 */
	protected function __construct(array $config = NULL)
	{
		if (isset($config['name']))
		{
			// Cookie name to store the session id in
			$this->_name = (string) $config['name'];
		}

		if (isset($config['lifetime']))
		{
			// Cookie lifetime
			$this->_lifetime = (int) $config['lifetime'];
		}

		if (isset($config['encrypted']))
		{
			// Enable or disable encryption of data
			$this->_encrypted = (bool) $config['encrypted'];
		}

		// Load the session
		$this->read();
	}

	/**
	 * Session object is rendered to a serialized string.
	 *
	 * @return  string
	 */
	public function __toString()
	{
		// Serialize the data array
		$data = serialize($this->_data);

		if ($this->_encrypted === TRUE)
		{
			// Encrypt the data using the default key
			$data = Encrypt::instance()->encode($data);
		}
		else
		{
			// Obfuscate the data with base64 encoding
			$data = base64_encode($data);
		}

		return $data;
	}

	/**
	 * Returns the current session array.
	 *
	 * @return  array
	 */
	public function & as_array()
	{
		return $this->_data;
	}

	/**
	 * Get a variable from the session array.
	 *
	 * @param   string   variable name
	 * @param   mixed    default value to return
	 * @return  mixed
	 */
	public function get($key, $default = NULL)
	{
		return array_key_exists($key, $this->_data) ? $this->_data[$key] : $default;
	}

	/**
	 * Set a variable in the session array.
	 *
	 * @param   string   variable name
	 * @param   mixed    value
	 * @return  Session
	 */
	public function set($key, $value)
	{
		$this->_data[$key] = $value;

		return $this;
	}

	/**
	 * Removes a variable in the session array.
	 *
	 * @param   string  variable name
	 * @return  Session
	 */
	public function delete($key)
	{
		unset($this->_data[$key]);

		return $this;
	}

	/**
	 * Loads the session data.
	 * 
	 * @return  void
	 */
	public function read()
	{
		if (is_string($data = $this->_read()))
		{
			try
			{
				if ($this->_encrypted === TRUE)
				{
					// Decrypt the data using the default key
					$data = Encrypt::instance()->decode($data);
				}
				else
				{
					// Decode the base64 encoded data
					$data = base64_decode($data);
				}

{'fraction_non_alphanumeric': 0.14729950900163666, 'fraction_numerical': 0.004637206764866339, 'mean_word_length': 2.747061829330608, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 35, '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': '4894051', 'n_tokens_mistral': 2709, 'n_tokens_neox': 2293, 'n_words': 682}