| 1 | package OpenInteract::Session; | ||
| 2 | |||
| 3 | # $Id: Session.pm,v 1.13 2003/01/08 05:21:33 lachoy Exp $ | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use Data::Dumper qw( Dumper ); | ||
| 7 | |||
| 8 | $OpenInteract::Session::VERSION = sprintf("%d.%02d", q$Revision: 1.13 $ =~ /(\d+)\.(\d+)/); | ||
| 9 | |||
| 10 | $OpenInteract::Session::COOKIE_NAME = 'session'; | ||
| 11 | |||
| 12 | sub parse { | ||
| 13 | my ( $class, $session_id ) = @_; | ||
| 14 | my $R = OpenInteract::Request->instance; | ||
| 15 | $session_id ||= $R->{cookie}{in}{ $OpenInteract::Session::COOKIE_NAME }; | ||
| 16 | |||
| 17 | # If no session id, just create an empty hashref; when we go to | ||
| 18 | # save it, we check to see if the hashref is tied and if not, | ||
| 19 | # create a new session | ||
| 20 | |||
| 21 | unless ( $session_id ) { | ||
| 22 | $R->DEBUG && $R->scrib( 1, "No session_id found. Skipping session_init..." ); | ||
| 23 | $R->{session} = {}; | ||
| 24 | return undef; | ||
| 25 | } | ||
| 26 | $R->{session} = $class->_create_session( $session_id ); | ||
| 27 | unless ( $R->{session} ) { | ||
| 28 | $R->{session} = {}; | ||
| 29 | return undef; | ||
| 30 | } | ||
| 31 | $R->DEBUG && $R->scrib( 2, "Retrieved session properly: ", Dumper( $R->{session} ) ); | ||
| 32 | |||
| 33 | # Check to see if we should expire the session | ||
| 34 | |||
| 35 | unless ( $class->is_session_valid ) { | ||
| 36 | $R->DEBUG && $R->scrib( 1, "Session is expired; clearing out." ); | ||
| 37 | eval { tied( %{ $R->{session} } )->delete() }; | ||
| 38 | if ( $@ ) { | ||
| 39 | $R->scrib( 0, "Caught error trying to remove expired session: $@\n", | ||
| 40 | "Continuing without problem since this just means", | ||
| 41 | "you'll have a stale session in your datastore" ); | ||
| 42 | } | ||
| 43 | $R->{session} = {}; | ||
| 44 | return undef; | ||
| 45 | } | ||
| 46 | |||
| 47 | return 1; | ||
| 48 | } | ||
| 49 | |||
| 50 | |||
| 51 | sub is_session_valid { | ||
| 52 | my ( $class ) = @_; | ||
| 53 | my $R = OpenInteract::Request->instance; | ||
| 54 | return 1 unless ( $R->{session}{timestamp} ); | ||
| 55 | my $expires_in = $R->CONFIG->{session_info}{expires_in}; | ||
| 56 | return 1 unless ( $expires_in > 0 ); | ||
| 57 | my $last_refresh = ( time - $R->{session}{timestamp} ) / 60; | ||
| 58 | return 1 unless ( $last_refresh > $expires_in ); | ||
| 59 | $R->DEBUG && $R->scrib( 1, "Session has expired. Last refresh was ", | ||
| 60 | "[", sprintf( '%5.2f', $last_refresh ) , "] minutes ago at ", | ||
| 61 | "[", scalar localtime( $R->{session}{timestamp} ) , "]", | ||
| 62 | "and threshold is [", $expires_in, "] minutes" ); | ||
| 63 | return undef; | ||
| 64 | } | ||
| 65 | |||
| 66 | |||
| 67 | sub save { | ||
| 68 | my ( $class ) = @_; | ||
| 69 | my $R = OpenInteract::Request->instance; | ||
| 70 | my $CONFIG = $R->CONFIG; | ||
| 71 | |||
| 72 | if ( tied %{ $R->{session} } ) { | ||
| 73 | $R->{session}{timestamp} = $R->{time}; | ||
| 74 | $R->DEBUG && $R->scrib( 2, "Saving tied session\n", Dumper( $R->{session} ) ); | ||
| 75 | untie %{ $R->{session} }; | ||
| 76 | } | ||
| 77 | elsif ( ref( $R->{session} ) ) { | ||
| 78 | $R->DEBUG && $R->scrib( 1, "Session not yet created. Creating..." ); | ||
| 79 | unless ( scalar keys %{ $R->{session} } ) { | ||
| 80 | $R->DEBUG && $R->scrib( 1, "No session information saved. Exiting..." ); | ||
| 81 | return 1; | ||
| 82 | } | ||
| 83 | my $session = $class->_create_session; | ||
| 84 | if ( $session ) { | ||
| 85 | |||
| 86 | # Set the expiration. If the key 'expiration' is defined | ||
| 87 | # then use that, even if the value in the session is blank | ||
| 88 | # or undef. This allows you to set sessions that expire | ||
| 89 | # when the browser is closed when a user requests (see | ||
| 90 | # OpenInteract::Auth). | ||
| 91 | |||
| 92 | my ( $expiration ); | ||
| 93 | if ( exists $R->{session}{expiration} ) { | ||
| 94 | $expiration = $R->{session}{expiration}; | ||
| 95 | $R->DEBUG && $R->scrib( 1, "Expiration for new session manually set to ($expiration)" ); | ||
| 96 | delete $R->{session}{expiration}; | ||
| 97 | } | ||
| 98 | else { | ||
| 99 | $expiration = $CONFIG->{session_info}{expiration}; | ||
| 100 | $R->DEBUG && $R->scrib( 1, "Expiration for new session set to default from config ($expiration)" ); | ||
| 101 | } | ||
| 102 | |||
| 103 | # Set the session values | ||
| 104 | |||
| 105 | foreach my $key ( keys %{ $R->{session} } ) { | ||
| 106 | $session->{ $key } = $R->{session}{ $key }; | ||
| 107 | } | ||
| 108 | |||
| 109 | $session->{timestamp} = $R->{time}; | ||
| 110 | $R->cookies->create_cookie({ name => $OpenInteract::Session::COOKIE_NAME, | ||
| 111 | value => $session->{_session_id}, | ||
| 112 | path => '/', | ||
| 113 | expires => $expiration }); | ||
| 114 | $R->DEBUG && $R->scrib( 2, "Saving new session\n", Dumper( $session ) ); | ||
| 115 | untie %{ $session }; | ||
| 116 | } | ||
| 117 | else { | ||
| 118 | $R->scrib( 0, "Could not create session! See error log." ); | ||
| 119 | } | ||
| 120 | } | ||
| 121 | return 1; | ||
| 122 | } | ||
| 123 | |||
| 124 | sub _create_session { return undef } | ||
| 125 | |||
| 126 | 1; | ||
| 127 | |||
| 128 | __END__ |