implemented hashbrown assignment.

This commit is contained in:
2025-04-17 14:58:51 -06:00
parent 5e88f52c21
commit d1097bedb3
157 changed files with 1038 additions and 12514 deletions
+139
View File
@@ -0,0 +1,139 @@
<?php
// Form handling
include_once 'includes/init.php';
if (isset($_POST['form_id'])) {
switch ($_POST['form_id']) {
case 'login':
$result = auth_user($_POST['username'], $_POST['password']);
if ($result === true)
header('Location: .');
else
$err = $result;
break;
case 'signup':
$result = create_user($_POST['username'], $_POST['password'], $_POST['v_password']);
if ($result === true)
header('Location: account.php?view=signup_success');
else
$err = $result;
break;
}
}
// HTML Generation
include_once 'includes/head.php';
if (isset($_GET['action']) && $_GET['action'] == 'logout')
logout();
if (!isset($_GET['view']) && !isset($_SESSION['hashbrown_access']))
$view = 'login';
else
$view = $_GET['view'];
echo '<div class="content">';
switch ($view) {
case 'login':
echo login((isset($err) ? $err : ''));
break;
case 'signup':
echo signup((isset($err) ? $err : ''));
break;
case 'display':
echo display();
break;
case 'signup_success':
echo signup_success();
break;
}
echo '</div>';
include_once 'includes/foot.php';
function login($err = null)
{
$form = '
<form method="post" class="card">
<input type="hidden" name="form_id" value="login">
<label for="username">Username:</label>
<input id="username" type="text" name="username"' . (isset($_POST['username']) ? ' value="' . $_POST['username'] . '"' : '') . ' required>
<label for="password">Password:</label>
<input id="password" type="password" name="password"' . (isset($_POST['password']) ? ' value="' . $_POST['password'] . '"' : '') . ' required>
<a href="account.php?view=signup">Or signup instead</a>
<div class="row">
<input type="reset" class="reset button">
<input type="submit" class="success button">
</div>
' . $err . '
</form>
';
return $form;
}
function signup($err = null)
{
$form = '
<form method="post" class="card">
<input type="hidden" name="form_id" value="signup">
<label for="username">Username:</label>
<input id="username" type="text" name="username"' . (isset($_POST['username']) ? ' value="' . $_POST['username'] . '"' : '') . ' required>
<label for="password">Password:</label>
<input id="password" type="password" name="password"' . (isset($_POST['password']) ? ' value="' . $_POST['password'] . '"' : '') . ' required>
<label for="v_password">Verify Password:</label>
<input id="v_password" type="password" name="v_password"' . (isset($_POST['v_password']) ? ' value="' . $_POST['v_password'] . '"' : '') . ' required>
<a href="account.php?view=login">Or login instead</a>
<div class="row">
<input type="reset" class="reset button">
<input type="submit" class="success button">
</div>
' . $err . '
</form>
';
return $form;
}
function signup_success()
{
$display = '
<div class="card success">
<h3>Account creation success!</h3>
<p><a href="account.php?view=login">Login</a> to get started.</p>
</div>
';
return $display;
}
function display()
{
$display = '
<h1>Account</h1>
<div class="content">
<div class="card">
<div class="row space_between">
<h3>' . $_SESSION['username'] . '</h3>
<img src="' . get_user_image() . '">
</div>
</div>
</div>
';
return $display;
}
+11
View File
@@ -0,0 +1,11 @@
{
"name": "violet/hashbrown",
"autoload": {
"psr-4": {
"Violet\\Hashbrown\\": "src/"
}
},
"require": {
"vlucas/phpdotenv": "^5.6"
}
}
View File
+214
View File
@@ -0,0 +1,214 @@
/********** GENERAL **********/
* {
margin: 0;
padding: 0;
box-sizing: border-box;
transition-duration: 500ms;
}
body {
/* Positioning */
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
/* Styling */
background-color: #bbedbe;
}
h1 {
/* Positioning */
width: 100%;
padding: 2vw;
/* Font */
font-size: 6vw;
text-align: center;
}
h3 {
font-size: 4vw;
}
h5,
label {
font-size: 2vw;
}
h1,
h3,
h5,
p,
nav ul li a,
a,
input {
color: #0e3610;
}
a:hover {
color: #1c6b20;
}
/********** NAV **********/
nav {
width: 100%;
}
nav ul {
display: flex;
flex-direction: row;
justify-content: left;
}
nav ul li {
display: inline-block;
}
nav ul li a {
font-size: 2vw;
font-weight: 800;
text-decoration: none;
display: block;
padding: 15px;
}
nav ul li a:hover {
transform: scale(1.2);
}
nav ul li a.active {
text-decoration: underline;
}
/********** GENERAL **********/
.content {
width: 80%;
margin: 0 auto;
}
form {
/* Form Shape */
width: 60%;
padding: 2.5vw 5vw 2.5vw 5vw;
/* Positioning */
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
.row {
/* Row Shape */
width: 100%;
display: flex;
flex-direction: row;
}
.space_between {
justify-content: space-between;
}
input {
/* Input Shape */
width: 50%;
padding: 1vw;
margin: 1vw;
border-radius: 18px;
border: none;
/* Styling */
background-color: #f1fbf2;
/* Font */
font-size: 1vw;
font-weight: 400;
}
input[type="text"],
input[type="password"] {
border: 2px solid #0e3610;
}
button {
/* Button Shape */
width: 60%;
padding: 1vw;
margin: 1vw;
border: none;
border-radius: 18px;
/* Font */
color: white;
font-size: 1vw;
font-weight: 600;
background-color: #1c6b20;
}
button:hover {
background-color: #26942D;
}
table {
border-spacing: 0;
table-layout: auto;
width: 90%;
border: 1px solid black;
}
td {
margin: 0;
padding: 1vw;
border: 1px solid black;
text-align: center;
}
.card {
padding: 50px;
background-color: white;
border-radius: 18px;
width: 80%;
margin: 0 auto;
}
.center {
margin: 0 auto;
text-align: center;
}
.card.error {
width: 30%;
}
.error,
.error *,
.reset,
.reset * {
color: white;
background-color: red;
font-weight: 800;
}
.success {
color: white;
background-color: #0e3610;
}
.success * {
color: white;
}
.success:hover {
background-color: #1c6b20;
}
img {
width: 250px;
height: auto;
border-radius: 50%;
}
View File
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 90 KiB

BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 84 KiB

BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

+1
View File
@@ -0,0 +1 @@
skinner,gatekeeper||>><<||csm,alien||>><<||dana,skeptic||>><<||fox,believer
+5
View File
@@ -0,0 +1,5 @@
</body>
<footer></footer>
</html>
+189
View File
@@ -0,0 +1,189 @@
<?php
// Use environment variables for the database password and IP address.
$db_pass = $_ENV['DATABASE_PASSWORD'];
$ip_addr = $_ENV['IP_ADDRESS'];
// Make some constants
define('HOST', 'localhost');
define('USER', 'root');
define('PASS', $db_pass);
define('DB', 'secureusers');
function table_from_file($file)
{
$file = 'includes/' . $file;
$fs = fopen($file, 'r');
$string = fread($fs, filesize($file));
$values = explode('||>><<||', $string);
$table = '
<div class="card center">
<table class="center">
<tr>
<th>Agent</th>
<th>Codename</th>
</tr>
';
foreach ($values as $v) {
list($a, $b) = explode(',', $v);
$table .= '
<tr>
<td>' . ucfirst($a) . '</td>
<td>' . ucwords($b) . '</td>
</tr>
';
}
$table .= '</div></table>';
return $table;
}
function salt_and_hash($pw)
{
$salt = 'fdiojhlkjasdfjh';
$o_salt = 'qwelnbfnvaslkfj';
$pw = $salt . $pw . $o_salt;
return hash('sha512', $pw);
}
function increment_login_count($id)
{
p_query('UPDATE users SET login_count = login_count + 1 WHERE id = ?', 'i', $id);
}
function auth_user($username, $password)
{
$results = p_query('SELECT id, password FROM users WHERE username = ?', 's', $_POST['username'])->fetch_assoc();
if (!$results) {
return '
<div class="card error">
<p>Username or Password is incorrect!</p>
</div>
';
} else {
if ($results['password'] == salt_and_hash($password)) {
$_SESSION['hashbrown_access'] = true;
$_SESSION['username'] = $username;
increment_login_count($results['id']);
return true;
} else {
if (isset($_SESSION['login_attempts']))
$_SESSION['login_attempts']++;
else
$_SESSION['login_attempts'] = 1;
if ($_SESSION['login_attempts'] >= 3) {
return '
<div class="card error">
<p>Reached 3+ failed login attempts!</p>
</div>
';
} else {
return '
<div class="card error">
<p>Username or Password is incorrect!</p>
</div>
';
}
}
}
}
function create_user($username, $password, $v_password)
{
if ($password != $v_password) {
return '
<div class="card error">
<p>Passwords do not match!</p>
</div>
';
}
$exists = p_query('SELECT username FROM users WHERE username = ?', 's', $username)->fetch_assoc();
if ($exists) {
return '
<div class="card error">
<p>Username is taken!</p>
</div>
';
}
$user = p_query('INSERT INTO users (username, password) VALUES (?, ?)', 'ss', $username, salt_and_hash($password));
if (!$user) {
return '
<div class="card error">
<p>Unable to create user!</p>
</div>
';
}
return true;
}
function logout()
{
foreach (array_keys($_SESSION) as $key)
unset($_SESSION[$key]);
header('Location: .');
}
function get_user_image()
{
$result = p_query('SELECT image_path FROM users WHERE username = ?', 's', $_SESSION['username'])->fetch_assoc();
return 'img/' . $result['image_path'];
}
function update_user_pw($password, $v_password)
{
if ($password != $v_password) {
return '
<div class="card error">
<p>Passwords do not match!</p>
</div>
';
}
$result = p_query('UPDATE users SET password = ? WHERE username = ?', 'ss', salt_and_hash($password), $_SESSION['username']);
if ($result) {
return true;
} else {
return false;
}
}
function query($stmt)
{
$conn = new mysqli(HOST, USER, PASS, DB);
$result = $conn->query($stmt);
if ($result)
return $result;
else
return mysqli_insert_id($conn);
}
function p_query($stmt, $types, ...$args)
{
$conn = new mysqli(HOST, USER, PASS, DB);
$stmt = $conn->prepare($stmt);
$stmt->bind_param($types, ...$args);
$stmt->execute();
$result = $stmt->get_result();
if ($result)
return $result;
else
return mysqli_insert_id($conn);
}
function pretty_dump($arg)
{
echo '<pre>';
print_r($arg);
echo '</pre>';
}
+13
View File
@@ -0,0 +1,13 @@
<?php include_once 'includes/nav.php'; ?>
<!doctype html>
<html lang="en">
<head>
<title>Hashbrowns</title>
<link href="css/styles.css" type="text/css" rel="stylesheet">
<script src="js/index.js"></script>
</head>
<body>
<?php echo nav(); ?>
+8
View File
@@ -0,0 +1,8 @@
<?php
session_start();
require_once 'vendor/autoload.php';
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__ . '/../');
$dotenv->safeLoad();
require_once 'includes/functions.php';
+26
View File
@@ -0,0 +1,26 @@
<?php
function nav()
{
$nav = '
<nav>
<ul>
<li><a href="index.php"' . ($_SERVER['SCRIPT_NAME'] == '/index.php' ? ' class="active"' : '') . '>Home</a></li>
';
if (isset($_SESSION['hashbrown_access']) && $_SESSION['hashbrown_access']) {
$nav .= '
<li><a href="account.php?view=display"' . ($_SERVER['SCRIPT_NAME'] == '/account.php' ? ' class="active"' : '') . '>Account</a></li>
<li><a href="users.php"' . ($_SERVER['SCRIPT_NAME'] == '/users.php' ? ' class="active"' : '') . '>Users</a></li>
<li><a href="video.php"' . ($_SERVER['SCRIPT_NAME'] == '/video.php' ? ' class="active"' : '') . '>Video</a></li>
<li><a href="account.php?action=logout">Logout</a></li>
';
}
$nav .= '
</ul>
</nav>
';
return $nav;
}
+1
View File
@@ -0,0 +1 @@
dale,the government||>><<||bill,the barber||>><<||hank,the texan||>><<||boomhauer,the marshall
+29
View File
@@ -0,0 +1,29 @@
<?php
include_once 'includes/init.php';
if (!isset($_SESSION['hashbrown_access']) || !$_SESSION['hashbrown_access'])
header('Location: account.php?view=login');
include_once 'includes/head.php';
echo '<div id="content">';
echo getHome((isset($_GET['file']) ? $_GET['file'] : 'fbi.txt'));
echo '</div>';
include_once 'includes/foot.php';
function getHome($file)
{
$view = '
<h1>View Confidential Information</h1>
<form method="get" class="center">
<div class="row">
<button name="file" value="fbi.txt">FBI</button>
<button name="file" value="spies.txt">Spies</button>
</div>
</form>
' . table_from_file($file) . '
';
return $view;
}
View File
View File
+32
View File
@@ -0,0 +1,32 @@
<?php
include_once 'includes/init.php';
if (!isset($_SESSION['hashbrown_access']) || !$_SESSION['hashbrown_access'])
header('Location: account.php?view=login');
$users = query('SELECT * FROM users');
include_once 'includes/head.php';
echo '
<div class="card">
<h3 class="center">Users</h3>
<table class="center">
<tr>
<th>ID</th>
<th>Username</th>
<th>Password</th>
</tr>
';
foreach ($users as $u) {
echo '
<tr>
<td>' . $u['id'] . '</td>
<td>' . $u['username'] . '</td>
<td>' . $u['password'] . '</td>
</tr>
';
}
echo '</div></table>';
include_once 'includes/foot.php';
+13
View File
@@ -0,0 +1,13 @@
<?php
include_once 'includes/init.php';
if (!isset($_SESSION['hashbrown_access']) || !$_SESSION['hashbrown_access'])
header('Location: account.php?view=login');
include_once 'includes/functions.php';
include_once 'includes/head.php';
echo '<h1>YouTube Video</h1>';
echo '<iframe width="560" height="315" src="https://www.youtube.com/embed/8BIcAZxFfrc?si=DMccopjQMFNs361I" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>';
include_once 'includes/foot.php';