banned-regexps.pl 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. $FullUrlPattern);
  40. our ($BannedRegexps);
  41. $BannedRegexps = 'BannedRegexps';
  42. push(@MyInitVariables, sub {
  43. $AdminPages{$BannedRegexps} = 1;
  44. $LockOnCreation{$BannedRegexps} = 1;
  45. $PlainTextPages{$BannedRegexps} = 1;
  46. });
  47. *RegexpOldBannedContent = \&BannedContent;
  48. *BannedContent = \&RegexpNewBannedContent;
  49. sub RegexpNewBannedContent {
  50. my $str = shift;
  51. # remove URLs as they are controlled by $BannedContent
  52. $str =~ s/$FullUrlPattern//g;
  53. my $rule = RegexpOldBannedContent($str, @_);
  54. if (not $rule) {
  55. foreach (split(/\n/, GetPageContent($BannedRegexps))) {
  56. next unless m/^\s*([^#]+?)\s*(#\s*(\d\d\d\d-\d\d-\d\d\s*)?(.*))?$/;
  57. my ($regexp, $comment, $re) = ($1, $4, undef);
  58. eval { $re = qr/$regexp/i; };
  59. if (defined($re) && $str =~ $re) {
  60. my $group1 = $1;
  61. my $explanation = ($group1
  62. ? Tss('Regular expression "%1" matched "%2" on this page.', QuoteHtml($regexp), $group1)
  63. : Ts('Regular expression "%s" matched on this page.', QuoteHtml($regexp)));
  64. $rule = $explanation . ' '
  65. . ($comment ? Ts('Reason: %s.', $comment) : T('Reason unknown.')) . ' '
  66. . Ts('See %s for more information.', GetPageLink($BannedRegexps));
  67. last;
  68. }
  69. }
  70. }
  71. LogWrite($rule) if $rule and $BannedFile;
  72. return $rule if $rule;
  73. return 0;
  74. }