quips.cgi 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #!/usr/bin/env perl -wT
  2. # -*- Mode: perl; indent-tabs-mode: nil -*-
  3. #
  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/
  8. #
  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.
  13. #
  14. # The Original Code is the Bugzilla Bug Tracking System.
  15. #
  16. # The Initial Developer of the Original Code is Netscape Communications
  17. # Corporation. Portions created by Netscape are
  18. # Copyright (C) 1998 Netscape Communications Corporation. All
  19. # Rights Reserved.
  20. #
  21. # 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. use strict;
  26. use lib qw(. lib);
  27. use Bugzilla;
  28. use Bugzilla::Constants;
  29. use Bugzilla::Util;
  30. use Bugzilla::Error;
  31. use Bugzilla::User;
  32. my $user = Bugzilla->login(LOGIN_REQUIRED);
  33. my $cgi = Bugzilla->cgi;
  34. my $dbh = Bugzilla->dbh;
  35. my $template = Bugzilla->template;
  36. my $vars = {};
  37. my $action = $cgi->param('action') || "";
  38. if ($action eq "show") {
  39. # Read in the entire quip list
  40. my $quipsref = $dbh->selectall_arrayref(
  41. "SELECT quipid, userid, quip, approved FROM quips");
  42. my $quips;
  43. my @quipids;
  44. foreach my $quipref (@$quipsref) {
  45. my ($quipid, $userid, $quip, $approved) = @$quipref;
  46. $quips->{$quipid} = {'userid' => $userid, 'quip' => $quip,
  47. 'approved' => $approved};
  48. push(@quipids, $quipid);
  49. }
  50. my $users;
  51. my $sth = $dbh->prepare("SELECT login_name FROM profiles WHERE userid = ?");
  52. foreach my $quipid (@quipids) {
  53. my $userid = $quips->{$quipid}{'userid'};
  54. if ($userid && not defined $users->{$userid}) {
  55. ($users->{$userid}) = $dbh->selectrow_array($sth, undef, $userid);
  56. }
  57. }
  58. $vars->{'quipids'} = \@quipids;
  59. $vars->{'quips'} = $quips;
  60. $vars->{'users'} = $users;
  61. $vars->{'show_quips'} = 1;
  62. }
  63. if ($action eq "add") {
  64. (Bugzilla->params->{'quip_list_entry_control'} eq "closed") &&
  65. ThrowUserError("no_new_quips");
  66. # Add the quip
  67. my $approved = (Bugzilla->params->{'quip_list_entry_control'} eq "open")
  68. || Bugzilla->user->in_group('admin') || 0;
  69. my $comment = $cgi->param("quip");
  70. $comment || ThrowUserError("need_quip");
  71. trick_taint($comment); # Used in a placeholder below
  72. $dbh->do("INSERT INTO quips (userid, quip, approved) VALUES (?, ?, ?)",
  73. undef, ($user->id, $comment, $approved));
  74. $vars->{'added_quip'} = $comment;
  75. }
  76. if ($action eq 'approve') {
  77. $user->in_group('admin')
  78. || ThrowUserError("auth_failure", {group => "admin",
  79. action => "approve",
  80. object => "quips"});
  81. # Read in the entire quip list
  82. my $quipsref = $dbh->selectall_arrayref("SELECT quipid, approved FROM quips");
  83. my %quips;
  84. foreach my $quipref (@$quipsref) {
  85. my ($quipid, $approved) = @$quipref;
  86. $quips{$quipid} = $approved;
  87. }
  88. my @approved;
  89. my @unapproved;
  90. foreach my $quipid (keys %quips) {
  91. # Must check for each quipid being defined for concurrency and
  92. # automated usage where only one quipid might be defined.
  93. my $quip = $cgi->param("quipid_$quipid") ? 1 : 0;
  94. if(defined($cgi->param("defined_quipid_$quipid"))) {
  95. if($quips{$quipid} != $quip) {
  96. if($quip) {
  97. push(@approved, $quipid);
  98. } else {
  99. push(@unapproved, $quipid);
  100. }
  101. }
  102. }
  103. }
  104. $dbh->do("UPDATE quips SET approved = 1 WHERE quipid IN (" .
  105. join(",", @approved) . ")") if($#approved > -1);
  106. $dbh->do("UPDATE quips SET approved = 0 WHERE quipid IN (" .
  107. join(",", @unapproved) . ")") if($#unapproved > -1);
  108. $vars->{ 'approved' } = \@approved;
  109. $vars->{ 'unapproved' } = \@unapproved;
  110. }
  111. if ($action eq "delete") {
  112. Bugzilla->user->in_group("admin")
  113. || ThrowUserError("auth_failure", {group => "admin",
  114. action => "delete",
  115. object => "quips"});
  116. my $quipid = $cgi->param("quipid");
  117. ThrowCodeError("need_quipid") unless $quipid =~ /(\d+)/;
  118. $quipid = $1;
  119. ($vars->{'deleted_quip'}) = $dbh->selectrow_array(
  120. "SELECT quip FROM quips WHERE quipid = ?",
  121. undef, $quipid);
  122. $dbh->do("DELETE FROM quips WHERE quipid = ?", undef, $quipid);
  123. }
  124. print $cgi->header();
  125. $template->process("list/quips.html.tmpl", $vars)
  126. || ThrowTemplateError($template->error());