| 1 | # -*- Mode: perl -*- | ||
| 2 | # | ||
| 3 | # $Id: MxScreen.pm,v 0.1.1.1 2001/05/30 21:13:07 ram Exp $ | ||
| 4 | # | ||
| 5 | # Copyright (c) 1998-2001, Raphael Manfredi | ||
| 6 | # Copyright (c) 2000-2001, Christophe Dehaudt | ||
| 7 | # | ||
| 8 | # You may redistribute only under the terms of the Artistic License, | ||
| 9 | # as specified in the README file that comes with the distribution. | ||
| 10 | # | ||
| 11 | # HISTORY | ||
| 12 | # $Log: MxScreen.pm,v $ | ||
| 13 | # Revision 0.1.1.1 2001/05/30 21:13:07 ram | ||
| 14 | # patch1: fixed HISTORY section | ||
| 15 | # patch1: random cleanup in named argument docs | ||
| 16 | # patch1: updated version number | ||
| 17 | # | ||
| 18 | # Revision 0.1 2001/04/22 17:57:03 ram | ||
| 19 | # Baseline for first Alpha release. | ||
| 20 | # | ||
| 21 | # $EndLog$ | ||
| 22 | # | ||
| 23 | |||
| 24 | |||
| 25 | use strict; | ||
| 26 | |||
| 27 | package CGI::MxScreen; | ||
| 28 | |||
| 29 | use vars qw($VERSION $BIN_VERSION); | ||
| 30 | $VERSION = '0.103'; | ||
| 31 | $BIN_VERSION = '0.1'; | ||
| 32 | |||
| 33 | use CGI::MxScreen::Constant; | ||
| 34 | use CGI::MxScreen::Error; | ||
| 35 | use Carp::Datum; | ||
| 36 | use Log::Agent; | ||
| 37 | use Getargs::Long; | ||
| 38 | use Time::HiRes qw(time); | ||
| 39 | |||
| 40 | require CGI; | ||
| 41 | require CGI::MxScreen::Form::Field; | ||
| 42 | require CGI::MxScreen::Form::Button; | ||
| 43 | require CGI::MxScreen::Layout; | ||
| 44 | require CGI::MxScreen::Session; | ||
| 45 | |||
| 46 | my @managers; # For END {} | ||
| 47 | |||
| 48 | # | ||
| 49 | # ->make | ||
| 50 | # | ||
| 51 | # Creation routine. | ||
| 52 | # | ||
| 53 | sub make { | ||
| 54 | DFEATURE my $f_; | ||
| 55 | my $self = bless {}, shift; | ||
| 56 | my ($tm_start, $tm_user, $tm_sys) = (time, times); | ||
| 57 | |||
| 58 | # | ||
| 59 | # Prevent anything to be written to STDOUT by tieing it to a package | ||
| 60 | # that will log anything written there, without letting it go through. | ||
| 61 | # | ||
| 62 | |||
| 63 | require CGI::MxScreen::Tie::Stdout; | ||
| 64 | tie *main::STDOUT, "CGI::MxScreen::Tie::Stdout"; | ||
| 65 | |||
| 66 | # | ||
| 67 | # Argument parsing. | ||
| 68 | # | ||
| 69 | |||
| 70 | ( | ||
| 71 | $self->{_screen_list}, | ||
| 72 | $self->{_initial_state}, | ||
| 73 | $self->{_cgi_version}, | ||
| 74 | $self->{_valid_time}, | ||
| 75 | $self->{_bgcolor}, | ||
| 76 | $self->{_layout}, | ||
| 77 | ) = | ||
| 78 | cxgetargs(@_, | ||
| 79 | -screens => 'HASH', | ||
| 80 | -initial => undef, | ||
| 81 | -version => [undef, '1.0'], | ||
| 82 | -timeout => ['i'], | ||
| 83 | -bgcolor => [undef, '#bfbfbf'], | ||
| 84 | -layout => ['CGI::MxScreen::Layout'], | ||
| 85 | ); | ||
| 86 | |||
| 87 | $self->{_start_times} = [$tm_start, $tm_user, $tm_sys]; | ||
| 88 | $self->{_last_times} = [$tm_start, $tm_user, $tm_sys]; | ||
| 89 | $self->{_layout} = CGI::MxScreen::Layout->make() | ||
| 90 | unless defined $self->{_layout}; | ||
| 91 | |||
| 92 | # | ||
| 93 | # Perform default initialization if not already done via a call | ||
| 94 | # to "use CGI::MxScreen::Config;". | ||
| 95 | # | ||
| 96 | |||
| 97 | require CGI::MxScreen::Config; | ||
| 98 | CGI::MxScreen::Config::configure(); # Will return if already done | ||
| 99 | |||
| 100 | if (defined $CGI::MxScreen::Config::LOG) { | ||
| 101 | $self->{_log} = $CGI::MxScreen::Config::LOG; | ||
| 102 | } else { | ||
| 103 | use File::Spec; | ||
| 104 | require Log::Agent::Logger; | ||
| 105 | require Log::Agent::Channel::File; | ||
| 106 | |||
| 107 | my $devnull = Log::Agent::Channel::File->make( | ||
| 108 | -filename => File::Spec->devnull, | ||
| 109 | -no_prefixing => 1, | ||
| 110 | -no_ucfirst => 1, | ||
| 111 | -no_newline => 1, | ||
| 112 | ); | ||
| 113 | $self->{_log} = Log::Agent::Logger->make(-channel => $devnull); | ||
| 114 | } | ||
| 115 | |||
| 116 | # | ||
| 117 | # Now that logging is up, validate the creation routine parameters. | ||
| 118 | # | ||
| 119 | |||
| 120 | logcroak "-initial must be either a plain scalar or an ARRAY ref" | ||
| 121 | if ref $self->initial_state && ref $self->initial_state ne "ARRAY"; | ||
| 122 | |||
| 123 | my $state_name = ref $self->initial_state ? | ||
| 124 | $self->initial_state->[0] : $self->initial_state; | ||
| 125 | |||
| 126 | logcroak "initial state '$state_name' is not a known state" | ||
| 127 | unless $self->is_valid_state($state_name); | ||
| 128 | |||
| 129 | # | ||
| 130 | # Initialize whole script context. | ||
| 131 | # | ||
| 132 | |||
| 133 | $self->trace_incoming; # XXX if logtrace_at("info") or logdebug_at("warn") | ||
| 134 | |||
| 135 | my $session = $self->{_session} = CGI::MxScreen::Session->make( | ||
| 136 | -serializer => $CGI::MxScreen::Config::SERIALIZER, | ||
| 137 | -medium => $CGI::MxScreen::Config::MEDIUM, | ||
| 138 | ); | ||
| 139 | |||
| 140 | $self->{_context} = $session->restore; | ||
| 141 | $self->check_validity(); | ||
| 142 | |||
| 143 | # | ||
| 144 | # Relink all serialized screens to the new manager. | ||
| 145 | # | ||
| 146 | |||
| 147 | my $ctx = $self->ctx; | ||
| 148 | |||
| 149 | if (exists $ctx->{'screens'}) { | ||
| 150 | foreach my $screen (values %{$ctx->{'screens'}}) { | ||
| 151 | $screen->relink_to_manager($self); | ||
| 152 | } | ||
| 153 | } | ||
| 154 | |||
| 155 | # | ||
| 156 | # For session logging, maintain the following parameters in | ||
| 157 | # the private CGI::MxScreen context. | ||
| 158 | # | ||
| 159 | # log_session unique session ID for logging (IP number-PID) | ||
| 160 | # log_starttime time when session started | ||
| 161 | # log_cnt counter incremented each time we're invoked | ||
| 162 | # | ||
| 163 | # | ||
| 164 | |||
| 165 | unless (exists $ctx->{'log_session'}) { | ||
| 166 | $ctx->{'log_session'} = CGI::remote_host() . "-" . $$; | ||
| 167 | $ctx->{'log_starttime'} = int(time); | ||
| 168 | $ctx->{'log_cnt'} = 0; | ||
| 169 | } else { | ||
| 170 | $ctx->{'log_cnt'}++; | ||
| 171 | } | ||
| 172 | |||
| 173 | # | ||
| 174 | # From now on, all Log::Agent messages will bear the session ID. | ||
| 175 | # | ||
| 176 | |||
| 177 | require Log::Agent::Tag::String; | ||
| 178 | use Log::Agent qw(logtags); | ||
| 179 | |||
| 180 | my $tag = Log::Agent::Tag::String->make( | ||
| 181 | -name => "session id", | ||
| 182 | -value => "(" . $ctx->{'log_session'} . ")", | ||
| 183 | ); | ||
| 184 | |||
| 185 | my $log = $self->log; | ||
| 186 | $log->tags->append($tag); | ||
| 187 | logtags()->append($tag); | ||
| 188 | |||
| 189 | $log->warning(""); | ||
| 190 | $log->warning(\&log_session, $self); | ||
| 191 | $log->info(\&log_agent, $self); | ||
| 192 | $log->debug(\&log_inc_times, $self, "context restore + log init"); | ||
| 193 | |||
| 194 | # | ||
| 195 | # Process incoming parameters, trap all errors. | ||
| 196 | # | ||
| 197 | # Since we might be using CGI::Carp, we must cancel any trap hook by | ||
| 198 | # localizing the __DIE__ and __WARN__ special handlers. | ||
| 199 | # | ||
| 200 | |||
| 201 | eval { | ||
| 202 | local $SIG{__DIE__}; | ||
| 203 | local $SIG{__WARN__}; | ||
| 204 | |||
| 205 | $self->process_params; | ||
| 206 | }; | ||
| 207 | $log->debug(\&log_inc_times, $self, "parameter init"); | ||
| 208 | $self->internal_error($@) if chomp $@; | ||
| 209 | |||
| 210 | push(@managers, $self); | ||
| 211 | |||
| 212 | return DVAL $self; | ||
| 213 | } | ||
| 214 | |||
| 215 | # | ||
| 216 | # ->process_params | ||
| 217 | # | ||
| 218 | # Get CGI parameters, fill internal data structures. | ||
| 219 | # | ||
| 220 | sub process_params { | ||
| 221 | DFEATURE my $f_; | ||
| 222 | my $self = shift; | ||
| 223 | |||
| 224 | # | ||
| 225 | # Save params provided by CGI | ||
| 226 | # | ||
| 227 | # It is a quite big story because there are different possiblities | ||
| 228 | # for the store location according to the way the fields have been | ||
| 229 | # recorded, and also according to the storage indication settings. | ||
| 230 | # | ||
| 231 | # When the field has been recorded at display time (use of | ||
| 232 | # record_field method), it might contain some storage indications | ||
| 233 | # (see Form::Field). It may also contain an indication to not save | ||
| 234 | # the value (useful for password). Anyway, when the incoming param | ||
| 235 | # matches a recorded field from the last display, the store_value | ||
| 236 | # method of the field is invoked to perform the task according to | ||
| 237 | # the indication. Returning true indicates there is no need to | ||
| 238 | # save the param value in the MxScreen repository (see | ||
| 239 | # below). Actually, either the value has been save somewhere else | ||
| 240 | # or there were some indication to not keep the value persistent. | ||
| 241 | # | ||
| 242 | # When there is no specific indication for the storage (either the | ||
| 243 | # field has not been recorded but has been merely displayed, or | ||
| 244 | # the store_value method returned false), the value is memorized | ||
| 245 | # into the MxScreen repository for the orphan params. It is a | ||
| 246 | # dedicated section of the context. Each orphan params is stored | ||
| 247 | # in that section under the index of the screen name. All the orphan | ||
| 248 | # params are replayed --put into the CGI param list-- to benefit | ||
| 249 | # from their values when the field is once again displayed. | ||
| 250 | # | ||
| 251 | # NOTES: You have to know that a button press returns also a value | ||
| 252 | # into the incoming CGI param list. The following code needs to | ||
| 253 | # take care of that by filtering them before being considered as | ||
| 254 | # orphan fields. For simple button, it is quite easy since the | ||
| 255 | # param name must have been recorded into the Mxscreen Button | ||
| 256 | # list, but for image button the returned param does not match | ||
| 257 | # exactly the one recorded. For this latter button, the returned | ||
| 258 | # param is in fact 2 params which indicates the click | ||
| 259 | # location. Their name is composed by the param name (recorded in | ||
| 260 | # the Mxscreen Button list) plus '.x' or '.y'. | ||
| 261 | # | ||
| 262 | # NOTES: CGI param list does not alway returned a value for all | ||
| 263 | # displayed field of the screen. For some specific elements (for | ||
| 264 | # instance checkbox group), no value is returned when the field is | ||
| 265 | # cleared (no box checked in the previous example). This clear | ||
| 266 | # value must however be saved into the storage location. To cope | ||
| 267 | # this problem, all the known displayed fields (those in the | ||
| 268 | # recorded list of fields, and those in the orphan repository of | ||
| 269 | # the screen) are checked to validate the existence of a value | ||
| 270 | # into the CGI param list. When no value is found, a clear value | ||
| 271 | # ('') is enforced. | ||
| 272 | # | ||
| 273 | |||
| 274 | # load the package of the last screen where all needed classes | ||
| 275 | # should have been defined. | ||
| 276 | my $current_state = $self->initial_state; | ||
| 277 | $current_state = $self->ctx->{'current_state'} if | ||
| 278 | (defined $self->ctx->{'current_state'}); | ||
| 279 | my ($screen_name) = $self->scatter($current_state); | ||
| 280 | $self->load_screen_package($screen_name); | ||
| 281 | |||
| 282 | # build a easy access way to recorded field and button: make a | ||
| 283 | # hash from the array | ||
| 284 | my $var_ctx = $self->context(PERSISTENT); | ||
| 285 | my $field_hash = {}; | ||
| 286 | for my $field (@{$self->context(SCREEN_FIELD)}) { | ||
| 287 | DASSERT $field->isa('CGI::MxScreen::Form::Field'); | ||
| 288 | $field_hash->{$field->name} = $field; | ||
| 289 | } | ||
| 290 | |||
| 291 | my $button_hash = {}; | ||
| 292 | for my $button (@{$self->context(SCREEN_BUTTON)}) { | ||
| 293 | DASSERT $button->isa('CGI::MxScreen::Form::Button'); | ||
| 294 | $button_hash->{$button->name} = $button; | ||
| 295 | } | ||
| 296 | |||
| 297 | # | ||
| 298 | # Patch the CGI param list for fields which are known to be | ||
| 299 | # displayed but no value appears in the CGI list. | ||
| 300 | # | ||
| 301 | |||
| 302 | my $cgi_param = $self->context(CGI_PARAM); | ||
| 303 | |||
| 304 | while (my ($k, $v) = each %{$cgi_param->{$screen_name}}) { | ||
| 305 | CGI::param(-name => $k, -values => $v) unless | ||
| 306 | defined CGI::param($k); | ||
| 307 | } | ||
| 308 | while (my ($k, $v) = each %$field_hash) { | ||
| 309 | CGI::param(-name => $k, -values => $v->value) unless | ||
| 310 | defined CGI::param($k); | ||
| 311 | } | ||
| 312 | |||
| 313 | # walkthrough the CGI param list to store values | ||
| 314 | for my $param (CGI::param()) { | ||
| 315 | DTRACE "storing incoming param $param"; | ||
| 316 | my $field = $field_hash->{$param}; | ||
| 317 | |||
| 318 | # return form CGI param might be either a single element or a | ||
| 319 | # list of elements. To get all of them, an array context must | ||
| 320 | # be used. Then, the value that will be stored is either the | ||
| 321 | # array reference or the first and single element of the | ||
| 322 | # array. | ||
| 323 | my @value = CGI::param($param); | ||
| 324 | my $value = $#value == 0 ? $value[0]: \@value; | ||
| 325 | |||
| 326 | if (defined $field) { | ||
| 327 | # | ||
| 328 | # Patch the value (if needed) | ||
| 329 | # Then store value according to the storage indication given in | ||
| 330 | # the field (if any) | ||
| 331 | # | ||
| 332 | my ($patched, $nvalue) = $field->patch_value($value); | ||
| 333 | if ($patched) { | ||
| 334 | CGI::param(-name => $param, -values => $nvalue) if $patched; | ||
| 335 | next if $field->store_value($var_ctx, $nvalue); | ||
| 336 | } else { | ||
| 337 | next if $field->store_value($var_ctx, $value); | ||
| 338 | } | ||
| 339 | } | ||
| 340 | # no storage indication is present | ||
| 341 | |||
| 342 | # | ||
| 343 | # perhaps it was a button rather than a field | ||
| 344 | # | ||
| 345 | |||
| 346 | # image button press is embarrassing. In such a case, the | ||
| 347 | # returned param is not 1 but 2 params which represents the | ||
| 348 | # location of the click within the image | ||
| 349 | if ($param =~ /(.*)\.([xy])$/) { | ||
| 350 | if (defined (my $x = CGI::param("$1.x")) && | ||
| 351 | defined (my $y = CGI::param("$1.y"))) { | ||
| 352 | next if $2 eq "y"; # do the job only for x | ||
| 353 | $param = $1; | ||
| 354 | } | ||
| 355 | } | ||
| 356 | |||
| 357 | if (defined $button_hash->{$param}) { | ||
| 358 | $self->internal_error( | ||
| 359 | "invalid input form: buttons '" . $self->button_pressed->name . | ||
| 360 | "' and '$param' were simultaneously pressed!") | ||
| 361 | if defined $self->button_pressed; | ||
| 362 | |||
| 363 | # Remember it as the button that was pressed | ||
| 364 | $self->{_button_pressed} = $button_hash->{$param}; | ||
| 365 | next; | ||
| 366 | } | ||
| 367 | |||
| 368 | # It is an orphan field that has not been saved. Keep it in | ||
| 369 | # mind into the param repository. The param context is stored | ||
| 370 | # under the name of the screen to build a kind of | ||
| 371 | # hierachy. That allows the clean up functionality when | ||
| 372 | # leaving a screen (on explicit request). | ||
| 373 | $cgi_param->{$screen_name}->{$param} = $value; | ||
| 374 | } | ||
| 375 | |||
| 376 | # all orphan params will populate the CGI's param list if they are | ||
| 377 | # not already present. That will allow to prefill fields when | ||
| 378 | # redisplay and to give an access to their values with regular | ||
| 379 | # CGI::param(). | ||
| 380 | # | ||
| 381 | # Information is organized in a hash table where the key is the | ||
| 382 | # screen id (state name) and the value is another hash. The latter | ||
| 383 | # contains the pair of data: symbol, value that must be restored. | ||
| 384 | while (my ($screen , $hash) = each %$cgi_param) { | ||
| 385 | while (my ($k, $v) = each %$hash) { | ||
| 386 | CGI::param(-name => $k, -values => $v); | ||
| 387 | } | ||
| 388 | } | ||
| 389 | |||
| 390 | return DVOID; | ||
| 391 | } | ||
| 392 | |||
| 393 | |||
| 394 | ######################################################################### | ||
| 395 | # Internal Attribute Access: these methods are not intended to be used # | ||
| 396 | # from the external world. # | ||
| 397 | ######################################################################### | ||
| 398 | |||
| 399 | sub screen_list { $_[0]->{'_screen_list'} } | ||
| 400 | sub context_root { $_[0]->{'_context'} } | ||
| 401 | sub screen { $_[0]->{'_screen'} } | ||
| 402 | sub session { $_[0]->{'_session'} } | ||
| 403 | sub cgi_version { $_[0]->{'_cgi_version'} } | ||
| 404 | sub valid_time { $_[0]->{'_valid_time'} } | ||
| 405 | sub initial_state { $_[0]->{'_initial_state'} } | ||
| 406 | sub bgcolor { $_[0]->{'_bgcolor'} } | ||
| 407 | sub layout { $_[0]->{'_layout'} } | ||
| 408 | sub log { $_[0]->{'_log'} } | ||
| 409 | sub start_times { $_[0]->{'_start_times'} } | ||
| 410 | sub last_times { $_[0]->{'_last_times'} } | ||
| 411 | |||
| 412 | sub button_pressed { $_[0]->{_button_pressed} } | ||
| 413 | sub ctx { | ||
| 414 | defined $_[0]->{'_context'} ? $_[0]->{'_context'}->[MXSCREEN] : {} | ||
| 415 | } | ||
| 416 | |||
| 417 | # | ||
| 418 | # ->is_valid_state | ||
| 419 | # | ||
| 420 | # Check whether state is known | ||
| 421 | # | ||
| 422 | sub is_valid_state { | ||
| 423 | DFEATURE my $f_; | ||
| 424 | my $self = shift; | ||
| 425 | my ($state) = @_; | ||
| 426 | |||
| 427 | return DVAL exists $self->screen_list->{$state}; | ||
| 428 | } | ||
| 429 | |||
| 430 | # | ||
| 431 | # ->load_screen_package | ||
| 432 | # | ||
| 433 | # Load source file for the class implementing the screen $name, unless | ||
| 434 | # it is already present. | ||
| 435 | # | ||
| 436 | sub load_screen_package { | ||
| 437 | DFEATURE my $f_; | ||
| 438 | my $self = shift; | ||
| 439 | my ($name) = @_; | ||
| 440 | |||
| 441 | DREQUIRE $self->is_valid_state($name), "valid state '$name'"; | ||
| 442 | |||
| 443 | my ($class_name) = cgetargs(@{$self->screen_list->{$name}}, | ||
| 444 | {-strict => 0}, | ||
| 445 | qw(class)); | ||
| 446 | |||
| 447 | # | ||
| 448 | # The following eval "" attempts to load the screen class by using | ||
| 449 | # a require, assuming there is one class by file. However, we | ||
| 450 | # check for the presence of an @ISA variable in the target package | ||
| 451 | # before performing the require, since the application could have | ||
| 452 | # already loaded all the screen classes. Given that all screens must | ||
| 453 | # inherit from CGI::MxScreen::Screen, we know @ISA is defined if the | ||
| 454 | # package is present. | ||
| 455 | # | ||
| 456 | |||
| 457 | eval "require $class_name unless defined \@${class_name}::ISA;"; | ||
| 458 | if (chomp $@) { | ||
| 459 | logerr "loading of $class_name failed: $@"; | ||
| 460 | logdie "can't locate class \"$class_name\" for screen state \"$name\""; | ||
| 461 | } | ||
| 462 | |||
| 463 | return DVOID; | ||
| 464 | } | ||
| 465 | |||
| 466 | # | ||
| 467 | # ->make_screen | ||
| 468 | # | ||
| 469 | # Create the screen for given state. | ||
| 470 | # | ||
| 471 | sub make_screen { | ||
| 472 | DFEATURE my $f_; | ||
| 473 | my $self = shift; | ||
| 474 | my ($name) = @_; | ||
| 475 | |||
| 476 | DREQUIRE $self->is_valid_state($name), "valid state '$name'"; | ||
| 477 | |||
| 478 | $self->load_screen_package($name); | ||
| 479 | my ($class_name, @remaining) = cgetargs(@{$self->screen_list->{$name}}, | ||
| 480 | {-strict => 0, -extra => 1}, | ||
| 481 | qw(class)); | ||
| 482 | |||
| 483 | # | ||
| 484 | # If the state has already been seen already, it has been serialized | ||
| 485 | # in the context, but it needs to be relinked to the new manager instance. | ||
| 486 | # | ||
| 487 | # Otherwise, a new object is created and remembered in the context. | ||
| 488 | # | ||
| 489 | |||
| 490 | my $cxt = $self->ctx; # CGI::MxScreen own private context | ||
| 491 | my $screen; | ||
| 492 | |||
| 493 | if (exists $cxt->{'screens'}->{$name}) { | ||
| 494 | $screen = $cxt->{'screens'}->{$name}; | ||
| 495 | $screen->remake($self); | ||
| 496 | } else { | ||
| 497 | $screen = $class_name->make( | ||
| 498 | -manager => $self, | ||
| 499 | -name => $name, | ||
| 500 | @remaining | ||
| 501 | ); | ||
| 502 | $cxt->{'screens'}->{$name} = $screen; | ||
| 503 | } | ||
| 504 | |||
| 505 | return DVAL $screen; | ||
| 506 | } | ||
| 507 | |||
| 508 | # | ||
| 509 | # ->scatter | ||
| 510 | # | ||
| 511 | # Return: | ||
| 512 | # either a list with a single element when incoming param is a | ||
| 513 | # scalar value or a list with all element of the incoming list. | ||
| 514 | # | ||
| 515 | sub scatter { | ||
| 516 | DFEATURE my $f_; | ||
| 517 | my $self = shift; | ||
| 518 | my ($id) = @_; | ||
| 519 | |||
| 520 | return DARY @$id if ref $id eq 'ARRAY'; | ||
| 521 | return DARY ($id); | ||
| 522 | } | ||
| 523 | |||
| 524 | # | ||
| 525 | # ->obj_scatter | ||
| 526 | # | ||
| 527 | # Same as scatter(), but handles ($obj, $routine, @args) as well. | ||
| 528 | # Supplies the screen if no blessed object is identified in the first | ||
| 529 | # position of the list. | ||
| 530 | # | ||
| 531 | sub obj_scatter { | ||
| 532 | DFEATURE my $f_; | ||
| 533 | my $self = shift; | ||
| 534 | my ($screen, $id) = @_; | ||
| 535 | |||
| 536 | return DARY ($screen, $id) unless ref $id eq 'ARRAY'; | ||
| 537 | |||
| 538 | if (ref $id->[0] && UNIVERSAL::isa($id->[0], "UNIVERSAL")) { | ||
| 539 | $screen = $id->[0]; | ||
| 540 | return DARY ($screen, @$id[1..$#$id]); | ||
| 541 | } | ||
| 542 | |||
| 543 | return DARY ($screen, @$id); | ||
| 544 | } | ||
| 545 | |||
| 546 | ######################################################################### | ||
| 547 | # Class Feature: usable from the external world # | ||
| 548 | ######################################################################### | ||
| 549 | |||
| 550 | # | ||
| 551 | # ->context | ||
| 552 | # | ||
| 553 | # return a reference of a given section withtin the overal context | ||
| 554 | # area | ||
| 555 | # | ||
| 556 | # Arguments: | ||
| 557 | # $index: index of the context section to returned | ||
| 558 | # | ||
| 559 | # Return: | ||
| 560 | # a reference to the requested context section | ||
| 561 | # | ||
| 562 | sub context { | ||
| 563 | DFEATURE my $f_; | ||
| 564 | |||
| 565 | DREQUIRE $_[1] =~ /^\d+$/; | ||
| 566 | DREQUIRE $_[1] >= 0 && $_[1] < CONTEXT_COUNT; | ||
| 567 | |||
| 568 | return DVAL $_[0]->context_root->[$_[1]]; | ||
| 569 | } | ||
| 570 | |||
| 571 | # | ||
| 572 | # ->spring_screen | ||
| 573 | # ->previous_screen | ||
| 574 | # ->current_screen | ||
| 575 | # | ||
| 576 | # Returns [state, display_args] | ||
| 577 | # | ||
| 578 | sub spring_screen { | ||
| 579 | DFEATURE my $f_; | ||
| 580 | return DVAL $_[0]->ctx->{'spring_state'}; # Last stable state(args) | ||
| 581 | } | ||
| 582 | sub previous_screen { | ||
| 583 | DFEATURE my $f_; | ||
| 584 | return DVAL $_[0]->ctx->{'previous_state'}; # Previous state(args) | ||
| 585 | } | ||
| 586 | sub current_screen { | ||
| 587 | DFEATURE my $f_; | ||
| 588 | return DVAL $_[0]->ctx->{'current_state'}; # Current state(args) | ||
| 589 | } | ||
| 590 | |||
| 591 | |||
| 592 | # | ||
| 593 | # ->play | ||
| 594 | # | ||
| 595 | # Play the sequence of action necessary to display the new screen. | ||
| 596 | # | ||
| 597 | sub play { | ||
| 598 | DFEATURE my $f_; | ||
| 599 | my $self = shift; | ||
| 600 | |||
| 601 | # coderef is a temporary arg until storable is able to select things to | ||
| 602 | # store (storable::Hook) | ||
| 603 | my ($coderef) = @_; | ||
| 604 | |||
| 605 | my $log = $self->log; | ||
| 606 | $log->debug(\&log_inc_times, $self, "outside CGI::MxScreen"); | ||
| 607 | |||
| 608 | # | ||
| 609 | # Compute target screen, trap all errors. | ||
| 610 | # | ||
| 611 | # Since we might be using CGI::Carp, we must cancel any trap hook by | ||
| 612 | # localizing the __DIE__ and __WARN__ special handlers. | ||
| 613 | # | ||
| 614 | |||
| 615 | my ($screen, $args); | ||
| 616 | eval { | ||
| 617 | local $SIG{__DIE__}; | ||
| 618 | local $SIG{__WARN__}; | ||
| 619 | |||
| 620 | ($screen, $args) = $self->compute_screen; | ||
| 621 | }; | ||
| 622 | $log->debug(\&log_inc_times, $self, "screen computation"); | ||
| 623 | $self->internal_error($@) if chomp $@; | ||
| 624 | |||
| 625 | # | ||
| 626 | # Emit CGI headers | ||
| 627 | # From now on, output is safe and will not get us a server error. | ||
| 628 | # | ||
| 629 | |||
| 630 | untie *main::STDOUT; # Restore original STDOUT stream | ||
| 631 | |||
| 632 | # | ||
| 633 | # If they configured us to buffer all STDOUT until context is ready | ||
| 634 | # to be emitted, then create object, print headers and mark the | ||
| 635 | # output of headers as done: further output to STDOUT will be buffered | ||
| 636 | # and printed only after the context. | ||
| 637 | # | ||
| 638 | # The reason for this is to have the context emitted before any other | ||
| 639 | # form widget. That way, pressing a submit button before the whole form | ||
| 640 | # is loaded in the browser won't matter as much, since we'll have at | ||
| 641 | # least the context to propagate in the POST parameters. | ||
| 642 | # | ||
| 643 | |||
| 644 | my $stdout; | ||
| 645 | if ($CGI::MxScreen::cf::mx_buffer_stdout) { | ||
| 646 | require CGI::MxScreen::Tie::Buffered_Output; | ||
| 647 | $stdout = tie *main::STDOUT, "CGI::MxScreen::Tie::Buffered_Output"; | ||
| 648 | } | ||
| 649 | |||
| 650 | # | ||
| 651 | # Display screen, with proper "bounce" exception support. | ||
| 652 | # Returns screen that was finally displayed. | ||
| 653 | # | ||
| 654 | |||
| 655 | $screen = $self->display($screen, $args, $stdout); | ||
| 656 | $log->debug(\&log_inc_times, $self, "\"%s\" display", $screen->name); | ||
| 657 | |||
| 658 | # | ||
| 659 | # Snapshot current time and last modification date of the | ||
| 660 | # scriptright before saving context. That fields can be used to | ||
| 661 | # check for session validity. | ||
| 662 | # | ||
| 663 | |||
| 664 | $self->ctx->{'time'} = time; | ||
| 665 | $self->ctx->{'script_date'} = (stat($0))[9]; | ||
| 666 | |||
| 667 | # | ||
| 668 | # Cleanup context to avoid saving transient data | ||
| 669 | # | ||
| 670 | |||
| 671 | &{$coderef}() if defined $coderef; # TBR | ||
| 672 | |||
| 673 | for my $f (@{$self->context_root->[SCREEN_FIELD]}) { | ||
| 674 | DASSERT $f->isa('CGI::MxScreen::Form::Field'); | ||
| 675 | $f->cleanup(); | ||
| 676 | } | ||
| 677 | for my $b (@{$self->context_root->[SCREEN_BUTTON]}) { | ||
| 678 | DASSERT $b->isa('CGI::MxScreen::Form::Button'); | ||
| 679 | $b->cleanup(); | ||
| 680 | } | ||
| 681 | |||
| 682 | # | ||
| 683 | # If STDOUT was bufferd, the context must be emitted explicitely | ||
| 684 | # between the header of the form and the remaining data. | ||
| 685 | # | ||
| 686 | |||
| 687 | if (defined $stdout) { | ||
| 688 | my $context = $self->session->save; | ||
| 689 | $stdout->print_all($context); | ||
| 690 | untie_stdout(); | ||
| 691 | } else { | ||
| 692 | print $self->session->save; | ||
| 693 | } | ||
| 694 | |||
| 695 | $log->debug(\&log_inc_times, $self, "context save"); | ||
| 696 | |||
| 697 | # | ||
| 698 | # Emit CGI trailers. | ||
| 699 | # | ||
| 700 | |||
| 701 | print CGI::endform; | ||
| 702 | |||
| 703 | my $layout = $self->layout; | ||
| 704 | $layout->postamble; | ||
| 705 | $layout->end_HTML; | ||
| 706 | |||
| 707 | return DVOID; | ||
| 708 | } | ||
| 709 | |||
| 710 | # | ||
| 711 | # ->compute_screen | ||
| 712 | # | ||
| 713 | # Compute target screen, and run and enter/leave hooks if we change screens. | ||
| 714 | # This routine does not display anything, but runs all the action callbacks. | ||
| 715 | # | ||
| 716 | # Returns new screen object, and a ref to the argument list. | ||
| 717 | # | ||
| 718 | sub compute_screen { | ||
| 719 | DFEATURE my $f_; | ||
| 720 | my $self = shift; | ||
| 721 | my ($current_state, $previous_state, $new_state); | ||
| 722 | my ($origin_name, $target_name, @arg_list); | ||
| 723 | my $screen; | ||
| 724 | my $errors = 0; | ||
| 725 | my $ctx = $self->ctx; | ||
| 726 | |||
| 727 | |||
| 728 | # get the current state from the context its format can be either | ||
| 729 | # 'screen_name' or ['screen_name', @arg_list]. 'screen_name' is the | ||
| 730 | # symbol key given to a screen name into the given screen list (at | ||
| 731 | # make time) and @arg_list is a list of arg to pass to the display | ||
| 732 | # routine of the screen. | ||
| 733 | |||
| 734 | $current_state = $self->initial_state; | ||
| 735 | $current_state = $ctx->{'current_state'} if | ||
| 736 | (defined $ctx->{'current_state'}); | ||
| 737 | |||
| 738 | $previous_state = $current_state; | ||
| 739 | $new_state = $current_state; | ||
| 740 | |||
| 741 | # | ||
| 742 | # Compute the destination and process the associated actions when | ||
| 743 | # a button has been detected as pressed (during the make method). | ||
| 744 | # | ||
| 745 | # If we could not identify a button that was pressed, we'll simply | ||
| 746 | # remain in the current state and re-display the form unless there was | ||
| 747 | # a default button recorded in the previous screen. | ||
| 748 | # | ||
| 749 | |||
| 750 | my $button_pressed = $self->button_pressed; | ||
| 751 | |||
| 752 | if ($ctx->{'log_cnt'} && !defined $button_pressed) { | ||
| 753 | |||
| 754 | # | ||
| 755 | # Create the previous screen to lookup for a default button | ||
| 756 | # | ||
| 757 | |||
| 758 | ($origin_name) = $self->scatter($previous_state); | ||
| 759 | $screen = $self->make_screen($origin_name); | ||
| 760 | my $default = $screen->default_button; | ||
| 761 | |||
| 762 | if (defined $default) { | ||
| 763 | $button_pressed = $self->{_button_pressed} = $default; | ||
| 764 | $self->log->warning("no button pressed, using default \"%s\"", | ||
| 765 | $default->value); | ||
| 766 | } else { | ||
| 767 | $self->log->error( | ||
| 768 | "no button pressed, no default, will stay in same state"); | ||
| 769 | } | ||
| 770 | } | ||
| 771 | |||
| 772 | if (defined $button_pressed) { | ||
| 773 | |||
| 774 | # | ||
| 775 | # Create the previous screen to perform the actions | ||
| 776 | # Screen could have been created above, during the default | ||
| 777 | # button computation, hence the check. | ||
| 778 | # | ||
| 779 | |||
| 780 | unless (defined $screen) { | ||
| 781 | ($origin_name) = $self->scatter($previous_state); | ||
| 782 | $screen = $self->make_screen($origin_name); | ||
| 783 | } | ||
| 784 | |||
| 785 | # Those are not serialized | ||
| 786 | DASSERT !defined $screen->error_env, "no callback error condition"; | ||
| 787 | DASSERT !defined $screen->error, "no user error condition"; | ||
| 788 | |||
| 789 | my $act_env; # Action environment | ||
| 790 | |||
| 791 | if (defined $button_pressed->action) { | ||
| 792 | DASSERT ref $button_pressed->action eq 'ARRAY'; | ||
| 793 | |||
| 794 | use CGI::MxScreen::Error qw(is_mx_errcode); | ||
| 795 | require CGI::MxScreen::Action_Env; | ||
| 796 | |||
| 797 | $act_env = CGI::MxScreen::Action_Env->make(); | ||
| 798 | |||
| 799 | for my $action (@{$button_pressed->action}) { | ||
| 800 | my ($obj, $routine, @routine_arg) = | ||
| 801 | $self->obj_scatter($screen, $action); | ||
| 802 | |||
| 803 | my $errcode = $obj->$routine(@routine_arg, $act_env); | ||
| 804 | |||
| 805 | # | ||
| 806 | # Temporary safety net whilst migration of all callback | ||
| 807 | # returned values is ongoing. | ||
| 808 | # | ||
| 809 | |||
| 810 | if ($errcode == 0 || $errcode == 1) { | ||
| 811 | logwarn "callback %s->%s returned OLD boolean status", | ||
| 812 | ref $obj, $routine; | ||
| 813 | $errcode = $errcode ? CGI_MX_OK : CGI_MX_ABORT; | ||
| 814 | } | ||
| 815 | |||
| 816 | VERIFY is_mx_errcode($errcode), | ||
| 817 | "callback ", ref($obj), "->$routine returns valid code", | ||
| 818 | " -- returned $errcode"; | ||
| 819 | |||
| 820 | next if $errcode == CGI_MX_OK; | ||
| 821 | |||
| 822 | # | ||
| 823 | # an error occurred, don't process the remaining | ||
| 824 | # of actions if it is CGI_MX_ABORT. | ||
| 825 | # | ||
| 826 | # The screen is tagged with an error flag and the state | ||
| 827 | # destination is resumed to the origin screen. | ||
| 828 | # | ||
| 829 | |||
| 830 | my $called = sprintf "%s->%s", ref($obj), $routine; | ||
| 831 | my $binfo = sprintf "for button \"%s\"", | ||
| 832 | $button_pressed->value; | ||
| 833 | $binfo .= sprintf " (%s)", $button_pressed->name | ||
| 834 | if $button_pressed->name ne $button_pressed->value; | ||
| 835 | |||
| 836 | DTRACE "error in action callback: $called $binfo"; | ||
| 837 | $self->log->error("action callback $called failed $binfo%s", | ||
| 838 | $errcode == CGI_MX_ABORT ? ", aborting" : ""); | ||
| 839 | |||
| 840 | $errors++; | ||
| 841 | $screen->set_error_env($act_env); | ||
| 842 | $act_env->add_error($obj, $routine, \@routine_arg); | ||
| 843 | last if $errcode == CGI_MX_ABORT; | ||
| 844 | } | ||
| 845 | |||
| 846 | $new_state = $current_state if $errors; | ||
| 847 | } | ||
| 848 | |||
| 849 | # | ||
| 850 | # Get the destination | ||
| 851 | # | ||
| 852 | # * when an error was found, we look at -on_error or -dyn_on_error, | ||
| 853 | # and if one is found, we clear the error condition. | ||
| 854 | # * when no error is raised, we look at -dyn_target or -target. | ||
| 855 | # | ||
| 856 | |||
| 857 | if ($errors) { | ||
| 858 | # | ||
| 859 | # Look for possible error trapping, which will force a move to | ||
| 860 | # an alternate screen. The error condition is reset, therefore | ||
| 861 | # the internal context of the screen will be cleared. | ||
| 862 | # | ||
| 863 | # For -dyn_on_error, we append the action environment. | ||
| 864 | # | ||
| 865 | |||
| 866 | DASSERT defined $act_env, "at least one action ran"; | ||
| 867 | |||
| 868 | if ($button_pressed->has_error_trap) { | ||
| 869 | my $dyn = $button_pressed->dyn_on_error; | ||
| 870 | if (defined $dyn) { | ||
| 871 | my ($routine, @args) = $self->scatter($dyn); | ||
| 872 | DASSERT $screen->can($routine); | ||
| 873 | $new_state = $screen->$routine(@args, $act_env); | ||
| 874 | } else { | ||
| 875 | $new_state = $button_pressed->on_error; | ||
| 876 | } | ||
| 877 | DASSERT defined $new_state; | ||
| 878 | $errors = 0; # Moving to alternate screen | ||
| 879 | } | ||
| 880 | } else { | ||
| 881 | # | ||
| 882 | # No error found. | ||
| 883 | # | ||
| 884 | |||
| 885 | if ($button_pressed->is_computed_target) { | ||
| 886 | my ($routine, @args) = | ||
| 887 | $self->scatter($button_pressed->dyn_target); | ||
| 888 | DASSERT $screen->can($routine); | ||
| 889 | |||
| 890 | $new_state = $screen->$routine(@args); | ||
| 891 | } | ||
| 892 | else { | ||
| 893 | $new_state = $button_pressed->target; | ||
| 894 | } | ||
| 895 | } | ||
| 896 | } | ||
| 897 | |||
| 898 | # clear context area dedicated to save field handles | ||
| 899 | $self->context_root->[SCREEN_FIELD] = []; | ||
| 900 | $self->context_root->[SCREEN_BUTTON] = []; | ||
| 901 | # context might have been saved by the screen -> also clear the copy | ||
| 902 | $screen->_clear_internal_context() if defined $screen && !$errors; | ||
| 903 | |||
| 904 | # | ||
| 905 | # update the MXSCREEN context | ||
| 906 | # | ||
| 907 | $ctx->{'current_state'} = $new_state; | ||
| 908 | $ctx->{'previous_state'} = $previous_state unless $errors; | ||
| 909 | $ctx->{'cgi_version'} = $self->cgi_version; | ||
| 910 | $ctx->{'bin_version'} = $BIN_VERSION; | ||
| 911 | |||
| 912 | $self->log->notice(\&log_state, $self, $previous_state, $new_state); | ||
| 913 | |||
| 914 | # | ||
| 915 | # Create the destination state (if needed) | ||
| 916 | # Then call ->leave and ->enter hooks. | ||
| 917 | # | ||
| 918 | |||
| 919 | ($target_name, @arg_list) = $self->scatter($new_state); | ||
| 920 | unless (defined $screen && $target_name eq $origin_name) { | ||
| 921 | my $prev_screen = $screen; | ||
| 922 | $screen = $self->make_screen($target_name); | ||
| 923 | if (defined $prev_screen) { | ||
| 924 | $prev_screen->leave($screen); | ||
| 925 | $ctx->{'spring_state'} = $previous_state; # Where we came from | ||
| 926 | } | ||
| 927 | $screen->enter($prev_screen); | ||
| 928 | } | ||
| 929 | |||
| 930 | return DARY ($screen, \@arg_list); | ||
| 931 | } | ||
| 932 | |||
| 933 | # | ||
| 934 | # ->display | ||
| 935 | # | ||
| 936 | # Display $screen, with args @$args, with proper support for screen "bounce". | ||
| 937 | # | ||
| 938 | # If $stdout is not undef, then it is a ref to a tied object, meaning STDOUT | ||
| 939 | # is buffered. When bouncing with untied STDOUT, the layout and the headers | ||
| 940 | # can only be emitted once, i.e. for the original screen. A warning is issued | ||
| 941 | # if bouncing. | ||
| 942 | # | ||
| 943 | # Returns screen that was finally displayed. | ||
| 944 | # | ||
| 945 | sub display { | ||
| 946 | DFEATURE my $f_; | ||
| 947 | my $self = shift; | ||
| 948 | my ($screen, $args, $stdout) = @_; | ||
| 949 | |||
| 950 | for (my $i = 0; $i < 20; $i++) { # Max 20 bounces | ||
| 951 | |||
| 952 | # | ||
| 953 | # Can only emit the layout and the header each time when $stdout | ||
| 954 | # is tied. We always emit the first time, naturally, since we | ||
| 955 | # don't know whether we'll bounce at all. | ||
| 956 | # | ||
| 957 | |||
| 958 | if ($i == 0 || defined $stdout) { | ||
| 959 | |||
| 960 | # | ||
| 961 | # The layout object controls the following aspects: | ||
| 962 | # | ||
| 963 | # html headers | ||
| 964 | # preamble | ||
| 965 | # <form goes here> | ||
| 966 | # postabmle | ||
| 967 | # html trailers | ||
| 968 | # | ||
| 969 | |||
| 970 | my $layout = $self->layout; | ||
| 971 | |||
| 972 | $layout->init($screen); | ||
| 973 | $layout->start_HTML( | ||
| 974 | -title => $screen->screen_title, | ||
| 975 | -bgcolor => $screen->bgcolor, | ||
| 976 | ); | ||
| 977 | $layout->preamble; | ||
| 978 | |||
| 979 | # | ||
| 980 | # Start the form | ||
| 981 | # | ||
| 982 | |||
| 983 | my @args = (-method => 'POST', -action => CGI::url()); | ||
| 984 | print $CGI::DISABLE_UPLOADS ? | ||
| 985 | CGI::startform(@args) : CGI::start_multipart_form(@args); | ||
| 986 | |||
| 987 | } | ||
| 988 | |||
| 989 | $stdout->header_ok if defined $stdout; # Buffer remaining as BODY | ||
| 990 | |||
| 991 | # | ||
| 992 | # Display target screen, trap all errors. | ||
| 993 | # | ||
| 994 | |||
| 995 | eval { | ||
| 996 | local $SIG{__DIE__}; | ||
| 997 | local $SIG{__WARN__}; | ||
| 998 | |||
| 999 | $screen->display(@$args); | ||
| 1000 | }; | ||
| 1001 | |||
| 1002 | # | ||
| 1003 | # Deal with "bounce" exceptions. | ||
| 1004 | # | ||
| 1005 | |||
| 1006 | if (ref $@ && $@->isa("CGI::MxScreen::Exception::Bounce")) { | ||
| 1007 | my $old_state = $self->current_screen; | ||
| 1008 | my $new_state = $@->target; | ||
| 1009 | my $log = $self->log; | ||
| 1010 | my $old_name = $screen->name; | ||
| 1011 | my $old_screen = $screen; | ||
| 1012 | |||
| 1013 | $log->notice(\&log_bounce, $self, $old_state, $new_state, $@); | ||
| 1014 | $log->debug(\&log_inc_times, $self, "bounce on \"%s\"", $old_name); | ||
| 1015 | |||
| 1016 | my ($target_name, @arg_list) = $self->scatter($new_state); | ||
| 1017 | |||
| 1018 | # | ||
| 1019 | # Clear buffered data in tied STDOUT, so we may start afresh | ||
| 1020 | # with new screen. If the old screen had started emitting data | ||
| 1021 | # before bouncing, warn them: the screen should not have done so | ||
| 1022 | # anyway, so we may discard data bluntly. | ||
| 1023 | # | ||
| 1024 | |||
| 1025 | if (defined $stdout) { | ||
| 1026 | my $discarded = $stdout->reset; | ||
| 1027 | logwarn "discarded %d byte%s emitted by \"%s\" " . | ||
| 1028 | "(before bouncing to \"%s\")", | ||
| 1029 | $discarded, $discarded == 1 ? "" : "s", | ||
| 1030 | $old_name, $target_name if $discarded; | ||
| 1031 | } | ||
| 1032 | |||
| 1033 | # | ||
| 1034 | # Set args for next loop. | ||
| 1035 | # | ||
| 1036 | |||
| 1037 | $screen = $self->make_screen($target_name); | ||
| 1038 | $args = \@arg_list; | ||
| 1039 | $self->ctx->{'current_state'} = $new_state; | ||
| 1040 | |||
| 1041 | # | ||
| 1042 | # Need to call ->leave() and ->enter() when states are different. | ||
| 1043 | # We pass undef to leave() to indicate that we left as the result | ||
| 1044 | # of a bounce. | ||
| 1045 | # | ||
| 1046 | # We don't alter `spring_state' though. | ||
| 1047 | # | ||
| 1048 | |||
| 1049 | if ($target_name ne $screen->name) { | ||
| 1050 | $old_screen->leave(undef); # Signals: bounced | ||
| 1051 | $screen->enter($old_screen); | ||
| 1052 | } | ||
| 1053 | |||
| 1054 | next; # Restart display loop | ||
| 1055 | } | ||
| 1056 | |||
| 1057 | # | ||
| 1058 | # Regular display error. | ||
| 1059 | # | ||
| 1060 | |||
| 1061 | if (ref $@ || chomp $@) { | ||
| 1062 | my $msg = $@; | ||
| 1063 | $msg =~ s/^\(.*?\)\s+//; # Remove already added session tag | ||
| 1064 | $self->log->critical("display error for screen \"%s\": %s", | ||
| 1065 | $screen->name, $msg); | ||
| 1066 | |||
| 1067 | # | ||
| 1068 | # If they buffered STDOUT, it's nice, because the screen will not | ||
| 1069 | # mix regular output and the error message. And since we discard | ||
| 1070 | # even the form header, the Content-Type printed by CGI::Carp will | ||
| 1071 | # not even show! | ||
| 1072 | # | ||
| 1073 | |||
| 1074 | untie_stdout(1) if defined $stdout; | ||
| 1075 | logdie $msg; | ||
| 1076 | } | ||
| 1077 | |||
| 1078 | return DVAL $screen; # Successfully displayed the screen | ||
| 1079 | } | ||
| 1080 | |||
| 1081 | $self->log->critical("too many screen bounces"); | ||
| 1082 | logdie "possible infinite loop detected, aborting"; | ||
| 1083 | } | ||
| 1084 | |||
| 1085 | # | ||
| 1086 | # ->check_validity | ||
| 1087 | # | ||
| 1088 | # Check context validity: proper version, no timeout. | ||
| 1089 | # | ||
| 1090 | sub check_validity { | ||
| 1091 | DFEATURE my $f_; | ||
| 1092 | my $self = shift; | ||
| 1093 | |||
| 1094 | unless (defined $self->context_root) { | ||
| 1095 | logerr "mangled context from %s", CGI::remote_host(); | ||
| 1096 | $self->internal_error("cannot retrieve application context"); | ||
| 1097 | } | ||
| 1098 | |||
| 1099 | my $ctx = $self->ctx; | ||
| 1100 | return DVOID unless exists $ctx->{'cgi_version'}; # Empty context | ||
| 1101 | |||
| 1102 | # | ||
| 1103 | # Ensure binary version (which traces variations in the way session | ||
| 1104 | # context are represented) is compatible. | ||
| 1105 | # | ||
| 1106 | |||
| 1107 | my $bin = $ctx->{'bin_version'}; | ||
| 1108 | if ($bin > $BIN_VERSION) { | ||
| 1109 | $self->internal_error(<<EOS); | ||
| 1110 | Script session used a format (v$bin) more recent than I am (v$BIN_VERSION). | ||
| 1111 | Please restart a new session. | ||
| 1112 | EOS | ||
| 1113 | } | ||
| 1114 | |||
| 1115 | # | ||
| 1116 | # check that the script file has not been modified (compare the | ||
| 1117 | # last modification time on the file system) | ||
| 1118 | # | ||
| 1119 | if ($ctx->{'script_date'} != (stat($0))[9]) { | ||
| 1120 | $self->internal_error(<<EOS); | ||
| 1121 | Script file has been modified since the last display, | ||
| 1122 | please restart a new session. | ||
| 1123 | EOS | ||
| 1124 | } | ||
| 1125 | |||
| 1126 | # | ||
| 1127 | # check whether the cgi version is still the same | ||
| 1128 | # | ||
| 1129 | if (defined $ctx->{'cgi_version'}) { | ||
| 1130 | my $version = $ctx->{'cgi_version'}; | ||
| 1131 | |||
| 1132 | if ($version ne $self->cgi_version) { | ||
| 1133 | $self->internal_error(<<EOS); | ||
| 1134 | Script version has evolved since the last display, please restart a new session. | ||
| 1135 | EOS | ||
| 1136 | } | ||
| 1137 | } | ||
| 1138 | |||
| 1139 | # | ||
| 1140 | # check whether the timeout is not exhausted | ||
| 1141 | # | ||
| 1142 | if (defined $self->valid_time && defined $ctx->{'time'}) { | ||
| 1143 | my $last_time = $ctx->{'time'}; | ||
| 1144 | |||
| 1145 | if ((time - $last_time) > $self->valid_time) { | ||
| 1146 | $self->internal_error(<<EOS); | ||
| 1147 | Session timeout since the last display, please restart a new session. | ||
| 1148 | EOS | ||
| 1149 | } | ||
| 1150 | } | ||
| 1151 | |||
| 1152 | return DVOID; | ||
| 1153 | } | ||
| 1154 | |||
| 1155 | # | ||
| 1156 | # ->internal_error | ||
| 1157 | # | ||
| 1158 | # | ||
| 1159 | sub internal_error { | ||
| 1160 | DFEATURE my $f_; | ||
| 1161 | my $self = shift; | ||
| 1162 | my ($message) = @_; | ||
| 1163 | |||
| 1164 | my $logmsg = $message; | ||
| 1165 | $logmsg =~ s/\s+/ /sg; | ||
| 1166 | logerr "internal error: $logmsg"; | ||
| 1167 | |||
| 1168 | untie_stdout(1); # Restore original STDOUT stream, discard all | ||
| 1169 | |||
| 1170 | my $layout = $self->layout; | ||
| 1171 | |||
| 1172 | $layout->init(undef); | ||
| 1173 | $layout->start_HTML("Internal Script Error"); | ||
| 1174 | $layout->preamble; | ||
| 1175 | |||
| 1176 | print CGI::h1("Internal Script Error"); | ||
| 1177 | print CGI::p(CGI::tt(ucfirst($message))); | ||
| 1178 | print CGI::p(CGI::a({-href => CGI::url()}, "Restart a new session")); | ||
| 1179 | |||
| 1180 | $layout->postamble; | ||
| 1181 | $layout->end_HTML; | ||
| 1182 | |||
| 1183 | my $log = $self->log; | ||
| 1184 | $log->alert("internal error: $logmsg") if defined $log; | ||
| 1185 | |||
| 1186 | exit 0; | ||
| 1187 | } | ||
| 1188 | |||
| 1189 | # | ||
| 1190 | # ->trace_incoming | ||
| 1191 | # | ||
| 1192 | # Trace incoming parameters | ||
| 1193 | # | ||
| 1194 | sub trace_incoming { | ||
| 1195 | DFEATURE my $f_; | ||
| 1196 | foreach my $p (CGI::param()) { | ||
| 1197 | my $value = CGI::param($p); | ||
| 1198 | DTRACE(TRC_INFO, "incoming param: '$p' => '$value'"); | ||
| 1199 | } | ||
| 1200 | return DVOID; | ||
| 1201 | } | ||
| 1202 | |||
| 1203 | # | ||
| 1204 | # (log_session) -- logging callback | ||
| 1205 | # | ||
| 1206 | # Log session state | ||
| 1207 | # | ||
| 1208 | sub log_session { | ||
| 1209 | DFEATURE my $f_; | ||
| 1210 | my $self = shift; | ||
| 1211 | my $current = $self->current_screen || $self->initial_state; | ||
| 1212 | my $cxt = $self->ctx; | ||
| 1213 | my $cnt = $cxt->{log_cnt}; | ||
| 1214 | my ($state) = $self->scatter($current); | ||
| 1215 | my $user = CGI::remote_user(); | ||
| 1216 | my @url_param = CGI::url_param(); | ||
| 1217 | my $query = join(';', map { "$_=" . CGI::url_param($_) } @url_param); | ||
| 1218 | |||
| 1219 | my $msg = sprintf "[%s/%d]", $state, $cnt; | ||
| 1220 | $msg .= sprintf " t=%s", relative_age(int(time) - $cxt->{log_starttime}); | ||
| 1221 | $msg .= sprintf " d=%s", relative_age($^T - $cxt->{time}) if $cnt; | ||
| 1222 | $msg .= " u=\"$user\"" if $user; | ||
| 1223 | |||
| 1224 | # | ||
| 1225 | # If there were no parameters on the URL, CGI still returns one entry | ||
| 1226 | # for a "keywords" parameter, so we need to guard against this as well. | ||
| 1227 | # | ||
| 1228 | |||
| 1229 | $msg .= " q=\"$query\"" if $query ne '' && $query ne 'keywords='; | ||
| 1230 | |||
| 1231 | return DVAL $msg; | ||
| 1232 | } | ||
| 1233 | |||
| 1234 | # | ||
| 1235 | # (log_state) -- logging callback | ||
| 1236 | # | ||
| 1237 | # Log state change and button pressed | ||
| 1238 | # | ||
| 1239 | sub log_state { | ||
| 1240 | DFEATURE my $f_; | ||
| 1241 | my $self = shift; | ||
| 1242 | my ($old, $new) = @_; | ||
| 1243 | my $cxt = $self->ctx; | ||
| 1244 | my $cnt = $cxt->{log_cnt}; | ||
| 1245 | |||
| 1246 | my ($old_state, @old_args) = $self->scatter($old); | ||
| 1247 | |||
| 1248 | my $msg = sprintf "%s%s", | ||
| 1249 | $old_state, @old_args ? ("(" . join(', ', @old_args) . ")") : ""; | ||
| 1250 | |||
| 1251 | unless ($cnt) { # First time | ||
| 1252 | return DVAL '' unless @old_args; # Don't log state if no args | ||
| 1253 | return DVAL $msg | ||
| 1254 | } | ||
| 1255 | |||
| 1256 | my ($new_state, @new_args) = $self->scatter($new); | ||
| 1257 | |||
| 1258 | $msg .= sprintf " -> %s%s", | ||
| 1259 | $new_state, @new_args ? ("(" . join(', ', @new_args) . ")") : ""; | ||
| 1260 | |||
| 1261 | # | ||
| 1262 | # Log button pressed, or bounce indication. | ||
| 1263 | # | ||
| 1264 | |||
| 1265 | my $button = $self->button_pressed; | ||
| 1266 | if (defined $button) { | ||
| 1267 | my $name = $button->name; | ||
| 1268 | my $value = $button->value; | ||
| 1269 | $msg .= sprintf " on \"%s\" pressed", $value; | ||
| 1270 | $msg .= sprintf " (%s)", $name if $value ne $name; | ||
| 1271 | } | ||
| 1272 | |||
| 1273 | return DVAL $msg; | ||
| 1274 | } | ||
| 1275 | |||
| 1276 | # | ||
| 1277 | # (log_bounce) -- logging callback | ||
| 1278 | # | ||
| 1279 | # Log screen bounces | ||
| 1280 | # | ||
| 1281 | sub log_bounce { | ||
| 1282 | DFEATURE my $f_; | ||
| 1283 | my $self = shift; | ||
| 1284 | my ($old, $new, $bounce) = @_; | ||
| 1285 | my $cxt = $self->ctx; | ||
| 1286 | |||
| 1287 | my ($old_state, @old_args) = $self->scatter($old); | ||
| 1288 | |||
| 1289 | my $msg = sprintf "%s%s", | ||
| 1290 | $old_state, @old_args ? ("(" . join(', ', @old_args) . ")") : ""; | ||
| 1291 | |||
| 1292 | my ($new_state, @new_args) = $self->scatter($new); | ||
| 1293 | |||
| 1294 | $msg .= sprintf " -> %s%s", | ||
| 1295 | $new_state, @new_args ? ("(" . join(', ', @new_args) . ")") : ""; | ||
| 1296 | |||
| 1297 | $msg .= " (via $bounce)"; | ||
| 1298 | |||
| 1299 | return DVAL $msg; | ||
| 1300 | } | ||
| 1301 | |||
| 1302 | # | ||
| 1303 | # (log_agent) -- logging callback | ||
| 1304 | # | ||
| 1305 | # Log user agent | ||
| 1306 | # | ||
| 1307 | sub log_agent { | ||
| 1308 | DFEATURE my $f_; | ||
| 1309 | my $self = shift; | ||
| 1310 | my $cnt = $self->ctx->{log_cnt}; | ||
| 1311 | return if $cnt; # Nothing after first time | ||
| 1312 | return DVAL sprintf "using \"%s\"", CGI::user_agent(); | ||
| 1313 | } | ||
| 1314 | |||
| 1315 | # | ||
| 1316 | # (log_inc_times) -- logging callback | ||
| 1317 | # | ||
| 1318 | # Log incremental time between values recorded in last_times and now. | ||
| 1319 | # Update last_times as a side effect for next incremental logging. | ||
| 1320 | # | ||
| 1321 | sub log_inc_times { | ||
| 1322 | DFEATURE my $f_; | ||
| 1323 | my $self = shift; | ||
| 1324 | my ($fmt, @args) = @_; # Can be single string or (fmt, args) | ||
| 1325 | $fmt = sprintf $fmt, @args if @args; | ||
| 1326 | my $times = $self->last_times; | ||
| 1327 | my $new_times = [time, (times)[0,1]]; | ||
| 1328 | $self->{_last_times} = $new_times; | ||
| 1329 | my @delta; | ||
| 1330 | for (my $i = 0; $i < @$times; $i++) { | ||
| 1331 | $delta[$i] = $new_times->[$i] - $times->[$i]; | ||
| 1332 | } | ||
| 1333 | return DVAL sprintf "t=%.2fs usr=%.2fs sys=%.2fs [%s]", @delta, $fmt; | ||
| 1334 | } | ||
| 1335 | |||
| 1336 | # | ||
| 1337 | # (log_total_time) -- logging callback | ||
| 1338 | # | ||
| 1339 | # Log total time spent since start_times. | ||
| 1340 | # | ||
| 1341 | sub log_total_time { | ||
| 1342 | DFEATURE my $f_; | ||
| 1343 | my $self = shift; | ||
| 1344 | my $times = $self->start_times; | ||
| 1345 | my $new_times = [time, (times)[0,1]]; | ||
| 1346 | my @delta; | ||
| 1347 | for (my $i = 0; $i < @$times; $i++) { | ||
| 1348 | $delta[$i] = $new_times->[$i] - $times->[$i]; | ||
| 1349 | } | ||
| 1350 | my $runtime = time - $^T; | ||
| 1351 | return DVAL sprintf "t=%.2fs usr=%.2fs sys=%.2fs [total time] T=%.2fs", | ||
| 1352 | @delta, $runtime; | ||
| 1353 | } | ||
| 1354 | |||
| 1355 | # | ||
| 1356 | # relative_age | ||
| 1357 | # | ||
| 1358 | # Given seconds, convert to 4d9h23m15s format. | ||
| 1359 | # | ||
| 1360 | sub relative_age { | ||
| 1361 | DFEATURE my $f_; | ||
| 1362 | my ($secs) = @_; | ||
| 1363 | my ($days, $hours, $mins); | ||
| 1364 | |||
| 1365 | $days = int($secs / (24 * 60 * 60)); | ||
| 1366 | $secs -= $days * (24 * 60 * 60); | ||
| 1367 | |||
| 1368 | $hours = int($secs / (60 * 60)); | ||
| 1369 | $secs -= $hours * (60 * 60); | ||
| 1370 | |||
| 1371 | $mins = int($secs / 60); | ||
| 1372 | $secs -= $mins * 60; | ||
| 1373 | |||
| 1374 | my $retstr = ''; | ||
| 1375 | $retstr .= $days . "d" if $days; | ||
| 1376 | $retstr .= $hours . "h" if $hours; | ||
| 1377 | $retstr .= $mins . "m" if $mins; | ||
| 1378 | $retstr .= int($secs + 0.5) . "s"; # can be fractional with Time::HiRes | ||
| 1379 | |||
| 1380 | return DVAL $retstr; | ||
| 1381 | } | ||
| 1382 | |||
| 1383 | # | ||
| 1384 | # ::add_utils_path -- static | ||
| 1385 | # | ||
| 1386 | # Screen designers can identify new Form::Utils packages for their own | ||
| 1387 | # specific uses with this routine. It must be invoked in the user | ||
| 1388 | # script as a static routine => CGI::MxScreen::add_utils_path , and | ||
| 1389 | # before the creation of the MxScreen object. | ||
| 1390 | # | ||
| 1391 | # NB: This routine name is misleading: it does not involve file paths, but | ||
| 1392 | # module names. The purpose is to allow some kind of routine lookup to | ||
| 1393 | # be able to locate a validation routine named "is_time" for instance. | ||
| 1394 | # I'm keeping it for now, because it's been used in production, but this | ||
| 1395 | # mechanism will have to be revisited. | ||
| 1396 | # -- RAM, 13/04/2001 | ||
| 1397 | # | ||
| 1398 | sub add_utils_path { | ||
| 1399 | DFEATURE my $f_; | ||
| 1400 | |||
| 1401 | VERIFY defined($_[0]) && !UNIVERSAL::isa($_[0], __PACKAGE__); | ||
| 1402 | |||
| 1403 | require CGI::MxScreen::Form::Utils; | ||
| 1404 | |||
| 1405 | CGI::MxScreen::Form::Utils::add_path(@_); | ||
| 1406 | return DVOID; | ||
| 1407 | } | ||
| 1408 | |||
| 1409 | # | ||
| 1410 | # ::untie_stdout | ||
| 1411 | # | ||
| 1412 | # Safely untie STDOUT by forcing a DESTROY, in case someone holds a reference | ||
| 1413 | # on the tied object. | ||
| 1414 | # | ||
| 1415 | sub untie_stdout { | ||
| 1416 | DFEATURE my $f_; | ||
| 1417 | my ($discard) = @_; | ||
| 1418 | my $stdout = tied *main::STDOUT; | ||
| 1419 | |||
| 1420 | # | ||
| 1421 | # Within CGI::MxScreen, all the packages that can be tied to STDOUT are | ||
| 1422 | # heirs of CGI::MxScreen::Tie::Sinkable, which provides a discard_all() | ||
| 1423 | # method. | ||
| 1424 | # | ||
| 1425 | |||
| 1426 | DASSERT !defined($stdout) || $stdout->isa("CGI::MxScreen::Tie::Sinkable"); | ||
| 1427 | |||
| 1428 | if (defined $stdout) { | ||
| 1429 | logtrc 'info', "un-tieing STDOUT (%s) with%s discarding", | ||
| 1430 | ref $stdout, $discard ? "" : "out"; | ||
| 1431 | $stdout->discard_all if defined $discard && $discard; | ||
| 1432 | $stdout->DESTROY; | ||
| 1433 | untie *main::STDOUT; | ||
| 1434 | } | ||
| 1435 | return DVOID; | ||
| 1436 | } | ||
| 1437 | |||
| 1438 | # | ||
| 1439 | # END | ||
| 1440 | # | ||
| 1441 | # Whatever happens, log total running time, provided they created a manager. | ||
| 1442 | # | ||
| 1443 | sub END { | ||
| 1444 | untie_stdout(); # They might have not got a chance to do so yet | ||
| 1445 | |||
| 1446 | # | ||
| 1447 | # Log running time, once per manager. | ||
| 1448 | # | ||
| 1449 | |||
| 1450 | foreach my $self (@managers) { | ||
| 1451 | $self->log->info(\&log_total_time, $self); | ||
| 1452 | } | ||
| 1453 | } | ||
| 1454 | |||
| 1455 | 1; | ||
| 1456 | __END__ |