post_bug.cgi 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. #!/usr/bin/env perl -wT
  2. # -*- Mode: perl; indent-tabs-mode: nil -*-
  3. #
  4. # The contents of this file are subject to the Mozilla Public
  5. # License Version 1.1 (the "License"); you may not use this file
  6. # except in compliance with the License. You may obtain a copy of
  7. # the License at http://www.mozilla.org/MPL/
  8. #
  9. # Software distributed under the License is distributed on an "AS
  10. # IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  11. # implied. See the License for the specific language governing
  12. # rights and limitations under the License.
  13. #
  14. # The Original Code is the Bugzilla Bug Tracking System.
  15. #
  16. # The Initial Developer of the Original Code is Netscape Communications
  17. # Corporation. Portions created by Netscape are
  18. # Copyright (C) 1998 Netscape Communications Corporation. All
  19. # Rights Reserved.
  20. #
  21. # Contributor(s): Terry Weissman <terry@mozilla.org>
  22. # Dan Mosedale <dmose@mozilla.org>
  23. # Joe Robins <jmrobins@tgix.com>
  24. # Gervase Markham <gerv@gerv.net>
  25. # Marc Schumann <wurblzap@gmail.com>
  26. use strict;
  27. use lib qw(. lib);
  28. use Bugzilla;
  29. use Bugzilla::Attachment;
  30. use Bugzilla::BugMail;
  31. use Bugzilla::Constants;
  32. use Bugzilla::Util;
  33. use Bugzilla::Error;
  34. use Bugzilla::Bug;
  35. use Bugzilla::User;
  36. use Bugzilla::Field;
  37. use Bugzilla::Hook;
  38. use Bugzilla::Product;
  39. use Bugzilla::Component;
  40. use Bugzilla::Keyword;
  41. use Bugzilla::Token;
  42. use Bugzilla::Flag;
  43. my $user = Bugzilla->login(LOGIN_REQUIRED);
  44. my $cgi = Bugzilla->cgi;
  45. my $dbh = Bugzilla->dbh;
  46. my $template = Bugzilla->template;
  47. my $vars = {};
  48. ######################################################################
  49. # Main Script
  50. ######################################################################
  51. # redirect to enter_bug if no field is passed.
  52. print $cgi->redirect(correct_urlbase() . 'enter_bug.cgi') unless $cgi->param();
  53. # Detect if the user already used the same form to submit a bug
  54. my $token = trim($cgi->param('token'));
  55. if ($token) {
  56. my ($creator_id, $date, $old_bug_id) = Bugzilla::Token::GetTokenData($token);
  57. unless ($creator_id
  58. && ($creator_id == $user->id)
  59. && ($old_bug_id =~ "^createbug:"))
  60. {
  61. # The token is invalid.
  62. ThrowUserError('token_does_not_exist');
  63. }
  64. $old_bug_id =~ s/^createbug://;
  65. if ($old_bug_id && (!$cgi->param('ignore_token')
  66. || ($cgi->param('ignore_token') != $old_bug_id)))
  67. {
  68. $vars->{'bugid'} = $old_bug_id;
  69. $vars->{'allow_override'} = defined $cgi->param('ignore_token') ? 0 : 1;
  70. print $cgi->header();
  71. $template->process("bug/create/confirm-create-dupe.html.tmpl", $vars)
  72. || ThrowTemplateError($template->error());
  73. exit;
  74. }
  75. }
  76. # do a match on the fields if applicable
  77. &Bugzilla::User::match_field ($cgi, {
  78. 'cc' => { 'type' => 'multi' },
  79. 'assigned_to' => { 'type' => 'single' },
  80. 'qa_contact' => { 'type' => 'single' },
  81. '^requestee_type-(\d+)$' => { 'type' => 'multi' },
  82. });
  83. if (defined $cgi->param('maketemplate')) {
  84. $vars->{'url'} = $cgi->canonicalise_query('token');
  85. $vars->{'short_desc'} = $cgi->param('short_desc');
  86. print $cgi->header();
  87. $template->process("bug/create/make-template.html.tmpl", $vars)
  88. || ThrowTemplateError($template->error());
  89. exit;
  90. }
  91. umask 0;
  92. # get current time
  93. my $timestamp = $dbh->selectrow_array(q{SELECT NOW()});
  94. # Group Validation
  95. my @selected_groups;
  96. foreach my $group (grep(/^bit-\d+$/, $cgi->param())) {
  97. $group =~ /^bit-(\d+)$/;
  98. push(@selected_groups, $1);
  99. }
  100. # The format of the initial comment can be structured by adding fields to the
  101. # enter_bug template and then referencing them in the comment template.
  102. my $comment;
  103. my $format = $template->get_format("bug/create/comment",
  104. scalar($cgi->param('format')), "txt");
  105. $template->process($format->{'template'}, $vars, \$comment)
  106. || ThrowTemplateError($template->error());
  107. # Include custom fields editable on bug creation.
  108. my @custom_bug_fields = grep {$_->type != FIELD_TYPE_MULTI_SELECT && $_->enter_bug}
  109. Bugzilla->active_custom_fields;
  110. # Undefined custom fields are ignored to ensure they will get their default
  111. # value (e.g. "---" for custom single select fields).
  112. my @bug_fields = grep { defined $cgi->param($_->name) } @custom_bug_fields;
  113. @bug_fields = map { $_->name } @bug_fields;
  114. push(@bug_fields, qw(
  115. product
  116. component
  117. assigned_to
  118. qa_contact
  119. alias
  120. blocked
  121. commentprivacy
  122. bug_file_loc
  123. bug_severity
  124. bug_status
  125. dependson
  126. keywords
  127. short_desc
  128. op_sys
  129. priority
  130. rep_platform
  131. version
  132. target_milestone
  133. status_whiteboard
  134. estimated_time
  135. deadline
  136. ));
  137. my %bug_params;
  138. foreach my $field (@bug_fields) {
  139. $bug_params{$field} = $cgi->param($field);
  140. }
  141. $bug_params{'creation_ts'} = $timestamp;
  142. $bug_params{'cc'} = [$cgi->param('cc')];
  143. $bug_params{'groups'} = \@selected_groups;
  144. $bug_params{'comment'} = $comment;
  145. my @multi_selects = grep {$_->type == FIELD_TYPE_MULTI_SELECT && $_->enter_bug}
  146. Bugzilla->active_custom_fields;
  147. foreach my $field (@multi_selects) {
  148. $bug_params{$field->name} = [$cgi->param($field->name)];
  149. }
  150. my $bug = Bugzilla::Bug->create(\%bug_params);
  151. # Get the bug ID back.
  152. my $id = $bug->bug_id;
  153. # Set Version cookie, but only if the user actually selected
  154. # a version on the page.
  155. if (defined $cgi->param('version')) {
  156. $cgi->send_cookie(-name => "VERSION-" . $bug->product,
  157. -value => $bug->version,
  158. -expires => "Fri, 01-Jan-2038 00:00:00 GMT");
  159. }
  160. # We don't have to check if the user can see the bug, because a user filing
  161. # a bug can always see it. You can't change reporter_accessible until
  162. # after the bug is filed.
  163. # Add an attachment if requested.
  164. if (defined($cgi->upload('data')) || $cgi->param('attachurl')) {
  165. $cgi->param('isprivate', $cgi->param('commentprivacy'));
  166. my $attachment = Bugzilla::Attachment->insert_attachment_for_bug(!THROW_ERROR,
  167. $bug, $user, $timestamp, $vars);
  168. if ($attachment) {
  169. # Update the comment to include the new attachment ID.
  170. # This string is hardcoded here because Template::quoteUrls()
  171. # expects to find this exact string.
  172. my $new_comment = "Created an attachment (id=" . $attachment->id . ")\n" .
  173. $attachment->description . "\n";
  174. # We can use $bug->longdescs here because we are sure that the bug
  175. # description is of type CMT_NORMAL. No need to include it if it's
  176. # empty, though.
  177. if ($bug->longdescs->[0]->{'body'} !~ /^\s+$/) {
  178. $new_comment .= "\n" . $bug->longdescs->[0]->{'body'};
  179. }
  180. $bug->update_comment($bug->longdescs->[0]->{'id'}, $new_comment);
  181. }
  182. else {
  183. $vars->{'message'} = 'attachment_creation_failed';
  184. }
  185. # Determine if Patch Viewer is installed, for Diff link
  186. eval {
  187. require PatchReader;
  188. $vars->{'patchviewerinstalled'} = 1;
  189. };
  190. }
  191. # Add flags, if any. To avoid dying if something goes wrong
  192. # while processing flags, we will eval() flag validation.
  193. # This requires errors to die().
  194. # XXX: this can go away as soon as flag validation is able to
  195. # fail without dying.
  196. my $error_mode_cache = Bugzilla->error_mode;
  197. Bugzilla->error_mode(ERROR_MODE_DIE);
  198. eval {
  199. Bugzilla::Flag::validate($id, undef, SKIP_REQUESTEE_ON_ERROR);
  200. Bugzilla::Flag->process($bug, undef, $timestamp, $vars);
  201. };
  202. Bugzilla->error_mode($error_mode_cache);
  203. if ($@) {
  204. $vars->{'message'} = 'flag_creation_failed';
  205. $vars->{'flag_creation_error'} = $@;
  206. }
  207. # Email everyone the details of the new bug
  208. $vars->{'mailrecipients'} = {'changer' => $user->login};
  209. $vars->{'id'} = $id;
  210. $vars->{'bug'} = $bug;
  211. Bugzilla::Hook::process("post_bug-after_creation", { vars => $vars });
  212. ThrowCodeError("bug_error", { bug => $bug }) if $bug->error;
  213. $vars->{'sentmail'} = [];
  214. push (@{$vars->{'sentmail'}}, { type => 'created',
  215. id => $id,
  216. });
  217. foreach my $i (@{$bug->dependson || []}, @{$bug->blocked || []}) {
  218. push (@{$vars->{'sentmail'}}, { type => 'dep', id => $i, });
  219. }
  220. my @bug_list;
  221. if ($cgi->cookie("BUGLIST")) {
  222. @bug_list = split(/:/, $cgi->cookie("BUGLIST"));
  223. }
  224. $vars->{'bug_list'} = \@bug_list;
  225. $vars->{'use_keywords'} = 1 if Bugzilla::Keyword::keyword_count();
  226. if ($token) {
  227. trick_taint($token);
  228. $dbh->do('UPDATE tokens SET eventdata = ? WHERE token = ?', undef,
  229. ("createbug:$id", $token));
  230. }
  231. if (Bugzilla->usage_mode == USAGE_MODE_EMAIL) {
  232. Bugzilla::BugMail::Send($id, $vars->{'mailrecipients'});
  233. }
  234. else {
  235. print $cgi->header();
  236. $template->process("bug/create/created.html.tmpl", $vars)
  237. || ThrowTemplateError($template->error());
  238. }
  239. 1;