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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
<?
#
# vim:set ts=4:
# vim:set shiftwidth=4;
#
require_once "../include/Tools.inc";
class Session
{
var $id = false;
var $ref = false;
var $db = false;
var $messages = false;
var $cookie_name = false;
/**
* create a new Session
*
* creates a new session by calling <code>get_id()</code> and
* throwing an error on failure.
*
* @params object a Database object
* @params string Name of the cookie holding session id
* @params object a Messages object
*/
function Session(&$database, $cookie_name, &$messages) {
assert($cookie_name);
assert($database);
assert($messages);
$this->cookie_name = $cookie_name;
$this->db = &$database;
$this->messages = &$messages;
$this->get_id();
}
/**
* stores current session to database
*
* This function stores the current session to the database by
* calling <code>store_data</code> and sets <code>id</code> and
* <code>ref</code> to false.
*
* @returns boolean true on success, false otherwhise
*/
function close() {
if (! $this->id)
return true;
assert($this->id != '');
assert($this->db);
if ($this->store_data() === false)
return false;
$this->id = false;
$this->ref = false;
$this->data = false;
return true;
}
/**
* Get data from database
*
* Fetches data for the current id from the database
* and sets <code>session_data_ref</code>.
*
* @returns boolean true on success, false otherwhise
*/
function get_data()
{
assert($this->id != '');
assert($this->db);
$row=$this->db->query_row('select ref, data from session where id=?', array($this->id));
if ( $row === false )
return false;
$this->ref = $row['ref'];
$this->data = ($row['data']) ? unserialize($row['data']) : false;
assert($this->ref);
return true;
}
/**
* Store data in database
*
* Dumps data for the current id into the database.
*
* @returns boolean true on success, false otherwhise
*/
function store_data()
{
assert($this->ref);
assert($this->id != '');
assert($this->db);
return $this->db->update('session', $this->ref, array(data => serialize($this->data), last_seen => 'now'));
}
/**
* Create a session in the database with the current id
*
* Creates a session in the database and fills local variables by
* calling <code>get_data()</code>.
*
* @returns boolean true if success
*/
function create_session()
{
assert($this->id != '');
assert($this->db);
$result = $this->db->insert('session',array(id => $this->id));
if ( $result === false )
return false;
return $this->get_data();
}
/**
* Create a unique string useable as session ID
*
* @returns string a unique session ID
*/
function create_id() {
$id = $_SERVER["UNIQUE_ID"];
$id .= md5(time.rand(0,1000000));
return $id;
}
/**
* Set session ID for current session
*
* Should there be no session ID a new session ID is created, inserted into
* the database (creating a new session), sent as a cookie to the luser,
* and returned.
*
* Also makes sure ref is set and persistent session data is loaded.
*
* @returns string session ID, boolean false on error
*/
function set_id() {
if ( ! $this->id ) {
$this->id = $this->create_id();
if ($this->create_session() === false) {
$this->id = false;
return false;
}
setcookie($this->cookie_name, $this->id);
} else {
if ($this->get_data() === false) {
$this->id = false;
return false;
}
}
return $this->id;
}
/**
* Get session ID for current session
*
* This function takes the session ID of a cookie, checks wheter it is
* database and if yes, returns it.
*
* Also makes sure ref is set and persistent session data is loaded.
*
* @returns string session ID, boolean false on error
*/
function get_id() {
$this->id=$_COOKIE[$this->cookie_name];
if ($this->id)
if ($this->get_data() === false)
$this->id = false;
return $this->id;
}
}
?>
|