| 1 | package CGI::XMLApplication; | ||
| 2 | |||
| 3 | # ################################################################ | ||
| 4 | # | ||
| 5 | # (c) 2001 Christian Glahn <phish@CPAN.org> | ||
| 6 | # | ||
| 7 | # This code is free software; you can redistribute it and/or | ||
| 8 | # modify it under the same terms as Perl itself. | ||
| 9 | # | ||
| 10 | # ################################################################ | ||
| 11 | |||
| 12 | ## | ||
| 13 | # CGI::XMLApplication - Application Module for CGI scripts | ||
| 14 | |||
| 15 | # ################################################################ | ||
| 16 | # module loading and global variable initializing | ||
| 17 | # ################################################################ | ||
| 18 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 19 | |||
| 20 | 0 | ▶ | use CGI; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 21 | use Carp; | ||
| 22 | #use Data::Dumper; | ||
| 23 | |||
| 24 | # ################################################################ | ||
| 25 | # inheritance | ||
| 26 | # ################################################################ | ||
| 27 | @CGI::XMLApplication::ISA = qw( CGI ); | ||
| 28 | |||
| 29 | # ################################################################ | ||
| 30 | |||
| 31 | $CGI::XMLApplication::VERSION = "1.1.5"; | ||
| 32 | |||
| 33 | # ################################################################ | ||
| 34 | # general configuration | ||
| 35 | # ################################################################ | ||
| 36 | |||
| 37 | # some hardcoded error messages, the application has always, e.g. | ||
| 38 | # to tell that a stylesheet is missing | ||
| 39 | @CGI::XMLApplication::panic = ( | ||
| 40 | 'No Stylesheet specified! ', | ||
| 41 | 'Stylesheet is not available! ', | ||
| 42 | 'Event not implemented', | ||
| 43 | 'Application Error', | ||
| 44 | ); | ||
| 45 | |||
| 46 | # The Debug Level for verbose error messages | ||
| 47 | $CGI::XMLApplication::DEBUG = 0; | ||
| 48 | |||
| 49 | # ################################################################ | ||
| 50 | # methods | ||
| 51 | # ################################################################ | ||
| 52 | sub new { | ||
| 53 | my $class = shift; | ||
| 54 | my $self = $class->SUPER::new( @_ ); | ||
| 55 | bless $self, $class; | ||
| 56 | |||
| 57 | $self->{XML_CGIAPP_HANDLER_} = [$self->registerEvents()]; | ||
| 58 | $self->{XML_CGIAPP_STYLESHEET_} = []; | ||
| 59 | $self->{XML_CGIAPP_STYLESDIR_} = ''; | ||
| 60 | |||
| 61 | return $self; | ||
| 62 | } | ||
| 63 | |||
| 64 | # ################################################################ | ||
| 65 | # straight forward coded methods | ||
| 66 | |||
| 67 | # application related ############################################ | ||
| 68 | # both functions are only for backward compatibilty with older scripts | ||
| 69 | sub debug_msg { | ||
| 70 | my $level = shift; | ||
| 71 | if ( $level <= $CGI::XMLApplication::DEBUG && scalar @_ ) { | ||
| 72 | my ($module, undef, $line) = caller(1); | ||
| 73 | warn "[$module; line: $line] ", join(' ', @_) , "\n"; | ||
| 74 | } | ||
| 75 | } | ||
| 76 | |||
| 77 | ## | ||
| 78 | # dummy functions | ||
| 79 | # | ||
| 80 | # each function is required to be overwritten by any class inheritated | ||
| 81 | sub registerEvents { return (); } | ||
| 82 | |||
| 83 | # all following function will recieve the context, too | ||
| 84 | sub getDOM { return undef; } | ||
| 85 | sub requestDOM { return undef; } # old style use getDOM! | ||
| 86 | |||
| 87 | sub getStylesheetString { return ""; } # return a XSL String | ||
| 88 | sub getStylesheet { return ""; } # returns either name of a stylesheetfile or the xsl DOM | ||
| 89 | sub selectStylesheet { return ""; } # old style getStylesheet | ||
| 90 | |||
| 91 | sub getXSLParameter { return (); } # should return a plain hash of parameters passed to xsl | ||
| 92 | sub setHttpHeader { return (); } # should return a hash of header | ||
| 93 | |||
| 94 | sub skipSerialization{ | ||
| 95 | my $self = shift; | ||
| 96 | $self->{CGI_XMLAPP_SKIP_TRANSFORM} = shift if scalar @_; | ||
| 97 | return $self->{CGI_XMLAPP_SKIP_TRANSFORM}; | ||
| 98 | } | ||
| 99 | |||
| 100 | # returns boolean | ||
| 101 | sub passthru { | ||
| 102 | my $self = shift; | ||
| 103 | if ( scalar @_ ) { | ||
| 104 | $self->{CGI_XMLAPP_PASSXML} = shift; | ||
| 105 | $self->delete( 'passthru' ); # delete any passthru parameter | ||
| 106 | } | ||
| 107 | elsif ( defined $self->param( "passthru" ) ) { | ||
| 108 | $self->{CGI_XMLAPP_PASSXML} = 1 ; | ||
| 109 | $self->delete( 'passthru' ); | ||
| 110 | } | ||
| 111 | return $self->{CGI_XMLAPP_PASSXML}; | ||
| 112 | } | ||
| 113 | |||
| 114 | sub redirectToURI { | ||
| 115 | my $self = shift; | ||
| 116 | $self->{CGI_XMLAPP_REDIRECT} = shift if scalar @_; | ||
| 117 | return $self->{CGI_XMLAPP_REDIRECT}; | ||
| 118 | } | ||
| 119 | |||
| 120 | # ################################################################ | ||
| 121 | # content related functions | ||
| 122 | |||
| 123 | # stylesheet directory information ############################### | ||
| 124 | sub setStylesheetDir { $_[0]->{XML_CGIAPP_STYLESDIR_} = $_[1];} | ||
| 125 | sub setStylesheetPath { $_[0]->{XML_CGIAPP_STYLESDIR_} = $_[1];} | ||
| 126 | sub getStylesheetDir { $_[0]->{XML_CGIAPP_STYLESDIR_}; } | ||
| 127 | sub getStylesheetPath { $_[0]->{XML_CGIAPP_STYLESDIR_}; } | ||
| 128 | |||
| 129 | # event control ################################################### | ||
| 130 | |||
| 131 | sub addEvent { my $s=shift; push @{$s->{XML_CGIAPP_HANDLER_}}, @_;} | ||
| 132 | |||
| 133 | sub getEventList { @{ $_[0]->{XML_CGIAPP_HANDLER_} }; } | ||
| 134 | sub testEvent { return $_[0]->checkPush( $_[0]->getEventList() ); } | ||
| 135 | |||
| 136 | sub deleteEvent { | ||
| 137 | my $self = shift; | ||
| 138 | if ( scalar @_ ){ # delete explicit events | ||
| 139 | foreach ( @_ ) { | ||
| 140 | debug_msg( 8, "[XML::CGIApplication] delete event $_" ); | ||
| 141 | $self->delete( $_ ); | ||
| 142 | $self->delete( $_.'.x' ); | ||
| 143 | $self->delete( $_.'.y' ); | ||
| 144 | } | ||
| 145 | } | ||
| 146 | else { # delete all | ||
| 147 | foreach ( @{ $self->{XML_CGIAPP_HANDLER_} } ){ | ||
| 148 | debug_msg( 8, "delete event $_" ); | ||
| 149 | $self->delete( $_ ); | ||
| 150 | $self->delete( $_.'.x' ); | ||
| 151 | $self->delete( $_.'.y' ); | ||
| 152 | } | ||
| 153 | } | ||
| 154 | } | ||
| 155 | |||
| 156 | sub sendEvent { | ||
| 157 | debug_msg( 10, "send event " . $_[1] ); | ||
| 158 | $_[0]->deleteEvent(); | ||
| 159 | $_[0]->param( -name=>$_[1] , -value=>1 ); | ||
| 160 | } | ||
| 161 | |||
| 162 | # error handling ################################################# | ||
| 163 | # for internal use only ... | ||
| 164 | sub setPanicMsg { $_[0]->{XML_CGIAPP_PANIC_} = $_[1] } | ||
| 165 | sub getPanicMsg { $_[0]->{XML_CGIAPP_PANIC_} } | ||
| 166 | |||
| 167 | # ################################################################ | ||
| 168 | # predefined events | ||
| 169 | |||
| 170 | # default event handler prototypes | ||
| 171 | sub event_init {} | ||
| 172 | sub event_exit {} | ||
| 173 | sub event_default { return 0 } | ||
| 174 | |||
| 175 | # ################################################################ | ||
| 176 | # CGI specific helper functions | ||
| 177 | |||
| 178 | # this is required by the eventhandling | ||
| 179 | sub checkPush { | ||
| 180 | my $self = shift; | ||
| 181 | my ( $pushed ) = grep { | ||
| 182 | defined $self->param( $_ ) || defined $self->param( $_.'.x') | ||
| 183 | } @_; | ||
| 184 | $pushed =~ s/\.x$//i if defined $pushed; | ||
| 185 | return $pushed; | ||
| 186 | } | ||
| 187 | |||
| 188 | # helper functions which were missing in CGI.pm | ||
| 189 | sub checkFields{ | ||
| 190 | my $self = shift; | ||
| 191 | my @missing = grep { | ||
| 192 | not length $self->param( $_ ) || $self->param( $_ ) =~ /^\s*$/ | ||
| 193 | } @_; | ||
| 194 | return wantarray ? @missing : ( scalar(@missing) > 0 ? undef : 1 ); | ||
| 195 | } | ||
| 196 | |||
| 197 | sub getParamHash { | ||
| 198 | my $self = shift; | ||
| 199 | my $ptrHash = $self->Vars; | ||
| 200 | my $ptrRV = {}; | ||
| 201 | |||
| 202 | foreach my $k ( keys( %{$ptrHash} ) ){ | ||
| 203 | next unless exists $ptrHash->{$_} && $ptrHash->{$_} !~ /^[\s\0]*$/; | ||
| 204 | $ptrRV->{$k} = $ptrHash->{$k}; | ||
| 205 | } | ||
| 206 | |||
| 207 | return wantarray ? %{$ptrRV} : $ptrRV; | ||
| 208 | } | ||
| 209 | |||
| 210 | # ################################################################ | ||
| 211 | # application related methods | ||
| 212 | # ################################################################ | ||
| 213 | # algorithm should be | ||
| 214 | # event registration | ||
| 215 | # app init | ||
| 216 | # event handling | ||
| 217 | # app exit | ||
| 218 | # serialization and output | ||
| 219 | # error handling | ||
| 220 | sub run { | ||
| 221 | my $self = shift; | ||
| 222 | my $sid = -1; | ||
| 223 | my $ctxt = (!@_ or scalar(@_) > 1) ? {@_} : shift; # nothing, hash or context object | ||
| 224 | |||
| 225 | $self->event_init($ctxt); | ||
| 226 | |||
| 227 | if ( my $n = $self->testEvent($ctxt) ) { | ||
| 228 | if ( my $func = $self->can('event_'.$n) ) { | ||
| 229 | $sid = $self->$func($ctxt); | ||
| 230 | } | ||
| 231 | else { | ||
| 232 | $sid = -3; | ||
| 233 | } | ||
| 234 | } | ||
| 235 | |||
| 236 | if ( $sid == -1 ){ | ||
| 237 | $sid = $self->event_default($ctxt); | ||
| 238 | } | ||
| 239 | |||
| 240 | $self->event_exit($ctxt); | ||
| 241 | |||
| 242 | # if we allready panic, don't try to render | ||
| 243 | if ( $sid >= 0 ) { | ||
| 244 | # check if we wanna redirect | ||
| 245 | if ( my $uri = $self->redirectToURI() ) { | ||
| 246 | my %h = $self->setHttpHeader( $ctxt ); | ||
| 247 | $h{-uri} = $uri; | ||
| 248 | print $self->SUPER::redirect( %h ) . "\n\n"; | ||
| 249 | } | ||
| 250 | elsif ( not $self->skipSerialization() ) { | ||
| 251 | # sometimes it is nessecary to skip the serialization | ||
| 252 | # eg. due passing binary data. | ||
| 253 | $sid = $self->serialization( $ctxt ); | ||
| 254 | } | ||
| 255 | } | ||
| 256 | |||
| 257 | $self->panic( $sid, $ctxt ); | ||
| 258 | } | ||
| 259 | |||
| 260 | sub serialization { | ||
| 261 | # i require both modules here, so one can implement his own | ||
| 262 | # serialization | ||
| 263 | require XML::LibXML; | ||
| 264 | require XML::LibXSLT; | ||
| 265 | |||
| 266 | my $self = shift; | ||
| 267 | my $ctxt = shift; | ||
| 268 | my $id; | ||
| 269 | |||
| 270 | my %header = $self->setHttpHeader( $ctxt ); | ||
| 271 | |||
| 272 | my $xml_doc = $self->getDOM( $ctxt ); | ||
| 273 | if ( not defined $xml_doc ) { | ||
| 274 | debug_msg( 10, "use old style interface"); | ||
| 275 | $xml_doc = $self->requestDOM( $ctxt ); | ||
| 276 | } | ||
| 277 | # if still no document is available | ||
| 278 | if ( not defined $xml_doc ) { | ||
| 279 | debug_msg( 10, "no DOM defined; use empty DOM" ); | ||
| 280 | $xml_doc = XML::LibXML::Document->new; | ||
| 281 | # the following line is to keep xpath.c quiet! | ||
| 282 | $xml_doc->setDocumentElement( $xml_doc->createElement( "dummy" ) ); | ||
| 283 | } | ||
| 284 | |||
| 285 | if( defined $self->passthru() && $self->passthru() == 1 ) { | ||
| 286 | # this is a useful feature for DOM debugging | ||
| 287 | debug_msg( 10, "attempt to pass the DOM to the client" ); | ||
| 288 | $header{-type} = 'text/xml'; | ||
| 289 | print $self->header( %header ); | ||
| 290 | print $xml_doc->toString(); | ||
| 291 | return 0; | ||
| 292 | } | ||
| 293 | |||
| 294 | my $stylesheet = $self->getStylesheet( $ctxt ); | ||
| 295 | |||
| 296 | my ( $xsl_dom, $style, $res ); | ||
| 297 | my $parser = XML::LibXML->new(); | ||
| 298 | my $xslt = XML::LibXSLT->new(); | ||
| 299 | |||
| 300 | if ( ref( $stylesheet ) ) { | ||
| 301 | debug_msg( 5, "stylesheet is reference" ); | ||
| 302 | $xsl_dom = $stylesheet; | ||
| 303 | } | ||
| 304 | elsif ( -f $stylesheet && -r $stylesheet ) { | ||
| 305 | debug_msg( 5, "filename is $stylesheet" ); | ||
| 306 | eval { | ||
| 307 | $xsl_dom = $parser->parse_file( $stylesheet ); | ||
| 308 | }; | ||
| 309 | if ( $@ ) { | ||
| 310 | debug_msg( 3, "Corrupted Stylesheet:\n broken XML\n". $@ ); | ||
| 311 | $self->setPanicMsg( "Corrupted document:\n broken XML\n". $@ ); | ||
| 312 | return -2; | ||
| 313 | } | ||
| 314 | } | ||
| 315 | else { | ||
| 316 | # first test the new style interface | ||
| 317 | my $xslstring = $self->getStylesheetString( $ctxt ); | ||
| 318 | if ( length $xslstring ) { | ||
| 319 | debug_msg( 5, "stylesheet is xml string" ); | ||
| 320 | eval { $xsl_dom = $parser->parse_string( $xslstring ); }; | ||
| 321 | if ( $@ || not defined $xsl_dom ) { | ||
| 322 | # the parse failed !!! | ||
| 323 | debug_msg( 3, "Corrupted Stylesheet String:\n". $@ ."\n" ); | ||
| 324 | $self->setPanicMsg( "Corrupted Stylesheet String:\n". $@ ); | ||
| 325 | return -2; | ||
| 326 | } | ||
| 327 | } | ||
| 328 | else { | ||
| 329 | # now test old style interface | ||
| 330 | # will be removed with the next major release | ||
| 331 | |||
| 332 | debug_msg( 5, "old style interface to select the stylesheet" ); | ||
| 333 | $stylesheet = $self->selectStylesheet( $ctxt ); | ||
| 334 | if ( ref( $stylesheet ) ) { | ||
| 335 | debug_msg( 5, "stylesheet is reference" ); | ||
| 336 | $xsl_dom = $stylesheet; | ||
| 337 | } | ||
| 338 | elsif ( -f $stylesheet && -r $stylesheet ) { | ||
| 339 | debug_msg( 5, "filename is $stylesheet" ); | ||
| 340 | eval { | ||
| 341 | $xsl_dom = $parser->parse_file( $stylesheet ); | ||
| 342 | }; | ||
| 343 | if ( $@ ) { | ||
| 344 | debug_msg( 3, "Corrupted Stylesheet:\n broken XML\n". $@ ); | ||
| 345 | $self->setPanicMsg( "Corrupted document:\n broken XML\n". $@ ); | ||
| 346 | return -2; | ||
| 347 | } | ||
| 348 | } | ||
| 349 | else { | ||
| 350 | debug_msg( 2 , "panic stylesheet file $stylesheet does not exist" ); | ||
| 351 | $self->setPanicMsg( "$stylesheet" ); | ||
| 352 | return length $stylesheet ? -2 : -1 ; | ||
| 353 | } | ||
| 354 | } | ||
| 355 | } | ||
| 356 | |||
| 357 | eval { | ||
| 358 | $style = $xslt->parse_stylesheet( $xsl_dom ); | ||
| 359 | # $style = $xslt->parse_stylesheet_file( $file ); | ||
| 360 | }; | ||
| 361 | if( $@ ) { | ||
| 362 | debug_msg( 3, "Corrupted Stylesheet:\n". $@ ."\n" ); | ||
| 363 | $self->setPanicMsg( "Corrupted Stylesheet:\n". $@ ); | ||
| 364 | return -2; | ||
| 365 | } | ||
| 366 | |||
| 367 | my %xslparam = $self->getXSLParameter( $ctxt ); | ||
| 368 | eval { | ||
| 369 | # first do special xpath encoding of the parameter | ||
| 370 | if ( %xslparam && scalar( keys %xslparam ) > 0 ) { | ||
| 371 | my @list; | ||
| 372 | foreach my $key ( keys %xslparam ) { | ||
| 373 | # check for multivalued parameters stored in a \0 separated string by CGI.pm :-/ | ||
| 374 | if ( $xslparam{$key} =~ /\0/ ) { | ||
| 375 | push @list, $key, (split("\0",$xslparam{$key}))[-1]; | ||
| 376 | } | ||
| 377 | else { | ||
| 378 | push @list, $key, $xslparam{$key}; | ||
| 379 | } | ||
| 380 | } | ||
| 381 | $res = $style->transform( $xml_doc, | ||
| 382 | XML::LibXSLT::xpath_to_string(@list) | ||
| 383 | ); | ||
| 384 | } | ||
| 385 | else { | ||
| 386 | $res = $style->transform( $xml_doc ); | ||
| 387 | } | ||
| 388 | }; | ||
| 389 | if( $@ ) { | ||
| 390 | debug_msg( 3, "Broken Transformation:\n". $@ ."\n" ); | ||
| 391 | $self->setPanicMsg( "Broken Transformation:\n". $@ ); | ||
| 392 | return -2; | ||
| 393 | } | ||
| 394 | |||
| 395 | # override content-type with the correct content-type | ||
| 396 | # of the style (is this ok?) | ||
| 397 | $header{-type} = $style->media_type; | ||
| 398 | $header{-charset} = $style->output_encoding; | ||
| 399 | |||
| 400 | debug_msg( 10, "serialization do output" ); | ||
| 401 | # we want nice xhtml and since the output_string does not the | ||
| 402 | # right job | ||
| 403 | my $out_string= undef; | ||
| 404 | |||
| 405 | debug_msg( 9, "serialization get output string" ); | ||
| 406 | eval { | ||
| 407 | $out_string = $style->output_string( $res ); | ||
| 408 | }; | ||
| 409 | debug_msg( 10, "serialization rendered output" ); | ||
| 410 | if ( $@ ) { | ||
| 411 | debug_msg( 3, "Corrupted Output:\n", $@ , "\n" ); | ||
| 412 | $self->setPanicMsg( "Corrupted Output:\n". $@ ); | ||
| 413 | return -2; | ||
| 414 | } | ||
| 415 | else { | ||
| 416 | # do the output | ||
| 417 | print $self->header( %header ); | ||
| 418 | print $out_string; | ||
| 419 | debug_msg( 10, "output printed" ); | ||
| 420 | } | ||
| 421 | |||
| 422 | return 0; | ||
| 423 | } | ||
| 424 | |||
| 425 | sub panic { | ||
| 426 | my ( $self, $pid ) = @_; | ||
| 427 | return unless $pid < 0; | ||
| 428 | $pid++; | ||
| 429 | $pid*=-1; | ||
| 430 | |||
| 431 | my $str = "Application Panic: "; | ||
| 432 | $str = "PANIC $pid :" . $CGI::XMLApplication::panic[$pid] ; | ||
| 433 | # this is nice for debugging from logfiles... | ||
| 434 | $str = $self->b( $str ) . "<br />\n"; | ||
| 435 | $str .= $self->pre( $self->getPanicMsg() ); | ||
| 436 | $str .= "Please Contact the Systemadminstrator<br />\n"; | ||
| 437 | |||
| 438 | debug_msg( 1, "$str" ); | ||
| 439 | |||
| 440 | if ( $CGI::XMLApplication::Quiet == 1 ) { | ||
| 441 | $str = "Application Panic"; | ||
| 442 | } | ||
| 443 | if ( $CGI::XMLApplication::Quiet == 2 ) { | ||
| 444 | $str = ""; | ||
| 445 | } | ||
| 446 | |||
| 447 | my $status = $pid < 3 ? 404 : 500; # default is the application error ... | ||
| 448 | print $self->header( -status => $status ) , $str ,"\n"; | ||
| 449 | |||
| 450 | } | ||
| 451 | |||
| 452 | 1; | ||
| 453 | # ################################################################ | ||
| 454 | __END__ | ||