Moved login and register to user POST | Added referrer analytics

This commit is contained in:
BlenderDumbass 2024-11-30 19:43:35 +02:00
parent 63ce632ce9
commit 6e9a24ae59
2 changed files with 41 additions and 12 deletions

View file

@ -16,6 +16,7 @@ from modules.Common import *
KnownCookies = [] KnownCookies = []
RecentArticles = {} RecentArticles = {}
RefferedArticles = {}
def guess_type(path): def guess_type(path):
@ -480,12 +481,15 @@ def ArticlePage(server, url):
user = validate(server.cookie) user = validate(server.cookie)
referrer = server.headers.get("referer", "")
if url.endswith(".md"): if url.endswith(".md"):
url = url.replace(".md", "") url = url.replace(".md", "")
# Recording when was the last time # Recording when was the last time
# the article loaded. # the article loaded.
RecentArticles["/"+url] = time.time() RecentArticles["/"+url] = time.time()
RefferedArticles["/"+url] = referrer
config = Set.Load() config = Set.Load()
tab, article, *rest = url.split("/") tab, article, *rest = url.split("/")
@ -542,7 +546,14 @@ def ArticlePage(server, url):
html = html + '<br><center><details><summary class="button">👁 '+views+'</summary>' html = html + '<br><center><details><summary class="button">👁 '+views+'</summary>'
html = html + """ html = html + """
<iframe width="100%" height="500px" style="border:none; border-radius:25px;" src="/graph"""+server.path+""""></iframe> <iframe width="100%" height="500px" style="border:none; border-radius:25px;" src="/graph"""+server.path+""""></iframe>
<br><br> <br><br>"""
referrers = Articles.get(article, {}).get("views", {}).get("referrers", {})
if referrers:
for referrer in referrers:
html = html + referrer + " : 👁 "+str(referrers[referrer]) + "<br>\n"
html = html + """
</details> </details>
""" """
@ -895,7 +906,7 @@ def LoginPage(server):
<div class="dark_box"> <div class="dark_box">
<form action="do_login"> <form action="do_login" method="post">
<img style="vertical-align: middle" src="/icon/user"> <img style="vertical-align: middle" src="/icon/user">
<input class="button" style="width:90%" maxlength="500" id="user_name" required="" name="user_name" pattern="[A-Za-z0-9\.\-\_\]*" placeholder="Username..."></input> <input class="button" style="width:90%" maxlength="500" id="user_name" required="" name="user_name" pattern="[A-Za-z0-9\.\-\_\]*" placeholder="Username..."></input>
@ -952,7 +963,7 @@ def RegisterPage(server):
<div class="dark_box"> <div class="dark_box">
<form action="do_register"> <form action="do_register" method="post">
""" """
if wrongcode: if wrongcode:
@ -1369,6 +1380,9 @@ def User(username, stretch=False):
def Graph(server, url): def Graph(server, url):
if url.endswith(".md"):
url = url.replace(".md", "")
# If there are any values after ? in the path # If there are any values after ? in the path
# which means, that somebody is sending the old # which means, that somebody is sending the old
# version of the graph link from the legacy code # version of the graph link from the legacy code
@ -1392,7 +1406,7 @@ def Graph(server, url):
# Sometimes scrapers try to load graph without # Sometimes scrapers try to load graph without
# loading the article first. We don't want to count # loading the article first. We don't want to count
# it as a view. # it as a view.
if time.time()-10 > RecentArticles.get(url, 0): if time.time()-20 > RecentArticles.get(url, 0):
print(consoleForm(server.cookie), "Article wasn't loaded, scrapers!") print(consoleForm(server.cookie), "Article wasn't loaded, scrapers!")
AccessDenied(server) AccessDenied(server)
return return
@ -1400,6 +1414,10 @@ def Graph(server, url):
user = validate(server.cookie) user = validate(server.cookie)
# Store general analytics about which search engines were used.
# To get to this article.
referrer = RefferedArticles.get(url, "")
html = """ html = """
<head> <head>
<link media="all" href="/css" type="text/css" rel="stylesheet" /> <!-- CSS theme link --> <link media="all" href="/css" type="text/css" rel="stylesheet" /> <!-- CSS theme link -->
@ -1414,8 +1432,7 @@ def Graph(server, url):
""" """
if url.endswith(".md"):
url = url.replace(".md", "")
try: try:
with open(Set.Folder()+"/tabs"+url+"/metadata.json") as o: with open(Set.Folder()+"/tabs"+url+"/metadata.json") as o:
@ -1464,6 +1481,14 @@ def Graph(server, url):
article["views"]["amount"] += 1 article["views"]["amount"] += 1
article["views"]["viewers"].append(cookie) article["views"]["viewers"].append(cookie)
if "referrers" not in article["views"]:
article["views"]["referrers"] = {}
if referrer and referrer not in article["views"]["referrers"]:
article["views"]["referrers"][referrer] = 0
if referrer:
article["views"]["referrers"][referrer] += 1
dates = article["views"]["dates"] dates = article["views"]["dates"]
date = datetime.now().strftime(dateformat) date = datetime.now().strftime(dateformat)
dates[date] = dates.get(date, 0) + 1 dates[date] = dates.get(date, 0) + 1

View file

@ -69,7 +69,7 @@ class handler(BaseHTTPRequestHandler):
# Failing early to make sure that nobody will # Failing early to make sure that nobody will
# try attacking this part of the server. # try attacking this part of the server.
commands = ["do_edit"] commands = ["do_edit", "do_login", "do_register"]
found = False found = False
for i in commands: for i in commands:
if i in self.path: if i in self.path:
@ -103,6 +103,12 @@ class handler(BaseHTTPRequestHandler):
if self.path[1:].startswith("do_edit"): if self.path[1:].startswith("do_edit"):
Render.Publish(self) Render.Publish(self)
elif self.path[1:].startswith("do_login"):
Render.Login(self)
elif self.path[1:].startswith("do_register"):
Render.Register(self)
else: else:
Render.NotFound(self) Render.NotFound(self)
@ -185,11 +191,9 @@ class handler(BaseHTTPRequestHandler):
elif self.path[1:].startswith("log_out"): elif self.path[1:].startswith("log_out"):
Render.LogOut(self) Render.LogOut(self)
elif self.path[1:].startswith("do_login"):
Render.Login(self)
elif self.path[1:].startswith("do_register"):
Render.Register(self)
elif self.path[1:].startswith("read_notification"): elif self.path[1:].startswith("read_notification"):
Render.ReadNotification(self) Render.ReadNotification(self)