1<?php defined('SYSPATH') OR die('No direct access allowed.');
2/**
3 * Session cache driver.
4 *
5 * Cache library config goes in the session.storage config entry:
6 * $config['storage'] = array(
7 *     'driver' => 'apc',
8 *     'requests' => 10000
9 * );
10 * Lifetime does not need to be set as it is
11 * overridden by the session expiration setting.
12 *
13 * $Id: Cache.php 3769 2008-12-15 00:48:56Z zombor $
14 *
15 * @package    Core
16 * @author     Kohana Team
17 * @copyright  (c) 2007-2008 Kohana Team
18 * @license    http://kohanaphp.com/license.html
19 */
20class Session_Cache_Driver implements Session_Driver {
21
22	protected $cache;
23	protected $encrypt;
24
25	public function __construct()
26	{
27		// Load Encrypt library
28		if (Kohana::config('session.encryption'))
29		{
30			$this->encrypt = new Encrypt;
31		}
32
33		Kohana::log('debug', 'Session Cache Driver Initialized');
34	}
35
36	public function open($path, $name)
37	{
38		$config = Kohana::config('session.storage');
39
40		if (empty($config))
41		{
42			// Load the default group
43			$config = Kohana::config('cache.default');
44		}
45		elseif (is_string($config))
46		{
47			$name = $config;
48
49			// Test the config group name
50			if (($config = Kohana::config('cache.'.$config)) === NULL)
51				throw new Kohana_Exception('cache.undefined_group', $name);
52		}
53
54		$config['lifetime'] = (Kohana::config('session.expiration') == 0) ? 86400 : Kohana::config('session.expiration');
55		$this->cache = new Cache($config);
56
57		return is_object($this->cache);
58	}
59
60	public function close()
61	{
62		return TRUE;
63	}
64
65	public function read($id)
66	{
67		$id = 'session_'.$id;
68		if ($data = $this->cache->get($id))
69		{
70			return Kohana::config('session.encryption') ? $this->encrypt->decode($data) : $data;
71		}
72
73		// Return value must be string, NOT a boolean
74		return '';
75	}
76
77	public function write($id, $data)
78	{
79		$id = 'session_'.$id;
80		$data = Kohana::config('session.encryption') ? $this->encrypt->encode($data) : $data;
81
82		return $this->cache->set($id, $data);
83	}
84
85	public function destroy($id)
86	{
87		$id = 'session_'.$id;
88		return $this->cache->delete($id);
89	}
90
91	public function regenerate()
92	{
93		session_regenerate_id(TRUE);
94
95		// Return new session id
96		return session_id();
97	}
98
99	public function gc($maxlifetime)
100	{
101		// Just return, caches are automatically cleaned up
102		return TRUE;
103	}
104
105} // End Session Cache Driver
106