Rubber-stamped by Brady Eidson.
[webbrowser.git] / BugsSite / editparams.cgi
blobd16c90be3788ec39acde3cf41e3015eb4ed2c47c
1 #!/usr/bin/env perl -wT
2 # -*- Mode: perl; indent-tabs-mode: nil -*-
4 # The contents of this file are subject to the Mozilla Public
5 # License Version 1.1 (the "License"); you may not use this file
6 # except in compliance with the License. You may obtain a copy of
7 # the License at http://www.mozilla.org/MPL/
9 # Software distributed under the License is distributed on an "AS
10 # IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
11 # implied. See the License for the specific language governing
12 # rights and limitations under the License.
14 # The Original Code is the Bugzilla Bug Tracking System.
16 # The Initial Developer of the Original Code is Netscape Communications
17 # Corporation. Portions created by Netscape are
18 # Copyright (C) 1998 Netscape Communications Corporation. All
19 # Rights Reserved.
21 # Contributor(s): Terry Weissman <terry@mozilla.org>
22 # J. Paul Reed <preed@sigkill.com>
23 # Frédéric Buclin <LpSolit@gmail.com>
25 use strict;
26 use lib qw(. lib);
28 use Bugzilla;
29 use Bugzilla::Constants;
30 use Bugzilla::Config qw(:admin);
31 use Bugzilla::Config::Common;
32 use Bugzilla::Hook;
33 use Bugzilla::Util;
34 use Bugzilla::Error;
35 use Bugzilla::Token;
36 use Bugzilla::User;
37 use Bugzilla::User::Setting;
38 use Bugzilla::Status;
40 my $user = Bugzilla->login(LOGIN_REQUIRED);
41 my $cgi = Bugzilla->cgi;
42 my $template = Bugzilla->template;
43 my $vars = {};
45 print $cgi->header();
47 $user->in_group('tweakparams')
48 || ThrowUserError("auth_failure", {group => "tweakparams",
49 action => "access",
50 object => "parameters"});
52 my $action = trim($cgi->param('action') || '');
53 my $token = $cgi->param('token');
54 my $current_panel = $cgi->param('section') || 'core';
55 $current_panel =~ /^([A-Za-z0-9_-]+)$/;
56 $current_panel = $1;
58 my $current_module;
59 my @panels = ();
60 my $param_panels = Bugzilla::Config::param_panels();
61 foreach my $panel (keys %$param_panels) {
62 my $module = $param_panels->{$panel};
63 eval("require $module") || die $@;
64 my @module_param_list = "$module"->get_param_list();
65 my $item = { name => lc($panel),
66 current => ($current_panel eq lc($panel)) ? 1 : 0,
67 param_list => \@module_param_list,
68 sortkey => eval "\$${module}::sortkey;"
70 push(@panels, $item);
71 $current_module = $panel if ($current_panel eq lc($panel));
74 $vars->{panels} = \@panels;
76 if ($action eq 'save' && $current_module) {
77 check_token_data($token, 'edit_parameters');
78 my @changes = ();
79 my @module_param_list = "$param_panels->{$current_module}"->get_param_list();
81 foreach my $i (@module_param_list) {
82 my $name = $i->{'name'};
83 my $value = $cgi->param($name);
85 if (defined $cgi->param("reset-$name")) {
86 $value = $i->{'default'};
87 } else {
88 if ($i->{'type'} eq 'm') {
89 # This simplifies the code below
90 $value = [ $cgi->param($name) ];
91 } else {
92 # Get rid of windows/mac-style line endings.
93 $value =~ s/\r\n?/\n/g;
94 # assume single linefeed is an empty string
95 $value =~ s/^\n$//;
99 my $changed;
100 if ($i->{'type'} eq 'm') {
101 my @old = sort @{Bugzilla->params->{$name}};
102 my @new = sort @$value;
103 if (scalar(@old) != scalar(@new)) {
104 $changed = 1;
105 } else {
106 $changed = 0; # Assume not changed...
107 for (my $cnt = 0; $cnt < scalar(@old); ++$cnt) {
108 if ($old[$cnt] ne $new[$cnt]) {
109 # entry is different, therefore changed
110 $changed = 1;
111 last;
115 } else {
116 $changed = ($value eq Bugzilla->params->{$name})? 0 : 1;
119 if ($changed) {
120 if (exists $i->{'checker'}) {
121 my $ok = $i->{'checker'}->($value, $i);
122 if ($ok ne "") {
123 ThrowUserError('invalid_parameter', { name => $name, err => $ok });
125 } elsif ($name eq 'globalwatchers') {
126 # can't check this as others, as Bugzilla::Config::Common
127 # can not use Bugzilla::User
128 foreach my $watcher (split(/[,\s]+/, $value)) {
129 ThrowUserError(
130 'invalid_parameter',
131 { name => $name, err => "no such user $watcher" }
132 ) unless login_to_id($watcher);
135 push(@changes, $name);
136 SetParam($name, $value);
137 if (($name eq "shutdownhtml") && ($value ne "")) {
138 $vars->{'shutdown_is_active'} = 1;
140 if ($name eq 'duplicate_or_move_bug_status') {
141 Bugzilla::Status::add_missing_bug_status_transitions($value);
146 $vars->{'message'} = 'parameters_updated';
147 $vars->{'param_changed'} = \@changes;
149 write_params();
150 delete_token($token);
153 $vars->{'token'} = issue_session_token('edit_parameters');
155 $template->process("admin/params/editparams.html.tmpl", $vars)
156 || ThrowTemplateError($template->error());