banned-regexps.pl 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. # Copyright (C) 2012 Alex Schroeder <alex@gnu.org>
  2. #
  3. # This program is free software; you can redistribute it and/or modify it under
  4. # the terms of the GNU General Public License as published by the Free Software
  5. # Foundation; either version 3 of the License, or (at your option) any later
  6. # version.
  7. #
  8. # This program is distributed in the hope that it will be useful, but WITHOUT
  9. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  10. # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  11. #
  12. # You should have received a copy of the GNU General Public License along with
  13. # this program. If not, see <http://www.gnu.org/licenses/>.
  14. use strict;
  15. use v5.10;
  16. AddModuleDescription('banned-regexps.pl', 'Banning Regular Expressions');
  17. =encoding utf8
  18. =head1 Compatibility
  19. This extension works with logbannedcontent.pl.
  20. =head1 Example content for the BannedRegexps page:
  21. # This page lists regular expressions that prevent the saving of a page.
  22. # The regexps are matched against any page or comment submitted.
  23. # The format is simple: # comments to the end of the line. Empty lines are ignored.
  24. # Everything else is a regular expression. If the regular expression is followed by
  25. # a comment, this is used as the explanation when a user is denied editing. If the
  26. # comment starts with a date, this date is not included in the explanation.
  27. # The date could help us decide which regular expressions to delete in the future.
  28. # In other words:
  29. # ^\s*([^#]+?)\s*(#\s*(\d\d\d\d-\d\d-\d\d\s*)?(.*))?$
  30. # Group 1 is the regular expression to use.
  31. # Group 4 is the explanation to use.
  32. порно # 2012-12-31 Russian porn
  33. <a\s+href=["']?http # 2012-12-31 HTML anchor tags usually mean spam
  34. \[url= # 2012-12-31 bbCode links usually mean spam
  35. \s+https?:\S+[ .\r\n]*$ # 2012-12-31 ending with a link usually means spam
  36. (?s)\s+https?:\S+.*\s+https?:\S+.*\s+https?:\S+.* # 2012-12-31 three naked links usually mean spam
  37. =cut
  38. our (%AdminPages, %LockOnCreation, @MyInitVariables, %PlainTextPages, $BannedContent, $BannedFile);
  39. our ($BannedRegexps);
  40. $BannedRegexps = 'BannedRegexps';
  41. push(@MyInitVariables, sub {
  42. $AdminPages{$BannedRegexps} = 1;
  43. $LockOnCreation{$BannedRegexps} = 1;
  44. $PlainTextPages{$BannedRegexps} = 1;
  45. });
  46. *RegexpOldBannedContent = \&BannedContent;
  47. *BannedContent = \&RegexpNewBannedContent;
  48. sub RegexpNewBannedContent {
  49. my $str = shift;
  50. my $rule = RegexpOldBannedContent($str, @_);
  51. if (not $rule) {
  52. foreach (split(/\n/, GetPageContent($BannedRegexps))) {
  53. next unless m/^\s*([^#]+?)\s*(#\s*(\d\d\d\d-\d\d-\d\d\s*)?(.*))?$/;
  54. my ($regexp, $comment, $re) = ($1, $4, undef);
  55. eval { $re = qr/$regexp/i; };
  56. if (defined($re) && $str =~ $re) {
  57. my $group1 = $1;
  58. my $explanation = ($group1
  59. ? Tss('Regular expression "%1" matched "%2" on this page.', QuoteHtml($regexp), $group1)
  60. : Ts('Regular expression "%s" matched on this page.', QuoteHtml($regexp)));
  61. $rule = $explanation . ' '
  62. . ($comment ? Ts('Reason: %s.', $comment) : T('Reason unknown.')) . ' '
  63. . Ts('See %s for more information.', GetPageLink($BannedRegexps));
  64. last;
  65. }
  66. }
  67. }
  68. LogWrite($rule) if $rule and $BannedFile;
  69. return $rule if $rule;
  70. return 0;
  71. }