|
11 | 11 | from email.Message import Message
|
12 | 12 | from copy import deepcopy
|
13 | 13 | import os.path
|
| 14 | +import socket |
| 15 | +import urllib2 |
14 | 16 |
|
15 | 17 | import ZODB # need this for pychecker
|
16 | 18 | from AccessControl import getSecurityManager, ClassSecurityInfo, Unauthorized
|
|
31 | 33 | import OFS.Image
|
32 | 34 |
|
33 | 35 | from plugins.pagetypes import PAGETYPES
|
34 |
| -from Defaults import DISABLE_JAVASCRIPT, LARGE_FILE_SIZE, LEAVE_PLACEHOLDER |
| 36 | +from Defaults import DISABLE_JAVASCRIPT, LARGE_FILE_SIZE, LEAVE_PLACEHOLDER, \ |
| 37 | + ZWIKI_SPAMPATTERNS_URL, ZWIKI_SPAMPATTERNS_TIMEOUT |
35 | 38 | import Permissions
|
36 | 39 | from Regexps import javascriptexpr, htmlheaderexpr, htmlfooterexpr
|
37 | 40 | from Utils import get_transaction, BLATHER, INFO, parseHeadersBody, isunicode, \
|
@@ -766,16 +769,43 @@ def checkForSpam(self, t=''):
|
766 | 769 | username = self.usernameFrom(REQUEST,ip_address=0)
|
767 | 770 | path = self.getPath()
|
768 | 771 | def forbid(reason):
|
769 |
| - INFO('%s blocked edit from %s (%s), %s' % (path, ip, username, reason)) |
770 |
| - BLATHER('blocked content:\n%s' % t) |
| 772 | + BLATHER('%s blocked edit from %s (%s), %s:\n%s' % (path, ip, username, reason, t)) |
771 | 773 | raise Forbidden, "There was a problem, please contact the site admin."
|
772 | 774 |
|
773 | 775 | # content matches a banned pattern ?
|
774 |
| - pats = getattr(self.folder(),'spampatterns',[]) |
775 |
| - for pat in pats: |
| 776 | + pats = self |
| 777 | + for pat in self.getSpamPatterns(): |
776 | 778 | pat = strip(pat)
|
777 | 779 | if pat and re.search(pat,t): forbid("spam pattern found")
|
778 | 780 |
|
| 781 | + def getSpamPatterns(self): |
| 782 | + """Fetch spam patterns from the global zwiki spam blacklist, or |
| 783 | + a local property. |
| 784 | + """ |
| 785 | + if safe_hasattr(self.folder(), 'spampatterns'): |
| 786 | + return list(getattr(self.folder(),'spampatterns',[])) |
| 787 | + else: |
| 788 | + BLATHER('checking zwiki.org spam blacklist') |
| 789 | + req = urllib2.Request( |
| 790 | + ZWIKI_SPAMPATTERNS_URL, |
| 791 | + None, |
| 792 | + {'User-Agent':'Zwiki %s' % self.zwiki_version()} |
| 793 | + ) |
| 794 | + # have to set timeout this way for python 2.4. XXX safe ? |
| 795 | + saved = socket.getdefaulttimeout() |
| 796 | + socket.setdefaulttimeout(ZWIKI_SPAMPATTERNS_TIMEOUT) |
| 797 | + try: |
| 798 | + response = urllib2.urlopen(req) |
| 799 | + #hdrs = response.info() |
| 800 | + t = response.read() |
| 801 | + except urllib2.URLError, e: |
| 802 | + BLATHER('failed to read blacklist, skipping (%s)' % e) |
| 803 | + t = '' |
| 804 | + finally: |
| 805 | + socket.setdefaulttimeout(saved) |
| 806 | + pats = t.split('\n') |
| 807 | + return pats |
| 808 | + |
779 | 809 | def cleanupText(self, t):
|
780 | 810 | """Clean up incoming text and convert to unicode for internal use."""
|
781 | 811 | def stripcr(t): return re.sub('\r\n','\n',t)
|
|
0 commit comments