This commit is contained in:
2025-05-01 14:37:08 -06:00
parent b6008d462e
commit 5cd4571a92
91 changed files with 1009 additions and 36 deletions
+38
View File
@@ -0,0 +1,38 @@
<?php
function pretty_dump($arg)
{
ob_start();
echo '<pre>';
print_r($arg);
echo '</pre>';
return ob_get_clean();
}
function random_string()
{
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$rand_string = '';
for ($i = 0; $i < 512; $i++)
$rand_string = $characters[rand(0, strlen($characters) - 1)];
return $rand_string;
}
function verify_hash($arg, $hash_salt)
{
list($salt_hex, $hash) = explode('.', $hash_salt, 2);
$salt = hex2bin($salt_hex);
$calculated_hash = hash('sha512', $salt . $arg . $_ENV['SALT']);
return hash_equals($hash, $calculated_hash);
}
function hash_salt($arg = null)
{
if (!$arg)
$arg = random_string();
$salt1 = random_bytes(256);
$salt2 = $_ENV['SALT'];
return bin2hex($salt1) . '.' . hash('sha512', $salt1 . $arg . $salt2);
}
+14
View File
@@ -0,0 +1,14 @@
<?php
function fetch_catalog()
{
$conn = get_connection();
$result = $conn->query('SELECT * FROM product');
return $result;
}
function fetch_product($id)
{
$result = exec_stmt('SELECT * FROM product WHERE id = ?', 'i', $id);
return $result->fetch_assoc();
}
+39
View File
@@ -0,0 +1,39 @@
<?php
// Use environment variables for the database password and IP address.
$db = $_ENV['DATABASE'];
$db_user = $_ENV['DATABASE_USER'];
$db_pass = $_ENV['DATABASE_PASSWORD'];
$ip_addr = $_ENV['IP_ADDRESS'];
// Make some constants
if ($_SERVER['HTTP_HOST'] == 'localhost:2440') {
define('HOST', 'localhost');
define('USER', $db_user);
define('PASS', $db_pass);
define('DB', $db);
} else {
define('HOST', $ip_addr);
define('USER', $db_user);
define('PASS', $db_pass);
define('DB', $db);
}
function exec_stmt($stmt, $param_types, ...$args)
{
$conn = get_connection();
$stmt = $conn->prepare($stmt);
$stmt->bind_param($param_types, ...$args);
$stmt->execute();
$result = $stmt->get_result();
if ($result)
return $result;
else
return mysqli_insert_id($conn);
}
function get_connection()
{
$conn = new mysqli(HOST, USER, PASS, DB);
return $conn;
}
+13
View File
@@ -0,0 +1,13 @@
<?php
session_start();
header("Content-Security-Policy: default-src 'self'; font-src 'self' https://www.nerdfonts.com/assets/fonts/Symbols-2048-em%20Nerd%20Font%20Complete.woff2; style-src 'self' 'unsafe-inline' https://nerdfonts.com/assets/css/webfont.css https://www.nerdfonts.com/assets/css/webfont.css; script-src 'self' 'unsafe-inline'; img-src 'self';");
// Initialize Composer.
require_once 'vendor/autoload.php';
require_once 'functions/base.php';
// Load environment variables.
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__ . '/../');
$dotenv->safeLoad();
require_once 'functions/db.php';
+62
View File
@@ -0,0 +1,62 @@
<?php
if (isset($_POST['form_id'])) {
require_once '../vendor/autoload.php';
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__ . '/../');
$dotenv->safeLoad();
require_once 'base.php';
require_once 'db.php';
switch ($_POST['form_id']) {
case 'create-account':
if (isset($_POST['username'])) {
$result['exists'] = (username_exists(urldecode($_POST['username'])) ? 'True' : 'False');
echo json_encode($result);
}
break;
}
}
function username_exists($username)
{
$result = exec_stmt('SELECT username FROM user WHERE username = ?', 's', $username)->fetch_assoc();
if (isset($result['username']))
return true;
else
return false;
}
function login($username, $password)
{
$user = exec_stmt('SELECT id FROM user WHERE username = ? AND password = ?', 'ss', $username, $password)->fetch_assoc();
if (!isset($user['id']))
return false;
$_SESSION['id'] = $user['id'];
return true;
}
function signup($username, $password, $verify_password)
{
echo 'test';
if (username_exists($username))
return false;
echo 'username=' . $username;
if ($password != $verify_password)
return false;
$user_id = exec_stmt('INSERT INTO user (username, password) VALUES (?, ?)', 'ss', $username, $password);
echo 'user_id=' . $user_id;
if (!isset($user_id))
return false;
$_SESSION['id'] = $user_id;
return true;
}