import of rt 3.0.9
[freeside.git] / rt / bin / webmux.pl
1 #!/usr/bin/perl
2 # BEGIN LICENSE BLOCK
3
4 # Copyright (c) 1996-2003 Jesse Vincent <jesse@bestpractical.com>
5
6 # (Except where explictly superceded by other copyright notices)
7
8 # This work is made available to you under the terms of Version 2 of
9 # the GNU General Public License. A copy of that license should have
10 # been provided with this software, but in any event can be snarfed
11 # from www.gnu.org.
12
13 # This work is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 # General Public License for more details.
17
18 # Unless otherwise specified, all modifications, corrections or
19 # extensions to this work which alter its source code become the
20 # property of Best Practical Solutions, LLC when submitted for
21 # inclusion in the work.
22
23
24 # END LICENSE BLOCK
25
26 use strict;
27
28 BEGIN {
29     $ENV{'PATH'}   = '/bin:/usr/bin';                      # or whatever you need
30     $ENV{'CDPATH'} = '' if defined $ENV{'CDPATH'};
31     $ENV{'SHELL'}  = '/bin/sh' if defined $ENV{'SHELL'};
32     $ENV{'ENV'}    = '' if defined $ENV{'ENV'};
33     $ENV{'IFS'}    = '' if defined $ENV{'IFS'};
34     
35 }
36
37 use lib ("/opt/rt3/local/lib", "/opt/rt3/lib");
38 use RT;
39
40 package RT::Mason;
41
42 use CGI qw(-private_tempfiles);    #bring this in before mason, to make sure we
43                                    #set private_tempfiles
44
45 BEGIN {
46     if ($mod_perl::VERSION >= 1.9908) {
47         require Apache::RequestUtil;
48         no warnings 'redefine';
49         my $sub = *Apache::request{CODE};
50         *Apache::request = sub {
51             my $r;
52             eval { $r = $sub->('Apache'); };
53             # warn $@ if $@;
54             return $r;
55         };
56     }
57     if ($CGI::MOD_PERL) {
58         require HTML::Mason::ApacheHandler;
59     }
60     else {
61         require HTML::Mason::CGIHandler;
62     }
63 }
64
65 use HTML::Mason;                   # brings in subpackages: Parser, Interp, etc.
66
67 use vars qw($Nobody $SystemUser $r);
68
69 #This drags in RT's config.pm
70 RT::LoadConfig();
71
72 use Carp;
73
74 {
75     package HTML::Mason::Commands;
76     use vars qw(%session);
77
78     use RT::Tickets;
79     use RT::Transactions;
80     use RT::Users;
81     use RT::CurrentUser;
82     use RT::Templates;
83     use RT::Queues;
84     use RT::ScripActions;
85     use RT::ScripConditions;
86     use RT::Scrips;
87     use RT::Groups;
88     use RT::GroupMembers;
89     use RT::CustomFields;
90     use RT::CustomFieldValues;
91     use RT::TicketCustomFieldValues;
92
93     use RT::Interface::Web;
94     use MIME::Entity;
95     use Text::Wrapper;
96     use CGI::Cookie;
97     use Time::ParseDate;
98     use HTML::Entities;
99 }
100
101
102 # Activate the following if running httpd as root (the normal case).
103 # Resets ownership of all files created by Mason at startup.
104 # Note that mysql uses DB for sessions, so there's no need to do this.
105 unless ($RT::DatabaseType =~ /(mysql|Pg)/) {
106     # Clean up our umask to protect session files
107     umask(0077);
108
109 if ( $CGI::MOD_PERL)  {
110     chown( Apache->server->uid, Apache->server->gid, [$RT::MasonSessionDir] )
111         if Apache->server->can('uid');
112         }
113     # Die if WebSessionDir doesn't exist or we can't write to it
114     stat($RT::MasonSessionDir);
115     die "Can't read and write $RT::MasonSessionDir"
116         unless ( ( -d _ ) and ( -r _ ) and ( -w _ ) );
117 }
118
119 my $ah = &RT::Interface::Web::NewApacheHandler(@RT::MasonParameters) if $CGI::MOD_PERL;
120
121 sub handler {
122     ($r) = @_;
123
124     local $SIG{__WARN__};
125     local $SIG{__DIE__};
126
127     RT::Init();
128
129     # We don't need to handle non-text items
130     return -1 if defined( $r->content_type ) && $r->content_type !~ m|^text/|io;
131
132     my %session;
133     my $status;
134     eval { $status = $ah->handle_request($r) };
135     if ($@) {
136         $RT::Logger->crit($@);
137     }
138
139     undef (%session);
140
141     if ($RT::Handle->TransactionDepth) {
142         $RT::Handle->ForceRollback;
143         $RT::Logger->crit("Transaction not committed. Usually indicates a software fault. Data loss may have occurred") ;
144     }
145     return $status;
146 }
147
148 1;