process_bug.cgi 25.1 KB
Newer Older
1
#!/usr/bin/perl -wT
2
# -*- Mode: perl; indent-tabs-mode: nil -*-
terry%netscape.com's avatar
terry%netscape.com committed
3
#
4 5 6 7 8 9 10 11 12 13
# The contents of this file are subject to the Mozilla Public
# License Version 1.1 (the "License"); you may not use this file
# except in compliance with the License. You may obtain a copy of
# the License at http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS
# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
# implied. See the License for the specific language governing
# rights and limitations under the License.
#
terry%netscape.com's avatar
terry%netscape.com committed
14
# The Original Code is the Bugzilla Bug Tracking System.
15
#
terry%netscape.com's avatar
terry%netscape.com committed
16
# The Initial Developer of the Original Code is Netscape Communications
17 18 19 20
# Corporation. Portions created by Netscape are
# Copyright (C) 1998 Netscape Communications Corporation. All
# Rights Reserved.
#
terry%netscape.com's avatar
terry%netscape.com committed
21
# Contributor(s): Terry Weissman <terry@mozilla.org>
22
#                 Dan Mosedale <dmose@mozilla.org>
23
#                 Dave Miller <justdave@syndicomm.com>
24
#                 Christopher Aillon <christopher@aillon.com>
25
#                 Myk Melez <myk@mozilla.org>
26
#                 Jeff Hedlund <jeff.hedlund@matrixsi.com>
27
#                 Frédéric Buclin <LpSolit@gmail.com>
28
#                 Lance Larsh <lance.larsh@oracle.com>
29
#                 Akamai Technologies <bugzilla-dev@akamai.com>
30
#                 Max Kanat-Alexander <mkanat@bugzilla.org>
terry%netscape.com's avatar
terry%netscape.com committed
31

32 33 34 35 36 37 38 39 40 41 42 43
# Implementation notes for this file:
#
# 1) the 'id' form parameter is validated early on, and if it is not a valid
# bugid an error will be reported, so it is OK for later code to simply check
# for a defined form 'id' value, and it can assume a valid bugid.
#
# 2) If the 'id' form parameter is not defined (after the initial validation),
# then we are processing multiple bugs, and @idlist will contain the ids.
#
# 3) If we are processing just the one id, then it is stored in @idlist for
# later processing.

44 45
use strict;

46
use lib qw(. lib);
47

48
use Bugzilla;
49
use Bugzilla::Constants;
50
use Bugzilla::Bug;
51
use Bugzilla::BugMail;
52
use Bugzilla::Mailer;
53
use Bugzilla::User;
54
use Bugzilla::Util;
55
use Bugzilla::Error;
56
use Bugzilla::Field;
57
use Bugzilla::Product;
58
use Bugzilla::Component;
59
use Bugzilla::Keyword;
60
use Bugzilla::Flag;
61
use Bugzilla::Status;
62
use Bugzilla::Token;
63

64 65
use Storable qw(dclone);

66
my $user = Bugzilla->login(LOGIN_REQUIRED);
67

68
my $cgi = Bugzilla->cgi;
69
my $dbh = Bugzilla->dbh;
70
my $template = Bugzilla->template;
71
my $vars = {};
72
$vars->{'use_keywords'} = 1 if Bugzilla::Keyword::keyword_count();
73

74 75 76 77
######################################################################
# Subroutines
######################################################################

78 79 80 81 82 83 84 85 86 87 88 89 90 91
# Used to send email when an update is done.
sub send_results {
    my ($bug_id, $vars) = @_;
    my $template = Bugzilla->template;
    if (Bugzilla->usage_mode == USAGE_MODE_EMAIL) {
         Bugzilla::BugMail::Send($bug_id, $vars->{'mailrecipients'});
    }
    else {
        $template->process("bug/process/results.html.tmpl", $vars)
            || ThrowTemplateError($template->error());
    }
    $vars->{'header_done'} = 1;
}

92
# Tells us whether or not a field should be changed by process_bug.
93
sub should_set {
94
    # check_defined is used for fields where there's another field
95 96 97 98
    # whose name starts with "defined_" and then the field name--it's used
    # to know when we did things like empty a multi-select or deselect
    # a checkbox.
    my ($field, $check_defined) = @_;
99
    my $cgi = Bugzilla->cgi;
100 101
    if ( defined $cgi->param($field) 
         || ($check_defined && defined $cgi->param("defined_$field")) )
102 103 104 105 106 107
    {
        return 1;
    }
    return 0;
}

108 109 110 111
######################################################################
# Begin Data/Security Validation
######################################################################

112 113
# Create a list of objects for all bugs being modified in this request.
my @bug_objects;
114
if (defined $cgi->param('id')) {
115 116 117
  my $bug = Bugzilla::Bug->check(scalar $cgi->param('id'));
  $cgi->param('id', $bug->id);
  push(@bug_objects, $bug);
118
} else {
119
    foreach my $i ($cgi->param()) {
120
        if ($i =~ /^id_([1-9][0-9]*)/) {
121
            my $id = $1;
122
            push(@bug_objects, Bugzilla::Bug->check($id));
123
        }
124 125 126
    }
}

127
# Make sure there are bugs to process.
128
scalar(@bug_objects) || ThrowUserError("no_bugs_chosen", {action => 'modify'});
129

130
my $first_bug = $bug_objects[0]; # Used when we're only updating a single bug.
131

132 133 134 135
# Delete any parameter set to 'dontchange'.
if (defined $cgi->param('dontchange')) {
    foreach my $name ($cgi->param) {
        next if $name eq 'dontchange'; # But don't delete dontchange itself!
136 137
        # Skip ones we've already deleted (such as "defined_$name").
        next if !defined $cgi->param($name);
138 139
        if ($cgi->param($name) eq $cgi->param('dontchange')) {
            $cgi->delete($name);
140
            $cgi->delete("defined_$name");
141 142
        }
    }
143 144
}

145 146
# do a match on the fields if applicable

147 148
# The order of these function calls is important, as Flag::validate
# assumes User::match_field has ensured that the values
149
# in the requestee fields are legitimate user email addresses.
150
&Bugzilla::User::match_field($cgi, {
151 152 153 154
    'qa_contact'                => { 'type' => 'single' },
    'newcc'                     => { 'type' => 'multi'  },
    'masscc'                    => { 'type' => 'multi'  },
    'assigned_to'               => { 'type' => 'single' },
155
    '^requestee(_type)?-(\d+)$' => { 'type' => 'multi'  },
156
});
157 158 159

# Validate flags in all cases. validate() should not detect any
# reference to flags if $cgi->param('id') is undefined.
160
Bugzilla::Flag::validate($cgi->param('id'));
161

162
print $cgi->header() unless Bugzilla->usage_mode == USAGE_MODE_EMAIL;
163

164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
# Check for a mid-air collision. Currently this only works when updating
# an individual bug.
if (defined $cgi->param('delta_ts')
    && $cgi->param('delta_ts') ne $first_bug->delta_ts)
{
    ($vars->{'operations'}) =
        Bugzilla::Bug::GetBugActivity($first_bug->id, undef,
                                      scalar $cgi->param('delta_ts'));

    $vars->{'title_tag'} = "mid_air";
    
    ThrowCodeError('undefined_field', { field => 'longdesclength' })
        if !defined $cgi->param('longdesclength');

    $vars->{'start_at'} = $cgi->param('longdesclength');
    # Always sort midair collision comments oldest to newest,
    # regardless of the user's personal preference.
    $vars->{'comments'} = Bugzilla::Bug::GetComments($first_bug->id,
                                                     "oldest_to_newest");
    $vars->{'bug'} = $first_bug;
184 185
    # The token contains the old delta_ts. We need a new one.
    $cgi->param('token', issue_hash_token([$first_bug->id, $first_bug->delta_ts]));
186 187 188 189 190
    
    # Warn the user about the mid-air collision and ask them what to do.
    $template->process("bug/process/midair.html.tmpl", $vars)
      || ThrowTemplateError($template->error());
    exit;
191
}
192

193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
# We couldn't do this check earlier as we first had to validate bug IDs
# and display the mid-air collision page if delta_ts changed.
# If we do a mass-change, we use session tokens.
my $token = $cgi->param('token');

if ($cgi->param('id')) {
    check_hash_token($token, [$first_bug->id, $first_bug->delta_ts]);
}
else {
    check_token_data($token, 'buglist_mass_change', 'query.cgi');
}

######################################################################
# End Data/Security Validation
######################################################################

209 210
$vars->{'title_tag'} = "bug_processed";

211
# Set up the vars for navigational <link> elements
212
my @bug_list;
213
if ($cgi->cookie("BUGLIST")) {
214
    @bug_list = split(/:/, $cgi->cookie("BUGLIST"));
215 216 217
    $vars->{'bug_list'} = \@bug_list;
}

218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
my ($action, $next_bug);
if (defined $cgi->param('id')) {
    $action = Bugzilla->user->settings->{'post_bug_submit_action'}->{'value'};

    if ($action eq 'next_bug') {
        my $cur = lsearch(\@bug_list, $cgi->param('id'));
        if ($cur >= 0 && $cur < $#bug_list) {
            $next_bug = $bug_list[$cur + 1];
            # No need to check whether the user can see the bug or not.
            # All we want is its ID. An error will be thrown later
            # if the user cannot see it.
            $vars->{'bug'} = {bug_id => $next_bug};
        }
    }
    # Include both action = 'same_bug' and 'nothing'.
    else {
        $vars->{'bug'} = {bug_id => $cgi->param('id')};
    }
}
else {
    # param('id') is not defined when changing multiple bugs at once.
    $action = 'nothing';
}

242 243 244 245 246 247
# For each bug, we have to check if the user can edit the bug the product
# is currently in, before we allow them to change anything.
foreach my $bug (@bug_objects) {
    if (!Bugzilla->user->can_edit_product($bug->product_obj->id) ) {
        ThrowUserError("product_edit_denied",
                      { product => $bug->product });
248
    }
249
}
250

251 252 253 254 255 256 257 258 259 260
my $product_change;
foreach my $bug (@bug_objects) {
    my $args;
    if (should_set('product')) {
        $args->{product} = scalar $cgi->param('product');
        $args->{component} = scalar $cgi->param('component');
        $args->{version} = scalar $cgi->param('version');
        $args->{target_milestone} = scalar $cgi->param('target_milestone');
        $args->{confirm_product_change} =  scalar $cgi->param('confirm_product_change');
        $args->{other_bugs} = \@bug_objects;
261
    }
262 263

    foreach my $group (@{$bug->product_obj->groups_valid}) {
264 265 266 267
        my $gid = $group->id;
        if (should_set("bit-$gid", 1)) {
            # Check ! first to avoid having to check defined below.
            if (!$cgi->param("bit-$gid")) {
268
                push (@{$args->{remove_group}}, $gid);
269 270 271 272
            }
            # "== 1" is important because mass-change uses -1 to mean
            # "don't change this restriction"
            elsif ($cgi->param("bit-$gid") == 1) {
273
               push (@{$args->{add_group}}, $gid);
274 275
            }
        }
terry%netscape.com's avatar
terry%netscape.com committed
276
    }
277 278 279 280

    # this will be deleted later when code moves to $bug->set_all
    my $changed = $bug->set_all($args);
    $product_change ||= $changed;
terry%netscape.com's avatar
terry%netscape.com committed
281 282
}

283 284 285 286 287 288
if ($cgi->param('id') && (defined $cgi->param('dependson')
                          || defined $cgi->param('blocked')) )
{
    $first_bug->set_dependencies(scalar $cgi->param('dependson'),
                                 scalar $cgi->param('blocked'));
}
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
elsif (should_set('dependson') || should_set('blocked')) {
    foreach my $bug (@bug_objects) {
        my %temp_deps;
        foreach my $type (qw(dependson blocked)) {
            $temp_deps{$type} = { map { $_ => 1 } @{$bug->$type} };
            if (should_set($type) && $cgi->param($type . '_action') =~ /^(add|remove)$/) {
                foreach my $id (split(/[,\s]+/, $cgi->param($type))) {
                    if ($cgi->param($type . '_action') eq 'remove') {
                        delete $temp_deps{$type}{$id};
                    }
                    else {
                        $temp_deps{$type}{$id} = 1;
                    }
                }
            }
        }
        $bug->set_dependencies([ keys %{$temp_deps{'dependson'}} ], [ keys %{$temp_deps{'blocked'}} ]);
    }
307 308 309 310 311 312 313 314 315 316 317 318
}

my $any_keyword_changes;
if (defined $cgi->param('keywords')) {
    foreach my $b (@bug_objects) {
        my $return =
            $b->modify_keywords(scalar $cgi->param('keywords'),
                                scalar $cgi->param('keywordaction'));
        $any_keyword_changes ||= $return;
    }
}

319 320 321
# Component, target_milestone, and version are in here just in case
# the 'product' field wasn't defined in the CGI. It doesn't hurt to set
# them twice.
322 323 324 325 326 327
my @set_fields = qw(op_sys rep_platform priority bug_severity
                    component target_milestone version
                    bug_file_loc status_whiteboard short_desc
                    deadline remaining_time estimated_time);
push(@set_fields, 'assigned_to') if !$cgi->param('set_default_assignee');
push(@set_fields, 'qa_contact')  if !$cgi->param('set_default_qa_contact');
328
my @custom_fields = Bugzilla->active_custom_fields;
329

330 331 332 333 334 335 336
my %methods = (
    bug_severity => 'set_severity',
    rep_platform => 'set_platform',
    short_desc   => 'set_summary',
    bug_file_loc => 'set_url',
);
foreach my $b (@bug_objects) {
337 338 339 340 341 342 343
    if (should_set('comment') || $cgi->param('work_time')) {
        # Add a comment as needed to each bug. This is done early because
        # there are lots of things that want to check if we added a comment.
        $b->add_comment(scalar($cgi->param('comment')),
            { isprivate => scalar $cgi->param('commentprivacy'),
              work_time => scalar $cgi->param('work_time') });
    }
344
    foreach my $field_name (@set_fields) {
345
        if (should_set($field_name)) {
346 347 348 349 350
            my $method = $methods{$field_name};
            $method ||= "set_" . $field_name;
            $b->$method($cgi->param($field_name));
        }
    }
351 352
    $b->reset_assigned_to if $cgi->param('set_default_assignee');
    $b->reset_qa_contact  if $cgi->param('set_default_qa_contact');
353

354 355 356 357 358 359 360 361
    if (should_set('see_also')) {
        my @see_also = split(',', $cgi->param('see_also'));
        $b->add_see_also($_) foreach @see_also;
    }
    if (should_set('remove_see_also')) {
        $b->remove_see_also($_) foreach $cgi->param('remove_see_also')
    }

362 363 364 365 366 367
    # And set custom fields.
    foreach my $field (@custom_fields) {
        my $fname = $field->name;
        if (should_set($fname, 1)) {
            $b->set_custom_field($field, [$cgi->param($fname)]);
        }
368
    }
369 370
}

371
# Certain changes can only happen on individual bugs, never on mass-changes.
372
if (defined $cgi->param('id')) {
373 374 375
    # Since aliases are unique (like bug numbers), they can only be changed
    # for one bug at a time.
    if (Bugzilla->params->{"usebugaliases"} && defined $cgi->param('alias')) {
376
        $first_bug->set_alias($cgi->param('alias'));
377 378 379
    }

    # reporter_accessible and cclist_accessible--these are only set if
380 381
    # the user can change them and they appear on the page.
    if (should_set('cclist_accessible', 1)) {
382
        $first_bug->set_cclist_accessible($cgi->param('cclist_accessible'))
383 384
    }
    if (should_set('reporter_accessible', 1)) {
385
        $first_bug->set_reporter_accessible($cgi->param('reporter_accessible'))
386
    }
387 388 389 390 391 392
    
    # You can only mark/unmark comments as private on single bugs. If
    # you're not in the insider group, this code won't do anything.
    foreach my $field (grep(/^defined_isprivate/, $cgi->param())) {
        $field =~ /(\d+)$/;
        my $comment_id = $1;
393 394
        $first_bug->set_comment_is_private($comment_id,
                                           $cgi->param("isprivate_$comment_id"));
395 396
    }
}
397

398 399 400 401
# We need to check the addresses involved in a CC change before we touch 
# any bugs. What we'll do here is formulate the CC data into two arrays of
# users involved in this CC change.  Then those arrays can be used later 
# on for the actual change.
402
my (@cc_add, @cc_remove);
403 404 405 406
if (defined $cgi->param('newcc')
    || defined $cgi->param('addselfcc')
    || defined $cgi->param('removecc')
    || defined $cgi->param('masscc')) {
407
        
408 409 410
    # If masscc is defined, then we came from buglist and need to either add or
    # remove cc's... otherwise, we came from bugform and may need to do both.
    my ($cc_add, $cc_remove) = "";
411 412 413 414 415
    if (defined $cgi->param('masscc')) {
        if ($cgi->param('ccaction') eq 'add') {
            $cc_add = join(' ',$cgi->param('masscc'));
        } elsif ($cgi->param('ccaction') eq 'remove') {
            $cc_remove = join(' ',$cgi->param('masscc'));
416 417
        }
    } else {
418
        $cc_add = join(' ',$cgi->param('newcc'));
419 420
        # We came from bug_form which uses a select box to determine what cc's
        # need to be removed...
421 422
        if (defined $cgi->param('removecc') && $cgi->param('cc')) {
            $cc_remove = join (",", $cgi->param('cc'));
423 424 425
        }
    }

426 427 428 429 430 431 432 433
    push(@cc_add, split(/[\s,]+/, $cc_add)) if $cc_add;
    push(@cc_add, Bugzilla->user) if $cgi->param('addselfcc');

    push(@cc_remove, split(/[\s,]+/, $cc_remove)) if $cc_remove;
}

foreach my $b (@bug_objects) {
    $b->remove_cc($_) foreach @cc_remove;
434
    $b->add_cc($_) foreach @cc_add;
435 436 437 438 439 440 441 442 443
    # Theoretically you could move a product without ever specifying
    # a new assignee or qa_contact, or adding/removing any CCs. So,
    # we have to check that the current assignee, qa, and CCs are still
    # valid if we've switched products, under strict_isolation. We can only
    # do that here. There ought to be some better way to do this,
    # architecturally, but I haven't come up with it.
    if ($product_change) {
        $b->_check_strict_isolation();
    }
444 445
}

446 447
my $move_action = $cgi->param('action') || '';
if ($move_action eq Bugzilla->params->{'move-button-text'}) {
448 449 450 451 452
    Bugzilla->params->{'move-enabled'} || ThrowUserError("move_bugs_disabled");

    $user->is_mover || ThrowUserError("auth_failure", {action => 'move',
                                                       object => 'bugs'});

453
    $dbh->bz_start_transaction();
454 455 456

    # First update all moved bugs.
    foreach my $bug (@bug_objects) {
457
        $bug->add_comment('', { type => CMT_MOVED_TO, extra_data => $user->login });
458
    }
459 460 461
    # Don't export the new status and resolution. We want the current ones.
    local $Storable::forgive_me = 1;
    my $bugs = dclone(\@bug_objects);
462 463

    my $new_status = Bugzilla->params->{'duplicate_or_move_bug_status'};
464
    foreach my $bug (@bug_objects) {
465
        $bug->set_status($new_status, {resolution => 'MOVED', moving => 1});
466
    }
467
    $_->update() foreach @bug_objects;
468
    $dbh->bz_commit_transaction();
469 470

    # Now send emails.
471
    foreach my $bug (@bug_objects) {
472
        $vars->{'mailrecipients'} = { 'changer' => $user->login };
473
        $vars->{'id'} = $bug->id;
474
        $vars->{'type'} = "move";
475
        send_results($bug->id, $vars);
476
    }
477 478 479 480 481 482 483
    # Prepare and send all data about these bugs to the new database
    my $to = Bugzilla->params->{'move-to-address'};
    $to =~ s/@/\@/;
    my $from = Bugzilla->params->{'moved-from-address'};
    $from =~ s/@/\@/;
    my $msg = "To: $to\n";
    $msg .= "From: Bugzilla <" . $from . ">\n";
484 485
    $msg .= "Subject: Moving bug(s) " . join(', ', map($_->id, @bug_objects))
            . "\n\n";
486 487 488 489 490 491

    my @fieldlist = (Bugzilla::Bug->fields, 'group', 'long_desc',
                     'attachment', 'attachmentdata');
    my %displayfields;
    foreach (@fieldlist) {
        $displayfields{$_} = 1;
492
    }
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509

    $template->process("bug/show.xml.tmpl", { bugs => $bugs,
                                              displayfields => \%displayfields,
                                            }, \$msg)
      || ThrowTemplateError($template->error());

    $msg .= "\n";
    MessageToMTA($msg);

    # End the response page.
    unless (Bugzilla->usage_mode == USAGE_MODE_EMAIL) {
        $template->process("bug/navigate.html.tmpl", $vars)
            || ThrowTemplateError($template->error());
        $template->process("global/footer.html.tmpl", $vars)
            || ThrowTemplateError($template->error());
    }
    exit;
510
}
511

512

513 514 515 516 517
# You cannot mark bugs as duplicates when changing several bugs at once
# (because currently there is no way to check for duplicate loops in that
# situation).
if (!$cgi->param('id') && $cgi->param('dup_id')) {
    ThrowUserError('dupe_not_allowed');
518 519
}

520 521 522 523
# Set the status, resolution, and dupe_of (if needed). This has to be done
# down here, because the validity of status changes depends on other fields,
# such as Target Milestone.
foreach my $b (@bug_objects) {
524 525 526 527 528 529 530 531 532 533 534 535 536
    if (should_set('bug_status')) {
        $b->set_status(
            scalar $cgi->param('bug_status'),
            {resolution =>  scalar $cgi->param('resolution'),
                dupe_of => scalar $cgi->param('dup_id')}
            );
    }
    elsif (should_set('resolution')) {
       $b->set_resolution(scalar $cgi->param('resolution'), 
                          {dupe_of => scalar $cgi->param('dup_id')});
    }
    elsif (should_set('dup_id')) {
        $b->set_dup_id(scalar $cgi->param('dup_id'));
537 538 539
    }
}

540 541 542 543
##############################
# Do Actual Database Updates #
##############################
foreach my $bug (@bug_objects) {
544
    $dbh->bz_start_transaction();
545

546
    my $timestamp = $dbh->selectrow_array(q{SELECT NOW()});
547
    my $changes = $bug->update($timestamp);
548 549 550 551 552 553 554 555

    my %notify_deps;
    if ($changes->{'bug_status'}) {
        my ($old_status, $new_status) = @{ $changes->{'bug_status'} };
        
        # If this bug has changed from opened to closed or vice-versa,
        # then all of the bugs we block need to be notified.
        if (is_open_state($old_status) ne is_open_state($new_status)) {
556
            $notify_deps{$_} = 1 foreach (@{$bug->blocked});
557 558 559 560 561 562 563 564 565
        }
        
        # We may have zeroed the remaining time, if we moved into a closed
        # status, so we should inform the user about that.
        if (!is_open_state($new_status) && $changes->{'remaining_time'}) {
            $vars->{'message'} = "remaining_time_zeroed"
              if Bugzilla->user->in_group(Bugzilla->params->{'timetrackinggroup'});
        }
    }
566

567 568 569 570 571 572 573 574 575 576 577
    # To get a list of all changed dependencies, convert the "changes" arrays
    # into a long string, then collapse that string into unique numbers in
    # a hash.
    my $all_changed_deps = join(', ', @{ $changes->{'dependson'} || [] });
    $all_changed_deps = join(', ', @{ $changes->{'blocked'} || [] },
                                   $all_changed_deps);
    my %changed_deps = map { $_ => 1 } split(', ', $all_changed_deps);
    # When clearning one field (say, blocks) and filling in the other
    # (say, dependson), an empty string can get into the hash and cause
    # an error later.
    delete $changed_deps{''};
578

579 580
    # @msgs will store emails which have to be sent to voters, if any.
    my @msgs;
581 582 583
    if ($changes->{'product'}) {
        # If some votes have been removed, RemoveVotes() returns
        # a list of messages to send to voters.
584 585
        # We delay the sending of these messages till changes are committed.
        @msgs = RemoveVotes($bug->id, 0, 'votes_bug_moved');
586
        CheckIfVotedConfirmed($bug->id, Bugzilla->user->id);
terry%netscape.com's avatar
terry%netscape.com committed
587
    }
588

589
    # Set and update flags.
590
    Bugzilla::Flag->process($bug, undef, $timestamp, $vars);
591

592
    $dbh->bz_commit_transaction();
593

594 595 596 597
    ###############
    # Send Emails #
    ###############

598
    # Now is a good time to send email to voters.
599
    foreach my $msg (@msgs) {
600
        MessageToMTA($msg);
601 602
    }

603 604 605
    my $old_qa  = $changes->{'qa_contact'}  ? $changes->{'qa_contact'}->[0] : '';
    my $old_own = $changes->{'assigned_to'} ? $changes->{'assigned_to'}->[0] : '';
    my $old_cc  = $changes->{cc}            ? $changes->{cc}->[0] : '';
606
    $vars->{'mailrecipients'} = {
607
        cc        => [split(/[\s,]+/, $old_cc)],
608 609
        owner     => $old_own,
        qacontact => $old_qa,
610
        changer   => Bugzilla->user->login };
611

612
    $vars->{'id'} = $bug->id;
613
    $vars->{'type'} = "bug";
614 615 616
    
    # Let the user know the bug was changed and who did and didn't
    # receive email about the change.
617
    send_results($bug->id, $vars);
618
 
619 620 621 622
    # If the bug was marked as a duplicate, we need to notify users on the
    # other bug of any changes to that bug.
    my $new_dup_id = $changes->{'dup_id'} ? $changes->{'dup_id'}->[1] : undef;
    if ($new_dup_id) {
623
        $vars->{'mailrecipients'} = { 'changer' => Bugzilla->user->login }; 
624

625
        $vars->{'id'} = $new_dup_id;
626 627
        $vars->{'type'} = "dupe";
        
628 629
        # Let the user know a duplication notation was added to the 
        # original bug.
630
        send_results($new_dup_id, $vars);
631 632
    }

633 634 635 636 637
    my %all_dep_changes = (%notify_deps, %changed_deps);
    foreach my $id (sort { $a <=> $b } (keys %all_dep_changes)) {
        $vars->{'mailrecipients'} = { 'changer' => Bugzilla->user->login };
        $vars->{'id'} = $id;
        $vars->{'type'} = "dep";
638

639 640 641 642 643
        # Let the user (if he is able to see the bug) know we checked to
        # see if we should email notice of this change to users with a 
        # relationship to the dependent bug and who did and didn't 
        # receive email about it.
        send_results($id, $vars);
644
    }
terry%netscape.com's avatar
terry%netscape.com committed
645 646
}

647 648 649 650 651 652
# Determine if Patch Viewer is installed, for Diff link
# (NB: Duplicate code with show_bug.cgi.)
eval {
    require PatchReader;
    $vars->{'patchviewerinstalled'} = 1;
};
653

654 655 656 657
if (Bugzilla->usage_mode == USAGE_MODE_EMAIL) {
    # Do nothing.
}
elsif ($action eq 'next_bug') {
658 659
    if ($next_bug) {
        if (detaint_natural($next_bug) && Bugzilla->user->can_see_bug($next_bug)) {
660
            my $bug = new Bugzilla::Bug($next_bug);
661
            ThrowCodeError("bug_error", { bug => $bug }) if $bug->error;
662

663 664
            $vars->{'bugs'} = [$bug];
            $vars->{'nextbug'} = $bug->bug_id;
665

666 667 668 669 670
            $template->process("bug/show.html.tmpl", $vars)
              || ThrowTemplateError($template->error());

            exit;
        }
671
    }
672
} elsif ($action eq 'same_bug') {
673
    if (Bugzilla->user->can_see_bug($cgi->param('id'))) {
674
        my $bug = new Bugzilla::Bug($cgi->param('id'));
675
        ThrowCodeError("bug_error", { bug => $bug }) if $bug->error;
676

677
        $vars->{'bugs'} = [$bug];
678

679 680
        $template->process("bug/show.html.tmpl", $vars)
          || ThrowTemplateError($template->error());
681

682 683
        exit;
    }
684 685
} elsif ($action ne 'nothing') {
    ThrowCodeError("invalid_post_bug_submit_action");
terry%netscape.com's avatar
terry%netscape.com committed
686
}
687

688
# End the response page.
689 690 691 692 693 694 695 696
unless (Bugzilla->usage_mode == USAGE_MODE_EMAIL) {
    $template->process("bug/navigate.html.tmpl", $vars)
        || ThrowTemplateError($template->error());
    $template->process("global/footer.html.tmpl", $vars)
        || ThrowTemplateError($template->error());
}

1;