JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
copyrighted dwt.php, metaform textboxes aren't puny by default, added kill_session()
[wfpl.git] / session.php
1 <?php
2
3 #  Copyright (C) 2006 Jason Woofenden
4 #
5 #  This file is part of wfpl.
6 #
7 #  wfpl is free software; you can redistribute it and/or modify it under the
8 #  terms of the GNU Lesser General Public License as published by the Free
9 #  Software Foundation; either version 2.1 of the License, or (at your option)
10 #  any later version.
11 #
12 #  wfpl is distributed in the hope that it will be useful, but WITHOUT ANY
13 #  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 #  FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
15 #  more details.
16 #
17 #  You should have received a copy of the GNU Lesser General Public License
18 #  along with wfpl; if not, write to the Free Software Foundation, Inc., 51
19 #  Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
20
21 # you'll need this file that calles db_connect()
22 if(!isset($GLOBALS['wfpl_db_handle'])) {
23         require_once('db_connect.php');
24 }
25
26 # and these database tables:
27 # create table wfpl_sessions (id int unique auto_increment, session_key varchar(16), length int, expires int);
28 # create table wfpl_session_data (id int unique auto_increment, session_id int, name varchar(100), value text);
29
30 # GLOSSARY
31 #
32 # session_key  16 digit string identifying the session
33 # session_id   integer id of the record in the "sessions" table of the database
34 # UNTIL_CLOSE  a constant passed as session length to indicate "until browser window closes"
35
36
37 # session_id is kept in $GLOBALS
38 # session_key is sent as a cookie, and thus appears in $_REQUEST. The clean version is in $GLOBALS
39
40 # generate a new random 16-character string
41 function session_generate_key() {
42         $character_set = "abcdefghijklmnopqrstuvwqyzABCDEFGHIJKLMNOPQRSTUVWQYZ0123456789";
43     $id = "                ";
44
45         # PHP 4.2.0 and up seed the random number generator for you.
46         # Lets hope that it seeds with something harder to guess than the clock.
47     for($i = 0; $i < 16; ++$i) {
48         $id{$i} = $character_set{mt_rand(0, 61)};
49     }
50
51     return $id;
52 }
53
54 # track this user with a session cookie (ie a cookie that goes away when the
55 # user closes the browser). The timestamp is how long to track the session in
56 # the database. Defaults to one day.
57 function session_new($length = 86400) {
58         $session_key = session_generate_key();
59
60         db_insert('wfpl_sessions', 'session_key,length', $session_key, $length);
61         $GLOBALS['session_id'] = db_auto_id();
62         $GLOBALS['session_key'] = $session_key;
63         $_REQUEST['session_key'] = $session_key; #just in case someone calls session_exists() after session_new()
64         session_touch($length);
65 }
66
67 # call to renew the timeout for the session.
68 # assumes there's a session. call session_init() if you'd like one auto-create one if not found.
69 function session_touch($length = false) {
70         if(!$length) {
71                 $length = db_get_value('wfpl_sessions', 'length', 'where id=%i', $GLOBALS['session_id']);
72         }
73         $expires = time() + $length;
74
75         header('Set-Cookie: session_key=' . $GLOBALS['session_key']);
76
77         db_update('wfpl_sessions', 'expires', $expires, 'where id=%i', $GLOBALS['session_id']);
78 }
79
80 # delete expired sessions from database
81 function session_purge_old() {
82         $now = time();
83         $exired_sessions = db_get_column('wfpl_sessions', 'id', 'where expires < %i', $now);
84         db_delete('wfpl_sessions', 'where expires < %i', $now);
85         if($expired_sessions) {
86                 foreach($expired_sessions as $expired_session) {
87                         db_delete('wfpl_session_data', 'where session_id=%i', $expired_session);
88                 }
89         }
90 }
91
92 # return true if a session exists
93 function session_exists() {
94         if(!isset($_REQUEST['session_key'])) {
95                 return false;
96         }
97
98         $session_key = ereg_replace('[^a-zA-Z0-9]', '', $_REQUEST['session_key']);
99
100         if(!strlen($session_key) == 16) {
101                 return false;
102         }
103
104         $GLOBALS['session_key'] = $session_key;
105
106         session_purge_old();
107         $id = db_get_value('wfpl_sessions', 'id', 'where session_key=%"', $session_key);
108         if($id === false) {
109                 return false;
110         }
111
112         $GLOBALS['session_id'] = $id;
113         return true;
114 }
115
116 # return username if a session exists and is authenticated
117 function session_exists_and_authed() {
118         if(!session_exists()) {
119                 return false;
120         }
121
122         return session_get('auth_username');
123 }
124
125
126 # find existing session, or make one
127 function init_session() {
128         if(!session_exists()) {
129                 session_new();
130         }
131 }
132
133 # delete the current session
134 function kill_session() {
135         if(!session_exists()) {
136                 return;
137         }
138         db_delete('wfpl_session_data', 'where session_id=%i', $GLOBALS['session_id']);
139         db_delete('wfpl_sessions', 'where id=%i', $GLOBALS['session_id']);
140 }
141
142 # save a variable into the session
143 function session_set($name, $value) {
144         db_delete('wfpl_session_data', 'where session_id=%i && name=%"', $GLOBALS['session_id'], $name);
145         db_insert('wfpl_session_data', 'session_id,name,value', $GLOBALS['session_id'], $name, $value);
146 }
147
148 # get a variable into the session
149 function session_get($name) {
150         return db_get_value('wfpl_session_data', 'value', 'where session_id=%i && name=%"', $GLOBALS['session_id'], $name);
151 }
152
153 ?>