| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | # | 
| 2 |  |  |  |  |  |  | # | 
| 3 |  |  |  |  |  |  | #  Copyright (C) 2006-2010 Andrew Speer . | 
| 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 |  |  | 1 |  | 37192 | sub BEGIN	{ $^W=0 }; | 
| 29 | 1 |  |  | 1 |  | 10 | use strict	qw(vars); | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 31 |  | 
| 30 | 1 |  |  | 1 |  | 5 | use vars	qw($VERSION); | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 54 |  | 
| 31 | 1 |  |  | 1 |  | 10 | use warnings; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 39 |  | 
| 32 | 1 |  |  | 1 |  | 6 | no  warnings	qw(uninitialized); | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 38 |  | 
| 33 |  |  |  |  |  |  |  | 
| 34 |  |  |  |  |  |  |  | 
| 35 |  |  |  |  |  |  | #  WebDyne Modules. | 
| 36 |  |  |  |  |  |  | # | 
| 37 |  |  |  |  |  |  |  | 
| 38 | 1 |  |  | 1 |  | 671 | use WebDyne::Session::Constant; | 
|  | 1 |  |  |  |  | 3 |  | 
|  | 1 |  |  |  |  | 182 |  | 
| 39 | 1 |  |  | 1 |  | 10 | use WebDyne::Base; | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 9 |  | 
| 40 |  |  |  |  |  |  |  | 
| 41 |  |  |  |  |  |  |  | 
| 42 |  |  |  |  |  |  | #  External modules | 
| 43 |  |  |  |  |  |  | # | 
| 44 | 1 |  |  | 1 |  | 127 | use Digest::MD5 qw(md5_hex); | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 56 |  | 
| 45 | 1 |  |  | 1 |  | 1384 | use CGI::Cookie; | 
|  | 1 |  |  |  |  | 10891 |  | 
|  | 1 |  |  |  |  | 713 |  | 
| 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 | 1 |  | 50 | 1 |  | 40 | my $self_cr=UNIVERSAL::can(scalar caller, 'self') || return; | 
| 78 | 0 |  | 0 |  |  |  | my $self=$self_cr->() || return; | 
| 79 |  |  |  |  |  |  | #$self->set_handler('WebDyne::Session'); | 
| 80 | 0 |  |  |  |  |  | $self->set_handler('WebDyne::Chain'); | 
| 81 | 0 |  |  |  |  |  | my $meta_hr=$self->meta(); | 
| 82 | 0 |  |  |  |  |  | push @{$meta_hr->{'webdynechain'}}, __PACKAGE__; | 
|  | 0 |  |  |  |  |  |  | 
| 83 |  |  |  |  |  |  |  | 
| 84 |  |  |  |  |  |  |  | 
| 85 |  |  |  |  |  |  | } | 
| 86 |  |  |  |  |  |  |  | 
| 87 |  |  |  |  |  |  |  | 
| 88 |  |  |  |  |  |  | sub handler : method { | 
| 89 |  |  |  |  |  |  |  | 
| 90 |  |  |  |  |  |  |  | 
| 91 |  |  |  |  |  |  | #  Get class, request object | 
| 92 |  |  |  |  |  |  | # | 
| 93 | 0 |  |  | 0 | 0 |  | my ($self, $r, $param_hr)=@_; | 
| 94 |  |  |  |  |  |  |  | 
| 95 |  |  |  |  |  |  |  | 
| 96 |  |  |  |  |  |  | #  Debug | 
| 97 |  |  |  |  |  |  | # | 
| 98 | 0 |  |  |  |  |  | 0 && debug("in %s handler, self $self, r $r, param_hr $param_hr", __PACKAGE__); | 
| 99 |  |  |  |  |  |  |  | 
| 100 |  |  |  |  |  |  |  | 
| 101 |  |  |  |  |  |  | #  Get cookie from header | 
| 102 |  |  |  |  |  |  | # | 
| 103 | 0 |  | 0 |  |  |  | my $header_hr=$r->headers_in() || | 
| 104 |  |  |  |  |  |  | return err('unable to get header hash ref'); | 
| 105 | 0 |  |  |  |  |  | my $cookie=$header_hr->{'cookie'}; | 
| 106 | 0 |  |  |  |  |  | 0 && debug("cookie $cookie"); | 
| 107 |  |  |  |  |  |  |  | 
| 108 |  |  |  |  |  |  |  | 
| 109 |  |  |  |  |  |  | #  Get cookies hash | 
| 110 |  |  |  |  |  |  | # | 
| 111 | 0 | 0 |  |  |  |  | my %cookies=$cookie ? CGI::Cookie->parse($cookie) : (); | 
| 112 |  |  |  |  |  |  |  | 
| 113 |  |  |  |  |  |  |  | 
| 114 |  |  |  |  |  |  | #  Get cookie name we are looking for | 
| 115 |  |  |  |  |  |  | # | 
| 116 | 0 |  |  |  |  |  | my $cookie_name=$WEBDYNE_SESSION_ID_COOKIE_NAME; | 
| 117 |  |  |  |  |  |  |  | 
| 118 |  |  |  |  |  |  |  | 
| 119 |  |  |  |  |  |  | #  Get or set the cookie id | 
| 120 |  |  |  |  |  |  | # | 
| 121 | 0 |  |  |  |  |  | my $session_id; | 
| 122 | 0 | 0 | 0 |  |  |  | unless ($session_id=($cookies{$cookie_name} && $cookies{$cookie_name}->value())) { | 
| 123 |  |  |  |  |  |  |  | 
| 124 |  |  |  |  |  |  |  | 
| 125 |  |  |  |  |  |  | #  Debug | 
| 126 |  |  |  |  |  |  | # | 
| 127 | 0 |  |  |  |  |  | 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 | 0 |  |  |  |  |  | $session_id=&Digest::MD5::md5_hex(rand($$.time())); | 
| 133 | 0 |  |  |  |  |  | 0 && debug("generated new session_id $session_id"); | 
| 134 |  |  |  |  |  |  |  | 
| 135 |  |  |  |  |  |  |  | 
| 136 |  |  |  |  |  |  |  | 
| 137 |  |  |  |  |  |  | #  If no session id now, something has gone horribly wrong | 
| 138 |  |  |  |  |  |  | # | 
| 139 | 0 | 0 |  |  |  |  | $session_id || return $self->err_html( | 
| 140 |  |  |  |  |  |  | 'unable to create unique session id'); | 
| 141 |  |  |  |  |  |  |  | 
| 142 |  |  |  |  |  |  |  | 
| 143 |  |  |  |  |  |  | #  Debug | 
| 144 |  |  |  |  |  |  | # | 
| 145 | 0 |  |  |  |  |  | 0 && debug("session_id generation success, generated id $session_id"); | 
| 146 |  |  |  |  |  |  |  | 
| 147 |  |  |  |  |  |  |  | 
| 148 |  |  |  |  |  |  |  | 
| 149 |  |  |  |  |  |  | #  Create a cookie with out session id | 
| 150 |  |  |  |  |  |  | # | 
| 151 | 0 |  | 0 |  |  |  | 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 | 0 |  | 0 |  |  |  | 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 | 0 |  |  |  |  |  | $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 | 0 |  |  |  |  |  | $self->{'_session_id'}=$session_id; | 
| 178 |  |  |  |  |  |  |  | 
| 179 |  |  |  |  |  |  |  | 
| 180 |  |  |  |  |  |  | #  All done, chain to next handler | 
| 181 |  |  |  |  |  |  | # | 
| 182 | 0 |  |  |  |  |  | $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 | 0 |  |  | 0 | 0 |  | my $self=shift(); | 
| 195 | 0 |  |  |  |  |  | return $self->{'_session_id'}; | 
| 196 |  |  |  |  |  |  |  | 
| 197 |  |  |  |  |  |  |  | 
| 198 |  |  |  |  |  |  | } | 
| 199 |  |  |  |  |  |  |  | 
| 200 |  |  |  |  |  |  | __END__ |