JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
clean up my urls
[wfpl.git] / session.php
1 <?php
2
3 #  Copyright (C) 2006 Jason Woofenden
4 #
5 #  This program is free software: you can redistribute it and/or modify
6 #  it under the terms of the GNU General Public License as published by
7 #  the Free Software Foundation, either version 3 of the License, or
8 #  (at your option) any later version.
9 #  
10 #  This program is distributed in the hope that it will be useful,
11 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 #  GNU General Public License for more details.
14 #  
15 #  You should have received a copy of the GNU General Public License
16 #  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18
19 # you'll need these database tables:
20 # create table wfpl_sessions (id int unique auto_increment, session_key varchar(16), length int, expires int);
21 # create table wfpl_session_data (id int unique auto_increment, session_id int, name varchar(100), value text);
22 # run this command to install/clear the tables:
23 #   mysql DATABASE_NAME < inc/wfpl/examples/session.sql
24 # note: you may need these parameters for mysql:  -u USERNAME -p
25
26 # GLOSSARY
27 #
28 # session_key  16 digit string identifying the session
29 # session_id   integer id of the record in the "wfpl_sessions" table of the database
30 # UNTIL_CLOSE  a constant passed as session length to indicate "until browser window closes"
31
32
33 # session_id is kept in $GLOBALS
34 # session_key is sent as a cookie, and thus appears in $_REQUEST. The clean version is in $GLOBALS
35
36 # generate a new random 16-character string
37 function session_generate_key() {
38         $character_set = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
39         $id = "                ";
40
41         # PHP 4.2.0 and up seed the random number generator for you.
42         # Lets hope that it seeds with something harder to guess than the clock.
43         for($i = 0; $i < 16; ++$i) {
44                 $id{$i} = $character_set{mt_rand(0, 61)};
45         }
46
47         return $id;
48 }
49
50 # track this user with a session cookie (ie a cookie that goes away when the
51 # user closes the browser). The timestamp is how long to track the session in
52 # the database. Defaults to one day.
53 function session_new($length = 86400) {
54         $session_key = session_generate_key();
55
56         db_insert('wfpl_sessions', 'session_key,length', $session_key, $length);
57         $GLOBALS['session_id'] = db_auto_id();
58         $GLOBALS['session_key'] = $session_key;
59         $_COOKIE['session_key'] = $session_key; #just in case someone calls session_exists() after session_new()
60         session_touch($length);
61         return $GLOBALS['session_key'];
62 }
63
64 # call to renew the timeout for the session.
65 # assumes there's a session. call init_session() if you'd like one auto-create one if not found.
66 function session_touch($length = false) {
67         if(!$length) {
68                 $length = db_get_value('wfpl_sessions', 'length', 'where id=%i', $GLOBALS['session_id']);
69         }
70         $expires = time() + $length;
71
72         header('Set-Cookie: session_key=' . $GLOBALS['session_key'] . '; Path=/');
73
74         db_update('wfpl_sessions', 'expires', $expires, 'where id=%i', $GLOBALS['session_id']);
75 }
76
77 # delete the current session
78 function kill_session() {
79         if(!session_exists()) {
80             return;
81         }
82         _kill_session($GLOBALS['session_id']);
83 }
84
85 # for internal use. use kill_session() above
86 function _kill_session($id) {
87         db_delete('wfpl_session_data', 'where session_id=%i', $id);
88         db_delete('wfpl_sessions', 'where id=%i', $id);
89 }
90
91 # delete expired sessions from database
92 function session_purge_old() {
93         $now = time();
94         $expired_sessions = db_get_column('wfpl_sessions', 'id', 'where expires < %i', $now);
95         if($expired_sessions) foreach($expired_sessions as $expired_session) {
96                 _kill_session($expired_session);
97         }
98 }
99
100 # return true if a session exists
101 function session_exists() {
102         if(!isset($_COOKIE['session_key'])) {
103                 return false;
104         }
105
106         if(isset($GLOBALS['session_id'])) {
107                 return true;
108         }
109
110         $session_key = ereg_replace('[^a-zA-Z0-9]', '', $_COOKIE['session_key']);
111
112         if(!strlen($session_key) == 16) {
113                 return false;
114         }
115
116         $GLOBALS['session_key'] = $session_key;
117
118         session_purge_old();
119         $id = db_get_value('wfpl_sessions', 'id', 'where session_key=%"', $session_key);
120         if($id === false) {
121                 return false;
122         }
123
124         $GLOBALS['session_id'] = $id;
125         return true;
126 }
127
128 # depricated
129 function session_exists_and_authed() {
130         return logged_in();
131 }
132
133
134 # generate a random password using only letters and numbers that look
135 # particularly unique
136 function new_readable_password($length = 8) {
137         $character_set = "ABCDEFHJKLMNPQRTUVWXY34789";
138         $code = "";
139
140         # PHP 4.2.0 and up seed the random number generator for you.
141         # Lets hope that it seeds with something harder to guess than the clock.
142         while($length--) {
143                 $code .= $character_set{mt_rand(0, 25)}; # inclusive
144         }
145
146         return $code;
147 }
148
149 # return username if a session exists and is authenticated
150 function logged_in() {
151         if(!session_exists()) {
152                 return false;
153         }
154
155         return session_get('auth_username');
156 }
157
158
159
160 # return true if a session exists and is authenticated
161 function logged_in_as_admin() {
162         if(!session_exists()) {
163                 return false;
164         }
165
166         if(session_get('auth_admin')) {
167                 return true;
168         }
169         return false;
170 }
171
172
173 # find existing session, or make one (name "session_init" was taken)
174 function init_session() {
175         if(!session_exists()) {
176                 session_new();
177         }
178 }
179
180 # save a variable into the session
181 function session_set($name, $value) {
182         session_clear($name);
183         db_insert('wfpl_session_data', 'session_id,name,value', $GLOBALS['session_id'], $name, $value);
184 }
185
186 # remove variable from the session
187 function session_clear($name) {
188         db_delete('wfpl_session_data', 'where session_id=%i && name=%"', $GLOBALS['session_id'], $name);
189 }
190
191 # get a variable into the session
192 function session_get($name) {
193         return db_get_value('wfpl_session_data', 'value', 'where session_id=%i && name=%"', $GLOBALS['session_id'], $name);
194 }
195
196 ?>