This commit is contained in:
@@ -8,6 +8,21 @@ function pretty_dump($arg)
|
||||
return ob_get_clean();
|
||||
}
|
||||
|
||||
function format_money($in)
|
||||
{
|
||||
$in = '$' . $in;
|
||||
$parts = explode('.', $in);
|
||||
if (count($parts) == 1)
|
||||
$in .= '.00';
|
||||
else {
|
||||
if (strlen($parts[1]) == 1) {
|
||||
$in .= '0';
|
||||
}
|
||||
}
|
||||
|
||||
return $in;
|
||||
}
|
||||
|
||||
function random_string()
|
||||
{
|
||||
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
||||
|
||||
@@ -1,14 +1,84 @@
|
||||
<?php
|
||||
if (isset($_POST['form_id'])) {
|
||||
session_start();
|
||||
require_once '../vendor/autoload.php';
|
||||
// Load environment variables.
|
||||
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__ . '/../');
|
||||
$dotenv->safeLoad();
|
||||
require_once 'base.php';
|
||||
require_once 'db.php';
|
||||
|
||||
switch ($_POST['form_id']) {
|
||||
case 'add_to_cart':
|
||||
if (isset($_POST['product_id']) && isset($_POST['quantity']))
|
||||
echo urlencode(add_to_cart($_POST['product_id'], $_POST['quantity']));
|
||||
break;
|
||||
case 'cart':
|
||||
handle_cart();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
function handle_cart()
|
||||
{
|
||||
if (!isset($_POST['product_id'])) {
|
||||
echo urlencode('failure');
|
||||
return;
|
||||
}
|
||||
|
||||
$id = $_POST['product_id'];
|
||||
|
||||
if (isset($_POST['action'])) {
|
||||
switch ($_POST['action']) {
|
||||
case 'remove':
|
||||
$_SESSION['cart'][$id] = 0;
|
||||
unset($_SESSION['cart'][$id]);
|
||||
echo urlencode(format_money(calculate_total()));
|
||||
break;
|
||||
case 'adjust':
|
||||
$_SESSION['cart'][$id] = $_POST['quantity'];
|
||||
echo urlencode(format_money(calculate_total()));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function calculate_total()
|
||||
{
|
||||
$total = 0;
|
||||
foreach ($_SESSION['cart'] as $id => $q) {
|
||||
$product = fetch_product($id);
|
||||
$total += $product['price'] * $q;
|
||||
}
|
||||
|
||||
return $total;
|
||||
}
|
||||
|
||||
function set_quantity($id, $q)
|
||||
{
|
||||
if (isset($_SESSION['cart'][$id]))
|
||||
$_SESSION['cart'][$id] = $q;
|
||||
}
|
||||
|
||||
function add_to_cart($id, $q)
|
||||
{
|
||||
if (!isset($_SESSION['cart'][$id]))
|
||||
$_SESSION['cart'][$id] = $q;
|
||||
else
|
||||
$_SESSION['cart'][$id] = $_SESSION['cart'][$id] + $q;
|
||||
|
||||
return count($_SESSION['cart']);
|
||||
}
|
||||
|
||||
function fetch_catalog()
|
||||
{
|
||||
$conn = get_connection();
|
||||
$result = $conn->query('SELECT * FROM product');
|
||||
return $result;
|
||||
$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();
|
||||
$result = exec_stmt('SELECT * FROM product WHERE id = ?', 'i', $id);
|
||||
return $result->fetch_assoc();
|
||||
}
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
<?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'];
|
||||
$db = $_ENV['DB'];
|
||||
$db_user = $_ENV['DB_UN'];
|
||||
$db_pass = $_ENV['DB_PW'];
|
||||
$ip_addr = $_ENV['DB_IP'];
|
||||
|
||||
// Make some constants
|
||||
if ($_SERVER['HTTP_HOST'] == 'localhost:2440') {
|
||||
@@ -26,10 +26,11 @@ function exec_stmt($stmt, $param_types, ...$args)
|
||||
$stmt->execute();
|
||||
$result = $stmt->get_result();
|
||||
|
||||
if ($result)
|
||||
if ($result) {
|
||||
return $result;
|
||||
else
|
||||
} else {
|
||||
return mysqli_insert_id($conn);
|
||||
}
|
||||
}
|
||||
|
||||
function get_connection()
|
||||
|
||||
@@ -11,3 +11,7 @@ $dotenv = Dotenv\Dotenv::createImmutable(__DIR__ . '/../');
|
||||
$dotenv->safeLoad();
|
||||
|
||||
require_once 'functions/db.php';
|
||||
|
||||
if (isset($_SESSION['id']))
|
||||
if (!isset($_SESSION['cart']))
|
||||
$_SESSION['cart'] = [];
|
||||
|
||||
+12
-10
@@ -1,12 +1,12 @@
|
||||
<?php
|
||||
|
||||
if (isset($_POST['form_id'])) {
|
||||
require_once '../vendor/autoload.php';
|
||||
include_once '../vendor/autoload.php';
|
||||
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__ . '/../');
|
||||
$dotenv->safeLoad();
|
||||
|
||||
require_once 'base.php';
|
||||
require_once 'db.php';
|
||||
include_once 'base.php';
|
||||
include_once 'db.php';
|
||||
|
||||
switch ($_POST['form_id']) {
|
||||
case 'create-account':
|
||||
@@ -15,6 +15,9 @@ if (isset($_POST['form_id'])) {
|
||||
echo json_encode($result);
|
||||
}
|
||||
break;
|
||||
case 'login':
|
||||
if (isset($_POST['username']) && isset($_POST['password']))
|
||||
echo json_encode(login($_POST['username'], $_POST['password']));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,29 +33,28 @@ function username_exists($username)
|
||||
|
||||
function login($username, $password)
|
||||
{
|
||||
$user = exec_stmt('SELECT id FROM user WHERE username = ? AND password = ?', 'ss', $username, $password)->fetch_assoc();
|
||||
$user = exec_stmt('SELECT id, password FROM user WHERE username = ?', 's', $username)->fetch_assoc();
|
||||
|
||||
if (!isset($user['id']))
|
||||
return false;
|
||||
|
||||
if (!password_verify($password, $user['password'])) {
|
||||
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;
|
||||
$user_id = exec_stmt('INSERT INTO user (username, password) VALUES (?, ?)', 'ss', $username, password_hash($password, PASSWORD_DEFAULT));
|
||||
|
||||
if (!isset($user_id))
|
||||
return false;
|
||||
|
||||
Reference in New Issue
Block a user