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