monotreme.org/scripts/register.cgi
2024-09-21 21:51:31 -04:00

93 lines
2.8 KiB
Perl

#!/bin/bash
echo "Content-type: text/html"
echo ""
# Log the raw POST data for debugging
read POST_DATA
echo "POST Data: $POST_DATA" >> /tmp/register_form.log
# URL decoding function
urldecode() {
local url_encoded="${1//+/ }"
printf '%b' "${url_encoded//%/\\x}"
}
# Parse the form data using IFS
USERNAME=""
EMAIL=""
PASSWORD=""
CONFIRM_PASSWORD=""
IFS='&' # Split fields by "&"
for param in $POST_DATA; do
IFS='=' read -r key value <<< "$param"
key=$(urldecode "$key")
value=$(urldecode "$value")
case $key in
username) USERNAME="$value" ;;
email) EMAIL="$value" ;;
password) PASSWORD="$value" ;;
confirm_password) CONFIRM_PASSWORD="$value" ;;
esac
done
# Check if passwords match
if [ "$PASSWORD" != "$CONFIRM_PASSWORD" ]; then
cat <<EOF
<html>
<head><title>Registration Failed</title></head>
<body>
<h1>Passwords do not match!</h1>
<a href="/login/register/">Go back</a>
</body>
</html>
EOF
exit 1
fi
# Hash the password using SHA-256
PASSWORD_HASH=$(echo -n "$PASSWORD" | sha256sum | awk '{print $1}')
# Insert the user into the SQLite database
DB_PATH="/var/lib/monotreme/data/monotreme.db"
sqlite3 $DB_PATH "INSERT INTO users (username, email, password_hash) VALUES ('$USERNAME', '$EMAIL', '$PASSWORD_HASH');" 2>> /tmp/register_form.log
# Log the username and email for debugging
echo "Username: $USERNAME, Email: $EMAIL" >> /tmp/register_form.log
# Create the email with proper headers
EMAIL_BODY=$(cat <<EOF
From: info@monotreme.org
To: $EMAIL
Subject: Welcome to monotreme.org
Hello $USERNAME,
Thank you for registering at monotreme.org. You can now log in with your credentials. I hope you enjoy the world of the monotreme!
Best regards,
Tristan
monotreme.org team
EOF
)
# Log the email body for debugging
echo "Email Body: $EMAIL_BODY" >> /tmp/register_form.log
# Send the email using msmtp (or your protonmail-bridge setup)
echo "$EMAIL_BODY" | msmtp --from=default "$EMAIL"
# Response back to the browser
cat <<EOF
<html>
<head><title>Registration Successful</title></head>
<body>
<h1>Registration successful!</h1>
<p>A confirmation email has been sent to $EMAIL.</p>
<a href="/login/">Go to login page</a>
</body>
</html>
EOF