From ebb34e29a355f11e38c9af3b587b9261adab7ce7 Mon Sep 17 00:00:00 2001 From: Aevann1 Date: Sat, 21 Aug 2021 14:30:54 +0200 Subject: [PATCH] df --- files/classes/submission.py | 2 +- files/helpers/sanitize.py | 135 +++++++++++++++------------------ files/templates/admins.html | 2 +- files/templates/settings2.html | 2 +- 4 files changed, 64 insertions(+), 77 deletions(-) diff --git a/files/classes/submission.py b/files/classes/submission.py index c31a521b81..21da28405c 100644 --- a/files/classes/submission.py +++ b/files/classes/submission.py @@ -393,7 +393,7 @@ class Submission(Base, Stndrd, Age_times, Scores, Fuzzing): @property def is_image(self): - if self.url: return self.url.lower().endswith('.jpg') or self.url.lower().endswith('.png') or self.url.lower().endswith('.gif') or self.url.lower().endswith('.jpeg') or self.url.lower().endswith('?maxwidth=9999') or self.url.lower().endswith('?maxwidth=8888') + if self.url: return self.url.lower().endswith('.jpg') or self.url.lower().endswith('.png') or self.url.lower().endswith('.gif') or self.url.lower().endswith('.jpeg') or self.url.lower().endswith('?maxwidth=9999') else: return False @property diff --git a/files/helpers/sanitize.py b/files/helpers/sanitize.py index 827d24fe56..746cc6df9b 100644 --- a/files/helpers/sanitize.py +++ b/files/helpers/sanitize.py @@ -59,8 +59,7 @@ _allowed_styles =[ # filter to make all links show domain on hover - -def a_modify(attrs, new=False): +def a_modify(attrs): raw_url=attrs.get((None, "href"), None) if raw_url: @@ -85,16 +84,14 @@ def a_modify(attrs, new=False): return attrs +def sanitize(sanitized): + sanitized = sanitized.replace("\ufeff", "").replace("m.youtube.com", "youtube.com") + for i in re.finditer('https://i.imgur.com/(.*?)\.(jpg|png|jpeg|)', sanitized): + sanitized = sanitized.replace(i.group(1), i.group(1) + "_d." + i.group(2)) - -_clean_wo_links = bleach.Cleaner(tags=_allowed_tags, - attributes=_allowed_attributes, - protocols=_allowed_protocols, - ) - -_clean_w_links = bleach.Cleaner(tags=_allowed_tags, + sanitized = bleach.Cleaner(tags=_allowed_tags, attributes=_allowed_attributes, protocols=_allowed_protocols, styles=_allowed_styles, @@ -104,74 +101,64 @@ _clean_w_links = bleach.Cleaner(tags=_allowed_tags, callbacks=[a_modify] ) ] - ) + ).clean(sanitized) + + #soupify + soup = BeautifulSoup(sanitized, features="html.parser") + + #img elements - embed + for tag in soup.find_all("img"): + + url = tag.get("src", "") + if not url: continue + + if "profile-pic-20" not in tag.get("class", ""): + #print(tag.get('class')) + # set classes and wrap in link + + tag["rel"] = "nofollow" + tag["style"] = "max-height: 100px; max-width: 100%;" + tag["class"] = "in-comment-image rounded-sm my-2" + + link = soup.new_tag("a") + link["href"] = tag["src"] + link["rel"] = "nofollow noopener" + link["target"] = "_blank" + + link["onclick"] = f"expandDesktopImage('{tag['src']}');" + link["data-toggle"] = "modal" + link["data-target"] = "#expandImageModal" + + tag.wrap(link) + + #disguised link preventer + for tag in soup.find_all("a"): + + if re.match("https?://\S+", str(tag.string)): + try: + tag.string = tag["href"] + except: + tag.string = "" + + #clean up tags in code + for tag in soup.find_all("code"): + tag.contents=[x.string for x in tag.contents if x.string] + + #whatever else happens with images, there are only two sets of classes allowed + for tag in soup.find_all("img"): + if 'profile-pic-20' not in tag.attrs.get("class",""): + tag.attrs['class']="in-comment-image rounded-sm my-2" + + #table format + for tag in soup.find_all("table"): + tag.attrs['class']="table table-striped" + + for tag in soup.find_all("thead"): + tag.attrs['class']="bg-primary text-white" -def sanitize(text, linkgen=False): + sanitized = str(soup) - text = text.replace("\ufeff", "").replace("m.youtube.com", "youtube.com") - - if linkgen: - sanitized = _clean_w_links.clean(text) - - #soupify - soup = BeautifulSoup(sanitized, features="html.parser") - - #img elements - embed - for tag in soup.find_all("img"): - - url = tag.get("src", "") - if not url: continue - - if "profile-pic-20" not in tag.get("class", ""): - #print(tag.get('class')) - # set classes and wrap in link - - tag["rel"] = "nofollow" - tag["style"] = "max-height: 100px; max-width: 100%;" - tag["class"] = "in-comment-image rounded-sm my-2" - - link = soup.new_tag("a") - link["href"] = tag["src"] - link["rel"] = "nofollow noopener" - link["target"] = "_blank" - - link["onclick"] = f"expandDesktopImage('{tag['src']}');" - link["data-toggle"] = "modal" - link["data-target"] = "#expandImageModal" - - tag.wrap(link) - - #disguised link preventer - for tag in soup.find_all("a"): - - if re.match("https?://\S+", str(tag.string)): - try: - tag.string = tag["href"] - except: - tag.string = "" - - #clean up tags in code - for tag in soup.find_all("code"): - tag.contents=[x.string for x in tag.contents if x.string] - - #whatever else happens with images, there are only two sets of classes allowed - for tag in soup.find_all("img"): - if 'profile-pic-20' not in tag.attrs.get("class",""): - tag.attrs['class']="in-comment-image rounded-sm my-2" - - #table format - for tag in soup.find_all("table"): - tag.attrs['class']="table table-striped" - - for tag in soup.find_all("thead"): - tag.attrs['class']="bg-primary text-white" - - - sanitized = str(soup) - - else: - sanitized = _clean_wo_links.clean(text) start = '<s>' end = '</s>' diff --git a/files/templates/admins.html b/files/templates/admins.html index 4c1fd7d5de..280d7d5b43 100644 --- a/files/templates/admins.html +++ b/files/templates/admins.html @@ -4,7 +4,7 @@ {% block content %}

-
admins
+
Admins

 
diff --git a/files/templates/settings2.html b/files/templates/settings2.html
index 850fe2ca2c..b2662ad139 100644
--- a/files/templates/settings2.html
+++ b/files/templates/settings2.html
@@ -98,7 +98,7 @@
 										Leaderboard