ban-contributors.pl 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. # Copyright (C) 2013 Alex Schroeder <alex@gnu.org>
  2. # This program is free software: you can redistribute it and/or modify it under
  3. # the terms of the GNU General Public License as published by the Free Software
  4. # Foundation, either version 3 of the License, or (at your option) any later
  5. # version.
  6. #
  7. # This program is distributed in the hope that it will be useful, but WITHOUT
  8. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  10. #
  11. # You should have received a copy of the GNU General Public License along with
  12. # this program. If not, see <http://www.gnu.org/licenses/>.
  13. =head1 Ban Contributors Extension
  14. This module adds "Ban contributors" to the administration page. If you
  15. click on it, it will list all the recent contributors to the page
  16. you've been looking at. Each contributor (IP or hostname) will be
  17. compared to the list of regular expressions on the C<BannedHosts> page
  18. (see C<$BannedHosts>). If the contributor is already banned, this is
  19. mentioned. If the contributor is not banned, you'll see a button
  20. allowing you to ban him or her immediately. If you click the button,
  21. the IP or hostname will be added to the C<BannedHosts> page for you.
  22. =cut
  23. use strict;
  24. use v5.10;
  25. our ($q, $Now, %Page, $OpenPageName, %Action, $UrlPattern, $BannedContent, $BannedHosts, @MyAdminCode);
  26. AddModuleDescription('ban-contributors.pl', 'Ban Contributors Extension');
  27. push(@MyAdminCode, \&BanMenu);
  28. sub BanMenu {
  29. my ($id, $menuref, $restref) = @_;
  30. if ($id and UserIsAdmin()) {
  31. push(@$menuref, ScriptLink('action=ban;id=' . UrlEncode($id),
  32. T('Ban contributors')));
  33. }
  34. }
  35. $Action{ban} = \&DoBanHosts;
  36. sub IsItBanned {
  37. my ($it, $regexps) = @_;
  38. my $re = undef;
  39. foreach my $regexp (@$regexps) {
  40. eval { $re = qr/$regexp/i; };
  41. if (defined($re) && $it =~ $re) {
  42. return $it;
  43. }
  44. }
  45. }
  46. sub DoBanHosts {
  47. my $id = shift;
  48. my $content = GetParam('content', '');
  49. my $host = GetParam('host', '');
  50. if ($content) {
  51. SetParam('text', GetPageContent($BannedContent)
  52. . $content . " # " . CalcDay($Now) . " "
  53. . NormalToFree($id) . "\n");
  54. SetParam('summary', NormalToFree($id));
  55. DoPost($BannedContent);
  56. } elsif ($host) {
  57. $host =~ s/\./\\./g;
  58. SetParam('text', GetPageContent($BannedHosts)
  59. . "^" . $host . " # " . CalcDay($Now) . " "
  60. . NormalToFree($id) . "\n");
  61. SetParam('summary', NormalToFree($id));
  62. DoPost($BannedHosts);
  63. } else {
  64. ValidIdOrDie($id);
  65. print GetHeader('', Ts('Ban Contributors to %s', NormalToFree($id)));
  66. SetParam('rcidonly', $id);
  67. SetParam('all', 1);
  68. SetParam('showedit', 1);
  69. my %contrib = ();
  70. for my $line (GetRcLines()) {
  71. $contrib{$line->[4]}->{$line->[5]} = 1 if $line->[4];
  72. }
  73. my @regexps = ();
  74. foreach (split(/\n/, GetPageContent($BannedHosts))) {
  75. if (/^\s*([^#]\S+)/) { # all lines except empty lines and comments, trim whitespace
  76. push(@regexps, $1);
  77. }
  78. }
  79. print '<div class="content ban">';
  80. foreach (sort(keys %contrib)) {
  81. my $name = $_;
  82. delete $contrib{$_}{''};
  83. $name .= " (" . join(", ", sort(keys(%{$contrib{$_}}))) . ")";
  84. if (IsItBanned($_, \@regexps)) {
  85. print $q->p(Ts("%s is banned", $name));
  86. } else {
  87. print GetFormStart(undef, 'get', 'ban'),
  88. GetHiddenValue('action', 'ban'),
  89. GetHiddenValue('id', $id),
  90. GetHiddenValue('host', $_),
  91. GetHiddenValue('recent_edit', 'on'),
  92. $q->p($name, $q->submit(T('Ban!'))), $q->end_form();
  93. }
  94. }
  95. }
  96. PrintFooter();
  97. }
  98. =head2 Rollback
  99. If you are an admin and rolled back a single page, this extension will
  100. list the URLs your rollback removed (assuming that those URLs are part
  101. of the spam) and it will allow you to provide a regular expression
  102. that will be added to BannedHosts.
  103. =cut
  104. *OldBanContributorsWriteRcLog = \&WriteRcLog;
  105. *WriteRcLog = \&NewBanContributorsWriteRcLog;
  106. sub NewBanContributorsWriteRcLog {
  107. my ($tag, $id, $to) = @_;
  108. if ($tag eq '[[rollback]]' and $id and $to > 0
  109. and $OpenPageName eq $id and UserIsAdmin()) {
  110. # we currently have the clean page loaded, so we need to reload
  111. # the spammed revision (there is a possible race condition here)
  112. my $old = GetTextRevision($Page{revision} - 1, 1)->{text};
  113. my %urls = map {$_ => 1 } $old =~ /$UrlPattern/g;
  114. # we open the file again to force a load of the despammed page
  115. foreach my $url ($Page{text} =~ /$UrlPattern/g) {
  116. delete($urls{$url});
  117. }
  118. # we also remove any candidates that are already banned
  119. my @regexps = ();
  120. foreach (split(/\n/, GetPageContent($BannedContent))) {
  121. if (/^\s*([^#]\S+)/) { # all lines except empty lines and comments, trim whitespace
  122. push(@regexps, $1);
  123. }
  124. }
  125. foreach my $url (keys %urls) {
  126. delete($urls{$url}) if IsItBanned($url, \@regexps);
  127. }
  128. if (keys %urls) {
  129. print $q->p(Ts("These URLs were rolled back. Perhaps you want to add a regular expression to %s?",
  130. GetPageLink($BannedContent)));
  131. print $q->pre(join("\n", sort keys %urls));
  132. print GetFormStart(undef, 'get', 'ban'),
  133. GetHiddenValue('action', 'ban'),
  134. GetHiddenValue('id', $id),
  135. GetHiddenValue('recent_edit', 'on'),
  136. $q->p($q->label({-for=>'content'}, T('Regular expression:')), " ",
  137. $q->textfield(-name=>'content', -size=>30), " ",
  138. $q->submit(T('Ban!'))),
  139. $q->end_form();
  140. };
  141. print $q->p(T("Consider banning the IP number as well:"), ' ',
  142. ScriptLink('action=ban;id=' . UrlEncode($id), T('Ban contributors')));
  143. };
  144. return OldBanContributorsWriteRcLog(@_);
  145. }