aboutsummaryrefslogtreecommitdiffstats
path: root/votes.cgi
blob: a6a86f426c8c14458f6008addf50525929f870e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#!/usr/bonsaitools/bin/perl -wT
# -*- 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>
#                 Stephan Niemz  <st.n@gmx.net>
#                 Christopher Aillon <christopher@aillon.com>
#                 Gervase Markham <gerv@gerv.net>

use diagnostics;
use strict;
use lib ".";

require "CGI.pl";

use vars qw($usergroupset);

# Use global template variables
use vars qw($template $vars);

ConnectToDatabase();

# If the action is show_bug, you need a bug_id.
# If the action is show_user, you can supply a userid to show the votes for
# another user, otherwise you see your own.
# If the action is vote, your votes are set to those encoded in the URL as 
# <bug_id>=<votes>.
#
# If no action is defined, we default to show_bug if a bug_id is given,
# otherwise to show_user.
my $action = $::FORM{'action'} || 
                                 ($::FORM{'bug_id'} ? "show_bug" : "show_user");

if ($action eq "show_bug" ||
    ($action eq "show_user" && defined($::FORM{'user'}))) 
{
    quietly_check_login();
}
else {
    confirm_login();
}

################################################################################
# Begin Data/Security Validation
################################################################################

# Make sure the bug ID is a positive integer representing an existing
# bug that the user is authorized to access.
if (defined $::FORM{'bug_id'}) {
  ValidateBugID($::FORM{'bug_id'});
}

################################################################################
# End Data/Security Validation
################################################################################

if ($action eq "show_bug") {
    show_bug();
} 
elsif ($action eq "show_user") {
    show_user();
}
elsif ($action eq "vote") {
    record_votes();
    show_user();
}
else {
    DisplayError("Unknown action: " . html_quote($action));
}

exit;

# Display the names of all the people voting for this one bug.
sub show_bug {
    my $bug_id = $::FORM{'bug_id'} 
                  || DisplayError("Please give a bug ID to show the votes for.")
                  && exit;
    my $total = 0;
    my @users;
    
    SendSQL("SELECT profiles.login_name, votes.who, votes.count 
             FROM votes, profiles 
             WHERE votes.bug_id = $bug_id 
               AND profiles.userid = votes.who");
                   
    while (MoreSQLData()) {
        my ($name, $userid, $count) = (FetchSQLData());
        push (@users, { name => $name, id => $userid, count => $count });
        $total += $count;
    }
    
    $vars->{'bug_id'} = $bug_id;
    $vars->{'users'} = \@users;
    $vars->{'total'} = $total;
    
    print "Content-type: text/html\n\n";
    $template->process("voting/show-bug-votes.html.tmpl", $vars)
      || DisplayError("Template process failed: " . $template->error())
      && exit;
}

# Display all the votes for a particular user. If it's the user
# doing the viewing, give them the option to edit them too.
sub show_user {
    GetVersionTable();
    
    # If a bug_id is given, and we're editing, we'll add it to the votes list.
    my $bug_id = $::FORM{'bug_id'} || "";
        
    my $name = $::FORM{'user'} || $::COOKIE{'Bugzilla_login'};
    my $who = DBname_to_id($name);
    
    # After DBNameToIdAndCheck is templatised and prints a Content-Type, 
    # the above should revert to a call to that function, and this 
    # special error handling should go away.
    if (!$who) {
        DisplayError(html_quote($name) . " is not a valid username.\n");
        exit;
    }
    
    my $canedit = 1 if ($name eq $::COOKIE{'Bugzilla_login'});
    
    SendSQL("LOCK TABLES bugs READ, products READ, votes WRITE,
             cc AS selectVisible_cc READ");
    
    if ($canedit && $bug_id) {
        # Make sure there is an entry for this bug
        # in the vote table, just so that things display right.
        SendSQL("SELECT votes.count FROM votes 
                 WHERE votes.bug_id = $bug_id AND votes.who = $who");
        if (!FetchOneColumn()) {
            SendSQL("INSERT INTO votes (who, bug_id, count) 
                     VALUES ($who, $bug_id, 0)");
        }
    }
    
    # Calculate the max votes per bug for each product; doing it here means
    # we can do it all in one query.
    my %maxvotesperbug;
    if($canedit) {
        SendSQL("SELECT products.product, products.maxvotesperbug 
                 FROM products");
        while (MoreSQLData()) {
            my ($prod, $max) = FetchSQLData();
            $maxvotesperbug{$prod} = $max;
        }
    }
    
    my @products;
    
    # Read the votes data for this user for each product
    foreach my $product (sort(keys(%::prodmaxvotes))) {
        next if $::prodmaxvotes{$product} <= 0;
        
        my @bugs;
        my $total = 0;
        my $onevoteonly = 0;
        
        SendSQL("SELECT votes.bug_id, votes.count, bugs.short_desc,
                        bugs.bug_status 
                  FROM  votes, bugs 
                  WHERE votes.who = $who 
                    AND votes.bug_id = bugs.bug_id 
                    AND bugs.product = " . SqlQuote($product) . 
                 "ORDER BY votes.bug_id");        
        
        while (MoreSQLData()) {
            my ($id, $count, $summary, $status) = FetchSQLData();
            next if !defined($status);
            $total += $count;
             
            # Next if user can't see this bug. So, the totals will be correct
            # and they can see there are votes 'missing', but not on what bug
            # they are. This seems a reasonable compromise; the alternative is
            # to lie in the totals.
            next if !CanSeeBug($id, $who, $usergroupset);            
            
            push (@bugs, { id => $id, 
                           summary => $summary,
                           count => $count,
                           opened => IsOpenedState($status) });
        }
        
        $onevoteonly = 1 if (min($::prodmaxvotes{$product},
                                 $maxvotesperbug{$product}) == 1);
        
        # Only add the product for display if there are any bugs in it.
        if ($#bugs > -1) {                         
            push (@products, { name => $product,
                               bugs => \@bugs,
                               onevoteonly => $onevoteonly,
                               total => $total,
                               maxvotes => $::prodmaxvotes{$product},
                               maxperbug => $maxvotesperbug{$product} });
        }
    }

    SendSQL("DELETE FROM votes WHERE count <= 0");
    SendSQL("UNLOCK TABLES");
    
    $vars->{'user'} = { canedit => $canedit, name => $name, id => $who };
    $vars->{'products'} = \@products;

    print "Content-type: text/html\n\n";
    $template->process("voting/show-user-votes.html.tmpl", $vars)
      || DisplayError("Template process failed: " . $template->error())
      && exit;
}

# Update the user's votes in the database.
sub record_votes {
    ############################################################################
    # Begin Data/Security Validation
    ############################################################################

    # Build a list of bug IDs for which votes have been submitted.  Votes
    # are submitted in form fields in which the field names are the bug 
    # IDs and the field values are the number of votes.
    my @buglist = grep {/^[1-9][0-9]*$/} keys(%::FORM);

    # If no bugs are in the buglist, let's make sure the user gets notified
    # that their votes will get nuked if they continue.
    if (scalar(@buglist) == 0) {
        if (!defined($::FORM{'delete_all_votes'})) {
            print "Content-type: text/html\n\n";
            $template->process("voting/delete-all-votes.html.tmpl", $vars)
              || DisplayError("Template process failed: " . $template->error());
            exit();
        }
        elsif ($::FORM{'delete_all_votes'} == 0) {
            print "Location: votes.cgi\n\n";
            exit();
        }
    }

    # Call ValidateBugID on each bug ID to make sure it is a positive
    # integer representing an existing bug that the user is authorized 
    # to access, and make sure the number of votes submitted is also
    # a non-negative integer (a series of digits not preceded by a
    # minus sign).
    foreach my $id (@buglist) {
      ValidateBugID($id);
      detaint_natural($::FORM{$id})
        || DisplayError("Only use non-negative numbers for your bug votes.")
        && exit;
    }

    ############################################################################
    # End Data/Security Validation
    ############################################################################

    GetVersionTable();

    my $who = DBNameToIdAndCheck($::COOKIE{'Bugzilla_login'});

    # If the user is voting for bugs, make sure they aren't overstuffing
    # the ballot box.
    if (scalar(@buglist)) {
        SendSQL("SELECT bugs.bug_id, bugs.product, products.maxvotesperbug
                 FROM bugs, products
                 WHERE products.product = bugs.product
                   AND bugs.bug_id IN (" . join(", ", @buglist) . ")");

        my %prodcount;

        while (MoreSQLData()) {
            my ($id, $prod, $max) = FetchSQLData();
            $prodcount{$prod} ||= 0;
            $prodcount{$prod} += $::FORM{$id};
            
            # Make sure we haven't broken the votes-per-bug limit
            if ($::FORM{$id} > $max) {                
                $prod = html_quote($prod);
                my $votes = html_quote($::FORM{$id});
                
                DisplayError("You may only use at most $max votes for a single
                              bug in the <tt>$prod</tt> product, but you are
                              trying to use $votes.", "Illegal vote");
                exit();
            } 
        }

        # Make sure we haven't broken the votes-per-product limit
        foreach my $prod (keys(%prodcount)) {
            if ($prodcount{$prod} > $::prodmaxvotes{$prod}) {
                $prod = html_quote($prod);
                
                DisplayError("You may only use at most $::prodmaxvotes{$prod}
                              votes for bugs in the <tt>$prod</tt> product, 
                              but you are trying to use $prodcount{$prod}.",
                              "Illegal vote");
                exit();
            }
        }
    }

    # Update the user's votes in the database.  If the user did not submit 
    # any votes, they may be using a form with checkboxes to remove all their
    # votes (checkboxes are not submitted along with other form data when
    # they are not checked, and Bugzilla uses them to represent single votes
    # for products that only allow one vote per bug).  In that case, we still
    # need to clear the user's votes from the database.
    my %affected;
    SendSQL("LOCK TABLES bugs write, votes write, products read");
    
    # Take note of, and delete the user's old votes from the database.
    SendSQL("SELECT bug_id FROM votes WHERE who = $who");
    while (MoreSQLData()) {
        my $id = FetchOneColumn();
        $affected{$id} = 1;
    }
    
    SendSQL("DELETE FROM votes WHERE who = $who");
    
    # Insert the new values in their place
    foreach my $id (@buglist) {
        if ($::FORM{$id} > 0) {
            SendSQL("INSERT INTO votes (who, bug_id, count) 
                     VALUES ($who, $id, $::FORM{$id})");
        }
        
        $affected{$id} = 1;
    }
    
    # Update the cached values in the bugs table
    foreach my $id (keys %affected) {
        SendSQL("SELECT sum(count) FROM votes WHERE bug_id = $id");
        my $v = FetchOneColumn();
        $v ||= 0;
        SendSQL("UPDATE bugs SET votes = $v, delta_ts=delta_ts 
                 WHERE bug_id = $id");
        CheckIfVotedConfirmed($id, $who);
    }

    SendSQL("UNLOCK TABLES");

    $vars->{'votes_recorded'} = 1;
}