2022-04-06 22:54:09 +00:00
|
|
|
import time
|
2023-09-28 23:58:09 +00:00
|
|
|
import math
|
2023-10-15 14:28:40 +00:00
|
|
|
import datetime
|
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
|
|
|
|
2023-05-04 22:58:49 +00:00
|
|
|
from flask import g, session, has_request_context, request
|
2022-11-15 09:19:08 +00:00
|
|
|
from jinja2 import pass_context
|
2023-08-12 13:22:47 +00:00
|
|
|
from PIL import ImageColor
|
2023-09-28 23:58:09 +00:00
|
|
|
from sqlalchemy import text
|
2022-11-15 09:19:08 +00:00
|
|
|
|
2022-11-17 21:02:08 +00:00
|
|
|
from files.classes.user import User
|
2023-10-13 15:56:12 +00:00
|
|
|
from files.classes.orgy import get_running_orgy
|
2023-10-27 15:58:37 +00:00
|
|
|
from files.classes.emoji import Emoji
|
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-10-04 13:36:26 +00:00
|
|
|
from files.helpers.const_stateful import OVER_18_EMOJIS
|
2023-01-21 04:27:30 +00:00
|
|
|
from files.helpers.regex import *
|
2023-03-11 06:13:58 +00:00
|
|
|
from files.helpers.settings import *
|
2023-03-13 05:28:22 +00:00
|
|
|
from files.helpers.cloudflare import *
|
2022-11-15 09:19:08 +00:00
|
|
|
from files.helpers.sorting_and_time import make_age_string
|
2023-10-05 10:09:58 +00:00
|
|
|
from files.helpers.can_see import *
|
2023-01-25 02:51:48 +00:00
|
|
|
from files.routes.routehelpers import get_alt_graph, get_formkey
|
2023-07-07 20:45:39 +00:00
|
|
|
from files.routes.wrappers import calc_users
|
2022-11-15 09:19:08 +00:00
|
|
|
from files.__main__ import app, cache
|
|
|
|
|
2023-05-04 22:58:49 +00:00
|
|
|
from urllib.parse import parse_qs, urlencode, urlsplit
|
|
|
|
|
2023-08-12 13:22:47 +00:00
|
|
|
@app.template_filter("rgb")
|
|
|
|
def rgb(color):
|
|
|
|
return str(ImageColor.getcolor(f"#{color}", "RGB"))[1:-1]
|
|
|
|
|
2022-11-15 09:19:08 +00:00
|
|
|
@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)
|
2023-06-07 23:26:32 +00:00
|
|
|
if p: return render_template("post_listing.html", listing=[p], v=v)
|
2022-01-28 19:27:16 +00:00
|
|
|
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)
|
|
|
|
|
2023-09-05 14:32:36 +00:00
|
|
|
@app.template_filter("change_arg")
|
|
|
|
def template_change_arg(arg, value, url):
|
2023-05-05 06:18:29 +00:00
|
|
|
parsed = urlsplit(url)
|
2023-05-04 22:58:49 +00:00
|
|
|
query_dict = parse_qs(parsed.query)
|
2023-09-11 18:03:05 +00:00
|
|
|
query_dict.pop('page', None)
|
2023-09-05 14:32:36 +00:00
|
|
|
query_dict[arg] = value
|
2023-05-04 22:58:49 +00:00
|
|
|
query_new = urlencode(query_dict, doseq=True)
|
|
|
|
parsed = parsed._replace(query=query_new)
|
|
|
|
return parsed.geturl()
|
|
|
|
|
2022-09-24 07:14:20 +00:00
|
|
|
@app.template_filter("asset_siteimg")
|
|
|
|
def template_asset_siteimg(asset_path):
|
2023-06-29 20:14:30 +00:00
|
|
|
return f'{SITE_FULL_IMAGES}/i/{SITE_NAME}/{asset_path}?x=6'
|
2022-09-24 07:14:20 +00:00
|
|
|
|
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
|
|
|
|
2023-05-19 13:51:49 +00:00
|
|
|
@app.template_filter("selected_tab")
|
|
|
|
def selected_tab(request):
|
|
|
|
if request.path == '/':
|
2023-05-20 00:12:06 +00:00
|
|
|
requested_sort = request.values.get('sort')
|
2023-05-20 00:11:45 +00:00
|
|
|
if hasattr(g, 'v') and g.v and g.v.defaultsorting == 'new' and requested_sort == 'hot':
|
2023-05-19 13:51:49 +00:00
|
|
|
return 'hot'
|
|
|
|
elif requested_sort == 'new':
|
2023-09-07 15:26:31 +00:00
|
|
|
return 'new'
|
2023-05-19 13:51:49 +00:00
|
|
|
elif request.path == '/comments':
|
|
|
|
return 'comments'
|
2023-08-05 10:44:05 +00:00
|
|
|
elif request.path == '/casino':
|
|
|
|
return 'casino'
|
2023-05-19 13:51:49 +00:00
|
|
|
elif request.path == '/chat':
|
|
|
|
return 'chat'
|
2023-08-05 10:44:26 +00:00
|
|
|
elif request.path.startswith('/shop/'):
|
2023-05-19 13:51:49 +00:00
|
|
|
return 'shop'
|
|
|
|
|
|
|
|
return 'home'
|
|
|
|
|
2022-11-29 23:50:32 +00:00
|
|
|
|
2022-12-30 13:54:49 +00:00
|
|
|
def current_registered_users():
|
2023-03-16 06:27:58 +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.
|
2023-09-14 23:19:44 +00:00
|
|
|
with open('.git/HEAD') as head_f:
|
2023-01-21 04:27:30 +00:00
|
|
|
head_txt = head_f.read()
|
2023-01-23 00:54:37 +00:00
|
|
|
try:
|
2023-02-01 17:17:12 +00:00
|
|
|
head_path = git_regex.match(head_txt).group(1)
|
2023-09-14 23:19:44 +00:00
|
|
|
with open('.git/' + head_path) as ref_f:
|
2023-01-23 00:54:37 +00:00
|
|
|
gitref = ref_f.read()[:7]
|
|
|
|
except:
|
|
|
|
gitref = 'Error'
|
2023-01-21 04:27:30 +00:00
|
|
|
return (gitref, head_txt)
|
|
|
|
|
2023-03-12 14:48:19 +00:00
|
|
|
def max_days():
|
|
|
|
return int((2147483647-time.time())/86400)
|
|
|
|
|
2023-09-28 23:58:09 +00:00
|
|
|
@cache.memoize(timeout=60)
|
|
|
|
def bar_position():
|
2023-10-14 20:12:30 +00:00
|
|
|
t = int(time.time()) - 86400
|
2023-10-12 14:00:02 +00:00
|
|
|
|
2023-09-28 23:58:09 +00:00
|
|
|
db = db_session()
|
|
|
|
vaxxed = db.execute(text("SELECT COUNT(*) FROM users WHERE zombie > 0")).one()[0]
|
|
|
|
zombie = db.execute(text("SELECT COUNT(*) FROM users WHERE zombie < 0")).one()[0]
|
|
|
|
total = db.execute(text("SELECT COUNT(*) FROM "
|
|
|
|
"(SELECT DISTINCT ON (author_id) author_id AS uid FROM comments "
|
2023-10-12 14:00:02 +00:00
|
|
|
f"WHERE created_utc > {t}) AS q1 "
|
2023-09-28 23:58:09 +00:00
|
|
|
"FULL OUTER JOIN (SELECT id AS uid FROM users WHERE zombie != 0) as q2 "
|
|
|
|
"ON q1.uid = q2.uid")).one()[0]
|
|
|
|
total = max(total, 1)
|
|
|
|
|
|
|
|
return [int((vaxxed * 100) / total), int((zombie * 100) / total), vaxxed, zombie]
|
|
|
|
|
2023-10-27 15:58:37 +00:00
|
|
|
@cache.cached(make_cache_key=lambda:"emoji_count")
|
|
|
|
def emoji_count():
|
|
|
|
return g.db.query(Emoji).filter_by(submitter_id=None).count()
|
|
|
|
|
2022-01-19 10:07:11 +00:00
|
|
|
@app.context_processor
|
|
|
|
def inject_constants():
|
2023-11-06 13:18:17 +00:00
|
|
|
return {"environ":environ, "SITE":SITE, "SITE_NAME":SITE_NAME, "SITE_FULL":SITE_FULL, "OTHER_SITE_NAME":OTHER_SITE_NAME,
|
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,
|
2023-10-07 18:04:12 +00:00
|
|
|
"HOLE_REQUIRED":HOLE_REQUIRED,
|
2023-02-01 19:49:39 +00:00
|
|
|
"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,
|
2023-08-22 22:47:28 +00:00
|
|
|
"SIDEBAR_THREAD":SIDEBAR_THREAD, "BANNER_THREAD":BANNER_THREAD, "BUG_THREAD":BUG_THREAD,
|
2023-08-11 16:35:14 +00:00
|
|
|
"BADGE_THREAD":BADGE_THREAD, "SNAPPY_THREAD":SNAPPY_THREAD, "CHANGELOG_THREAD":CHANGELOG_THREAD,
|
2023-02-28 19:36:14 +00:00
|
|
|
"approved_embed_hosts":approved_embed_hosts, "POST_BODY_LENGTH_LIMIT":POST_BODY_LENGTH_LIMIT,
|
2023-10-05 10:09:58 +00:00
|
|
|
"SITE_SETTINGS":get_settings(), "EMAIL":EMAIL, "max": max, "min": min, "can_see":can_see,
|
2023-11-03 19:27:26 +00:00
|
|
|
"TELEGRAM_ID":TELEGRAM_ID, "TWITTER_ID":TWITTER_ID, "TRUESCORE_MINIMUM":TRUESCORE_MINIMUM, "PROGSTACK_ID":PROGSTACK_ID,
|
2023-03-21 15:39:26 +00:00
|
|
|
"DONATE_LINK":DONATE_LINK, "DONATE_SERVICE":DONATE_SERVICE,
|
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-09-05 14:32:36 +00:00
|
|
|
"PAGE_SIZES":PAGE_SIZES, "THEMES":THEMES, "COMMENT_SORTS":COMMENT_SORTS, "POST_SORTS":POST_SORTS,
|
2023-05-14 22:16:01 +00:00
|
|
|
"TIME_FILTERS":TIME_FILTERS, "HOUSES":HOUSES, "TIER_TO_NAME":TIER_TO_NAME,
|
2023-04-25 14:34:09 +00:00
|
|
|
"DEFAULT_CONFIG_VALUE":DEFAULT_CONFIG_VALUE, "IS_LOCALHOST":IS_LOCALHOST, "BACKGROUND_CATEGORIES":BACKGROUND_CATEGORIES, "PAGE_SIZE":PAGE_SIZE, "TAGLINES":TAGLINES, "get_alt_graph":get_alt_graph, "current_registered_users":current_registered_users,
|
|
|
|
"git_head":git_head, "max_days":max_days, "EMOJI_KINDS":EMOJI_KINDS,
|
2023-03-03 04:19:52 +00:00
|
|
|
"BIO_FRIENDS_ENEMIES_LENGTH_LIMIT":BIO_FRIENDS_ENEMIES_LENGTH_LIMIT,
|
2023-10-02 14:24:09 +00:00
|
|
|
"SITE_FULL_IMAGES": SITE_FULL_IMAGES,
|
2023-09-29 00:33:16 +00:00
|
|
|
"IS_EVENT":IS_EVENT, "IS_FISTMAS":IS_FISTMAS, "IS_HOMOWEEN":IS_HOMOWEEN,
|
|
|
|
"IS_DKD":IS_DKD, "IS_BIRTHGAY":IS_BIRTHGAY, "IS_BIRTHDEAD":IS_BIRTHDEAD,
|
2023-10-04 13:36:26 +00:00
|
|
|
"CHUD_PHRASES":CHUD_PHRASES, "hasattr":hasattr, "calc_users":calc_users, "HOLE_INACTIVITY_DELETION":HOLE_INACTIVITY_DELETION, "LIGHT_THEMES":LIGHT_THEMES, "OVER_18_EMOJIS":OVER_18_EMOJIS,
|
2023-08-12 18:28:21 +00:00
|
|
|
"MAX_IMAGE_AUDIO_SIZE_MB":MAX_IMAGE_AUDIO_SIZE_MB, "MAX_IMAGE_AUDIO_SIZE_MB_PATRON":MAX_IMAGE_AUDIO_SIZE_MB_PATRON,
|
|
|
|
"MAX_VIDEO_SIZE_MB":MAX_VIDEO_SIZE_MB, "MAX_VIDEO_SIZE_MB_PATRON":MAX_VIDEO_SIZE_MB_PATRON,
|
2023-10-27 15:58:37 +00:00
|
|
|
"CURSORMARSEY_DEFAULT":CURSORMARSEY_DEFAULT, "SNAPPY_ID":SNAPPY_ID, "get_running_orgy":get_running_orgy, "TRUESCORE_MINIMUM":TRUESCORE_MINIMUM, "bar_position":bar_position, "datetime":datetime, "CSS_LENGTH_LIMIT":CSS_LENGTH_LIMIT, "cache":cache, "emoji_count":emoji_count,
|
2023-08-12 18:28:21 +00:00
|
|
|
}
|