line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
#$Id: Session.pm 292 2008-06-15 08:24:28Z zag $ |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
package HTML::WebDAO::Session; |
4
|
5
|
|
|
5
|
|
24
|
use HTML::WebDAO::Base; |
|
5
|
|
|
|
|
64
|
|
|
5
|
|
|
|
|
259
|
|
5
|
5
|
|
|
5
|
|
2714
|
use HTML::WebDAO::CVcgi; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
use HTML::WebDAO::Store::Abstract; |
7
|
|
|
|
|
|
|
use HTML::WebDAO::Response; |
8
|
|
|
|
|
|
|
use Data::Dumper; |
9
|
|
|
|
|
|
|
use base qw( HTML::WebDAO::Base ); |
10
|
|
|
|
|
|
|
use Encode qw(encode decode is_utf8); |
11
|
|
|
|
|
|
|
use strict; |
12
|
|
|
|
|
|
|
__PACKAGE__->attributes |
13
|
|
|
|
|
|
|
qw( Cgi_obj Cgi_env U_id Header Params _store_obj _response_obj _is_absolute_url); |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub _init() { |
16
|
|
|
|
|
|
|
my $self = shift; |
17
|
|
|
|
|
|
|
$self->Init(@_); |
18
|
|
|
|
|
|
|
return 1; |
19
|
|
|
|
|
|
|
} |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
#Need to be forever called from over classes; |
22
|
|
|
|
|
|
|
sub Init { |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
#Parametrs is realm |
25
|
|
|
|
|
|
|
my $self = shift; |
26
|
|
|
|
|
|
|
my %args = @_; |
27
|
|
|
|
|
|
|
Header $self ( {} ); |
28
|
|
|
|
|
|
|
U_id $self undef; |
29
|
|
|
|
|
|
|
Cgi_obj $self $args{cv} |
30
|
|
|
|
|
|
|
|| new HTML::WebDAO::CVcgi::; #create default controller |
31
|
|
|
|
|
|
|
my $cv = $self->Cgi_obj; # Store Cgi_obj in local var |
32
|
|
|
|
|
|
|
#create response object |
33
|
|
|
|
|
|
|
$self->_response_obj( |
34
|
|
|
|
|
|
|
new HTML::WebDAO::Response:: |
35
|
|
|
|
|
|
|
session => $self, |
36
|
|
|
|
|
|
|
cv => $cv |
37
|
|
|
|
|
|
|
); |
38
|
|
|
|
|
|
|
_store_obj $self ( $args{store} || new HTML::WebDAO::Store::Abstract:: ); |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
#workaround for CGI.pm: http://rt.cpan.org/Ticket/Display.html?id=36435 |
41
|
|
|
|
|
|
|
my %accept = (); |
42
|
|
|
|
|
|
|
if ( $cv->http('accept') ) { |
43
|
|
|
|
|
|
|
%accept = map { $_ => $cv->Accept($_) } $cv->Accept(); |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
Cgi_env $self ( |
46
|
|
|
|
|
|
|
{ |
47
|
|
|
|
|
|
|
url => $cv->url( -base => 1 ), #http://eng.zag |
48
|
|
|
|
|
|
|
path_info => $cv->url( -absolute => 1, -path_info => 1 ), |
49
|
|
|
|
|
|
|
path_info_elments => [], |
50
|
|
|
|
|
|
|
file => "", |
51
|
|
|
|
|
|
|
base_url => $cv->url( -base => 1 ), #http://base.com |
52
|
|
|
|
|
|
|
query_string => $cv->query_string, |
53
|
|
|
|
|
|
|
referer => $cv->referer(), |
54
|
|
|
|
|
|
|
accept => \%accept |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
); |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
#fix CGI.pm bug http://rt.cpan.org/Ticket/Display.html?id=25908 |
59
|
|
|
|
|
|
|
$self->Cgi_env->{path_info} =~ s/\?.*//s; |
60
|
|
|
|
|
|
|
$self->get_id; |
61
|
|
|
|
|
|
|
Params $self ( $self->_get_params() ); |
62
|
|
|
|
|
|
|
$self->Cgi_env->{path_info_elments} = |
63
|
|
|
|
|
|
|
[ grep { defined $_ } split( /\//, $self->Cgi_env->{path_info} ) ]; |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
#Can be overlap if you choose another |
68
|
|
|
|
|
|
|
#alghoritm generate unique session ID (i.e cookie,http_auth) |
69
|
|
|
|
|
|
|
sub get_id { |
70
|
|
|
|
|
|
|
my $self = shift; |
71
|
|
|
|
|
|
|
my $coo = U_id $self; |
72
|
|
|
|
|
|
|
return $coo if ($coo); |
73
|
|
|
|
|
|
|
return rand(100); |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
=head2 call_path [$url] |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
Return ref to array of element from $url or from CGI ENV |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
=cut |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
sub call_path { |
83
|
|
|
|
|
|
|
my $self = shift; |
84
|
|
|
|
|
|
|
my $url = shift || return $self->Cgi_env->{path_info_elments}; |
85
|
|
|
|
|
|
|
$url =~ s%^/%%; |
86
|
|
|
|
|
|
|
$url =~ s%/$%%; |
87
|
|
|
|
|
|
|
return [ grep { defined $_ } split( /\//, $url ) ]; |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
=head2 set_absolute_url 1|0 |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
Set flag for build absolute pathes. Return previus value. |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
=cut |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
sub set_absolute_url { |
98
|
|
|
|
|
|
|
my $self = shift; |
99
|
|
|
|
|
|
|
my $value = shift; |
100
|
|
|
|
|
|
|
my $prev_value = $self->_is_absolute_url; |
101
|
|
|
|
|
|
|
$self->_is_absolute_url($value) if defined $value; |
102
|
|
|
|
|
|
|
return $prev_value; |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
sub _load_attributes_by_path { |
106
|
|
|
|
|
|
|
my $self = shift; |
107
|
|
|
|
|
|
|
$self->_store_obj->_load_attributes( $self->get_id(), @_ ); |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
sub _store_attributes_by_path { |
111
|
|
|
|
|
|
|
my $self = shift; |
112
|
|
|
|
|
|
|
$self->_store_obj->_store_attributes( $self->get_id(), @_ ); |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
sub flush_session { |
116
|
|
|
|
|
|
|
my $self = shift; |
117
|
|
|
|
|
|
|
$self->_store_obj->flush( $self->get_id() ); |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
sub response_obj { |
121
|
|
|
|
|
|
|
my $self = shift; |
122
|
|
|
|
|
|
|
return $self->_response_obj; |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
#Session interface to device(HTTP protocol) specific function |
126
|
|
|
|
|
|
|
#$self->SendEvent("_sess_servise",{ |
127
|
|
|
|
|
|
|
# funct => geturl, |
128
|
|
|
|
|
|
|
# par => $ref, |
129
|
|
|
|
|
|
|
# result => \$res |
130
|
|
|
|
|
|
|
#}); |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
sub sess_servise { |
133
|
|
|
|
|
|
|
my ( $self, $event_name, $par ) = @_; |
134
|
|
|
|
|
|
|
my %service = ( |
135
|
|
|
|
|
|
|
geturl => sub { $self->sess_servise_geturl(@_) }, |
136
|
|
|
|
|
|
|
getenv => sub { $self->sess_servise_getenv(@_) }, |
137
|
|
|
|
|
|
|
getsess => sub { return $self }, |
138
|
|
|
|
|
|
|
); |
139
|
|
|
|
|
|
|
if ( exists( $service{ $par->{funct} } ) ) { |
140
|
|
|
|
|
|
|
${ $par->{result} } = $service{ $par->{funct} }->( $par->{par} ); |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
else { |
143
|
|
|
|
|
|
|
logmsgs $self "not exist request funct !" . $par->{funct}; |
144
|
|
|
|
|
|
|
} |
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
# |
148
|
|
|
|
|
|
|
#{variable=>{ |
149
|
|
|
|
|
|
|
# name=>Par, |
150
|
|
|
|
|
|
|
# value=>"10"}, |
151
|
|
|
|
|
|
|
#event =>{ |
152
|
|
|
|
|
|
|
# name=>"_info_on", |
153
|
|
|
|
|
|
|
# value=>"10" |
154
|
|
|
|
|
|
|
# }}) |
155
|
|
|
|
|
|
|
sub sess_servise_geturl { |
156
|
|
|
|
|
|
|
my ( $self, $par ) = @_; |
157
|
|
|
|
|
|
|
my $str; |
158
|
|
|
|
|
|
|
$str = $par->{path} || ''; |
159
|
|
|
|
|
|
|
if ( exists( $par->{event} ) ) { |
160
|
|
|
|
|
|
|
$str .= "ev/evn_" |
161
|
|
|
|
|
|
|
. $par->{event}->{name} . "/" |
162
|
|
|
|
|
|
|
. $par->{event}->{value} . "/"; |
163
|
|
|
|
|
|
|
} |
164
|
|
|
|
|
|
|
if ( exists( $par->{variable} ) ) { |
165
|
|
|
|
|
|
|
$par->{variable}->{name} =~ s/\./\//g; |
166
|
|
|
|
|
|
|
$str .= "par/" |
167
|
|
|
|
|
|
|
. $par->{variable}->{name} . "/" |
168
|
|
|
|
|
|
|
. $par->{variable}->{value} . "/"; |
169
|
|
|
|
|
|
|
} |
170
|
|
|
|
|
|
|
$str .= ( exists( $par->{file} ) ) ? $par->{file} : $self->Cgi_env->{file}; |
171
|
|
|
|
|
|
|
if ( ref( $par->{pars} ) eq 'HASH' ) { |
172
|
|
|
|
|
|
|
my @pars; |
173
|
|
|
|
|
|
|
while ( my ( $key, $val ) = each %{ $par->{pars} } ) { |
174
|
|
|
|
|
|
|
push @pars, "$key=$val"; |
175
|
|
|
|
|
|
|
} |
176
|
|
|
|
|
|
|
$str .= "?" . join "&" => @pars; |
177
|
|
|
|
|
|
|
} |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
#set absolute path |
180
|
|
|
|
|
|
|
$str = $self->Cgi_env->{base_url} . $str if $self->set_absolute_url; |
181
|
|
|
|
|
|
|
return $str; |
182
|
|
|
|
|
|
|
} |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
#get current session enviro-ent |
185
|
|
|
|
|
|
|
sub sess_servise_getenv { |
186
|
|
|
|
|
|
|
my ($self) = @_; |
187
|
|
|
|
|
|
|
return $self->Cgi_env; |
188
|
|
|
|
|
|
|
} |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
sub response { |
191
|
|
|
|
|
|
|
my $self = shift; |
192
|
|
|
|
|
|
|
my $res = shift; |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
# unless $res->type |
195
|
|
|
|
|
|
|
return if $res->{cleared}; |
196
|
|
|
|
|
|
|
my $headers = $self->Header(); |
197
|
|
|
|
|
|
|
$headers->{-TYPE} = $res->{type} if $res->{type}; |
198
|
|
|
|
|
|
|
while ( my ( $key, $val ) = each %$headers ) { |
199
|
|
|
|
|
|
|
my $UKey = uc $key; |
200
|
|
|
|
|
|
|
$res->{headers}->{$UKey} = $headers->{$UKey} |
201
|
|
|
|
|
|
|
unless exists $res->{headers}->{$UKey}; |
202
|
|
|
|
|
|
|
} |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
# $res->{headers} = $headers; |
205
|
|
|
|
|
|
|
$self->Cgi_obj->response($res); |
206
|
|
|
|
|
|
|
} |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
sub print { |
209
|
|
|
|
|
|
|
my $self = shift; |
210
|
|
|
|
|
|
|
$self->Cgi_obj->print(@_); |
211
|
|
|
|
|
|
|
} |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
sub ExecEngine() { |
214
|
|
|
|
|
|
|
my ( $self, $eng_ref ) = @_; |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
#print $self->print_header(); |
217
|
|
|
|
|
|
|
$eng_ref->RegEvent( $self, "_sess_servise", \&sess_servise ); |
218
|
|
|
|
|
|
|
$eng_ref->Work($self); |
219
|
|
|
|
|
|
|
$eng_ref->SendEvent("_sess_ended"); |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
#print @{$eng_ref->Fetch()}; |
222
|
|
|
|
|
|
|
$eng_ref->_destroy; |
223
|
|
|
|
|
|
|
$self->flush_session(); |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
} |
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
#for setup Output headers |
228
|
|
|
|
|
|
|
sub set_header { |
229
|
|
|
|
|
|
|
my $self = shift; |
230
|
|
|
|
|
|
|
my $response = $self->response_obj; |
231
|
|
|
|
|
|
|
return $self->response_obj->set_header(@_) |
232
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
} |
234
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
#Get cgi params; |
236
|
|
|
|
|
|
|
sub _get_params { |
237
|
|
|
|
|
|
|
my $self = shift; |
238
|
|
|
|
|
|
|
my $_cgi = $self->Cgi_obj(); |
239
|
|
|
|
|
|
|
my %params; |
240
|
|
|
|
|
|
|
foreach my $i ( $_cgi->param() ) { |
241
|
|
|
|
|
|
|
my @all = $_cgi->param($i); |
242
|
|
|
|
|
|
|
foreach my $value (@all) { |
243
|
|
|
|
|
|
|
next if ref $value; |
244
|
|
|
|
|
|
|
$value = decode( 'utf8', $value ) unless is_utf8($value); |
245
|
|
|
|
|
|
|
} |
246
|
|
|
|
|
|
|
$params{$i} = scalar @all > 1 ? \@all : $all[0]; |
247
|
|
|
|
|
|
|
} |
248
|
|
|
|
|
|
|
return \%params; |
249
|
|
|
|
|
|
|
} |
250
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
sub print_header() { |
252
|
|
|
|
|
|
|
my ($self) = @_; |
253
|
|
|
|
|
|
|
my $_cgi = $self->Cgi_obj(); |
254
|
|
|
|
|
|
|
my $ref = $self->Header(); |
255
|
|
|
|
|
|
|
return $self->response( { data => '', } ); |
256
|
|
|
|
|
|
|
return $_cgi->header( map { $_ => $ref->{$_} } keys %{ $self->Header() } ); |
257
|
|
|
|
|
|
|
} |
258
|
|
|
|
|
|
|
|
259
|
|
|
|
|
|
|
sub destroy { |
260
|
|
|
|
|
|
|
my $self = shift; |
261
|
|
|
|
|
|
|
$self->_response_obj(undef); |
262
|
|
|
|
|
|
|
} |
263
|
|
|
|
|
|
|
1; |