File Coverage

blib/lib/AxKit/XSP/Session.pm
Criterion Covered Total %
statement 4 6 66.6
branch n/a
condition n/a
subroutine 2 2 100.0
pod n/a
total 6 8 75.0


line stmt bran cond sub pod time code
1              
2             package AxKit::XSP::Session;
3 1     1   822 use strict;
  1         2  
  1         43  
4 1     1   1901 use Apache::AxKit::Language::XSP;
  0            
  0            
5             use Apache::Session;
6             use Date::Format;
7             use Apache;
8             use Carp;
9              
10             use vars qw/@ISA $NS $VERSION $SESSION/;
11              
12             @ISA = ('Apache::AxKit::Language::XSP');
13             $NS = 'http://www.apache.org/1999/XSP/Session';
14              
15             $VERSION = "0.11";
16              
17             ## Taglib subs
18              
19             sub get_attribute
20             {
21             my ( $attribute ) = @_;
22             $attribute =~ s/^\s*//;
23             $attribute =~ s/\s*$//;
24             my $r = Apache->request;
25             my $session = $r->pnotes('xsp_session');
26             return $session->{$attribute};
27             }
28              
29             sub get_attribute_names
30             {
31             my $r = Apache->request;
32             my $session = $r->pnotes('xsp_session');
33             return keys(%$session);
34             }
35              
36             sub get_id
37             {
38             my $r = Apache->request;
39             my $session = $r->pnotes('xsp_session');
40             return $session->{_session_id};
41             }
42              
43             # I've changed the API for this particular tag. The Cocoon XSP definition for get-creation-time
44             # really stinks. The default first of all, is "long", which is basically number of seconds since
45             # epoch. It also has support for "node" output, which basically is the same as "long", except it
46             # outputs the value in a hard-coded XML tag. If there's any merit to the node output, someone
47             # please tell me; otherwise, I'll leave it out.
48             sub get_creation_time
49             {
50             my ( $as ) = @_;
51             my $r = Apache->request;
52             my $session = $r->pnotes('xsp_session');
53             return _get_time( $as, $session->{_creation_time} );
54             }
55              
56             # Ahh, the wonders of "Copy-Paste(tm)" technology! Since it's late at night, and this
57             # XSP taglib is just a means-to-an-end for me, I'm just copying the 'get_creation_time'
58             # subroutine. If anyone feels so inclined, you're welcome to clean this up. :)
59             sub get_last_accessed_time
60             {
61             my ( $as ) = @_;
62             my $r = Apache->request;
63             # This isn't necessary anymore
64             # TODO update the documentation to reflect this
65             #return undef unless ( $r->dir_config( 'UseSessionAccessTimestamp' ) =~ /on/i );
66              
67             my $session = $r->pnotes('xsp_session');
68             return _get_time( $as, $session->{_last_accessed_time} );
69             }
70              
71             sub _get_time
72             {
73             my ( $as, $time ) = @_;
74             $as = 'string' unless ( $as );
75             my $formatted_time = undef;
76             if ( $as eq 'long' )
77             {
78             return $time;
79             }
80             elsif ( $as eq 'string' )
81             {
82             # Outputs a string like "Wed Jun 13 15:57:06 EDT 2001"
83             return time2str('%a %b %d %H:%M:%S %Z %Y', $time);
84             }
85             }
86              
87             sub set_attribute
88             {
89             my ( $attribute, $value ) = @_;
90             $attribute =~ s/^\s*//;
91             $attribute =~ s/\s*$//;
92             $value =~ s/^\s*//;
93             $value =~ s/\s*$//;
94             # exit out if they try to set any magic keys
95             return if ( $attribute =~ /^_/ );
96             #print STDERR "set-attribute: \$attribute=\"$attribute\", \$value=\"$value\".\n";
97              
98             my $r = Apache->request;
99             my $session = $r->pnotes('xsp_session');
100             $session->{$attribute} = $value;
101             return;
102             }
103              
104             sub remove_attribute
105             {
106             my ( $attribute ) = @_;
107             $attribute =~ s/^\s*//;
108             $attribute =~ s/\s*$//;
109             # exit out if they try to set any magic keys
110             return if ( $attribute =~ /^_/ );
111             #print STDERR "remove-attribute: \$attribute=\"$attribute\".\n";
112              
113             my $r = Apache->request;
114             my $session = $r->pnotes('xsp_session');
115             delete $session->{$attribute};
116             #print STDERR "remove-attribute: value=\"" . $session->{$attribute} . "\".\n";
117             return;
118             }
119              
120             sub invalidate
121             {
122             my $r = Apache->request;
123             my $session_obj = $r->pnotes('xsp_session_ref');
124             $session_obj->delete;
125             return;
126             }
127              
128             ## Parser subs
129              
130             sub parse_start {
131             my ($e, $tag, %attribs) = @_;
132             #warn "Checking: $tag\n";
133              
134             if ($tag eq 'get-attribute')
135             {
136             my $code = '{ my ($name);';
137             if ( $attribs{name} )
138             {
139             $code .= '$name = q|' . $attribs{name} . '|;';
140             }
141             return $code;
142             }
143              
144             elsif ($tag eq 'name')
145             {
146             return '$name = ""';
147             }
148              
149             elsif ($tag eq 'value')
150             {
151             return '$value = ""';
152             }
153              
154             elsif ($tag eq 'get-id')
155             {
156             return q|{ AxKit::XSP::Session::get_id(); }|;
157             }
158              
159             elsif ($tag eq 'get-creation-time')
160             {
161             my $code = '{ my $as = ';
162             if ( defined( $attribs{as} ) )
163             {
164             $code .= 'q|' . $attribs{as} . '|;';
165             }
166             else
167             {
168             $code .= 'undef;';
169             }
170             $code .= ' AxKit::XSP::Session::get_creation_time( $as ); }';
171             }
172              
173             elsif ($tag eq 'get-last-accessed-time')
174             {
175             my $code = '{ my $as = ';
176             if ( defined( $attribs{as} ) )
177             {
178             $code .= 'q|' . $attribs{as} . '|;';
179             }
180             else
181             {
182             $code .= 'undef;';
183             }
184             $code .= ' AxKit::XSP::Session::get_last_accessed_time( $as ); }';
185             }
186              
187             elsif ($tag eq 'invalidate')
188             {
189             return q|{ AxKit::XSP::Session::invalidate(); }|;
190             }
191              
192             elsif ($tag eq 'remove-attribute')
193             {
194             my $code = '{ my ($name);';
195             if ( $attribs{name} )
196             {
197             $code .= '$name = q|' . $attribs{name} . '|;';
198             }
199             return $code;
200             }
201              
202             elsif ($tag eq 'set-attribute')
203             {
204             my $code = '{ my ($name, $value);';
205             if ( $attribs{name} )
206             {
207             $code .= '$name = q|' . $attribs{name} . '|;';
208             }
209             if ( $attribs{value} )
210             {
211             $code .= '$value = q|' . $attribs{value} . '|;';
212             }
213             return $code;
214             }
215              
216             else {
217             die "Unknown session tag: $tag";
218             }
219             }
220              
221             sub parse_char {
222             my ($e, $text) = @_;
223             return '' unless $text =~ /\S/;
224              
225             $text =~ s/\|/\\\|/g;
226             $text =~ s/\\$/\\\\/gsm;
227             return " . q|$text| ";
228             }
229              
230             sub parse_end {
231             my ($e, $tag) = @_;
232              
233             if ($tag =~ /name|value/)
234             {
235             return ';';
236             }
237              
238             elsif ($tag eq 'get-attribute')
239             {
240             return 'AxKit::XSP::Session::get_attribute($name);}' . "\n";
241             }
242              
243             elsif ($tag eq 'remove-attribute')
244             {
245             return 'AxKit::XSP::Session::remove_attribute($name);}' . "\n";
246             }
247              
248             elsif ($tag eq 'set-attribute')
249             {
250             return 'AxKit::XSP::Session::set_attribute($name, $value);}' . "\n";
251             }
252              
253             return ";";
254             }
255              
256             sub parse_comment {
257             # compat only
258             }
259              
260             sub parse_final {
261             # compat only
262             }
263              
264             1;
265            
266             __END__