3 ###----------------------------------------------------------------###
4 # See the perldoc in CGI/Ex/App.pod
5 # Copyright 2006 - Paul Seamons #
6 # Distributed under the Perl Artistic License without warranty #
7 ###----------------------------------------------------------------###
10 use vars
qw($VERSION);
15 Time
::HiRes-
>import('time') if eval {require Time
::HiRes
};
20 $msg = 'Something happened' if ! defined $msg;
21 die $msg if ref $msg || $msg =~ /\n\z/;
22 my ($pkg, $file, $line, $sub) = caller(1);
23 die "$msg in ${sub}() at $file line $line\n";
26 ###----------------------------------------------------------------###
29 my $class = shift || croak
"Usage: Package->new";
30 my $self = shift || {};
42 ###----------------------------------------------------------------###
45 my ($self, $args) = @_;
46 $self = $self->new($args) if ! ref $self;
48 $self->{'_time'} = time;
51 ### allow for authentication
52 my $ref = $self->require_auth;
53 if ($ref && ! ref $ref) {
54 return $self if ! $self->get_valid_auth;
57 ### a chance to do things at the very beginning
58 return $self if ! $self->{'_no_pre_navigate'} && $self->pre_navigate;
62 local $self->{'__morph_lineage_start_index'} = $#{$self->{'__morph_lineage'} || []};
66 ### rethrow the error unless we long jumped out of recursive nav_loop calls
67 croak
$@ if $@ ne "Long Jump\n";
70 ### one chance to do things at the very end
71 $self->post_navigate if ! $self->{'_no_post_navigate'};
75 $self->handle_error($@) if $@; # catch any errors
77 $self->{'_time'} = time;
87 ### keep from an infinate nesting
88 local $self->{'recurse'} = $self->{'recurse'} || 0;
89 if ($self->{'recurse'} ++ >= $self->recurse_limit) {
90 my $err = "recurse_limit (".$self->recurse_limit.") reached";
91 $err .= " number of jumps (".$self->{'jumps'}.")" if ($self->{'jumps'} || 0) > 1;
95 my $path = $self->path;
97 ### allow for an early return
98 return if $self->pre_loop($path); # a true value means to abort the navigate
100 my $req_auth = ref($self->require_auth) ? $self->require_auth : undef;
102 ### iterate on each step of the path
103 foreach ($self->{'path_i'} ||= 0;
104 $self->{'path_i'} <= $#$path;
105 $self->{'path_i'} ++) {
106 my $step = $path->[$self->{'path_i'}];
107 if ($step !~ /^([^\W0-9]\w*)$/) { # don't process the step if it contains odd characters
108 $self->stash->{'forbidden_step'} = $step;
109 $self->replace_path($self->forbidden_step);
112 $step = $1; # untaint
114 ### allow for per-step authentication
116 && $req_auth->{$step}
117 && ! $self->get_valid_auth) {
121 ### allow for becoming another package (allows for some steps in external files)
124 ### run the guts of the step
125 my $handled = $self->run_hook('run_step', $step);
127 ### Allow for the run_step to intercept.
128 ### A true status means the run_step took over navigation.
130 $self->unmorph($step);
134 ### if there are no future steps - allow for this step to designate one to follow
135 my $is_at_end = $self->{'path_i'} >= $#$path ? 1 : 0;
136 $self->run_hook('refine_path', $step, $is_at_end);
138 $self->unmorph($step);
141 ### allow for one exit point after the loop
142 return if $self->post_loop($path); # a true value means to abort the navigate
144 ### run the default step as a last resort
145 $self->insert_path($self->default_step);
146 $self->nav_loop; # go recursive
151 sub pre_navigate
{ 0 } # true means to not enter nav_loop
155 sub pre_loop
{ 0 } # true value means to abort the nav_loop routine
157 sub post_loop
{ 0 } # true value means to abort the nav_loop - don't recurse
159 sub recurse_limit
{ shift-
>{'recurse_limit'} || 15 }
161 ### default die handler - show what happened and die (so its in the error logs)
169 ###----------------------------------------------------------------###
171 sub default_step
{ shift-
>{'default_step'} || 'main' }
173 sub js_step
{ shift-
>{'js_step'} || 'js' }
175 sub forbidden_step
{ shift-
>{'forbidden_step'} || '__forbidden' }
177 sub step_key
{ shift-
>{'step_key'} || 'step' }
181 if (! $self->{'path'}) {
182 my $path = $self->{'path'} = []; # empty path
184 my $step = $self->form->{ $self->step_key };
185 $step = lc($1) if ! $step && $ENV{'PATH_INFO'} && $ENV{'PATH_INFO'} =~ m
|^/(\w
+)|;
187 ### make sure the step is valid
189 if ($step =~ /^_/) { # can't begin with _
190 $self->stash->{'forbidden_step'} = $step;
191 push @$path, $self->forbidden_step;
192 } elsif ($self->valid_steps # must be in valid_steps if defined
193 && ! $self->valid_steps->{$step}
194 && $step ne $self->default_step
195 && $step ne $self->js_step) {
196 $self->stash->{'forbidden_step'} = $step;
197 push @$path, $self->forbidden_step;
204 return $self->{'path'};
209 my $path = $self->{'path'} ||= [];
210 croak
"Cannot call set_path after the navigation loop has begun" if $self->{'path_i'};
211 splice @$path, 0, $#$path + 1, @_; # change entries in the ref (which updates other copies of the ref)
214 ### legacy - same as append_path
217 push @{ $self->path }, @_;
222 push @{ $self->path }, @_;
227 my $ref = $self->path;
228 my $i = $self->{'path_i'} || 0;
229 if ($i + 1 > $#$ref) {
232 splice(@$ref, $i + 1, $#$ref - $i, @_); # replace remaining entries
238 my $ref = $self->path;
239 my $i = $self->{'path_i'} || 0;
240 if ($i + 1 > $#$ref) {
243 splice(@$ref, $i + 1, 0, @_); # insert a path at the current location
247 ### a hash of paths that are allowed, default undef is all are allowed
250 ###----------------------------------------------------------------###
251 ### allow for checking where we are in the path and for jumping around
257 if (my $ref = $self->{'__morph_lineage'}) {
258 ### use the saved index - this allows for early "morphers" to only get rolled back so far
259 my $index = $self->{'__morph_lineage_start_index'};
260 $index = -1 if ! defined $index;
261 $self->unmorph while $#$ref != $index;
270 my $i = @_ == 1 ? shift : 1;
271 my $path = $self->path;
272 my $path_i = $self->{'path_i'};
273 croak
"Can't jump if nav_loop not started" if ! defined $path_i;
275 ### validate where we are jumping to
279 } elsif ($i eq 'LAST') {
280 $i = $#$path - $path_i;
281 } elsif ($i eq 'NEXT') {
283 } elsif ($i eq 'CURRENT') {
285 } elsif ($i eq 'PREVIOUS') {
287 } else { # look for a step by that name
288 for (my $j = $#$path; $j >= 0; $j --) {
289 if ($path->[$j] eq $i) {
296 if ($i !~ /^-?\d+$/) {
298 Carp
::croak
("Invalid jump index ($i)");
301 ### manipulate the path to contain the new jump location
303 my $cut_i = $path_i + $i;
304 if ($cut_i > $#$path) {
305 push @replace, $self->default_step;
306 } elsif ($cut_i < 0) {
307 push @replace, @$path;
309 push @replace, @$path[$cut_i .. $#$path];
311 $self->replace_path(@replace);
313 ### record the number of jumps
314 $self->{'jumps'} ||= 0;
317 ### run the newly fixed up path (recursively)
318 $self->{'path_i'} ++; # move along now that the path is updated
320 $self->exit_nav_loop;
323 sub step_by_path_index
{
326 my $ref = $self->path;
328 return $self->default_step if $i > $#$ref;
334 return $self->step_by_path_index( ($self->{'path_i'} || 0) - 1 );
339 return $self->step_by_path_index( ($self->{'path_i'} || 0) );
342 sub next_step
{ # method and hook
344 return $self->step_by_path_index( ($self->{'path_i'} || 0) + 1 );
349 return $self->step_by_path_index( $#{ $self->path } );
354 return $self->step_by_path_index( 0 );
357 ###----------------------------------------------------------------###
358 ### hooks and history
362 my $hook = shift || do { require Carp
; Carp
::confess
("Missing hook name") };
363 my $step = shift || '';
365 if ($step && ($code = $self->can("${step}_${hook}"))) {
366 return [$code, "${step}_${hook}"],
368 } elsif ($code = $self->can($hook)) {
369 return [$code, $hook];
382 my ($code, $found) = @{ $self->find_hook($hook, $step) };
384 croak
"Could not find a method named ${step}_${hook} or ${hook}";
395 push @{ $self->history }, $hist;
397 $hist->{'level'} = $self->{'_level'};
398 local $self->{'_level'} = 1 + ($self->{'_level'} || 0);
400 $hist->{'elapsed'} = time - $hist->{'time'};
402 my $resp = $self->$code($step, @_);
404 $hist->{'elapsed'} = time - $hist->{'time'};
405 $hist->{'response'} = $resp;
411 return shift-
>{'history'} ||= [];
416 my $all = shift || 0;
417 my $hist = $self->history;
419 push @$dump, sprintf("Elapsed: %.5f", time - $self->{'_time'});
421 ### show terse - yet informative info
422 foreach my $row (@$hist) {
424 || ref($row) ne 'HASH'
425 || ! exists $row->{'elapsed'}) {
428 my $note = (' ' x
($row->{'level'} || 0))
429 . join(' - ', $row->{'step'}, $row->{'meth'}, $row->{'found'}, sprintf('%.5f', $row->{'elapsed'}));
430 my $resp = $row->{'response'};
431 if (ref($resp) eq 'HASH' && ! scalar keys %$resp) {
433 } elsif (ref($resp) eq 'ARRAY' && ! @$resp) {
435 } elsif (! ref $resp || ! $all) {
436 my $max = $self->{'history_max'} || 30;
437 if (length($resp) > $max) {
438 $resp = substr($resp, 0, $max);
444 $note = [$note, $resp];
454 ###----------------------------------------------------------------###
455 ### utility methods to allow for storing separate steps in other modules
459 return $self->{'allow_morph'} ? 1 : 0;
462 sub allow_nested_morph
{
464 return $self->{'allow_nested_morph'} ? 1 : 0;
469 my $step = shift || return;
470 my $allow = $self->allow_morph($step) || return;
472 ### place to store the lineage
473 my $lin = $self->{'__morph_lineage'} ||= [];
474 my $cur = ref $self; # what are we currently
475 push @$lin, $cur; # store so subsequent unmorph calls can do the right thing
485 push @{ $self->history }, $hist;
487 if (ref($allow) && ! $allow->{$step}) { # hash - but no step - record for unbless
488 $hist->{'found'} .= " (not allowed to morph to that step)";
492 ### make sure we haven't already been reblessed
493 if ($#$lin != 0 # is this the second morph call
494 && (! ($allow = $self->allow_nested_morph($step)) # not true
495 || (ref($allow) && ! $allow->{$step}) # hash - but no step
497 $hist->{'found'} .= $allow ? " (not allowed to nested_morph to that step)" : " (nested_morph disabled)";
498 return 0; # just return - don't die so that we can morph early
501 ### if we are not already that package - bless us there
502 my $new = $self->run_hook('morph_package', $step);
504 my $file = $new .'.pm';
506 if (UNIVERSAL
::can
($new, 'can') # check if the package space exists
507 || eval { require $file }) { # check for a file that holds this package
508 ### become that package
510 $hist->{'found'} .= " (changed $cur to $new)";
511 $self->fixup_after_morph($step);
514 if ($@ =~ /^\s*(Can\'t locate \S+ in \@INC)/) { # let us know what happened
515 $hist->{'found'} .= " (failed from $cur to $new: $1)";
517 $hist->{'found'} .= " (failed from $cur to $new: $@)";
518 my $err = "Trouble while morphing to $file: $@";
525 $hist->{'response'} = 1;
531 my $step = shift || '__no_step';
532 my $lin = $self->{'__morph_lineage'} || return;
535 my $prev = pop(@$lin) || croak
"unmorph called more times than morph - current ($cur)";
536 delete $self->{'__morph_lineage'} if ! @$lin;
538 ### if we are not already that package - bless us there
547 push @{ $self->history }, $hist;
550 $self->fixup_before_unmorph($step);
552 $hist->{'found'} .= " (changed from $cur to $prev)";
554 $hist->{'found'} .= " (already isa $cur)";
557 $hist->{'response'} = 1;
561 sub fixup_after_morph
{}
563 sub fixup_before_unmorph
{}
565 ###----------------------------------------------------------------###
566 ### allow for authentication
568 sub navigate_authenticated
{
569 my ($self, $args) = @_;
570 $self = $self->new($args) if ! ref $self;
572 $self->require_auth(1);
574 return $self->navigate;
579 $self->{'require_auth'} = shift if @_ == 1;
580 return $self->{'require_auth'};
583 sub is_authed
{ shift-
>auth_data }
587 $self->{'auth_data'} = shift if @_ == 1;
588 return $self->{'auth_data'};
593 return 1 if $self->is_authed;
595 ### augment the args with sensible defaults
596 my $args = $self->auth_args;
597 $args->{'cgix'} ||= $self->cgix;
598 $args->{'form'} ||= $self->form;
599 $args->{'cookies'} ||= $self->cookies;
600 $args->{'js_uri_path'} ||= $self->js_uri_path;
601 $args->{'get_pass_by_user'} ||= sub { my ($auth, $user) = @_; $self->get_pass_by_user($user, $auth) };
602 $args->{'verify_user'} ||= sub { my ($auth, $user) = @_; $self->verify_user( $user, $auth) };
603 $args->{'cleanup_user'} ||= sub { my ($auth, $user) = @_; $self->cleanup_user( $user, $auth) };
604 $args->{'login_print'} ||= sub {
605 my ($auth, $template, $hash) = @_;
606 my $out = $self->run_hook('swap_template', '__login', $template, $hash);
607 $self->run_hook('fill_template', '__login', \
$out, $hash);
608 $self->run_hook('print_out', '__login', $out);
611 require CGI
::Ex
::Auth
;
612 my $obj = CGI
::Ex
::Auth-
>new($args);
613 my $resp = $obj->get_valid_auth;
615 my $data = $obj->last_auth_data;
616 delete $data->{'real_pass'} if defined $data; # data may be defined but false
617 $self->auth_data($data); # failed authentication may still have auth_data
619 return ($resp && $data) ? 1 : 0;
624 sub get_pass_by_user
{ die "get_pass_by_user is a virtual method and needs to be overridden for authentication to work" }
625 sub cleanup_user
{ my ($self, $user) = @_; $user }
626 sub verify_user
{ 1 }
628 ###----------------------------------------------------------------###
629 ### a few standard base accessors
633 $self->{'form'} = shift if @_ == 1;
634 return $self->{'form'} ||= $self->cgix->get_form;
639 $self->{'cookies'} = shift if @_ == 1;
640 return $self->{'cookies'} ||= $self->cgix->get_cookies;
645 $self->{'cgix'} = shift if @_ == 1;
646 return $self->{'cgix'} ||= do {
648 CGI
::Ex-
>new; # return of the do
654 $self->{'vob'} = shift if @_ == 1;
655 return $self->{'vob'} ||= do {
656 require CGI
::Ex
::Validate
;
657 CGI
::Ex
::Validate-
>new($self->vob_args); # return of the do
668 ### provide a place for placing variables
671 return $self->{'stash'} ||= {};
674 ###----------------------------------------------------------------###
675 ### default hook implementations
681 ### if the pre_step exists and returns true, exit the nav_loop
682 return 1 if $self->run_hook('pre_step', $step);
684 ### allow for skipping this step (but stay in the nav_loop)
685 return 0 if $self->run_hook('skip', $step);
687 ### see if we have complete valid information for this step
688 ### if so, do the next step
689 ### if not, get necessary info and print it out
690 if ( ! $self->run_hook('prepare', $step)
691 || ! $self->run_hook('info_complete', $step)
692 || ! $self->run_hook('finalize', $step)) {
694 ### show the page requesting the information
695 $self->run_hook('prepared_print', $step);
697 ### a hook after the printing process
698 $self->run_hook('post_print', $step);
703 ### a hook before end of loop
704 ### if the post_step exists and returns true, exit the nav_loop
705 return 1 if $self->run_hook('post_step', $step);
707 ### let the nav_loop continue searching the path
712 my ($self, $step, $is_at_end) = @_;
713 return 0 if ! $is_at_end; # if we aren't at the end of the path, don't do anything
715 my $next_step = $self->run_hook('next_step', $step) || return 0;
716 $self->run_hook('set_ready_validate', $step, 0);
717 $self->append_path($next_step);
725 my $hash_base = $self->run_hook('hash_base', $step) || {};
726 my $hash_comm = $self->run_hook('hash_common', $step) || {};
727 my $hash_form = $self->run_hook('hash_form', $step) || {};
728 my $hash_fill = $self->run_hook('hash_fill', $step) || {};
729 my $hash_swap = $self->run_hook('hash_swap', $step) || {};
730 my $hash_errs = $self->run_hook('hash_errors', $step) || {};
733 $hash_errs->{$_} = $self->format_error($hash_errs->{$_})
734 foreach keys %$hash_errs;
735 $hash_errs->{'has_errors'} = 1 if scalar keys %$hash_errs;
737 ### layer hashes together
738 my $fill = {%$hash_form, %$hash_base, %$hash_comm, %$hash_fill};
739 my $swap = {%$hash_form, %$hash_base, %$hash_comm, %$hash_swap, %$hash_errs};
741 ### run the print hook - passing it the form and fill info
742 $self->run_hook('print', $step, $swap, $fill);
746 my ($self, $step, $swap, $fill) = @_;
748 my $file = $self->run_hook('file_print', $step); # get a filename relative to base_dir_abs
750 my $out = $self->run_hook('swap_template', $step, $file, $swap);
752 $self->run_hook('fill_template', $step, \
$out, $fill);
754 $self->run_hook('print_out', $step, $out);
758 my ($self, $step, $out) = @_;
760 $self->cgix->print_content_type;
765 my ($self, $step, $file, $swap) = @_;
767 my $args = $self->run_hook('template_args', $step);
768 $args->{'INCLUDE_PATH'} ||= sub { $self->base_dir_abs || die "Could not find base_dir_abs while looking for template INCLUDE_PATH on step \"$step\"" };
770 require CGI
::Ex
::Template
;
771 my $t = CGI
::Ex
::Template-
>new($args);
774 $t->process($file, $swap, \
$out) || die $t->error;
779 sub template_args
{ {} }
782 my ($self, $step, $outref, $fill) = @_;
786 my $args = $self->run_hook('fill_args', $step);
787 local $args->{'text'} = $outref;
788 local $args->{'form'} = $fill;
790 require CGI
::Ex
::Fill
;
791 CGI
::Ex
::Fill
::fill
($args);
796 sub pre_step
{ 0 } # success indicates we handled step (don't continue step or loop)
797 sub skip
{ 0 } # success indicates to skip the step (and continue loop)
798 sub prepare
{ 1 } # failure means show step
799 sub finalize
{ 1 } # failure means show step
800 sub post_print
{ 0 } # success indicates we handled step (don't continue loop)
801 sub post_step
{ 0 } # success indicates we handled step (don't continue step or loop)
804 my ($self, $step) = @_;
810 my $step = shift || '';
811 my $cur = ref $self; # default to using self as the base for morphed modules
812 my $new = $cur .'::'. $step;
813 $new =~ s/(\b|_+)(\w)/\u$2/g; # turn Foo::my_step_name into Foo::MyStepName
819 my $step = shift || '';
821 return $self->{'name_module'} ||= do {
822 # allow for cgi-bin/foo or cgi-bin/foo.pl to resolve to "foo"
823 my $script = $ENV{'SCRIPT_NAME'} || $0;
824 $script =~ m/ (\w+) (?:\.\w+)? $/x || die "Couldn't determine module name from \"name_module\" lookup ($step)";
825 $1; # return of the do
833 my $base_dir = $self->base_dir_rel;
834 my $module = $self->run_hook('name_module', $step);
835 my $_step = $self->run_hook('name_step', $step) || die "Missing name_step";
836 $_step .= '.'. $self->ext_print if $_step !~ /\.\w+$/;
838 foreach ($base_dir, $module) { $_ .= '/' if length($_) && ! m
|/$| }
840 return $base_dir . $module . $_step;
847 my $abs = $self->base_dir_abs || return {};
848 my $base_dir = $self->base_dir_rel;
849 my $module = $self->run_hook('name_module', $step);
850 my $_step = $self->run_hook('name_step', $step);
851 $_step .= '.'. $self->ext_val if $_step !~ /\.\w+$/;
853 foreach ($abs, $base_dir, $module) { $_ .= '/' if length($_) && ! m
|/$| }
855 return $abs . $base_dir . $module . $_step;
859 my ($self, $step) = @_;
860 return 0 if ! $self->run_hook('ready_validate', $step);
861 return 0 if ! $self->run_hook('validate', $step, $self->form);
866 my ($self, $step) = @_;
867 return ($ENV{'REQUEST_METHOD'} && $ENV{'REQUEST_METHOD'} eq 'POST') ? 1 : 0;
870 sub set_ready_validate
{ # hook and method
872 my ($step, $is_ready) = (@_ == 2) ? @_ : (undef, shift);
873 $ENV{'REQUEST_METHOD'} = ($is_ready) ? 'POST' : 'GET';
878 my ($self, $step, $form) = @_;
880 my $hash = $self->run_hook('hash_validation', $step);
881 my $what_was_validated = [];
883 my $err_obj = eval { $self->vob->validate($form, $hash, $what_was_validated) };
884 die "Step $step: $@" if $@ && ! $err_obj;
886 ### had an error - store the errors and return false
888 $self->add_errors($err_obj->as_hash({
889 as_hash_join
=> "<br>\n",
890 as_hash_suffix
=> '_error',
895 ### allow for the validation to give us some redirection
896 foreach my $ref (@$what_was_validated) {
897 foreach my $method (qw(append_path replace_path insert_path)) {
898 next if ! (my $val = $ref->{$method});
899 $self->$method(ref $val ? @$val : $val);
906 ### creates javascript suitable for validating the form
910 return '' if $self->ext_val =~ /^html?$/; # let htm validation do it itself
912 my $form_name = shift || $self->run_hook('form_name', $step);
913 my $hash_val = shift || $self->run_hook('hash_validation', $step);
914 my $js_uri = $self->js_uri_path;
915 return '' if UNIVERSAL
::isa
($hash_val, 'HASH') && ! scalar keys %$hash_val
916 || UNIVERSAL
::isa
($hash_val, 'ARRAY') && ! @$hash_val;
918 return $self->vob->generate_js($hash_val, $form_name, $js_uri);
921 sub form_name
{ 'theform' }
923 sub hash_validation
{
924 my ($self, $step) = @_;
926 return $self->{'hash_validation'}->{$step} ||= do {
928 my $file = $self->run_hook('file_val', $step);
930 ### allow for returning the validation hash in the filename
931 ### a scalar ref means it is a yaml document to be read by get_validation
932 if (ref($file) && ! UNIVERSAL
::isa
($file, 'SCALAR')) {
935 ### read the file - if it is not found, errors will be in the webserver logs (all else dies)
937 $hash = $self->vob->get_validation($file) || {};
943 $hash; # return of the do
948 my ($self, $step) = @_;
950 return $self->{'hash_base'} ||= do {
951 ### create a weak copy of self to use in closures
953 if (eval {require Scalar
::Util
} && defined &Scalar
::Util
::weaken
) {
955 Scalar
::Util
::weaken
($copy);
957 $copy = bless {%$self}, ref($self); # hackish way to avoid circular refs on older perls (pre 5.8)
961 script_name
=> $ENV{'SCRIPT_NAME'} || $0,
962 path_info
=> $ENV{'PATH_INFO'} || '',
963 js_validation
=> sub { $copy->run_hook('js_validation', $step, shift) },
964 form_name
=> sub { $copy->run_hook('form_name', $step) },
965 $self->step_key => $step,
966 }; # return of the do
970 sub hash_common
{ shift-
>{'hash_common'} ||= {} }
971 sub hash_form
{ shift-
>form }
972 sub hash_fill
{ shift-
>{'hash_fill'} ||= {} }
973 sub hash_swap
{ shift-
>{'hash_swap'} ||= {} }
974 sub hash_errors
{ shift-
>{'hash_errors'} ||= {} }
976 ###----------------------------------------------------------------###
977 ### routines to support the base hooks
981 my $hash = $self->hash_errors;
982 my $args = ref($_[0]) ? shift : {@_};
983 foreach my $key (keys %$args) {
984 my $_key = ($key =~ /error$/) ? $key : "${key}_error";
985 if ($hash->{$_key}) {
986 $hash->{$_key} .= '<br>' . $args->{$key};
988 $hash->{$_key} = $args->{$key};
991 $hash->{'has_errors'} = 1;
994 sub has_errors
{ scalar keys %{ shift-
>hash_errors } }
997 my ($self, $error) = @_;
1001 sub add_to_errors
{ shift-
>add_errors(@_) }
1002 sub add_to_swap
{ my $self = shift; $self->add_to_hash($self->hash_swap, @_) }
1003 sub add_to_fill
{ my $self = shift; $self->add_to_hash($self->hash_fill, @_) }
1004 sub add_to_form
{ my $self = shift; $self->add_to_hash($self->hash_form, @_) }
1005 sub add_to_common
{ my $self = shift; $self->add_to_hash($self->hash_common, @_) }
1006 sub add_to_base
{ my $self = shift; $self->add_to_hash($self->hash_base, @_) }
1012 $new = {$new, @_} if ! ref $new; # non-hashref
1013 $old->{$_} = $new->{$_} foreach keys %$new;
1019 $self->{'base_dir_rel'} = shift if $#_ != -1;
1020 return $self->{'base_dir_rel'} || '';
1025 $self->{'base_dir_abs'} = shift if $#_ != -1;
1026 return $self->{'base_dir_abs'} || '';
1031 $self->{'ext_val'} = shift if $#_ != -1;
1032 return $self->{'ext_val'} || 'val';
1037 $self->{'ext_print'} = shift if $#_ != -1;
1038 return $self->{'ext_print'} || 'html';
1041 ### where to find the javascript files
1042 ### default to using this script as a handler
1045 my $script = $ENV{'SCRIPT_NAME'} || return '';
1046 my $js_step = $self->js_step;
1047 return ($self->can('path') == \
&CGI
::Ex
::App
::path
)
1048 ? $script .'/'. $js_step # try to use a cache friendly URI (if path is our own)
1049 : $script . '?'.$self->step_key.'='.$js_step.'&js='; # use one that works with more paths
1052 ###----------------------------------------------------------------###
1053 ### a simple step that allows for printing javascript libraries that
1054 ### are stored in perls @INC. Which ever step is in js_step should do something similar.
1059 ### make sure path info looks like /js/CGI/Ex/foo.js
1060 my $file = $self->form->{'js'} || $ENV{'PATH_INFO'} || '';
1061 $file = ($file =~ m!^(?:/js/|/)?(\w+(?:/\w+)*\.js)$!) ? $1 : '';
1063 $self->cgix->print_js($file);
1064 $self->{'_no_post_navigate'} = 1;
1068 ###----------------------------------------------------------------###
1069 ### a step that will be used if a valid_steps is defined
1070 ### and the current step of the path is not in valid_steps
1071 ### or if the step is a "hidden" step that begins with _
1072 ### or if the step name contains \W
1074 sub __forbidden_info_complete
{ 0 }
1076 sub __forbidden_hash_swap
{ {forbidden_step
=> shift-
>stash->{'forbidden_step'}} }
1078 sub __forbidden_file_print
{ \
"<h1>Denied</h1>You do not have access to the step <b>\"[% forbidden_step %]\"</b>" }
1080 ###----------------------------------------------------------------###
1084 ### See the perldoc in CGI/Ex/App.pod