diff --git a/config/config.php b/config/config.php new file mode 100644 index 0000000..dc9b225 --- /dev/null +++ b/config/config.php @@ -0,0 +1,18 @@ + \ No newline at end of file diff --git a/includes/form_handlers/login_handler.php b/includes/form_handlers/login_handler.php new file mode 100644 index 0000000..7b4e846 --- /dev/null +++ b/includes/form_handlers/login_handler.php @@ -0,0 +1,40 @@ +prepare("SELECT username,user_closed FROM users WHERE email = ? AND password = ?"); + $stmt->bind_param("ss", $email, $pass); // "ss" indicates two string parameters + $stmt->execute(); + $stmt->bind_result($username,$closed); + $stmt->fetch(); + + if ($username != null) { + $_SESSION['username'] = $username; + + if ($closed === 0) { // Reopens account if the account was closed. + $stmt = $con->prepare("UPDATE users SET user_closed = '0' WHERE email = ?"); + $stmt->bind_param("s", $email); // Bind parameters for security + $stmt->execute(); + $stmt->close(); // Close the statement for proper resource management + } + + header("Location: index.php"); + exit(); + } else { + array_push($errors, "login_error"); + } + + $stmt->close(); +} + +?> \ No newline at end of file diff --git a/includes/form_handlers/register_handler.php b/includes/form_handlers/register_handler.php new file mode 100644 index 0000000..f117d5e --- /dev/null +++ b/includes/form_handlers/register_handler.php @@ -0,0 +1,188 @@ +prepare("SELECT COUNT(*) FROM users WHERE email = ?"); + $username_check->bind_param("s", $email); + $username_check->bind_result($num_rows); + $username_check->execute(); + $username_check->fetch(); + if ($num_rows > 0) { + array_push($errors, "email_exists"); + } + } else { + array_push($errors, "email_invalid"); + } + } else { + array_push($errors, "email_mismatch"); + } + + if (strlen($fname) > 25 || strlen($fname) < 2) { + array_push($errors, "fname_invalid"); + } + + if (strlen($lname) > 25 || strlen($lname) < 2) { + array_push($errors, "lname_invalid"); + } + + if ($pass != $pass_conf) { + array_push($errors, "pass_mismatch"); + } else { + if(preg_match('/[^A-Za-z0-9]/', $pass)) { + array_push($errors, "pass_invalid_char"); + } else { + if (strlen($pass) > 30 || strlen($pass) < 5) { + array_push($errors, "pass_invalid_length"); + } + } + } + + if (!file_exists('debug/logs')) { + mkdir('debug/logs', 0755, true); // Create recursively with appropriate permissions + } + + if (empty($errors)) { + $pass = password_hash($pass, PASSWORD_BCRYPT); // Encrypts password + $username = strtolower($fname . "." . $lname); + + $username_check = $con->prepare("SELECT COUNT(*) FROM users WHERE username = ?"); + $temp = $username; + $username_check->bind_param("s", $temp); + $username_check->bind_result($count); + $username_check->execute(); + $username_check->fetch(); + $i = 0; + while ($count == 1) { + $i++; + $temp = $username; + if ($i > 0) { + $temp .= $i; // Use string concatenation directly with .= + } + $username_check->bind_param("s", $temp); + $username_check->bind_result($count); + $username_check->execute(); + $username_check->fetch(); + error_log("\nCounter=$i\nTemp = $temp\nNum Of Names: $count", 3, 'debug/logs/register_error.log'); + } + + if ($i > 0) { + $username = $username . $i; + } + + $username_check->close(); + + $rand = rand(1,16); + $profile_pic = random_profile_pic($rand); + + $create_user = $con->prepare("INSERT INTO users VALUES (NULL,?,?,?,?,?,?,?,'0','0','0',',')"); + $create_user->bind_param("sssssss", $fname,$lname,$username,$email,$pass,$date,$profile_pic); + $create_user->execute(); + $create_user->close(); + + array_push($errors, "You're all set! Go ahead and login!
"); + + $_SESSION['reg_fname'] = ""; + $_SESSION['reg_lname'] = ""; + $_SESSION['reg_email'] = ""; + $_SESSION['reg_email_conf'] = ""; + + } +} + +function random_profile_pic($rand) { + $profile_pic = "assets/profile_pics/defaults/"; + switch ($rand) { + case 1: + $profile_pic = $profile_pic . "head_alizarin.png"; + break; + case 2: + $profile_pic = $profile_pic . "head_amethyst.png"; + break; + case 3: + $profile_pic = $profile_pic . "head_belize_hole.png"; + break; + case 4: + $profile_pic = $profile_pic . "head_carrot.png"; + break; + case 5: + $profile_pic = $profile_pic . "head_deep_blue.png"; + break; + case 6: + $profile_pic = $profile_pic . "head_emerald.png"; + break; + case 7: + $profile_pic = $profile_pic . "head_green_sea.png"; + break; + case 8: + $profile_pic = $profile_pic . "head_nephritis.png"; + break; + case 9: + $profile_pic = $profile_pic . "head_pete_river.png"; + break; + case 10: + $profile_pic = $profile_pic . "head_pomegranate.png"; + break; + case 11: + $profile_pic = $profile_pic . "head_pumpkin.png"; + break; + case 12: + $profile_pic = $profile_pic . "head_red.png"; + break; + case 13: + $profile_pic = $profile_pic . "head_sun_flower.png"; + break; + case 14: + $profile_pic = $profile_pic . "head_turqoise.png"; + break; + case 15: + $profile_pic = $profile_pic . "head_wet_asphalt.png"; + break; + case 16: + $profile_pic = $profile_pic . "head_wistera.png"; + break; + } + return $profile_pic; +} +?> \ No newline at end of file diff --git a/index.php b/index.php index df7eaa7..cfdd240 100644 --- a/index.php +++ b/index.php @@ -1,14 +1,5 @@ diff --git a/register.php b/register.php index 669509f..89a4151 100644 --- a/register.php +++ b/register.php @@ -1,171 +1,7 @@ 0) { - array_push($errors, "email_exists"); - } - } else { - array_push($errors, "email_invalid"); - } - } else { - array_push($errors, "email_mismatch"); - } - - if (strlen($fname) > 25 || strlen($fname) < 2) { - array_push($errors, "fname_invalid"); - } - - if (strlen($lname) > 25 || strlen($lname) < 2) { - array_push($errors, "lname_invalid"); - } - - if ($pass != $pass_conf) { - array_push($errors, "pass_mismatch"); - } else { - if(preg_match('/[^A-Za-z0-9]/', $pass)) { - array_push($errors, "pass_invalid_char"); - } else { - if (strlen($pass) > 30 || strlen($pass) < 5) { - array_push($errors, "pass_invalid_length"); - } - } - } - - if (empty($errors)) { - $pass = password_hash($pass, PASSWORD_BCRYPT); // Encrypts password - $username = strtolower($fname . "." . $lname); - $check_username_query = mysqli_query($con, "SELECT username FROM users WHERE username='$username'"); - - $i = 0; - $temp = $username; - while (mysqli_num_rows($check_username_query) != 0) { - $i++; - $temp = $username . $i; - $check_username_query = mysqli_query($con, "SELECT username FROM users WHERE username='$temp'"); - } - if ($i > 0) { - $username = $username . $i; - } - - $rand = rand(1,16); - $profile_pic = random_profile_pic($rand); - - $query = mysqli_query($con, "INSERT INTO users VALUES (NULL,'$fname','$lname','$username','$email','$pass','$date','$profile_pic','0','0','0',',')"); - } -} - -function random_profile_pic($rand) { - $profile_pic = "assets/profile_pics/defaults/"; - switch ($rand) { - case 1: - $profile_pic = $profile_pic . "head_alizarin.png"; - break; - case 2: - $profile_pic = $profile_pic . "head_amethyst.png"; - break; - case 3: - $profile_pic = $profile_pic . "head_belize_hole.png"; - break; - case 4: - $profile_pic = $profile_pic . "head_carrot.png"; - break; - case 5: - $profile_pic = $profile_pic . "head_deep_blue.png"; - break; - case 6: - $profile_pic = $profile_pic . "head_emerald.png"; - break; - case 7: - $profile_pic = $profile_pic . "head_green_sea.png"; - break; - case 8: - $profile_pic = $profile_pic . "head_nephritis.png"; - break; - case 9: - $profile_pic = $profile_pic . "head_pete_river.png"; - break; - case 10: - $profile_pic = $profile_pic . "head_pomegranate.png"; - break; - case 11: - $profile_pic = $profile_pic . "head_pumpkin.png"; - break; - case 12: - $profile_pic = $profile_pic . "head_red.png"; - break; - case 13: - $profile_pic = $profile_pic . "head_sun_flower.png"; - break; - case 14: - $profile_pic = $profile_pic . "head_turqoise.png"; - break; - case 15: - $profile_pic = $profile_pic . "head_wet_asphalt.png"; - break; - case 16: - $profile_pic = $profile_pic . "head_wistera.png"; - break; - } - return $profile_pic; -} - +require 'config/config.php'; +require 'includes/form_handlers/register_handler.php'; +require 'includes/form_handlers/login_handler.php'; ?> @@ -175,6 +11,18 @@ function random_profile_pic($rand) { + +
+
+
+ ";?> + +
+