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