Attachment.pm 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960
  1. # -*- Mode: perl; indent-tabs-mode: nil -*-
  2. #
  3. # The contents of this file are subject to the Mozilla Public
  4. # License Version 1.1 (the "License"); you may not use this file
  5. # except in compliance with the License. You may obtain a copy of
  6. # the License at http://www.mozilla.org/MPL/
  7. #
  8. # Software distributed under the License is distributed on an "AS
  9. # IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  10. # implied. See the License for the specific language governing
  11. # rights and limitations under the License.
  12. #
  13. # The Original Code is the Bugzilla Bug Tracking System.
  14. #
  15. # The Initial Developer of the Original Code is Netscape Communications
  16. # Corporation. Portions created by Netscape are
  17. # Copyright (C) 1998 Netscape Communications Corporation. All
  18. # Rights Reserved.
  19. #
  20. # Contributor(s): Terry Weissman <terry@mozilla.org>
  21. # Myk Melez <myk@mozilla.org>
  22. # Marc Schumann <wurblzap@gmail.com>
  23. # Frédéric Buclin <LpSolit@gmail.com>
  24. use strict;
  25. package Bugzilla::Attachment;
  26. =head1 NAME
  27. Bugzilla::Attachment - a file related to a bug that a user has uploaded
  28. to the Bugzilla server
  29. =head1 SYNOPSIS
  30. use Bugzilla::Attachment;
  31. # Get the attachment with the given ID.
  32. my $attachment = Bugzilla::Attachment->get($attach_id);
  33. # Get the attachments with the given IDs.
  34. my $attachments = Bugzilla::Attachment->get_list($attach_ids);
  35. =head1 DESCRIPTION
  36. This module defines attachment objects, which represent files related to bugs
  37. that users upload to the Bugzilla server.
  38. =cut
  39. use Bugzilla::Constants;
  40. use Bugzilla::Error;
  41. use Bugzilla::Flag;
  42. use Bugzilla::User;
  43. use Bugzilla::Util;
  44. use Bugzilla::Field;
  45. sub get {
  46. my $invocant = shift;
  47. my $id = shift;
  48. my $attachments = _retrieve([$id]);
  49. my $self = $attachments->[0];
  50. bless($self, ref($invocant) || $invocant) if $self;
  51. return $self;
  52. }
  53. sub get_list {
  54. my $invocant = shift;
  55. my $ids = shift;
  56. my $attachments = _retrieve($ids);
  57. foreach my $attachment (@$attachments) {
  58. bless($attachment, ref($invocant) || $invocant);
  59. }
  60. return $attachments;
  61. }
  62. sub _retrieve {
  63. my ($ids) = @_;
  64. return [] if scalar(@$ids) == 0;
  65. my @columns = (
  66. 'attachments.attach_id AS id',
  67. 'attachments.bug_id AS bug_id',
  68. 'attachments.description AS description',
  69. 'attachments.mimetype AS contenttype',
  70. 'attachments.submitter_id AS attacher_id',
  71. Bugzilla->dbh->sql_date_format('attachments.creation_ts',
  72. '%Y.%m.%d %H:%i') . " AS attached",
  73. 'attachments.modification_time',
  74. 'attachments.filename AS filename',
  75. 'attachments.ispatch AS ispatch',
  76. 'attachments.isurl AS isurl',
  77. 'attachments.isobsolete AS isobsolete',
  78. 'attachments.isprivate AS isprivate'
  79. );
  80. my $columns = join(", ", @columns);
  81. my $dbh = Bugzilla->dbh;
  82. my $records = $dbh->selectall_arrayref(
  83. "SELECT $columns
  84. FROM attachments
  85. WHERE "
  86. . Bugzilla->dbh->sql_in('attach_id', $ids)
  87. . " ORDER BY attach_id",
  88. { Slice => {} });
  89. return $records;
  90. }
  91. =pod
  92. =head2 Instance Properties
  93. =over
  94. =item C<id>
  95. the unique identifier for the attachment
  96. =back
  97. =cut
  98. sub id {
  99. my $self = shift;
  100. return $self->{id};
  101. }
  102. =over
  103. =item C<bug_id>
  104. the ID of the bug to which the attachment is attached
  105. =back
  106. =cut
  107. # XXX Once Bug.pm slims down sufficiently this should become a reference
  108. # to a bug object.
  109. sub bug_id {
  110. my $self = shift;
  111. return $self->{bug_id};
  112. }
  113. =over
  114. =item C<description>
  115. user-provided text describing the attachment
  116. =back
  117. =cut
  118. sub description {
  119. my $self = shift;
  120. return $self->{description};
  121. }
  122. =over
  123. =item C<contenttype>
  124. the attachment's MIME media type
  125. =back
  126. =cut
  127. sub contenttype {
  128. my $self = shift;
  129. return $self->{contenttype};
  130. }
  131. =over
  132. =item C<attacher>
  133. the user who attached the attachment
  134. =back
  135. =cut
  136. sub attacher {
  137. my $self = shift;
  138. return $self->{attacher} if exists $self->{attacher};
  139. $self->{attacher} = new Bugzilla::User($self->{attacher_id});
  140. return $self->{attacher};
  141. }
  142. =over
  143. =item C<attached>
  144. the date and time on which the attacher attached the attachment
  145. =back
  146. =cut
  147. sub attached {
  148. my $self = shift;
  149. return $self->{attached};
  150. }
  151. =over
  152. =item C<modification_time>
  153. the date and time on which the attachment was last modified.
  154. =back
  155. =cut
  156. sub modification_time {
  157. my $self = shift;
  158. return $self->{modification_time};
  159. }
  160. =over
  161. =item C<filename>
  162. the name of the file the attacher attached
  163. =back
  164. =cut
  165. sub filename {
  166. my $self = shift;
  167. return $self->{filename};
  168. }
  169. =over
  170. =item C<ispatch>
  171. whether or not the attachment is a patch
  172. =back
  173. =cut
  174. sub ispatch {
  175. my $self = shift;
  176. return $self->{ispatch};
  177. }
  178. =over
  179. =item C<isurl>
  180. whether or not the attachment is a URL
  181. =back
  182. =cut
  183. sub isurl {
  184. my $self = shift;
  185. return $self->{isurl};
  186. }
  187. =over
  188. =item C<isobsolete>
  189. whether or not the attachment is obsolete
  190. =back
  191. =cut
  192. sub isobsolete {
  193. my $self = shift;
  194. return $self->{isobsolete};
  195. }
  196. =over
  197. =item C<isprivate>
  198. whether or not the attachment is private
  199. =back
  200. =cut
  201. sub isprivate {
  202. my $self = shift;
  203. return $self->{isprivate};
  204. }
  205. =over
  206. =item C<is_viewable>
  207. Returns 1 if the attachment has a content-type viewable in this browser.
  208. Note that we don't use $cgi->Accept()'s ability to check if a content-type
  209. matches, because this will return a value even if it's matched by the generic
  210. */* which most browsers add to the end of their Accept: headers.
  211. =back
  212. =cut
  213. sub is_viewable {
  214. my $self = shift;
  215. my $contenttype = $self->contenttype;
  216. my $cgi = Bugzilla->cgi;
  217. # We assume we can view all text and image types.
  218. return 1 if ($contenttype =~ /^(text|image)\//);
  219. # Mozilla can view XUL. Note the trailing slash on the Gecko detection to
  220. # avoid sending XUL to Safari.
  221. return 1 if (($contenttype =~ /^application\/vnd\.mozilla\./)
  222. && ($cgi->user_agent() =~ /Gecko\//));
  223. # If it's not one of the above types, we check the Accept: header for any
  224. # types mentioned explicitly.
  225. my $accept = join(",", $cgi->Accept());
  226. return 1 if ($accept =~ /^(.*,)?\Q$contenttype\E(,.*)?$/);
  227. return 0;
  228. }
  229. =over
  230. =item C<data>
  231. the content of the attachment
  232. =back
  233. =cut
  234. sub data {
  235. my $self = shift;
  236. return $self->{data} if exists $self->{data};
  237. # First try to get the attachment data from the database.
  238. ($self->{data}) = Bugzilla->dbh->selectrow_array("SELECT thedata
  239. FROM attach_data
  240. WHERE id = ?",
  241. undef,
  242. $self->{id});
  243. # If there's no attachment data in the database, the attachment is stored
  244. # in a local file, so retrieve it from there.
  245. if (length($self->{data}) == 0) {
  246. if (open(AH, $self->_get_local_filename())) {
  247. local $/;
  248. binmode AH;
  249. $self->{data} = <AH>;
  250. close(AH);
  251. }
  252. }
  253. return $self->{data};
  254. }
  255. =over
  256. =item C<datasize>
  257. the length (in characters) of the attachment content
  258. =back
  259. =cut
  260. # datasize is a property of the data itself, and it's unclear whether we should
  261. # expose it at all, since you can easily derive it from the data itself: in TT,
  262. # attachment.data.size; in Perl, length($attachment->{data}). But perhaps
  263. # it makes sense for performance reasons, since accessing the data forces it
  264. # to get retrieved from the database/filesystem and loaded into memory,
  265. # while datasize avoids loading the attachment into memory, calling SQL's
  266. # LENGTH() function or stat()ing the file instead. I've left it in for now.
  267. sub datasize {
  268. my $self = shift;
  269. return $self->{datasize} if exists $self->{datasize};
  270. # If we have already retrieved the data, return its size.
  271. return length($self->{data}) if exists $self->{data};
  272. $self->{datasize} =
  273. Bugzilla->dbh->selectrow_array("SELECT LENGTH(thedata)
  274. FROM attach_data
  275. WHERE id = ?",
  276. undef, $self->{id}) || 0;
  277. # If there's no attachment data in the database, either the attachment
  278. # is stored in a local file, and so retrieve its size from the file,
  279. # or the attachment has been deleted.
  280. unless ($self->{datasize}) {
  281. if (open(AH, $self->_get_local_filename())) {
  282. binmode AH;
  283. $self->{datasize} = (stat(AH))[7];
  284. close(AH);
  285. }
  286. }
  287. return $self->{datasize};
  288. }
  289. =over
  290. =item C<flags>
  291. flags that have been set on the attachment
  292. =back
  293. =cut
  294. sub flags {
  295. my $self = shift;
  296. return $self->{flags} if exists $self->{flags};
  297. $self->{flags} = Bugzilla::Flag->match({ 'attach_id' => $self->id });
  298. return $self->{flags};
  299. }
  300. # Instance methods; no POD documentation here yet because the only ones so far
  301. # are private.
  302. sub _get_local_filename {
  303. my $self = shift;
  304. my $hash = ($self->id % 100) + 100;
  305. $hash =~ s/.*(\d\d)$/group.$1/;
  306. return bz_locations()->{'attachdir'} . "/$hash/attachment." . $self->id;
  307. }
  308. sub _validate_filename {
  309. my ($throw_error) = @_;
  310. my $cgi = Bugzilla->cgi;
  311. defined $cgi->upload('data')
  312. || ($throw_error ? ThrowUserError("file_not_specified") : return 0);
  313. my $filename = $cgi->upload('data');
  314. # Remove path info (if any) from the file name. The browser should do this
  315. # for us, but some are buggy. This may not work on Mac file names and could
  316. # mess up file names with slashes in them, but them's the breaks. We only
  317. # use this as a hint to users downloading attachments anyway, so it's not
  318. # a big deal if it munges incorrectly occasionally.
  319. $filename =~ s/^.*[\/\\]//;
  320. # Truncate the filename to 100 characters, counting from the end of the
  321. # string to make sure we keep the filename extension.
  322. $filename = substr($filename, -100, 100);
  323. return $filename;
  324. }
  325. sub _validate_data {
  326. my ($throw_error, $hr_vars) = @_;
  327. my $cgi = Bugzilla->cgi;
  328. my $maxsize = $cgi->param('ispatch') ? Bugzilla->params->{'maxpatchsize'}
  329. : Bugzilla->params->{'maxattachmentsize'};
  330. $maxsize *= 1024; # Convert from K
  331. my $fh;
  332. # Skip uploading into a local variable if the user wants to upload huge
  333. # attachments into local files.
  334. if (!$cgi->param('bigfile')) {
  335. $fh = $cgi->upload('data');
  336. }
  337. my $data;
  338. # We could get away with reading only as much as required, except that then
  339. # we wouldn't have a size to print to the error handler below.
  340. if (!$cgi->param('bigfile')) {
  341. # enable 'slurp' mode
  342. local $/;
  343. $data = <$fh>;
  344. }
  345. $data
  346. || ($cgi->param('bigfile'))
  347. || ($throw_error ? ThrowUserError("zero_length_file") : return 0);
  348. # Windows screenshots are usually uncompressed BMP files which
  349. # makes for a quick way to eat up disk space. Let's compress them.
  350. # We do this before we check the size since the uncompressed version
  351. # could easily be greater than maxattachmentsize.
  352. if (Bugzilla->params->{'convert_uncompressed_images'}
  353. && $cgi->param('contenttype') eq 'image/bmp') {
  354. require Image::Magick;
  355. my $img = Image::Magick->new(magick=>'bmp');
  356. $img->BlobToImage($data);
  357. $img->set(magick=>'png');
  358. my $imgdata = $img->ImageToBlob();
  359. $data = $imgdata;
  360. $cgi->param('contenttype', 'image/png');
  361. $hr_vars->{'convertedbmp'} = 1;
  362. }
  363. # Make sure the attachment does not exceed the maximum permitted size
  364. my $len = $data ? length($data) : 0;
  365. if ($maxsize && $len > $maxsize) {
  366. my $vars = { filesize => sprintf("%.0f", $len/1024) };
  367. if ($cgi->param('ispatch')) {
  368. $throw_error ? ThrowUserError("patch_too_large", $vars) : return 0;
  369. }
  370. else {
  371. $throw_error ? ThrowUserError("file_too_large", $vars) : return 0;
  372. }
  373. }
  374. return $data || '';
  375. }
  376. =pod
  377. =head2 Class Methods
  378. =over
  379. =item C<get_attachments_by_bug($bug_id)>
  380. Description: retrieves and returns the attachments the currently logged in
  381. user can view for the given bug.
  382. Params: C<$bug_id> - integer - the ID of the bug for which
  383. to retrieve and return attachments.
  384. Returns: a reference to an array of attachment objects.
  385. =cut
  386. sub get_attachments_by_bug {
  387. my ($class, $bug_id) = @_;
  388. my $user = Bugzilla->user;
  389. my $dbh = Bugzilla->dbh;
  390. # By default, private attachments are not accessible, unless the user
  391. # is in the insider group or submitted the attachment.
  392. my $and_restriction = '';
  393. my @values = ($bug_id);
  394. unless ($user->is_insider) {
  395. $and_restriction = 'AND (isprivate = 0 OR submitter_id = ?)';
  396. push(@values, $user->id);
  397. }
  398. my $attach_ids = $dbh->selectcol_arrayref("SELECT attach_id FROM attachments
  399. WHERE bug_id = ? $and_restriction",
  400. undef, @values);
  401. my $attachments = Bugzilla::Attachment->get_list($attach_ids);
  402. return $attachments;
  403. }
  404. =pod
  405. =item C<validate_is_patch()>
  406. Description: validates the "patch" flag passed in by CGI.
  407. Returns: 1 on success.
  408. =cut
  409. sub validate_is_patch {
  410. my ($class, $throw_error) = @_;
  411. my $cgi = Bugzilla->cgi;
  412. # Set the ispatch flag to zero if it is undefined, since the UI uses
  413. # an HTML checkbox to represent this flag, and unchecked HTML checkboxes
  414. # do not get sent in HTML requests.
  415. $cgi->param('ispatch', $cgi->param('ispatch') ? 1 : 0);
  416. # Set the content type to text/plain if the attachment is a patch.
  417. $cgi->param('contenttype', 'text/plain') if $cgi->param('ispatch');
  418. return 1;
  419. }
  420. =pod
  421. =item C<validate_description()>
  422. Description: validates the description passed in by CGI.
  423. Returns: 1 on success.
  424. =cut
  425. sub validate_description {
  426. my ($class, $throw_error) = @_;
  427. my $cgi = Bugzilla->cgi;
  428. $cgi->param('description')
  429. || ($throw_error ? ThrowUserError("missing_attachment_description") : return 0);
  430. return 1;
  431. }
  432. =pod
  433. =item C<validate_content_type()>
  434. Description: validates the content type passed in by CGI.
  435. Returns: 1 on success.
  436. =cut
  437. sub validate_content_type {
  438. my ($class, $throw_error) = @_;
  439. my $cgi = Bugzilla->cgi;
  440. if (!defined $cgi->param('contenttypemethod')) {
  441. $throw_error ? ThrowUserError("missing_content_type_method") : return 0;
  442. }
  443. elsif ($cgi->param('contenttypemethod') eq 'autodetect') {
  444. my $contenttype =
  445. $cgi->uploadInfo($cgi->param('data'))->{'Content-Type'};
  446. # The user asked us to auto-detect the content type, so use the type
  447. # specified in the HTTP request headers.
  448. if ( !$contenttype ) {
  449. $throw_error ? ThrowUserError("missing_content_type") : return 0;
  450. }
  451. $cgi->param('contenttype', $contenttype);
  452. }
  453. elsif ($cgi->param('contenttypemethod') eq 'list') {
  454. # The user selected a content type from the list, so use their
  455. # selection.
  456. $cgi->param('contenttype', $cgi->param('contenttypeselection'));
  457. }
  458. elsif ($cgi->param('contenttypemethod') eq 'manual') {
  459. # The user entered a content type manually, so use their entry.
  460. $cgi->param('contenttype', $cgi->param('contenttypeentry'));
  461. }
  462. else {
  463. $throw_error ?
  464. ThrowCodeError("illegal_content_type_method",
  465. { contenttypemethod => $cgi->param('contenttypemethod') }) :
  466. return 0;
  467. }
  468. if ( $cgi->param('contenttype') !~
  469. /^(application|audio|image|message|model|multipart|text|video)\/.+$/ ) {
  470. $throw_error ?
  471. ThrowUserError("invalid_content_type",
  472. { contenttype => $cgi->param('contenttype') }) :
  473. return 0;
  474. }
  475. return 1;
  476. }
  477. =pod
  478. =item C<validate_can_edit($attachment, $product_id)>
  479. Description: validates if the user is allowed to view and edit the attachment.
  480. Only the submitter or someone with editbugs privs can edit it.
  481. Only the submitter and users in the insider group can view
  482. private attachments.
  483. Params: $attachment - the attachment object being edited.
  484. $product_id - the product ID the attachment belongs to.
  485. Returns: 1 on success. Else an error is thrown.
  486. =cut
  487. sub validate_can_edit {
  488. my ($attachment, $product_id) = @_;
  489. my $user = Bugzilla->user;
  490. # The submitter can edit their attachments.
  491. return 1 if ($attachment->attacher->id == $user->id
  492. || ((!$attachment->isprivate || $user->is_insider)
  493. && $user->in_group('editbugs', $product_id)));
  494. # If we come here, then this attachment cannot be seen by the user.
  495. ThrowUserError('illegal_attachment_edit', { attach_id => $attachment->id });
  496. }
  497. =item C<validate_obsolete($bug)>
  498. Description: validates if attachments the user wants to mark as obsolete
  499. really belong to the given bug and are not already obsolete.
  500. Moreover, a user cannot mark an attachment as obsolete if
  501. he cannot view it (due to restrictions on it).
  502. Params: $bug - The bug object obsolete attachments should belong to.
  503. Returns: 1 on success. Else an error is thrown.
  504. =cut
  505. sub validate_obsolete {
  506. my ($class, $bug) = @_;
  507. my $cgi = Bugzilla->cgi;
  508. # Make sure the attachment id is valid and the user has permissions to view
  509. # the bug to which it is attached. Make sure also that the user can view
  510. # the attachment itself.
  511. my @obsolete_attachments;
  512. foreach my $attachid ($cgi->param('obsolete')) {
  513. my $vars = {};
  514. $vars->{'attach_id'} = $attachid;
  515. detaint_natural($attachid)
  516. || ThrowCodeError('invalid_attach_id_to_obsolete', $vars);
  517. my $attachment = Bugzilla::Attachment->get($attachid);
  518. # Make sure the attachment exists in the database.
  519. ThrowUserError('invalid_attach_id', $vars) unless $attachment;
  520. # Check that the user can view and edit this attachment.
  521. $attachment->validate_can_edit($bug->product_id);
  522. $vars->{'description'} = $attachment->description;
  523. if ($attachment->bug_id != $bug->bug_id) {
  524. $vars->{'my_bug_id'} = $bug->bug_id;
  525. $vars->{'attach_bug_id'} = $attachment->bug_id;
  526. ThrowCodeError('mismatched_bug_ids_on_obsolete', $vars);
  527. }
  528. if ($attachment->isobsolete) {
  529. ThrowCodeError('attachment_already_obsolete', $vars);
  530. }
  531. push(@obsolete_attachments, $attachment);
  532. }
  533. return @obsolete_attachments;
  534. }
  535. =pod
  536. =item C<insert_attachment_for_bug($throw_error, $bug, $user, $timestamp, $hr_vars)>
  537. Description: inserts an attachment from CGI input for the given bug.
  538. Params: C<$bug> - Bugzilla::Bug object - the bug for which to insert
  539. the attachment.
  540. C<$user> - Bugzilla::User object - the user we're inserting an
  541. attachment for.
  542. C<$timestamp> - scalar - timestamp of the insert as returned
  543. by SELECT NOW().
  544. C<$hr_vars> - hash reference - reference to a hash of template
  545. variables.
  546. Returns: the ID of the new attachment.
  547. =cut
  548. sub insert_attachment_for_bug {
  549. my ($class, $throw_error, $bug, $user, $timestamp, $hr_vars) = @_;
  550. my $cgi = Bugzilla->cgi;
  551. my $dbh = Bugzilla->dbh;
  552. my $attachurl = $cgi->param('attachurl') || '';
  553. my $data;
  554. my $filename;
  555. my $contenttype;
  556. my $isurl;
  557. $class->validate_is_patch($throw_error) || return;
  558. $class->validate_description($throw_error) || return;
  559. if (Bugzilla->params->{'allow_attach_url'}
  560. && ($attachurl =~ /^(http|https|ftp):\/\/\S+/)
  561. && !defined $cgi->upload('data'))
  562. {
  563. $filename = '';
  564. $data = $attachurl;
  565. $isurl = 1;
  566. $contenttype = 'text/plain';
  567. $cgi->param('ispatch', 0);
  568. $cgi->delete('bigfile');
  569. }
  570. else {
  571. $filename = _validate_filename($throw_error) || return;
  572. # need to validate content type before data as
  573. # we now check the content type for image/bmp in _validate_data()
  574. unless ($cgi->param('ispatch')) {
  575. $class->validate_content_type($throw_error) || return;
  576. # Set the ispatch flag to 1 if we're set to autodetect
  577. # and the content type is text/x-diff or text/x-patch
  578. if ($cgi->param('contenttypemethod') eq 'autodetect'
  579. && $cgi->param('contenttype') =~ m{text/x-(?:diff|patch)})
  580. {
  581. $cgi->param('ispatch', 1);
  582. $cgi->param('contenttype', 'text/plain');
  583. }
  584. }
  585. $data = _validate_data($throw_error, $hr_vars);
  586. # If the attachment is stored locally, $data eq ''.
  587. # If an error is thrown, $data eq '0'.
  588. ($data ne '0') || return;
  589. $contenttype = $cgi->param('contenttype');
  590. # These are inserted using placeholders so no need to panic
  591. trick_taint($filename);
  592. trick_taint($contenttype);
  593. $isurl = 0;
  594. }
  595. # Check attachments the user tries to mark as obsolete.
  596. my @obsolete_attachments;
  597. if ($cgi->param('obsolete')) {
  598. @obsolete_attachments = $class->validate_obsolete($bug);
  599. }
  600. # The order of these function calls is important, as Flag::validate
  601. # assumes User::match_field has ensured that the
  602. # values in the requestee fields are legitimate user email addresses.
  603. my $match_status = Bugzilla::User::match_field($cgi, {
  604. '^requestee(_type)?-(\d+)$' => { 'type' => 'multi' },
  605. }, MATCH_SKIP_CONFIRM);
  606. $hr_vars->{'match_field'} = 'requestee';
  607. if ($match_status == USER_MATCH_FAILED) {
  608. $hr_vars->{'message'} = 'user_match_failed';
  609. }
  610. elsif ($match_status == USER_MATCH_MULTIPLE) {
  611. $hr_vars->{'message'} = 'user_match_multiple';
  612. }
  613. # Escape characters in strings that will be used in SQL statements.
  614. my $description = $cgi->param('description');
  615. trick_taint($description);
  616. my $isprivate = $cgi->param('isprivate') ? 1 : 0;
  617. # Insert the attachment into the database.
  618. my $sth = $dbh->do(
  619. "INSERT INTO attachments
  620. (bug_id, creation_ts, modification_time, filename, description,
  621. mimetype, ispatch, isurl, isprivate, submitter_id)
  622. VALUES (?,?,?,?,?,?,?,?,?,?)", undef, ($bug->bug_id, $timestamp, $timestamp,
  623. $filename, $description, $contenttype, $cgi->param('ispatch'),
  624. $isurl, $isprivate, $user->id));
  625. # Retrieve the ID of the newly created attachment record.
  626. my $attachid = $dbh->bz_last_key('attachments', 'attach_id');
  627. # We only use $data here in this INSERT with a placeholder,
  628. # so it's safe.
  629. $sth = $dbh->prepare("INSERT INTO attach_data
  630. (id, thedata) VALUES ($attachid, ?)");
  631. trick_taint($data);
  632. $sth->bind_param(1, $data, $dbh->BLOB_TYPE);
  633. $sth->execute();
  634. # If the file is to be stored locally, stream the file from the web server
  635. # to the local file without reading it into a local variable.
  636. if ($cgi->param('bigfile')) {
  637. my $attachdir = bz_locations()->{'attachdir'};
  638. my $fh = $cgi->upload('data');
  639. my $hash = ($attachid % 100) + 100;
  640. $hash =~ s/.*(\d\d)$/group.$1/;
  641. mkdir "$attachdir/$hash", 0770;
  642. chmod 0770, "$attachdir/$hash";
  643. open(AH, ">$attachdir/$hash/attachment.$attachid");
  644. binmode AH;
  645. my $sizecount = 0;
  646. my $limit = (Bugzilla->params->{"maxlocalattachment"} * 1048576);
  647. while (<$fh>) {
  648. print AH $_;
  649. $sizecount += length($_);
  650. if ($sizecount > $limit) {
  651. close AH;
  652. close $fh;
  653. unlink "$attachdir/$hash/attachment.$attachid";
  654. $throw_error ? ThrowUserError("local_file_too_large") : return;
  655. }
  656. }
  657. close AH;
  658. close $fh;
  659. }
  660. # Make existing attachments obsolete.
  661. my $fieldid = get_field_id('attachments.isobsolete');
  662. foreach my $obsolete_attachment (@obsolete_attachments) {
  663. # If the obsolete attachment has request flags, cancel them.
  664. # This call must be done before updating the 'attachments' table.
  665. Bugzilla::Flag->CancelRequests($bug, $obsolete_attachment, $timestamp);
  666. $dbh->do('UPDATE attachments SET isobsolete = 1, modification_time = ?
  667. WHERE attach_id = ?',
  668. undef, ($timestamp, $obsolete_attachment->id));
  669. $dbh->do('INSERT INTO bugs_activity (bug_id, attach_id, who, bug_when,
  670. fieldid, removed, added)
  671. VALUES (?,?,?,?,?,?,?)',
  672. undef, ($bug->bug_id, $obsolete_attachment->id, $user->id,
  673. $timestamp, $fieldid, 0, 1));
  674. }
  675. my $attachment = Bugzilla::Attachment->get($attachid);
  676. # 1. Add flags, if any. To avoid dying if something goes wrong
  677. # while processing flags, we will eval() flag validation.
  678. # This requires errors to die().
  679. # XXX: this can go away as soon as flag validation is able to
  680. # fail without dying.
  681. #
  682. # 2. Flag::validate() should not detect any reference to existing flags
  683. # when creating a new attachment. Setting the third param to -1 will
  684. # force this function to check this point.
  685. my $error_mode_cache = Bugzilla->error_mode;
  686. Bugzilla->error_mode(ERROR_MODE_DIE);
  687. eval {
  688. Bugzilla::Flag::validate($bug->bug_id, -1, SKIP_REQUESTEE_ON_ERROR);
  689. Bugzilla::Flag->process($bug, $attachment, $timestamp, $hr_vars);
  690. };
  691. Bugzilla->error_mode($error_mode_cache);
  692. if ($@) {
  693. $hr_vars->{'message'} = 'flag_creation_failed';
  694. $hr_vars->{'flag_creation_error'} = $@;
  695. }
  696. # Return the new attachment object.
  697. return $attachment;
  698. }
  699. =pod
  700. =item C<remove_from_db()>
  701. Description: removes an attachment from the DB.
  702. Params: none
  703. Returns: nothing
  704. =back
  705. =cut
  706. sub remove_from_db {
  707. my $self = shift;
  708. my $dbh = Bugzilla->dbh;
  709. $dbh->bz_start_transaction();
  710. $dbh->do('DELETE FROM flags WHERE attach_id = ?', undef, $self->id);
  711. $dbh->do('DELETE FROM attach_data WHERE id = ?', undef, $self->id);
  712. $dbh->do('UPDATE attachments SET mimetype = ?, ispatch = ?, isurl = ?, isobsolete = ?
  713. WHERE attach_id = ?', undef, ('text/plain', 0, 0, 1, $self->id));
  714. $dbh->bz_commit_transaction();
  715. }
  716. 1;