| 1 | ############################################################################## | ||
| 2 | ## | ||
| 3 | ## Web::Reactor application machinery | ||
| 4 | ## 2013-2021 (c) Vladi Belperchinov-Shabanski "Cade" | ||
| 5 | ## <cade@bis.bg> <cade@biscom.net> <cade@cpan.org> | ||
| 6 | ## | ||
| 7 | ## LICENSE: GPLv2 | ||
| 8 | ## | ||
| 9 | ############################################################################## | ||
| 10 | package Web::Reactor; | ||
| 11 | use strict; | ||
| 12 | use Storable qw( dclone freeze thaw ); # FIXME: move to Data::Tools (data_freeze/data_thaw) | ||
| 13 | use CGI 4.08; | ||
| 14 | # use CGI 4.51; | ||
| 15 | use CGI::Cookie; | ||
| 16 | use MIME::Base64; | ||
| 17 | use Data::Tools; | ||
| 18 | use Exception::Sink; | ||
| 19 | use Data::Dumper; | ||
| 20 | use Encode; | ||
| 21 | |||
| 22 | use Web::Reactor::Utils; | ||
| 23 | use Web::Reactor::HTML::Form; | ||
| 24 | |||
| 25 | our $VERSION = '2.10'; | ||
| 26 | |||
| 27 | ############################################################################## | ||
| 28 | |||
| 29 | #minimum config: | ||
| 30 | #my %cfg = ( | ||
| 31 | # '' => , | ||
| 32 | # ); | ||
| 33 | |||
| 34 | our @HTTP_VARS_CHECK = qw( | ||
| 35 | REMOTE_ADDR | ||
| 36 | HTTP_USER_AGENT | ||
| 37 | ); | ||
| 38 | |||
| 39 | our @HTTP_VARS_SAVE = qw( | ||
| 40 | REMOTE_ADDR | ||
| 41 | REMOTE_PORT | ||
| 42 | REQUEST_METHOD | ||
| 43 | REQUEST_URI | ||
| 44 | HTTP_REFERRER | ||
| 45 | QUERY_STRING | ||
| 46 | HTTP_COOKIE | ||
| 47 | HTTP_USER_AGENT | ||
| 48 | ); | ||
| 49 | |||
| 50 | our %ENV_ALLOWED_KEYS = { | ||
| 51 | |||
| 52 | }; | ||
| 53 | |||
| 54 | ############################################################################## | ||
| 55 | |||
| 56 | sub new | ||
| 57 | { | ||
| 58 | my $class = shift; | ||
| 59 | my %env = @_; | ||
| 60 | |||
| 61 | # FIXME: verify %env content! Data::Validate::Struct | ||
| 62 | boom "fatal: configuration: request scheme [HTTP] does not match cookies security policy! either enable HTTPS scheme or set DISABLE_SECURE_COOKIES=1" | ||
| 63 | if uc $ENV{ 'REQUEST_SCHEME' } eq 'HTTP' and ! $env{ 'DISABLE_SECURE_COOKIES' }; | ||
| 64 | |||
| 65 | $class = ref( $class ) || $class; | ||
| 66 | my $self = { | ||
| 67 | 'ENV' => \%env, | ||
| 68 | }; | ||
| 69 | bless $self, $class; | ||
| 70 | |||
| 71 | # my $root = $self->{ 'ENV' }{ 'ROOT' }; | ||
| 72 | # # autosetup defaults | ||
| 73 | # if( ! $self->{ 'ENV' }{ 'HTML_DIRS' } ) | ||
| 74 | # { | ||
| 75 | # $self->{ 'ENV' }{ 'HTML_DIRS' } = [ "$root/html" ]; | ||
| 76 | # } | ||
| 77 | |||
| 78 | # FIXME: common directories setup code? | ||
| 79 | if( ! $env{ 'LIB_DIRS' } or @{ $env{ 'LIB_DIRS' } } < 1 ) | ||
| 80 | { | ||
| 81 | my $root = $env{ 'APP_ROOT' }; | ||
| 82 | $env{ 'LIB_DIRS' } = [ "$root/lib" ]; | ||
| 83 | } | ||
| 84 | |||
| 85 | my $lib_dirs = $env{ 'LIB_DIRS' } || []; | ||
| 86 | my $lib_dirs_ok = 0; | ||
| 87 | for my $lib_dir ( @$lib_dirs ) | ||
| 88 | { | ||
| 89 | next unless -d $lib_dir; | ||
| 90 | push @INC, $lib_dir; | ||
| 91 | $lib_dirs_ok++; | ||
| 92 | } | ||
| 93 | # FIXME: this should not be a fatal error? | ||
| 94 | #boom "invalid or not accessible LIB_DIR's [@$lib_dirs]" unless $lib_dirs_ok; | ||
| 95 | |||
| 96 | # sanity, remove '.' from include list, TODO: optionally remove other entries by config (%env) | ||
| 97 | for my $z ( 0 .. scalar( @INC ) - 1 ) | ||
| 98 | { | ||
| 99 | next unless $INC[ $z ] eq '.'; | ||
| 100 | splice @INC, $z, 1; | ||
| 101 | last; | ||
| 102 | } | ||
| 103 | |||
| 104 | my $reo_sess_class = $env{ 'REO_SESS_CLASS' } ||= 'Web::Reactor::Sessions::Filesystem'; | ||
| 105 | my $reo_prep_class = $env{ 'REO_PREP_CLASS' } ||= 'Web::Reactor::Preprocessor::Native'; | ||
| 106 | my $reo_acts_class = $env{ 'REO_ACTS_CLASS' } ||= 'Web::Reactor::Actions::Native'; | ||
| 107 | |||
| 108 | my $reo_sess_class_file = perl_package_to_file( $reo_sess_class ); | ||
| 109 | my $reo_prep_class_file = perl_package_to_file( $reo_prep_class ); | ||
| 110 | my $reo_acts_class_file = perl_package_to_file( $reo_acts_class ); | ||
| 111 | |||
| 112 | require $reo_sess_class_file; | ||
| 113 | require $reo_prep_class_file; | ||
| 114 | require $reo_acts_class_file; | ||
| 115 | |||
| 116 | # new objects for part slots | ||
| 117 | # FIXME: pass %env reference to use the same env hash | ||
| 118 | $self->{ 'REO_SESS' } = new $reo_sess_class %env; | ||
| 119 | $self->{ 'REO_PREP' } = new $reo_prep_class %env; | ||
| 120 | $self->{ 'REO_ACTS' } = new $reo_acts_class %env; | ||
| 121 | |||
| 122 | # set backlinks to reactor | ||
| 123 | $self->{ 'REO_SESS' }->__set_reo( $self ); | ||
| 124 | $self->{ 'REO_PREP' }->__set_reo( $self ); | ||
| 125 | $self->{ 'REO_ACTS' }->__set_reo( $self ); | ||
| 126 | |||
| 127 | # debug setup | ||
| 128 | $self->log_debug( "debug: Reactor[$self] setup: " . Dumper( $self->{ 'ENV' } ) ); | ||
| 129 | |||
| 130 | return $self; | ||
| 131 | } | ||
| 132 | |||
| 133 | sub DESTROY | ||
| 134 | { | ||
| 135 | my $self = shift; | ||
| 136 | |||
| 137 | $self->log_debug( "debug: DESTROY: Reactor[$self] destroyed" ); | ||
| 138 | } | ||
| 139 | |||
| 140 | ############################################################################## | ||
| 141 | |||
| 142 | sub run | ||
| 143 | { | ||
| 144 | my $self = shift; | ||
| 145 | |||
| 146 | eval | ||
| 147 | { | ||
| 148 | $self->main_process(); | ||
| 149 | }; | ||
| 150 | if( surface( 'CONTENT' ) ) | ||
| 151 | { | ||
| 152 | # nothing, should be ok | ||
| 153 | } | ||
| 154 | elsif( surface( '*' ) ) | ||
| 155 | { | ||
| 156 | $self->log( "error: main process failed: $@" ); | ||
| 157 | } | ||
| 158 | $self->save(); | ||
| 159 | |||
| 160 | if( $self->is_debug() ) | ||
| 161 | { | ||
| 162 | my $psid = $self->get_page_session_id( 0 ) || 'empty'; | ||
| 163 | my $rsid = $self->get_page_session_id( 1 ) || 'empty'; | ||
| 164 | my $usid = $self->get_user_session_id() || 'empty'; | ||
| 165 | $self->log_dumper( "USER INPUT -----------------------------------", $self->get_user_input() ); | ||
| 166 | $self->log_dumper( "SAFE INPUT -----------------------------------", $self->get_safe_input() ); | ||
| 167 | $self->log_dumper( "FINAL PAGE SESSION [$psid]-----------------------------------", $self->get_page_session() ); | ||
| 168 | $self->log_dumper( "FINAL REF SESSION [$rsid]-----------------------------------", $self->get_page_session( 1 ) ); | ||
| 169 | $self->log_dumper( "FINAL USER SESSION [$usid]-----------------------------------", $self->get_user_session() ); | ||
| 170 | } | ||
| 171 | |||
| 172 | } | ||
| 173 | |||
| 174 | sub main_process | ||
| 175 | { | ||
| 176 | my $self = shift; | ||
| 177 | |||
| 178 | # 0. load/setup env/config defaults | ||
| 179 | my $app_name = $self->{ 'ENV' }{ 'APP_NAME' } or $self->boom( "missing APP_NAME" ); | ||
| 180 | |||
| 181 | # 1. loading request header and environment | ||
| 182 | $self->{ 'HTTP_ENV' } = { %ENV }; | ||
| 183 | |||
| 184 | # 2. loading cookie | ||
| 185 | my $cookie_name = lc( $self->{ 'ENV' }{ 'COOKIE_NAME' } || "$app_name\_cookie" ); | ||
| 186 | my $user_sid = $self->get_cookie( $cookie_name ); | ||
| 187 | $self->log_debug( "debug: incoming USER_SID cookie name [$cookie_name] value [$user_sid]" ); | ||
| 188 | |||
| 189 | # 3. loading user session, setup new session and cookie if needed | ||
| 190 | my $user_shr = {}; # user session hash ref | ||
| 191 | if( ! ( $user_sid =~ /^[a-zA-Z0-9]+$/ and $user_shr = $self->sess_load( 'USER', $user_sid ) ) ) | ||
| 192 | { | ||
| 193 | # print STDERR Dumper( $user_sid, $user_shr ); | ||
| 194 | |||
| 195 | $self->log( "warning: invalid user session [$user_sid]" ); | ||
| 196 | |||
| 197 | ( $user_sid, $user_shr ) = $self->__create_new_user_session(); | ||
| 198 | } | ||
| 199 | $self->{ 'SESSIONS' }{ 'SID' }{ 'USER' } = $user_sid; | ||
| 200 | $self->{ 'SESSIONS' }{ 'DATA' }{ 'USER' }{ $user_sid } = $user_shr; | ||
| 201 | |||
| 202 | if( ( $user_shr->{ ':LOGGED_IN' } and $user_shr->{ ':XTIME' } > 0 and time() > $user_shr->{ ':XTIME' } ) | ||
| 203 | or | ||
| 204 | ( $user_shr->{ ':CLOSED' } ) ) | ||
| 205 | { | ||
| 206 | $self->log( "status: user session expired or closed, sid [$user_sid]" ); | ||
| 207 | # not logged-in sessions dont expire | ||
| 208 | $user_shr->{ ':XTIME_STR' } = scalar localtime() if time() > $user_shr->{ ':XTIME' }; | ||
| 209 | $user_shr->{ ':CLOSED' } = 1; | ||
| 210 | $user_shr->{ ':ETIME' } = time(); | ||
| 211 | $user_shr->{ ':ETIME_STR' } = scalar localtime(); | ||
| 212 | |||
| 213 | ( $user_sid, $user_shr ) = $self->__create_new_user_session(); | ||
| 214 | |||
| 215 | $self->render( PAGE => 'eexpired' ); | ||
| 216 | } | ||
| 217 | |||
| 218 | for my $k ( keys %{ $user_shr->{ ":HTTP_CHECK_HR" } } ) | ||
| 219 | { | ||
| 220 | # check if session parameters are changed, stealing session? | ||
| 221 | next if $user_shr->{ ":HTTP_CHECK_HR" }{ $k } eq $ENV{ $k }; | ||
| 222 | |||
| 223 | $self->log( "status: user session parameter [$k] check failed, sid [$user_sid]" ); | ||
| 224 | # FIXME: move to function: close_session(); | ||
| 225 | $user_shr->{ ':CLOSED' } = 1; | ||
| 226 | $user_shr->{ ':ETIME' } = time(); | ||
| 227 | $user_shr->{ ':ETIME_STR' } = scalar localtime(); | ||
| 228 | |||
| 229 | ( $user_sid, $user_shr ) = $self->__create_new_user_session(); | ||
| 230 | |||
| 231 | $self->render( PAGE => 'einvalid' ); | ||
| 232 | last; | ||
| 233 | } | ||
| 234 | |||
| 235 | # read and save http environment data into user session, used for checks and info | ||
| 236 | $user_shr->{ ":HTTP_ENV_HR" } = { map { $_ => $ENV{ $_ } } @HTTP_VARS_SAVE }; | ||
| 237 | |||
| 238 | # FIXME: move to single place | ||
| 239 | my $user_session_expire = $self->{ 'ENV' }{ 'USER_SESSION_EXPIRE' } || 600; # 10 minutes | ||
| 240 | $self->set_user_session_expire_time_in( $user_session_expire ); | ||
| 241 | |||
| 242 | $self->save(); | ||
| 243 | |||
| 244 | # 4. get input data, CGI::params, postdata | ||
| 245 | my $input_user_hr = $self->{ 'INPUT_USER_HR' } = {}; | ||
| 246 | my $input_safe_hr = $self->{ 'INPUT_SAFE_HR' } = {}; | ||
| 247 | |||
| 248 | # FIXME: TODO: handle and URL params here. only for EX? | ||
| 249 | my $iconv; | ||
| 250 | my $app_charset = uc $self->{ 'ENV' }{ 'APP_CHARSET' } || 'UTF-8'; | ||
| 251 | my $incoming_charset = $app_charset; | ||
| 252 | |||
| 253 | if( uc( CGI::http( 'HTTP_X_REQUESTED_WITH' ) ) eq 'XMLHTTPREQUEST' ) | ||
| 254 | { | ||
| 255 | # TODO: it can be different, but nobody seems to use it, should be fixed eventually | ||
| 256 | $incoming_charset = 'UTF-8'; | ||
| 257 | } | ||
| 258 | |||
| 259 | # import plain parameters from GET/POST request | ||
| 260 | for my $n ( CGI::url_param(), CGI::param() ) | ||
| 261 | { | ||
| 262 | if( $n !~ /^[A-Za-z0-9\-\_\.\:]+$/o ) | ||
| 263 | { | ||
| 264 | $self->log( "error: invalid CGI/input parameter name: [$n]" ); | ||
| 265 | next; | ||
| 266 | } | ||
| 267 | my @u = CGI::upload( $n ); | ||
| 268 | my $u = CGI::upload( $n ); | ||
| 269 | my $v = CGI::url_param( $n ); | ||
| 270 | $v = CGI::param( $n ) if $v eq ''; | ||
| 271 | my @v = CGI::multi_param( $n ); # TODO: handling of multi-values | ||
| 272 | |||
| 273 | #my $iiuu = Encode::is_utf8( $v ); | ||
| 274 | #print STDERR "------------->>>>>>>>>>>>>>. INCOMING PARAM [$n] => [$v] (utf:$iiuu)\n"; | ||
| 275 | |||
| 276 | $n = uc $n; | ||
| 277 | if( $n eq 'PUTDATA' or $n eq 'POSTDATA' or $n eq 'PATCHDATA' ) | ||
| 278 | { | ||
| 279 | # post data is raw, do not process | ||
| 280 | $input_user_hr->{ $n } = $v; | ||
| 281 | next; | ||
| 282 | } | ||
| 283 | |||
| 284 | # process multiple incoming file uploads | ||
| 285 | if( @u > 0 ) | ||
| 286 | { | ||
| 287 | my $uc = 0; | ||
| 288 | for my $uh ( @u ) | ||
| 289 | { | ||
| 290 | $input_user_hr->{ "$n:FN:$uc" } = "$uh"; # file name | ||
| 291 | $input_user_hr->{ "$n:FH:$uc" } = $uh; # file handle | ||
| 292 | $input_user_hr->{ "$n:FI:$uc" } = CGI::uploadInfo( $uh ); # file info | ||
| 293 | $uc++; | ||
| 294 | } | ||
| 295 | $input_user_hr->{ "$n:FC" } = @u; # file count | ||
| 296 | } | ||
| 297 | |||
| 298 | if( ref( $u ) ) | ||
| 299 | { | ||
| 300 | $input_user_hr->{ "$n:FH" } = $u; | ||
| 301 | # this is file upload, get more info | ||
| 302 | $input_user_hr->{ "$n:UPLOAD_INFO" } = CGI::uploadInfo( $u ); | ||
| 303 | $v = "$v"; | ||
| 304 | } | ||
| 305 | |||
| 306 | #my $iiuu = Encode::is_utf8( $v ); | ||
| 307 | #print STDERR "------------->>>>>>>>>>>>>>. PRE ICOV [$v] (utf:$iiuu)\n"; | ||
| 308 | if( $incoming_charset ne $app_charset ) | ||
| 309 | { | ||
| 310 | Encode::from_to( $v, $incoming_charset, $app_charset ); | ||
| 311 | Encode::from_to( $_, $incoming_charset, $app_charset ) for @v; | ||
| 312 | } | ||
| 313 | |||
| 314 | #print STDERR "------------->>>>>>>>>>>>>>. INCOMING [$v]\n"; | ||
| 315 | #$v = decode( $app_charset, $v ); | ||
| 316 | #print STDERR "------------->>>>>>>>>>>>>>. DECODED [$v] from $app_charset\n\n"; | ||
| 317 | #$_ = decode( $app_charset, $_ ) for @v; | ||
| 318 | |||
| 319 | $self->log_debug( "debug: CGI input param [$n] value [$v] [@v]" ); | ||
| 320 | |||
| 321 | if( $self->__input_cgi_skip_invalid_value( $n, $v ) ) | ||
| 322 | { | ||
| 323 | $self->log( "error: invalid CGI/input value for parameter: [$n]" ); | ||
| 324 | next; | ||
| 325 | } | ||
| 326 | $v = $self->__input_cgi_make_safe_value( $n, $v ); | ||
| 327 | if ( $n =~ /BUTTON:([a-z0-9_\-]+)(:(.+?))?(\.[XY])?$/oi ) | ||
| 328 | { | ||
| 329 | # regular button BUTTON:CANCEL | ||
| 330 | # button with id BUTTON:REDIRECT:USERID | ||
| 331 | $input_user_hr->{ 'BUTTON' } = uc $1; | ||
| 332 | $input_user_hr->{ 'BUTTON_ID' } = $3; | ||
| 333 | } | ||
| 334 | elsif( $n eq '_BUTTON_NAME' ) | ||
| 335 | { | ||
| 336 | my ( undef, $b, $i ) = split /:/, $v, 3; | ||
| 337 | $input_user_hr->{ 'BUTTON' } = uc $b; | ||
| 338 | $input_user_hr->{ 'BUTTON_ID' } = $i; | ||
| 339 | } | ||
| 340 | else | ||
| 341 | { | ||
| 342 | $input_user_hr->{ $n } = $v; | ||
| 343 | } | ||
| 344 | } | ||
| 345 | |||
| 346 | my $safe_input_link_sess = $input_user_hr->{ '_' }; | ||
| 347 | # parse link session: link-sid.link-key | ||
| 348 | if( $safe_input_link_sess =~ /^([a-zA-Z0-9]+)\.([a-zA-Z0-9]+)$/ ) | ||
| 349 | { | ||
| 350 | my ( $link_sid, $link_key ) = ( $1, $2 ); | ||
| 351 | |||
| 352 | my $link_session_hr = $self->sess_load( 'LINK', $link_sid ); | ||
| 353 | |||
| 354 | my $link_data = $link_session_hr->{ $link_key }; | ||
| 355 | |||
| 356 | # merge safe input if valid | ||
| 357 | %$input_safe_hr = ( %$input_safe_hr, %$link_data ) if $link_data; | ||
| 358 | } | ||
| 359 | elsif( $safe_input_link_sess ne '' ) | ||
| 360 | { | ||
| 361 | $self->log( "warning: invalid safe input link session.key [$safe_input_link_sess]" ); | ||
| 362 | } | ||
| 363 | |||
| 364 | # 5. loading page session | ||
| 365 | my $page_sid = $input_safe_hr->{ '_P' }; | ||
| 366 | my $page_shr = {}; # user session hash ref | ||
| 367 | if( ! ( $page_sid =~ /^[a-zA-Z0-9]+$/ and $page_shr = $self->sess_load( 'PAGE', $page_sid ) ) ) | ||
| 368 | { | ||
| 369 | $self->log( "warning: invalid page session [$page_sid]" ); | ||
| 370 | $page_sid = $self->sess_create( 'PAGE', 8 ); | ||
| 371 | $self->log( "warning: new page session created [$page_sid]" ); | ||
| 372 | $page_shr = { ':ID' => $page_sid }; | ||
| 373 | } | ||
| 374 | $self->{ 'SESSIONS' }{ 'SID' }{ 'PAGE' } = $page_sid; | ||
| 375 | $self->{ 'SESSIONS' }{ 'DATA' }{ 'PAGE' }{ $page_sid } = $page_shr; | ||
| 376 | |||
| 377 | |||
| 378 | my $ref_page_sid = $input_safe_hr->{ '_R' }; | ||
| 379 | if( $ref_page_sid =~ /^[a-zA-Z0-9]+$/ ) | ||
| 380 | { | ||
| 381 | $page_shr->{ ':REF_PAGE_SID' } = $ref_page_sid; | ||
| 382 | } | ||
| 383 | |||
| 384 | # 6. remap form input data, post to safe input | ||
| 385 | my $form_name = $input_safe_hr->{ 'FORM_NAME' }; # FIXME: replace with _FO | ||
| 386 | if( $form_name and exists $page_shr->{ ':FORM_DEF' }{ $form_name } ) | ||
| 387 | { | ||
| 388 | my $rm = $page_shr->{ ':FORM_DEF' }{ $form_name }{ 'RET_MAP' }; | ||
| 389 | |||
| 390 | for my $k ( keys %$rm ) | ||
| 391 | { | ||
| 392 | next unless exists $input_user_hr->{ $k }; | ||
| 393 | $input_safe_hr->{ $k } = $rm->{ $k }{ $input_user_hr->{ $k } }; | ||
| 394 | delete $input_user_hr->{ $k }; | ||
| 395 | } | ||
| 396 | } | ||
| 397 | |||
| 398 | my $frame_name = $input_safe_hr->{ '_FR' }; | ||
| 399 | if( $frame_name ne '' ) | ||
| 400 | { | ||
| 401 | if( $frame_name =~ /^[a-zA-Z_0-9]+$/ ) | ||
| 402 | { | ||
| 403 | $page_shr->{ ':FRAME_NAME' } = $frame_name; | ||
| 404 | } | ||
| 405 | else | ||
| 406 | { | ||
| 407 | $self->log( "error: invalid frame name [$frame_name]" ); | ||
| 408 | } | ||
| 409 | } | ||
| 410 | |||
| 411 | # 7. get action from input (USER/CGI) or page session | ||
| 412 | my $action_name = lc( $input_safe_hr->{ '_AN' } || $input_user_hr->{ '_AN' } || $page_shr->{ ':ACTION_NAME' } ); | ||
| 413 | if( $action_name =~ /^[a-z_0-9]+$/ ) | ||
| 414 | { | ||
| 415 | $page_shr->{ ':ACTION_NAME' } = $action_name; | ||
| 416 | } | ||
| 417 | else | ||
| 418 | { | ||
| 419 | # $self->log( "error: invalid action name [$action_name]" ); | ||
| 420 | } | ||
| 421 | |||
| 422 | # 8. get page from input (USER/CGI) or page session | ||
| 423 | my $page_name = lc( $input_safe_hr->{ '_PN' } || $input_user_hr->{ '_PN' } || $page_shr->{ ':PAGE_NAME' } || 'main' ); | ||
| 424 | if( $page_name ne '' ) | ||
| 425 | { | ||
| 426 | if( $page_name =~ /^[a-z_0-9]+$/ ) | ||
| 427 | { | ||
| 428 | $page_shr->{ ':PAGE_NAME' } = $page_name; | ||
| 429 | } | ||
| 430 | else | ||
| 431 | { | ||
| 432 | $self->log( "error: invalid page name [$page_name]" ); | ||
| 433 | } | ||
| 434 | } | ||
| 435 | |||
| 436 | # pre-9. print debug status... | ||
| 437 | if( $self->is_debug() ) | ||
| 438 | { | ||
| 439 | my $psid = $self->get_page_session_id( 0 ) || 'empty'; | ||
| 440 | my $rsid = $self->get_page_session_id( 1 ) || 'empty'; | ||
| 441 | $self->log_dumper( "USER INPUT-------------------------------------", $self->get_user_input() ); | ||
| 442 | $self->log_dumper( "SAFE INPUT-------------------------------------", $self->get_safe_input() ); | ||
| 443 | $self->log_dumper( "PAGE SESSION [$psid]-----------------------------------", $self->get_page_session() ); | ||
| 444 | $self->log_dumper( "REF SESSION [$rsid]-----------------------------------", $self->get_page_session( 1 ) ); | ||
| 445 | # $self->log_dumper( "USER SESSION-----------------------------------", $self->get_user_session() ); | ||
| 446 | } | ||
| 447 | |||
| 448 | # 9. render output action/page | ||
| 449 | if( $action_name ) | ||
| 450 | { | ||
| 451 | $self->render( ACTION => $action_name ); | ||
| 452 | } | ||
| 453 | else | ||
| 454 | { | ||
| 455 | $self->render( PAGE => $page_name ); | ||
| 456 | } | ||
| 457 | |||
| 458 | } | ||
| 459 | |||
| 460 | sub __create_new_user_session | ||
| 461 | { | ||
| 462 | my $self = shift; | ||
| 463 | |||
| 464 | my $user_sid; | ||
| 465 | my $user_shr; | ||
| 466 | |||
| 467 | # FIXME: move to function | ||
| 468 | my $app_name = $self->{ 'ENV' }{ 'APP_NAME' } or $self->boom( "missing APP_NAME" ); | ||
| 469 | my $cookie_name = lc( $self->{ 'ENV' }{ 'COOKIE_NAME' } || "$app_name\_cookie" ); | ||
| 470 | |||
| 471 | $user_sid = $self->sess_create( 'USER' ); | ||
| 472 | $user_shr = { ':ID' => $user_sid }; | ||
| 473 | $self->{ 'SESSIONS' }{ 'SID' }{ 'USER' } = $user_sid; | ||
| 474 | $self->{ 'SESSIONS' }{ 'DATA' }{ 'USER' }{ $user_sid } = $user_shr; | ||
| 475 | |||
| 476 | my $path = $self->{ 'ENV' }{ 'COOKIE_PATH' }; | ||
| 477 | if( ! $path ) | ||
| 478 | { | ||
| 479 | $path = $self->{ 'HTTP_ENV' }{ 'REQUEST_URI' }; | ||
| 480 | #print STDERR ">>>>>>>>>>>>>>>>>>>111>>>>>>>>>>>>>>> cookie path [$path]\n"; | ||
| 481 | $path =~ s/^([^\?]*\/)([^\?\/]*)(\?.*)?$/$1/; # remove args: ?... | ||
| 482 | #print STDERR ">>>>>>>>>>>>>>>>>>>222>>>>>>>>>>>>>>> cookie path [$path]\n"; | ||
| 483 | } | ||
| 484 | $path ||= '/'; | ||
| 485 | |||
| 486 | my $secure_cookie = $self->{ 'ENV' }{ 'DISABLE_SECURE_COOKIES' } ? 0 : 1; | ||
| 487 | $self->set_cookie( $cookie_name, -value => $user_sid, -path => $path, -httponly => 1, -secure => $secure_cookie ); | ||
| 488 | $self->log( "debug: creating new user session [$user_sid]" ); | ||
| 489 | |||
| 490 | my $user_session_expire = $self->{ 'ENV' }{ 'USER_SESSION_EXPIRE' } || 600; # 10 minutes | ||
| 491 | |||
| 492 | $user_shr->{ ':CTIME' } = time(); | ||
| 493 | $user_shr->{ ':CTIME_STR' } = scalar localtime(); | ||
| 494 | |||
| 495 | $self->set_user_session_expire_time_in( $user_session_expire ); | ||
| 496 | |||
| 497 | $user_shr->{ ":HTTP_CHECK_HR" } = { map { $_ => $ENV{ $_ } } @HTTP_VARS_CHECK }; | ||
| 498 | $user_shr->{ ":HTTP_ENV_HR" } = { map { $_ => $ENV{ $_ } } @HTTP_VARS_SAVE }; | ||
| 499 | |||
| 500 | return ( $user_sid, $user_shr ); | ||
| 501 | } | ||
| 502 | |||
| 503 | ############################################################################## | ||
| 504 | # | ||
| 505 | # usual user visible api | ||
| 506 | # | ||
| 507 | |||
| 508 | sub get_user_session | ||
| 509 | { | ||
| 510 | my $self = shift; | ||
| 511 | |||
| 512 | my $user_sid = $self->{ 'SESSIONS' }{ 'SID' }{ 'USER' }; | ||
| 513 | my $user_shr = $self->{ 'SESSIONS' }{ 'DATA' }{ 'USER' }{ $user_sid }; | ||
| 514 | |||
| 515 | return $user_shr; | ||
| 516 | } | ||
| 517 | |||
| 518 | sub get_user_session_id | ||
| 519 | { | ||
| 520 | my $self = shift; | ||
| 521 | |||
| 522 | my $user_sid = $self->{ 'SESSIONS' }{ 'SID' }{ 'USER' }; | ||
| 523 | |||
| 524 | return $user_sid; | ||
| 525 | } | ||
| 526 | |||
| 527 | sub get_page_session | ||
| 528 | { | ||
| 529 | my $self = shift; | ||
| 530 | my $level = shift; | ||
| 531 | |||
| 532 | my $page_sid = $self->{ 'SESSIONS' }{ 'SID' }{ 'PAGE' }; | ||
| 533 | my $page_shr = $self->{ 'SESSIONS' }{ 'DATA' }{ 'PAGE' }{ $page_sid }; | ||
| 534 | |||
| 535 | while( $level-- ) | ||
| 536 | { | ||
| 537 | $page_sid = $page_shr->{ ':REF_PAGE_SID' }; | ||
| 538 | return undef unless $page_sid; | ||
| 539 | $page_shr = $self->{ 'SESSIONS' }{ 'DATA' }{ 'PAGE' }{ $page_sid }; | ||
| 540 | if( ! $page_shr ) | ||
| 541 | { | ||
| 542 | $page_shr = $self->sess_load( 'PAGE', $page_sid ); | ||
| 543 | $self->{ 'SESSIONS' }{ 'DATA' }{ 'PAGE' }{ $page_sid } = $page_shr; | ||
| 544 | } | ||
| 545 | } | ||
| 546 | |||
| 547 | return $page_shr; | ||
| 548 | } | ||
| 549 | |||
| 550 | sub get_http_env | ||
| 551 | { | ||
| 552 | my $self = shift; | ||
| 553 | |||
| 554 | my $user_shr = $self->get_user_session(); | ||
| 555 | |||
| 556 | boom "missing HTTP_ENV inside user session" unless exists $user_shr->{ ':HTTP_ENV_HR' }; | ||
| 557 | |||
| 558 | return $user_shr->{ ':HTTP_ENV_HR' }; | ||
| 559 | } | ||
| 560 | |||
| 561 | sub get_page_session_id | ||
| 562 | { | ||
| 563 | my $self = shift; | ||
| 564 | my $level = shift; | ||
| 565 | |||
| 566 | my $shr = $self->get_page_session( $level ) || {}; | ||
| 567 | |||
| 568 | return $shr->{ ':ID' }; | ||
| 569 | } | ||
| 570 | |||
| 571 | sub get_ref_page_session_id | ||
| 572 | { | ||
| 573 | my $self = shift; | ||
| 574 | my $level = shift; | ||
| 575 | |||
| 576 | my $shr = $self->get_page_session( $level ) || {}; | ||
| 577 | |||
| 578 | return $shr->{ ':REF_PAGE_SID' }; | ||
| 579 | } | ||
| 580 | |||
| 581 | sub get_safe_input | ||
| 582 | { | ||
| 583 | my $self = shift; | ||
| 584 | |||
| 585 | my $input_safe_hr = $self->{ 'INPUT_SAFE_HR' }; | ||
| 586 | return $input_safe_hr; | ||
| 587 | } | ||
| 588 | |||
| 589 | sub get_user_input | ||
| 590 | { | ||
| 591 | my $self = shift; | ||
| 592 | |||
| 593 | my $input_user_hr = $self->{ 'INPUT_USER_HR' }; | ||
| 594 | return $input_user_hr; | ||
| 595 | } | ||
| 596 | |||
| 597 | sub get_input_button | ||
| 598 | { | ||
| 599 | my $self = shift; | ||
| 600 | |||
| 601 | my $input_user_hr = $self->get_user_input(); | ||
| 602 | my $input_safe_hr = $self->get_safe_input(); | ||
| 603 | return $input_safe_hr->{ 'BUTTON' } || $input_user_hr->{ 'BUTTON' }; | ||
| 604 | } | ||
| 605 | |||
| 606 | sub get_input_button_id | ||
| 607 | { | ||
| 608 | my $self = shift; | ||
| 609 | |||
| 610 | my $input_user_hr = $self->get_user_input(); | ||
| 611 | my $input_safe_hr = $self->get_safe_input(); | ||
| 612 | return $input_safe_hr->{ 'BUTTON_ID' } || $input_user_hr->{ 'BUTTON_ID' }; | ||
| 613 | } | ||
| 614 | |||
| 615 | sub get_input_button_and_remove | ||
| 616 | { | ||
| 617 | my $self = shift; | ||
| 618 | |||
| 619 | my $input_user_hr = $self->get_user_input(); | ||
| 620 | my $input_safe_hr = $self->get_safe_input(); | ||
| 621 | my $button = $input_safe_hr->{ 'BUTTON' } || $input_user_hr->{ 'BUTTON' }; | ||
| 622 | delete $input_user_hr->{ 'BUTTON' }; | ||
| 623 | delete $input_safe_hr->{ 'BUTTON' }; | ||
| 624 | return $button; | ||
| 625 | } | ||
| 626 | |||
| 627 | sub get_input_form_name | ||
| 628 | { | ||
| 629 | my $self = shift; | ||
| 630 | |||
| 631 | my $input_safe_hr = $self->get_safe_input(); | ||
| 632 | my $form_name = $input_safe_hr->{ 'FORM_NAME' }; # FIXME: replace with _FN | ||
| 633 | |||
| 634 | return $form_name; | ||
| 635 | } | ||
| 636 | |||
| 637 | sub get_page_frame | ||
| 638 | { | ||
| 639 | my $self = shift; | ||
| 640 | |||
| 641 | my $page_shr = $self->get_page_session(); | ||
| 642 | |||
| 643 | return exists $page_shr->{ ':FRAME_NAME' } ? $page_shr->{ ':FRAME_NAME' } : undef; | ||
| 644 | } | ||
| 645 | |||
| 646 | sub get_lang | ||
| 647 | { | ||
| 648 | my $self = shift; | ||
| 649 | |||
| 650 | return $self->{ 'ENV' }{ 'LANG' }; | ||
| 651 | } | ||
| 652 | |||
| 653 | sub get_app_root | ||
| 654 | { | ||
| 655 | my $self = shift; | ||
| 656 | |||
| 657 | return $self->{ 'ENV' }{ 'APP_ROOT' }; | ||
| 658 | } | ||
| 659 | |||
| 660 | sub args | ||
| 661 | { | ||
| 662 | my $self = shift; | ||
| 663 | my %args = @_; | ||
| 664 | |||
| 665 | hash_uc_ipl( \%args ); | ||
| 666 | |||
| 667 | my $link_sid; | ||
| 668 | my $link_shr; | ||
| 669 | |||
| 670 | if( ! $self->{ 'SESSIONS' }{ 'SID' }{ 'LINK' } ) | ||
| 671 | { | ||
| 672 | $link_sid = $self->sess_create( 'LINK', 8 ); | ||
| 673 | $link_shr = { ':ID' => $link_sid }; | ||
| 674 | $self->{ 'SESSIONS' }{ 'DATA' }{ 'LINK' }{ $link_sid } = $link_shr; | ||
| 675 | $self->{ 'SESSIONS' }{ 'SID' }{ 'LINK' } = $link_sid; | ||
| 676 | } | ||
| 677 | else | ||
| 678 | { | ||
| 679 | $link_sid = $self->{ 'SESSIONS' }{ 'SID' }{ 'LINK' }; | ||
| 680 | $link_shr = $self->{ 'SESSIONS' }{ 'DATA' }{ 'LINK' }{ $link_sid }; | ||
| 681 | } | ||
| 682 | |||
| 683 | my $link_key; | ||
| 684 | while(4) | ||
| 685 | { | ||
| 686 | $link_key = $self->{ 'REO_SESS' }->create_id( 8 ); # FIXME: length param env | ||
| 687 | last if ! exists $link_shr->{ $link_key }; | ||
| 688 | } | ||
| 689 | $self->boom( "cannot create LINK key" ) unless $link_key; | ||
| 690 | |||
| 691 | $link_shr->{ $link_key } = \%args; | ||
| 692 | |||
| 693 | return $link_sid . '.' . $link_key; | ||
| 694 | } | ||
| 695 | |||
| 696 | sub args_back | ||
| 697 | { | ||
| 698 | my $self = shift; | ||
| 699 | my %args = @_; | ||
| 700 | |||
| 701 | $args{ '_P' } = $self->get_ref_page_session_id(); | ||
| 702 | $args{ '_PN' } = 'main' unless $args{ '_P' }; # return to 'main' if no referer given | ||
| 703 | |||
| 704 | return $self->args( %args ); | ||
| 705 | } | ||
| 706 | |||
| 707 | sub args_back_back | ||
| 708 | { | ||
| 709 | my $self = shift; | ||
| 710 | my %args = @_; | ||
| 711 | |||
| 712 | $args{ '_P' } = $self->get_ref_page_session_id( 1 ); | ||
| 713 | $args{ '_PN' } = 'main' unless $args{ '_P' }; # return to 'main' if no referer given | ||
| 714 | |||
| 715 | return $self->args( %args ); | ||
| 716 | } | ||
| 717 | |||
| 718 | sub args_new | ||
| 719 | { | ||
| 720 | my $self = shift; | ||
| 721 | my %args = @_; | ||
| 722 | |||
| 723 | $args{ '_R' } = $self->get_page_session_id(); | ||
| 724 | |||
| 725 | my $page_shr = $self->get_page_session(); | ||
| 726 | if( exists $page_shr->{ ':FRAME_NAME' } ) | ||
| 727 | { | ||
| 728 | $args{ '_FR' } = $page_shr->{ ':FRAME_NAME' }; | ||
| 729 | } | ||
| 730 | |||
| 731 | return $self->args( %args ); | ||
| 732 | } | ||
| 733 | |||
| 734 | sub args_here | ||
| 735 | { | ||
| 736 | my $self = shift; | ||
| 737 | my %args = @_; | ||
| 738 | |||
| 739 | $args{ '_P' } = $self->get_page_session_id(); | ||
| 740 | |||
| 741 | return $self->args( %args ); | ||
| 742 | } | ||
| 743 | |||
| 744 | sub args_type | ||
| 745 | { | ||
| 746 | my $self = shift; | ||
| 747 | |||
| 748 | my $type = lc shift; | ||
| 749 | |||
| 750 | return $self->args_new( @_ ) if $type eq 'new'; | ||
| 751 | return $self->args_here( @_ ) if $type eq 'here'; | ||
| 752 | return $self->args_back( @_ ) if $type eq 'back'; | ||
| 753 | return $self->args( @_ ) if $type eq 'none'; | ||
| 754 | $self->boom( "unknown or not supported TYPE [$type]" ); | ||
| 755 | } | ||
| 756 | |||
| 757 | ############################################################################## | ||
| 758 | |||
| 759 | sub get_cookie | ||
| 760 | { | ||
| 761 | my $self = shift; | ||
| 762 | my $name = shift; | ||
| 763 | |||
| 764 | my $cookie = CGI::cookie( $name ); | ||
| 765 | $self->log_debug( "get_cookie: name [$name] value [$cookie]" ); | ||
| 766 | return $cookie; | ||
| 767 | } | ||
| 768 | |||
| 769 | sub set_cookie | ||
| 770 | { | ||
| 771 | my $self = shift; | ||
| 772 | my $name = shift; | ||
| 773 | my %opt = @_; | ||
| 774 | |||
| 775 | $self->log( "debug: creating new cookie [$name]" ); | ||
| 776 | # FIXME: validate %opt Data::Validate::Struct | ||
| 777 | |||
| 778 | $opt{ -name } = $name; | ||
| 779 | |||
| 780 | $self->{ 'OUTPUT' }{ 'COOKIES' }{ $name } = new CGI::Cookie( %opt ); | ||
| 781 | } | ||
| 782 | |||
| 783 | ############################################################################## | ||
| 784 | |||
| 785 | sub get_headers | ||
| 786 | { | ||
| 787 | my $self = shift; | ||
| 788 | |||
| 789 | my %h = map { $_ => CGI::http( $_ ) } CGI::http(); | ||
| 790 | |||
| 791 | return \%h; | ||
| 792 | } | ||
| 793 | |||
| 794 | sub set_headers | ||
| 795 | { | ||
| 796 | my $self = shift; | ||
| 797 | my %h = @_; | ||
| 798 | |||
| 799 | hash_lc_ipl( \%h ); | ||
| 800 | |||
| 801 | $self->{ 'OUTPUT' }{ 'HEADERS' } ||= {}; | ||
| 802 | $self->{ 'OUTPUT' }{ 'HEADERS' } = { %{ $self->{ 'OUTPUT' }{ 'HEADERS' } }, %h }; | ||
| 803 | } | ||
| 804 | |||
| 805 | sub __make_headers | ||
| 806 | { | ||
| 807 | my $self = shift; | ||
| 808 | |||
| 809 | my $headers; | ||
| 810 | |||
| 811 | $self->{ 'OUTPUT' }{ 'HEADERS' }{ 'content-type' } ||= 'text/html'; | ||
| 812 | |||
| 813 | # postprocess headers, custom logic, etc. | ||
| 814 | my %headers_out = %{ $self->{ 'OUTPUT' }{ 'HEADERS' } }; | ||
| 815 | |||
| 816 | if( $headers_out{ 'content-charset' } ) | ||
| 817 | { | ||
| 818 | if( $headers_out{ 'content-type' } !~ /;\s*charset=/i ) | ||
| 819 | { | ||
| 820 | $headers_out{ 'content-type' } .= '; charset=' . $headers_out{ 'content-charset' }; | ||
| 821 | } | ||
| 822 | delete $headers_out{ 'content-charset' }; | ||
| 823 | }; | ||
| 824 | |||
| 825 | while( my ( $k, $v ) = each %headers_out ) | ||
| 826 | { | ||
| 827 | $headers .= "$k: $v\n"; | ||
| 828 | } | ||
| 829 | |||
| 830 | while( my ( $k, $v ) = each %{ $self->{ 'OUTPUT' }{ 'COOKIES' } } ) | ||
| 831 | { | ||
| 832 | $k = 'set-cookie'; | ||
| 833 | $headers .= "$k: $v\n"; | ||
| 834 | } | ||
| 835 | |||
| 836 | $headers .= "\n"; # just single newline separator | ||
| 837 | |||
| 838 | $self->log_dumper( 'HEADERS----------------------------------------', $headers ); | ||
| 839 | |||
| 840 | return $headers; | ||
| 841 | } | ||
| 842 | |||
| 843 | ############################################################################## | ||
| 844 | |||
| 845 | sub save | ||
| 846 | { | ||
| 847 | my $self = shift; | ||
| 848 | |||
| 849 | my $mod_cache = $self->{ 'CACHE' }{ 'SESSION_DATA_SHA1' } ||= {}; | ||
| 850 | |||
| 851 | for my $type ( qw( USER PAGE LINK ) ) | ||
| 852 | { | ||
| 853 | while( my ( $sid, $shr ) = each %{ $self->{ 'SESSIONS' }{ 'DATA' }{ $type } } ) | ||
| 854 | { | ||
| 855 | $self->boom( "SESSION:DATA:$type:$sid is not hashref" ) unless ref( $shr ) eq 'HASH'; | ||
| 856 | |||
| 857 | my $sha1 = sha1_hex( freeze( $shr ) ); | ||
| 858 | my $cache1 = $mod_cache->{ $type }{ $sid }; | ||
| 859 | |||
| 860 | next if $sha1 eq $cache1; | ||
| 861 | |||
| 862 | $self->log_debug( "saving session data [$type:$sid]" ); | ||
| 863 | |||
| 864 | $mod_cache->{ $type }{ $sid } = $sha1; | ||
| 865 | |||
| 866 | $self->sess_save( $type, $sid, $shr ); | ||
| 867 | } | ||
| 868 | } | ||
| 869 | } | ||
| 870 | |||
| 871 | ############################################################################## | ||
| 872 | ## | ||
| 873 | ## CRYPTO api :) | ||
| 874 | ## | ||
| 875 | |||
| 876 | ## FIXME: move most to Data::Tools or separate module | ||
| 877 | |||
| 878 | sub __new_crypto_object | ||
| 879 | { | ||
| 880 | my $self = shift; | ||
| 881 | |||
| 882 | # NOTE: RTFM says encryptor and decryptor must be different | ||
| 883 | |||
| 884 | # FIXME: read key from config file only! | ||
| 885 | my $key = $self->{ 'ENV' }{ 'ENCRYPT_KEY' }; | ||
| 886 | $self->boom( "missing ENV:ENCRYPT_KEY" ) unless $key =~ /\S/; | ||
| 887 | |||
| 888 | my $ci = $self->{ 'ENV' }{ 'ENCRYPT_CIPHER' } || 'Twofish2'; # :) | ||
| 889 | |||
| 890 | return Crypt::CBC->new( -key => $key, -cipher => $ci ); | ||
| 891 | } | ||
| 892 | |||
| 893 | sub encrypt | ||
| 894 | { | ||
| 895 | my $self = shift; | ||
| 896 | my $data = shift; | ||
| 897 | |||
| 898 | my $enc = $self->{ 'ENCRYPTOR' } ||= $self->__new_crypto_object(); | ||
| 899 | return $enc->encrypt( $data ); | ||
| 900 | } | ||
| 901 | |||
| 902 | sub decrypt | ||
| 903 | { | ||
| 904 | my $self = shift; | ||
| 905 | my $data = shift; | ||
| 906 | |||
| 907 | my $dec = $self->{ 'DECRYPTOR' } ||= $self->__new_crypto_object(); | ||
| 908 | return $dec->decrypt( $data ); | ||
| 909 | } | ||
| 910 | |||
| 911 | sub encrypt_hex | ||
| 912 | { | ||
| 913 | my $self = shift; | ||
| 914 | |||
| 915 | return str_hex( $self->encrypt( @_ ) ); | ||
| 916 | } | ||
| 917 | |||
| 918 | sub decrypt_hex | ||
| 919 | { | ||
| 920 | my $self = shift; | ||
| 921 | |||
| 922 | return $self->decrypt( str_unhex( @_ ) ); | ||
| 923 | } | ||
| 924 | |||
| 925 | sub crypto_freeze_hex | ||
| 926 | { | ||
| 927 | my $self = shift; | ||
| 928 | my $data = shift; # reference to any data/scalar/hash/array | ||
| 929 | |||
| 930 | return $self->encrypt_hex( freeze( $data ) ); | ||
| 931 | } | ||
| 932 | |||
| 933 | sub crypto_thaw_hex | ||
| 934 | { | ||
| 935 | my $self = shift; | ||
| 936 | my $data = shift; # hex encoded data | ||
| 937 | |||
| 938 | return thaw( $self->decrypt_hex( $data ) ); | ||
| 939 | } | ||
| 940 | |||
| 941 | sub encrypt_base64u | ||
| 942 | { | ||
| 943 | my $self = shift; | ||
| 944 | |||
| 945 | return MIME::Base64::encode_base64url( $self->encrypt( @_ ) ); | ||
| 946 | } | ||
| 947 | |||
| 948 | sub decrypt_base64u | ||
| 949 | { | ||
| 950 | my $self = shift; | ||
| 951 | |||
| 952 | return $self->decrypt( MIME::Base64::decode_base64url( @_ ) ); | ||
| 953 | } | ||
| 954 | |||
| 955 | sub crypto_freeze_base64u | ||
| 956 | { | ||
| 957 | my $self = shift; | ||
| 958 | my $data = shift; # reference to any data/scalar/hash/array | ||
| 959 | |||
| 960 | return $self->encrypt_base64u( freeze( $data ) ); | ||
| 961 | } | ||
| 962 | |||
| 963 | sub crypto_thaw_base64u | ||
| 964 | { | ||
| 965 | my $self = shift; | ||
| 966 | my $data = shift; # base64u encoded data | ||
| 967 | |||
| 968 | return thaw( $self->decrypt_base64u( $data ) ); | ||
| 969 | } | ||
| 970 | |||
| 971 | ############################################################################## | ||
| 972 | |||
| 973 | sub set_debug | ||
| 974 | { | ||
| 975 | my $self = shift; | ||
| 976 | my $level = int(shift); | ||
| 977 | |||
| 978 | if( $level > 0 ) | ||
| 979 | { | ||
| 980 | $self->{ 'ENV' }{ 'DEBUG' } = $level > 0 ? $level : 0; | ||
| 981 | } | ||
| 982 | return $self->{ 'ENV' }{ 'DEBUG' }; | ||
| 983 | } | ||
| 984 | |||
| 985 | sub is_debug | ||
| 986 | { | ||
| 987 | my $self = shift; | ||
| 988 | |||
| 989 | return $self->{ 'ENV' }{ 'DEBUG' }; | ||
| 990 | } | ||
| 991 | |||
| 992 | #----------------------------------------------------------------------------- | ||
| 993 | |||
| 994 | sub log | ||
| 995 | { | ||
| 996 | my $self = shift; | ||
| 997 | |||
| 998 | print STDERR @_, "\n"; | ||
| 999 | } | ||
| 1000 | |||
| 1001 | sub log_debug | ||
| 1002 | { | ||
| 1003 | my $self = shift; | ||
| 1004 | |||
| 1005 | return unless $self->is_debug(); | ||
| 1006 | my @args = @_; | ||
| 1007 | chomp( @args ); | ||
| 1008 | my $msg = join( "\n", @args ); | ||
| 1009 | $msg = "debug: $msg" unless $msg =~ /^debug:/i; | ||
| 1010 | $self->log( $msg ); | ||
| 1011 | } | ||
| 1012 | |||
| 1013 | sub log_stack | ||
| 1014 | { | ||
| 1015 | my $self = shift; | ||
| 1016 | |||
| 1017 | $self->log_debug( @_, "\n", Exception::Sink::get_stack_trace() ); | ||
| 1018 | } | ||
| 1019 | |||
| 1020 | sub log_dumper | ||
| 1021 | { | ||
| 1022 | my $self = shift; | ||
| 1023 | |||
| 1024 | return unless $self->is_debug(); | ||
| 1025 | $self->log_debug( Dumper( @_ ) ); | ||
| 1026 | } | ||
| 1027 | |||
| 1028 | ############################################################################## | ||
| 1029 | ## | ||
| 1030 | ## sanity policies | ||
| 1031 | ## these are internal subs but are designed to be overriden if required | ||
| 1032 | ## | ||
| 1033 | |||
| 1034 | # fix/remove invalid parts of a CGI/input value | ||
| 1035 | sub __input_cgi_make_safe_value | ||
| 1036 | { | ||
| 1037 | my $self = shift; | ||
| 1038 | my $n = shift; # arg name | ||
| 1039 | my $v = shift; # arg value | ||
| 1040 | |||
| 1041 | $v =~ s/[\000]//go; | ||
| 1042 | |||
| 1043 | return $v; | ||
| 1044 | } | ||
| 1045 | |||
| 1046 | # must return 1 for values which must be removed from input or 0 for ok | ||
| 1047 | # this is called before __input_cgi_make_safe_value, default is pass all | ||
| 1048 | sub __input_cgi_skip_invalid_value | ||
| 1049 | { | ||
| 1050 | my $self = shift; | ||
| 1051 | # this is placeholder really | ||
| 1052 | # my $n = shift; # arg name | ||
| 1053 | # my $v = shift; # arg value | ||
| 1054 | return 0; | ||
| 1055 | } | ||
| 1056 | |||
| 1057 | ############################################################################## | ||
| 1058 | |||
| 1059 | sub html_content | ||
| 1060 | { | ||
| 1061 | my $self = shift; | ||
| 1062 | my %hc = @_; | ||
| 1063 | |||
| 1064 | hash_lc_ipl( \%hc ); | ||
| 1065 | $self->{ 'HTML_CONTENT' } ||= {}; | ||
| 1066 | %{ $self->{ 'HTML_CONTENT' } } = ( %{ $self->{ 'HTML_CONTENT' } }, %hc ); | ||
| 1067 | |||
| 1068 | return $self->{ 'HTML_CONTENT' }; | ||
| 1069 | } | ||
| 1070 | |||
| 1071 | sub html_content_clear | ||
| 1072 | { | ||
| 1073 | my $self = shift; | ||
| 1074 | |||
| 1075 | $self->{ 'HTML_CONTENT' } ||= {}; | ||
| 1076 | } | ||
| 1077 | |||
| 1078 | sub html_content_set | ||
| 1079 | { | ||
| 1080 | my $self = shift; | ||
| 1081 | |||
| 1082 | $self->html_content_clear(); | ||
| 1083 | return $self->html_content( @_ ); | ||
| 1084 | } | ||
| 1085 | |||
| 1086 | sub html_content_accumulator | ||
| 1087 | { | ||
| 1088 | my $self = shift; | ||
| 1089 | my $name = shift; | ||
| 1090 | my $text = shift; | ||
| 1091 | |||
| 1092 | $self->{ 'HTML_CONTENT' } ||= {}; | ||
| 1093 | $self->{ 'HTML_CONTENT' }{ $name }{ $text }++; | ||
| 1094 | |||
| 1095 | $self->html_content_set( $name, join '', keys %{ $self->{ 'HTML_CONTENT' }{ $name } } ); | ||
| 1096 | } | ||
| 1097 | |||
| 1098 | sub html_content_accumulator_js | ||
| 1099 | { | ||
| 1100 | my $self = shift; | ||
| 1101 | my $text = shift; | ||
| 1102 | |||
| 1103 | $text = "<script type='text/javascript' src='$text'></script>"; | ||
| 1104 | $self->html_content_accumulator( "ACCUMULATOR_JS", $text ); | ||
| 1105 | } | ||
| 1106 | |||
| 1107 | sub html_content_accumulator_css | ||
| 1108 | { | ||
| 1109 | my $self = shift; | ||
| 1110 | my $css = shift; | ||
| 1111 | |||
| 1112 | my $text = qq{ <link href="$css" rel="stylesheet" type="text/css"> }; | ||
| 1113 | $self->html_content_accumulator( "ACCUMULATOR_HEAD", $text ); | ||
| 1114 | } | ||
| 1115 | |||
| 1116 | ############################################################################## | ||
| 1117 | |||
| 1118 | sub render_data | ||
| 1119 | { | ||
| 1120 | my $self = shift; | ||
| 1121 | |||
| 1122 | return $self->render( DATA => $self->portray( @_ ) ); | ||
| 1123 | } | ||
| 1124 | |||
| 1125 | sub render_action | ||
| 1126 | { | ||
| 1127 | my $self = shift; | ||
| 1128 | my $action = shift; | ||
| 1129 | |||
| 1130 | return $self->render( ACTION => $action, @_ ); | ||
| 1131 | } | ||
| 1132 | |||
| 1133 | sub render_page | ||
| 1134 | { | ||
| 1135 | my $self = shift; | ||
| 1136 | my $page = shift; | ||
| 1137 | |||
| 1138 | return $self->render( PAGE => $page, @_ ); | ||
| 1139 | } | ||
| 1140 | |||
| 1141 | sub render | ||
| 1142 | { | ||
| 1143 | my $self = shift; | ||
| 1144 | my %opt = @_; | ||
| 1145 | |||
| 1146 | #print STDERR Dumper( 'RENDER --- ' x 11, \%opt ); | ||
| 1147 | |||
| 1148 | boom "too many nesting levels in rendering, probable bug in actions or pages" if (caller(512))[0] ne ''; # FIXME: config option for max level | ||
| 1149 | |||
| 1150 | my $action = $opt{ 'ACTION' }; | ||
| 1151 | my $page = $opt{ 'PAGE' }; | ||
| 1152 | my $data = $opt{ 'DATA' }; | ||
| 1153 | |||
| 1154 | # FIXME: content vars handling set_content()/etc. | ||
| 1155 | my $ah = $self->args_here(); | ||
| 1156 | $self->html_content( 'FORM_INPUT_SESSION_KEEPER' => "<input type=hidden name=_ value=$ah>" ); | ||
| 1157 | $self->html_content( %opt ); | ||
| 1158 | |||
| 1159 | my $portray_data; | ||
| 1160 | |||
| 1161 | if( ref( $data ) eq 'HASH' ) | ||
| 1162 | { | ||
| 1163 | $portray_data = $data; | ||
| 1164 | $page = $action = undef; | ||
| 1165 | } | ||
| 1166 | elsif( $action ) | ||
| 1167 | { | ||
| 1168 | # FIXME: handle content type also! | ||
| 1169 | $portray_data = $self->action_call( $action ); | ||
| 1170 | |||
| 1171 | #print STDERR Dumper( '--------------->>> RENDER >>>>>>>>>>>>>>>>>>>>>', $portray_data ); | ||
| 1172 | |||
| 1173 | $page = undef; | ||
| 1174 | } | ||
| 1175 | elsif( $page ) | ||
| 1176 | { | ||
| 1177 | $portray_data = $self->prep_load_page( $page ); | ||
| 1178 | $action = undef; | ||
| 1179 | } | ||
| 1180 | else | ||
| 1181 | { | ||
| 1182 | boom "render() needs PAGE or ACTION"; | ||
| 1183 | } | ||
| 1184 | |||
| 1185 | if( ref( $portray_data ) eq 'HASH' ) | ||
| 1186 | { | ||
| 1187 | # nothing, ok | ||
| 1188 | } | ||
| 1189 | elsif( ref( $portray_data ) ) | ||
| 1190 | { | ||
| 1191 | boom "expected portray data (i.e. HASHREF) but got different reference"; | ||
| 1192 | } | ||
| 1193 | else | ||
| 1194 | { | ||
| 1195 | # default portray type is html | ||
| 1196 | $portray_data = $self->portray( $portray_data, 'text/html' ); | ||
| 1197 | } | ||
| 1198 | |||
| 1199 | #print STDERR Dumper( 'PORTRAY --- ' x 11, $page, $portray_data ); | ||
| 1200 | |||
| 1201 | my $page_data = $portray_data->{ 'DATA' }; | ||
| 1202 | my $page_fh = $portray_data->{ 'FH' }; # filehandle has priority | ||
| 1203 | my $page_type = $portray_data->{ 'TYPE' }; | ||
| 1204 | my $file_name = $portray_data->{ 'FILE_NAME' }; | ||
| 1205 | my $disp_type = $portray_data->{ 'DISPOSITION_TYPE' } || 'inline'; # default, rest must be handled as 'attachment', ref: rfc6266#section-4.2 | ||
| 1206 | |||
| 1207 | my $page_type_is_text = $page_type =~ /^text\//i; | ||
| 1208 | |||
| 1209 | if( lc $page_type =~ /^text\/html/ ) | ||
| 1210 | { | ||
| 1211 | my $prep_opt1 = {}; | ||
| 1212 | $page_data = $self->prep_process( $page, $page_data, $prep_opt1 ); | ||
| 1213 | |||
| 1214 | #print STDERR Dumper( 'OPT1 PREP --- ' x 11, $prep_opt1); | ||
| 1215 | |||
| 1216 | my $prep_opt2 = {}; | ||
| 1217 | $page_data = $self->prep_process( $page, $page_data, $prep_opt2 ) if $prep_opt1->{ 'SECOND_PASS_REQUIRED' }; | ||
| 1218 | |||
| 1219 | #print STDERR Dumper( 'PAGE DATA --- ' x 11, $page, $page_data ); | ||
| 1220 | |||
| 1221 | # FIXME: translation | ||
| 1222 | $self->load_trans(); | ||
| 1223 | my $tr = $self->{ 'TRANS' }{ $self->{ 'ENV' }{ 'LANG' } } || {}; | ||
| 1224 | $page_data =~ s/\<~([^\<\>]*)\>/$tr->{ $1 } || $1/ge; | ||
| 1225 | $page_data =~ s/\[~([^\[\]]*)\]/$tr->{ $1 } || $1/ge; | ||
| 1226 | } | ||
| 1227 | |||
| 1228 | # FIXME: charset | ||
| 1229 | $self->set_headers( 'content-type' => $page_type ); | ||
| 1230 | $self->set_headers( 'content-disposition' => "$disp_type; filename=$file_name" ) if $file_name; | ||
| 1231 | |||
| 1232 | my $http_csp = $self->{ 'ENV' }{ 'HTTP_CSP' }; # || " default-src 'self' "; | ||
| 1233 | $self->set_headers( 'Content-Security-Policy' => $http_csp ) if $http_csp; | ||
| 1234 | |||
| 1235 | my $app_charset = uc $self->{ 'ENV' }{ 'APP_CHARSET' } || 'UTF-8'; | ||
| 1236 | if( $page_type_is_text ) | ||
| 1237 | { | ||
| 1238 | # set charset for TEXT only | ||
| 1239 | $self->set_headers( 'content-charset' => $app_charset ); | ||
| 1240 | } | ||
| 1241 | |||
| 1242 | my $page_headers = $self->__make_headers(); | ||
| 1243 | |||
| 1244 | print $page_headers; | ||
| 1245 | if( $page_fh ) | ||
| 1246 | { | ||
| 1247 | my $buf_size = 1024*1024; | ||
| 1248 | my $print_data; | ||
| 1249 | while(4) | ||
| 1250 | { | ||
| 1251 | my $read_size = read( $page_fh, $print_data, $buf_size ); | ||
| 1252 | print $print_data; | ||
| 1253 | last if $read_size < $buf_size; | ||
| 1254 | } | ||
| 1255 | } | ||
| 1256 | else | ||
| 1257 | { | ||
| 1258 | # TODO: FIXME: check app-charset and convert only if needed | ||
| 1259 | #print $page_type_is_text ? encode( $app_charset, $page_data ) : $page_data; | ||
| 1260 | #print "[[$page_type_is_text|$app_charset]]"; | ||
| 1261 | print $page_data; | ||
| 1262 | } | ||
| 1263 | |||
| 1264 | $self->log_debug( "debug: page response content: page, action, type, headers:\n" . Dumper( $page, $action, $page_type, $page_headers ) ) if $self->is_debug() > 2; | ||
| 1265 | |||
| 1266 | if( $self->is_debug() > 1 and lc $page_type =~ /^text\/html/ ) | ||
| 1267 | { | ||
| 1268 | my $psid = $self->get_page_session_id( 0 ) || 'empty'; | ||
| 1269 | my $rsid = $self->get_page_session_id( 1 ) || 'empty'; | ||
| 1270 | print "<hr><pre>[$rsid] << [$psid]</pre>"; | ||
| 1271 | } | ||
| 1272 | if( $self->is_debug() > 2 and lc $page_type =~ /^text\/html/ ) | ||
| 1273 | { | ||
| 1274 | local $Data::Dumper::Sortkeys = 1; | ||
| 1275 | local $Data::Dumper::Terse = 1; | ||
| 1276 | local $Data::Dumper::Indent = 3; | ||
| 1277 | print "<hr><pre>"; | ||
| 1278 | print Dumper( 'USER INPUT:'.'_'x80, $self->{ 'INPUT_USER_HR' } ); | ||
| 1279 | print Dumper( 'SAFE INPUT:'.'_'x80, $self->{ 'INPUT_SAFE_HR' } ); | ||
| 1280 | print Dumper( 'PAGE SESSION:'.'_'x80, $self->{ 'SESSIONS' }{ 'DATA' }{ 'PAGE' }{ $self->{ 'SESSIONS' }{ 'SID' }{ 'PAGE' } } ); | ||
| 1281 | print Dumper( 'USER SESSION:'.'_'x80, $self->{ 'SESSIONS' }{ 'DATA' }{ 'USER' }{ $self->{ 'SESSIONS' }{ 'SID' }{ 'USER' } } ); | ||
| 1282 | # print "<hr>"; | ||
| 1283 | # print Dumper( $self ); | ||
| 1284 | print "</pre><hr>"; | ||
| 1285 | } | ||
| 1286 | |||
| 1287 | sink 'CONTENT'; | ||
| 1288 | } | ||
| 1289 | |||
| 1290 | my %SIMPLE_PORTRAY_TYPE_MAP = ( | ||
| 1291 | html => 'text/html', | ||
| 1292 | text => 'text/plain', | ||
| 1293 | txt => 'text/plain', | ||
| 1294 | jpeg => 'image/jpeg', | ||
| 1295 | png => 'image/png', | ||
| 1296 | bin => 'application/octet-stream', | ||
| 1297 | ); | ||
| 1298 | |||
| 1299 | sub portray | ||
| 1300 | { | ||
| 1301 | my $self = shift; | ||
| 1302 | my $data = shift; | ||
| 1303 | my $type = lc shift; # mime type text/html | ||
| 1304 | my %opt = @_; # file name, charset, etc. | ||
| 1305 | |||
| 1306 | $type = $SIMPLE_PORTRAY_TYPE_MAP{ $type } || $type; | ||
| 1307 | |||
| 1308 | boom "portray needs mime type xxx/xxx as arg 2, got [$type]" unless $type =~ /^[a-z\-_0-9]+\/[a-z\-_0-9]+$/; | ||
| 1309 | |||
| 1310 | return { DATA => $data, TYPE => $type, @_ }; | ||
| 1311 | } | ||
| 1312 | |||
| 1313 | ############################################################################## | ||
| 1314 | |||
| 1315 | sub forward_url | ||
| 1316 | { | ||
| 1317 | my $self = shift; | ||
| 1318 | my $url = shift; | ||
| 1319 | |||
| 1320 | # FIXME: use render+portray | ||
| 1321 | $self->set_headers( location => $url ); | ||
| 1322 | |||
| 1323 | my $page_headers = $self->__make_headers(); | ||
| 1324 | print $page_headers; | ||
| 1325 | |||
| 1326 | sink 'CONTENT'; | ||
| 1327 | } | ||
| 1328 | |||
| 1329 | sub forward | ||
| 1330 | { | ||
| 1331 | my $self = shift; | ||
| 1332 | |||
| 1333 | boom "expected even number of arguments" unless @_ % 2 == 0; | ||
| 1334 | |||
| 1335 | my $fw = $self->args( @_ ); | ||
| 1336 | return $self->forward_url( "?_=$fw" ); | ||
| 1337 | } | ||
| 1338 | |||
| 1339 | sub forward_type | ||
| 1340 | { | ||
| 1341 | my $self = shift; | ||
| 1342 | |||
| 1343 | boom "expected odd number of arguments" if @_ % 2 == 0; | ||
| 1344 | |||
| 1345 | my $fw = $self->args_type( @_ ); | ||
| 1346 | return $self->forward_url( "?_=$fw" ); | ||
| 1347 | } | ||
| 1348 | |||
| 1349 | sub forward_here | ||
| 1350 | { | ||
| 1351 | my $self = shift; | ||
| 1352 | |||
| 1353 | boom "expected even number of arguments" unless @_ % 2 == 0; | ||
| 1354 | |||
| 1355 | my $fw = $self->args_here( @_ ); | ||
| 1356 | return $self->forward_url( "?_=$fw" ); | ||
| 1357 | } | ||
| 1358 | |||
| 1359 | sub forward_back | ||
| 1360 | { | ||
| 1361 | my $self = shift; | ||
| 1362 | |||
| 1363 | boom "expected even number of arguments" unless @_ % 2 == 0; | ||
| 1364 | |||
| 1365 | my $fw = $self->args_back( @_ ); | ||
| 1366 | return $self->forward_url( "?_=$fw" ); | ||
| 1367 | } | ||
| 1368 | |||
| 1369 | sub forward_back_back | ||
| 1370 | { | ||
| 1371 | my $self = shift; | ||
| 1372 | |||
| 1373 | boom "expected even number of arguments" unless @_ % 2 == 0; | ||
| 1374 | |||
| 1375 | my $fw = $self->args_back_back( @_ ); | ||
| 1376 | return $self->forward_url( "?_=$fw" ); | ||
| 1377 | } | ||
| 1378 | |||
| 1379 | sub forward_new | ||
| 1380 | { | ||
| 1381 | my $self = shift; | ||
| 1382 | |||
| 1383 | boom "expected even number of arguments" unless @_ % 2 == 0; | ||
| 1384 | |||
| 1385 | my $fw = $self->args_new( @_ ); | ||
| 1386 | return $self->forward_url( "?_=$fw" ); | ||
| 1387 | } | ||
| 1388 | |||
| 1389 | sub forward_new_page | ||
| 1390 | { | ||
| 1391 | my $self = shift; | ||
| 1392 | my $page = shift; | ||
| 1393 | |||
| 1394 | boom "expected page name + even number of arguments" unless @_ % 2 == 0; | ||
| 1395 | |||
| 1396 | return $self->forward_new( _PN => $page, @_ ); | ||
| 1397 | } | ||
| 1398 | |||
| 1399 | sub forward_new_action | ||
| 1400 | { | ||
| 1401 | my $self = shift; | ||
| 1402 | my $actn = shift; | ||
| 1403 | |||
| 1404 | boom "expected action name + even number of arguments" unless @_ % 2 == 0; | ||
| 1405 | |||
| 1406 | return $self->forward_new( _AN => $actn, @_ ); | ||
| 1407 | } | ||
| 1408 | |||
| 1409 | ############################################################################## | ||
| 1410 | ## | ||
| 1411 | ## helpers | ||
| 1412 | ## | ||
| 1413 | |||
| 1414 | sub __param | ||
| 1415 | { | ||
| 1416 | my $self = shift; | ||
| 1417 | my $save = shift; # 0 not save, 1 save in cache, 2 save in cache and page session (ps) | ||
| 1418 | my $safe = shift; # 0 user (unsafe) input, 1 safe input | ||
| 1419 | |||
| 1420 | my $input_hr; | ||
| 1421 | my $save_key; | ||
| 1422 | if( $safe ) | ||
| 1423 | { | ||
| 1424 | $input_hr = $self->get_safe_input(); | ||
| 1425 | $save_key = 'SAVE_SAFE_INPUT'; | ||
| 1426 | } | ||
| 1427 | else | ||
| 1428 | { | ||
| 1429 | $input_hr = $self->get_user_input(); | ||
| 1430 | $save_key = 'SAVE_USER_INPUT'; | ||
| 1431 | } | ||
| 1432 | |||
| 1433 | my $ps = $self->get_page_session(); | ||
| 1434 | |||
| 1435 | $ps->{ $save_key } ||= {} if $save > 0; | ||
| 1436 | |||
| 1437 | my @res; | ||
| 1438 | while( @_ ) | ||
| 1439 | { | ||
| 1440 | my $p = uc shift; | ||
| 1441 | if( $save > 0 ) | ||
| 1442 | { | ||
| 1443 | if( exists $input_hr->{ $p } ) | ||
| 1444 | { | ||
| 1445 | $ps->{ $save_key }{ $p } = $input_hr->{ $p }; | ||
| 1446 | $ps->{ $p } = $input_hr->{ $p } if $save > 1; | ||
| 1447 | } | ||
| 1448 | push @res, $ps->{ $save_key }{ $p }; | ||
| 1449 | } | ||
| 1450 | else | ||
| 1451 | { | ||
| 1452 | push @res, $input_hr->{ $p }; | ||
| 1453 | } | ||
| 1454 | } | ||
| 1455 | |||
| 1456 | return wantarray ? @res : shift( @res ); | ||
| 1457 | } | ||
| 1458 | |||
| 1459 | sub param_unsafe | ||
| 1460 | { | ||
| 1461 | my $self = shift; | ||
| 1462 | return $self->__param( 1, 0, @_ ); | ||
| 1463 | } | ||
| 1464 | |||
| 1465 | sub param | ||
| 1466 | { | ||
| 1467 | my $self = shift; | ||
| 1468 | return $self->__param( 1, 1, @_ ); | ||
| 1469 | } | ||
| 1470 | |||
| 1471 | sub param_save | ||
| 1472 | { | ||
| 1473 | my $self = shift; | ||
| 1474 | return $self->__param( 2, 1, @_ ); | ||
| 1475 | } | ||
| 1476 | |||
| 1477 | sub param_safe | ||
| 1478 | { | ||
| 1479 | my $self = shift; | ||
| 1480 | return $self->param( @_ ); | ||
| 1481 | } | ||
| 1482 | |||
| 1483 | sub param_peek_unsafe | ||
| 1484 | { | ||
| 1485 | my $self = shift; | ||
| 1486 | return $self->__param( 0, 0, @_ ); | ||
| 1487 | } | ||
| 1488 | |||
| 1489 | sub param_peek | ||
| 1490 | { | ||
| 1491 | my $self = shift; | ||
| 1492 | return $self->__param( 0, 1, @_ ); | ||
| 1493 | } | ||
| 1494 | |||
| 1495 | sub param_peek_safe | ||
| 1496 | { | ||
| 1497 | my $self = shift; | ||
| 1498 | return $self->param_peek( @_ ); | ||
| 1499 | } | ||
| 1500 | |||
| 1501 | sub param_clear_cache | ||
| 1502 | { | ||
| 1503 | my $self = shift; | ||
| 1504 | |||
| 1505 | my $ps = $self->get_page_session(); | ||
| 1506 | |||
| 1507 | while( @_ ) | ||
| 1508 | { | ||
| 1509 | my $p = uc shift; | ||
| 1510 | delete $ps->{ 'SAVE_SAFE_INPUT' }{ $p }; | ||
| 1511 | delete $ps->{ 'SAVE_USER_INPUT' }{ $p }; | ||
| 1512 | } | ||
| 1513 | |||
| 1514 | return 1; | ||
| 1515 | } | ||
| 1516 | |||
| 1517 | |||
| 1518 | sub is_logged_in | ||
| 1519 | { | ||
| 1520 | my $self = shift; | ||
| 1521 | |||
| 1522 | my $user_shr = $self->get_user_session(); | ||
| 1523 | return $user_shr->{ ':LOGGED_IN' } ? 1 : 0; | ||
| 1524 | } | ||
| 1525 | |||
| 1526 | sub login | ||
| 1527 | { | ||
| 1528 | my $self = shift; | ||
| 1529 | |||
| 1530 | my $user_shr = $self->get_user_session(); | ||
| 1531 | $user_shr->{ ':LOGGED_IN' } = 1; | ||
| 1532 | $user_shr->{ ':LTIME' } = time(); | ||
| 1533 | $user_shr->{ ':LTIME_STR' } = scalar localtime(); | ||
| 1534 | # FIXME: add more login info | ||
| 1535 | } | ||
| 1536 | |||
| 1537 | sub logout | ||
| 1538 | { | ||
| 1539 | my $self = shift; | ||
| 1540 | |||
| 1541 | my $user_shr = $self->get_user_session(); | ||
| 1542 | $user_shr->{ ':LOGGED_IN' } = 0; | ||
| 1543 | $user_shr->{ ':CLOSED' } = 1; | ||
| 1544 | $user_shr->{ ':ETIME' } = time(); | ||
| 1545 | $user_shr->{ ':ETIME_STR' } = scalar localtime(); | ||
| 1546 | # FIXME: add more logout info | ||
| 1547 | my ( $user_sid, $user_shr ) = $self->__create_new_user_session(); | ||
| 1548 | } | ||
| 1549 | |||
| 1550 | # FIXME: s? | ||
| 1551 | sub need_login | ||
| 1552 | { | ||
| 1553 | my $self = shift; | ||
| 1554 | |||
| 1555 | return if $self->is_logged_in(); | ||
| 1556 | |||
| 1557 | my $fw = $self->args_new( _PN => 'login' ); | ||
| 1558 | return $self->forward_url( "?_=$fw" ); | ||
| 1559 | |||
| 1560 | # return $self->forward( _PN => 'login' ); | ||
| 1561 | } | ||
| 1562 | |||
| 1563 | sub set_user_session_expire_time | ||
| 1564 | { | ||
| 1565 | my $self = shift; | ||
| 1566 | my $xtime = shift; | ||
| 1567 | |||
| 1568 | #use Exception::Sink; | ||
| 1569 | #my $xtt = localtime( $xtime ); | ||
| 1570 | #print STDERR "set_user_session_expire_time($xtime)[$xtt]\n" . Exception::Sink::get_stack_trace(); | ||
| 1571 | |||
| 1572 | my $user_shr = $self->get_user_session(); | ||
| 1573 | $user_shr->{ ':XTIME' } = $xtime; # FIXME: sanity? | ||
| 1574 | $user_shr->{ ':XTIME_STR' } = scalar localtime $user_shr->{ ':XTIME' }; | ||
| 1575 | return exists $user_shr->{ ':XTIME' } ? $user_shr->{ ':XTIME' } : undef; | ||
| 1576 | } | ||
| 1577 | |||
| 1578 | sub set_user_session_expire_time_in | ||
| 1579 | { | ||
| 1580 | my $self = shift; | ||
| 1581 | my $seconds = shift; | ||
| 1582 | |||
| 1583 | #use Exception::Sink; | ||
| 1584 | #print STDERR "set_user_session_expire_time_in($seconds)\n" . Exception::Sink::get_stack_trace(); | ||
| 1585 | |||
| 1586 | # FIXME: support for more user friendly time periods 10m 60s | ||
| 1587 | return $self->set_user_session_expire_time( time() + $seconds ); | ||
| 1588 | } | ||
| 1589 | |||
| 1590 | # returns unix time at which user session will expire, undef if no expire time specified | ||
| 1591 | sub get_user_session_expire_time | ||
| 1592 | { | ||
| 1593 | my $self = shift; | ||
| 1594 | |||
| 1595 | my $user_shr = $self->get_user_session(); | ||
| 1596 | |||
| 1597 | #use Exception::Sink; | ||
| 1598 | #my $xtt = localtime( $user_shr->{ ':XTIME' } ); | ||
| 1599 | #print STDERR "get_user_session_expire_time($user_shr->{ ':XTIME' })[$xtt]\n" . Exception::Sink::get_stack_trace(); | ||
| 1600 | |||
| 1601 | return exists $user_shr->{ ':XTIME' } ? $user_shr->{ ':XTIME' } : undef; | ||
| 1602 | } | ||
| 1603 | |||
| 1604 | # returns time period in seconds, in which user session will expire, undef if no expire time specified | ||
| 1605 | sub get_user_session_expire_time_in | ||
| 1606 | { | ||
| 1607 | my $self = shift; | ||
| 1608 | |||
| 1609 | my $xi = $self->get_user_session_expire_time() - time(); | ||
| 1610 | return $xi > 0 ? $xi : undef; | ||
| 1611 | } | ||
| 1612 | |||
| 1613 | sub need_post_method | ||
| 1614 | { | ||
| 1615 | my $self = shift; | ||
| 1616 | |||
| 1617 | my $he = $self->get_http_env(); | ||
| 1618 | |||
| 1619 | return if $he->{ 'REQUEST_METHOD' } eq 'POST'; | ||
| 1620 | |||
| 1621 | $self->logout(); | ||
| 1622 | $self->render( PAGE => 'epostrequired' ); | ||
| 1623 | } | ||
| 1624 | |||
| 1625 | sub get_user_session_agent | ||
| 1626 | { | ||
| 1627 | my $self = shift; | ||
| 1628 | |||
| 1629 | my $user_session = $self->get_user_session(); | ||
| 1630 | my $user_agent = $user_session->{ ':HTTP_ENV_HR' }{ 'HTTP_USER_AGENT' }; | ||
| 1631 | |||
| 1632 | return $user_agent || 'n/a'; | ||
| 1633 | } | ||
| 1634 | |||
| 1635 | ############################################################################## | ||
| 1636 | |||
| 1637 | sub load_trans | ||
| 1638 | { | ||
| 1639 | my $self = shift; | ||
| 1640 | |||
| 1641 | my $lang = lc $self->{ 'ENV' }{ 'LANG' }; | ||
| 1642 | |||
| 1643 | return 0 if $lang !~ /^[a-z][a-z]$/; # FIXME: move to init check! verofy hash etc. data::tools | ||
| 1644 | |||
| 1645 | $self->{ 'TRANS' }{ 'LANG' } = $lang; | ||
| 1646 | |||
| 1647 | return 1 if $self->{ 'TRANS' }{ $lang }; | ||
| 1648 | |||
| 1649 | my $tr = $self->{ 'TRANS' }{ $lang } = {}; | ||
| 1650 | |||
| 1651 | my $trans_dirs = $self->{ 'ENV' }{ 'TRANS_DIRS' }; | ||
| 1652 | |||
| 1653 | my @tf; | ||
| 1654 | for my $dir ( @$trans_dirs ) | ||
| 1655 | { | ||
| 1656 | push @tf, glob( "$dir/$lang/*.tr" ); | ||
| 1657 | push @tf, glob( "$dir/$lang/text/*.tr" ); | ||
| 1658 | } | ||
| 1659 | |||
| 1660 | for my $tf ( @tf ) | ||
| 1661 | { | ||
| 1662 | my $hr = $self->load_trans_file( $tf ); | ||
| 1663 | # trim whitespace | ||
| 1664 | my @temp = %$hr; | ||
| 1665 | for( @temp ) | ||
| 1666 | { | ||
| 1667 | s/^\s*//; | ||
| 1668 | s/\s*$//; | ||
| 1669 | } | ||
| 1670 | %$hr = @temp; | ||
| 1671 | @temp = (); | ||
| 1672 | @{ $tr }{ keys %$hr } = values %$hr; | ||
| 1673 | } | ||
| 1674 | |||
| 1675 | return 1; | ||
| 1676 | } | ||
| 1677 | |||
| 1678 | sub load_trans_file | ||
| 1679 | { | ||
| 1680 | my $self = shift; | ||
| 1681 | |||
| 1682 | return hash_load( shift ); | ||
| 1683 | } | ||
| 1684 | |||
| 1685 | ############################################################################## | ||
| 1686 | ## | ||
| 1687 | ## REO proxies | ||
| 1688 | ## | ||
| 1689 | |||
| 1690 | # FIXME: should actions have access to low-level session handling? | ||
| 1691 | sub sess_create { my $self = shift; $self->{ 'REO_SESS' }->create( @_ ) }; | ||
| 1692 | sub sess_delete { my $self = shift; $self->{ 'REO_SESS' }->delete( @_ ) }; | ||
| 1693 | sub sess_load { my $self = shift; $self->{ 'REO_SESS' }->load( @_ ) }; | ||
| 1694 | sub sess_save { my $self = shift; $self->{ 'REO_SESS' }->save( @_ ) }; | ||
| 1695 | sub sess_exists { my $self = shift; $self->{ 'REO_SESS' }->exists( @_ ) }; | ||
| 1696 | |||
| 1697 | #sub prep_render { my $self = shift; $self->{ 'REO_PREP' }->render( @_ ) }; | ||
| 1698 | sub prep_load_page { my $self = shift; $self->{ 'REO_PREP' }->load_page( @_ ) }; | ||
| 1699 | sub prep_load_file { my $self = shift; $self->{ 'REO_PREP' }->load_file( @_ ) }; | ||
| 1700 | sub prep_process { my $self = shift; $self->{ 'REO_PREP' }->process( @_ ) }; | ||
| 1701 | |||
| 1702 | sub action_call { my $self = shift; $self->{ 'REO_ACTS' }->call( @_ ) }; | ||
| 1703 | |||
| 1704 | sub new_form | ||
| 1705 | { | ||
| 1706 | my $self = shift; | ||
| 1707 | |||
| 1708 | my $form = new Web::Reactor::HTML::Form( @_, REO_REACTOR => $self ); | ||
| 1709 | |||
| 1710 | return $form; | ||
| 1711 | } | ||
| 1712 | |||
| 1713 | ############################################################################## | ||
| 1714 | |||
| 1715 | sub set_browser_window_title | ||
| 1716 | { | ||
| 1717 | my $self = shift; | ||
| 1718 | |||
| 1719 | $self->html_content( 'BROWSER_WINDOW_TITLE', shift() ); | ||
| 1720 | } | ||
| 1721 | |||
| 1722 | ############################################################################## | ||
| 1723 | |||
| 1724 | sub html_new_id | ||
| 1725 | { | ||
| 1726 | my $self = shift; | ||
| 1727 | |||
| 1728 | my $psid = $self->get_page_session_id(); | ||
| 1729 | $self->{ 'HTML_ID_COUNTER' }++; | ||
| 1730 | # FIXME: hash $psid once more to hide... | ||
| 1731 | return "REO_EID_$psid\_" . $self->{ 'HTML_ID_COUNTER' }; | ||
| 1732 | } | ||
| 1733 | |||
| 1734 | ############################################################################## | ||
| 1735 | |||
| 1736 | |||
| 1737 | =pod | ||
| 1738 | |||
| 1739 | =head1 NAME | ||
| 1740 | |||
| 1741 | Web::Reactor perl-based web application machinery. | ||
| 1742 | |||
| 1743 | =head1 SYNOPSIS | ||
| 1744 | |||
| 1745 | Startup CGI script example: | ||
| 1746 | |||
| 1747 | #!/usr/bin/perl | ||
| 1748 | use strict; | ||
| 1749 | use lib '/opt/perl/reactor/lib'; # if Reactor is custom location installed | ||
| 1750 | use Web::Reactor; | ||
| 1751 | |||
| 1752 | my %cfg = ( | ||
| 1753 | 'APP_NAME' => 'demo', | ||
| 1754 | 'APP_ROOT' => '/opt/reactor/demo/', | ||
| 1755 | 'LIB_DIRS' => [ '/opt/reactor/demo/lib/' ], | ||
| 1756 | 'ACTIONS_SETS' => [ 'demo', 'Base', 'Core' ], | ||
| 1757 | 'HTML_DIRS' => [ '/opt/reactor/demo/html/' ], | ||
| 1758 | 'SESS_VAR_DIR' => '/opt/reactor/demo/var/sess/', | ||
| 1759 | 'DEBUG' => 4, | ||
| 1760 | ); | ||
| 1761 | |||
| 1762 | eval { new Web::Reactor( %cfg )->run(); }; | ||
| 1763 | if( $@ ) | ||
| 1764 | { | ||
| 1765 | print STDERR "REACTOR CGI EXCEPTION: $@"; | ||
| 1766 | print "content-type: text/html\n\nsystem is temporary unavailable"; | ||
| 1767 | } | ||
| 1768 | |||
| 1769 | =head1 INTRODUCTION | ||
| 1770 | |||
| 1771 | Web::Reactor is a perl module which automates as much as possible of the all | ||
| 1772 | routine tasks when implementing web applications, interactive sites, etc. | ||
| 1773 | Main task is to handle all the repetative work and adding more comfortable | ||
| 1774 | functionality like: | ||
| 1775 | |||
| 1776 | * setting and recognising web browser cookies (for sessions or other data) | ||
| 1777 | * handling user and page sessions (storage, cookie management, etc.) | ||
| 1778 | * hiding html link data and forms data to rise page-to-page transfer safety. | ||
| 1779 | * preprocessing of text/html, including hiding data, calling actions etc. | ||
| 1780 | * on-demand loading of 'actions', perl code modules to handle dynamic pages. | ||
| 1781 | |||
| 1782 | Web::Reactor can be extended, though it was not supposed to. There are 4 main | ||
| 1783 | parts of it which can be extended. See section EXTENDING below for details. | ||
| 1784 | |||
| 1785 | =head1 EXAMPLES | ||
| 1786 | |||
| 1787 | HTML page file example: | ||
| 1788 | |||
| 1789 | <#html_header> | ||
| 1790 | |||
| 1791 | <$app_name> | ||
| 1792 | |||
| 1793 | <#menu> | ||
| 1794 | |||
| 1795 | testing page html file | ||
| 1796 | |||
| 1797 | action test: <&test> | ||
| 1798 | |||
| 1799 | <#html_footer> | ||
| 1800 | |||
| 1801 | Action module example: | ||
| 1802 | |||
| 1803 | package Reactor::Actions::demo::test; | ||
| 1804 | use strict; | ||
| 1805 | use Data::Dumper; | ||
| 1806 | use Web::Reactor::HTML::FormEngine; | ||
| 1807 | |||
| 1808 | sub main | ||
| 1809 | { | ||
| 1810 | my $reo = shift; # Web::Reactor object. Provides all API and context. | ||
| 1811 | |||
| 1812 | my $text; # result html text | ||
| 1813 | |||
| 1814 | if( $reo->get_input_button() eq 'FORM_CANCEL' ) | ||
| 1815 | { | ||
| 1816 | # if clicked form button is cancel, | ||
| 1817 | # return back to the calling/previous page/view with optional data | ||
| 1818 | return $reo->forward_back( ACTION_RETURN => 'IS_CANCEL' ); | ||
| 1819 | } | ||
| 1820 | |||
| 1821 | # add some html content | ||
| 1822 | $text .= "<p>Reactor::Actions::demo::test here!<p>"; | ||
| 1823 | |||
| 1824 | # create link and hide its data. only accessible from inside web app. | ||
| 1825 | my $grid_href = $reo->args_new( _PN => 'grid', TABLE => 'testtable', ); | ||
| 1826 | $text .= "<a href=?_=$grid_href>go to grid</a><p>"; | ||
| 1827 | |||
| 1828 | # access page session. it will be auto-loaded on demand | ||
| 1829 | my $page_session_hr = $reo->get_page_session(); | ||
| 1830 | my $fortune = $page_session_hr->{ 'FORTUNE' } ||= `/usr/games/fortune`; | ||
| 1831 | |||
| 1832 | # access input (form) data. $i and $e are hashrefs | ||
| 1833 | my $i = $reo->get_user_input(); # get plain user input (hashref) | ||
| 1834 | my $e = $reo->get_safe_input(); # get safe data (never reach user browser) | ||
| 1835 | |||
| 1836 | $text .= "<p><hr><p>$fortune<hr>"; | ||
| 1837 | |||
| 1838 | my $bc = $reo->args_here(); # session keeper, this is manual use | ||
| 1839 | |||
| 1840 | $text .= "<form method=post>"; | ||
| 1841 | $text .= "<input type=hidden name=_ value=$bc>"; | ||
| 1842 | $text .= "input <input name=inp>"; | ||
| 1843 | $text .= "<input type=submit name=button:form_ok>"; | ||
| 1844 | $text .= "<input type=submit name=button:form_cancel>"; | ||
| 1845 | $text .= "</form>"; | ||
| 1846 | |||
| 1847 | my $form = $reo->new_form(); | ||
| 1848 | |||
| 1849 | $text .= "<p><hr><p>"; | ||
| 1850 | |||
| 1851 | return $text; | ||
| 1852 | } | ||
| 1853 | |||
| 1854 | 1; | ||
| 1855 | |||
| 1856 | =head1 PAGE NAMES, HTML FILE TEMPLATES, PAGE INSTANCES | ||
| 1857 | |||
| 1858 | Web::Reactor has a notion of a "page" which represents visible output to the | ||
| 1859 | end user browser. It has (i.e. uses) the following attributes: | ||
| 1860 | |||
| 1861 | * html file template (page name) | ||
| 1862 | * page session data | ||
| 1863 | * actions code (i.e. callbacks) used inside html text | ||
| 1864 | |||
| 1865 | All of those represent "page instance" and produce end user html visible page. | ||
| 1866 | |||
| 1867 | "Page names" are strictly limited to be alphanumeric and are mapped to file | ||
| 1868 | (or other storage) html content: | ||
| 1869 | |||
| 1870 | page name: example | ||
| 1871 | html file template will be: page_example.html | ||
| 1872 | |||
| 1873 | HTML content may include other files (also limited to be alphanumeric): | ||
| 1874 | |||
| 1875 | include text: <#other_file> | ||
| 1876 | file included: other_file.html | ||
| 1877 | directories searched: 'HTML_DIRS' from Web::Reactor parameters. | ||
| 1878 | |||
| 1879 | Page names may be requested from the end user side, but include html files may | ||
| 1880 | be used only from the pages already requested. | ||
| 1881 | |||
| 1882 | =head1 ACTIONS/MODULES/CALLBACKS | ||
| 1883 | |||
| 1884 | Actions are loaded and executed by package names. In the HTML source files they | ||
| 1885 | can be called this way: | ||
| 1886 | |||
| 1887 | <&test_action arg1=val1 arg2=val2 flag1 flag2...> | ||
| 1888 | <&test_action> | ||
| 1889 | |||
| 1890 | This will instruct Reactor action handler to look for this package name inside | ||
| 1891 | standard or user-added library directories: | ||
| 1892 | |||
| 1893 | Web/Reactor/Actions/*/test_action.pm | ||
| 1894 | |||
| 1895 | Asterisk will be replaced with the name of the used "action sets" give in config | ||
| 1896 | hash: | ||
| 1897 | |||
| 1898 | 'ACTIONS_SETS' => [ 'demo', 'Base', 'Core' ], | ||
| 1899 | |||
| 1900 | So the result list in this example will be: | ||
| 1901 | |||
| 1902 | Web/Reactor/Actions/demo/test_action.pm | ||
| 1903 | Web/Reactor/Actions/Base/test_action.pm | ||
| 1904 | Web/Reactor/Actions/Core/test_action.pm | ||
| 1905 | |||
| 1906 | This is used to allow overriding of standard modules or modules you dont have | ||
| 1907 | write access to. | ||
| 1908 | |||
| 1909 | Another way to call a module is directly from another module code with: | ||
| 1910 | |||
| 1911 | $reo->action_call( 'test_action', @args ); | ||
| 1912 | |||
| 1913 | The package file will look like this: | ||
| 1914 | |||
| 1915 | package Web/Reactor/Actions/demo/test_action; | ||
| 1916 | use strict; | ||
| 1917 | |||
| 1918 | sub main | ||
| 1919 | { | ||
| 1920 | my $reo = shift; # Web::Reactor object/instance | ||
| 1921 | my %args = @_; # all args passed to the action | ||
| 1922 | |||
| 1923 | my $html_args = $args{ 'HTML_ARGS' }; # all | ||
| 1924 | ... | ||
| 1925 | return $result_data; # usually html text | ||
| 1926 | } | ||
| 1927 | |||
| 1928 | $html_args is hashref with all args give inside the html code if this action | ||
| 1929 | is called from a html text. If you look the example above: | ||
| 1930 | |||
| 1931 | <&test_action arg1=val1 arg2=val2 flag1 flag2...> | ||
| 1932 | |||
| 1933 | The $html_args will look like this: | ||
| 1934 | |||
| 1935 | $html_args = { | ||
| 1936 | 'arg1' => 'val1', | ||
| 1937 | 'arg2' => 'val2', | ||
| 1938 | 'flag1' => 1, | ||
| 1939 | 'flag2' => 1, | ||
| 1940 | }; | ||
| 1941 | |||
| 1942 | |||
| 1943 | |||
| 1944 | =head1 HTTP PARAMETERS NAMES | ||
| 1945 | |||
| 1946 | Web::Reactor uses underscore and one or two letters for its system http/html | ||
| 1947 | parameters. Some of the system params are: | ||
| 1948 | |||
| 1949 | _PN -- html page name (points to file template, restricted to alphanumeric) | ||
| 1950 | _AN -- action name (points to action package name, restricted to alphanumeric) | ||
| 1951 | _P -- page session | ||
| 1952 | _R -- referer (caller) page session | ||
| 1953 | |||
| 1954 | Usually those names should not be directly used or visible inside actions code. | ||
| 1955 | More details about how those params are used can be found below. | ||
| 1956 | |||
| 1957 | =head1 USER SESSIONS | ||
| 1958 | |||
| 1959 | WR creates unique session for each connected user. The session is kept by a cookie. | ||
| 1960 | Usually WR needs justthis cookie to handle all user/server interaction. Inside | ||
| 1961 | WR action code, user session is represented as a hash reference. It may hold | ||
| 1962 | arbitrary data. "System" or WR-specific data inside user session has colon as | ||
| 1963 | prefix: | ||
| 1964 | |||
| 1965 | # $reo is Web::Reactor object (i.e. context) passed to the action/module code | ||
| 1966 | my $user_session = $reo->get_user_session(); | ||
| 1967 | print STDERR $user_session->{ ':CTIME_STR' }; | ||
| 1968 | # prints in http log the create time in human friendly form | ||
| 1969 | |||
| 1970 | All data saved inside user session is automatically saved. When needed it can | ||
| 1971 | be explicitly with: | ||
| 1972 | |||
| 1973 | $reo->save(); | ||
| 1974 | # saves all modified context to disk or other storage | ||
| 1975 | |||
| 1976 | =head1 PAGE SESSIONS | ||
| 1977 | |||
| 1978 | Each page presented to the user has own session. It is very similar to the user | ||
| 1979 | session (it is hash reference, may hold any data, can be saved with $reo->save()). | ||
| 1980 | It is expected that page sessions hold all context data needed for any page to | ||
| 1981 | display properly. To preserve page session it is needed that it is included | ||
| 1982 | in any link to this page instance or in any html form used. | ||
| 1983 | |||
| 1984 | When called for the first time, each page request needs page name (_PN). Afterwards | ||
| 1985 | a unique page session is created and page name is saved inside. At this moment | ||
| 1986 | this page instance can be accessed (i.e. given control to) only with a page | ||
| 1987 | session id (_P): | ||
| 1988 | |||
| 1989 | $page_sid = ...; # taken from somewhere | ||
| 1990 | # to pass control to the page instance: | ||
| 1991 | $reo->forward( _P => $page_sid ); | ||
| 1992 | # the page instance will pull data from its page session and display in | ||
| 1993 | # its last known state | ||
| 1994 | |||
| 1995 | Not always page session are needed. For example, when forward to the caller is | ||
| 1996 | needed, you just need to: | ||
| 1997 | |||
| 1998 | $reo->forward_back(); | ||
| 1999 | # this is equivalent to | ||
| 2000 | my $ref_page_sid = $reo->get_ref_page_session_id(); | ||
| 2001 | $reo->forward( _P => $ref_page_sid ); | ||
| 2002 | |||
| 2003 | Each page instance knows the caller page session and can give control back to. | ||
| 2004 | However it may pass more data when returning back to the caller: | ||
| 2005 | |||
| 2006 | $reo->forward_back( MORE_DATA => 'is here', OPTIONS_LIST => \@list ); | ||
| 2007 | |||
| 2008 | When new page instance has to be called (created): | ||
| 2009 | |||
| 2010 | |||
| 2011 | $reo->forward_new( _PN => 'some_page_name' ); | ||
| 2012 | |||
| 2013 | =head1 CONFIG ENTRIES | ||
| 2014 | |||
| 2015 | Upon creation, Web:Reactor instance gets hash with config entries/keys: | ||
| 2016 | |||
| 2017 | * APP_NAME -- alphanumeric application name (plus underscore) | ||
| 2018 | * APP_ROOT -- application root dir, used for app components search | ||
| 2019 | * LIB_DIRS -- directories from which actions and other libs are loaded | ||
| 2020 | * ACTIONS_SETS -- list of action "sets", appended to ACTIONS_DIRS | ||
| 2021 | * HTML_DIRS -- html file inlude directories | ||
| 2022 | * SESS_VAR_DIR -- used by filesystem session handling to store sess data | ||
| 2023 | * DEBUG -- positive number, enables debugging with verbosity level | ||
| 2024 | |||
| 2025 | Some entries may be omitted and default values are: | ||
| 2026 | |||
| 2027 | * LIB_DIRS -- [ "$APP_ROOT/lib" ] | ||
| 2028 | * ACTIONS_SETS -- [ $APP_NAME, 'Base', 'Core' ] | ||
| 2029 | * HTML_DIRS -- [ "$APP_ROOT/html" ] | ||
| 2030 | * SESS_VAR_DIR -- [ "$APP_ROOT/var" ] | ||
| 2031 | * DEBUG -- 0 | ||
| 2032 | |||
| 2033 | =head1 API FUNCTIONS | ||
| 2034 | |||
| 2035 | # TODO: input | ||
| 2036 | # TODO: sessions | ||
| 2037 | # TODO: arguments, constructing links | ||
| 2038 | # TODO: forwarding | ||
| 2039 | # TODO: html, forms, session keeping | ||
| 2040 | |||
| 2041 | =head1 DEPLOYMENT, DIRECTORIES, FILESYSTEM STRUCTURE | ||
| 2042 | |||
| 2043 | # TODO: install, cpan, manual, github, custom locations | ||
| 2044 | # TODO: sessions dir, custom storage/session handling | ||
| 2045 | |||
| 2046 | =head1 EXTENDING | ||
| 2047 | |||
| 2048 | Web::Reactor is designed to allow extending or replacing the 4 main parts: | ||
| 2049 | |||
| 2050 | * Session storage (data store on filesystem, database, remote or vmem) | ||
| 2051 | |||
| 2052 | base module: Web::Reactor::Sessions | ||
| 2053 | current in use: Web::Reactor::Sessions::Filesystem | ||
| 2054 | |||
| 2055 | * HTML creation/expansion/preprocessing | ||
| 2056 | |||
| 2057 | base module: Web::Reactor::Preprocessor | ||
| 2058 | current in use: Web::Reactor::Preprocessor::Native | ||
| 2059 | |||
| 2060 | * Actions/modules execution (can be skipped if custom HTML prep used) | ||
| 2061 | |||
| 2062 | base module: Web::Reactor::Actions | ||
| 2063 | current in use: Web::Reactor::Actions::Native | ||
| 2064 | |||
| 2065 | * Main Web::Reactor modules, which controlls all the functionality. | ||
| 2066 | |||
| 2067 | base module: Web::Reactor | ||
| 2068 | current in use: Web::Reactor | ||
| 2069 | |||
| 2070 | Except main module (Web::Reactor) is is expected that base modules are | ||
| 2071 | subclassed for extension. Inside each of them there are notes on what must | ||
| 2072 | be extended and usage hints. | ||
| 2073 | |||
| 2074 | Current implementations of the modules, shipped with Web::Reactor, can also | ||
| 2075 | be extended and/or modified. However it is suggested checking base modules | ||
| 2076 | first. | ||
| 2077 | |||
| 2078 | Main module (Web::Reactor) handles all of the logic. It is not expected to | ||
| 2079 | be modified since it is designed to handle tightly all the parts. However, | ||
| 2080 | there are few things which can be modified but it is recommended to contact | ||
| 2081 | authors for an advice first. On the other hand, the main module instance is | ||
| 2082 | always passed as argument to all other modules/actions so it is good idea | ||
| 2083 | to add specific functionality which will be readily available everywhere. | ||
| 2084 | |||
| 2085 | =head1 PROJECT STATUS | ||
| 2086 | |||
| 2087 | Web::Reactor is stable and it is used in many production sites including | ||
| 2088 | banks, insurance, travel and other smaller companies. | ||
| 2089 | |||
| 2090 | API is frozen but it could be extended | ||
| 2091 | |||
| 2092 | If you are interested in the project or have some notes etc, contact me at: | ||
| 2093 | |||
| 2094 | Vladi Belperchinov-Shabanski "Cade" | ||
| 2095 | <cade@noxrun.com> | ||
| 2096 | <cade@cpan.org> | ||
| 2097 | <shabanski@gmail.com> | ||
| 2098 | |||
| 2099 | further contact info, mailing list and github repository is listed below. | ||
| 2100 | |||
| 2101 | =head1 FIXME: TODO: | ||
| 2102 | |||
| 2103 | * config examples | ||
| 2104 | * pages example | ||
| 2105 | * actions example | ||
| 2106 | * API description (input data, safe data, sessions, forwarding, actions, html) | ||
| 2107 | * ... | ||
| 2108 | |||
| 2109 | =head1 REQUIRED ADDITIONAL MODULES | ||
| 2110 | |||
| 2111 | Reactor uses mostly perl core modules but it needs few others: | ||
| 2112 | |||
| 2113 | * CGI | ||
| 2114 | * Scalar::Util | ||
| 2115 | * Hash::Util | ||
| 2116 | * Data::Dumper (for debugging) | ||
| 2117 | * Exception::Sink | ||
| 2118 | * Data::Tools | ||
| 2119 | |||
| 2120 | All modules are available with the perl package or from CPAN. | ||
| 2121 | |||
| 2122 | Additionally, several are available and from github: | ||
| 2123 | |||
| 2124 | * Exception::Sink | ||
| 2125 | https://github.com/cade-vs/perl-exception-sink | ||
| 2126 | |||
| 2127 | * Data::Tools | ||
| 2128 | https://github.com/cade-vs/perl-data-tools | ||
| 2129 | |||
| 2130 | =head1 DEMO APPLICATION | ||
| 2131 | |||
| 2132 | Documentation will be improved. Meanwhile you can check 'demo' | ||
| 2133 | directory inside distribution tarball or inside the github repository. This is | ||
| 2134 | fully functional (however stupid :)) application. It shows how data is processed, | ||
| 2135 | calling pages/views, inspecting page (calling views) stack, html forms automation, | ||
| 2136 | forwarding. | ||
| 2137 | |||
| 2138 | Additionally you may check DECOR information systems infrastructure, which uses | ||
| 2139 | Web::Reactor for its main web interface: | ||
| 2140 | |||
| 2141 | https://github.com/cade-vs/perl-decor | ||
| 2142 | |||
| 2143 | =head1 MAILING LIST | ||
| 2144 | |||
| 2145 | web-reactor@googlegroups.com | ||
| 2146 | |||
| 2147 | =head1 GITHUB REPOSITORY | ||
| 2148 | |||
| 2149 | https://github.com/cade-vs/perl-web-reactor | ||
| 2150 | |||
| 2151 | git clone git://github.com/cade-vs/perl-web-reactor.git | ||
| 2152 | |||
| 2153 | =head1 AUTHOR | ||
| 2154 | |||
| 2155 | Vladi Belperchinov-Shabanski "Cade" | ||
| 2156 | |||
| 2157 | <cade@bis.bg> <cade@cpan.org> <shabanski@gmail.com> | ||
| 2158 | |||
| 2159 | http://cade.noxrun.com | ||
| 2160 | |||
| 2161 | https://github.com/cade-vs | ||
| 2162 | |||
| 2163 | =head2 EOF | ||
| 2164 | |||
| 2165 | =cut | ||
| 2166 | |||
| 2167 | |||
| 2168 | ############################################################################## | ||
| 2169 | 1; | ||
| 2170 | ###EOF######################################################################## |