1 # BEGIN BPS TAGGED BLOCK {{{
5 # This software is Copyright (c) 1996-2007 Best Practical Solutions, LLC
6 # <jesse@bestpractical.com>
8 # (Except where explicitly superseded by other copyright notices)
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
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.
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.
30 # CONTRIBUTION SUBMISSION POLICY:
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.)
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.
47 # END BPS TAGGED BLOCK }}}
54 use vars qw($VERSION $System $SystemUser $Nobody $Handle $Logger
66 $MasonLocalComponentRoot
72 $CORE_CONFIG_FILE = "/opt/rt3/etc/RT_Config.pm";
73 $SITE_CONFIG_FILE = "/opt/rt3/etc/RT_SiteConfig.pm";
77 $BasePath = '/opt/rt3';
79 $EtcPath = '/opt/rt3/etc';
80 $BinPath = '/opt/rt3/bin';
81 $VarPath = '/opt/rt3/var';
82 $LocalPath = '/opt/rt3/local';
83 $LocalEtcPath = '/opt/rt3/local/etc';
84 $LocalLexiconPath = '/opt/rt3/local/po';
86 # $MasonComponentRoot is where your rt instance keeps its mason html files
88 $MasonComponentRoot = '/var/www/freeside/rt';
90 # $MasonLocalComponentRoot is where your rt instance keeps its site-local
93 $MasonLocalComponentRoot = '/opt/rt3/local/html';
95 # $MasonDataDir Where mason keeps its datafiles
97 $MasonDataDir = '/usr/local/etc/freeside/masondata';
99 # RT needs to put session data (for preserving state between connections
100 # via the web interface)
101 $MasonSessionDir = '/opt/rt3/var/session_data';
111 A fully featured request tracker package
117 Load RT's config file. First, the site configuration file
118 (C<RT_SiteConfig.pm>) is loaded, in order to establish overall site
119 settings like hostname and name of RT instance. Then, the core
120 configuration file (C<RT_Config.pm>) is loaded to set fallback values
121 for all settings; it bases some values on settings from the site
124 In order for the core configuration to not override the site's
125 settings, the function C<Set> is used; it only sets values if they
126 have not been set already.
131 local *Set = sub { $_[0] = $_[1] unless defined $_[0] };
133 my $username = getpwuid($>);
134 my $group = getgrgid($();
137 RT couldn't load RT config file %s as:
141 The file is owned by user %s and group %s.
143 This usually means that the user/group your webserver is running
144 as cannot read the file. Be careful not to make the permissions
145 on this file too liberal, because it contains database passwords.
146 You may need to put the webserver user in the appropriate group
147 (%s) or change permissions be able to run succesfully.
151 if ( -f "$SITE_CONFIG_FILE" ) {
152 eval { require $SITE_CONFIG_FILE };
154 my ($fileuid,$filegid) = (stat($SITE_CONFIG_FILE))[4,5];
155 my $fileusername = getpwuid($fileuid);
156 my $filegroup = getgrgid($filegid);
157 my $errormessage = sprintf($message, $SITE_CONFIG_FILE,
158 $fileusername, $filegroup, $filegroup);
159 die ("$errormessage\n$@");
162 eval { require $CORE_CONFIG_FILE };
164 my ($fileuid,$filegid) = (stat($SITE_CONFIG_FILE))[4,5];
165 my $fileusername = getpwuid($fileuid);
166 my $filegroup = getgrgid($filegid);
167 my $errormessage = sprintf($message, $SITE_CONFIG_FILE,
168 $fileusername, $filegroup, $filegroup);
169 die ("$errormessage '$CORE_CONFIG_FILE'\n$@")
172 # RT::Essentials mistakenly recommends that WebPath be set to '/'.
173 # If the user does that, do what they mean.
174 $RT::WebPath = '' if ($RT::WebPath eq '/');
176 $ENV{'TZ'} = $RT::Timezone if ($RT::Timezone);
183 Conenct to the database, set up logging.
189 CheckPerlRequirements();
191 #Get a database connection
194 #RT's system user is a genuine database user. its id lives here
195 $SystemUser = new RT::CurrentUser();
196 $SystemUser->LoadByName('RT_System');
198 #RT's "nobody user" is a genuine database user. its ID lives here.
199 $Nobody = new RT::CurrentUser();
200 $Nobody->LoadByName('Nobody');
202 $System = RT::System->new();
209 =head2 ConnectToDatabase
211 Get a database connection
215 sub ConnectToDatabase {
217 unless ($Handle && $Handle->dbh && $Handle->dbh->ping) {
218 $Handle = RT::Handle->new();
225 Create the RT::Logger object.
231 # We have to set the record separator ($, man perlvar)
232 # or Log::Dispatch starts getting
233 # really pissy, as some other module we use unsets it.
236 use Log::Dispatch 1.6;
238 unless ($RT::Logger) {
240 $RT::Logger = Log::Dispatch->new();
242 my $simple_cb = sub {
243 # if this code throw any warning we can get segfault
248 my $frame = 0; # stack frame index
249 # skip Log::* stack frames
250 $frame++ while( caller($frame) && caller($frame) =~ /^Log::/ );
252 my ($package, $filename, $line) = caller($frame);
253 $p{message} =~ s/(?:\r*\n)+$//;
254 my $str = "[".gmtime(time)."] [".$p{level}."]: $p{message} ($filename:$line)\n";
256 if( $RT::LogStackTraces ) {
257 $str .= "\nStack trace:\n";
258 # skip calling of the Log::* subroutins
259 $frame++ while( caller($frame) && (caller($frame))[3] =~ /^Log::/ );
260 while( my ($package, $filename, $line, $sub) = caller($frame++) ) {
261 $str .= "\t". $sub ."() called at $filename:$line\n";
267 my $syslog_cb = sub {
270 my $frame = 0; # stack frame index
271 # skip Log::* stack frames
272 $frame++ while( caller($frame) && caller($frame) =~ /^Log::/ );
273 my ($package, $filename, $line) = caller($frame);
275 # syswrite() cannot take utf8; turn it off here.
276 Encode::_utf8_off($p{message});
278 $p{message} =~ s/(?:\r*\n)+$//;
279 if ($p{level} eq 'debug') {
280 return "$p{message}\n"
282 return "$p{message} ($filename:$line)\n"
286 if ($RT::LogToFile) {
287 my ($filename, $logdir);
288 if ($RT::LogToFileNamed =~ m![/\\]!) {
289 # looks like an absolute path.
290 $filename = $RT::LogToFileNamed;
291 ($logdir) = $RT::LogToFileNamed =~ m!^(.*[/\\])!;
294 $filename = "$RT::LogDir/$RT::LogToFileNamed";
295 $logdir = $RT::LogDir;
298 unless ( -d $logdir && ( ( -f $filename && -w $filename ) || -w $logdir ) ) {
299 # localizing here would be hard when we don't have a current user yet
300 die "Log file $filename couldn't be written or created.\n RT can't run.";
303 package Log::Dispatch::File;
304 require Log::Dispatch::File;
305 $RT::Logger->add(Log::Dispatch::File->new
307 min_level=> $RT::LogToFile,
308 filename=> $filename,
310 callbacks => $simple_cb,
313 if ($RT::LogToScreen) {
314 package Log::Dispatch::Screen;
315 require Log::Dispatch::Screen;
316 $RT::Logger->add(Log::Dispatch::Screen->new
318 min_level => $RT::LogToScreen,
319 callbacks => $simple_cb,
323 if ($RT::LogToSyslog) {
324 package Log::Dispatch::Syslog;
325 require Log::Dispatch::Syslog;
326 $RT::Logger->add(Log::Dispatch::Syslog->new
329 min_level => $RT::LogToSyslog,
330 callbacks => $syslog_cb,
338 # {{{ Signal handlers
340 ## This is the default handling of warnings and die'ings in the code
341 ## (including other used modules - maybe except for errors catched by
342 ## Mason). It will log all problems through the standard logging
343 ## mechanism (see above).
345 $SIG{__WARN__} = sub {
346 # The 'wide character' warnings has to be silenced for now, at least
347 # until HTML::Mason offers a sane way to process both raw output and
349 # use 'goto &foo' syntax to hide ANON sub from stack
350 if( index($_[0], 'Wide character in ') != 0 ) {
351 unshift @_, $RT::Logger, qw(level warning message);
352 goto &Log::Dispatch::log;
356 #When we call die, trap it and log->crit with the value of the die.
358 $SIG{__DIE__} = sub {
359 unless ($^S || !defined $^S ) {
360 $RT::Handle->Rollback();
361 $RT::Logger->crit("$_[0]");
371 sub CheckPerlRequirements {
372 if ($^V < 5.008003) {
373 die sprintf "RT requires Perl v5.8.3 or newer. Your current Perl is v%vd\n", $^V;
380 require Scalar::Util; Scalar::Util::weaken($y);
385 RT requires the Scalar::Util module be built with support for the 'weaken'
388 It is sometimes the case that operating system upgrades will replace
389 a working Scalar::Util with a non-working one. If your system was working
390 correctly up until now, this is likely the cause of the problem.
392 Please reinstall Scalar::Util, being careful to let it build with your C
393 compiler. Ususally this is as simple as running the following command as
396 perl -MCPAN -e'install Scalar::Util'
406 Load all modules that define base classes
412 require RT::Transactions;
414 require RT::CurrentUser;
415 require RT::Templates;
417 require RT::ScripActions;
418 require RT::ScripConditions;
421 require RT::GroupMembers;
422 require RT::CustomFields;
423 require RT::CustomFieldValues;
424 require RT::ObjectCustomFields;
425 require RT::ObjectCustomFieldValues;
441 Please report them to rt-bugs@fsck.com, if you know what's broken and have at least
442 some idea of what needs to be fixed.
444 If you're not sure what's going on, report them rt-devel@lists.bestpractical.com.
449 L<DBIx::SearchBuilder>
453 ok ($RT::Nobody->Name() eq 'Nobody', "Nobody is nobody");
454 ok ($RT::Nobody->Name() ne 'root', "Nobody isn't named root");
455 ok ($RT::SystemUser->Name() eq 'RT_System', "The system user is RT_System");
456 ok ($RT::SystemUser->Name() ne 'noname', "The system user isn't noname");
462 eval "require RT_Local";
463 die $@ if ($@ && $@ !~ qr{^Can't locate RT_Local.pm});