lib/TWiki/UI/Oops.pm
changeset 0 414e01d06fd5
child 1 e2915a7cbdfa
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/lib/TWiki/UI/Oops.pm	Sat Jan 26 15:50:53 2008 +0100
     1.3 @@ -0,0 +1,139 @@
     1.4 +# Module of TWiki Enterprise Collaboration Platform, http://TWiki.org/
     1.5 +#
     1.6 +# Copyright (C) 1999-2007 Peter Thoeny, peter@thoeny.org
     1.7 +# and TWiki Contributors. All Rights Reserved. TWiki Contributors
     1.8 +# are listed in the AUTHORS file in the root of this distribution.
     1.9 +# NOTE: Please extend that file, not this notice.
    1.10 +#
    1.11 +# This program is free software; you can redistribute it and/or
    1.12 +# modify it under the terms of the GNU General Public License
    1.13 +# as published by the Free Software Foundation; either version 2
    1.14 +# of the License, or (at your option) any later version. For
    1.15 +# more details read LICENSE in the root of this distribution.
    1.16 +#
    1.17 +# This program is distributed in the hope that it will be useful,
    1.18 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.19 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    1.20 +#
    1.21 +# As per the GPL, removal of this notice is prohibited.
    1.22 +
    1.23 +=begin twiki
    1.24 +
    1.25 +---+ package TWiki::UI::Oops
    1.26 +
    1.27 +UI delegate for oops function
    1.28 +
    1.29 +=cut
    1.30 +
    1.31 +package TWiki::UI::Oops;
    1.32 +
    1.33 +use strict;
    1.34 +use Assert;
    1.35 +
    1.36 +require TWiki;
    1.37 +
    1.38 +=pod
    1.39 +
    1.40 +---++ StaticMethod oops_cgi($session)
    1.41 +
    1.42 +=oops= command handler.
    1.43 +This method is designed to be invoked via the =UI::run= method.
    1.44 +CGI parameters:
    1.45 +| =template= | name of template to use |
    1.46 +| =paramN= | Parameter for expansion of template |
    1.47 +%PARAMn% tags will be expanded in the template using the 'paramN'
    1.48 +values in the query.
    1.49 +
    1.50 +=cut
    1.51 +
    1.52 +sub oops_cgi {
    1.53 +    my $session = shift;
    1.54 +    my $topic = $session->{topicName};
    1.55 +    my $web = $session->{webName};
    1.56 +    my $query = $session->{cgiQuery};
    1.57 +
    1.58 +    oops( $session, $web, $topic, $query, 0 );
    1.59 +}
    1.60 +
    1.61 +=pod
    1.62 +
    1.63 +---++ StaticMethod oops($session, $web, $topic, $query, $keep)
    1.64 +
    1.65 +The body of an oops script call, abstracted out so it can be called for
    1.66 +the case where an oops is required, but all the parameters in the query
    1.67 +must be saved for passing on to another URL invoked from a form in
    1.68 +the template. If $keep is defined, it must be a reference to a hash
    1.69 +(usually an oopsexception) that defines the parameters to the
    1.70 +script (template, def etc). In this case, all the parameters in
    1.71 +the =$query= are added as hiddens into the expanded template.
    1.72 +
    1.73 +=cut
    1.74 +
    1.75 +sub oops {
    1.76 +    my( $session, $web, $topic, $query, $keep ) = @_;
    1.77 +
    1.78 +    my $tmplName;
    1.79 +    my $def;
    1.80 +    my @params;
    1.81 +    my $n = 1;
    1.82 +
    1.83 +    if( $keep ) {
    1.84 +        # Use oops parameters from the keep hash instead
    1.85 +        $tmplName = $keep->{template};
    1.86 +        $def = $keep->{def};
    1.87 +        if( ref($keep->{params}) eq 'ARRAY' ) {
    1.88 +            foreach my $p ( @{$keep->{params}} ) {
    1.89 +                push( @params, $p );
    1.90 +                $n++;
    1.91 +            }
    1.92 +        } elsif( defined $keep->{params} ) {
    1.93 +            push( @params, $keep->{params} );
    1.94 +        }
    1.95 +    } else {
    1.96 +        $tmplName = $query->param( 'template' );
    1.97 +        $def = $query->param( 'def' );
    1.98 +        while( defined( my $param = $query->param( 'param'.$n ) )) {
    1.99 +            push( @params, $param );
   1.100 +            $n++;
   1.101 +        }
   1.102 +    }
   1.103 +    $tmplName ||= 'oops';
   1.104 +
   1.105 +    # Do not pass on the template parameter otherwise continuation won't work
   1.106 +    $query->delete( 'template' );
   1.107 +
   1.108 +    my $skin = $session->getSkin();
   1.109 +
   1.110 +    my $tmplData = $session->templates->readTemplate( $tmplName, $skin );
   1.111 +
   1.112 +    if( ! $tmplData ) {
   1.113 +        $tmplData = CGI::start_html()
   1.114 +          . CGI::h1('TWiki Installation Error')
   1.115 +            . 'Template "'.$tmplName.'" not found.'.CGI::p()
   1.116 +              . 'Check the configuration setting for {TemplateDir}.'
   1.117 +                .CGI::end_html();
   1.118 +    } else {
   1.119 +        if( defined $def ) {
   1.120 +            # if a def is specified, instantiate that def
   1.121 +            my $blah = $session->templates->expandTemplate( $def );
   1.122 +            $tmplData =~ s/%INSTANTIATE%/$blah/;
   1.123 +        }
   1.124 +        $tmplData = $session->handleCommonTags( $tmplData, $web, $topic );
   1.125 +        $n = 1;
   1.126 +        foreach my $param ( @params ) {
   1.127 +            # Entity-encode, to block any potential HTML payload
   1.128 +            $param = TWiki::entityEncode( $param );
   1.129 +            $tmplData =~ s/%PARAM$n%/$param/g;
   1.130 +            $n++;
   1.131 +        }
   1.132 +        $tmplData =~ s/%(PARAM\d+)%/
   1.133 +          CGI::span({class=>'twikiAlert'},"MISSING $1 ")/ge if DEBUG;
   1.134 +        $tmplData = $session->handleCommonTags( $tmplData, $web, $topic );
   1.135 +        $tmplData = $session->renderer->getRenderedVersion( $tmplData, $web,
   1.136 +                                                              $topic );
   1.137 +    }
   1.138 +
   1.139 +    $session->writeCompletePage( $tmplData );
   1.140 +}
   1.141 +
   1.142 +1;