attachment.cgi 25.5 KB
Newer Older
1
#!/usr/bin/perl -wT
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# -*- Mode: perl; indent-tabs-mode: nil -*-
#
# 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.
#
# The Original Code is the Bugzilla Bug Tracking System.
#
# The Initial Developer of the Original Code is Netscape Communications
# Corporation. Portions created by Netscape are
# Copyright (C) 1998 Netscape Communications Corporation. All
# Rights Reserved.
#
# Contributor(s): Terry Weissman <terry@mozilla.org>
#                 Myk Melez <myk@mozilla.org>
23 24
#                 Daniel Raichle <draichle@gmx.net>
#                 Dave Miller <justdave@syndicomm.com>
25
#                 Alexander J. Vincent <ajvincent@juno.com>
26
#                 Max Kanat-Alexander <mkanat@bugzilla.org>
27
#                 Greg Hendricks <ghendricks@novell.com>
28
#                 Frédéric Buclin <LpSolit@gmail.com>
29
#                 Marc Schumann <wurblzap@gmail.com>
30 31 32 33 34 35 36 37

################################################################################
# Script Initialization
################################################################################

# Make it harder for us to do dangerous things in Perl.
use strict;

38
use lib qw(. lib);
39

40
use Bugzilla;
41
use Bugzilla::Constants;
42
use Bugzilla::Error;
43 44
use Bugzilla::Flag; 
use Bugzilla::FlagType; 
45
use Bugzilla::User;
46
use Bugzilla::Util;
47
use Bugzilla::Bug;
48
use Bugzilla::Field;
49
use Bugzilla::Attachment;
50
use Bugzilla::Attachment::PatchReader;
51
use Bugzilla::Token;
52
use Bugzilla::Keyword;
53

54
Bugzilla->login();
55

56 57 58 59 60 61 62
# For most scripts we don't make $cgi and $template global variables. But
# when preparing Bugzilla for mod_perl, this script used these
# variables in so many subroutines that it was easier to just
# make them globals.
local our $cgi = Bugzilla->cgi;
local our $template = Bugzilla->template;
local our $vars = {};
63

64 65 66 67
################################################################################
# Main Body Execution
################################################################################

68 69 70 71
# All calls to this script should contain an "action" variable whose
# value determines what the user wants to do.  The code below checks
# the value of that variable and runs the appropriate code. If none is
# supplied, we default to 'view'.
72 73

# Determine whether to use the action specified by the user or the default.
74
my $action = $cgi->param('action') || 'view';
75

76 77 78 79 80 81
# Determine if PatchReader is installed
eval {
    require PatchReader;
    $vars->{'patchviewerinstalled'} = 1;
};

82
if ($action eq "view")  
83
{
84
    view();
85
}
86 87
elsif ($action eq "interdiff")
{
88
    interdiff();
89 90 91
}
elsif ($action eq "diff")
{
92
    diff();
93
}
94 95
elsif ($action eq "viewall") 
{ 
96
    viewall(); 
97
}
98 99
elsif ($action eq "enter") 
{ 
100 101
    Bugzilla->login(LOGIN_REQUIRED);
    enter(); 
102 103 104
}
elsif ($action eq "insert")
{
105 106
    Bugzilla->login(LOGIN_REQUIRED);
    insert();
107
}
108 109
elsif ($action eq "edit") 
{ 
110
    edit(); 
111 112 113
}
elsif ($action eq "update") 
{ 
114 115
    Bugzilla->login(LOGIN_REQUIRED);
    update();
116
}
117 118 119
elsif ($action eq "delete") {
    delete_attachment();
}
120 121
else 
{ 
122
  ThrowCodeError("unknown_action", { action => $action });
123 124 125 126 127 128 129 130
}

exit;

################################################################################
# Data Validation / Security Authorization
################################################################################

131 132 133 134 135 136 137 138
# Validates an attachment ID. Optionally takes a parameter of a form
# variable name that contains the ID to be validated. If not specified,
# uses 'id'.
# 
# Will throw an error if 1) attachment ID is not a valid number,
# 2) attachment does not exist, or 3) user isn't allowed to access the
# attachment.
#
139 140 141
# Returns an attachment object.

sub validateID {
142
    my $param = @_ ? $_[0] : 'id';
143 144
    my $user = Bugzilla->user;

145 146 147
    # If we're not doing interdiffs, check if id wasn't specified and
    # prompt them with a page that allows them to choose an attachment.
    # Happens when calling plain attachment.cgi from the urlbar directly
148
    if ($param eq 'id' && !$cgi->param('id')) {
149
        print $cgi->header();
150 151 152 153
        $template->process("attachment/choose.html.tmpl", $vars) ||
            ThrowTemplateError($template->error());
        exit;
    }
154
    
155 156 157 158 159 160 161
    my $attach_id = $cgi->param($param);

    # Validate the specified attachment id. detaint kills $attach_id if
    # non-natural, so use the original value from $cgi in our exception
    # message here.
    detaint_natural($attach_id)
     || ThrowUserError("invalid_attach_id", { attach_id => $cgi->param($param) });
162
  
163
    # Make sure the attachment exists in the database.
164 165
    my $attachment = Bugzilla::Attachment->get($attach_id)
      || ThrowUserError("invalid_attach_id", { attach_id => $attach_id });
166

167
    # Make sure the user is authorized to access this attachment's bug.
168 169
    ValidateBugID($attachment->bug_id);
    if ($attachment->isprivate && $user->id != $attachment->attacher->id && !$user->is_insider) {
170 171
        ThrowUserError('auth_failure', {action => 'access',
                                        object => 'attachment'});
172
    }
173
    return $attachment;
174 175
}

176 177 178
# Validates format of a diff/interdiff. Takes a list as an parameter, which
# defines the valid format values. Will throw an error if the format is not
# in the list. Returns either the user selected or default format.
179 180
sub validateFormat
{
181 182 183
  # receives a list of legal formats; first item is a default
  my $format = $cgi->param('format') || $_[0];
  if ( lsearch(\@_, $format) == -1)
184
  {
185
     ThrowUserError("invalid_format", { format  => $format, formats => \@_ });
186
  }
187

188
  return $format;
189 190
}

191 192
# Validates context of a diff/interdiff. Will throw an error if the context
# is not number, "file" or "patch". Returns the validated, detainted context.
193 194
sub validateContext
{
195 196 197 198
  my $context = $cgi->param('context') || "patch";
  if ($context ne "file" && $context ne "patch") {
    detaint_natural($context)
      || ThrowUserError("invalid_context", { context => $cgi->param('context') });
199
  }
200 201

  return $context;
202 203
}

204 205 206
sub validateCanChangeBug
{
    my ($bugid) = @_;
207 208 209
    my $dbh = Bugzilla->dbh;
    my ($productid) = $dbh->selectrow_array(
            "SELECT product_id
210
             FROM bugs 
211 212
             WHERE bug_id = ?", undef, $bugid);

213
    Bugzilla->user->can_edit_product($productid)
214 215
      || ThrowUserError("illegal_attachment_edit_bug",
                        { bug_id => $bugid });
216 217
}

218 219 220 221
################################################################################
# Functions
################################################################################

222
# Display an attachment.
223
sub view {
224
    # Retrieve and validate parameters
225 226 227 228
    my $attachment = validateID();
    my $contenttype = $attachment->contenttype;
    my $filename = $attachment->filename;

229 230 231
    # Bug 111522: allow overriding content-type manually in the posted form
    # params.
    if (defined $cgi->param('content_type'))
232
    {
233 234
        $cgi->param('contenttypemethod', 'manual');
        $cgi->param('contenttypeentry', $cgi->param('content_type'));
235
        Bugzilla::Attachment->validate_content_type(THROW_ERROR);
236
        $contenttype = $cgi->param('content_type');
237
    }
238

239
    # Return the appropriate HTTP response headers.
240
    $attachment->datasize || ThrowUserError("attachment_removed");
241

242
    $filename =~ s/^.*[\/\\]//;
243 244 245 246
    # escape quotes and backslashes in the filename, per RFCs 2045/822
    $filename =~ s/\\/\\\\/g; # escape backslashes
    $filename =~ s/"/\\"/g; # escape quotes

247 248
    print $cgi->header(-type=>"$contenttype; name=\"$filename\"",
                       -content_disposition=> "inline; filename=\"$filename\"",
249
                       -content_length => $attachment->datasize);
250
    disable_utf8();
251
    print $attachment->data;
252 253
}

254 255
sub interdiff {
    # Retrieve and validate parameters
256 257
    my $old_attachment = validateID('oldid');
    my $new_attachment = validateID('newid');
258 259 260 261 262
    my $format = validateFormat('html', 'raw');
    my $context = validateContext();

    Bugzilla::Attachment::PatchReader::process_interdiff(
        $old_attachment, $new_attachment, $format, $context);
263 264
}

265 266
sub diff {
    # Retrieve and validate parameters
267
    my $attachment = validateID();
268 269
    my $format = validateFormat('html', 'raw');
    my $context = validateContext();
270

271 272 273 274
    # If it is not a patch, view normally.
    if (!$attachment->ispatch) {
        view();
        return;
275 276
    }

277
    Bugzilla::Attachment::PatchReader::process_diff($attachment, $format, $context);
278
}
279

280 281
# Display all attachments for a given bug in a series of IFRAMEs within one
# HTML page.
282
sub viewall {
283 284 285
    # Retrieve and validate parameters
    my $bugid = $cgi->param('bugid');
    ValidateBugID($bugid);
286
    my $bug = new Bugzilla::Bug($bugid);
287

288
    my $attachments = Bugzilla::Attachment->get_attachments_by_bug($bugid);
289

290 291 292
    # Define the variables and functions that will be passed to the UI template.
    $vars->{'bug'} = $bug;
    $vars->{'attachments'} = $attachments;
293

294
    print $cgi->header();
295

296 297 298
    # Generate and return the UI (HTML page) from the appropriate template.
    $template->process("attachment/show-multiple.html.tmpl", $vars)
      || ThrowTemplateError($template->error());
299 300
}

301
# Display a form for entering a new attachment.
302
sub enter {
303 304 305 306
  # Retrieve and validate parameters
  my $bugid = $cgi->param('bugid');
  ValidateBugID($bugid);
  validateCanChangeBug($bugid);
307
  my $dbh = Bugzilla->dbh;
308 309 310
  my $user = Bugzilla->user;

  my $bug = new Bugzilla::Bug($bugid, $user->id);
311 312 313
  # Retrieve the attachments the user can edit from the database and write
  # them into an array of hashes where each hash represents one attachment.
  my $canEdit = "";
314 315
  if (!$user->in_group('editbugs', $bug->product_id)) {
      $canEdit = "AND submitter_id = " . $user->id;
316
  }
317 318 319
  my $attach_ids = $dbh->selectcol_arrayref("SELECT attach_id FROM attachments
                                             WHERE bug_id = ? AND isobsolete = 0 $canEdit
                                             ORDER BY attach_id", undef, $bugid);
320 321

  # Define the variables and functions that will be passed to the UI template.
322
  $vars->{'bug'} = $bug;
323
  $vars->{'attachments'} = Bugzilla::Attachment->get_list($attach_ids);
324

325
  my $flag_types = Bugzilla::FlagType::match({'target_type'  => 'attachment',
326 327
                                              'product_id'   => $bug->product_id,
                                              'component_id' => $bug->component_id});
328
  $vars->{'flag_types'} = $flag_types;
329
  $vars->{'any_flags_requesteeble'} = grep($_->is_requesteeble, @$flag_types);
330

331
  print $cgi->header();
332 333

  # Generate and return the UI (HTML page) from the appropriate template.
334 335
  $template->process("attachment/create.html.tmpl", $vars)
    || ThrowTemplateError($template->error());
336 337
}

338
# Insert a new attachment into the database.
339
sub insert {
340 341
    my $dbh = Bugzilla->dbh;
    my $user = Bugzilla->user;
342

343 344
    $dbh->bz_start_transaction;

345 346 347 348
    # Retrieve and validate parameters
    my $bugid = $cgi->param('bugid');
    ValidateBugID($bugid);
    validateCanChangeBug($bugid);
349
    my ($timestamp) = Bugzilla->dbh->selectrow_array("SELECT NOW()");
350

351
    my $bug = new Bugzilla::Bug($bugid);
352
    my $attachment =
353
        Bugzilla::Attachment->insert_attachment_for_bug(THROW_ERROR, $bug, $user,
354
                                                        $timestamp, $vars);
355

356 357 358 359
    # Insert a comment about the new attachment into the database.
    my $comment = "Created an attachment (id=" . $attachment->id . ")\n" .
                  $attachment->description . "\n";
    $comment .= ("\n" . $cgi->param('comment')) if defined $cgi->param('comment');
360

361
    $bug->add_comment($comment, { isprivate => $attachment->isprivate });
362

363
  # Assign the bug to the user, if they are allowed to take it
364
  my $owner = "";
365
  if ($cgi->param('takebug') && $user->in_group('editbugs', $bug->product_id)) {
366 367 368 369 370 371 372 373 374 375
      # When taking a bug, we have to follow the workflow.
      my $bug_status = $cgi->param('bug_status') || '';
      ($bug_status) = grep {$_->name eq $bug_status} @{$bug->status->can_change_to};

      if ($bug_status && $bug_status->is_open
          && ($bug_status->name ne 'UNCONFIRMED' || $bug->product_obj->votes_to_confirm))
      {
          $bug->set_status($bug_status->name);
          $bug->clear_resolution();
      }
376
      # Make sure the person we are taking the bug from gets mail.
377
      $owner = $bug->assigned_to->login;
378
      $bug->set_assigned_to($user);
379
  }
380 381 382
  $bug->update($timestamp);

  $dbh->bz_commit_transaction;
383

384
  # Define the variables and functions that will be passed to the UI template.
385
  $vars->{'mailrecipients'} =  { 'changer' => $user->login,
386
                                 'owner'   => $owner };
387
  $vars->{'attachment'} = $attachment;
388 389 390 391
  # We cannot reuse the $bug object as delta_ts has eventually been updated
  # since the object was created.
  $vars->{'bugs'} = [new Bugzilla::Bug($bugid)];
  $vars->{'header_done'} = 1;
392
  $vars->{'contenttypemethod'} = $cgi->param('contenttypemethod');
393 394
  $vars->{'valid_keywords'} = [map($_->name, Bugzilla::Keyword->get_all)];
  $vars->{'use_keywords'} = 1 if Bugzilla::Keyword::keyword_count();
395

396
  print $cgi->header();
397
  # Generate and return the UI (HTML page) from the appropriate template.
398 399
  $template->process("attachment/created.html.tmpl", $vars)
    || ThrowTemplateError($template->error());
400 401
}

402 403 404 405
# Displays a form for editing attachment properties.
# Any user is allowed to access this page, unless the attachment
# is private and the user does not belong to the insider group.
# Validations are done later when the user submits changes.
406
sub edit {
407
  my $attachment = validateID();
408
  my $dbh = Bugzilla->dbh;
409 410 411

  # Retrieve a list of attachments for this bug as well as a summary of the bug
  # to use in a navigation bar across the top of the screen.
412
  my $bugattachments =
413 414 415
      Bugzilla::Attachment->get_attachments_by_bug($attachment->bug_id);
  # We only want attachment IDs.
  @$bugattachments = map { $_->id } @$bugattachments;
416 417 418 419 420 421

  my ($bugsummary, $product_id, $component_id) =
      $dbh->selectrow_array('SELECT short_desc, product_id, component_id
                               FROM bugs
                              WHERE bug_id = ?', undef, $attachment->bug_id);

422
  # Get a list of flag types that can be set for this attachment.
423 424
  my $flag_types = Bugzilla::FlagType::match({ 'target_type'  => 'attachment' ,
                                               'product_id'   => $product_id ,
425
                                               'component_id' => $component_id });
426
  foreach my $flag_type (@$flag_types) {
427
    $flag_type->{'flags'} = Bugzilla::Flag->match({ 'type_id'   => $flag_type->id,
428
                                                    'attach_id' => $attachment->id });
429 430
  }
  $vars->{'flag_types'} = $flag_types;
431
  $vars->{'any_flags_requesteeble'} = grep($_->is_requesteeble, @$flag_types);
432
  $vars->{'attachment'} = $attachment;
433
  $vars->{'bugsummary'} = $bugsummary; 
434
  $vars->{'attachments'} = $bugattachments;
435

436
  print $cgi->header();
437 438

  # Generate and return the UI (HTML page) from the appropriate template.
439 440
  $template->process("attachment/edit.html.tmpl", $vars)
    || ThrowTemplateError($template->error());
441 442
}

443 444 445 446 447
# Updates an attachment record. Users with "editbugs" privileges, (or the
# original attachment's submitter) can edit the attachment's description,
# content type, ispatch and isobsolete flags, and statuses, and they can
# also submit a comment that appears in the bug.
# Users cannot edit the content of the attachment itself.
448
sub update {
449 450
    my $user = Bugzilla->user;
    my $dbh = Bugzilla->dbh;
451 452

    # Retrieve and validate parameters
453 454
    my $attachment = validateID();
    my $bug = new Bugzilla::Bug($attachment->bug_id);
455
    $attachment->validate_can_edit($bug->product_id);
456
    validateCanChangeBug($bug->id);
457 458 459
    Bugzilla::Attachment->validate_description(THROW_ERROR);
    Bugzilla::Attachment->validate_is_patch(THROW_ERROR);
    Bugzilla::Attachment->validate_content_type(THROW_ERROR) unless $cgi->param('ispatch');
460 461
    $cgi->param('isobsolete', $cgi->param('isobsolete') ? 1 : 0);
    $cgi->param('isprivate', $cgi->param('isprivate') ? 1 : 0);
462

463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
    # Now make sure the attachment has not been edited since we loaded the page.
    if (defined $cgi->param('delta_ts')
        && $cgi->param('delta_ts') ne $attachment->modification_time)
    {
        ($vars->{'operations'}) =
            Bugzilla::Bug::GetBugActivity($bug->id, $attachment->id, $cgi->param('delta_ts'));

        # If the modification date changed but there is no entry in
        # the activity table, this means someone commented only.
        # In this case, there is no reason to midair.
        if (scalar(@{$vars->{'operations'}})) {
            $cgi->param('delta_ts', $attachment->modification_time);
            $vars->{'attachment'} = $attachment;

            print $cgi->header();
            # Warn the user about the mid-air collision and ask them what to do.
            $template->process("attachment/midair.html.tmpl", $vars)
              || ThrowTemplateError($template->error());
            exit;
        }
    }
484 485 486 487 488 489 490 491
    # If the submitter of the attachment is not in the insidergroup,
    # be sure that he cannot overwrite the private bit.
    # This check must be done before calling Bugzilla::Flag*::validate(),
    # because they will look at the private bit when checking permissions.
    # XXX - This is a ugly hack. Ideally, we shouldn't have to look at the
    # old private bit twice (first here, and then below again), but this is
    # the less risky change.
    unless ($user->is_insider) {
492
        $cgi->param('isprivate', $attachment->isprivate);
493
    }
494

495 496 497 498 499 500 501 502 503 504 505
    # If the user submitted a comment while editing the attachment,
    # add the comment to the bug. Do this after having validated isprivate!
    if ($cgi->param('comment')) {
        # Prepend a string to the comment to let users know that the comment came
        # from the "edit attachment" screen.
        my $comment = "(From update of attachment " . $attachment->id . ")\n" .
                      $cgi->param('comment');

        $bug->add_comment($comment, { isprivate => $cgi->param('isprivate') });
    }

506 507 508
    # The order of these function calls is important, as Flag::validate
    # assumes User::match_field has ensured that the values in the
    # requestee fields are legitimate user email addresses.
509
    Bugzilla::User::match_field($cgi, {
510
        '^requestee(_type)?-(\d+)$' => { 'type' => 'multi' }
511
    });
512
    Bugzilla::Flag::validate($bug->id, $attachment->id);
513

514 515
    # Start a transaction in preparation for updating the attachment.
    $dbh->bz_start_transaction();
516

517
  # Quote the description and content type for use in the SQL UPDATE statement.
518 519 520 521 522 523 524
  my $description = $cgi->param('description');
  my $contenttype = $cgi->param('contenttype');
  my $filename = $cgi->param('filename');
  # we can detaint this way thanks to placeholders
  trick_taint($description);
  trick_taint($contenttype);
  trick_taint($filename);
525

526
  # Figure out when the changes were made.
527
  my ($timestamp) = $dbh->selectrow_array("SELECT NOW()");
528
    
529 530 531 532
  # Update flags.  We have to do this before committing changes
  # to attachments so that we can delete pending requests if the user
  # is obsoleting this attachment without deleting any requests
  # the user submits at the same time.
533
  Bugzilla::Flag->process($bug, $attachment, $timestamp, $vars);
534

535
  # Update the attachment record in the database.
536 537 538 539 540 541
  $dbh->do("UPDATE  attachments 
            SET     description = ?,
                    mimetype    = ?,
                    filename    = ?,
                    ispatch     = ?,
                    isobsolete  = ?,
542 543
                    isprivate   = ?,
                    modification_time = ?
544 545 546
            WHERE   attach_id   = ?",
            undef, ($description, $contenttype, $filename,
            $cgi->param('ispatch'), $cgi->param('isobsolete'), 
547
            $cgi->param('isprivate'), $timestamp, $attachment->id));
548

549
  my $updated_attachment = Bugzilla::Attachment->get($attachment->id);
550
  # Record changes in the activity table.
551 552 553 554 555
  my $sth = $dbh->prepare('INSERT INTO bugs_activity (bug_id, attach_id, who, bug_when,
                                                      fieldid, removed, added)
                           VALUES (?, ?, ?, ?, ?, ?, ?)');

  if ($attachment->description ne $updated_attachment->description) {
556
    my $fieldid = get_field_id('attachments.description');
557 558
    $sth->execute($bug->id, $attachment->id, $user->id, $timestamp, $fieldid,
                  $attachment->description, $updated_attachment->description);
559
  }
560
  if ($attachment->contenttype ne $updated_attachment->contenttype) {
561
    my $fieldid = get_field_id('attachments.mimetype');
562 563
    $sth->execute($bug->id, $attachment->id, $user->id, $timestamp, $fieldid,
                  $attachment->contenttype, $updated_attachment->contenttype);
564
  }
565
  if ($attachment->filename ne $updated_attachment->filename) {
566
    my $fieldid = get_field_id('attachments.filename');
567 568
    $sth->execute($bug->id, $attachment->id, $user->id, $timestamp, $fieldid,
                  $attachment->filename, $updated_attachment->filename);
569
  }
570
  if ($attachment->ispatch != $updated_attachment->ispatch) {
571
    my $fieldid = get_field_id('attachments.ispatch');
572 573
    $sth->execute($bug->id, $attachment->id, $user->id, $timestamp, $fieldid,
                  $attachment->ispatch, $updated_attachment->ispatch);
574
  }
575
  if ($attachment->isobsolete != $updated_attachment->isobsolete) {
576
    my $fieldid = get_field_id('attachments.isobsolete');
577 578
    $sth->execute($bug->id, $attachment->id, $user->id, $timestamp, $fieldid,
                  $attachment->isobsolete, $updated_attachment->isobsolete);
579
  }
580
  if ($attachment->isprivate != $updated_attachment->isprivate) {
581
    my $fieldid = get_field_id('attachments.isprivate');
582 583
    $sth->execute($bug->id, $attachment->id, $user->id, $timestamp, $fieldid,
                  $attachment->isprivate, $updated_attachment->isprivate);
584
  }
585
  
586 587
  # Commit the transaction now that we are finished updating the database.
  $dbh->bz_commit_transaction();
588

589 590
  # Commit the comment, if any.
  $bug->update();
591 592

  # Define the variables and functions that will be passed to the UI template.
593
  $vars->{'mailrecipients'} = { 'changer' => Bugzilla->user->login };
594
  $vars->{'attachment'} = $attachment;
595 596 597 598
  # We cannot reuse the $bug object as delta_ts has eventually been updated
  # since the object was created.
  $vars->{'bugs'} = [new Bugzilla::Bug($bug->id)];
  $vars->{'header_done'} = 1;
599 600
  $vars->{'valid_keywords'} = [map($_->name, Bugzilla::Keyword->get_all)];
  $vars->{'use_keywords'} = 1 if Bugzilla::Keyword::keyword_count();
601

602
  print $cgi->header();
603 604

  # Generate and return the UI (HTML page) from the appropriate template.
605 606
  $template->process("attachment/updated.html.tmpl", $vars)
    || ThrowTemplateError($template->error());
607
}
608 609 610 611 612 613 614 615 616 617 618 619 620

# Only administrators can delete attachments.
sub delete_attachment {
    my $user = Bugzilla->login(LOGIN_REQUIRED);
    my $dbh = Bugzilla->dbh;

    print $cgi->header();

    $user->in_group('admin')
      || ThrowUserError('auth_failure', {group  => 'admin',
                                         action => 'delete',
                                         object => 'attachment'});

621
    Bugzilla->params->{'allow_attachment_deletion'}
622 623 624
      || ThrowUserError('attachment_deletion_disabled');

    # Make sure the administrator is allowed to edit this attachment.
625 626
    my $attachment = validateID();
    validateCanChangeBug($attachment->bug_id);
627 628 629 630 631 632 633 634 635

    $attachment->datasize || ThrowUserError('attachment_removed');

    # We don't want to let a malicious URL accidentally delete an attachment.
    my $token = trim($cgi->param('token'));
    if ($token) {
        my ($creator_id, $date, $event) = Bugzilla::Token::GetTokenData($token);
        unless ($creator_id
                  && ($creator_id == $user->id)
636
                  && ($event eq 'attachment' . $attachment->id))
637 638
        {
            # The token is invalid.
639
            ThrowUserError('token_does_not_exist');
640 641
        }

642 643
        my $bug = new Bugzilla::Bug($attachment->bug_id);

644 645
        # The token is valid. Delete the content of the attachment.
        my $msg;
646
        $vars->{'attachment'} = $attachment;
647 648 649 650 651 652 653
        $vars->{'date'} = $date;
        $vars->{'reason'} = clean_text($cgi->param('reason') || '');
        $vars->{'mailrecipients'} = { 'changer' => $user->login };

        $template->process("attachment/delete_reason.txt.tmpl", $vars, \$msg)
          || ThrowTemplateError($template->error());

654 655 656
        # Paste the reason provided by the admin into a comment.
        $bug->add_comment($msg);

657 658 659 660
        # If the attachment is stored locally, remove it.
        if (-e $attachment->_get_local_filename) {
            unlink $attachment->_get_local_filename;
        }
661
        $attachment->remove_from_db();
662 663

        # Now delete the token.
664
        delete_token($token);
665

666 667
        # Insert the comment.
        $bug->update();
668

669
        # Required to display the bug the deleted attachment belongs to.
670
        $vars->{'bugs'} = [$bug];
671
        $vars->{'header_done'} = 1;
672 673
        $vars->{'valid_keywords'} = [map($_->name, Bugzilla::Keyword->get_all)];
        $vars->{'use_keywords'} = 1 if Bugzilla::Keyword::keyword_count();
674

675 676 677 678 679
        $template->process("attachment/updated.html.tmpl", $vars)
          || ThrowTemplateError($template->error());
    }
    else {
        # Create a token.
680
        $token = issue_session_token('attachment' . $attachment->id);
681 682 683 684 685 686 687 688

        $vars->{'a'} = $attachment;
        $vars->{'token'} = $token;

        $template->process("attachment/confirm-delete.html.tmpl", $vars)
          || ThrowTemplateError($template->error());
    }
}