1 # BEGIN BPS TAGGED BLOCK {{{
5 # This software is Copyright (c) 1996-2009 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/licenses/old-licenses/gpl-2.0.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 }}}
58 RT::Config - RT's config
64 my $config = new RT::Config;
68 my $rt_web_path = $config->Get('WebPath');
69 $config->Set(EmailOutputEncoding => 'latin1');
71 # get config object from RT package
74 my $config = RT->Config;
78 C<RT::Config> class provide access to RT's and RT extensions' config files.
80 RT uses two files for site configuring:
82 First file is F<RT_Config.pm> - core config file. This file is shipped
83 with RT distribution and contains default values for all available options.
84 B<You should never edit this file.>
86 Second file is F<RT_SiteConfig.pm> - site config file. You can use it
87 to customize your RT instance. In this file you can override any option
88 listed in core config file.
90 RT extensions could also provide thier config files. Extensions should
91 use F<< <NAME>_Config.pm >> and F<< <NAME>_SiteConfig.pm >> names for
92 config files, where <NAME> is extension name.
94 B<NOTE>: All options from RT's config and extensions' configs are saved
95 in one place and thus extension could override RT's options, but it is not
102 Hash of Config options that may be user overridable
103 or may require more logic than should live in RT_*Config.pm
105 Keyed by config name, there are several properties that
106 can be set for each config optin:
108 Section - What header this option should be grouped
109 under on the user Settings page
110 Overridable - Can users change this option
111 SortOrder - Within a Section, how should the options be sorted
112 for display to the user
113 Widget - Mason component path to widget that should be used
114 to display this config option
115 WidgetArguments - An argument hash passed to the WIdget
116 Description - Friendly description to show the user
117 Values - Arrayref of options (for select Widget)
118 ValuesLabel - Hashref, key is the Value from the Values
119 list, value is a user friendly description
121 Callback - subref that receives no arguments. It returns
122 a hashref of items that are added to the rest
123 of the WidgetArguments
124 PostLoadCheck - subref passed the RT::Config object and the current
125 setting of the config option. Can make further checks
126 (such as seeing if a library is installed) and then change
127 the setting of this or other options in the Config using
128 the RT::Config option.
133 # General user overridable options
135 Section => 'General',
138 Widget => '/Widgets/Form/Select',
140 Description => 'Default queue', #loc
142 my $ret = { Values => [], ValuesLabel => {}};
143 my $q = new RT::Queues($HTML::Mason::Commands::session{'CurrentUser'});
145 while (my $queue = $q->Next) {
146 next unless $queue->CurrentUserHasRight("CreateTicket");
147 push @{$ret->{Values}}, $queue->Id;
148 $ret->{ValuesLabel}{$queue->Id} = $queue->Name;
155 Section => 'General',
158 Widget => '/Widgets/Form/Select',
160 Description => 'Username format', # loc
161 Values => [qw(concise verbose)],
163 concise => 'Short usernames', # loc_left_pair
164 verbose => 'Name and email address', # loc_left_pair
168 WebDefaultStylesheet => {
169 Section => 'General', #loc
172 Widget => '/Widgets/Form/Select',
174 Description => 'Theme', #loc
175 # XXX: we need support for 'get values callback'
176 Values => [qw(3.5-default 3.4-compat web2)],
179 MessageBoxRichText => {
180 Section => 'General',
183 Widget => '/Widgets/Form/Boolean',
185 Description => 'WYSIWYG message composer' # loc
188 MessageBoxRichTextHeight => {
189 Section => 'General',
192 Widget => '/Widgets/Form/Integer',
194 Description => 'WYSIWYG composer height', # loc
198 Section => 'General',
201 Widget => '/Widgets/Form/Integer',
203 Description => 'Message box width', #loc
206 MessageBoxHeight => {
207 Section => 'General',
210 Widget => '/Widgets/Form/Integer',
212 Description => 'Message box height', #loc
215 SearchResultsRefreshInterval => {
216 Section => 'General', #loc
219 Widget => '/Widgets/Form/Select',
221 Description => 'Search results refresh interval', #loc
222 Values => [qw(0 120 300 600 1200 3600 7200)],
224 0 => "Don't refresh search results.", #loc
225 120 => "Refresh search results every 2 minutes.", #loc
226 300 => "Refresh search results every 5 minutes.", #loc
227 600 => "Refresh search results every 10 minutes.", #loc
228 1200 => "Refresh search results every 20 minutes.", #loc
229 3600 => "Refresh search results every 60 minutes.", #loc
230 7200 => "Refresh search results every 120 minutes.", #loc
235 # User overridable options for RT at a glance
236 DefaultSummaryRows => {
237 Section => 'RT at a glance', #loc
240 Widget => '/Widgets/Form/Integer',
242 Description => 'Number of search results', #loc
245 HomePageRefreshInterval => {
246 Section => 'RT at a glance', #loc
249 Widget => '/Widgets/Form/Select',
251 Description => 'Home page refresh interval', #loc
252 Values => [qw(0 120 300 600 1200 3600 7200)],
254 0 => "Don't refresh home page.", #loc
255 120 => "Refresh home page every 2 minutes.", #loc
256 300 => "Refresh home page every 5 minutes.", #loc
257 600 => "Refresh home page every 10 minutes.", #loc
258 1200 => "Refresh home page every 20 minutes.", #loc
259 3600 => "Refresh home page every 60 minutes.", #loc
260 7200 => "Refresh home page every 120 minutes.", #loc
265 # User overridable options for Ticket displays
267 Section => 'Ticket display', #loc
270 Widget => '/Widgets/Form/Integer',
272 Description => 'Maximum inline message length', #loc
274 "Length in characters; Use '0' to show all messages inline, regardless of length" #loc
277 OldestTransactionsFirst => {
278 Section => 'Ticket display',
281 Widget => '/Widgets/Form/Boolean',
283 Description => 'Show oldest history first', #loc
286 ShowUnreadMessageNotifications => {
287 Section => 'Ticket display',
290 Widget => '/Widgets/Form/Boolean',
292 Description => 'Notify me of unread messages', #loc
297 Section => 'Ticket display',
300 Widget => '/Widgets/Form/Boolean',
302 Description => 'add <pre> tag around plain text attachments', #loc
303 Hints => "Use this to protect the format of plain text" #loc
307 Section => 'Ticket display',
310 Widget => '/Widgets/Form/Boolean',
312 Description => 'display wrapped and formatted plain text attachments', #loc
313 Hints => 'Use css rules to display text monospaced and with formatting preserved, but wrap as needed. This does not work well with IE6 and you should use the previous option', #loc
317 # User overridable locale options
319 Section => 'Locale', #loc
321 Widget => '/Widgets/Form/Select',
323 Description => 'Date format', #loc
324 Callback => sub { my $ret = { Values => [], ValuesLabel => {}};
325 my $date = new RT::Date($HTML::Mason::Commands::session{'CurrentUser'});
327 foreach my $value ($date->Formatters) {
328 push @{$ret->{Values}}, $value;
329 $ret->{ValuesLabel}{$value} = $date->$value();
336 # User overridable mail options
338 Section => 'Mail', #loc
340 Default => 'Individual messages',
341 Widget => '/Widgets/Form/Select',
343 Description => 'Email delivery', #loc
345 'Individual messages', #loc
347 'Weekly digest', #loc
353 # this tends to break extensions that stash links in ticket update pages
356 PostLoadCheck => sub {
357 my ($self,$value) = @_;
358 $RT::Logger->error("your \$Organization setting ($value) appears to contain whitespace. Please fix this.")
363 # Internal config options
366 PostLoadCheck => sub {
370 return if $INC{'GraphViz.pm'};
372 return if eval {require GraphViz; 1};
373 $RT::Logger->debug("You've enabled GraphViz, but we couldn't load the module: $@");
374 $self->Set( DisableGraphViz => 1 );
379 PostLoadCheck => sub {
383 return if $INC{'GD.pm'};
385 return if eval {require GD; 1};
386 $RT::Logger->debug("You've enabled GD, but we couldn't load the module: $@");
387 $self->Set( DisableGD => 1 );
390 MailPlugins => { Type => 'ARRAY' },
391 Plugins => { Type => 'ARRAY' },
392 GnuPG => { Type => 'HASH' },
393 GnuPGOptions => { Type => 'HASH',
394 PostLoadCheck => sub {
396 my $gpg = $self->Get('GnuPG');
397 return unless $gpg->{'Enable'};
398 my $gpgopts = $self->Get('GnuPGOptions');
399 unless (-d $gpgopts->{homedir} && -r _ ) { # no homedir, no gpg
401 "RT's GnuPG libraries couldn't successfully read your".
402 " configured GnuPG home directory (".$gpgopts->{homedir}
403 ."). PGP support has been disabled");
404 $gpg->{'Enable'} = 0;
409 require RT::Crypt::GnuPG;
410 unless (RT::Crypt::GnuPG->Probe()) {
412 "RT's GnuPG libraries couldn't successfully execute gpg.".
413 " PGP support has been disabled");
414 $gpg->{'Enable'} = 0;
425 Object constructor returns new object. Takes no arguments.
431 my $class = ref($proto) ? ref($proto) : $proto;
432 my $self = bless {}, $class;
449 my %args = ( File => '', @_ );
450 $args{'File'} =~ s/(?<=Config)(?=\.pm$)/Meta/;
456 Load all configs. First of all load RT's config then load
457 extensions' config files in alphabetical order.
465 $self->InitConfig( File => 'RT_Config.pm' );
466 $self->LoadConfig( File => 'RT_Config.pm' );
468 my @configs = $self->Configs;
469 $self->InitConfig( File => $_ ) foreach @configs;
470 $self->LoadConfig( File => $_ ) foreach @configs;
476 Takes param hash with C<File> field.
477 First, the site configuration file is loaded, in order to establish
478 overall site settings like hostname and name of RT instance.
479 Then, the core configuration file is loaded to set fallback values
480 for all settings; it bases some values on settings from the site
483 B<Note> that core config file don't change options if site config
484 has set them so to add value to some option instead of
485 overriding you have to copy original value from core config file.
491 my %args = ( File => '', @_ );
492 $args{'File'} =~ s/(?<!Site)(?=Config\.pm$)/Site/;
493 if ( $args{'File'} eq 'RT_SiteConfig.pm'
494 and my $site_config = $ENV{RT_SITE_CONFIG} )
496 $self->_LoadConfig( %args, File => $site_config );
498 $self->_LoadConfig(%args);
500 $args{'File'} =~ s/Site(?=Config\.pm$)//;
501 $self->_LoadConfig(%args);
507 my %args = ( File => '', @_ );
509 my ($is_ext, $is_site);
510 if ( $args{'File'} eq ($ENV{RT_SITE_CONFIG}||'') ) {
511 ($is_ext, $is_site) = ('', 1);
513 $is_ext = $args{'File'} =~ /^(?!RT_)(?:(.*)_)(?:Site)?Config/ ? $1 : '';
514 $is_site = $args{'File'} =~ /SiteConfig/ ? 1 : 0;
519 local *Set = sub(\[$@%]@) {
520 my ( $opt_ref, @args ) = @_;
521 my ( $pack, $file, $line ) = caller;
522 return $self->SetFromConfig(
528 SiteConfig => $is_site,
529 Extension => $is_ext,
532 my @etc_dirs = ($RT::LocalEtcPath);
533 push @etc_dirs, RT->PluginDirs('etc') if $is_ext;
534 push @etc_dirs, $RT::EtcPath, @INC;
535 local @INC = @etc_dirs;
536 require $args{'File'};
539 return 1 if $is_site && $@ =~ qr{^Can't locate \Q$args{File}};
540 if ( $is_site || $@ !~ qr{^Can't locate \Q$args{File}} ) {
541 die qq{Couldn't load RT config file $args{'File'}:\n\n$@};
544 my $username = getpwuid($>);
545 my $group = getgrgid($();
547 my ( $file_path, $fileuid, $filegid );
548 foreach ( $RT::LocalEtcPath, $RT::EtcPath, @INC ) {
549 my $tmp = File::Spec->catfile( $_, $args{File} );
550 ( $fileuid, $filegid ) = ( stat($tmp) )[ 4, 5 ];
551 if ( defined $fileuid ) {
556 unless ($file_path) {
558 qq{Couldn't load RT config file $args{'File'} as user $username / group $group.\n}
559 . qq{The file couldn't be found in $RT::LocalEtcPath and $RT::EtcPath.\n$@};
564 RT couldn't load RT config file %s as:
568 The file is owned by user %s and group %s.
570 This usually means that the user/group your webserver is running
571 as cannot read the file. Be careful not to make the permissions
572 on this file too liberal, because it contains database passwords.
573 You may need to put the webserver user in the appropriate group
574 (%s) or change permissions be able to run succesfully.
577 my $fileusername = getpwuid($fileuid);
578 my $filegroup = getgrgid($filegid);
579 my $errormessage = sprintf( $message,
580 $file_path, $fileusername, $filegroup, $filegroup );
581 die "$errormessage\n$@";
588 foreach my $o ( grep $META{$_}{'PostLoadCheck'}, $self->Options( Overridable => undef ) ) {
589 $META{$o}->{'PostLoadCheck'}->( $self, $self->Get($o) );
595 Returns list of config files found in local etc, plugins' etc
596 and main etc directories.
604 foreach my $path ( $RT::LocalEtcPath, RT->PluginDirs('etc'), $RT::EtcPath ) {
605 my $mask = File::Spec->catfile( $path, "*_Config.pm" );
606 my @files = glob $mask;
607 @files = grep !/^RT_Config\.pm$/,
608 grep $_ && /^\w+_Config\.pm$/,
609 map { s/^.*[\\\/]//; $_ } @files;
610 push @configs, sort @files;
614 @configs = grep !$seen{$_}++, @configs;
620 Takes name of the option as argument and returns its current value.
622 In the case of a user-overridable option, first checks the user's
623 preferences before looking for site-wide configuration.
625 Returns values from RT_SiteConfig, RT_Config and then the %META hash
626 of configuration variables's "Default" for this config variable,
629 Returns different things in scalar and array contexts. For scalar
630 options it's not that important, however for arrays and hash it's.
631 In scalar context returns references to arrays and hashes.
633 Use C<scalar> perl's op to force context, especially when you use
634 C<(..., Argument => RT->Config->Get('ArrayOpt'), ...)>
635 as perl's '=>' op doesn't change context of the right hand argument to
636 scalar. Instead use C<(..., Argument => scalar RT->Config->Get('ArrayOpt'), ...)>.
638 It's also important for options that have no default value(no default
639 in F<etc/RT_Config.pm>). If you don't force scalar context then you'll
640 get empty list and all your named args will be messed up. For example
641 C<(arg1 => 1, arg2 => RT->Config->Get('OptionDoesNotExist'), arg3 => 3)>
642 will result in C<(arg1 => 1, arg2 => 'arg3', 3)> what is most probably
643 unexpected, or C<(arg1 => 1, arg2 => RT->Config->Get('ArrayOption'), arg3 => 3)>
644 will result in C<(arg1 => 1, arg2 => 'element of option', 'another_one' => ..., 'arg3', 3)>.
649 my ( $self, $name, $user ) = @_;
652 if ( $user && $user->id && $META{$name}->{'Overridable'} ) {
653 $user = $user->UserObj if $user->isa('RT::CurrentUser');
654 my $prefs = $user->Preferences($RT::System);
655 $res = $prefs->{$name} if $prefs;
657 $res = $OPTIONS{$name} unless defined $res;
658 $res = $META{$name}->{'Default'} unless defined $res;
659 return $self->_ReturnValue( $res, $META{$name}->{'Type'} || 'SCALAR' );
664 Set option's value to new value. Takes name of the option and new value.
667 The new value should be scalar, array or hash depending on type of the option.
668 If the option is not defined in meta or the default RT config then it is of
674 my ( $self, $name ) = ( shift, shift );
676 my $old = $OPTIONS{$name};
677 my $type = $META{$name}->{'Type'} || 'SCALAR';
678 if ( $type eq 'ARRAY' ) {
679 $OPTIONS{$name} = [@_];
680 { no warnings 'once'; no strict 'refs'; @{"RT::$name"} = (@_); }
681 } elsif ( $type eq 'HASH' ) {
682 $OPTIONS{$name} = {@_};
683 { no warnings 'once'; no strict 'refs'; %{"RT::$name"} = (@_); }
685 $OPTIONS{$name} = shift;
686 {no warnings 'once'; no strict 'refs'; ${"RT::$name"} = $OPTIONS{$name}; }
688 $META{$name}->{'Type'} = $type;
689 return $self->_ReturnValue( $old, $type );
693 my ( $self, $res, $type ) = @_;
694 return $res unless wantarray;
696 if ( $type eq 'ARRAY' ) {
697 return @{ $res || [] };
698 } elsif ( $type eq 'HASH' ) {
699 return %{ $res || {} };
717 unless ( $args{'File'} ) {
718 ( $args{'Package'}, $args{'File'}, $args{'Line'} ) = caller(1);
721 my $opt = $args{'Option'};
724 my $name = $self->__GetNameByRef($opt);
730 $type = $META{$name}->{'Type'} || 'SCALAR';
733 # if option is already set we have to check where
734 # it comes from and may be ignore it
735 if ( exists $OPTIONS{$name} ) {
736 if ( $args{'SiteConfig'} && $args{'Extension'} ) {
737 # if it's site config of an extension then it can only
738 # override options that came from its main config
739 if ( $args{'Extension'} ne $META{$name}->{'Source'}{'Extension'} ) {
740 my %source = %{ $META{$name}->{'Source'} };
742 "Change of config option '$name' at $args{'File'} line $args{'Line'} has been ignored."
743 ." This option earlier has been set in $source{'File'} line $source{'Line'}."
744 ." To overide this option use ". ($source{'Extension'}||'RT')
749 } elsif ( !$args{'SiteConfig'} && $META{$name}->{'Source'}{'SiteConfig'} ) {
750 # if it's core config then we can override any option that came from another
751 # core config, but not site config
753 my %source = %{ $META{$name}->{'Source'} };
754 if ( $source{'Extension'} ne $args{'Extension'} ) {
755 # as a site config is loaded earlier then its base config
756 # then we warn only on different extensions, for example
757 # RTIR's options is set in main site config or RTFM's
759 "Change of config option '$name' at $args{'File'} line $args{'Line'} has been ignored."
760 ." It's may be ok, but we want you to be aware."
761 ." This option earlier has been set in $source{'File'} line $source{'Line'}."
769 $META{$name}->{'Type'} = $type;
770 foreach (qw(Package File Line SiteConfig Extension)) {
771 $META{$name}->{'Source'}->{$_} = $args{$_};
773 $self->Set( $name, @{ $args{'Value'} } );
785 if ( !$pack && $last_pack ) {
786 my $tmp = $self->__GetNameByRef( $ref, $last_pack );
790 $pack .= '::' unless substr( $pack, -2 ) eq '::';
801 # scan $pack's nametable(hash)
802 foreach my $k ( keys %{$pack} ) {
804 # hash for main:: has reference on itself
805 next if $k eq 'main::';
807 # if entry has trailing '::' then
808 # it is link to other name space
810 $name = $self->__GetNameByRef( $ref, $k );
811 return $name if $name;
814 # entry of the table with references to
815 # SCALAR, ARRAY... and other types with
817 my $entry = ${$pack}{$k};
820 # get entry for type we are looking for
821 # XXX skip references to scalars or other references.
822 # Otherwie 5.10 goes boom. may be we should skip any
824 return if ref($entry) eq 'SCALAR' || ref($entry) eq 'REF';
825 my $entry_ref = *{$entry}{ ref($ref) };
826 next unless $entry_ref;
828 # if references are equal then we've found
829 if ( $entry_ref == $ref ) {
831 return ( $ref_sym{ ref($ref) } || '*' ) . $pack . $k;
846 return $META{ $_[1] };
854 map $_->{'Section'} || 'General',
860 my %args = ( Section => undef, Overridable => 1, Sorted => 1, @_ );
861 my @res = keys %META;
863 @res = grep( ( $META{$_}->{'Section'} || 'General' ) eq $args{'Section'},
865 ) if defined $args{'Section'};
867 if ( defined $args{'Overridable'} ) {
869 = grep( ( $META{$_}->{'Overridable'} || 0 ) == $args{'Overridable'},
873 if ( $args{'Sorted'} ) {
875 ($META{$a}->{SortOrder}||9999) <=> ($META{$b}->{SortOrder}||9999)
879 @res = sort { $a cmp $b } @res;
884 eval "require RT::Config_Vendor";
885 if ($@ && $@ !~ qr{^Can't locate RT/Config_Vendor.pm}) {
889 eval "require RT::Config_Local";
890 if ($@ && $@ !~ qr{^Can't locate RT/Config_Local.pm}) {