banned-regexps.pl 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. # Copyright (C) 2012-2018 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. # check whether Banned Content complains
  52. my $rule = RegexpOldBannedContent($str, @_);
  53. if (not $rule) {
  54. foreach (split(/\n/, GetPageContent($BannedRegexps))) {
  55. next unless m/^\s*([^#]+?)\s*(#\s*(\d\d\d\d-\d\d-\d\d\s*)?(.*))?$/;
  56. my ($regexp, $comment, $re) = ($1, $4, undef);
  57. eval { $re = qr/($regexp)/i; };
  58. if (defined($re) && $str =~ $re) {
  59. my $group1 = $1;
  60. my $explanation = ($group1
  61. ? Tss('Regular expression "%1" matched "%2" on this page.', QuoteHtml($regexp), $group1)
  62. : Ts('Regular expression "%s" matched on this page.', QuoteHtml($regexp)));
  63. $rule = $explanation . ' '
  64. . ($comment ? Ts('Reason: %s.', $comment) : T('Reason unknown.')) . ' '
  65. . Ts('See %s for more information.', GetPageLink($BannedRegexps));
  66. last;
  67. }
  68. }
  69. }
  70. LogWrite($rule) if $rule and $BannedFile;
  71. return $rule if $rule;
  72. return 0;
  73. }