lib/TWiki/UI/Oops.pm
author Colas Nahaboo <colas@nahaboo.net>
Sat, 26 Jan 2008 15:50:53 +0100
changeset 0 414e01d06fd5
child 1 e2915a7cbdfa
permissions -rw-r--r--
RELEASE 4.2.0 freetown
     1 # Module of TWiki Enterprise Collaboration Platform, http://TWiki.org/
     2 #
     3 # Copyright (C) 1999-2007 Peter Thoeny, peter@thoeny.org
     4 # and TWiki Contributors. All Rights Reserved. TWiki Contributors
     5 # are listed in the AUTHORS file in the root of this distribution.
     6 # NOTE: Please extend that file, not this notice.
     7 #
     8 # This program is free software; you can redistribute it and/or
     9 # modify it under the terms of the GNU General Public License
    10 # as published by the Free Software Foundation; either version 2
    11 # of the License, or (at your option) any later version. For
    12 # more details read LICENSE in the root of this distribution.
    13 #
    14 # This program is distributed in the hope that it will be useful,
    15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
    16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    17 #
    18 # As per the GPL, removal of this notice is prohibited.
    19 
    20 =begin twiki
    21 
    22 ---+ package TWiki::UI::Oops
    23 
    24 UI delegate for oops function
    25 
    26 =cut
    27 
    28 package TWiki::UI::Oops;
    29 
    30 use strict;
    31 use Assert;
    32 
    33 require TWiki;
    34 
    35 =pod
    36 
    37 ---++ StaticMethod oops_cgi($session)
    38 
    39 =oops= command handler.
    40 This method is designed to be invoked via the =UI::run= method.
    41 CGI parameters:
    42 | =template= | name of template to use |
    43 | =paramN= | Parameter for expansion of template |
    44 %PARAMn% tags will be expanded in the template using the 'paramN'
    45 values in the query.
    46 
    47 =cut
    48 
    49 sub oops_cgi {
    50     my $session = shift;
    51     my $topic = $session->{topicName};
    52     my $web = $session->{webName};
    53     my $query = $session->{cgiQuery};
    54 
    55     oops( $session, $web, $topic, $query, 0 );
    56 }
    57 
    58 =pod
    59 
    60 ---++ StaticMethod oops($session, $web, $topic, $query, $keep)
    61 
    62 The body of an oops script call, abstracted out so it can be called for
    63 the case where an oops is required, but all the parameters in the query
    64 must be saved for passing on to another URL invoked from a form in
    65 the template. If $keep is defined, it must be a reference to a hash
    66 (usually an oopsexception) that defines the parameters to the
    67 script (template, def etc). In this case, all the parameters in
    68 the =$query= are added as hiddens into the expanded template.
    69 
    70 =cut
    71 
    72 sub oops {
    73     my( $session, $web, $topic, $query, $keep ) = @_;
    74 
    75     my $tmplName;
    76     my $def;
    77     my @params;
    78     my $n = 1;
    79 
    80     if( $keep ) {
    81         # Use oops parameters from the keep hash instead
    82         $tmplName = $keep->{template};
    83         $def = $keep->{def};
    84         if( ref($keep->{params}) eq 'ARRAY' ) {
    85             foreach my $p ( @{$keep->{params}} ) {
    86                 push( @params, $p );
    87                 $n++;
    88             }
    89         } elsif( defined $keep->{params} ) {
    90             push( @params, $keep->{params} );
    91         }
    92     } else {
    93         $tmplName = $query->param( 'template' );
    94         $def = $query->param( 'def' );
    95         while( defined( my $param = $query->param( 'param'.$n ) )) {
    96             push( @params, $param );
    97             $n++;
    98         }
    99     }
   100     $tmplName ||= 'oops';
   101 
   102     # Do not pass on the template parameter otherwise continuation won't work
   103     $query->delete( 'template' );
   104 
   105     my $skin = $session->getSkin();
   106 
   107     my $tmplData = $session->templates->readTemplate( $tmplName, $skin );
   108 
   109     if( ! $tmplData ) {
   110         $tmplData = CGI::start_html()
   111           . CGI::h1('TWiki Installation Error')
   112             . 'Template "'.$tmplName.'" not found.'.CGI::p()
   113               . 'Check the configuration setting for {TemplateDir}.'
   114                 .CGI::end_html();
   115     } else {
   116         if( defined $def ) {
   117             # if a def is specified, instantiate that def
   118             my $blah = $session->templates->expandTemplate( $def );
   119             $tmplData =~ s/%INSTANTIATE%/$blah/;
   120         }
   121         $tmplData = $session->handleCommonTags( $tmplData, $web, $topic );
   122         $n = 1;
   123         foreach my $param ( @params ) {
   124             # Entity-encode, to block any potential HTML payload
   125             $param = TWiki::entityEncode( $param );
   126             $tmplData =~ s/%PARAM$n%/$param/g;
   127             $n++;
   128         }
   129         $tmplData =~ s/%(PARAM\d+)%/
   130           CGI::span({class=>'twikiAlert'},"MISSING $1 ")/ge if DEBUG;
   131         $tmplData = $session->handleCommonTags( $tmplData, $web, $topic );
   132         $tmplData = $session->renderer->getRenderedVersion( $tmplData, $web,
   133                                                               $topic );
   134     }
   135 
   136     $session->writeCompletePage( $tmplData );
   137 }
   138 
   139 1;