]> git.bts.cx Git - cx.git/blob - cx/lib/sessions.php
post metadata support (not very good yet)
[cx.git] / cx / lib / sessions.php
1 <?php
2
3 cx_require('lib', 'db.php');
4 cx_require('lib', 'setup.php');
5
6 function cx_sessions_create_session($user) {
7 $uid = sha1(random_bytes(100));
8 $update_time = $creation_time = time();
9
10 $sql = 'INSERT INTO sessions (
11 session_uid,
12 session_user_id,
13 session_creation_time,
14 session_update_time,
15 session_closed_time)
16 VALUES (?, ?, ?, ?, ?);';
17 cx_db_exec($sql, $uid, $user, $creation_time, $update_time, -1);
18
19 return $uid;
20 }
21
22 function cx_sessions_close_session($uid) {
23 $close_time = time();
24
25 $sql = 'UPDATE sessions
26 SET session_closed_time = ?
27 WHERE session_uid == ?;';
28 //LIMIT 1;';
29
30 cx_db_exec($sql, $close_time, $uid);
31 }
32
33 function cx_sessions_active_session_user($uid, $lifetime) {
34 $sql = 'SELECT
35 session_user_id
36 FROM sessions
37 WHERE session_uid == ?
38 AND session_update_time >= ?
39 AND session_closed_time == -1
40 LIMIT 1;';
41
42 $update_time = time() - $lifetime;
43
44 foreach (cx_db_query($sql, $uid, $update_time) as $session) {
45 return $session['session_user_id'];
46 }
47
48 return null;
49 }
50
51 cx_setup_register(1, function() {
52 cx_db_exec('CREATE TABLE sessions (
53 session_id INTEGER PRIMARY KEY,
54 session_uid STRING,
55 session_user_id INTEGER,
56 session_creation_time INTEGER,
57 session_update_time INTEGER,
58 session_closed_time INTEGER,
59
60 FOREIGN KEY(session_user_id) REFERENCES users(user_id))');
61 });
62
63