Language updates. New upload form. new classes.

This commit is contained in:
Cody Cook 2025-02-22 00:20:39 -08:00
commit 8f3061ab99
62 changed files with 3107 additions and 1883 deletions

View file

@ -1,124 +1,110 @@
<?php
session_start();
require_once 'includes/globals.php';
require_once 'vendor/autoload.php';
use DJMixHosting\Database;
use DJMixHosting\User;
use DJMixHosting\SessionManager;
use Aws\Ses\SesClient;
use Aws\Exception\AwsException;
// If the user is already logged in, redirect them
if(isset($_SESSION['user'])) {
if (SessionManager::getUser()) {
header("Location: profile.php");
exit;
}
if($_SERVER['REQUEST_METHOD'] == 'POST') {
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// Gather form fields
$username = trim($_POST['username'] ?? '');
$email = trim($_POST['email'] ?? '');
$password = $_POST['password'] ?? '';
$username = trim($_POST['username'] ?? '');
$email = trim($_POST['email'] ?? '');
$password = $_POST['password'] ?? '';
$confirm_password = $_POST['confirm_password'] ?? '';
$first_name = trim($_POST['first_name'] ?? '');
$last_name = trim($_POST['last_name'] ?? '');
$first_name = trim($_POST['first_name'] ?? '');
$last_name = trim($_POST['last_name'] ?? '');
// Basic validation
$errors = [];
if(empty($username) || empty($email) || empty($password) || empty($confirm_password) || empty($first_name) || empty($last_name)) {
if (empty($username) || empty($email) || empty($password) || empty($confirm_password) || empty($first_name) || empty($last_name)) {
$errors[] = "All fields are required.";
}
if($password !== $confirm_password) {
if ($password !== $confirm_password) {
$errors[] = "Passwords do not match.";
}
if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errors[] = "Invalid email format.";
}
if(!preg_match('/^[a-zA-Z0-9_]{3,25}$/', $username)) {
if (!preg_match('/^[a-zA-Z0-9_]{3,25}$/', $username)) {
$errors[] = "Invalid username format.";
}
if(!empty($errors)) {
if (!empty($errors)) {
$_SESSION['error'] = implode(" ", $errors);
header("Location: register.php");
exit;
}
$db = new Database($config);
$user = new User($db);
// Check if username or email already exists
$stmt = $db->prepare("SELECT id FROM users WHERE username = ? OR email = ?");
$stmt->bind_param("ss", $username, $email);
$stmt->execute();
$result = $stmt->get_result();
if($result->num_rows > 0) {
$_SESSION['error'] = "Username or email already exists.";
try {
// Delegate registration to the User class
$user_id = $user->newUser($username, $password, $email, $first_name, $last_name);
} catch (\Exception $e) {
$_SESSION['error'] = $e->getMessage();
header("Location: register.php");
exit;
}
$stmt->close();
// Insert the new user record. (Assuming columns firstName and lastName exist.)
$hashed_password = password_hash($password, PASSWORD_DEFAULT);
$stmt = $db->prepare("INSERT INTO users (username, password, email, firstName, lastName, img, emailVerified) VALUES (?, ?, ?, ?, ?, '', 0)");
$stmt->bind_param("sssss", $username, $hashed_password, $email, $first_name, $last_name);
if(!$stmt->execute()){
$_SESSION['error'] = "Registration failed. Please try again.";
header("Location: register.php");
exit;
}
$user_id = $stmt->insert_id;
$stmt->close();
// Log the user in
$_SESSION['user'] = [
'id' => $user_id,
// Log the user in using SessionManager
SessionManager::setUser([
'id' => $user_id,
'username' => $username,
'email' => $email
];
'email' => $email
]);
// Trigger email verification: generate a verification code valid for 15 minutes
$verification_code = bin2hex(random_bytes(16));
$expires_at = date("Y-m-d H:i:s", strtotime("+15 minutes"));
// Insert record with purpose 'email_verification'
// Insert verification record (with purpose 'email_verification')
$stmt = $db->prepare("REPLACE INTO email_verifications (user_id, email, verification_code, expires_at, purpose) VALUES (?, ?, ?, ?, 'email_verification')");
$stmt->bind_param("isss", $user_id, $email, $verification_code, $expires_at);
$stmt->execute();
$stmt->close();
// Send verification email via AWS SES using config settings
// Send verification email via AWS SES
$sesClient = new SesClient([
'version' => 'latest',
'region' => $config['aws']['ses']['region'],
'version' => 'latest',
'region' => $config['aws']['ses']['region'],
'credentials' => [
'key' => $config['aws']['ses']['access_key'],
'secret' => $config['aws']['ses']['secret_key']
]
]);
$sender_email = $config['aws']['ses']['sender_email'];
$sender_email = $config['aws']['ses']['sender_email'];
$recipient_email = $email;
$subject = "Verify Your Email Address";
$subject = "Verify Your Email Address";
$verification_link = $config['app']['url'] . "/verify_email.php?code={$verification_code}";
$body_text = "Thank you for registering at " . $config['app']['name'] . ".\n\n";
$body_text .= "Please verify your email address by clicking the link below or by entering the verification code in your profile:\n\n";
$body_text .= "{$verification_link}\n\nYour verification code is: {$verification_code}\nThis code will expire in 15 minutes.";
$body_text = "Thank you for registering at " . $config['app']['name'] . ".\n\n";
$body_text .= "Please verify your email address by clicking the link below or by entering the verification code in your profile:\n\n";
$body_text .= "{$verification_link}\n\nYour verification code is: {$verification_code}\nThis code will expire in 15 minutes.";
try {
$result = $sesClient->sendEmail([
'Destination' => [
'ToAddresses' => [$recipient_email],
],
'ReplyToAddresses' => [$sender_email],
'Source' => $sender_email,
'Message' => [
'Body' => [
$sesClient->sendEmail([
'Destination' => ['ToAddresses' => [$recipient_email]],
'ReplyToAddresses' => [$sender_email],
'Source' => $sender_email,
'Message' => [
'Body' => [
'Text' => [
'Charset' => 'UTF-8',
'Data' => $body_text,
'Data' => $body_text,
],
],
'Subject' => [
'Charset' => 'UTF-8',
'Data' => $subject,
'Data' => $subject,
],
],
]);
@ -139,8 +125,10 @@ require_once 'includes/header.php';
<div class="row justify-content-center">
<div class="col-lg-6">
<?php
if(isset($_SESSION['error'])) {
echo '<div class="alert alert-danger alert-dismissible fade show mb-4" role="alert">' . htmlspecialchars($_SESSION['error']) . '<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button></div>';
if (isset($_SESSION['error'])) {
echo '<div class="alert alert-danger alert-dismissible fade show mb-4" role="alert">'
. htmlspecialchars($_SESSION['error'])
. '<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button></div>';
unset($_SESSION['error']);
}
?>