diff --git a/files/classes/comment.py b/files/classes/comment.py index e7c2fcc6a..7a97a666e 100644 --- a/files/classes/comment.py +++ b/files/classes/comment.py @@ -11,6 +11,7 @@ from files.helpers.const import * from files.helpers.regex import * from files.helpers.lazy import lazy from files.helpers.sorting_and_time import * +from files.helpers.hosts import current_host from .flags import CommentFlag from .votes import CommentVote from .saves import CommentSaveRelationship @@ -182,12 +183,12 @@ class Comment(Base): @property @lazy def permalink(self): - return f"{SITE_FULL}{self.shortlink}" + return f"{current_host()}{self.shortlink}" @property @lazy def log_link(self): - return f"{SITE_FULL}/transfers/{self.id}" + return f"{current_host()}/transfers/{self.id}" @property @lazy diff --git a/files/classes/submission.py b/files/classes/submission.py index 27dfdea36..aa2093654 100644 --- a/files/classes/submission.py +++ b/files/classes/submission.py @@ -10,6 +10,7 @@ from files.helpers.const import * from files.helpers.regex import * from files.helpers.lazy import lazy from files.helpers.sorting_and_time import make_age_string +from files.helpers.hosts import current_host from .flags import Flag from .comment import Comment, normalize_urls_runtime from .saves import SaveRelationship @@ -138,7 +139,7 @@ class Submission(Base): @property @lazy def permalink(self): - return SITE_FULL + self.shortlink + return current_host() + self.shortlink @property @lazy @@ -163,16 +164,17 @@ class Submission(Base): @property @lazy def thumb_url(self): - if self.over_18: return f"{SITE_FULL}/assets/images/nsfw.webp?v=1" - elif not self.url: return f"{SITE_FULL}/assets/images/{SITE_NAME}/default_text.webp?v=2" + host = current_host() + if self.over_18: return f"{host}/assets/images/nsfw.webp?v=1" + elif not self.url: return f"{host}/assets/images/{SITE_NAME}/default_text.webp?v=2" elif self.thumburl: - if self.thumburl.startswith('/'): return SITE_FULL + self.thumburl + if self.thumburl.startswith('/'): return host + self.thumburl return self.thumburl - elif self.is_youtube or self.is_video: return f"{SITE_FULL}/assets/images/default_thumb_video.webp?v=1" - elif self.is_audio: return f"{SITE_FULL}/assets/images/default_thumb_audio.webp?v=1" + elif self.is_youtube or self.is_video: return f"{host}/assets/images/default_thumb_video.webp?v=1" + elif self.is_audio: return f"{host}/assets/images/default_thumb_audio.webp?v=1" elif self.domain.split('.')[0] == SITE.split('.')[0]: - return f"{SITE_FULL}/assets/images/{SITE_NAME}/site_preview.webp?v=3009" - else: return f"{SITE_FULL}/assets/images/default_thumb_link.webp?v=1" + return f"{host}/assets/images/{SITE_NAME}/site_preview.webp?v=3009" + else: return f"{host}/assets/images/default_thumb_link.webp?v=1" @property @lazy @@ -252,7 +254,7 @@ class Submission(Base): if not url: return '' - if url.startswith('/'): return SITE_FULL + url + if url.startswith('/'): return current_host() + url url = normalize_urls_runtime(url, v) diff --git a/files/classes/user.py b/files/classes/user.py index 99529b9a6..9e1e2de0e 100644 --- a/files/classes/user.py +++ b/files/classes/user.py @@ -24,6 +24,7 @@ from .sub_join import * from .hats import * from files.__main__ import Base, cache from files.helpers.security import * +from files.helpers.hosts import current_host from copy import deepcopy import random from os import remove, path @@ -756,12 +757,15 @@ class User(Base): @property @lazy def profile_url(self): + host = current_host() + if self.hw_zombie < 0: + return f"{host}/assets/images/halloween/zombies/{random.randint(1, 10)}.webp?v=1" if self.agendaposter: return f"{SITE_FULL}/e/chudsey.webp" if self.rainbow: - return f"{SITE_FULL}/e/marseysalutepride.webp" + return f"{host}/e/marseysalutepride.webp" if self.profileurl: - if self.profileurl.startswith('/'): return SITE_FULL + self.profileurl + if self.profileurl.startswith('/'): return host + self.profileurl return self.profileurl return f"{SITE_FULL}/assets/images/default-profile-pic.webp?v=1008" diff --git a/files/helpers/const.py b/files/helpers/const.py index 701b1e0fa..40b14a231 100644 --- a/files/helpers/const.py +++ b/files/helpers/const.py @@ -11,6 +11,7 @@ from os import path SITE = environ.get("SITE").strip() SITE_NAME = environ.get("SITE_NAME").strip() +SITE_HOSTS = environ.get("SITE_HOSTS").strip() SECRET_KEY = environ.get("SECRET_KEY").strip() PROXY_URL = environ.get("PROXY_URL").strip() GIPHY_KEY = environ.get('GIPHY_KEY').strip() diff --git a/files/helpers/hosts.py b/files/helpers/hosts.py new file mode 100644 index 000000000..47eb832ac --- /dev/null +++ b/files/helpers/hosts.py @@ -0,0 +1,8 @@ +from .const import * + +def current_host(): + if SITE == "localhost": prefix = "http://" + else: prefix = "https://" + + if request.host not in SITE_HOSTS: return SITE_FULL + return prefix + request.host diff --git a/files/helpers/jinja2.py b/files/helpers/jinja2.py index b4ad87454..29095f321 100644 --- a/files/helpers/jinja2.py +++ b/files/helpers/jinja2.py @@ -6,6 +6,8 @@ from .const import * import time from files.helpers.assetcache import assetcache_path from files.helpers.wrappers import calc_users +from files.helpers.hosts import current_host +import math @app.template_filter("post_embed") def post_embed(id, v): @@ -52,4 +54,5 @@ def inject_constants(): "EMAIL_REGEX_PATTERN":EMAIL_REGEX_PATTERN, "CONTENT_SECURITY_POLICY_DEFAULT":CONTENT_SECURITY_POLICY_DEFAULT, "CONTENT_SECURITY_POLICY_HOME":CONTENT_SECURITY_POLICY_HOME, + "bar_position": bar_position(), "current_host": current_host() } diff --git a/files/routes/login.py b/files/routes/login.py index bb61b0c9c..b5f880301 100644 --- a/files/routes/login.py +++ b/files/routes/login.py @@ -5,6 +5,7 @@ from files.helpers.const import * from files.helpers.regex import * from files.helpers.actions import * from files.helpers.get import * +from files.helpers.hosts import current_host import requests import secrets @@ -177,7 +178,7 @@ def sign_up_get(v): if not app.config['SETTINGS']['Signups']: return {"error": "New account registration is currently closed. Please come back later."}, 403 - if v: return redirect(SITE_FULL) + if v: return redirect(current_host()) ref = request.values.get("ref") diff --git a/files/routes/posts.py b/files/routes/posts.py index 5113726f7..ed41414c2 100644 --- a/files/routes/posts.py +++ b/files/routes/posts.py @@ -11,6 +11,7 @@ from files.helpers.slots import * from files.helpers.get import * from files.helpers.actions import * from files.helpers.sorting_and_time import * +from files.helpers.hosts import current_host from files.classes import * from flask import * from io import BytesIO @@ -537,7 +538,7 @@ def thumbnail_thread(pid): fetch_url = post.url if fetch_url.startswith('/') and '\\' not in fetch_url: - fetch_url = f"{SITE_FULL}{fetch_url}" + fetch_url = f"{current_host()}{fetch_url}" headers={"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.72 Safari/537.36"} diff --git a/files/routes/users.py b/files/routes/users.py index 3de649521..dfbf9b732 100644 --- a/files/routes/users.py +++ b/files/routes/users.py @@ -9,6 +9,7 @@ from files.helpers.sanitize import * from files.helpers.const import * from files.helpers.sorting_and_time import * from files.helpers.actions import * +from files.helpers.hosts import current_host from files.mail import * from flask import * from files.__main__ import app, limiter, db_session @@ -765,7 +766,7 @@ def u_username(username, v=None): if username != u.username: - return redirect(SITE_FULL + request.full_path.replace(username, u.username)) + return redirect(current_host() + request.full_path.replace(username, u.username)) if v and v.id not in (u.id, DAD_ID) and u.viewers_recorded: g.db.flush()