| 1 | ############################################################################## | ||
| 2 | ## | ||
| 3 | ## Web::Reactor application machinery | ||
| 4 | ## 2013 (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 Web::Reactor::Utils; | ||
| 13 | use Web::Reactor::HTML::Form; | ||
| 14 | use Storable qw( dclone freeze thaw ); # FIXME: move to Data::Tools (data_freeze/data_thaw) | ||
| 15 | use CGI; | ||
| 16 | use CGI::Cookie; | ||
| 17 | use Data::Tools; | ||
| 18 | use Data::Dumper; | ||
| 19 | use Exception::Sink; | ||
| 20 | |||
| 21 | our $VERSION = '0.05'; | ||
| 22 | |||
| 23 | ############################################################################## | ||
| 24 | |||
| 25 | #minimum config: | ||
| 26 | #my %cfg = ( | ||
| 27 | # '' => , | ||
| 28 | # ); | ||
| 29 | |||
| 30 | our @HTTP_VARS_CHECK = qw( | ||
| 31 | REMOTE_ADDR | ||
| 32 | HTTP_USER_AGENT | ||
| 33 | ); | ||
| 34 | |||
| 35 | our @HTTP_VARS_SAVE = qw( | ||
| 36 | REMOTE_ADDR | ||
| 37 | REMOTE_PORT | ||
| 38 | REQUEST_METHOD | ||
| 39 | REQUEST_URI | ||
| 40 | HTTP_REFERRER | ||
| 41 | QUERY_STRING | ||
| 42 | HTTP_COOKIE | ||
| 43 | HTTP_USER_AGENT | ||
| 44 | ); | ||
| 45 | |||
| 46 | our %ENV_ALLOWED_KEYS = { | ||
| 47 | |||
| 48 | }; | ||
| 49 | |||
| 50 | ############################################################################## | ||
| 51 | |||
| 52 | sub new | ||
| 53 | { | ||
| 54 | my $class = shift; | ||
| 55 | my %env = @_; | ||
| 56 | |||
| 57 | # FIXME: verify %env content! Data::Validate::Struct | ||
| 58 | |||
| 59 | $class = ref( $class ) || $class; | ||
| 60 | my $self = { | ||
| 61 | 'ENV' => \%env, | ||
| 62 | }; | ||
| 63 | bless $self, $class; | ||
| 64 | # rcd_log( "debug: rcd_rec:$self created" ); | ||
| 65 | |||
| 66 | # my $root = $self->{ 'ENV' }{ 'ROOT' }; | ||
| 67 | # # autosetup defaults | ||
| 68 | # if( ! $self->{ 'ENV' }{ 'HTML_DIRS' } ) | ||
| 69 | # { | ||
| 70 | # $self->{ 'ENV' }{ 'HTML_DIRS' } = [ "$root/html" ]; | ||
| 71 | # } | ||
| 72 | |||
| 73 | my $lib_dirs = $env{ 'LIB_DIRS' } || []; | ||
| 74 | for my $lib_dir ( @$lib_dirs ) | ||
| 75 | { | ||
| 76 | boom "invalid or not accessible LIB_DIR [$lib_dir]" unless -d $lib_dir; | ||
| 77 | push @INC, $lib_dir; | ||
| 78 | } | ||
| 79 | |||
| 80 | # sanity, remove '.' from include list, TODO: optionally remove other entries by config (%env) | ||
| 81 | for my $z ( 0 .. scalar( @INC ) - 1 ) | ||
| 82 | { | ||
| 83 | next unless $INC[ $z ] eq '.'; | ||
| 84 | splice @INC, $z, 1; | ||
| 85 | last; | ||
| 86 | } | ||
| 87 | |||
| 88 | my $reo_sess_class = $env{ 'REO_SESS_CLASS' } ||= 'Web::Reactor::Sessions::Filesystem'; | ||
| 89 | my $reo_prep_class = $env{ 'REO_PREP_CLASS' } ||= 'Web::Reactor::Preprocessor::Expander'; | ||
| 90 | my $reo_acts_class = $env{ 'REO_ACTS_CLASS' } ||= 'Web::Reactor::Actions::Native'; | ||
| 91 | |||
| 92 | my $reo_sess_class_file = perl_package_to_file( $reo_sess_class ); | ||
| 93 | my $reo_prep_class_file = perl_package_to_file( $reo_prep_class ); | ||
| 94 | my $reo_acts_class_file = perl_package_to_file( $reo_acts_class ); | ||
| 95 | |||
| 96 | require $reo_sess_class_file; | ||
| 97 | require $reo_prep_class_file; | ||
| 98 | require $reo_acts_class_file; | ||
| 99 | |||
| 100 | # new objects for part slots | ||
| 101 | # FIXME: pass %env reference to use the same env hash | ||
| 102 | $self->{ 'REO_SESS' } = new $reo_sess_class %env; | ||
| 103 | $self->{ 'REO_PREP' } = new $reo_prep_class %env; | ||
| 104 | $self->{ 'REO_ACTS' } = new $reo_acts_class %env; | ||
| 105 | |||
| 106 | # save backlinks to reactor | ||
| 107 | $self->{ 'REO_SESS' }{ 'REO_REACTOR' } = $self; | ||
| 108 | $self->{ 'REO_PREP' }{ 'REO_REACTOR' } = $self; | ||
| 109 | $self->{ 'REO_ACTS' }{ 'REO_REACTOR' } = $self; | ||
| 110 | |||
| 111 | return $self; | ||
| 112 | } | ||
| 113 | |||
| 114 | ############################################################################## | ||
| 115 | |||
| 116 | sub run | ||
| 117 | { | ||
| 118 | my $self = shift; | ||
| 119 | |||
| 120 | eval | ||
| 121 | { | ||
| 122 | $self->main_process(); | ||
| 123 | }; | ||
| 124 | if( surface( 'CONTENT' ) ) | ||
| 125 | { | ||
| 126 | # nothing, should be ok | ||
| 127 | } | ||
| 128 | elsif( surface( '*' ) ) | ||
| 129 | { | ||
| 130 | $self->log( "main process failed: $@" ); | ||
| 131 | } | ||
| 132 | $self->save(); | ||
| 133 | } | ||
| 134 | |||
| 135 | sub main_process | ||
| 136 | { | ||
| 137 | my $self = shift; | ||
| 138 | |||
| 139 | # 0. load/setup env/config defaults | ||
| 140 | my $app_name = $self->{ 'ENV' }{ 'APP_NAME' } or $self->boom( "missing APP_NAME" ); | ||
| 141 | |||
| 142 | # 1. loading request header | ||
| 143 | |||
| 144 | # 2. loading cookie | ||
| 145 | my $cookie_name = lc( $self->{ 'ENV' }{ 'COOKIE_NAME' } || "$app_name\_cookie" ); | ||
| 146 | my $user_sid = $self->get_cookie( $cookie_name ); | ||
| 147 | |||
| 148 | # 3. loading user session, setup new session and cookie if needed | ||
| 149 | my $user_shr = {}; # user session hash ref | ||
| 150 | if( ! ( $user_sid =~ /^[a-zA-Z0-9]+$/ and $user_shr = $self->sess_load( 'USER', $user_sid ) ) ) | ||
| 151 | { | ||
| 152 | # print STDERR Dumper( $user_sid, $user_shr ); | ||
| 153 | |||
| 154 | $self->log( "warning: invalid user session [$user_sid]" ); | ||
| 155 | |||
| 156 | ( $user_sid, $user_shr ) = $self->__create_new_user_session(); | ||
| 157 | } | ||
| 158 | $self->{ 'SESSIONS' }{ 'SID' }{ 'USER' } = $user_sid; | ||
| 159 | $self->{ 'SESSIONS' }{ 'DATA' }{ 'USER' }{ $user_sid } = $user_shr; | ||
| 160 | |||
| 161 | if( ( $user_shr->{ ':LOGGED_IN' } and $user_shr->{ ':XTIME' } > 0 and time() > $user_shr->{ ':XTIME' } ) | ||
| 162 | or | ||
| 163 | ( $user_shr->{ ':CLOSED' } ) ) | ||
| 164 | { | ||
| 165 | $self->log( "status: user session expired or closed [$user_sid]" ); | ||
| 166 | # not logged-in sessions dont expire | ||
| 167 | $user_shr->{ ':XTIME_STR' } = scalar localtime() if time() > $user_shr->{ ':XTIME' }; | ||
| 168 | $user_shr->{ ':CLOSED' } = 1; | ||
| 169 | $user_shr->{ ':ETIME' } = time(); | ||
| 170 | $user_shr->{ ':ETIME_STR' } = scalar localtime(); | ||
| 171 | |||
| 172 | ( $user_sid, $user_shr ) = $self->__create_new_user_session(); | ||
| 173 | |||
| 174 | $self->render( PAGE => 'eexpired' ); | ||
| 175 | } | ||
| 176 | |||
| 177 | for my $k ( keys %{ $user_shr->{ ":HTTP_CHECK_HR" } } ) | ||
| 178 | { | ||
| 179 | # check if session parameters are changed, stealing session? | ||
| 180 | next if $user_shr->{ ":HTTP_CHECK_HR" }{ $k } eq $ENV{ $k }; | ||
| 181 | |||
| 182 | $self->log( "status: user session parameters check failed [$user_sid]" ); | ||
| 183 | # FIXME: move to function: close_session(); | ||
| 184 | $user_shr->{ ':CLOSED' } = 1; | ||
| 185 | $user_shr->{ ':ETIME' } = time(); | ||
| 186 | $user_shr->{ ':ETIME_STR' } = scalar localtime(); | ||
| 187 | |||
| 188 | ( $user_sid, $user_shr ) = $self->__create_new_user_session(); | ||
| 189 | |||
| 190 | $self->render( PAGE => 'einvalid' ); | ||
| 191 | last; | ||
| 192 | } | ||
| 193 | |||
| 194 | $self->save(); | ||
| 195 | |||
| 196 | # 4. get input data, CGI::params, postdata | ||
| 197 | my $input_user_hr = $self->{ 'INPUT_USER_HR' } = {}; | ||
| 198 | my $input_safe_hr = $self->{ 'INPUT_SAFE_HR' } = {}; | ||
| 199 | |||
| 200 | # FIXME: TODO: handle and URL params here. only for EX? | ||
| 201 | |||
| 202 | # import plain parameters from GET/POST request | ||
| 203 | for my $n ( CGI::param() ) | ||
| 204 | { | ||
| 205 | if( $n !~ /^[A-Za-z0-9\-\_\.\:]+$/o ) | ||
| 206 | { | ||
| 207 | $self->log( "error: invalid CGI/input parameter name: [$n]" ); | ||
| 208 | next; | ||
| 209 | } | ||
| 210 | my $v = CGI::param( $n ); | ||
| 211 | my @v = CGI::param( $n ); | ||
| 212 | |||
| 213 | $self->log_debug( "debug: CGI input param [$n] value [$v] [@v]" ); | ||
| 214 | |||
| 215 | if( $self->__input_cgi_skip_invalid_value( $n, $v ) ) | ||
| 216 | { | ||
| 217 | $self->log( "error: invalid CGI/input value for parameter: [$n]" ); | ||
| 218 | next; | ||
| 219 | } | ||
| 220 | $v = $self->__input_cgi_make_safe_value( $n, $v ); | ||
| 221 | if ( $n =~ /BUTTON:([a-z0-9_\-]+)(:(.+?))?(\.[XY])?$/oi ) | ||
| 222 | { | ||
| 223 | # regular button BUTTON:CANCEL | ||
| 224 | # button with id BUTTON:REDIRECT:USERID | ||
| 225 | $input_user_hr->{ 'BUTTON' } = uc $1; | ||
| 226 | $input_user_hr->{ 'BUTTON_ID' } = $3; | ||
| 227 | } | ||
| 228 | else | ||
| 229 | { | ||
| 230 | $n = uc $n; | ||
| 231 | $input_user_hr->{ $n } = $v; | ||
| 232 | if( ref( $v ) eq 'Fh' ) | ||
| 233 | { | ||
| 234 | # this is file upload, get more info | ||
| 235 | $input_user_hr->{ "$n:UPLOAD_INFO" } = CGI::uploadInfo( $v ); | ||
| 236 | } | ||
| 237 | } | ||
| 238 | } | ||
| 239 | |||
| 240 | my $safe_input_link_sess = $input_user_hr->{ '_' }; | ||
| 241 | # parse link session: link-sid.link-key | ||
| 242 | if( $safe_input_link_sess =~ /^([a-zA-Z0-9]+)\.([a-zA-Z0-9]+)$/ ) | ||
| 243 | { | ||
| 244 | my ( $link_sid, $link_key ) = ( $1, $2 ); | ||
| 245 | |||
| 246 | my $link_session_hr = $self->sess_load( 'LINK', $link_sid ); | ||
| 247 | |||
| 248 | my $link_data = $link_session_hr->{ $link_key }; | ||
| 249 | |||
| 250 | # merge safe input if valid | ||
| 251 | %$input_safe_hr = ( %$input_safe_hr, %$link_data ) if $link_data; | ||
| 252 | } | ||
| 253 | elsif( $safe_input_link_sess ne '' ) | ||
| 254 | { | ||
| 255 | $self->log( "warning: invalid safe input link session.key [$safe_input_link_sess]" ); | ||
| 256 | } | ||
| 257 | |||
| 258 | # 5. loading page session | ||
| 259 | my $page_sid = $input_safe_hr->{ '_P' }; | ||
| 260 | my $page_shr = {}; # user session hash ref | ||
| 261 | if( ! ( $page_sid =~ /^[a-zA-Z0-9]+$/ and $page_shr = $self->sess_load( 'PAGE', $page_sid ) ) ) | ||
| 262 | { | ||
| 263 | $self->log( "warning: invalid page session [$page_sid]" ); | ||
| 264 | $page_sid = $self->sess_create( 'PAGE', 8 ); | ||
| 265 | $self->log( "warning: new page session created [$page_sid]" ); | ||
| 266 | $page_shr = { ':ID' => $page_sid }; | ||
| 267 | } | ||
| 268 | $self->{ 'SESSIONS' }{ 'SID' }{ 'PAGE' } = $page_sid; | ||
| 269 | $self->{ 'SESSIONS' }{ 'DATA' }{ 'PAGE' }{ $page_sid } = $page_shr; | ||
| 270 | |||
| 271 | |||
| 272 | my $ref_page_sid = $input_safe_hr->{ '_R' }; | ||
| 273 | if( $ref_page_sid =~ /^[a-zA-Z0-9]+$/ ) | ||
| 274 | { | ||
| 275 | $page_shr->{ ':REF_PAGE_SID' } = $ref_page_sid; | ||
| 276 | } | ||
| 277 | |||
| 278 | # 6. remap form input data, post to safe input | ||
| 279 | my $form_name = $input_safe_hr->{ 'FORM_NAME' }; # FIXME: replace with _FN | ||
| 280 | if( $form_name and exists $page_shr->{ ':FORM_DEF' }{ $form_name } ) | ||
| 281 | { | ||
| 282 | my $rm = $page_shr->{ ':FORM_DEF' }{ $form_name }{ 'RET_MAP' }; | ||
| 283 | |||
| 284 | for my $k ( keys %$rm ) | ||
| 285 | { | ||
| 286 | $input_safe_hr->{ $k } = $rm->{ $k }{ $input_user_hr->{ $k } }; | ||
| 287 | delete $input_user_hr->{ $k }; | ||
| 288 | } | ||
| 289 | } | ||
| 290 | |||
| 291 | # 7. get action from input (USER/CGI) or page session | ||
| 292 | my $action_name = lc( $input_safe_hr->{ '_AN' } || $input_user_hr->{ '_AN' } || $page_shr->{ ':ACTION_NAME' } ); | ||
| 293 | if( $action_name =~ /^[a-z_0-9]+$/ ) | ||
| 294 | { | ||
| 295 | $page_shr->{ ':ACTION_NAME' } = $action_name; | ||
| 296 | } | ||
| 297 | else | ||
| 298 | { | ||
| 299 | # $self->log( "error: invalid action name [$action_name]" ); | ||
| 300 | } | ||
| 301 | |||
| 302 | # 8. get page from input (USER/CGI) or page session | ||
| 303 | my $page_name = lc( $input_safe_hr->{ '_PN' } || $input_user_hr->{ '_PN' } || $page_shr->{ ':PAGE_NAME' } || 'index' ); | ||
| 304 | if( $page_name =~ /^[a-z_0-9]+$/ ) | ||
| 305 | { | ||
| 306 | $page_shr->{ ':PAGE_NAME' } = $page_name; | ||
| 307 | } | ||
| 308 | else | ||
| 309 | { | ||
| 310 | $self->log( "error: invalid page name [$page_name]" ); | ||
| 311 | } | ||
| 312 | |||
| 313 | # 9. render output action/page | ||
| 314 | if( $action_name ) | ||
| 315 | { | ||
| 316 | $self->render( ACTION => $action_name ); | ||
| 317 | } | ||
| 318 | else | ||
| 319 | { | ||
| 320 | $self->render( PAGE => $page_name ); | ||
| 321 | } | ||
| 322 | } | ||
| 323 | |||
| 324 | sub __create_new_user_session | ||
| 325 | { | ||
| 326 | my $self = shift; | ||
| 327 | |||
| 328 | my $user_sid; | ||
| 329 | my $user_shr; | ||
| 330 | |||
| 331 | # FIXME: move to function | ||
| 332 | my $app_name = $self->{ 'ENV' }{ 'APP_NAME' } or $self->boom( "missing APP_NAME" ); | ||
| 333 | my $cookie_name = lc( $self->{ 'ENV' }{ 'COOKIE_NAME' } || "$app_name\_cookie" ); | ||
| 334 | |||
| 335 | $user_sid = $self->sess_create( 'USER' ); | ||
| 336 | $user_shr = { ':ID' => $user_sid }; | ||
| 337 | $self->{ 'SESSIONS' }{ 'SID' }{ 'USER' } = $user_sid; | ||
| 338 | $self->{ 'SESSIONS' }{ 'DATA' }{ 'USER' }{ $user_sid } = $user_shr; | ||
| 339 | |||
| 340 | $self->set_cookie( $cookie_name, -value => $user_sid ); | ||
| 341 | $self->log( "debug: creating new user session [$user_sid]" ); | ||
| 342 | |||
| 343 | my $user_session_expire = $self->{ 'ENV' }{ 'USER_SESSION_EXPIRE' } || 1382400; # 16 days :) | ||
| 344 | |||
| 345 | $user_shr->{ ':CTIME' } = time(); | ||
| 346 | $user_shr->{ ':CTIME_STR' } = scalar localtime(); | ||
| 347 | $user_shr->{ ':XTIME' } = time() + $user_session_expire; | ||
| 348 | $user_shr->{ ':XTIME_STR' } = scalar localtime( $user_shr->{ ':XTIME' } ); | ||
| 349 | |||
| 350 | $user_shr->{ ":HTTP_ENV_HR" } = { map { $_ => $ENV{ $_ } } @HTTP_VARS_SAVE }; | ||
| 351 | $user_shr->{ ":HTTP_CHECK_HR" } = { map { $_ => $ENV{ $_ } } @HTTP_VARS_CHECK }; | ||
| 352 | |||
| 353 | return ( $user_sid, $user_shr ); | ||
| 354 | } | ||
| 355 | |||
| 356 | ############################################################################## | ||
| 357 | # | ||
| 358 | # usual user visible api | ||
| 359 | # | ||
| 360 | |||
| 361 | sub get_user_session | ||
| 362 | { | ||
| 363 | my $self = shift; | ||
| 364 | |||
| 365 | my $user_sid = $self->{ 'SESSIONS' }{ 'SID' }{ 'USER' }; | ||
| 366 | my $user_shr = $self->{ 'SESSIONS' }{ 'DATA' }{ 'USER' }{ $user_sid }; | ||
| 367 | |||
| 368 | return $user_shr; | ||
| 369 | } | ||
| 370 | |||
| 371 | sub get_page_session | ||
| 372 | { | ||
| 373 | my $self = shift; | ||
| 374 | my $level = shift; | ||
| 375 | |||
| 376 | my $page_sid = $self->{ 'SESSIONS' }{ 'SID' }{ 'PAGE' }; | ||
| 377 | my $page_shr = $self->{ 'SESSIONS' }{ 'DATA' }{ 'PAGE' }{ $page_sid }; | ||
| 378 | |||
| 379 | while( $level-- ) | ||
| 380 | { | ||
| 381 | $page_sid = $page_shr->{ ':REF_PAGE_SID' }; | ||
| 382 | return undef unless $page_sid; | ||
| 383 | $page_shr = $self->{ 'SESSIONS' }{ 'DATA' }{ 'PAGE' }{ $page_sid }; | ||
| 384 | if( ! $page_shr ) | ||
| 385 | { | ||
| 386 | $page_shr = $self->sess_load( 'PAGE', $page_sid ); | ||
| 387 | $self->{ 'SESSIONS' }{ 'DATA' }{ 'PAGE' }{ $page_sid } = $page_shr; | ||
| 388 | } | ||
| 389 | } | ||
| 390 | |||
| 391 | return $page_shr; | ||
| 392 | } | ||
| 393 | |||
| 394 | sub get_http_env | ||
| 395 | { | ||
| 396 | my $self = shift; | ||
| 397 | |||
| 398 | my $user_shr = $self->get_user_session(); | ||
| 399 | |||
| 400 | boom "missing HTTP_ENV inside user session" unless exists $user_shr->{ ':HTTP_ENV_HR' }; | ||
| 401 | |||
| 402 | return $user_shr->{ ':HTTP_ENV_HR' }; | ||
| 403 | } | ||
| 404 | |||
| 405 | sub get_page_session_id | ||
| 406 | { | ||
| 407 | my $self = shift; | ||
| 408 | my $level = shift; | ||
| 409 | |||
| 410 | my $shr = $self->get_page_session( $level ) || {}; | ||
| 411 | |||
| 412 | return $shr->{ ':ID' }; | ||
| 413 | } | ||
| 414 | |||
| 415 | sub get_ref_page_session_id | ||
| 416 | { | ||
| 417 | my $self = shift; | ||
| 418 | my $level = shift; | ||
| 419 | |||
| 420 | my $shr = $self->get_page_session( $level ) || {}; | ||
| 421 | |||
| 422 | return $shr->{ ':REF_PAGE_SID' }; | ||
| 423 | } | ||
| 424 | |||
| 425 | sub get_safe_input | ||
| 426 | { | ||
| 427 | my $self = shift; | ||
| 428 | |||
| 429 | my $input_safe_hr = $self->{ 'INPUT_SAFE_HR' }; | ||
| 430 | return $input_safe_hr; | ||
| 431 | } | ||
| 432 | |||
| 433 | sub get_user_input | ||
| 434 | { | ||
| 435 | my $self = shift; | ||
| 436 | |||
| 437 | my $input_user_hr = $self->{ 'INPUT_USER_HR' }; | ||
| 438 | return $input_user_hr; | ||
| 439 | } | ||
| 440 | |||
| 441 | sub get_input_button | ||
| 442 | { | ||
| 443 | my $self = shift; | ||
| 444 | |||
| 445 | my $input_user_hr = $self->get_user_input(); | ||
| 446 | return $input_user_hr->{ 'BUTTON' }; | ||
| 447 | } | ||
| 448 | |||
| 449 | sub get_input_button_id | ||
| 450 | { | ||
| 451 | my $self = shift; | ||
| 452 | |||
| 453 | my $input_user_hr = $self->get_user_input(); | ||
| 454 | return $input_user_hr->{ 'BUTTON_ID' }; | ||
| 455 | } | ||
| 456 | |||
| 457 | sub get_input_form_name | ||
| 458 | { | ||
| 459 | my $self = shift; | ||
| 460 | |||
| 461 | my $input_safe_hr = $self->get_safe_input(); | ||
| 462 | my $form_name = $input_safe_hr->{ 'FORM_NAME' }; # FIXME: replace with _FN | ||
| 463 | |||
| 464 | return $form_name; | ||
| 465 | } | ||
| 466 | |||
| 467 | sub args | ||
| 468 | { | ||
| 469 | my $self = shift; | ||
| 470 | my %args = @_; | ||
| 471 | |||
| 472 | hash_uc_ipl( \%args ); | ||
| 473 | |||
| 474 | my $link_sid; | ||
| 475 | my $link_shr; | ||
| 476 | |||
| 477 | if( ! $self->{ 'SESSIONS' }{ 'SID' }{ 'LINK' } ) | ||
| 478 | { | ||
| 479 | $link_sid = $self->sess_create( 'LINK', 8 ); | ||
| 480 | $link_shr = { ':ID' => $link_sid }; | ||
| 481 | $self->{ 'SESSIONS' }{ 'DATA' }{ 'LINK' }{ $link_sid } = $link_shr; | ||
| 482 | $self->{ 'SESSIONS' }{ 'SID' }{ 'LINK' } = $link_sid; | ||
| 483 | } | ||
| 484 | else | ||
| 485 | { | ||
| 486 | $link_sid = $self->{ 'SESSIONS' }{ 'SID' }{ 'LINK' }; | ||
| 487 | $link_shr = $self->{ 'SESSIONS' }{ 'DATA' }{ 'LINK' }{ $link_sid }; | ||
| 488 | } | ||
| 489 | |||
| 490 | my $link_key; | ||
| 491 | while(4) | ||
| 492 | { | ||
| 493 | $link_key = $self->{ 'REO_SESS' }->create_id( 8 ); # FIXME: length param env | ||
| 494 | last if ! exists $link_shr->{ $link_key }; | ||
| 495 | } | ||
| 496 | $self->boom( "cannot create LINK key" ) unless $link_key; | ||
| 497 | |||
| 498 | $link_shr->{ $link_key } = \%args; | ||
| 499 | |||
| 500 | $self->save(); | ||
| 501 | return $link_sid . '.' . $link_key; | ||
| 502 | } | ||
| 503 | |||
| 504 | sub args_back | ||
| 505 | { | ||
| 506 | my $self = shift; | ||
| 507 | my %args = @_; | ||
| 508 | |||
| 509 | $args{ '_P' } = $self->get_ref_page_session_id(); | ||
| 510 | |||
| 511 | return $self->args( %args ); | ||
| 512 | } | ||
| 513 | |||
| 514 | sub args_new | ||
| 515 | { | ||
| 516 | my $self = shift; | ||
| 517 | my %args = @_; | ||
| 518 | |||
| 519 | $args{ '_R' } = $self->get_page_session_id(); | ||
| 520 | |||
| 521 | return $self->args( %args ); | ||
| 522 | } | ||
| 523 | |||
| 524 | sub args_here | ||
| 525 | { | ||
| 526 | my $self = shift; | ||
| 527 | my %args = @_; | ||
| 528 | |||
| 529 | $args{ '_P' } = $self->get_page_session_id(); | ||
| 530 | |||
| 531 | return $self->args( %args ); | ||
| 532 | } | ||
| 533 | |||
| 534 | ############################################################################## | ||
| 535 | |||
| 536 | sub get_cookie | ||
| 537 | { | ||
| 538 | my $self = shift; | ||
| 539 | my $name = shift; | ||
| 540 | |||
| 541 | my $cookie = CGI::cookie( $name ); | ||
| 542 | $self->log_debug( "get_cookie: name [$name] value [$cookie]" ); | ||
| 543 | return $cookie; | ||
| 544 | } | ||
| 545 | |||
| 546 | sub set_cookie | ||
| 547 | { | ||
| 548 | my $self = shift; | ||
| 549 | my $name = shift; | ||
| 550 | my %opt = @_; | ||
| 551 | |||
| 552 | $self->log( "debug: creating new cookie [$name]" ); | ||
| 553 | # FIXME: validate %opt Data::Validate::Struct | ||
| 554 | |||
| 555 | $opt{ -name } = $name; | ||
| 556 | |||
| 557 | $self->{ 'OUTPUT' }{ 'COOKIES' }{ $name } = new CGI::Cookie( %opt ); | ||
| 558 | } | ||
| 559 | |||
| 560 | ############################################################################## | ||
| 561 | |||
| 562 | sub get_headers | ||
| 563 | { | ||
| 564 | my $self = shift; | ||
| 565 | |||
| 566 | my %h = map { $_ => CGI::http( $_ ) } CGI::http(); | ||
| 567 | |||
| 568 | return \%h; | ||
| 569 | } | ||
| 570 | |||
| 571 | sub set_headers | ||
| 572 | { | ||
| 573 | my $self = shift; | ||
| 574 | my %h = @_; | ||
| 575 | |||
| 576 | $self->{ 'OUTPUT' }{ 'HEADERS' } ||= {}; | ||
| 577 | $self->{ 'OUTPUT' }{ 'HEADERS' } = { %{ $self->{ 'OUTPUT' }{ 'HEADERS' } }, %h }; | ||
| 578 | } | ||
| 579 | |||
| 580 | sub __make_headers | ||
| 581 | { | ||
| 582 | my $self = shift; | ||
| 583 | |||
| 584 | my $headers; | ||
| 585 | |||
| 586 | $self->{ 'OUTPUT' }{ 'HEADERS' }{ 'content-type' } ||= 'text/html'; | ||
| 587 | |||
| 588 | while( my ( $k, $v ) = each %{ $self->{ 'OUTPUT' }{ 'HEADERS' } } ) | ||
| 589 | { | ||
| 590 | $headers .= "$k: $v\n"; | ||
| 591 | } | ||
| 592 | |||
| 593 | while( my ( $k, $v ) = each %{ $self->{ 'OUTPUT' }{ 'COOKIES' } } ) | ||
| 594 | { | ||
| 595 | $k = 'set-cookie'; | ||
| 596 | $headers .= "$k: $v\n"; | ||
| 597 | } | ||
| 598 | |||
| 599 | $headers .= "\n\n"; | ||
| 600 | |||
| 601 | $self->log_dumper( 'HEADERS', $headers ); | ||
| 602 | |||
| 603 | return $headers; | ||
| 604 | } | ||
| 605 | |||
| 606 | ############################################################################## | ||
| 607 | |||
| 608 | sub save | ||
| 609 | { | ||
| 610 | my $self = shift; | ||
| 611 | |||
| 612 | my $mod_cache = $self->{ 'CACHE' }{ 'SESSION_DATA_SHA1' } ||= {}; | ||
| 613 | |||
| 614 | for my $type ( qw( USER PAGE LINK ) ) | ||
| 615 | { | ||
| 616 | while( my ( $sid, $shr ) = each %{ $self->{ 'SESSIONS' }{ 'DATA' }{ $type } } ) | ||
| 617 | { | ||
| 618 | $self->boom( "SESSION:DATA:$type:$sid is not hashref" ) unless ref( $shr ) eq 'HASH'; | ||
| 619 | |||
| 620 | my $sha1 = sha1_hex( freeze( $shr ) ); | ||
| 621 | my $cache1 = $mod_cache->{ $type }{ $sid }; | ||
| 622 | |||
| 623 | next if $sha1 eq $cache1; | ||
| 624 | |||
| 625 | $self->log_debug( "saving session data [$type:$sid]" ); | ||
| 626 | |||
| 627 | $mod_cache->{ $type }{ $sid } = $sha1; | ||
| 628 | |||
| 629 | $self->sess_save( $type, $sid, $shr ); | ||
| 630 | } | ||
| 631 | } | ||
| 632 | } | ||
| 633 | |||
| 634 | ############################################################################## | ||
| 635 | ## | ||
| 636 | ## CRYPTO api :) | ||
| 637 | ## | ||
| 638 | |||
| 639 | ## FIXME: move most to Data::Tools or separate module | ||
| 640 | |||
| 641 | sub __new_crypto_object | ||
| 642 | { | ||
| 643 | my $self = shift; | ||
| 644 | |||
| 645 | # NOTE: RTFM says encryptor and decryptor must be different | ||
| 646 | |||
| 647 | # FIXME: read key from config file only! | ||
| 648 | my $key = $self->{ 'ENV' }{ 'ENCRYPT_KEY' }; | ||
| 649 | $self->boom( "missing ENV:ENCRYPT_KEY" ) unless $key =~ /\S/; | ||
| 650 | |||
| 651 | my $ci = $self->{ 'ENV' }{ 'ENCRYPT_CIPHER' } || 'Twofish2'; # :) | ||
| 652 | |||
| 653 | return new Crypt::CBC->new( -key => $key, -cipher => $ci ); | ||
| 654 | } | ||
| 655 | |||
| 656 | sub encrypt | ||
| 657 | { | ||
| 658 | my $self = shift; | ||
| 659 | my $data = shift; | ||
| 660 | |||
| 661 | my $enc = $self->{ 'ENCRYPTOR' } ||= $self->__new_crypto_object(); | ||
| 662 | return $enc->encrypt( $data ); | ||
| 663 | } | ||
| 664 | |||
| 665 | sub decrypt | ||
| 666 | { | ||
| 667 | my $self = shift; | ||
| 668 | my $data = shift; | ||
| 669 | |||
| 670 | my $dec = $self->{ 'DECRYPTOR' } ||= $self->__new_crypto_object(); | ||
| 671 | return $dec->decrypt( $data ); | ||
| 672 | } | ||
| 673 | |||
| 674 | sub encrypt_hex | ||
| 675 | { | ||
| 676 | my $self = shift; | ||
| 677 | |||
| 678 | return str_hex( $self->encrypt( @_ ) ); | ||
| 679 | } | ||
| 680 | |||
| 681 | sub decrypt_hex | ||
| 682 | { | ||
| 683 | my $self = shift; | ||
| 684 | |||
| 685 | return $self->decrypt( str_unhex( @_ ) ); | ||
| 686 | } | ||
| 687 | |||
| 688 | sub crypto_freeze_hex | ||
| 689 | { | ||
| 690 | my $self = shift; | ||
| 691 | my $data = shift; # reference to any data/scalar/hash/array | ||
| 692 | |||
| 693 | return $self->encrypt_hex( freeze( $data ) ); | ||
| 694 | } | ||
| 695 | |||
| 696 | sub crypto_thaw_hex | ||
| 697 | { | ||
| 698 | my $self = shift; | ||
| 699 | my $data = shift; # hex encoded data | ||
| 700 | |||
| 701 | return thaw( $self->decrypt_hex( $data ) ); | ||
| 702 | } | ||
| 703 | |||
| 704 | ############################################################################## | ||
| 705 | |||
| 706 | sub set_debug | ||
| 707 | { | ||
| 708 | my $self = shift; | ||
| 709 | |||
| 710 | if( @_ > 0 ) | ||
| 711 | { | ||
| 712 | $self->{ 'ENV' }{ 'DEBUG' } = shift() ? 1 : 0; | ||
| 713 | } | ||
| 714 | return $self->{ 'ENV' }{ 'DEBUG' }; | ||
| 715 | } | ||
| 716 | |||
| 717 | sub is_debug | ||
| 718 | { | ||
| 719 | my $self = shift; | ||
| 720 | |||
| 721 | return $self->{ 'ENV' }{ 'DEBUG' }; | ||
| 722 | } | ||
| 723 | |||
| 724 | #----------------------------------------------------------------------------- | ||
| 725 | |||
| 726 | sub log | ||
| 727 | { | ||
| 728 | my $self = shift; | ||
| 729 | |||
| 730 | print STDERR @_; | ||
| 731 | } | ||
| 732 | |||
| 733 | sub log_debug | ||
| 734 | { | ||
| 735 | my $self = shift; | ||
| 736 | |||
| 737 | return unless $self->is_debug(); | ||
| 738 | my $msg = join( ' ', @_ ); | ||
| 739 | $msg = "debug: $msg" unless $msg =~ /^debug:/i; | ||
| 740 | $self->log( $msg ); | ||
| 741 | } | ||
| 742 | |||
| 743 | sub log_dumper | ||
| 744 | { | ||
| 745 | my $self = shift; | ||
| 746 | |||
| 747 | return unless $self->is_debug(); | ||
| 748 | $self->log_debug( Dumper( @_ ) ); | ||
| 749 | } | ||
| 750 | |||
| 751 | ############################################################################## | ||
| 752 | ## | ||
| 753 | ## sanity policies | ||
| 754 | ## these are internal subs but are designed to be overriden if required | ||
| 755 | ## | ||
| 756 | |||
| 757 | # fix/remove invalid parts of a CGI/input value | ||
| 758 | sub __input_cgi_make_safe_value | ||
| 759 | { | ||
| 760 | my $self = shift; | ||
| 761 | my $n = shift; # arg name | ||
| 762 | my $v = shift; # arg value | ||
| 763 | |||
| 764 | $v =~ s/[\000]//go; | ||
| 765 | |||
| 766 | return $v; | ||
| 767 | } | ||
| 768 | |||
| 769 | # must return 1 for values which must be removed from input or 0 for ok | ||
| 770 | # this is called before __input_cgi_make_safe_value, default is pass all | ||
| 771 | sub __input_cgi_skip_invalid_value | ||
| 772 | { | ||
| 773 | my $self = shift; | ||
| 774 | # this is placeholder really | ||
| 775 | # my $n = shift; # arg name | ||
| 776 | # my $v = shift; # arg value | ||
| 777 | return 0; | ||
| 778 | } | ||
| 779 | |||
| 780 | ############################################################################## | ||
| 781 | |||
| 782 | sub render | ||
| 783 | { | ||
| 784 | my $self = shift; | ||
| 785 | my %opt = @_; | ||
| 786 | |||
| 787 | my $action = $opt{ 'ACTION' }; | ||
| 788 | my $page = $opt{ 'PAGE' }; | ||
| 789 | |||
| 790 | # FIXME: content vars handling set_content()/etc. | ||
| 791 | my $ah = $self->args_here(); | ||
| 792 | $self->{ 'HTML_CONTENT' }{ 'form_input_session_keeper' } = "<input type=hidden name=_ value=$ah>"; | ||
| 793 | |||
| 794 | my $page_text; | ||
| 795 | |||
| 796 | if( $action ) | ||
| 797 | { | ||
| 798 | # FIXME: handle content type also! | ||
| 799 | $page_text = $self->act_call( $action ); | ||
| 800 | } | ||
| 801 | elsif( $page ) | ||
| 802 | { | ||
| 803 | $page_text = $self->prep_load_file( "page_$page" ); | ||
| 804 | } | ||
| 805 | else | ||
| 806 | { | ||
| 807 | boom "render() needs PAGE or ACTION"; | ||
| 808 | } | ||
| 809 | |||
| 810 | # FIXME: preprocess and translation only for content-type text/* | ||
| 811 | $page_text = $self->prep_process( $page_text ); | ||
| 812 | # FIXME: translation | ||
| 813 | $page_text =~ s/<~(([^<>]*))>/$1/g; | ||
| 814 | |||
| 815 | my $page_headers = $self->__make_headers(); | ||
| 816 | |||
| 817 | print $page_headers; | ||
| 818 | print $page_text; | ||
| 819 | |||
| 820 | #$self->log( Dumper( $page, "[$page_headers]", $page_text ) ); | ||
| 821 | |||
| 822 | if( $self->is_debug() ) | ||
| 823 | { | ||
| 824 | local $Data::Dumper::Sortkeys = 1; | ||
| 825 | local $Data::Dumper::Terse = 1; | ||
| 826 | local $Data::Dumper::Indent = 3; | ||
| 827 | print "<hr><pre>"; | ||
| 828 | print Dumper( 'USER INPUT:'.'_'x80, $self->{ 'INPUT_USER_HR' } ); | ||
| 829 | print Dumper( 'SAFE INPUT:'.'_'x80, $self->{ 'INPUT_SAFE_HR' } ); | ||
| 830 | print Dumper( 'PAGE SESSION:'.'_'x80, $self->{ 'SESSIONS' }{ 'DATA' }{ 'PAGE' }{ $self->{ 'SESSIONS' }{ 'SID' }{ 'PAGE' } } ); | ||
| 831 | print Dumper( 'USER SESSION:'.'_'x80, $self->{ 'SESSIONS' }{ 'DATA' }{ 'USER' }{ $self->{ 'SESSIONS' }{ 'SID' }{ 'USER' } } ); | ||
| 832 | print "<hr>"; | ||
| 833 | print Dumper( $self ); | ||
| 834 | print "</pre><hr>"; | ||
| 835 | } | ||
| 836 | |||
| 837 | sink 'CONTENT'; | ||
| 838 | } | ||
| 839 | |||
| 840 | sub forward_url | ||
| 841 | { | ||
| 842 | my $self = shift; | ||
| 843 | my $url = shift; | ||
| 844 | |||
| 845 | $self->set_headers( location => $url ); | ||
| 846 | |||
| 847 | my $page_headers = $self->__make_headers(); | ||
| 848 | print $page_headers; | ||
| 849 | |||
| 850 | sink 'CONTENT'; | ||
| 851 | } | ||
| 852 | |||
| 853 | sub forward | ||
| 854 | { | ||
| 855 | my $self = shift; | ||
| 856 | |||
| 857 | boom "expected even number of arguments" unless @_ % 2 == 0; | ||
| 858 | |||
| 859 | my $fw = $self->args( @_ ); | ||
| 860 | return $self->forward_url( "?_=$fw" ); | ||
| 861 | } | ||
| 862 | |||
| 863 | sub forward_back | ||
| 864 | { | ||
| 865 | my $self = shift; | ||
| 866 | |||
| 867 | boom "expected even number of arguments" unless @_ % 2 == 0; | ||
| 868 | |||
| 869 | my $fw = $self->args_back( @_ ); | ||
| 870 | return $self->forward_url( "?_=$fw" ); | ||
| 871 | } | ||
| 872 | |||
| 873 | sub forward_new | ||
| 874 | { | ||
| 875 | my $self = shift; | ||
| 876 | |||
| 877 | boom "expected even number of arguments" unless @_ % 2 == 0; | ||
| 878 | |||
| 879 | my $fw = $self->args_new( @_ ); | ||
| 880 | return $self->forward_url( "?_=$fw" ); | ||
| 881 | } | ||
| 882 | |||
| 883 | ############################################################################## | ||
| 884 | ## | ||
| 885 | ## helpers | ||
| 886 | ## | ||
| 887 | |||
| 888 | sub __param | ||
| 889 | { | ||
| 890 | my $self = shift; | ||
| 891 | my $safe = shift; # 1 safe_input, 0 user_input | ||
| 892 | |||
| 893 | my $input_hr; | ||
| 894 | my $save_key; | ||
| 895 | if( $safe ) | ||
| 896 | { | ||
| 897 | $input_hr = $self->get_safe_input(); | ||
| 898 | $save_key = 'SAVE_SAFE_INPUT'; | ||
| 899 | } | ||
| 900 | else | ||
| 901 | { | ||
| 902 | $input_hr = $self->get_user_input(); | ||
| 903 | $save_key = 'SAVE_USER_INPUT'; | ||
| 904 | } | ||
| 905 | |||
| 906 | my $ps = $self->get_page_session(); | ||
| 907 | |||
| 908 | $ps->{ $save_key } ||= {}; | ||
| 909 | |||
| 910 | my @res; | ||
| 911 | while( @_ ) | ||
| 912 | { | ||
| 913 | my $p = uc shift; | ||
| 914 | if( exists $input_hr->{ $p } ) | ||
| 915 | { | ||
| 916 | $ps->{ $save_key }{ $p } = $input_hr->{ $p }; | ||
| 917 | } | ||
| 918 | push @res, $ps->{ $save_key }{ $p }; | ||
| 919 | } | ||
| 920 | |||
| 921 | return wantarray ? @res : shift( @res ); | ||
| 922 | } | ||
| 923 | |||
| 924 | sub param_unsafe | ||
| 925 | { | ||
| 926 | my $self = shift; | ||
| 927 | return $self->__param( 0, @_ ); | ||
| 928 | } | ||
| 929 | |||
| 930 | sub param | ||
| 931 | { | ||
| 932 | my $self = shift; | ||
| 933 | return $self->__param( 1, @_ ); | ||
| 934 | } | ||
| 935 | |||
| 936 | sub param_safe | ||
| 937 | { | ||
| 938 | my $self = shift; | ||
| 939 | return $self->param( @_ ); | ||
| 940 | } | ||
| 941 | |||
| 942 | sub is_logged_in | ||
| 943 | { | ||
| 944 | my $self = shift; | ||
| 945 | |||
| 946 | my $user_shr = $self->get_user_session(); | ||
| 947 | return $user_shr->{ ':LOGGED_IN' } ? 1 : 0; | ||
| 948 | } | ||
| 949 | |||
| 950 | sub login | ||
| 951 | { | ||
| 952 | my $self = shift; | ||
| 953 | |||
| 954 | my $user_shr = $self->get_user_session(); | ||
| 955 | $user_shr->{ ':LOGGED_IN' } = 1; | ||
| 956 | $user_shr->{ ':LTIME' } = time(); | ||
| 957 | $user_shr->{ ':LTIME_STR' } = scalar localtime(); | ||
| 958 | # FIXME: add more login info | ||
| 959 | } | ||
| 960 | |||
| 961 | sub logout | ||
| 962 | { | ||
| 963 | my $self = shift; | ||
| 964 | |||
| 965 | my $user_shr = $self->get_user_session(); | ||
| 966 | $user_shr->{ ':LOGGED_IN' } = 0; | ||
| 967 | $user_shr->{ ':CLOSED' } = 1; | ||
| 968 | $user_shr->{ ':ETIME' } = time(); | ||
| 969 | $user_shr->{ ':ETIME_STR' } = scalar localtime(); | ||
| 970 | # FIXME: add more logout info | ||
| 971 | my ( $user_sid, $user_shr ) = $self->__create_new_user_session(); | ||
| 972 | } | ||
| 973 | |||
| 974 | # FIXME: s? | ||
| 975 | sub need_login | ||
| 976 | { | ||
| 977 | my $self = shift; | ||
| 978 | |||
| 979 | return if $self->is_logged_in(); | ||
| 980 | |||
| 981 | my $fw = $self->args_new( _PN => 'login' ); | ||
| 982 | return $self->forward_url( "?_=$fw" ); | ||
| 983 | |||
| 984 | # return $self->forward( _PN => 'login' ); | ||
| 985 | } | ||
| 986 | |||
| 987 | sub set_user_session_expire_time | ||
| 988 | { | ||
| 989 | my $self = shift; | ||
| 990 | my $xtime = shift; | ||
| 991 | |||
| 992 | my $user_shr = $self->get_user_session(); | ||
| 993 | $user_shr->{ ':XTIME' } = $xtime; # FIXME: sanity? | ||
| 994 | $user_shr->{ ':XTIME_STR' } = scalar localtime $user_shr->{ ':XTIME' }; | ||
| 995 | return exists $user_shr->{ ':XTIME' } ? $user_shr->{ ':XTIME' } : undef; | ||
| 996 | } | ||
| 997 | |||
| 998 | sub set_user_session_expire_time_in | ||
| 999 | { | ||
| 1000 | my $self = shift; | ||
| 1001 | my $seconds = shift; | ||
| 1002 | |||
| 1003 | # FIXME: support for more user friendly time periods 10m 60s | ||
| 1004 | return $self->set_user_session_expire_time( time() + $seconds ); | ||
| 1005 | } | ||
| 1006 | |||
| 1007 | # returns unix time at which user session will expire, undef if no expire time specified | ||
| 1008 | sub get_user_session_expire_time | ||
| 1009 | { | ||
| 1010 | my $self = shift; | ||
| 1011 | |||
| 1012 | my $user_shr = $self->get_user_session(); | ||
| 1013 | return exists $user_shr->{ ':XTIME' } ? $user_shr->{ ':XTIME' } : undef; | ||
| 1014 | } | ||
| 1015 | |||
| 1016 | # returns time period in seconds, in which user session will expire, undef if no expire time specified | ||
| 1017 | sub get_user_session_expire_time_in | ||
| 1018 | { | ||
| 1019 | my $self = shift; | ||
| 1020 | |||
| 1021 | my $xi = $self->get_user_session_expire_time() - time(); | ||
| 1022 | return $xi > 0 ? $xi : undef; | ||
| 1023 | } | ||
| 1024 | |||
| 1025 | sub need_post_method | ||
| 1026 | { | ||
| 1027 | my $self = shift; | ||
| 1028 | |||
| 1029 | my $he = $self->get_http_env(); | ||
| 1030 | |||
| 1031 | print STDERR Dumper( $he ); | ||
| 1032 | return if $he->{ 'REQUEST_METHOD' } eq 'POST'; | ||
| 1033 | |||
| 1034 | $self->logout(); | ||
| 1035 | $self->render( PAGE => 'epostrequired' ); | ||
| 1036 | } | ||
| 1037 | |||
| 1038 | ############################################################################## | ||
| 1039 | ## | ||
| 1040 | ## REO proxies | ||
| 1041 | ## | ||
| 1042 | |||
| 1043 | # FIXME: should actions have access to low-level session handling? | ||
| 1044 | sub sess_create { my $self = shift; $self->{ 'REO_SESS' }->create( @_ ) }; | ||
| 1045 | sub sess_delete { my $self = shift; $self->{ 'REO_SESS' }->delete( @_ ) }; | ||
| 1046 | sub sess_load { my $self = shift; $self->{ 'REO_SESS' }->load( @_ ) }; | ||
| 1047 | sub sess_save { my $self = shift; $self->{ 'REO_SESS' }->save( @_ ) }; | ||
| 1048 | sub sess_exists { my $self = shift; $self->{ 'REO_SESS' }->exists( @_ ) }; | ||
| 1049 | |||
| 1050 | #sub prep_render { my $self = shift; $self->{ 'REO_PREP' }->render( @_ ) }; | ||
| 1051 | sub prep_process { my $self = shift; $self->{ 'REO_PREP' }->process( @_ ) }; | ||
| 1052 | sub prep_load_file { my $self = shift; $self->{ 'REO_PREP' }->load_file( @_ ) }; | ||
| 1053 | |||
| 1054 | sub act_call { my $self = shift; $self->{ 'REO_ACTS' }->call( @_ ) }; | ||
| 1055 | |||
| 1056 | sub new_form | ||
| 1057 | { | ||
| 1058 | my $self = shift; | ||
| 1059 | |||
| 1060 | my $form = new Web::Reactor::HTML::Form( @_, REO_REACTOR => $self ); | ||
| 1061 | |||
| 1062 | return $form; | ||
| 1063 | } | ||
| 1064 | |||
| 1065 | ############################################################################## | ||
| 1066 | |||
| 1067 | =pod | ||
| 1068 | |||
| 1069 | =head1 NAME | ||
| 1070 | |||
| 1071 | Web::Reactor perl-based web application machinery. | ||
| 1072 | |||
| 1073 | =head1 SYNOPSIS | ||
| 1074 | |||
| 1075 | Startup CGI script example: | ||
| 1076 | |||
| 1077 | #!/usr/bin/perl | ||
| 1078 | use strict; | ||
| 1079 | use lib '/opt/perl/reactor/lib'; # if Reactor is custom location installed | ||
| 1080 | use Web::Reactor; | ||
| 1081 | |||
| 1082 | my %cfg = ( | ||
| 1083 | 'APP_NAME' => 'demo', | ||
| 1084 | 'APP_ROOT' => '/opt/reactor/demo/', | ||
| 1085 | 'LIB_DIRS' => [ '/opt/reactor/demo/lib/' ], | ||
| 1086 | 'ACTIONS_SETS' => [ 'demo', 'Base', 'Core' ], | ||
| 1087 | 'HTML_DIRS' => [ '/opt/reactor/demo/html/' ], | ||
| 1088 | 'SESS_VAR_DIR' => '/opt/reactor/demo/var/sess/', | ||
| 1089 | 'DEBUG' => 4, | ||
| 1090 | ); | ||
| 1091 | |||
| 1092 | eval { new Web::Reactor( %cfg )->run(); }; | ||
| 1093 | if( $@ ) | ||
| 1094 | { | ||
| 1095 | print STDERR "REACTOR CGI EXCEPTION: $@"; | ||
| 1096 | print "content-type: text/html\n\nsystem is temporary unavailable"; | ||
| 1097 | } | ||
| 1098 | |||
| 1099 | HTML page file example: | ||
| 1100 | |||
| 1101 | <#html_header> | ||
| 1102 | |||
| 1103 | <$app_name> | ||
| 1104 | |||
| 1105 | <#menu> | ||
| 1106 | |||
| 1107 | testing page html file | ||
| 1108 | |||
| 1109 | action test: <&test> | ||
| 1110 | |||
| 1111 | <#html_footer> | ||
| 1112 | |||
| 1113 | Action module example: | ||
| 1114 | |||
| 1115 | package Reactor::Actions::demo::test; | ||
| 1116 | use strict; | ||
| 1117 | use Data::Dumper; | ||
| 1118 | use Web::Reactor::HTML::FormEngine; | ||
| 1119 | |||
| 1120 | sub main | ||
| 1121 | { | ||
| 1122 | my $reo = shift; # Web::Reactor object. Provides all API and context. | ||
| 1123 | |||
| 1124 | my $text; # result html text | ||
| 1125 | |||
| 1126 | if( $reo->get_input_button() eq 'FORM_CANCEL' ) | ||
| 1127 | { | ||
| 1128 | # if clicked form button is cancel, | ||
| 1129 | # return back to the calling/previous page/view with optional data | ||
| 1130 | return $reo->forward_back( ACTION_RETURN => 'IS_CANCEL' ); | ||
| 1131 | } | ||
| 1132 | |||
| 1133 | # add some html content | ||
| 1134 | $text .= "<p>Reactor::Actions::demo::test here!<p>"; | ||
| 1135 | |||
| 1136 | # create link and hide its data. only accessible from inside web app. | ||
| 1137 | my $grid_href = $reo->args_new( _PN => 'grid', TABLE => 'testtable', ); | ||
| 1138 | $text .= "<a href=?_=$grid_href>go to grid</a><p>"; | ||
| 1139 | |||
| 1140 | # access page session. it will be auto-loaded on demand | ||
| 1141 | my $page_session_hr = $reo->get_page_session(); | ||
| 1142 | my $fortune = $page_session_hr->{ 'FORTUNE' } ||= `/usr/games/fortune`; | ||
| 1143 | |||
| 1144 | # access input (form) data. $i and $e are hashrefs | ||
| 1145 | my $i = $reo->get_user_input(); # get plain user input (hashref) | ||
| 1146 | my $e = $reo->get_safe_input(); # get safe data (never reach user browser) | ||
| 1147 | |||
| 1148 | $text .= "<p><hr><p>$fortune<hr>"; | ||
| 1149 | |||
| 1150 | my $bc = $reo->args_here(); # session keeper, this is manual use | ||
| 1151 | |||
| 1152 | $text .= "<form method=post>"; | ||
| 1153 | $text .= "<input type=hidden name=_ value=$bc>"; | ||
| 1154 | $text .= "input <input name=inp>"; | ||
| 1155 | $text .= "<input type=submit name=button:form_ok>"; | ||
| 1156 | $text .= "<input type=submit name=button:form_cancel>"; | ||
| 1157 | $text .= "</form>"; | ||
| 1158 | |||
| 1159 | my $form = $reo->new_form(); | ||
| 1160 | |||
| 1161 | $text .= "<p><hr><p>"; | ||
| 1162 | |||
| 1163 | return $text; | ||
| 1164 | } | ||
| 1165 | |||
| 1166 | 1; | ||
| 1167 | |||
| 1168 | =head1 DESCRIPTION | ||
| 1169 | |||
| 1170 | Web::Reactor (WR) provides automation of most of the usual and frequent tasks | ||
| 1171 | when constructing a web application. Such tasks include: | ||
| 1172 | |||
| 1173 | * User session handling (creation, cookies support, storage) | ||
| 1174 | * Page (web screen/view) session handling (similar to user sessions attributes) | ||
| 1175 | * Sessions (user/page/etc.) data storage and auto load/ssave | ||
| 1176 | * Inter-page relations and data transport (hides real data from the end-user) | ||
| 1177 | * HTML page creation and expansion (i.e. including preprocessing :)) | ||
| 1178 | * Optional HTML forms creation and data handling | ||
| 1179 | |||
| 1180 | Web::Reactor is designed to allow extending or replacing some parts as: | ||
| 1181 | |||
| 1182 | * Session storage (data store on filesystem, database, remote or vmem) | ||
| 1183 | * HTML creation/expansion/preprocessing | ||
| 1184 | * Page actions/modules execution (can be skipped if custom HTML prep used) | ||
| 1185 | |||
| 1186 | =head1 PAGE NAMES, HTML FILE TEMPLATES, PAGE INSTANCES | ||
| 1187 | |||
| 1188 | WR has a notion of a "page" which represents visible output to the end user | ||
| 1189 | browser. It has (i.e. uses) the following attributes: | ||
| 1190 | |||
| 1191 | * html file template (page name) | ||
| 1192 | * page session data | ||
| 1193 | * actions code (i.e. callbacks) used inside html text | ||
| 1194 | |||
| 1195 | All of those represent "page instance" and produce end user html visible page. | ||
| 1196 | |||
| 1197 | "Page names" are limited to be alphanumeric and are mapped to file | ||
| 1198 | (or other storage) html content: | ||
| 1199 | |||
| 1200 | page name: example | ||
| 1201 | html file template will be: page_example.html | ||
| 1202 | |||
| 1203 | HTML content may include other files (also limited to be alphanumeric): | ||
| 1204 | |||
| 1205 | include text: <#other_file> | ||
| 1206 | file included: other_file.html | ||
| 1207 | |||
| 1208 | Page names may be requested from the end user side, but include html files may | ||
| 1209 | be used only from the pages already requested. | ||
| 1210 | |||
| 1211 | =head1 ACTIONS/MODULES/CALLBACKS | ||
| 1212 | |||
| 1213 | Actions are loaded and executed by package names. In the HTML source files they | ||
| 1214 | can be called this way: | ||
| 1215 | |||
| 1216 | <&test_action arg1=val1 arg2=val2 flag1 flag2...> | ||
| 1217 | <&test_action> | ||
| 1218 | |||
| 1219 | This will instruct Reactor action handler to look for this package name inside | ||
| 1220 | standard or user-added library directories: | ||
| 1221 | |||
| 1222 | Web/Reactor/Actions/*/test_action.pm | ||
| 1223 | |||
| 1224 | Asterisk will be replaced with the name of the used "action sets" give in config | ||
| 1225 | hash: | ||
| 1226 | |||
| 1227 | 'ACTIONS_SETS' => [ 'demo', 'Base', 'Core' ], | ||
| 1228 | |||
| 1229 | So the result list in this example will be: | ||
| 1230 | |||
| 1231 | Web/Reactor/Actions/demo/test_action.pm | ||
| 1232 | Web/Reactor/Actions/Base/test_action.pm | ||
| 1233 | Web/Reactor/Actions/Core/test_action.pm | ||
| 1234 | |||
| 1235 | This is used to allow overriding of standard modules or modules you dont have | ||
| 1236 | write access to. | ||
| 1237 | |||
| 1238 | Another way to call a module is directly from another module code with: | ||
| 1239 | |||
| 1240 | $reo->act_call( 'test_action', @args ); | ||
| 1241 | |||
| 1242 | The package file will look like this: | ||
| 1243 | |||
| 1244 | package Web/Reactor/Actions/demo/test_action; | ||
| 1245 | use strict; | ||
| 1246 | |||
| 1247 | sub main | ||
| 1248 | { | ||
| 1249 | my $reo = shift; # Web::Reactor object/instance | ||
| 1250 | my %args = @_; # all args passed to the action | ||
| 1251 | |||
| 1252 | my $html_args = $args{ 'HTML_ARGS' }; # all | ||
| 1253 | ... | ||
| 1254 | return $result_data; # usually html text | ||
| 1255 | } | ||
| 1256 | |||
| 1257 | $html_args is hashref with all args give inside the html code if this action | ||
| 1258 | is called from a html text. If you look the example above: | ||
| 1259 | |||
| 1260 | <&test_action arg1=val1 arg2=val2 flag1 flag2...> | ||
| 1261 | |||
| 1262 | The $html_args will look like this: | ||
| 1263 | |||
| 1264 | $html_args = { | ||
| 1265 | 'arg1' => 'val1', | ||
| 1266 | 'arg2' => 'val2', | ||
| 1267 | 'flag1' => 1, | ||
| 1268 | 'flag2' => 1, | ||
| 1269 | }; | ||
| 1270 | |||
| 1271 | |||
| 1272 | |||
| 1273 | =head1 HTTP PARAMETERS NAMES | ||
| 1274 | |||
| 1275 | Web::Reactor uses underscore and one or two letters for its system http/html | ||
| 1276 | parameters. Some of the system params are: | ||
| 1277 | |||
| 1278 | _PN -- html page name (points to file template, restricted to alphanumeric) | ||
| 1279 | _P -- page session | ||
| 1280 | _R -- referer (caller) page session | ||
| 1281 | |||
| 1282 | Usually those names should not be directly used or visible inside actions code. | ||
| 1283 | More details about how those params are used can be found below. | ||
| 1284 | |||
| 1285 | =head1 USER SESSIONS | ||
| 1286 | |||
| 1287 | WR creates unique session for each connected user. The session is kept by a cookie. | ||
| 1288 | Usually WR needs justthis cookie to handle all user/server interaction. Inside | ||
| 1289 | WR action code, user session is represented as a hash reference. It may hold | ||
| 1290 | arbitrary data. "System" or WR-specific data inside user session has colon as | ||
| 1291 | prefix: | ||
| 1292 | |||
| 1293 | # $reo is Web::Reactor object (i.e. context) passed to the action/module code | ||
| 1294 | my $user_session = $reo->get_user_session(); | ||
| 1295 | print STDERR $user_session->{ ':CTIME_STR' }; | ||
| 1296 | # prints in http log the create time in human friendly form | ||
| 1297 | |||
| 1298 | All data saved inside user session is automatically saved. When needed it can | ||
| 1299 | be explicitly with: | ||
| 1300 | |||
| 1301 | $reo->save(); | ||
| 1302 | # saves all modified context to disk or other storage | ||
| 1303 | |||
| 1304 | =head1 PAGE SESSIONS | ||
| 1305 | |||
| 1306 | Each page presented to the user has own session. It is very similar to the user | ||
| 1307 | session (it is hash reference, may hold any data, can be saved with $reo->save()). | ||
| 1308 | It is expected that page sessions hold all context data needed for any page to | ||
| 1309 | display properly. To preserve page session it is needed that it is included | ||
| 1310 | in any link to this page instance or in any html form used. | ||
| 1311 | |||
| 1312 | When called for the first time, each page request needs page name (_PN). Afterwards | ||
| 1313 | a unique page session is created and page name is saved inside. At this moment | ||
| 1314 | this page instance can be accessed (i.e. given control to) only with a page | ||
| 1315 | session id (_P): | ||
| 1316 | |||
| 1317 | $page_sid = ...; # taken from somewhere | ||
| 1318 | # to pass control to the page instance: | ||
| 1319 | $reo->forward( _P => $page_sid ); | ||
| 1320 | # the page instance will pull data from its page session and display in | ||
| 1321 | # its last known state | ||
| 1322 | |||
| 1323 | Not always page session are needed. For example, when forward to the caller is | ||
| 1324 | needed, you just need to: | ||
| 1325 | |||
| 1326 | $reo->forward_back(); | ||
| 1327 | # this is equivalent to | ||
| 1328 | my $ref_page_sid = $reo->get_ref_page_session_id(); | ||
| 1329 | $reo->forward( _P => $ref_page_sid ); | ||
| 1330 | |||
| 1331 | Each page instance knows the caller page session and can give control back to. | ||
| 1332 | However it may pass more data when returning back to the caller: | ||
| 1333 | |||
| 1334 | $reo->forward_back( MORE_DATA => 'is here', OPTIONS_LIST => \@list ); | ||
| 1335 | |||
| 1336 | When new page instance has to be called (created): | ||
| 1337 | |||
| 1338 | |||
| 1339 | $reo->forward_new( _PN => 'some_page_name' ); | ||
| 1340 | |||
| 1341 | =head1 CONFIG ENTRIES | ||
| 1342 | |||
| 1343 | Upon creation, Web:Reactor instance gets hash with config entries/keys: | ||
| 1344 | |||
| 1345 | * APP_NAME -- alphanumeric application name (plus underscore) | ||
| 1346 | * APP_ROOT -- application root dir, used for app components search | ||
| 1347 | * LIB_DIRS -- directories from which actions and other libs are loaded | ||
| 1348 | * ACTIONS_SETS -- list of action "sets", appended to ACTIONS_DIRS | ||
| 1349 | * HTML_DIRS -- html file inlude directories | ||
| 1350 | * SESS_VAR_DIR -- used by filesystem session handling to store sess data | ||
| 1351 | * DEBUG -- positive number, enables debugging with verbosity level | ||
| 1352 | |||
| 1353 | Some entries may be omitted and default values are: | ||
| 1354 | |||
| 1355 | * LIB_DIRS -- [ "$APP_ROOT/lib" ] | ||
| 1356 | * ACTIONS_SETS -- [ $APP_NAME, 'Base', 'Core' ] | ||
| 1357 | * HTML_DIRS -- [ "$APP_ROOT/html" ] | ||
| 1358 | * SESS_VAR_DIR -- [ "$APP_ROOT/var" ] | ||
| 1359 | * DEBUG -- 0 | ||
| 1360 | |||
| 1361 | =head1 API FUNCTIONS | ||
| 1362 | |||
| 1363 | # TODO: input | ||
| 1364 | # TODO: sessions | ||
| 1365 | # TODO: arguments, constructing links | ||
| 1366 | # TODO: forwarding | ||
| 1367 | # TODO: html, forms, session keeping | ||
| 1368 | |||
| 1369 | =head1 DEPLOYMENT, DIRECTORIES, FILESYSTEM STRUCTURE | ||
| 1370 | |||
| 1371 | # TODO: install, cpan, manual, github, custom locations | ||
| 1372 | # TODO: sessions dir, custom storage/session handling | ||
| 1373 | |||
| 1374 | =head1 PROJECT STATUS | ||
| 1375 | |||
| 1376 | At the moment Web::Reactor is in beta. API is mostly frozen but it is fairly | ||
| 1377 | possible to be changed and/or extended. However drastic changes are not planned :) | ||
| 1378 | |||
| 1379 | If you are interested in the project or have some notes etc, contact me at: | ||
| 1380 | |||
| 1381 | Vladi Belperchinov-Shabanski "Cade" | ||
| 1382 | <cade@bis.bg> | ||
| 1383 | <cade@biscom.net> | ||
| 1384 | <cade@cpan.org> | ||
| 1385 | <cade@datamax.bg> | ||
| 1386 | |||
| 1387 | further contact info, mailing list and github repository is listed below. | ||
| 1388 | |||
| 1389 | =head1 FIXME: TODO: | ||
| 1390 | |||
| 1391 | * config examples | ||
| 1392 | * pages example | ||
| 1393 | * actions example | ||
| 1394 | * API description (input data, safe data, sessions, forwarding, actions, html) | ||
| 1395 | * ... | ||
| 1396 | |||
| 1397 | =head1 REQUIRED ADDITIONAL MODULES | ||
| 1398 | |||
| 1399 | Reactor uses mostly perl core modules but it needs few others: | ||
| 1400 | |||
| 1401 | * CGI | ||
| 1402 | * Exception::Sink | ||
| 1403 | * Data::Tools | ||
| 1404 | |||
| 1405 | =head1 DEMO APPLICATION | ||
| 1406 | |||
| 1407 | Documentation will be improved shortly, but meanwhile you can check 'demo' | ||
| 1408 | directory inside distribution tarball or inside the github repository. This is | ||
| 1409 | fully functional (however stupid :)) application. It shows how data is processed, | ||
| 1410 | calling pages/views, inspecting page (calling views) stack, html forms automation, | ||
| 1411 | forwarding. | ||
| 1412 | |||
| 1413 | =head1 MAILING LIST | ||
| 1414 | |||
| 1415 | web-reactor@googlegroups.com | ||
| 1416 | |||
| 1417 | =head1 GITHUB REPOSITORY | ||
| 1418 | |||
| 1419 | https://github.com/cade-vs/perl-web-reactor | ||
| 1420 | |||
| 1421 | git clone git://github.com/cade-vs/perl-web-reactor.git | ||
| 1422 | |||
| 1423 | =head1 AUTHOR | ||
| 1424 | |||
| 1425 | Vladi Belperchinov-Shabanski "Cade" | ||
| 1426 | |||
| 1427 | <cade@biscom.net> <cade@datamax.bg> <cade@cpan.org> | ||
| 1428 | |||
| 1429 | http://cade.datamax.bg | ||
| 1430 | |||
| 1431 | =cut | ||
| 1432 | |||
| 1433 | ############################################################################## | ||
| 1434 | 1; | ||
| 1435 | ###EOF######################################################################## |