-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcouchdb_session_handler.php
More file actions
129 lines (105 loc) · 3.92 KB
/
couchdb_session_handler.php
File metadata and controls
129 lines (105 loc) · 3.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
require_once 'couchdbwrapper.php';
/**
* CouchdbSessionHandler - a PHP Session Handler using Couchdb as backend
* This class is more or less autoconfiguring.
* If it doesn't find the database it is pointed to in the config, it tries to make it
* and proceeds to load up the necessary design-docs it needs to function.
*
* @author Jo Giraerts <jo.giraerts@gmail.com>
* @version 0.0.1
* @package CouchdbSessionHandler
*/
class CouchdbSessionHandler extends couchdbwrapper
{
public $lifeTime;
public $initSessionData;
protected $revision;
function __construct($config = null)
{
if (!$config)
return false;
parent::__construct($config);
$designdoc = '{
"_id": "_design/sessions",
"language": "javascript",
"views": {
"gc": {"map": "function(doc) { if (doc.type == \'session\') { emit(doc.expiration,doc._rev); }}"}
}
}';
register_shutdown_function("session_write_close");
$this->lifeTime = intval(ini_get("session.gc_maxlifetime"));
$this->initSessionData = null;
// Let's try to make the database in case it doesn't exist
$result = $this->send('GET', '');
if (isset($result->error) && $result->error == "not_found") {
$this->send('PUT', 'http://'.$this->config->couchdb->host.':'.$this->config->couchdb->port.'/'.$this->config->couchdb->dbname);
// Load the design documents
$result = $this->send('PUT', '_design/sessions', $designdoc);
}
}
function open($savePath="",$sessionName="")
{
$sessionID = session_id();
if ($sessionID !== "") {
$this->initSessionData = $this->read($sessionID);
}
return true;
}
function close()
{
$this->lifeTime = null;
$this->initSessionData = null;
// Cleanup
$this->gc();
return true;
}
function read($sessionID)
{
$result = $this->send("GET", ''.$sessionID);
if (isset($result->_id)) {
$this->revision = $result->_rev;
return $result->data;
}
else
return '';
}
function write($sessionID,$data)
{
$exp = time() + $this->lifeTime;
$rev = '';
if ($this->revision != '')
$rev = '"_rev": "'.$this->revision.'",';
$result = $this->send("PUT", ''. $sessionID, '{'.$rev.'"type": "session","data": "'.addslashes($data).'", "data_unencoded":'.json_encode($_SESSION).', "expiration": "'.$exp.'"}');
if (isset($result->rev))
$this->revision = $result->rev;
return isset($result->ok) && $result->ok;
}
function destroy($sessionID, $revision=false)
{
// Let's go get the latest revision so we can get rid of this session cleanly
if (!$revision) {
$result = $this->send("GET", $sessionID);
if (isset($result->_rev))
$revision = $result->_rev;
}
// Delete!
if ($revision) {
# Called when a user logs out...
$result = $this->send("DELETE", $sessionID."?rev=".$revision);
return isset($result->ok) && $result->ok;
}
return false;
}
function gc($maxlifetime=0) {
$exp = time() - $this->lifeTime;
$query = "_design/sessions/_view/gc?endkey=\"".$exp."\"";
$expired = $this->send("GET", $query);
if (isset($expired->rows) && count($expired->rows) > 0) {
foreach ($expired->rows as $row) {
$this->destroy($row->id, $row->value);
}
}
return true;
}
}