commit | author | age
|
4df4ab
|
1 |
<?php |
C |
2 |
|
a95874
|
3 |
/** |
4df4ab
|
4 |
+-----------------------------------------------------------------------+ |
C |
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> | |
11d5e7
|
18 |
| Author: Cor Bosman <cor@roundcu.bet> | |
4df4ab
|
19 |
+-----------------------------------------------------------------------+ |
C |
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; |
11d5e7
|
34 |
private $debug; |
4df4ab
|
35 |
|
b4be89
|
36 |
/** |
C |
37 |
* @param Object $config |
|
38 |
*/ |
|
39 |
public function __construct($config) |
4df4ab
|
40 |
{ |
b4be89
|
41 |
parent::__construct($config); |
C |
42 |
|
4df4ab
|
43 |
$this->memcache = rcube::get_instance()->get_memcache(); |
11d5e7
|
44 |
$this->debug = $config->get('memcache_debug'); |
4df4ab
|
45 |
|
b4be89
|
46 |
if (!$this->memcache) { |
11d5e7
|
47 |
rcube::raise_error(array( |
AM |
48 |
'code' => 604, 'type' => 'db', |
|
49 |
'line' => __LINE__, 'file' => __FILE__, |
|
50 |
'message' => "Failed to connect to memcached. Please check configuration"), |
|
51 |
true, true); |
4df4ab
|
52 |
} |
C |
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 |
11d5e7
|
86 |
$result = $this->memcache->delete($key, 0); |
AM |
87 |
|
|
88 |
if ($this->debug) { |
|
89 |
$this->debug('delete', $key, null, $result); |
|
90 |
} |
4df4ab
|
91 |
} |
C |
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 |
|
11d5e7
|
112 |
if ($this->debug) { |
AM |
113 |
$this->debug('get', $key, $value); |
|
114 |
} |
|
115 |
|
|
116 |
return $this->vars ?: ''; |
4df4ab
|
117 |
} |
C |
118 |
|
|
119 |
/** |
11d5e7
|
120 |
* Write data to memcache storage |
4df4ab
|
121 |
* |
C |
122 |
* @param $key |
|
123 |
* @param $vars |
11d5e7
|
124 |
* |
4df4ab
|
125 |
* @return bool |
C |
126 |
*/ |
|
127 |
public function write($key, $vars) |
|
128 |
{ |
11d5e7
|
129 |
$data = serialize(array('changed' => time(), 'ip' => $this->ip, 'vars' => $vars)); |
AM |
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; |
4df4ab
|
137 |
} |
C |
138 |
|
|
139 |
/** |
11d5e7
|
140 |
* Update memcache session data |
4df4ab
|
141 |
* |
C |
142 |
* @param $key |
|
143 |
* @param $newvars |
|
144 |
* @param $oldvars |
11d5e7
|
145 |
* |
4df4ab
|
146 |
* @return bool |
C |
147 |
*/ |
|
148 |
public function update($key, $newvars, $oldvars) |
|
149 |
{ |
|
150 |
$ts = microtime(true); |
|
151 |
|
|
152 |
if ($newvars !== $oldvars || $ts - $this->changed > $this->lifetime / 3) { |
11d5e7
|
153 |
$data = serialize(array('changed' => time(), 'ip' => $this->ip, 'vars' => $newvars)); |
AM |
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; |
4df4ab
|
161 |
} |
C |
162 |
|
|
163 |
return true; |
|
164 |
} |
|
165 |
|
11d5e7
|
166 |
/** |
AM |
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($this->type, $line, $result); |
|
178 |
} |
|
179 |
} |