added scripts

This commit is contained in:
Tristan Smith 2024-09-22 17:37:04 -04:00
parent 3ff1d42924
commit d2e62585c0
3 changed files with 58 additions and 1 deletions

View file

@ -43,7 +43,7 @@
<h2>Login to monotreme.org</h2>
<!-- Login Form -->
<div class="form-container">
<form action="/login/" method="post">
<form action="/ " method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="username" required>
<br>

25
scripts/logout.cgi Normal file
View file

@ -0,0 +1,25 @@
# logout.cgi
import sqlite3
import os
import http.cookies
print("Content-Type: text/html")
print("Set-Cookie: session_id=; Path=/; Expires=Thu, 01 Jan 1970 00:00:00 GMT")
print()
# Get the session ID from the cookie
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 remove the session
db = sqlite3.connect('/var/lib/monotreme/data/monotreme.db')
cursor = db.cursor()
cursor.execute("DELETE FROM sessions WHERE session_id=?", (session_id,))
db.commit()
db.close()
print("<h1>You have been logged out!</h1>")
print("<a href='/login/'>Login again</a>")

32
scripts/user_panel.cgi Normal file
View file

@ -0,0 +1,32 @@
import sqlite3
import os
import http.cookies
import time
print("Content-Type: text/html")
print()
# Get the session ID from the cookie
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()
# 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>")
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>")