Skip to content

Commit

Permalink
Detect link in posts better
Browse files Browse the repository at this point in the history
Credit goes to Josef Kokeš
  • Loading branch information
hroncok committed Nov 22, 2014
1 parent 013a024 commit a21dfa9
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
5 changes: 4 additions & 1 deletion models/__init__.py
Expand Up @@ -69,6 +69,8 @@
from askbot import mail

from django import VERSION
import HTMLParser


#stores the 1.X version not the security release numbers
DJANGO_VERSION = VERSION[:2]
Expand Down Expand Up @@ -758,7 +760,8 @@ def user_assert_can_post_text(self, text):
"""Raises exceptions.PermissionDenied, if user does not have
privilege to post given text, depending on the contents
"""
if re.search(URL_RE, text):
html_parser = HTMLParser.HTMLParser()
if re.search(URL_RE, html_parser.unescape(text).lower()):
min_rep = askbot_settings.MIN_REP_TO_SUGGEST_LINK
if self.is_authenticated() and self.reputation < min_rep:
message = _(
Expand Down
4 changes: 2 additions & 2 deletions utils/markup.py
Expand Up @@ -12,8 +12,8 @@
from askbot.utils.html import urlize_html
from django.utils.html import urlize
from markdown2 import Markdown
#url taken from http://regexlib.com/REDetails.aspx?regexp_id=501
URL_RE = re.compile("((?<!(href|.src|data)=['\"])((http|https|ftp)\://([a-zA-Z0-9\.\-]+(\:[a-zA-Z0-9\.&amp;%\$\-]+)*@)*((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|localhost|([a-zA-Z0-9\-]+\.)*[a-zA-Z0-9\-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(\:[0-9]+)*(/($|[a-zA-Z0-9\.\,\?\'\\\+&amp;%\$#\=~_\-]+))*))")
#url taken from https://askfit.cz/question/1076/testovaci-spam-2/?answer=1077#post-id-1077
URL_RE = re.compile('(https?|ftp)://')

def get_parser():
"""returns an instance of configured ``markdown2`` parser
Expand Down

0 comments on commit a21dfa9

Please sign in to comment.