1 # BEGIN BPS TAGGED BLOCK {{{
5 # This software is Copyright (c) 1996-2011 Best Practical Solutions, LLC
6 # <sales@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
234 ResolveDefaultUpdateType => {
235 Section => 'General', #loc
238 Widget => '/Widgets/Form/Select',
240 Description => 'Default Update Type when Resolving', #loc
241 Values => [qw(Comment Respond)],
243 Comment => "Comments (Not sent to requestors)", #loc
244 Respond => "Reply to requestors", #loc
248 SuppressAutoOpenOnUpdate => {
249 Section => 'General',
252 Widget => '/Widgets/Form/Boolean',
254 Description => 'Suppress automatic new to open status change on ticket update' # loc
258 # User overridable options for RT at a glance
259 DefaultSummaryRows => {
260 Section => 'RT at a glance', #loc
263 Widget => '/Widgets/Form/Integer',
265 Description => 'Number of search results', #loc
268 HomePageRefreshInterval => {
269 Section => 'RT at a glance', #loc
272 Widget => '/Widgets/Form/Select',
274 Description => 'Home page refresh interval', #loc
275 Values => [qw(0 120 300 600 1200 3600 7200)],
277 0 => "Don't refresh home page.", #loc
278 120 => "Refresh home page every 2 minutes.", #loc
279 300 => "Refresh home page every 5 minutes.", #loc
280 600 => "Refresh home page every 10 minutes.", #loc
281 1200 => "Refresh home page every 20 minutes.", #loc
282 3600 => "Refresh home page every 60 minutes.", #loc
283 7200 => "Refresh home page every 120 minutes.", #loc
288 # User overridable options for Ticket displays
290 Section => 'Ticket display', #loc
293 Widget => '/Widgets/Form/Integer',
295 Description => 'Maximum inline message length', #loc
297 "Length in characters; Use '0' to show all messages inline, regardless of length" #loc
300 OldestTransactionsFirst => {
301 Section => 'Ticket display',
304 Widget => '/Widgets/Form/Boolean',
306 Description => 'Show oldest history first', #loc
309 ShowUnreadMessageNotifications => {
310 Section => 'Ticket display',
313 Widget => '/Widgets/Form/Boolean',
315 Description => 'Notify me of unread messages', #loc
320 Section => 'Ticket display',
323 Widget => '/Widgets/Form/Boolean',
325 Description => 'add <pre> tag around plain text attachments', #loc
326 Hints => "Use this to protect the format of plain text" #loc
330 Section => 'Ticket display',
333 Widget => '/Widgets/Form/Boolean',
335 Description => 'display wrapped and formatted plain text attachments', #loc
336 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
340 # User overridable locale options
342 Section => 'Locale', #loc
344 Widget => '/Widgets/Form/Select',
346 Description => 'Date format', #loc
347 Callback => sub { my $ret = { Values => [], ValuesLabel => {}};
348 my $date = new RT::Date($HTML::Mason::Commands::session{'CurrentUser'});
350 foreach my $value ($date->Formatters) {
351 push @{$ret->{Values}}, $value;
352 $ret->{ValuesLabel}{$value} = $date->$value();
361 PostLoadCheck => sub {
363 my $value = $self->Get('RTAddressRegexp');
367 'The RTAddressRegexp option is not set in the config.'
368 .' Not setting this option results in additional SQL queries to'
369 .' check whether each address belongs to RT or not.'
370 .' It is especially important to set this option if RT recieves'
371 .' emails on addresses that are not in the database or config.'
375 # User overridable mail options
377 Section => 'Mail', #loc
379 Default => 'Individual messages',
380 Widget => '/Widgets/Form/Select',
382 Description => 'Email delivery', #loc
384 'Individual messages', #loc
386 'Weekly digest', #loc
392 Section => 'Mail', #loc
395 Widget => '/Widgets/Form/Boolean',
397 Description => 'Outgoing mail', #loc
398 Hints => 'Should RT send you mail for ticket updates you make?', #loc
402 # this tends to break extensions that stash links in ticket update pages
405 PostLoadCheck => sub {
406 my ($self,$value) = @_;
407 $RT::Logger->error("your \$Organization setting ($value) appears to contain whitespace. Please fix this.")
412 # Internal config options
415 PostLoadCheck => sub {
419 return if $INC{'GraphViz.pm'};
421 return if eval {require GraphViz; 1};
422 $RT::Logger->debug("You've enabled GraphViz, but we couldn't load the module: $@");
423 $self->Set( DisableGraphViz => 1 );
428 PostLoadCheck => sub {
432 return if $INC{'GD.pm'};
434 return if eval {require GD; 1};
435 $RT::Logger->debug("You've enabled GD, but we couldn't load the module: $@");
436 $self->Set( DisableGD => 1 );
439 MailPlugins => { Type => 'ARRAY' },
440 Plugins => { Type => 'ARRAY' },
441 GnuPG => { Type => 'HASH' },
442 GnuPGOptions => { Type => 'HASH',
443 PostLoadCheck => sub {
445 my $gpg = $self->Get('GnuPG');
446 return unless $gpg->{'Enable'};
447 my $gpgopts = $self->Get('GnuPGOptions');
448 unless (-d $gpgopts->{homedir} && -r _ ) { # no homedir, no gpg
450 "RT's GnuPG libraries couldn't successfully read your".
451 " configured GnuPG home directory (".$gpgopts->{homedir}
452 ."). PGP support has been disabled");
453 $gpg->{'Enable'} = 0;
458 require RT::Crypt::GnuPG;
459 unless (RT::Crypt::GnuPG->Probe()) {
461 "RT's GnuPG libraries couldn't successfully execute gpg.".
462 " PGP support has been disabled");
463 $gpg->{'Enable'} = 0;
474 Object constructor returns new object. Takes no arguments.
480 my $class = ref($proto) ? ref($proto) : $proto;
481 my $self = bless {}, $class;
498 my %args = ( File => '', @_ );
499 $args{'File'} =~ s/(?<=Config)(?=\.pm$)/Meta/;
505 Load all configs. First of all load RT's config then load
506 extensions' config files in alphabetical order.
514 $self->InitConfig( File => 'RT_Config.pm' );
515 $self->LoadConfig( File => 'RT_Config.pm' );
517 my @configs = $self->Configs;
518 $self->InitConfig( File => $_ ) foreach @configs;
519 $self->LoadConfig( File => $_ ) foreach @configs;
525 Takes param hash with C<File> field.
526 First, the site configuration file is loaded, in order to establish
527 overall site settings like hostname and name of RT instance.
528 Then, the core configuration file is loaded to set fallback values
529 for all settings; it bases some values on settings from the site
532 B<Note> that core config file don't change options if site config
533 has set them so to add value to some option instead of
534 overriding you have to copy original value from core config file.
540 my %args = ( File => '', @_ );
541 $args{'File'} =~ s/(?<!Site)(?=Config\.pm$)/Site/;
542 if ( $args{'File'} eq 'RT_SiteConfig.pm'
543 and my $site_config = $ENV{RT_SITE_CONFIG} )
545 $self->_LoadConfig( %args, File => $site_config );
547 $self->_LoadConfig(%args);
549 $args{'File'} =~ s/Site(?=Config\.pm$)//;
550 $self->_LoadConfig(%args);
556 my %args = ( File => '', @_ );
558 my ($is_ext, $is_site);
559 if ( $args{'File'} eq ($ENV{RT_SITE_CONFIG}||'') ) {
560 ($is_ext, $is_site) = ('', 1);
562 $is_ext = $args{'File'} =~ /^(?!RT_)(?:(.*)_)(?:Site)?Config/ ? $1 : '';
563 $is_site = $args{'File'} =~ /SiteConfig/ ? 1 : 0;
568 local *Set = sub(\[$@%]@) {
569 my ( $opt_ref, @args ) = @_;
570 my ( $pack, $file, $line ) = caller;
571 return $self->SetFromConfig(
577 SiteConfig => $is_site,
578 Extension => $is_ext,
581 my @etc_dirs = ($RT::LocalEtcPath);
582 push @etc_dirs, RT->PluginDirs('etc') if $is_ext;
583 push @etc_dirs, $RT::EtcPath, @INC;
584 local @INC = @etc_dirs;
585 require $args{'File'};
588 return 1 if $is_site && $@ =~ qr{^Can't locate \Q$args{File}};
589 if ( $is_site || $@ !~ qr{^Can't locate \Q$args{File}} ) {
590 die qq{Couldn't load RT config file $args{'File'}:\n\n$@};
593 my $username = getpwuid($>);
594 my $group = getgrgid($();
596 my ( $file_path, $fileuid, $filegid );
597 foreach ( $RT::LocalEtcPath, $RT::EtcPath, @INC ) {
598 my $tmp = File::Spec->catfile( $_, $args{File} );
599 ( $fileuid, $filegid ) = ( stat($tmp) )[ 4, 5 ];
600 if ( defined $fileuid ) {
605 unless ($file_path) {
607 qq{Couldn't load RT config file $args{'File'} as user $username / group $group.\n}
608 . qq{The file couldn't be found in $RT::LocalEtcPath and $RT::EtcPath.\n$@};
613 RT couldn't load RT config file %s as:
617 The file is owned by user %s and group %s.
619 This usually means that the user/group your webserver is running
620 as cannot read the file. Be careful not to make the permissions
621 on this file too liberal, because it contains database passwords.
622 You may need to put the webserver user in the appropriate group
623 (%s) or change permissions be able to run succesfully.
626 my $fileusername = getpwuid($fileuid);
627 my $filegroup = getgrgid($filegid);
628 my $errormessage = sprintf( $message,
629 $file_path, $fileusername, $filegroup, $filegroup );
630 die "$errormessage\n$@";
637 foreach my $o ( grep $META{$_}{'PostLoadCheck'}, $self->Options( Overridable => undef ) ) {
638 $META{$o}->{'PostLoadCheck'}->( $self, $self->Get($o) );
644 Returns list of config files found in local etc, plugins' etc
645 and main etc directories.
653 foreach my $path ( $RT::LocalEtcPath, RT->PluginDirs('etc'), $RT::EtcPath ) {
654 my $mask = File::Spec->catfile( $path, "*_Config.pm" );
655 my @files = glob $mask;
656 @files = grep !/^RT_Config\.pm$/,
657 grep $_ && /^\w+_Config\.pm$/,
658 map { s/^.*[\\\/]//; $_ } @files;
659 push @configs, sort @files;
663 @configs = grep !$seen{$_}++, @configs;
669 Takes name of the option as argument and returns its current value.
671 In the case of a user-overridable option, first checks the user's
672 preferences before looking for site-wide configuration.
674 Returns values from RT_SiteConfig, RT_Config and then the %META hash
675 of configuration variables's "Default" for this config variable,
678 Returns different things in scalar and array contexts. For scalar
679 options it's not that important, however for arrays and hash it's.
680 In scalar context returns references to arrays and hashes.
682 Use C<scalar> perl's op to force context, especially when you use
683 C<(..., Argument => RT->Config->Get('ArrayOpt'), ...)>
684 as perl's '=>' op doesn't change context of the right hand argument to
685 scalar. Instead use C<(..., Argument => scalar RT->Config->Get('ArrayOpt'), ...)>.
687 It's also important for options that have no default value(no default
688 in F<etc/RT_Config.pm>). If you don't force scalar context then you'll
689 get empty list and all your named args will be messed up. For example
690 C<(arg1 => 1, arg2 => RT->Config->Get('OptionDoesNotExist'), arg3 => 3)>
691 will result in C<(arg1 => 1, arg2 => 'arg3', 3)> what is most probably
692 unexpected, or C<(arg1 => 1, arg2 => RT->Config->Get('ArrayOption'), arg3 => 3)>
693 will result in C<(arg1 => 1, arg2 => 'element of option', 'another_one' => ..., 'arg3', 3)>.
698 my ( $self, $name, $user ) = @_;
701 if ( $user && $user->id && $META{$name}->{'Overridable'} ) {
702 $user = $user->UserObj if $user->isa('RT::CurrentUser');
703 my $prefs = $user->Preferences($RT::System);
704 $res = $prefs->{$name} if $prefs;
706 $res = $OPTIONS{$name} unless defined $res;
707 $res = $META{$name}->{'Default'} unless defined $res;
708 return $self->_ReturnValue( $res, $META{$name}->{'Type'} || 'SCALAR' );
713 Set option's value to new value. Takes name of the option and new value.
716 The new value should be scalar, array or hash depending on type of the option.
717 If the option is not defined in meta or the default RT config then it is of
723 my ( $self, $name ) = ( shift, shift );
725 my $old = $OPTIONS{$name};
726 my $type = $META{$name}->{'Type'} || 'SCALAR';
727 if ( $type eq 'ARRAY' ) {
728 $OPTIONS{$name} = [@_];
729 { no warnings 'once'; no strict 'refs'; @{"RT::$name"} = (@_); }
730 } elsif ( $type eq 'HASH' ) {
731 $OPTIONS{$name} = {@_};
732 { no warnings 'once'; no strict 'refs'; %{"RT::$name"} = (@_); }
734 $OPTIONS{$name} = shift;
735 {no warnings 'once'; no strict 'refs'; ${"RT::$name"} = $OPTIONS{$name}; }
737 $META{$name}->{'Type'} = $type;
738 return $self->_ReturnValue( $old, $type );
742 my ( $self, $res, $type ) = @_;
743 return $res unless wantarray;
745 if ( $type eq 'ARRAY' ) {
746 return @{ $res || [] };
747 } elsif ( $type eq 'HASH' ) {
748 return %{ $res || {} };
766 unless ( $args{'File'} ) {
767 ( $args{'Package'}, $args{'File'}, $args{'Line'} ) = caller(1);
770 my $opt = $args{'Option'};
773 my $name = $self->__GetNameByRef($opt);
779 $type = $META{$name}->{'Type'} || 'SCALAR';
782 # if option is already set we have to check where
783 # it comes from and may be ignore it
784 if ( exists $OPTIONS{$name} ) {
785 if ( $args{'SiteConfig'} && $args{'Extension'} ) {
786 # if it's site config of an extension then it can only
787 # override options that came from its main config
788 if ( $args{'Extension'} ne $META{$name}->{'Source'}{'Extension'} ) {
789 my %source = %{ $META{$name}->{'Source'} };
791 "Change of config option '$name' at $args{'File'} line $args{'Line'} has been ignored."
792 ." This option earlier has been set in $source{'File'} line $source{'Line'}."
793 ." To overide this option use ". ($source{'Extension'}||'RT')
798 } elsif ( !$args{'SiteConfig'} && $META{$name}->{'Source'}{'SiteConfig'} ) {
799 # if it's core config then we can override any option that came from another
800 # core config, but not site config
802 my %source = %{ $META{$name}->{'Source'} };
803 if ( $source{'Extension'} ne $args{'Extension'} ) {
804 # as a site config is loaded earlier then its base config
805 # then we warn only on different extensions, for example
806 # RTIR's options is set in main site config or RTFM's
808 "Change of config option '$name' at $args{'File'} line $args{'Line'} has been ignored."
809 ." It may be ok, but we want you to be aware."
810 ." This option has been set earlier in $source{'File'} line $source{'Line'}."
818 $META{$name}->{'Type'} = $type;
819 foreach (qw(Package File Line SiteConfig Extension)) {
820 $META{$name}->{'Source'}->{$_} = $args{$_};
822 $self->Set( $name, @{ $args{'Value'} } );
834 if ( !$pack && $last_pack ) {
835 my $tmp = $self->__GetNameByRef( $ref, $last_pack );
839 $pack .= '::' unless substr( $pack, -2 ) eq '::';
850 # scan $pack's nametable(hash)
851 foreach my $k ( keys %{$pack} ) {
853 # hash for main:: has reference on itself
854 next if $k eq 'main::';
856 # if entry has trailing '::' then
857 # it is link to other name space
859 $name = $self->__GetNameByRef( $ref, $k );
860 return $name if $name;
863 # entry of the table with references to
864 # SCALAR, ARRAY... and other types with
866 my $entry = ${$pack}{$k};
869 # get entry for type we are looking for
870 # XXX skip references to scalars or other references.
871 # Otherwie 5.10 goes boom. maybe we should skip any
873 next if ref($entry) eq 'SCALAR' || ref($entry) eq 'REF';
874 my $entry_ref = *{$entry}{ ref($ref) };
875 next unless $entry_ref;
877 # if references are equal then we've found
878 if ( $entry_ref == $ref ) {
880 return ( $ref_sym{ ref($ref) } || '*' ) . $pack . $k;
895 return $META{ $_[1] };
903 map $_->{'Section'} || 'General',
910 my %args = ( Section => undef, Overridable => 1, Sorted => 1, @_ );
911 my @res = keys %META;
913 @res = grep( ( $META{$_}->{'Section'} || 'General' ) eq $args{'Section'},
915 ) if defined $args{'Section'};
917 if ( defined $args{'Overridable'} ) {
919 = grep( ( $META{$_}->{'Overridable'} || 0 ) == $args{'Overridable'},
923 if ( $args{'Sorted'} ) {
925 ($META{$a}->{SortOrder}||9999) <=> ($META{$b}->{SortOrder}||9999)
929 @res = sort { $a cmp $b } @res;
934 RT::Base->_ImportOverlays();