2022-04-06 22:54:09 +00:00
|
|
|
import time
|
2023-01-01 14:27:12 +00:00
|
|
|
import secrets
|
2022-11-15 09:19:08 +00:00
|
|
|
|
2022-11-15 01:28:43 +00:00
|
|
|
from os import environ, listdir, path
|
2022-11-15 09:19:08 +00:00
|
|
|
|
2022-12-05 14:25:25 +00:00
|
|
|
import user_agents
|
|
|
|
|
2022-11-29 23:50:32 +00:00
|
|
|
from flask import g, session, has_request_context
|
2022-11-15 09:19:08 +00:00
|
|
|
from jinja2 import pass_context
|
|
|
|
|
2022-11-17 21:02:08 +00:00
|
|
|
from files.classes.user import User
|
2022-09-24 06:40:26 +00:00
|
|
|
from files.helpers.assetcache import assetcache_path
|
2022-12-11 23:44:34 +00:00
|
|
|
from files.helpers.config.const import *
|
2023-01-21 04:27:30 +00:00
|
|
|
from files.helpers.regex import *
|
2023-01-23 09:58:38 +00:00
|
|
|
from files.helpers.settings import get_settings, get_setting
|
2022-11-15 09:19:08 +00:00
|
|
|
from files.helpers.sorting_and_time import make_age_string
|
2023-01-01 14:25:30 +00:00
|
|
|
from files.routes.routehelpers import get_alt_graph, get_formkey
|
2022-11-15 09:19:08 +00:00
|
|
|
from files.__main__ import app, cache
|
|
|
|
|
|
|
|
@app.template_filter("formkey")
|
|
|
|
def formkey(u):
|
|
|
|
return get_formkey(u)
|
2022-01-19 10:07:11 +00:00
|
|
|
|
|
|
|
@app.template_filter("post_embed")
|
|
|
|
def post_embed(id, v):
|
2022-11-15 09:19:08 +00:00
|
|
|
from flask import render_template
|
|
|
|
|
|
|
|
from files.helpers.get import get_post
|
2022-07-08 18:28:56 +00:00
|
|
|
p = get_post(id, v, graceful=True)
|
2022-01-28 19:27:16 +00:00
|
|
|
if p: return render_template("submission_listing.html", listing=[p], v=v)
|
|
|
|
return ''
|
2022-01-19 10:07:11 +00:00
|
|
|
|
2022-09-24 06:40:26 +00:00
|
|
|
@app.template_filter("asset")
|
2022-09-27 06:02:02 +00:00
|
|
|
@pass_context
|
|
|
|
def template_asset(ctx, asset_path):
|
2022-09-24 06:40:26 +00:00
|
|
|
return assetcache_path(asset_path)
|
|
|
|
|
|
|
|
|
2022-09-24 07:14:20 +00:00
|
|
|
@app.template_filter("asset_siteimg")
|
|
|
|
def template_asset_siteimg(asset_path):
|
|
|
|
# TODO: Add hashing for these using files.helpers.assetcache
|
|
|
|
return f'/i/{SITE_NAME}/{asset_path}?v=3010'
|
|
|
|
|
2022-04-06 22:54:09 +00:00
|
|
|
@app.template_filter("timestamp")
|
|
|
|
def timestamp(timestamp):
|
2022-10-15 18:11:43 +00:00
|
|
|
return make_age_string(timestamp)
|
2022-04-06 22:54:09 +00:00
|
|
|
|
2022-11-29 23:50:32 +00:00
|
|
|
@app.context_processor
|
|
|
|
def calc_users():
|
|
|
|
loggedin_counter = 0
|
|
|
|
loggedout_counter = 0
|
|
|
|
loggedin_chat = 0
|
|
|
|
v = getattr(g, 'v', None) if g else None
|
2022-11-30 01:12:30 +00:00
|
|
|
if has_request_context and g and g.desires_auth and not g.is_api_or_xhr:
|
2022-11-29 23:50:32 +00:00
|
|
|
loggedin = cache.get(LOGGED_IN_CACHE_KEY) or {}
|
|
|
|
loggedout = cache.get(LOGGED_OUT_CACHE_KEY) or {}
|
|
|
|
loggedin_chat = cache.get(CHAT_ONLINE_CACHE_KEY) or 0
|
|
|
|
timestamp = int(time.time())
|
|
|
|
|
2023-01-01 14:27:12 +00:00
|
|
|
if not session.get("session_id"):
|
|
|
|
session.permanent = True
|
|
|
|
session["session_id"] = secrets.token_hex(49)
|
|
|
|
|
2022-11-29 23:50:32 +00:00
|
|
|
if v:
|
|
|
|
if session["session_id"] in loggedout: del loggedout[session["session_id"]]
|
|
|
|
loggedin[v.id] = timestamp
|
|
|
|
else:
|
2022-12-05 14:25:25 +00:00
|
|
|
ua = str(user_agents.parse(g.agent))
|
|
|
|
if 'spider' not in ua.lower() and 'bot' not in ua.lower():
|
|
|
|
loggedout[session["session_id"]] = (timestamp, ua)
|
2023-01-01 11:36:20 +00:00
|
|
|
|
2022-11-29 23:50:32 +00:00
|
|
|
loggedin = {k: v for k, v in loggedin.items() if (timestamp - v) < LOGGEDIN_ACTIVE_TIME}
|
|
|
|
loggedout = {k: v for k, v in loggedout.items() if (timestamp - v[0]) < LOGGEDIN_ACTIVE_TIME}
|
|
|
|
cache.set(LOGGED_IN_CACHE_KEY, loggedin)
|
|
|
|
cache.set(LOGGED_OUT_CACHE_KEY, loggedout)
|
|
|
|
loggedin_counter = len(loggedin)
|
|
|
|
loggedout_counter = len(loggedout)
|
2023-01-01 11:36:20 +00:00
|
|
|
return {'loggedin_counter':loggedin_counter,
|
2022-11-29 23:50:32 +00:00
|
|
|
'loggedout_counter':loggedout_counter,
|
|
|
|
'loggedin_chat':loggedin_chat}
|
|
|
|
|
2022-12-30 13:54:49 +00:00
|
|
|
def current_registered_users():
|
2023-01-03 10:57:21 +00:00
|
|
|
return "{:,}".format(g.db.query(User).count())
|
2022-12-30 13:54:49 +00:00
|
|
|
|
2023-01-21 04:27:30 +00:00
|
|
|
def git_head():
|
|
|
|
# Note: doing zero sanitization. Git branch names are extremely permissive.
|
|
|
|
# However, they forbid '..', so I don't see an obvious dir traversal attack.
|
|
|
|
# Also, a malicious branch name would mean someone already owned the server
|
|
|
|
# or repo, so I think this isn't a weak link.
|
|
|
|
with open('.git/HEAD', encoding='utf_8') as head_f:
|
|
|
|
head_txt = head_f.read()
|
|
|
|
head_path = git_regex.match(head_txt).group(1)
|
2023-01-23 00:54:37 +00:00
|
|
|
try:
|
|
|
|
with open('.git/' + head_path, encoding='utf_8') as ref_f:
|
|
|
|
gitref = ref_f.read()[:7]
|
|
|
|
except:
|
|
|
|
gitref = 'Error'
|
2023-01-21 04:27:30 +00:00
|
|
|
return (gitref, head_txt)
|
|
|
|
|
2022-01-19 10:07:11 +00:00
|
|
|
@app.context_processor
|
|
|
|
def inject_constants():
|
2022-05-30 01:43:16 +00:00
|
|
|
return {"environ":environ, "SITE":SITE, "SITE_NAME":SITE_NAME, "SITE_FULL":SITE_FULL,
|
2022-12-02 22:21:18 +00:00
|
|
|
"AUTOJANNY_ID":AUTOJANNY_ID, "MODMAIL_ID":MODMAIL_ID, "VAPID_PUBLIC_KEY":VAPID_PUBLIC_KEY,
|
2023-01-22 08:04:49 +00:00
|
|
|
"listdir":listdir, "os_path":path,
|
2023-01-01 11:36:20 +00:00
|
|
|
"PIZZASHILL_ID":PIZZASHILL_ID, "DEFAULT_COLOR":DEFAULT_COLOR,
|
2022-09-01 19:12:05 +00:00
|
|
|
"COLORS":COLORS, "time":time, "PERMS":PERMS, "FEATURES":FEATURES,
|
|
|
|
"HOLE_NAME":HOLE_NAME, "HOLE_STYLE_FLAIR":HOLE_STYLE_FLAIR, "HOLE_REQUIRED":HOLE_REQUIRED,
|
2022-10-11 13:19:55 +00:00
|
|
|
"GUMROAD_LINK":GUMROAD_LINK, "DEFAULT_THEME":DEFAULT_THEME, "DESCRIPTION":DESCRIPTION,
|
2022-11-19 22:04:08 +00:00
|
|
|
"has_sidebar":has_sidebar, "has_logo":has_logo,
|
2023-01-23 09:58:38 +00:00
|
|
|
"FP":FP, "patron":patron, "get_setting": get_setting,
|
2022-10-05 22:53:36 +00:00
|
|
|
"SIDEBAR_THREAD":SIDEBAR_THREAD, "BANNER_THREAD":BANNER_THREAD,
|
|
|
|
"BADGE_THREAD":BADGE_THREAD, "SNAPPY_THREAD":SNAPPY_THREAD,
|
|
|
|
"KOFI_TOKEN":KOFI_TOKEN, "KOFI_LINK":KOFI_LINK,
|
|
|
|
"approved_embed_hosts":approved_embed_hosts,
|
2022-11-29 23:50:32 +00:00
|
|
|
"site_settings":get_settings(), "EMAIL":EMAIL, "max": max, "min": min, "user_can_see":User.can_see,
|
2022-12-25 03:28:57 +00:00
|
|
|
"TELEGRAM_ID":TELEGRAM_ID, "EMAIL_REGEX_PATTERN":EMAIL_REGEX_PATTERN,
|
2023-01-23 12:40:44 +00:00
|
|
|
"TRUESCORE_DONATE_MINIMUM":TRUESCORE_DONATE_MINIMUM, "PROGSTACK_ID":PROGSTACK_ID,
|
2023-01-01 11:36:20 +00:00
|
|
|
"DONATE_LINK":DONATE_LINK, "DONATE_SERVICE":DONATE_SERVICE, "BAN_EVASION_DOMAIN":BAN_EVASION_DOMAIN,
|
2023-01-01 03:38:37 +00:00
|
|
|
"HOUSE_JOIN_COST":HOUSE_JOIN_COST, "HOUSE_SWITCH_COST":HOUSE_SWITCH_COST, "IMAGE_FORMATS":','.join(IMAGE_FORMATS),
|
2023-01-01 11:36:20 +00:00
|
|
|
"PAGE_SIZES":PAGE_SIZES, "THEMES":THEMES, "COMMENT_SORTS":COMMENT_SORTS, "SORTS":SORTS,
|
|
|
|
"TIME_FILTERS":TIME_FILTERS, "HOUSES":HOUSES, "TIERS_ID_TO_NAME":TIERS_ID_TO_NAME,
|
2023-01-21 04:27:30 +00:00
|
|
|
"DEFAULT_CONFIG_VALUE":DEFAULT_CONFIG_VALUE, "IS_LOCALHOST":IS_LOCALHOST, "BACKGROUND_CATEGORIES":BACKGROUND_CATEGORIES, "PAGE_SIZE":PAGE_SIZE, "TAGLINES":TAGLINES, "IS_FISTMAS":IS_FISTMAS, "get_alt_graph":get_alt_graph, "current_registered_users":current_registered_users, "gitref":git_head()
|
2022-10-05 22:53:36 +00:00
|
|
|
}
|