| 1 | # | ||
| 2 | # | ||
| 3 | # Copyright (C) 2006-2010 Andrew Speer <andrew@webdyne.org>. | ||
| 4 | # All rights reserved. | ||
| 5 | # | ||
| 6 | # This file is part of WebDyne::Session. | ||
| 7 | # | ||
| 8 | # WebDyne::Session is free software; you can redistribute it and/or modify | ||
| 9 | # it under the terms of the GNU General Public License as published by | ||
| 10 | # the Free Software Foundation; either version 2 of the License, or | ||
| 11 | # (at your option) any later version. | ||
| 12 | # | ||
| 13 | # This program is distributed in the hope that it will be useful, | ||
| 14 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 16 | # GNU General Public License for more details. | ||
| 17 | # | ||
| 18 | # You should have received a copy of the GNU General Public License | ||
| 19 | # along with this program; if not, write to the Free Software | ||
| 20 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
| 21 | # | ||
| 22 | # | ||
| 23 | package WebDyne::Session; | ||
| 24 | |||
| 25 | |||
| 26 | # Compiler Pragma | ||
| 27 | # | ||
| 28 | 1 | ▶ | sub BEGIN { $^W=0 }; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 29 | 1 | ▶ | use strict qw(vars); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 30 | 1 | ▶ | use vars qw($VERSION); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 31 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 32 | 1 | ▶ | no warnings qw(uninitialized); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 33 | |||
| 34 | |||
| 35 | # WebDyne Modules. | ||
| 36 | # | ||
| 37 | |||
| 38 | 1 | ▶ | use WebDyne::Session::Constant; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 39 | 1 | ▶ | use WebDyne::Base; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 40 | |||
| 41 | |||
| 42 | # External modules | ||
| 43 | # | ||
| 44 | use Digest::MD5 qw(md5_hex); | ||
| 45 | use CGI::Cookie; | ||
| 46 | |||
| 47 | |||
| 48 | # Version information | ||
| 49 | # | ||
| 50 | $VERSION='1.044'; | ||
| 51 | |||
| 52 | |||
| 53 | # Shortcut error handler. | ||
| 54 | # | ||
| 55 | require WebDyne::Err; | ||
| 56 | *err_html=\&WebDyne::Err::err_html || *err_html; | ||
| 57 | |||
| 58 | |||
| 59 | # Debug | ||
| 60 | # | ||
| 61 | 0 && debug("%s loaded, version $VERSION", __PACKAGE__); | ||
| 62 | |||
| 63 | |||
| 64 | # And done | ||
| 65 | # | ||
| 66 | 1; | ||
| 67 | |||
| 68 | |||
| 69 | #------------------------------------------------------------------------------ | ||
| 70 | |||
| 71 | |||
| 72 | sub import { | ||
| 73 | |||
| 74 | |||
| 75 | # Will only work if called from within a __PERL__ block in WebDyne | ||
| 76 | # | ||
| 77 | my $self_cr=UNIVERSAL::can(scalar caller, 'self') || return; | ||
| 78 | my $self=$self_cr->() || return; | ||
| 79 | #$self->set_handler('WebDyne::Session'); | ||
| 80 | $self->set_handler('WebDyne::Chain'); | ||
| 81 | my $meta_hr=$self->meta(); | ||
| 82 | push @{$meta_hr->{'webdynechain'}}, __PACKAGE__; | ||
| 83 | |||
| 84 | |||
| 85 | } | ||
| 86 | |||
| 87 | |||
| 88 | sub handler : method { | ||
| 89 | |||
| 90 | |||
| 91 | # Get class, request object | ||
| 92 | # | ||
| 93 | my ($self, $r, $param_hr)=@_; | ||
| 94 | |||
| 95 | |||
| 96 | # Debug | ||
| 97 | # | ||
| 98 | 0 && debug("in %s handler, self $self, r $r, param_hr $param_hr", __PACKAGE__); | ||
| 99 | |||
| 100 | |||
| 101 | # Get cookie from header | ||
| 102 | # | ||
| 103 | my $header_hr=$r->headers_in() || | ||
| 104 | return err('unable to get header hash ref'); | ||
| 105 | my $cookie=$header_hr->{'cookie'}; | ||
| 106 | 0 && debug("cookie $cookie"); | ||
| 107 | |||
| 108 | |||
| 109 | # Get cookies hash | ||
| 110 | # | ||
| 111 | my %cookies=$cookie ? CGI::Cookie->parse($cookie) : (); | ||
| 112 | |||
| 113 | |||
| 114 | # Get cookie name we are looking for | ||
| 115 | # | ||
| 116 | my $cookie_name=$WEBDYNE_SESSION_ID_COOKIE_NAME; | ||
| 117 | |||
| 118 | |||
| 119 | # Get or set the cookie id | ||
| 120 | # | ||
| 121 | my $session_id; | ||
| 122 | unless ($session_id=($cookies{$cookie_name} && $cookies{$cookie_name}->value())) { | ||
| 123 | |||
| 124 | |||
| 125 | # Debug | ||
| 126 | # | ||
| 127 | 0 && debug('session cookie not found, generating new session_id'); | ||
| 128 | |||
| 129 | |||
| 130 | # Generate a new session id based on an MD5 checksum | ||
| 131 | # | ||
| 132 | $session_id=&Digest::MD5::md5_hex(rand($$.time())); | ||
| 133 | 0 && debug("generated new session_id $session_id"); | ||
| 134 | |||
| 135 | |||
| 136 | |||
| 137 | # If no session id now, something has gone horribly wrong | ||
| 138 | # | ||
| 139 | $session_id || return $self->err_html( | ||
| 140 | 'unable to create unique session id'); | ||
| 141 | |||
| 142 | |||
| 143 | # Debug | ||
| 144 | # | ||
| 145 | 0 && debug("session_id generation success, generated id $session_id"); | ||
| 146 | |||
| 147 | |||
| 148 | |||
| 149 | # Create a cookie with out session id | ||
| 150 | # | ||
| 151 | my $cookie=CGI::Cookie->new( | ||
| 152 | |||
| 153 | -name => $cookie_name, | ||
| 154 | -value => $session_id, | ||
| 155 | -path => '/' | ||
| 156 | |||
| 157 | ) || return $self->err_html("unable to generate sid: $session_id cookie"); | ||
| 158 | |||
| 159 | |||
| 160 | # Get our header hash ref | ||
| 161 | # | ||
| 162 | my $header_hr=$r->headers_out() || | ||
| 163 | return $self->err_html('unable to get outbound headers'); | ||
| 164 | |||
| 165 | |||
| 166 | # Reinstall the new cookie into the params that will be passed | ||
| 167 | # to our base header function | ||
| 168 | # | ||
| 169 | $header_hr->{'Set-cookie'}=$cookie; | ||
| 170 | |||
| 171 | |||
| 172 | } | ||
| 173 | |||
| 174 | |||
| 175 | # Set in class _self area so will be propogated to next blessed self ref | ||
| 176 | # | ||
| 177 | $self->{'_session_id'}=$session_id; | ||
| 178 | |||
| 179 | |||
| 180 | # All done, chain to next handler | ||
| 181 | # | ||
| 182 | $self->SUPER::handler($r, @_[2..$#_]); | ||
| 183 | |||
| 184 | |||
| 185 | } | ||
| 186 | |||
| 187 | |||
| 188 | |||
| 189 | sub session_id { | ||
| 190 | |||
| 191 | |||
| 192 | # Accessor for session_id var, set in handler above | ||
| 193 | # | ||
| 194 | my $self=shift(); | ||
| 195 | return $self->{'_session_id'}; | ||
| 196 | |||
| 197 | |||
| 198 | } | ||
| 199 | |||
| 200 | __END__ | ||