monotreme.org/scripts/user_panel.cgi

68 lines
2.7 KiB
Text
Raw Normal View History

2024-09-23 06:27:02 +01:00
#!/usr/bin/python3
2024-09-22 22:37:04 +01:00
2024-09-23 06:27:02 +01:00
import sqlite3
import os
2024-09-23 07:00:19 +01:00
import http.cookies
import time
2024-09-22 22:37:04 +01:00
2024-09-23 06:27:02 +01:00
print("Content-Type: text/html")
print()
# 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
# Connect to SQLite and check the session
db = sqlite3.connect('/var/lib/monotreme/data/monotreme.db')
cursor = db.cursor()
2024-09-23 06:21:45 +01:00
2024-09-23 06:27:02 +01:00
# Check if the session exists and is still valid
2024-09-23 07:00:19 +01:00
cursor.execute("SELECT username FROM sessions WHERE session_id=? AND expires_at > ?", (session_id, int(time.time())))
2024-09-23 06:27:02 +01:00
result = cursor.fetchone()
if result:
2024-09-23 07:00:19 +01:00
username = result[0]
2024-09-23 06:27:02 +01:00
print(f"<h1>Welcome, {username}!</h1>")
print("<p>This is your user panel.</p>")
2024-09-23 07:00:19 +01:00
print("<p>This page contains a couple things once I figure out how databases work.</p>")
2024-10-29 18:58:14 +00:00
print("<p>Click <a href='/cgi-bin/logout.cgi'>here</a> to logout.</p>")
2024-09-23 07:00:19 +01:00
# Fetch and display profile info
cursor.execute("SELECT email, date_joined, last_login FROM users WHERE username=?", (username,))
profile_info = cursor.fetchone()
if profile_info:
email, date_joined, last_login = profile_info
print(f"<p>Email: {email}</p>")
print(f"<p>Date Joined: {date_joined}</p>")
print(f"<p>Last Login: {last_login}</p>")
# Fetch and display recent activity
cursor.execute("SELECT action, timestamp FROM activity_log WHERE username=? ORDER BY timestamp DESC LIMIT 5", (username,))
recent_activity = cursor.fetchall()
print("<h3>Recent Activity</h3>")
if recent_activity:
for action, timestamp in recent_activity:
time_str = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(timestamp))
print(f"<p>{action} at {time_str}</p>")
else:
print("<p>No recent activity</p>")
# Fetch and display notifications
cursor.execute("SELECT message, timestamp FROM notifications WHERE username=? AND read=0 ORDER BY timestamp DESC", (username,))
notifications = cursor.fetchall()
print("<h3>Notifications</h3>")
if notifications:
for message, timestamp in notifications:
time_str = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(timestamp))
print(f"<p>{message} at {time_str}</p>")
else:
print("<p>No new notifications</p>")
2024-09-22 22:37:04 +01:00
else:
2024-09-23 06:27:02 +01:00
print("<h1>Session expired or invalid!</h1>")
2024-09-22 22:37:04 +01:00
print("<a href='/login/'>Login again</a>")
2024-09-23 06:27:02 +01:00
else:
print("<h1>No session found!</h1>")
2024-09-23 07:00:19 +01:00
print("<a href='/login/'>Login again</a>")