2008-11-04 Anders Carlsson <andersca@apple.com>
[webkit/qt.git] / BugsSite / enter_bug.cgi
blob3e6b741afbe96375e087b34968facb143fa3023e
1 #!/usr/bin/perl -wT
2 # -*- Mode: perl; indent-tabs-mode: nil -*-
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/
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.
14 # The Original Code is the Bugzilla Bug Tracking System.
16 # The Initial Developer of the Original Code is Netscape Communications
17 # Corporation. Portions created by Netscape are Copyright (C) 1998
18 # Netscape Communications Corporation. All Rights Reserved.
20 # Contributor(s): Terry Weissman <terry@mozilla.org>
21 # Dave Miller <justdave@syndicomm.com>
22 # Joe Robins <jmrobins@tgix.com>
23 # Gervase Markham <gerv@gerv.net>
24 # Shane H. W. Travis <travis@sedsystems.ca>
26 ##############################################################################
28 # enter_bug.cgi
29 # -------------
30 # Displays bug entry form. Bug fields are specified through popup menus,
31 # drop-down lists, or text fields. Default for these values can be
32 # passed in as parameters to the cgi.
34 ##############################################################################
36 use strict;
38 use lib qw(.);
40 use Bugzilla;
41 use Bugzilla::Constants;
42 use Bugzilla::Bug;
43 use Bugzilla::User;
44 require "CGI.pl";
46 use vars qw(
47 $template
48 $vars
49 @enterable_products
50 @legal_opsys
51 @legal_platform
52 @legal_priority
53 @legal_severity
54 @legal_keywords
55 $userid
56 %versions
57 %target_milestone
58 $proddesc
59 $classdesc
62 # If we're using bug groups to restrict bug entry, we need to know who the
63 # user is right from the start.
64 Bugzilla->login(LOGIN_REQUIRED) if AnyEntryGroups();
66 my $cloned_bug;
67 my $cloned_bug_id;
69 my $cgi = Bugzilla->cgi;
71 my $product = $cgi->param('product');
73 if (!defined $product || $product eq "") {
74 GetVersionTable();
75 Bugzilla->login();
77 if ( ! Param('useclassification') ) {
78 # just pick the default one
79 $cgi->param(-name => 'classification', -value => (keys %::classdesc)[0]);
82 if (!$cgi->param('classification')) {
83 my %classdesc;
84 my %classifications;
86 foreach my $c (GetSelectableClassifications()) {
87 my $found = 0;
88 foreach my $p (@enterable_products) {
89 if (CanEnterProduct($p)
90 && IsInClassification($c,$p)) {
91 $found = 1;
94 if ($found) {
95 $classdesc{$c} = $::classdesc{$c};
96 $classifications{$c} = $::classifications{$c};
100 my $classification_size = scalar(keys %classdesc);
101 if ($classification_size == 0) {
102 ThrowUserError("no_products");
104 elsif ($classification_size > 1) {
105 $vars->{'classdesc'} = \%classdesc;
106 $vars->{'classifications'} = \%classifications;
108 $vars->{'target'} = "enter_bug.cgi";
109 $vars->{'format'} = $cgi->param('format');
111 $vars->{'cloned_bug_id'} = $cgi->param('cloned_bug_id');
113 print $cgi->header();
114 $template->process("global/choose-classification.html.tmpl", $vars)
115 || ThrowTemplateError($template->error());
116 exit;
118 $cgi->param(-name => 'classification', -value => (keys %classdesc)[0]);
121 my %products;
122 foreach my $p (@enterable_products) {
123 if (CanEnterProduct($p)) {
124 if (IsInClassification(scalar $cgi->param('classification'),$p) ||
125 $cgi->param('classification') eq "__all") {
126 $products{$p} = $::proddesc{$p};
131 my $prodsize = scalar(keys %products);
132 if ($prodsize == 0) {
133 ThrowUserError("no_products");
135 elsif ($prodsize > 1) {
136 my %classifications;
137 if ( ! Param('useclassification') ) {
138 @{$classifications{"all"}} = keys %products;
140 elsif ($cgi->param('classification') eq "__all") {
141 %classifications = %::classifications;
142 } else {
143 $classifications{$cgi->param('classification')} =
144 $::classifications{$cgi->param('classification')};
146 $vars->{'proddesc'} = \%products;
147 $vars->{'classifications'} = \%classifications;
148 $vars->{'classdesc'} = \%::classdesc;
150 $vars->{'target'} = "enter_bug.cgi";
151 $vars->{'format'} = $cgi->param('format');
153 $vars->{'cloned_bug_id'} = $cgi->param('cloned_bug_id');
155 print $cgi->header();
156 $template->process("global/choose-product.html.tmpl", $vars)
157 || ThrowTemplateError($template->error());
158 exit;
159 } else {
160 # Only one product exists
161 $product = (keys %products)[0];
165 ##############################################################################
166 # Useful Subroutines
167 ##############################################################################
168 sub formvalue {
169 my ($name, $default) = (@_);
170 return $cgi->param($name) || $default || "";
173 # Takes the name of a field and a list of possible values for that
174 # field. Returns the first value in the list that is actually a
175 # valid value for that field.
176 # The field should be named after its DB table.
177 # Returns undef if none of the platforms match.
178 sub pick_valid_field_value (@) {
179 my ($field, @values) = @_;
180 my $dbh = Bugzilla->dbh;
182 foreach my $value (@values) {
183 return $value if $dbh->selectrow_array(
184 "SELECT 1 FROM $field WHERE value = ?", undef, $value);
186 return undef;
189 sub pickplatform {
190 return formvalue("rep_platform") if formvalue("rep_platform");
192 my @platform;
194 if (Param('defaultplatform')) {
195 @platform = Param('defaultplatform');
196 } else {
197 # If @platform is a list, this function will return the first
198 # item in the list that is a valid platform choice. If
199 # no choice is valid, we return "Other".
200 for ($ENV{'HTTP_USER_AGENT'}) {
201 #PowerPC
202 /\(.*PowerPC.*\)/i && do {@platform = "Macintosh"; last;};
203 /\(.*PPC.*\)/ && do {@platform = "Macintosh"; last;};
204 /\(.*AIX.*\)/ && do {@platform = "Macintosh"; last;};
205 #Intel x86
206 /\(.*[ix0-9]86.*\)/ && do {@platform = "PC"; last;};
207 #Versions of Windows that only run on Intel x86
208 /\(.*Win(?:dows )[39M].*\)/ && do {@platform = "PC"; last};
209 /\(.*Win(?:dows )16.*\)/ && do {@platform = "PC"; last;};
210 #Sparc
211 /\(.*sparc.*\)/ && do {@platform = "Sun"; last;};
212 /\(.*sun4.*\)/ && do {@platform = "Sun"; last;};
213 #Alpha
214 /\(.*AXP.*\)/i && do {@platform = "DEC"; last;};
215 /\(.*[ _]Alpha.\D/i && do {@platform = "DEC"; last;};
216 /\(.*[ _]Alpha\)/i && do {@platform = "DEC"; last;};
217 #MIPS
218 /\(.*IRIX.*\)/i && do {@platform = "SGI"; last;};
219 /\(.*MIPS.*\)/i && do {@platform = "SGI"; last;};
220 #68k
221 /\(.*68K.*\)/ && do {@platform = "Macintosh"; last;};
222 /\(.*680[x0]0.*\)/ && do {@platform = "Macintosh"; last;};
224 /\(.*9000.*\)/ && do {@platform = "HP"; last;};
225 #ARM
226 # /\(.*ARM.*\) && do {$platform = "ARM";};
227 #Stereotypical and broken
228 /\(.*Macintosh.*\)/ && do {@platform = "Macintosh"; last;};
229 /\(.*Mac OS [89].*\)/ && do {@platform = "Macintosh"; last;};
230 /\(Win.*\)/ && do {@platform = "PC"; last;};
231 /\(.*Win(?:dows[ -])NT.*\)/ && do {@platform = "PC"; last;};
232 /\(.*OSF.*\)/ && do {@platform = "DEC"; last;};
233 /\(.*HP-?UX.*\)/i && do {@platform = "HP"; last;};
234 /\(.*IRIX.*\)/i && do {@platform = "SGI"; last;};
235 /\(.*(SunOS|Solaris).*\)/ && do {@platform = "Sun"; last;};
236 #Braindead old browsers who didn't follow convention:
237 /Amiga/ && do {@platform = "Macintosh"; last;};
238 /WinMosaic/ && do {@platform = "PC"; last;};
242 return pick_valid_field_value('rep_platform', @platform) || "Other";
245 sub pickos {
246 if (formvalue('op_sys') ne "") {
247 return formvalue('op_sys');
250 my @os;
252 if (Param('defaultopsys')) {
253 @os = Param('defaultopsys');
254 } else {
255 # This function will return the first
256 # item in @os that is a valid platform choice. If
257 # no choice is valid, we return "Other".
258 for ($ENV{'HTTP_USER_AGENT'}) {
259 /\(.*IRIX.*\)/ && do {@os = "IRIX"; last;};
260 /\(.*OSF.*\)/ && do {@os = "OSF/1"; last;};
261 /\(.*Linux.*\)/ && do {@os = "Linux"; last;};
262 /\(.*Solaris.*\)/ && do {@os = "Solaris"; last;};
263 /\(.*SunOS 5.*\)/ && do {@os = "Solaris"; last;};
264 /\(.*SunOS.*sun4u.*\)/ && do {@os = "Solaris"; last;};
265 /\(.*SunOS.*\)/ && do {@os = "SunOS"; last;};
266 /\(.*HP-?UX.*\)/ && do {@os = "HP-UX"; last;};
267 /\(.*BSD\/(?:OS|386).*\)/ && do {@os = "BSDI"; last;};
268 /\(.*FreeBSD.*\)/ && do {@os = "FreeBSD"; last;};
269 /\(.*OpenBSD.*\)/ && do {@os = "OpenBSD"; last;};
270 /\(.*NetBSD.*\)/ && do {@os = "NetBSD"; last;};
271 /\(.*BeOS.*\)/ && do {@os = "BeOS"; last;};
272 /\(.*AIX.*\)/ && do {@os = "AIX"; last;};
273 /\(.*OS\/2.*\)/ && do {@os = "OS/2"; last;};
274 /\(.*QNX.*\)/ && do {@os = "Neutrino"; last;};
275 /\(.*VMS.*\)/ && do {@os = "OpenVMS"; last;};
276 /\(.*Windows XP.*\)/ && do {@os = "Windows XP"; last;};
277 /\(.*Windows NT 5\.2.*\)/ && do {@os = "Windows Server 2003"; last;};
278 /\(.*Windows NT 5\.1.*\)/ && do {@os = "Windows XP"; last;};
279 /\(.*Windows 2000.*\)/ && do {@os = "Windows 2000"; last;};
280 /\(.*Windows NT 5.*\)/ && do {@os = "Windows 2000"; last;};
281 /\(.*Win.*9[8x].*4\.9.*\)/ && do {@os = "Windows ME"; last;};
282 /\(.*Win(?:dows )M[Ee].*\)/ && do {@os = "Windows ME"; last;};
283 /\(.*Win(?:dows )98.*\)/ && do {@os = "Windows 98"; last;};
284 /\(.*Win(?:dows )95.*\)/ && do {@os = "Windows 95"; last;};
285 /\(.*Win(?:dows )16.*\)/ && do {@os = "Windows 3.1"; last;};
286 /\(.*Win(?:dows[ -])NT.*\)/ && do {@os = "Windows NT"; last;};
287 /\(.*Windows.*NT.*\)/ && do {@os = "Windows NT"; last;};
288 /\(.*32bit.*\)/ && do {@os = "Windows 95"; last;};
289 /\(.*16bit.*\)/ && do {@os = "Windows 3.1"; last;};
290 /\(.*Mac OS 9.*\)/ && do {@os = "Mac System 9.x"; last;};
291 /\(.*Mac OS 8\.6.*\)/ && do {@os = "Mac System 8.6"; last;};
292 /\(.*Mac OS 8\.5.*\)/ && do {@os = "Mac System 8.5"; last;};
293 # Bugzilla doesn't have an entry for 8.1
294 /\(.*Mac OS 8\.1.*\)/ && do {@os = "Mac System 8.0"; last;};
295 /\(.*Mac OS 8\.0.*\)/ && do {@os = "Mac System 8.0"; last;};
296 /\(.*Mac OS 8[^.].*\)/ && do {@os = "Mac System 8.0"; last;};
297 /\(.*Mac OS 8.*\)/ && do {@os = "Mac System 8.6"; last;};
298 /\(.*Mac OS X.*\)/ && do {@os = "Mac OS X 10.0"; last;};
299 /\(.*Darwin.*\)/ && do {@os = "Mac OS X 10.0"; last;};
300 # Silly
301 /\(.*Mac.*PowerPC.*\)/ && do {@os = "Mac System 9.x"; last;};
302 /\(.*Mac.*PPC.*\)/ && do {@os = "Mac System 9.x"; last;};
303 /\(.*Mac.*68k.*\)/ && do {@os = "Mac System 8.0"; last;};
304 # Evil
305 /Amiga/i && do {@os = "Other"; last;};
306 /WinMosaic/ && do {@os = "Windows 95"; last;};
307 /\(.*PowerPC.*\)/ && do {@os = "Mac System 9.x"; last;};
308 /\(.*PPC.*\)/ && do {@os = "Mac System 9.x"; last;};
309 /\(.*68K.*\)/ && do {@os = "Mac System 8.0"; last;};
313 push(@os, "Windows") if grep(/^Windows /, @os);
314 push(@os, "Mac OS") if grep(/^Mac /, @os);
316 return pick_valid_field_value('op_sys', @os) || "Other";
318 ##############################################################################
319 # End of subroutines
320 ##############################################################################
322 Bugzilla->login(LOGIN_REQUIRED) if (!(AnyEntryGroups()));
324 # If a user is trying to clone a bug
325 # Check that the user has authorization to view the parent bug
326 # Create an instance of Bug that holds the info from the parent
327 $cloned_bug_id = $cgi->param('cloned_bug_id');
329 if ($cloned_bug_id) {
330 ValidateBugID($cloned_bug_id);
331 $cloned_bug = new Bugzilla::Bug($cloned_bug_id, $userid);
334 # We need to check and make sure
335 # that the user has permission to enter a bug against this product.
336 CanEnterProductOrWarn($product);
338 GetVersionTable();
340 my $product_id = get_product_id($product);
342 if (1 == @{$::components{$product}}) {
343 # Only one component; just pick it.
344 $cgi->param('component', $::components{$product}->[0]);
347 my @components;
349 my $dbh = Bugzilla->dbh;
350 my $sth = $dbh->prepare(
351 q{SELECT name, description, p1.login_name, p2.login_name
352 FROM components
353 LEFT JOIN profiles p1 ON components.initialowner = p1.userid
354 LEFT JOIN profiles p2 ON components.initialqacontact = p2.userid
355 WHERE product_id = ?
356 ORDER BY name});
358 $sth->execute($product_id);
359 while (my ($name, $description, $owner, $qacontact)
360 = $sth->fetchrow_array()) {
361 push @components, {
362 name => $name,
363 description => $description,
364 initialowner => $owner,
365 initialqacontact => $qacontact || '',
369 my %default;
371 $vars->{'product'} = $product;
372 $vars->{'component_'} = \@components;
374 $vars->{'priority'} = \@legal_priority;
375 $vars->{'bug_severity'} = \@legal_severity;
376 $vars->{'rep_platform'} = \@legal_platform;
377 $vars->{'op_sys'} = \@legal_opsys;
379 $vars->{'use_keywords'} = 1 if (@::legal_keywords);
381 $vars->{'assigned_to'} = formvalue('assigned_to');
382 $vars->{'assigned_to_disabled'} = !UserInGroup('editbugs');
383 $vars->{'cc_disabled'} = 0;
385 $vars->{'qa_contact'} = formvalue('qa_contact');
386 $vars->{'qa_contact_disabled'} = !UserInGroup('editbugs');
388 $vars->{'cloned_bug_id'} = $cloned_bug_id;
390 if ($cloned_bug_id) {
392 $default{'component_'} = $cloned_bug->{'component'};
393 $default{'priority'} = $cloned_bug->{'priority'};
394 $default{'bug_severity'} = $cloned_bug->{'bug_severity'};
395 $default{'rep_platform'} = $cloned_bug->{'rep_platform'};
396 $default{'op_sys'} = $cloned_bug->{'op_sys'};
398 $vars->{'short_desc'} = $cloned_bug->{'short_desc'};
399 $vars->{'bug_file_loc'} = $cloned_bug->{'bug_file_loc'};
400 $vars->{'keywords'} = $cloned_bug->keywords;
401 $vars->{'dependson'} = $cloned_bug_id;
402 $vars->{'blocked'} = "";
403 $vars->{'deadline'} = $cloned_bug->{'deadline'};
405 if (defined $cloned_bug->cc) {
406 $vars->{'cc'} = join (" ", @{$cloned_bug->cc});
407 } else {
408 $vars->{'cc'} = formvalue('cc');
411 # We need to ensure that we respect the 'insider' status of
412 # the first comment, if it has one. Either way, make a note
413 # that this bug was cloned from another bug.
415 $cloned_bug->longdescs();
416 my $isprivate = $cloned_bug->{'longdescs'}->[0]->{'isprivate'};
418 $vars->{'comment'} = "";
419 $vars->{'commentprivacy'} = 0;
421 if ( !($isprivate) ||
422 ( ( Param("insidergroup") ) &&
423 ( UserInGroup(Param("insidergroup")) ) )
425 $vars->{'comment'} = $cloned_bug->{'longdescs'}->[0]->{'body'};
426 $vars->{'commentprivacy'} = $isprivate;
429 # Ensure that the groupset information is set up for later use.
430 $cloned_bug->groups();
432 } # end of cloned bug entry form
434 else {
436 $default{'component_'} = formvalue('component');
437 $default{'priority'} = formvalue('priority', Param('defaultpriority'));
438 $default{'bug_severity'} = formvalue('bug_severity', Param('defaultseverity'));
439 $default{'rep_platform'} = pickplatform();
440 $default{'op_sys'} = pickos();
442 $vars->{'short_desc'} = formvalue('short_desc');
443 $vars->{'bug_file_loc'} = formvalue('bug_file_loc', "http://");
444 $vars->{'keywords'} = formvalue('keywords');
445 $vars->{'dependson'} = formvalue('dependson');
446 $vars->{'blocked'} = formvalue('blocked');
447 $vars->{'deadline'} = formvalue('deadline');
449 $vars->{'cc'} = join(', ', $cgi->param('cc'));
451 $vars->{'comment'} = formvalue('comment');
452 $vars->{'commentprivacy'} = formvalue('commentprivacy');
454 } # end of normal/bookmarked entry form
457 # IF this is a cloned bug,
458 # AND the clone's product is the same as the parent's
459 # THEN use the version from the parent bug
460 # ELSE IF a version is supplied in the URL
461 # THEN use it
462 # ELSE IF there is a version in the cookie
463 # THEN use it (Posting a bug sets a cookie for the current version.)
464 # ELSE
465 # The default version is the last one in the list (which, it is
466 # hoped, will be the most recent one).
468 # Eventually maybe each product should have a "current version"
469 # parameter.
470 $vars->{'version'} = $::versions{$product} || [];
472 if ( ($cloned_bug_id) &&
473 ("$product" eq "$cloned_bug->{'product'}" ) ) {
474 $default{'version'} = $cloned_bug->{'version'};
475 } elsif (formvalue('version')) {
476 $default{'version'} = formvalue('version');
477 } elsif (defined $cgi->cookie("VERSION-$product") &&
478 lsearch($vars->{'version'}, $cgi->cookie("VERSION-$product")) != -1) {
479 $default{'version'} = $cgi->cookie("VERSION-$product");
480 } else {
481 $default{'version'} = $vars->{'version'}->[$#{$vars->{'version'}}];
484 # Get list of milestones.
485 if ( Param('usetargetmilestone') ) {
486 $vars->{'target_milestone'} = $::target_milestone{$product};
487 if (formvalue('target_milestone')) {
488 $default{'target_milestone'} = formvalue('target_milestone');
489 } else {
490 SendSQL("SELECT defaultmilestone FROM products WHERE " .
491 "name = " . SqlQuote($product));
492 $default{'target_milestone'} = FetchOneColumn();
497 # List of status values for drop-down.
498 my @status;
500 # Construct the list of allowable values. There are three cases:
502 # case values
503 # product does not have confirmation NEW
504 # confirmation, user cannot confirm UNCONFIRMED
505 # confirmation, user can confirm NEW, UNCONFIRMED.
507 SendSQL("SELECT votestoconfirm FROM products WHERE name = " .
508 SqlQuote($product));
509 if (FetchOneColumn()) {
510 if (UserInGroup("editbugs") || UserInGroup("canconfirm")) {
511 push(@status, "NEW");
513 push(@status, 'UNCONFIRMED');
514 } else {
515 push(@status, "NEW");
518 $vars->{'bug_status'} = \@status;
520 # Get the default from a template value if it is legitimate.
521 # Otherwise, set the default to the first item on the list.
523 if (formvalue('bug_status') && (lsearch(\@status, formvalue('bug_status')) >= 0)) {
524 $default{'bug_status'} = formvalue('bug_status');
525 } else {
526 $default{'bug_status'} = $status[0];
529 SendSQL("SELECT DISTINCT groups.id, groups.name, groups.description, " .
530 "membercontrol, othercontrol " .
531 "FROM groups LEFT JOIN group_control_map " .
532 "ON group_id = id AND product_id = $product_id " .
533 "WHERE isbuggroup != 0 AND isactive != 0 ORDER BY description");
535 my @groups;
537 while (MoreSQLData()) {
538 my ($id, $groupname, $description, $membercontrol, $othercontrol)
539 = FetchSQLData();
540 # Only include groups if the entering user will have an option.
541 next if ((!$membercontrol)
542 || ($membercontrol == CONTROLMAPNA)
543 || ($membercontrol == CONTROLMAPMANDATORY)
544 || (($othercontrol != CONTROLMAPSHOWN)
545 && ($othercontrol != CONTROLMAPDEFAULT)
546 && (!UserInGroup($groupname)))
548 my $check;
550 # If this is a cloned bug,
551 # AND the product for this bug is the same as for the original
552 # THEN set a group's checkbox if the original also had it on
553 # ELSE IF this is a bookmarked template
554 # THEN set a group's checkbox if was set in the bookmark
555 # ELSE
556 # set a groups's checkbox based on the group control map
558 if ( ($cloned_bug_id) &&
559 ("$product" eq "$cloned_bug->{'product'}" ) ) {
560 foreach my $i (0..(@{$cloned_bug->{'groups'}}-1) ) {
561 if ($cloned_bug->{'groups'}->[$i]->{'bit'} == $id) {
562 $check = $cloned_bug->{'groups'}->[$i]->{'ison'};
566 elsif(formvalue("maketemplate") ne "") {
567 $check = formvalue("bit-$id", 0);
569 else {
570 # Checkbox is checked by default if $control is a default state.
571 $check = (($membercontrol == CONTROLMAPDEFAULT)
572 || (($othercontrol == CONTROLMAPDEFAULT)
573 && (!UserInGroup($groupname))));
576 my $group =
578 'bit' => $id ,
579 'checked' => $check ,
580 'description' => $description
583 push @groups, $group;
586 $vars->{'group'} = \@groups;
588 $vars->{'default'} = \%default;
590 my $format =
591 GetFormat("bug/create/create", scalar $cgi->param('format'),
592 scalar $cgi->param('ctype'));
594 print $cgi->header($format->{'ctype'});
595 $template->process($format->{'template'}, $vars)
596 || ThrowTemplateError($template->error());