0
|
1 <?php
|
|
2
|
|
3 /**
|
|
4 +-----------------------------------------------------------------------+
|
|
5 | This file is part of the Roundcube Webmail client |
|
|
6 | Copyright (C) 2005-2014, The Roundcube Dev Team |
|
|
7 | Copyright (C) 2011, Kolab Systems AG |
|
|
8 | |
|
|
9 | Licensed under the GNU General Public License version 3 or |
|
|
10 | any later version with exceptions for skins & plugins. |
|
|
11 | See the README file for a full license statement. |
|
|
12 | |
|
|
13 | PURPOSE: |
|
|
14 | Provide database supported session management |
|
|
15 +-----------------------------------------------------------------------+
|
|
16 | Author: Thomas Bruederli <roundcube@gmail.com> |
|
|
17 | Author: Aleksander Machniak <alec@alec.pl> |
|
|
18 | Author: Cor Bosman <cor@roundcu.bet> |
|
|
19 +-----------------------------------------------------------------------+
|
|
20 */
|
|
21
|
|
22 /**
|
|
23 * Class to provide memcache session storage
|
|
24 *
|
|
25 * @package Framework
|
|
26 * @subpackage Core
|
|
27 * @author Thomas Bruederli <roundcube@gmail.com>
|
|
28 * @author Aleksander Machniak <alec@alec.pl>
|
|
29 * @author Cor Bosman <cor@roundcu.be>
|
|
30 */
|
|
31 class rcube_session_memcache extends rcube_session
|
|
32 {
|
|
33 private $memcache;
|
|
34 private $debug;
|
|
35
|
|
36 /**
|
|
37 * @param Object $config
|
|
38 */
|
|
39 public function __construct($config)
|
|
40 {
|
|
41 parent::__construct($config);
|
|
42
|
|
43 $this->memcache = rcube::get_instance()->get_memcache();
|
|
44 $this->debug = $config->get('memcache_debug');
|
|
45
|
|
46 if (!$this->memcache) {
|
|
47 rcube::raise_error(array(
|
|
48 'code' => 604, 'type' => 'db',
|
|
49 'line' => __LINE__, 'file' => __FILE__,
|
|
50 'message' => "Failed to connect to memcached. Please check configuration"),
|
|
51 true, true);
|
|
52 }
|
|
53
|
|
54 // register sessions handler
|
|
55 $this->register_session_handler();
|
|
56 }
|
|
57
|
|
58 /**
|
|
59 * @param $save_path
|
|
60 * @param $session_name
|
|
61 * @return bool
|
|
62 */
|
|
63 public function open($save_path, $session_name)
|
|
64 {
|
|
65 return true;
|
|
66 }
|
|
67
|
|
68 /**
|
|
69 * @return bool
|
|
70 */
|
|
71 public function close()
|
|
72 {
|
|
73 return true;
|
|
74 }
|
|
75
|
|
76 /**
|
|
77 * Handler for session_destroy() with memcache backend
|
|
78 *
|
|
79 * @param $key
|
|
80 * @return bool
|
|
81 */
|
|
82 public function destroy($key)
|
|
83 {
|
|
84 if ($key) {
|
|
85 // #1488592: use 2nd argument
|
|
86 $result = $this->memcache->delete($key, 0);
|
|
87
|
|
88 if ($this->debug) {
|
|
89 $this->debug('delete', $key, null, $result);
|
|
90 }
|
|
91 }
|
|
92
|
|
93 return true;
|
|
94 }
|
|
95
|
|
96 /**
|
|
97 * Read session data from memcache
|
|
98 *
|
|
99 * @param $key
|
|
100 * @return null|string
|
|
101 */
|
|
102 public function read($key)
|
|
103 {
|
|
104 if ($value = $this->memcache->get($key)) {
|
|
105 $arr = unserialize($value);
|
|
106 $this->changed = $arr['changed'];
|
|
107 $this->ip = $arr['ip'];
|
|
108 $this->vars = $arr['vars'];
|
|
109 $this->key = $key;
|
|
110 }
|
|
111
|
|
112 if ($this->debug) {
|
|
113 $this->debug('get', $key, $value);
|
|
114 }
|
|
115
|
|
116 return $this->vars ?: '';
|
|
117 }
|
|
118
|
|
119 /**
|
|
120 * Write data to memcache storage
|
|
121 *
|
|
122 * @param $key
|
|
123 * @param $vars
|
|
124 *
|
|
125 * @return bool
|
|
126 */
|
|
127 public function write($key, $vars)
|
|
128 {
|
|
129 $data = serialize(array('changed' => time(), 'ip' => $this->ip, 'vars' => $vars));
|
|
130 $result = $this->memcache->set($key, $data, MEMCACHE_COMPRESSED, $this->lifetime + 60);
|
|
131
|
|
132 if ($this->debug) {
|
|
133 $this->debug('set', $key, $data, $result);
|
|
134 }
|
|
135
|
|
136 return $result;
|
|
137 }
|
|
138
|
|
139 /**
|
|
140 * Update memcache session data
|
|
141 *
|
|
142 * @param $key
|
|
143 * @param $newvars
|
|
144 * @param $oldvars
|
|
145 *
|
|
146 * @return bool
|
|
147 */
|
|
148 public function update($key, $newvars, $oldvars)
|
|
149 {
|
|
150 $ts = microtime(true);
|
|
151
|
|
152 if ($newvars !== $oldvars || $ts - $this->changed > $this->lifetime / 3) {
|
|
153 $data = serialize(array('changed' => time(), 'ip' => $this->ip, 'vars' => $newvars));
|
|
154 $result = $this->memcache->set($key, $data, MEMCACHE_COMPRESSED, $this->lifetime + 60);
|
|
155
|
|
156 if ($this->debug) {
|
|
157 $this->debug('set', $key, $data, $result);
|
|
158 }
|
|
159
|
|
160 return $result;
|
|
161 }
|
|
162
|
|
163 return true;
|
|
164 }
|
|
165
|
|
166 /**
|
|
167 * Write memcache debug info to the log
|
|
168 */
|
|
169 protected function debug($type, $key, $data = null, $result = null)
|
|
170 {
|
|
171 $line = strtoupper($type) . ' ' . $key;
|
|
172
|
|
173 if ($data !== null) {
|
|
174 $line .= ' ' . $data;
|
|
175 }
|
|
176
|
|
177 rcube::debug('memcache', $line, $result);
|
|
178 }
|
|
179 }
|