editparams.cgi 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #!/usr/bin/env perl -wT
  2. # -*- Mode: perl; indent-tabs-mode: nil -*-
  3. #
  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/
  8. #
  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.
  13. #
  14. # The Original Code is the Bugzilla Bug Tracking System.
  15. #
  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.
  20. #
  21. # Contributor(s): Terry Weissman <terry@mozilla.org>
  22. # J. Paul Reed <preed@sigkill.com>
  23. # Frédéric Buclin <LpSolit@gmail.com>
  24. use strict;
  25. use lib qw(. lib);
  26. use Bugzilla;
  27. use Bugzilla::Constants;
  28. use Bugzilla::Config qw(:admin);
  29. use Bugzilla::Config::Common;
  30. use Bugzilla::Hook;
  31. use Bugzilla::Util;
  32. use Bugzilla::Error;
  33. use Bugzilla::Token;
  34. use Bugzilla::User;
  35. use Bugzilla::User::Setting;
  36. use Bugzilla::Status;
  37. my $user = Bugzilla->login(LOGIN_REQUIRED);
  38. my $cgi = Bugzilla->cgi;
  39. my $template = Bugzilla->template;
  40. my $vars = {};
  41. print $cgi->header();
  42. $user->in_group('tweakparams')
  43. || ThrowUserError("auth_failure", {group => "tweakparams",
  44. action => "access",
  45. object => "parameters"});
  46. my $action = trim($cgi->param('action') || '');
  47. my $token = $cgi->param('token');
  48. my $current_panel = $cgi->param('section') || 'core';
  49. $current_panel =~ /^([A-Za-z0-9_-]+)$/;
  50. $current_panel = $1;
  51. my $current_module;
  52. my @panels = ();
  53. my $param_panels = Bugzilla::Config::param_panels();
  54. foreach my $panel (keys %$param_panels) {
  55. my $module = $param_panels->{$panel};
  56. eval("require $module") || die $@;
  57. my @module_param_list = "$module"->get_param_list();
  58. my $item = { name => lc($panel),
  59. current => ($current_panel eq lc($panel)) ? 1 : 0,
  60. param_list => \@module_param_list,
  61. sortkey => eval "\$${module}::sortkey;"
  62. };
  63. push(@panels, $item);
  64. $current_module = $panel if ($current_panel eq lc($panel));
  65. }
  66. $vars->{panels} = \@panels;
  67. if ($action eq 'save' && $current_module) {
  68. check_token_data($token, 'edit_parameters');
  69. my @changes = ();
  70. my @module_param_list = "$param_panels->{$current_module}"->get_param_list();
  71. foreach my $i (@module_param_list) {
  72. my $name = $i->{'name'};
  73. my $value = $cgi->param($name);
  74. if (defined $cgi->param("reset-$name")) {
  75. $value = $i->{'default'};
  76. } else {
  77. if ($i->{'type'} eq 'm') {
  78. # This simplifies the code below
  79. $value = [ $cgi->param($name) ];
  80. } else {
  81. # Get rid of windows/mac-style line endings.
  82. $value =~ s/\r\n?/\n/g;
  83. # assume single linefeed is an empty string
  84. $value =~ s/^\n$//;
  85. }
  86. }
  87. my $changed;
  88. if ($i->{'type'} eq 'm') {
  89. my @old = sort @{Bugzilla->params->{$name}};
  90. my @new = sort @$value;
  91. if (scalar(@old) != scalar(@new)) {
  92. $changed = 1;
  93. } else {
  94. $changed = 0; # Assume not changed...
  95. for (my $cnt = 0; $cnt < scalar(@old); ++$cnt) {
  96. if ($old[$cnt] ne $new[$cnt]) {
  97. # entry is different, therefore changed
  98. $changed = 1;
  99. last;
  100. }
  101. }
  102. }
  103. } else {
  104. $changed = ($value eq Bugzilla->params->{$name})? 0 : 1;
  105. }
  106. if ($changed) {
  107. if (exists $i->{'checker'}) {
  108. my $ok = $i->{'checker'}->($value, $i);
  109. if ($ok ne "") {
  110. ThrowUserError('invalid_parameter', { name => $name, err => $ok });
  111. }
  112. } elsif ($name eq 'globalwatchers') {
  113. # can't check this as others, as Bugzilla::Config::Common
  114. # can not use Bugzilla::User
  115. foreach my $watcher (split(/[,\s]+/, $value)) {
  116. ThrowUserError(
  117. 'invalid_parameter',
  118. { name => $name, err => "no such user $watcher" }
  119. ) unless login_to_id($watcher);
  120. }
  121. }
  122. push(@changes, $name);
  123. SetParam($name, $value);
  124. if (($name eq "shutdownhtml") && ($value ne "")) {
  125. $vars->{'shutdown_is_active'} = 1;
  126. }
  127. if ($name eq 'duplicate_or_move_bug_status') {
  128. Bugzilla::Status::add_missing_bug_status_transitions($value);
  129. }
  130. }
  131. }
  132. $vars->{'message'} = 'parameters_updated';
  133. $vars->{'param_changed'} = \@changes;
  134. write_params();
  135. delete_token($token);
  136. }
  137. $vars->{'token'} = issue_session_token('edit_parameters');
  138. $template->process("admin/params/editparams.html.tmpl", $vars)
  139. || ThrowTemplateError($template->error());