import rt 3.6.4
[freeside.git] / rt / lib / RT / Interface / Web / Handler.pm
1 # BEGIN BPS TAGGED BLOCK {{{
2
3 # COPYRIGHT:
4 #  
5 # This software is Copyright (c) 1996-2007 Best Practical Solutions, LLC 
6 #                                          <jesse@bestpractical.com>
7
8 # (Except where explicitly superseded by other copyright notices)
9
10
11 # LICENSE:
12
13 # This work is made available to you under the terms of Version 2 of
14 # the GNU General Public License. A copy of that license should have
15 # been provided with this software, but in any event can be snarfed
16 # from www.gnu.org.
17
18 # This work is distributed in the hope that it will be useful, but
19 # WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21 # General Public License for more details.
22
23 # You should have received a copy of the GNU General Public License
24 # along with this program; if not, write to the Free Software
25 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26 # 02110-1301 or visit their web page on the internet at
27 # http://www.gnu.org/copyleft/gpl.html.
28
29
30 # CONTRIBUTION SUBMISSION POLICY:
31
32 # (The following paragraph is not intended to limit the rights granted
33 # to you to modify and distribute this software under the terms of
34 # the GNU General Public License and is only of importance to you if
35 # you choose to contribute your changes and enhancements to the
36 # community by submitting them to Best Practical Solutions, LLC.)
37
38 # By intentionally submitting any modifications, corrections or
39 # derivatives to this work, or any other work intended for use with
40 # Request Tracker, to Best Practical Solutions, LLC, you confirm that
41 # you are the copyright holder for those contributions and you grant
42 # Best Practical Solutions,  LLC a nonexclusive, worldwide, irrevocable,
43 # royalty-free, perpetual, license to use, copy, create derivative
44 # works based on those contributions, and sublicense and distribute
45 # those contributions and any derivatives thereof.
46
47 # END BPS TAGGED BLOCK }}}
48 package RT::Interface::Web::Handler;
49
50 use CGI qw/-private_tempfiles/;
51 use MIME::Entity;
52 use Text::Wrapper;
53 use CGI::Cookie;
54 use Time::ParseDate;
55 use Time::HiRes;
56 use HTML::Entities;
57 use HTML::Scrubber;
58 use RT::Interface::Web::Handler;
59 use File::Path qw( rmtree );
60 use File::Glob qw( bsd_glob );
61 use File::Spec::Unix;
62
63 sub DefaultHandlerArgs  { (
64     comp_root => [
65         [ local    => $RT::MasonLocalComponentRoot ],
66         [ standard => $RT::MasonComponentRoot ]
67     ],
68     default_escape_flags => 'h',
69     data_dir             => "$RT::MasonDataDir",
70     allow_globals        => [qw(%session)],
71     # Turn off static source if we're in developer mode.
72     static_source        => ($RT::DevelMode ? '0' : '1'), 
73     use_object_files     => ($RT::DevelMode ? '0' : '1'), 
74     autoflush            => 0
75 ) };
76
77 # {{{ sub new 
78
79 =head2 new
80
81   Constructs a web handler of the appropriate class.
82   Takes options to pass to the constructor.
83
84 =cut
85
86 sub new {
87     my $class = shift;
88     $class->InitSessionDir;
89
90     if ( $mod_perl::VERSION && $mod_perl::VERSION >= 1.9908 ) {
91         goto &NewApacheHandler;
92     }
93     elsif ($CGI::MOD_PERL) {
94         goto &NewApacheHandler;
95     }
96     else {
97         goto &NewCGIHandler;
98     }
99 }
100
101 sub InitSessionDir {
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
107         # Clean up our umask to protect session files
108         umask(0077);
109
110         if ($CGI::MOD_PERL) { local $@; eval {
111
112             chown( Apache->server->uid, Apache->server->gid,
113                 $RT::MasonSessionDir )
114         }} 
115
116         # Die if WebSessionDir doesn't exist or we can't write to it
117         stat($RT::MasonSessionDir);
118         die "Can't read and write $RT::MasonSessionDir"
119         unless ( ( -d _ ) and ( -r _ ) and ( -w _ ) );
120     }
121
122 }
123
124 # }}}
125
126 # {{{ sub NewApacheHandler 
127
128 =head2 NewApacheHandler
129
130   Takes extra options to pass to HTML::Mason::ApacheHandler->new
131   Returns a new Mason::ApacheHandler object
132
133 =cut
134
135 sub NewApacheHandler {
136     require HTML::Mason::ApacheHandler;
137     return NewHandler('HTML::Mason::ApacheHandler', args_method => "CGI", @_);
138 }
139
140 # }}}
141
142 # {{{ sub NewApache2Handler 
143
144 =head2 NewApache2Handler
145
146   Takes extra options to pass to MasonX::Apache2Handler->new
147   Returns a new MasonX::Apache2Handler object
148
149 =cut
150
151 sub NewApache2Handler {
152     require MasonX::Apache2Handler;
153     return NewHandler('MasonX::Apache2Handler', args_method => "CGI", @_);
154 }
155
156 # }}}
157
158 # {{{ sub NewCGIHandler 
159
160 =head2 NewCGIHandler
161
162   Returns a new Mason::CGIHandler object
163
164 =cut
165
166 sub NewCGIHandler {
167     require HTML::Mason::CGIHandler;
168     return NewHandler('HTML::Mason::CGIHandler', @_);
169 }
170
171 sub NewHandler {
172     my $class = shift;
173     my $handler = $class->new(
174         DefaultHandlerArgs(),
175         @_
176     );
177   
178     $handler->interp->set_escape( h => \&RT::Interface::Web::EscapeUTF8 );
179     $handler->interp->set_escape( u => \&RT::Interface::Web::EscapeURI  );
180     return($handler);
181 }
182
183 =head2 CleanupRequest
184
185 Rollback any uncommitted transaction.
186 Flush the ACL cache
187 Flush the searchbuilder query cache
188
189 =cut
190
191 sub CleanupRequest {
192
193     if ( $RT::Handle->TransactionDepth ) {
194         $RT::Handle->ForceRollback;
195         $RT::Logger->crit(
196             "Transaction not committed. Usually indicates a software fault."
197             . "Data loss may have occurred" );
198     }
199
200     # Clean out the ACL cache. the performance impact should be marginal.
201     # Consistency is imprived, too.
202     RT::Principal->InvalidateACLCache();
203     DBIx::SearchBuilder::Record::Cachable->FlushCache
204       if ( $RT::WebFlushDbCacheEveryRequest
205         and UNIVERSAL::can(
206             'DBIx::SearchBuilder::Record::Cachable' => 'FlushCache' ) );
207
208 }
209 # }}}
210
211 1;