quips.cgi 5.17 KB
Newer Older
1
#!/usr/bin/perl -wT
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
# -*- 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): Owen Taylor <otaylor@redhat.com>
22
#                 Gervase Markham <gerv@gerv.net>
23
#                 David Fallon <davef@tetsubo.com>
24
#                 Tobias Burnus <burnus@net-b.de>
25 26

use strict;
27

28
use lib qw(. lib);
29

30
use Bugzilla;
31
use Bugzilla::Constants;
32 33 34
use Bugzilla::Util;
use Bugzilla::Error;
use Bugzilla::User;
35
use Bugzilla::Token;
36

37
my $user = Bugzilla->login(LOGIN_REQUIRED);
38

39
my $cgi = Bugzilla->cgi;
40
my $dbh = Bugzilla->dbh;
41 42
my $template = Bugzilla->template;
my $vars = {};
43

44
my $action = $cgi->param('action') || "";
45
my $token = $cgi->param('token');
46

47
if ($action eq "show") {
48
    # Read in the entire quip list
49 50
    my $quipsref = $dbh->selectall_arrayref(
                       "SELECT quipid, userid, quip, approved FROM quips");
51 52 53

    my $quips;
    my @quipids;
54 55
    foreach my $quipref (@$quipsref) {
        my ($quipid, $userid, $quip, $approved) = @$quipref;
56 57
        $quips->{$quipid} = {'userid' => $userid, 'quip' => $quip, 
                             'approved' => $approved};
58 59 60 61
        push(@quipids, $quipid);
    }

    my $users;
62
    my $sth = $dbh->prepare("SELECT login_name FROM profiles WHERE userid = ?");
63
    foreach my $quipid (@quipids) {
64
        my $userid = $quips->{$quipid}{'userid'};
65
        if ($userid && not defined $users->{$userid}) {
66
            ($users->{$userid}) = $dbh->selectrow_array($sth, undef, $userid);
67 68 69 70 71
        }
    }
    $vars->{'quipids'} = \@quipids;
    $vars->{'quips'} = $quips;
    $vars->{'users'} = $users;
72
    $vars->{'show_quips'} = 1;
73 74
}

75
if ($action eq "add") {
76
    (Bugzilla->params->{'quip_list_entry_control'} eq "closed") &&
77 78
      ThrowUserError("no_new_quips");

79
    check_hash_token($token, ['create-quips']);
80
    # Add the quip 
81
    my $approved = (Bugzilla->params->{'quip_list_entry_control'} eq "open")
82
                   || $user->in_group('bz_quip_moderators') || 0;
83
    my $comment = $cgi->param("quip");
84
    $comment || ThrowUserError("need_quip");
85
    trick_taint($comment); # Used in a placeholder below
86

87
    $dbh->do("INSERT INTO quips (userid, quip, approved) VALUES (?, ?, ?)",
88
             undef, ($user->id, $comment, $approved));
89 90 91 92

    $vars->{'added_quip'} = $comment;
}

93
if ($action eq 'approve') {
94 95
    $user->in_group('bz_quip_moderators')
      || ThrowUserError("auth_failure", {group  => "bz_quip_moderators",
96 97
                                         action => "approve",
                                         object => "quips"});
98 99

    check_hash_token($token, ['approve-quips']);
100
    # Read in the entire quip list
101 102
    my $quipsref = $dbh->selectall_arrayref("SELECT quipid, approved FROM quips");
    
103
    my %quips;
104 105
    foreach my $quipref (@$quipsref) {
        my ($quipid, $approved) = @$quipref;
106 107 108 109 110 111
        $quips{$quipid} = $approved;
    }

    my @approved;
    my @unapproved;
    foreach my $quipid (keys %quips) {
112 113 114 115 116 117 118 119 120 121 122 123
        # Must check for each quipid being defined for concurrency and
        # automated usage where only one quipid might be defined.
        my $quip = $cgi->param("quipid_$quipid") ? 1 : 0;
        if(defined($cgi->param("defined_quipid_$quipid"))) {
            if($quips{$quipid} != $quip) {
                if($quip) { 
                    push(@approved, $quipid); 
                } else { 
                    push(@unapproved, $quipid); 
                }
            }
        }
124
    }
125
    $dbh->do("UPDATE quips SET approved = 1 WHERE quipid IN (" .
126
            join(",", @approved) . ")") if($#approved > -1);
127
    $dbh->do("UPDATE quips SET approved = 0 WHERE quipid IN (" .
128 129 130 131 132
            join(",", @unapproved) . ")") if($#unapproved > -1);
    $vars->{ 'approved' }   = \@approved;
    $vars->{ 'unapproved' } = \@unapproved;
}

133
if ($action eq "delete") {
134 135
    $user->in_group('bz_quip_moderators')
      || ThrowUserError("auth_failure", {group  => "bz_quip_moderators",
136 137
                                         action => "delete",
                                         object => "quips"});
138
    my $quipid = $cgi->param("quipid");
139 140
    ThrowCodeError("need_quipid") unless $quipid =~ /(\d+)/; 
    $quipid = $1;
141
    check_hash_token($token, ['quips', $quipid]);
142

143 144 145 146
    ($vars->{'deleted_quip'}) = $dbh->selectrow_array(
                                    "SELECT quip FROM quips WHERE quipid = ?",
                                    undef, $quipid);
    $dbh->do("DELETE FROM quips WHERE quipid = ?", undef, $quipid);
147 148
}

149
print $cgi->header();
150 151
$template->process("list/quips.html.tmpl", $vars)
  || ThrowTemplateError($template->error());