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