import rt 3.8.10
[freeside.git] / rt / bin / mason_handler.fcgi
index e8a4e12..996e960 100755 (executable)
-#!!!PERL!!
-# $Header: /home/cvs/cvsroot/freeside/rt/bin/mason_handler.fcgi,v 1.1 2002-08-12 06:17:07 ivan Exp $
-# RT is (c) 1996-2001 Jesse Vincent (jesse@fsck.com);
-
+#!/usr/bin/perl
+# BEGIN BPS TAGGED BLOCK {{{
+#
+# COPYRIGHT:
+#
+# This software is Copyright (c) 1996-2011 Best Practical Solutions, LLC
+#                                          <sales@bestpractical.com>
+#
+# (Except where explicitly superseded by other copyright notices)
+#
+#
+# LICENSE:
+#
+# This work is made available to you under the terms of Version 2 of
+# the GNU General Public License. A copy of that license should have
+# been provided with this software, but in any event can be snarfed
+# from www.gnu.org.
+#
+# This work is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+# 02110-1301 or visit their web page on the internet at
+# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
+#
+#
+# CONTRIBUTION SUBMISSION POLICY:
+#
+# (The following paragraph is not intended to limit the rights granted
+# to you to modify and distribute this software under the terms of
+# the GNU General Public License and is only of importance to you if
+# you choose to contribute your changes and enhancements to the
+# community by submitting them to Best Practical Solutions, LLC.)
+#
+# By intentionally submitting any modifications, corrections or
+# derivatives to this work, or any other work intended for use with
+# Request Tracker, to Best Practical Solutions, LLC, you confirm that
+# you are the copyright holder for those contributions and you grant
+# Best Practical Solutions,  LLC a nonexclusive, worldwide, irrevocable,
+# royalty-free, perpetual, license to use, copy, create derivative
+# works based on those contributions, and sublicense and distribute
+# those contributions and any derivatives thereof.
+#
+# END BPS TAGGED BLOCK }}}
 use strict;
-$ENV{'PATH'} = '/bin:/usr/bin';    # or whatever you need
-$ENV{'CDPATH'} = '' if defined $ENV{'CDPATH'};
-$ENV{'SHELL'} = '/bin/sh' if defined $ENV{'SHELL'};
-$ENV{'ENV'} = '' if defined $ENV{'ENV'};
-$ENV{'IFS'} = ''          if defined $ENV{'IFS'};
-
-
-# We really don't want apache to try to eat all vm
-# see http://perl.apache.org/guide/control.html#Preventing_mod_perl_Processes_Fr
-
-
-package RT::Mason;
-#use CGI qw(-private_tempfiles);   # pull in CGI with the private tempfiles
-                                 #option predefined
-use HTML::Mason;  # brings in subpackages: Parser, Interp, etc.
-
-use vars qw($VERSION %session $Nobody $SystemUser $cgi);
-
-# List of modules that you want to use from components (see Admin
-# manual for details)
+use warnings;
+no warnings qw(once);
 
-#Clean up our umask...so that the session files aren't world readable, writable or executable
-umask(0077);
+use File::Basename;
+require (dirname(__FILE__) .'/webmux.pl');
 
+# Enter CGI::Fast mode, which should also work as a vanilla CGI script.
+require CGI::Fast;
 
-  
-$VERSION="!!RT_VERSION!!";
+while ( my $cgi = CGI::Fast->new ) {
+    # the whole point of fastcgi requires the env to get reset here..
+    # So we must squash it again
+    $ENV{'PATH'}   = '/bin:/usr/bin';
+    $ENV{'CDPATH'} = '' if defined $ENV{'CDPATH'};
+    $ENV{'SHELL'}  = '/bin/sh' if defined $ENV{'SHELL'};
+    $ENV{'ENV'}    = '' if defined $ENV{'ENV'};
+    $ENV{'IFS'}    = '' if defined $ENV{'IFS'};
 
-use lib "!!RT_LIB_PATH!!";
-use lib "!!RT_ETC_PATH!!";
+    Module::Refresh->refresh if RT->Config->Get('DevelMode');
+    RT::ConnectToDatabase();
 
-#This drags in  RT's config.pm
-use config;
-use Carp;
+    # Each environment has its own way of handling .. and so on in paths,
+    # so RT consistently forbids such paths.
+    if ( $cgi->path_info =~ m{/\.} ) {
+        $RT::Logger->crit("Invalid request for ".$cgi->path_info." aborting");
+        print STDOUT "HTTP/1.0 400\r\n\r\n";
 
-{  
-    package HTML::Mason::Commands;
-    use vars qw(%session $ContentType);
-    
-    use RT; 
-    use RT::Ticket;
-    use RT::Tickets;
-    use RT::Transaction;
-    use RT::Transactions;
-    use RT::User;
-    use RT::Users;
-    use RT::CurrentUser;
-    use RT::Template;
-    use RT::Templates;
-    use RT::Queue;
-    use RT::Queues;
-    use RT::ScripAction;
-    use RT::ScripActions;
-    use RT::ScripCondition;
-    use RT::ScripConditions;
-    use RT::Scrip;
-    use RT::Scrips;
-    use RT::Group;
-    use RT::Groups;
-    use RT::Keyword;
-    use RT::Keywords;
-    use RT::ObjectKeyword;
-    use RT::ObjectKeywords;
-    use RT::KeywordSelect;
-    use RT::KeywordSelects;
-    use RT::GroupMember;
-    use RT::GroupMembers;
-    use RT::Watcher;
-    use RT::Watchers;
-    use RT::Handle;
-    use RT::Interface::Web;    
-    use MIME::Entity;
-    use CGI::Cookie;
-    use Date::Parse;
-    use HTML::Entities;
-    use Text::Wrapper;
-    #TODO: make this use DBI
-    use Apache::Session::File;
-    use CGI::Fast;
+        RT::Interface::Web::Handler->CleanupRequest();
 
-    # set the page's content type.
-    # In this case, just save it to a variable that we can pull later;
-    sub SetContentType {
-       $ContentType = shift;
+        next;
     }
-    sub CGIObject {
-       return $RT::Mason::cgi;
-    }
-}
-
 
-my ($output, $parser, $interp);
-if ($HTML::Mason::VERSION < 1.0902) {
-        require HTML::Mason::ApacheHandler;
-
-         $parser = &RT::Interface::Web::NewParser(allow_globals => [%session]);
-
-         $interp = &RT::Interface::Web::NewInterp(parser=>$parser,
-                                             allow_recursive_autohandlers => 1,
-                                           out_method => \$output);
-}
-else {
-         $interp = &RT::Interface::Web::NewInterp(
-                                                  allow_globals => [%session],
-                                                  default_escape_flags => 'h',
-
-                                           out_method => \$output);
-}
-# Die if WebSessionDir doesn't exist or we can't write to it
-
-stat ($RT::MasonSessionDir);
-die "Can't read and write $RT::MasonSessionDir"
-  unless (( -d _ ) and ( -r _ ) and ( -w _ ));
-
-
-RT::Init();
-
-# Response loop
-while ($RT::Mason::cgi = new CGI::Fast) {
-    
-    $HTML::Mason::Commands::ContentType = 'text/html';
-        
-    # This routine comes from ApacheHandler.pm:
-    my (%args, $cookie);
-    foreach my $key ( $cgi->param ) {
-       foreach my $value ( $cgi->param($key) ) {
-           if (exists($args{$key})) {
-               if (ref($args{$key})) {
-                   $args{$key} = [@{$args{$key}}, $value];
-               } else {
-                   $args{$key} = [$args{$key}, $value];
-               }
-           } else {
-               $args{$key} = $value;
-           }
-
-       }
-       
+    my $interp = $RT::Mason::Handler->interp;
+    if (
+        !$interp->comp_exists( $cgi->path_info )
+        && $interp->comp_exists( $cgi->path_info . "/index.html" )
+    ) {
+        $cgi->path_info( $cgi->path_info . "/index.html" );
     }
-    
-
-    my $comp = $ENV{'PATH_INFO'};
-    
-    if ($comp =~ /^(.*)$/) {  # untaint the path info. apache should
-                             # never hand us a bogus path. 
-                             # We should be more careful here.
-       $comp = $1;
-    }    
-    
-    if ($comp =~ /\/$/) {
-       $comp .= "index.html";
-    }  
-    
-    #This is all largely cut and pasted from mason's session_handler.pl
-    
-    # {{{ Cookies
-    my %cookies = fetch CGI::Cookie();
-    
-    eval {
-       my $session_id = undef;
 
-       #Get the session id and untaint it
-       if ($cookies{'AF_SID'} && $cookies{'AF_SID'}->value() =~ /^(.*)$/) {
-               $session_id = $1;
-       }
-       tie %HTML::Mason::Commands::session, 'Apache::Session::File',
-               $session_id, 
-           { Directory => $RT::MasonSessionDir,
-             LockDirectory => $RT::MasonSessionDir,
-           }   ;
-    };
-    
-    if ( $@ ) {
-       # If the session is invalid, create a new session.
-       if ( $@ =~ m#^Object does not exist in the data store# ) {
-            tie %HTML::Mason::Commands::session, 'Apache::Session::File', undef,
-            { Directory => $RT::MasonSessionDir,
-              LockDirectory => $RT::MasonSessionDir,
-            };
-            undef $cookies{'AF_SID'};
-       }
-         else {
-             die "$@ \nProbably means that RT Couldn't write to session directory '$RT::MasonSessionDir'. Check that this directory's permissions are correct.";
-         }
-    }
-    
-    if ( !$cookies{'AF_SID'} ) {
-       $cookie = new CGI::Cookie
-         (-name=>'AF_SID', 
-          -value=>$HTML::Mason::Commands::session{_session_id}, 
-          -path => '/',);
-       
-    } else {
-       $cookie = undef;
-    }
-    
-    # }}}
-    
-    $output = '';
-    eval {
-           my $status = $interp->exec($comp, %args);
-    };
+    local $@;
+    eval { $RT::Mason::Handler->handle_cgi_object($cgi); };
     if ($@) {
-       $output = "<PRE>$@</PRE>";
+        $RT::Logger->crit($@);
     }
-    print "Content-Type: $HTML::Mason::Commands::ContentType\r\n";
-    print "Set-Cookie: $cookie\r\n" if ($cookie);
-    print "\r\n";
-    print $output;
-    untie %HTML::Mason::Commands::session;
-    
+    RT::Interface::Web::Handler->CleanupRequest(); 
+
 }
+
+1;