blob: b2790be548f6abaf96f905d0590fdfdb8589394a [file] [log] [blame]
ddkilzer@apple.com8040bb02017-03-21 16:27:49 +00001#!/usr/bin/perl -T
2# This Source Code Form is subject to the terms of the Mozilla Public
3# License, v. 2.0. If a copy of the MPL was not distributed with this
4# file, You can obtain one at http://mozilla.org/MPL/2.0/.
timothy@apple.comf42518d2008-02-06 20:19:16 +00005#
ddkilzer@apple.com8040bb02017-03-21 16:27:49 +00006# This Source Code Form is "Incompatible With Secondary Licenses", as
7# defined by the Mozilla Public License, v. 2.0.
timothy@apple.comf42518d2008-02-06 20:19:16 +00008
ddkilzer@apple.com8040bb02017-03-21 16:27:49 +00009use 5.10.1;
timothy@apple.comf42518d2008-02-06 20:19:16 +000010use strict;
ddkilzer@apple.com8040bb02017-03-21 16:27:49 +000011use warnings;
timothy@apple.comf42518d2008-02-06 20:19:16 +000012
ddkilzer@apple.com097da082009-07-03 02:14:25 +000013use lib qw(. lib);
timothy@apple.comf42518d2008-02-06 20:19:16 +000014
ddkilzer@apple.comf3615fc2009-07-03 02:13:41 +000015use Bugzilla;
timothy@apple.comf42518d2008-02-06 20:19:16 +000016use Bugzilla::Constants;
ddkilzer@apple.comf3615fc2009-07-03 02:13:41 +000017use Bugzilla::Util;
18use Bugzilla::Error;
19use Bugzilla::User;
ddkilzer@apple.com57772842014-10-16 16:00:58 +000020use Bugzilla::Token;
timothy@apple.comf42518d2008-02-06 20:19:16 +000021
ddkilzer@apple.comf3615fc2009-07-03 02:13:41 +000022my $user = Bugzilla->login(LOGIN_REQUIRED);
timothy@apple.comf42518d2008-02-06 20:19:16 +000023
24my $cgi = Bugzilla->cgi;
ddkilzer@apple.comf3615fc2009-07-03 02:13:41 +000025my $dbh = Bugzilla->dbh;
26my $template = Bugzilla->template;
27my $vars = {};
timothy@apple.comf42518d2008-02-06 20:19:16 +000028
29my $action = $cgi->param('action') || "";
ddkilzer@apple.com57772842014-10-16 16:00:58 +000030my $token = $cgi->param('token');
timothy@apple.comf42518d2008-02-06 20:19:16 +000031
32if ($action eq "show") {
33 # Read in the entire quip list
ddkilzer@apple.comf3615fc2009-07-03 02:13:41 +000034 my $quipsref = $dbh->selectall_arrayref(
ddkilzer@apple.com8040bb02017-03-21 16:27:49 +000035 "SELECT quipid, userid, quip, approved FROM quips ORDER BY quipid");
timothy@apple.comf42518d2008-02-06 20:19:16 +000036
37 my $quips;
38 my @quipids;
ddkilzer@apple.comf3615fc2009-07-03 02:13:41 +000039 foreach my $quipref (@$quipsref) {
40 my ($quipid, $userid, $quip, $approved) = @$quipref;
timothy@apple.comf42518d2008-02-06 20:19:16 +000041 $quips->{$quipid} = {'userid' => $userid, 'quip' => $quip,
42 'approved' => $approved};
43 push(@quipids, $quipid);
44 }
45
46 my $users;
ddkilzer@apple.comf3615fc2009-07-03 02:13:41 +000047 my $sth = $dbh->prepare("SELECT login_name FROM profiles WHERE userid = ?");
timothy@apple.comf42518d2008-02-06 20:19:16 +000048 foreach my $quipid (@quipids) {
49 my $userid = $quips->{$quipid}{'userid'};
50 if ($userid && not defined $users->{$userid}) {
ddkilzer@apple.comf3615fc2009-07-03 02:13:41 +000051 ($users->{$userid}) = $dbh->selectrow_array($sth, undef, $userid);
timothy@apple.comf42518d2008-02-06 20:19:16 +000052 }
53 }
54 $vars->{'quipids'} = \@quipids;
55 $vars->{'quips'} = $quips;
56 $vars->{'users'} = $users;
57 $vars->{'show_quips'} = 1;
58}
59
60if ($action eq "add") {
ddkilzer@apple.comf3615fc2009-07-03 02:13:41 +000061 (Bugzilla->params->{'quip_list_entry_control'} eq "closed") &&
timothy@apple.comf42518d2008-02-06 20:19:16 +000062 ThrowUserError("no_new_quips");
63
ddkilzer@apple.com57772842014-10-16 16:00:58 +000064 check_hash_token($token, ['create-quips']);
timothy@apple.comf42518d2008-02-06 20:19:16 +000065 # Add the quip
ddkilzer@apple.comf3615fc2009-07-03 02:13:41 +000066 my $approved = (Bugzilla->params->{'quip_list_entry_control'} eq "open")
ddkilzer@apple.com57772842014-10-16 16:00:58 +000067 || $user->in_group('bz_quip_moderators') || 0;
timothy@apple.comf42518d2008-02-06 20:19:16 +000068 my $comment = $cgi->param("quip");
69 $comment || ThrowUserError("need_quip");
ddkilzer@apple.com8040bb02017-03-21 16:27:49 +000070
71 ThrowUserError("quip_too_long", { length => length($comment) })
72 if length($comment) > MAX_QUIP_LENGTH;
73
ddkilzer@apple.comf3615fc2009-07-03 02:13:41 +000074 trick_taint($comment); # Used in a placeholder below
timothy@apple.comf42518d2008-02-06 20:19:16 +000075
ddkilzer@apple.comf3615fc2009-07-03 02:13:41 +000076 $dbh->do("INSERT INTO quips (userid, quip, approved) VALUES (?, ?, ?)",
77 undef, ($user->id, $comment, $approved));
timothy@apple.comf42518d2008-02-06 20:19:16 +000078
79 $vars->{'added_quip'} = $comment;
ddkilzer@apple.com8040bb02017-03-21 16:27:49 +000080 $vars->{'message'} = 'quips_added';
timothy@apple.comf42518d2008-02-06 20:19:16 +000081}
82
83if ($action eq 'approve') {
ddkilzer@apple.com57772842014-10-16 16:00:58 +000084 $user->in_group('bz_quip_moderators')
85 || ThrowUserError("auth_failure", {group => "bz_quip_moderators",
ddkilzer@apple.com097da082009-07-03 02:14:25 +000086 action => "approve",
87 object => "quips"});
ddkilzer@apple.com57772842014-10-16 16:00:58 +000088
89 check_hash_token($token, ['approve-quips']);
timothy@apple.comf42518d2008-02-06 20:19:16 +000090 # Read in the entire quip list
ddkilzer@apple.comf3615fc2009-07-03 02:13:41 +000091 my $quipsref = $dbh->selectall_arrayref("SELECT quipid, approved FROM quips");
92
timothy@apple.comf42518d2008-02-06 20:19:16 +000093 my %quips;
ddkilzer@apple.comf3615fc2009-07-03 02:13:41 +000094 foreach my $quipref (@$quipsref) {
95 my ($quipid, $approved) = @$quipref;
timothy@apple.comf42518d2008-02-06 20:19:16 +000096 $quips{$quipid} = $approved;
97 }
98
99 my @approved;
100 my @unapproved;
101 foreach my $quipid (keys %quips) {
ddkilzer@apple.com097da082009-07-03 02:14:25 +0000102 # Must check for each quipid being defined for concurrency and
103 # automated usage where only one quipid might be defined.
104 my $quip = $cgi->param("quipid_$quipid") ? 1 : 0;
105 if(defined($cgi->param("defined_quipid_$quipid"))) {
106 if($quips{$quipid} != $quip) {
107 if($quip) {
108 push(@approved, $quipid);
109 } else {
110 push(@unapproved, $quipid);
111 }
112 }
113 }
timothy@apple.comf42518d2008-02-06 20:19:16 +0000114 }
ddkilzer@apple.comf3615fc2009-07-03 02:13:41 +0000115 $dbh->do("UPDATE quips SET approved = 1 WHERE quipid IN (" .
timothy@apple.comf42518d2008-02-06 20:19:16 +0000116 join(",", @approved) . ")") if($#approved > -1);
ddkilzer@apple.comf3615fc2009-07-03 02:13:41 +0000117 $dbh->do("UPDATE quips SET approved = 0 WHERE quipid IN (" .
timothy@apple.comf42518d2008-02-06 20:19:16 +0000118 join(",", @unapproved) . ")") if($#unapproved > -1);
119 $vars->{ 'approved' } = \@approved;
120 $vars->{ 'unapproved' } = \@unapproved;
ddkilzer@apple.com8040bb02017-03-21 16:27:49 +0000121 $vars->{'message'} = 'quips_approved_unapproved';
timothy@apple.comf42518d2008-02-06 20:19:16 +0000122}
123
124if ($action eq "delete") {
ddkilzer@apple.com57772842014-10-16 16:00:58 +0000125 $user->in_group('bz_quip_moderators')
126 || ThrowUserError("auth_failure", {group => "bz_quip_moderators",
timothy@apple.comf42518d2008-02-06 20:19:16 +0000127 action => "delete",
128 object => "quips"});
129 my $quipid = $cgi->param("quipid");
ddkilzer@apple.com8040bb02017-03-21 16:27:49 +0000130 detaint_natural($quipid) || ThrowUserError("need_quipid");
ddkilzer@apple.com57772842014-10-16 16:00:58 +0000131 check_hash_token($token, ['quips', $quipid]);
timothy@apple.comf42518d2008-02-06 20:19:16 +0000132
ddkilzer@apple.comf3615fc2009-07-03 02:13:41 +0000133 ($vars->{'deleted_quip'}) = $dbh->selectrow_array(
134 "SELECT quip FROM quips WHERE quipid = ?",
135 undef, $quipid);
136 $dbh->do("DELETE FROM quips WHERE quipid = ?", undef, $quipid);
ddkilzer@apple.com8040bb02017-03-21 16:27:49 +0000137 $vars->{'message'} = 'quips_deleted';
timothy@apple.comf42518d2008-02-06 20:19:16 +0000138}
139
140print $cgi->header();
141$template->process("list/quips.html.tmpl", $vars)
142 || ThrowTemplateError($template->error());