import rt 3.0.12
[freeside.git] / rt / bin / webmux.pl.in
1 #!@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     @ORACLE_ENV_PREF@
35
36     eval { require Apache2; require Apache::compat }
37         if $ENV{'MOD_PERL'} and $ENV{'MOD_PERL'} =~ m{mod_perl/(?:2|1\.9)};
38
39 }
40
41 use lib ("@LOCAL_LIB_PATH@", "@RT_LIB_PATH@");
42 use RT;
43
44 package RT::Mason;
45
46 use CGI qw(-private_tempfiles);    #bring this in before mason, to make sure we
47                                    #set private_tempfiles
48
49 BEGIN {
50     if ($mod_perl::VERSION >= 1.9908) {
51         require Apache::RequestUtil;
52         no warnings 'redefine';
53         my $sub = *Apache::request{CODE};
54         *Apache::request = sub {
55             my $r;
56             eval { $r = $sub->('Apache'); };
57             # warn $@ if $@;
58             return $r;
59         };
60     }
61     if ($CGI::MOD_PERL) {
62         require HTML::Mason::ApacheHandler;
63     }
64     else {
65         require HTML::Mason::CGIHandler;
66     }
67 }
68
69 use HTML::Mason;                   # brings in subpackages: Parser, Interp, etc.
70
71 use vars qw($Nobody $SystemUser $r);
72
73 #This drags in RT's config.pm
74 RT::LoadConfig();
75
76 use Carp;
77
78 {
79     package HTML::Mason::Commands;
80     use vars qw(%session);
81
82     use RT::Tickets;
83     use RT::Transactions;
84     use RT::Users;
85     use RT::CurrentUser;
86     use RT::Templates;
87     use RT::Queues;
88     use RT::ScripActions;
89     use RT::ScripConditions;
90     use RT::Scrips;
91     use RT::Groups;
92     use RT::GroupMembers;
93     use RT::CustomFields;
94     use RT::CustomFieldValues;
95     use RT::TicketCustomFieldValues;
96
97     use RT::Interface::Web;
98     use MIME::Entity;
99     use Text::Wrapper;
100     use CGI::Cookie;
101     use Time::ParseDate;
102     use HTML::Entities;
103 }
104
105
106 # Activate the following if running httpd as root (the normal case).
107 # Resets ownership of all files created by Mason at startup.
108 # Note that mysql uses DB for sessions, so there's no need to do this.
109 unless ($RT::DatabaseType =~ /(mysql|Pg)/) {
110     # Clean up our umask to protect session files
111     umask(0077);
112
113 if ( $CGI::MOD_PERL)  {
114     chown( Apache->server->uid, Apache->server->gid, [$RT::MasonSessionDir] )
115         if Apache->server->can('uid');
116         }
117     # Die if WebSessionDir doesn't exist or we can't write to it
118     stat($RT::MasonSessionDir);
119     die "Can't read and write $RT::MasonSessionDir"
120         unless ( ( -d _ ) and ( -r _ ) and ( -w _ ) );
121 }
122
123 my $ah = &RT::Interface::Web::NewApacheHandler(@RT::MasonParameters) if $CGI::MOD_PERL;
124
125 sub handler {
126     ($r) = @_;
127
128     local $SIG{__WARN__};
129     local $SIG{__DIE__};
130
131     RT::Init();
132
133     # We don't need to handle non-text items
134     return -1 if defined( $r->content_type ) && $r->content_type !~ m|^text/|io;
135
136     my %session;
137     my $status;
138     eval { $status = $ah->handle_request($r) };
139     if ($@) {
140         $RT::Logger->crit($@);
141     }
142
143     undef (%session);
144
145     if ($RT::Handle->TransactionDepth) {
146         $RT::Handle->ForceRollback;
147         $RT::Logger->crit("Transaction not committed. Usually indicates a software fault. Data loss may have occurred") ;
148     }
149     return $status;
150 }
151
152 1;