monotreme.org/scripts/user_panel.cgi
2024-09-23 01:21:45 -04:00

39 lines
1.4 KiB
Python

import sqlite3
import http.cookies
import os
# Ensure the session_id is properly parsed
cookie = http.cookies.SimpleCookie(os.environ.get('HTTP_COOKIE', ''))
session_id = cookie.get('session_id')
if session_id:
session_id = session_id.value
else:
session_id = None
# Log the session ID for debugging
with open("/tmp/user_panel_session.log", "a") as f:
f.write(f"Parsed session ID: {session_id}\n")
if session_id:
session_id = session_id.value
# Connect to SQLite and check the session
db = sqlite3.connect('/var/lib/monotreme/data/monotreme.db')
cursor = db.cursor()
# Check if the session exists and is still valid
cursor.execute("SELECT username FROM sessions WHERE session_id=? AND expires_at > ?", (session_id, int(time.time())))
result = cursor.fetchone()
if result:
username = result[0]
print(f"<h1>Welcome, {username}!</h1>")
print("<p>This is your user panel.</p>")
print("<p>Here, for now, you can only log out.</p>")
print("<a href='/cgi-bin/logout.cgi'>Log out</a>")
else:
print("<h1>Session expired or invalid!</h1>")
print("<a href='/login/'>Login again</a>")
else:
print("<h1>No session found!</h1>")
print("<a href='/login/'>Login again</a>")