]> git.ipfire.org Git - thirdparty/bugzilla.git/commitdiff
Bug 1313766 - Bugzilla::Bug->send_changes() should not output HTML directly
authorDylan William Hardison <dylan@hardison.net>
Wed, 14 Dec 2016 23:49:48 +0000 (18:49 -0500)
committerDylan William Hardison <dylan@hardison.net>
Wed, 14 Dec 2016 23:50:08 +0000 (18:50 -0500)
Bugzilla/Bug.pm
Bugzilla/Error/Template.pm [new file with mode: 0644]
process_bug.cgi
template/en/default/bug/process/bugmail.html.tmpl

index 69734e6c04336746b3c06f948e6bab81d57e5510..637d941145cec0c1bd50d6c09b15c6b290fff148 100644 (file)
@@ -1331,13 +1331,13 @@ sub remove_from_db {
 #####################################################################
 
 sub send_changes {
-    my ($self, $changes, $vars) = @_;
-
+    my ($self, $changes) = @_;
+    my @results;
     my $user = Bugzilla->user;
 
-    my $old_qa  = $changes->{'qa_contact'}  
+    my $old_qa  = $changes->{'qa_contact'}
                   ? $changes->{'qa_contact'}->[0] : '';
-    my $old_own = $changes->{'assigned_to'} 
+    my $old_own = $changes->{'assigned_to'}
                   ? $changes->{'assigned_to'}->[0] : '';
     my $old_cc  = $changes->{cc}
                   ? $changes->{cc}->[0] : '';
@@ -1349,15 +1349,15 @@ sub send_changes {
         changer   => $user,
     );
 
-    my $recipient_count = _send_bugmail(
-        { id => $self->id, type => 'bug', forced => \%forced }, $vars);
+    push @results, _send_bugmail(
+        { id => $self->id, type => 'bug', forced => \%forced });
 
     # 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) {
-        $recipient_count += _send_bugmail(
-            { forced => { changer => $user }, type => "dupe", id => $new_dup_id }, $vars);
+        push @results, _send_bugmail(
+            { forced => { changer => $user }, type => "dupe", id => $new_dup_id });
     }
 
     # If there were changes in dependencies, we need to notify those
@@ -1376,7 +1376,7 @@ sub send_changes {
 
             foreach my $id (@{ $self->blocked }) {
                 $params->{id} = $id;
-                $recipient_count += _send_bugmail($params, $vars);
+                push @results, _send_bugmail($params);
             }
         }
     }
@@ -1394,37 +1394,28 @@ sub send_changes {
     delete $changed_deps{''};
 
     foreach my $id (sort { $a <=> $b } (keys %changed_deps)) {
-        $recipient_count += _send_bugmail(
-            { forced => { changer => $user }, type => "dep", id => $id }, $vars);
+        push @results, _send_bugmail(
+            { forced => { changer => $user }, type => "dep", id => $id });
     }
 
     # Sending emails for the referenced bugs.
     foreach my $ref_bug_id (uniq @{ $self->{see_also_changes} || [] }) {
-        $recipient_count += _send_bugmail(
-            { forced => { changer => $user }, id => $ref_bug_id }, $vars);
+        push @results, _send_bugmail(
+            { forced => { changer => $user }, id => $ref_bug_id });
     }
 
-    return $recipient_count;
+    return \@results;
 }
 
 sub _send_bugmail {
-    my ($params, $vars) = @_;
+    my ($params) = @_;
 
     require Bugzilla::BugMail;
 
-    my $results =
+    my $sent_bugmail =
         Bugzilla::BugMail::Send($params->{'id'}, $params->{'forced'}, $params);
 
-    if (Bugzilla->usage_mode == USAGE_MODE_BROWSER) {
-        my $template = Bugzilla->template;
-        $vars->{$_} = $params->{$_} foreach keys %$params;
-        $vars->{'sent_bugmail'} = $results;
-        $template->process("bug/process/results.html.tmpl", $vars)
-            || ThrowTemplateError($template->error());
-        $vars->{'header_done'} = 1;
-    }
-
-    return scalar @{ $results->{sent} };
+    return { params => $params, sent_bugmail => $sent_bugmail };
 }
 
 #####################################################################
@@ -2778,10 +2769,11 @@ sub _set_product {
         if (%vars) {
             $vars{product} = $product;
             $vars{bug} = $self;
-            my $template = Bugzilla->template;
-            $template->process("bug/process/verify-new-product.html.tmpl",
-                \%vars) || ThrowTemplateError($template->error());
-            exit;
+            require Bugzilla::Error::Template;
+            die Bugzilla::Error::Template->new(
+                file => "bug/process/verify-new-product.html.tmpl",
+                vars => \%vars
+            );
         }
     }
     else {
diff --git a/Bugzilla/Error/Template.pm b/Bugzilla/Error/Template.pm
new file mode 100644 (file)
index 0000000..a3afa7e
--- /dev/null
@@ -0,0 +1,26 @@
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
+#
+# This Source Code Form is "Incompatible With Secondary Licenses", as
+# defined by the Mozilla Public License, v. 2.0.
+
+package Bugzilla::Error::Template;
+
+use 5.10.1;
+use strict;
+use warnings;
+use Moo;
+
+has 'file' => (
+    is       => 'ro',
+    required => 1,
+);
+
+has 'vars' => (
+    is      => 'ro',
+    default => sub { {} },
+);
+
+
+1;
\ No newline at end of file
index 80018cfb304faa33968268e8958fac328a847c0d..f97d02b28279b8ce6ae48771251c462b2b36f183 100755 (executable)
@@ -27,6 +27,7 @@ use Bugzilla::Status;
 use Bugzilla::Token;
 use Bugzilla::Hook;
 
+use Scalar::Util qw(blessed);
 use List::MoreUtils qw(firstidx);
 use Storable qw(dclone);
 
@@ -111,8 +112,6 @@ my $user_match_fields = {
 Bugzilla::Hook::process('bug_user_match_fields', { fields => $user_match_fields });
 Bugzilla::User::match_field($user_match_fields);
 
-print $cgi->header() unless Bugzilla->usage_mode == USAGE_MODE_EMAIL;
-
 # Check for a mid-air collision. Currently this only works when updating
 # an individual bug.
 my $delta_ts = $cgi->param('delta_ts') || '';
@@ -158,6 +157,7 @@ if ($delta_ts) {
             $cgi->param('token', issue_hash_token([$first_bug->id, $first_bug->delta_ts]));
 
             # Warn the user about the mid-air collision and ask them what to do.
+            print $cgi->header() unless Bugzilla->usage_mode == USAGE_MODE_EMAIL;
             $template->process("bug/process/midair.html.tmpl", $vars)
                 || ThrowTemplateError($template->error());
             exit;
@@ -186,6 +186,9 @@ $vars->{'title_tag'} = "bug_processed";
 my $action;
 if (defined $cgi->param('id')) {
     $action = $user->setting('post_bug_submit_action');
+    if ($action ne 'nothing' && $action ne 'same_bug' && $action ne 'next_bug') {
+        ThrowCodeError("invalid_post_bug_submit_action");
+    }
 
     if ($action eq 'next_bug') {
         my $bug_list_obj = $user->recent_search_for($first_bug);
@@ -361,7 +364,21 @@ foreach my $b (@bug_objects) {
         push(@remove_groups, $g->name) if grep { $_ eq $g->name } @unchecked_groups;
     }
     local $set_all_fields{groups}->{remove} = \@remove_groups;
-    $b->set_all(\%set_all_fields);
+    my $ok = eval {
+        $b->set_all(\%set_all_fields);
+        1;
+    };
+    unless ($ok) {
+        my $error = $@;
+        if (blessed $error && $error->isa('Bugzilla::Error::Template')) {
+            print $cgi->header();
+            $template->process($error->file, $error->vars);
+            exit;
+        }
+        else {
+            die $error;
+        }
+    }
 }
 
 if (defined $cgi->param('id')) {
@@ -376,6 +393,7 @@ if (defined $cgi->param('id')) {
 ##############################
 # Do Actual Database Updates #
 ##############################
+my $sent_changes;
 foreach my $bug (@bug_objects) {
     my $changes = $bug->update();
 
@@ -389,50 +407,58 @@ foreach my $bug (@bug_objects) {
         }
     }
 
-    my $recipient_count = $bug->send_changes($changes, $vars);
+    $sent_changes = $bug->send_changes($changes);
 }
 
 # Delete the session token used for the mass-change.
 delete_token($token) unless $cgi->param('id');
 
-if (Bugzilla->usage_mode == USAGE_MODE_EMAIL) {
-    # Do nothing.
-}
-elsif ($action eq 'next_bug' or $action eq 'same_bug') {
-    my $bug = $vars->{'bug'};
-    if ($bug and $user->can_see_bug($bug)) {
-        if ($action eq 'same_bug') {
-            # $bug->update() does not update the internal structure of
-            # the bug sufficiently to display the bug with the new values.
-            # (That is, if we just passed in the old Bug object, we'd get
-            # a lot of old values displayed.)
-            $bug = new Bugzilla::Bug($bug->id);
-            $vars->{'bug'} = $bug;
-        }
-        $vars->{'bugs'} = [$bug];
-        if ($action eq 'next_bug') {
-            $vars->{'nextbug'} = $bug->id;
-        }
+# BMO: add show_bug_format hook for experimental UI work
+my $format_params = {
+    format => scalar $cgi->param('format'),
+    ctype  => scalar $cgi->param('ctype'),
+};
+Bugzilla::Hook::process('show_bug_format', $format_params);
+my $format = $template->get_format("bug/show",
+                                    $format_params->{format},
+                                    $format_params->{ctype});
+
+if (Bugzilla->usage_mode != USAGE_MODE_EMAIL) {
+    print $cgi->header();
+
+    foreach my $sent_change (@$sent_changes) {
+        my $params       = $sent_change->{params};
+        my $sent_bugmail = $sent_change->{sent_bugmail};
+        $vars->{$_} = $params->{$_} foreach keys %$params;
+        $vars->{'sent_bugmail'} = $sent_bugmail;
+        $template->process("bug/process/results.html.tmpl", $vars)
+            || ThrowTemplateError($template->error());
+        $vars->{'header_done'} = 1;
+    }
+
+    if ($action eq 'next_bug' or $action eq 'same_bug') {
+        my $bug = $vars->{'bug'};
+        if ($bug and $user->can_see_bug($bug)) {
+            if ($action eq 'same_bug') {
+                # $bug->update() does not update the internal structure of
+                # the bug sufficiently to display the bug with the new values.
+                # (That is, if we just passed in the old Bug object, we'd get
+                # a lot of old values displayed.)
+                $bug = Bugzilla::Bug->new($bug->id);
+                $vars->{'bug'} = $bug;
+            }
+            $vars->{'bugs'} = [$bug];
+            if ($action eq 'next_bug') {
+                $vars->{'nextbug'} = $bug->id;
+            }
 
-        # BMO: add show_bug_format hook for experimental UI work
-        my $format_params = {
-            format => scalar $cgi->param('format'),
-            ctype  => scalar $cgi->param('ctype'),
-        };
-        Bugzilla::Hook::process('show_bug_format', $format_params);
-        my $format = $template->get_format("bug/show",
-                                           $format_params->{format},
-                                           $format_params->{ctype});
-        $template->process($format->{template}, $vars)
-          || ThrowTemplateError($template->error());
-        exit;
+            $template->process($format->{template}, $vars)
+                || ThrowTemplateError($template->error());
+            exit;
+        }
     }
-} elsif ($action ne 'nothing') {
-    ThrowCodeError("invalid_post_bug_submit_action");
-}
 
-# End the response page.
-unless (Bugzilla->usage_mode == USAGE_MODE_EMAIL) {
+    # End the response page.
     $template->process("bug/navigate.html.tmpl", $vars)
         || ThrowTemplateError($template->error());
     $template->process("global/footer.html.tmpl", $vars)
index 21e4ff7b749f00590a01e82fc1417a7c0ea60809..0c4f2f27d96ebd69ef3d7095c686cf565307a6e9 100644 (file)
   # sent_bugmail: The results of Bugzilla::BugMail::Send().
   #%]
 
-[% USE CGI %]
+[% USE Bugzilla %]
 [% PROCESS global/variables.none.tmpl %]
 
 [%# hide the recipient list by default from new users %]
 [% show_recipients =
      user.settings.post_bug_submit_action.value == 'nothing'
-     || CGI.cookie('show_bugmail_recipients')
+     || Bugzilla.cgi.cookie('show_bugmail_recipients')
      || !user.can_see_bug(mailing_bugid)
 %]
 [% recipient_count = sent_bugmail.sent.size %]