post_bug.cgi 9.85 KB
Newer Older
1
#!/usr/bonsaitools/bin/perl -wT
2
# -*- Mode: perl; indent-tabs-mode: nil -*-
terry%netscape.com's avatar
terry%netscape.com committed
3
#
4 5 6 7 8 9 10 11 12 13
# 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.
#
terry%netscape.com's avatar
terry%netscape.com committed
14
# The Original Code is the Bugzilla Bug Tracking System.
15
#
terry%netscape.com's avatar
terry%netscape.com committed
16
# The Initial Developer of the Original Code is Netscape Communications
17 18 19 20
# Corporation. Portions created by Netscape are
# Copyright (C) 1998 Netscape Communications Corporation. All
# Rights Reserved.
#
terry%netscape.com's avatar
terry%netscape.com committed
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>
terry%netscape.com's avatar
terry%netscape.com committed
25

26 27
use diagnostics;
use strict;
28 29
use lib qw(.);

30
require "CGI.pl";
terry%netscape.com's avatar
terry%netscape.com committed
31

32
# Shut up misguided -w warnings about "used only once". For some reason,
33
# "use vars" chokes on me when I try it here.
34 35 36
sub sillyness {
    my $zz;
    $zz = $::buffer;
37
    $zz = $::usergroupset;
38
    $zz = %::COOKIE;
39 40 41 42 43 44 45
    $zz = %::components;
    $zz = %::versions;
    $zz = @::legal_opsys;
    $zz = @::legal_platform;
    $zz = @::legal_priority;
    $zz = @::legal_product;
    $zz = @::legal_severity;
46
    $zz = %::target_milestone;
47
}
48

49 50 51
# Use global template variables.
use vars qw($vars $template);

52
confirm_login();
terry%netscape.com's avatar
terry%netscape.com committed
53

54 55 56 57 58 59

# The format of the initial comment can be structured by adding fields to the
# enter_bug template and then referencing them in the comment template.
my $comment;

$vars->{'form'} = \%::FORM;
60 61

$template->process("bug/create/initial-comment.txt.tmpl", $vars, \$comment)
62 63 64
  || ThrowTemplateError($template->error());

ValidateComment($comment);
65

66
my $product = $::FORM{'product'};
dmose%mozilla.org's avatar
dmose%mozilla.org committed
67

68 69 70 71 72 73 74 75 76 77 78
# Set cookies
my $cookiepath = Param("cookiepath");
if (exists $::FORM{'product'}) {
    print "Set-Cookie: PLATFORM=$product ; path=$cookiepath ; \
           expires=Sun, 30-Jun-2029 00:00:00 GMT\n";
    
    if (exists $::FORM{'version'}) {           
        print "Set-Cookie: VERSION-$product=$::FORM{'version'} ; \
               path=$cookiepath ; expires=Sun, 30-Jun-2029 00:00:00 GMT\n"; 
    }
}
terry%netscape.com's avatar
terry%netscape.com committed
79

80
if (defined $::FORM{'maketemplate'}) {
81
    $vars->{'url'} = $::buffer;
terry%netscape.com's avatar
terry%netscape.com committed
82
    
83
    print "Content-type: text/html\n\n";
84 85
    $template->process("bug/create/make-template.html.tmpl", $vars)
      || ThrowTemplateError($template->error());
86
    exit;
terry%netscape.com's avatar
terry%netscape.com committed
87 88
}

89 90
umask 0;
ConnectToDatabase();
terry%netscape.com's avatar
terry%netscape.com committed
91

92
# Some sanity checking
93
if(Param("usebuggroupsentry") && GroupExists($product)) {
94 95 96 97 98
    if(!UserInGroup($product)) {
        DisplayError("Sorry; you do not have the permissions necessary to enter
                      a bug against this product.", "Permission Denied");
        exit;
    }
99 100
}

101 102 103 104
if (!$::FORM{'component'}) {
    DisplayError("You must choose a component that corresponds to this bug.
                  If necessary, just guess.");
    exit;                  
105
}
terry%netscape.com's avatar
terry%netscape.com committed
106

107
if (!defined $::FORM{'short_desc'} || trim($::FORM{'short_desc'}) eq "") {
108 109
    DisplayError("You must enter a summary for this bug.");
    exit;
110 111
}

112 113 114 115 116 117 118 119
# If bug_file_loc is "http://", the default, strip it out and use an empty
# value. 
$::FORM{'bug_file_loc'} = "" if $::FORM{'bug_file_loc'} eq 'http://';
    
my $sql_product = SqlQuote($::FORM{'product'});
my $sql_component = SqlQuote($::FORM{'component'});

# Default assignee is the component owner.
120
if ($::FORM{'assigned_to'} eq "") {
121 122
    SendSQL("SELECT initialowner FROM components " .
            "WHERE program=$sql_product AND value=$sql_component");
123
    $::FORM{'assigned_to'} = FetchOneColumn();
124 125
} else {
    $::FORM{'assigned_to'} = DBNameToIdAndCheck($::FORM{'assigned_to'});
terry%netscape.com's avatar
terry%netscape.com committed
126 127
}

128
my @bug_fields = ("product", "version", "rep_platform",
129
                  "bug_severity", "priority", "op_sys", "assigned_to",
130 131
                  "bug_status", "bug_file_loc", "short_desc", "component",
                  "target_milestone");
132 133

if (Param("useqacontact")) {
134 135 136 137 138
    SendSQL("SELECT initialqacontact FROM components " .
            "WHERE program=$sql_product AND value=$sql_component");
    my $qa_contact = FetchOneColumn();
    if (defined $qa_contact && $qa_contact != 0) {
        $::FORM{'qa_contact'} = $qa_contact;
139 140 141 142
        push(@bug_fields, "qa_contact");
    }
}

143
if (exists $::FORM{'bug_status'}) {
144 145 146 147
    # Ignore the given status, so that we can set it to UNCONFIRMED
    # or NEW, depending on votestoconfirm if either the given state was
    # unconfirmed (so that a user can't override the below check), or if
    # the user doesn't have permission to change the default status anyway
148
    if ($::FORM{'bug_status'} eq $::unconfirmedstate
149
        || (!UserInGroup("canedit") && !UserInGroup("canconfirm"))) {
150 151 152 153 154 155
        delete $::FORM{'bug_status'};
    }
}

if (!exists $::FORM{'bug_status'}) {
    $::FORM{'bug_status'} = $::unconfirmedstate;
156
    SendSQL("SELECT votestoconfirm FROM products WHERE product=$sql_product");
157 158 159 160 161
    if (!FetchOneColumn()) {
        $::FORM{'bug_status'} = "NEW";
    }
}

162
if (!exists $::FORM{'target_milestone'}) {
163
    SendSQL("SELECT defaultmilestone FROM products WHERE product=$sql_product");
164 165 166
    $::FORM{'target_milestone'} = FetchOneColumn();
}

167
if (!Param('letsubmitterchoosepriority')) {
168
    $::FORM{'priority'} = Param('defaultpriority');
169 170
}

171
GetVersionTable();
172 173 174

# Some more sanity checking
CheckFormField(\%::FORM, 'product',      \@::legal_product);
175 176
CheckFormField(\%::FORM, 'rep_platform', \@::legal_platform);
CheckFormField(\%::FORM, 'bug_severity', \@::legal_severity);
177 178 179 180 181 182
CheckFormField(\%::FORM, 'priority',     \@::legal_priority);
CheckFormField(\%::FORM, 'op_sys',       \@::legal_opsys);
CheckFormField(\%::FORM, 'bug_status',   [$::unconfirmedstate, 'NEW']);
CheckFormField(\%::FORM, 'version',          $::versions{$product});
CheckFormField(\%::FORM, 'component',        $::components{$product});
CheckFormField(\%::FORM, 'target_milestone', $::target_milestone{$product});
183 184 185
CheckFormFieldDefined(\%::FORM, 'assigned_to');
CheckFormFieldDefined(\%::FORM, 'bug_file_loc');
CheckFormFieldDefined(\%::FORM, 'comment');
186

187
my @used_fields;
188 189 190
foreach my $field (@bug_fields) {
    if (exists $::FORM{$field}) {
        push (@used_fields, $field);
191 192
    }
}
193 194 195 196

if (exists $::FORM{'bug_status'} 
    && $::FORM{'bug_status'} ne $::unconfirmedstate) 
{
197 198 199
    push(@used_fields, "everconfirmed");
    $::FORM{'everconfirmed'} = 1;
}
200

201 202 203 204
# Build up SQL string to add bug.
my $sql = "INSERT INTO bugs " . 
  "(" . join(",", @used_fields) . ", reporter, creation_ts, groupset) " . 
  "VALUES (";
terry%netscape.com's avatar
terry%netscape.com committed
205

206
foreach my $field (@used_fields) {
207
    $sql .= SqlQuote($::FORM{$field}) . ",";
terry%netscape.com's avatar
terry%netscape.com committed
208 209
}

210
$comment =~ s/\r\n?/\n/g;     # Get rid of \r.
211
$comment = trim($comment);
212
# If comment is all whitespace, it'll be null at this point. That's
213 214
# OK except for the fact that it causes e-mail to be suppressed.
$comment = $comment ? $comment : " ";
215

216
$sql .= "$::userid, now(), (0";
217

218
# Groups
219 220 221
foreach my $b (grep(/^bit-\d*$/, keys %::FORM)) {
    if ($::FORM{$b}) {
        my $v = substr($b, 4);
222 223 224 225 226 227 228 229
        $v =~ /^(\d+)$/
          || PuntTryAgain("One of the group bits submitted was invalid.");
        if (!GroupIsActive($v)) {
            # Prevent the user from adding the bug to an inactive group.
            # Should only happen if there is a bug in Bugzilla or the user
            # hacked the "enter bug" form since otherwise the UI 
            # for adding the bug to the group won't appear on that form.
            PuntTryAgain("You can't add this bug to the inactive group " . 
230
                         "identified by the bit '$v'. This shouldn't happen, " .
231 232
                         "so it may indicate a bug in Bugzilla.");
        }
233 234 235
        $sql .= " + $v";    # Carefully written so that the math is
                            # done by MySQL, which can handle 64-bit math,
                            # and not by Perl, which I *think* can not.
236 237 238
    }
}

239
$sql .= ") & $::usergroupset)\n";
240

241 242 243 244 245
# Lock tables before inserting records for the new bug into the database
# if we are using a shadow database to prevent shadow database corruption
# when two bugs get created at the same time.
SendSQL("LOCK TABLES bugs WRITE, longdescs WRITE, cc WRITE") if Param("shadowdb");

246 247
# Add the bug report to the DB.
SendSQL($sql);
248

249 250 251
# Get the bug ID back.
SendSQL("select LAST_INSERT_ID()");
my $id = FetchOneColumn();
terry%netscape.com's avatar
terry%netscape.com committed
252

253 254 255
# Add the comment
SendSQL("INSERT INTO longdescs (bug_id, who, bug_when, thetext) 
         VALUES ($id, $::userid, now(), " . SqlQuote($comment) . ")");
terry%netscape.com's avatar
terry%netscape.com committed
256

257
my %ccids;
258
my $ccid;
259
my @cc;
terry%netscape.com's avatar
terry%netscape.com committed
260

261
# Add the CC list
262 263 264
if (defined $::FORM{'cc'}) {
    foreach my $person (split(/[ ,]/, $::FORM{'cc'})) {
        if ($person ne "") {
265 266 267 268
            $ccid = DBNameToIdAndCheck($person);
            if ($ccid && !$ccids{$ccid}) {
                SendSQL("INSERT INTO cc (bug_id, who) VALUES ($id, $ccid)");
                $ccids{$ccid} = 1;
269
                push(@cc, $person);
270
            }
terry%netscape.com's avatar
terry%netscape.com committed
271 272 273 274
        }
    }
}

275 276
SendSQL("UNLOCK TABLES") if Param("shadowdb");

277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
# Assemble the -force* strings so this counts as "Added to this capacity"
my @ARGLIST = ();
if (@cc) {
    push (@ARGLIST, "-forcecc", join(",", @cc));
}

push (@ARGLIST, "-forceowner", DBID_to_name($::FORM{assigned_to}));

if (defined $::FORM{qacontact}) {
    push (@ARGLIST, "-forceqacontact", DBID_to_name($::FORM{qacontact}));
}

push (@ARGLIST, "-forcereporter", DBID_to_name($::userid));

push (@ARGLIST, $id, $::COOKIE{'Bugzilla_login'});

293 294 295
# Send mail to let people know the bug has been created.
# See attachment.cgi for explanation of why it's done this way.
my $mailresults = '';
296
open(PMAIL, "-|") or exec('./processmail', @ARGLIST);
297 298
$mailresults .= $_ while <PMAIL>;
close(PMAIL);
terry%netscape.com's avatar
terry%netscape.com committed
299

300 301 302
# Tell the user all about it
$vars->{'bug_id'} = $id;
$vars->{'mailresults'} = $mailresults;
terry%netscape.com's avatar
terry%netscape.com committed
303

304
print "Content-type: text/html\n\n";
305 306
$template->process("bug/create/created.html.tmpl", $vars)
  || ThrowTemplateError($template->error());