This commit is contained in:
count-null 2025-02-09 12:02:22 -05:00
parent 7428ff8b8f
commit 9b15ac9fd3
87 changed files with 4975 additions and 1 deletions

331
src/controllers/account.php Normal file
View file

@ -0,0 +1,331 @@
<?php
namespace app\controllers;
use app\models\addresses;
use app\models\users;
use app\models\user_addresses;
class account
{
public static function index($defaults): void
{
if (!isset($_SESSION['user_id'])) {
header('Location: /account/login');
}
$email = $_SESSION['user_email'];
$user = users::getByEmail($email);
$default_shipping = null;
$default_billing = null;
$ship_addrs = [];
$bill_addrs = [];
$addresses = user_addresses::getShippingByUserId($user['id']);
foreach ($addresses as $address) {
if ($address['id'] == $user['shipping_address_id']){
$default_shipping = $address;
} else {
$ship_addrs[] = $address;
}
}
$bill_addresses = user_addresses::getBillingByUserId($_SESSION['user_id']);
foreach ($bill_addresses as $addr) {
if ($addr['id'] == $user['billing_address_id']){
$default_billing = $addr;
} else {
$bill_addrs[] = $addr;
}
}
echo $GLOBALS['twig']->render('lib/page/index.twig', array_merge($defaults, [
'child_template' => 'account/index.twig',
'page_title' => 'Manage Account - ' . $_ENV['APP_NAME'],
'user' => $user,
'shipping' => $ship_addrs,
'billing' => $bill_addrs,
'default_shipping' => $default_shipping,
'default_billing' => $default_billing,
'breadcrumbs' => [
[
'url' => null,
'title' => 'My Account',
]
]
]));
}
public static function billing($defaults)
{
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (!$_SESSION['user_id']) {
http_response_code(403);
}
$bill = addresses::validatePost("billing");
$bill_id = addresses::add(
$bill['name'],
$bill['company'],
$bill['street'],
$bill['boxapt'],
$bill['city'],
$bill['state'],
$bill['zip'],
$bill['phone'],
1,
0
);
user_addresses::add(
$_SESSION['user_id'],
$bill_id
);
$_SESSION['success'] = "Billing address saved!";
header('Location: /account/billing');
}
$email = $_SESSION['user_email'];
$user = users::getByEmail($email);
$default_billing = null;
$bill_addrs = [];
$bill_addresses = user_addresses::getBillingByUserId($_SESSION['user_id']);
foreach ($bill_addresses as $addr) {
if ($addr['id'] == $user['billing_address_id']){
$default_billing = $addr;
} else {
$bill_addrs[] = $addr;
}
}
echo $GLOBALS['twig']->render('lib/page/index.twig', array_merge($defaults, [
'child_template' => 'account/billing.twig',
'page_title' => 'Billing Information - ' . $_ENV['APP_NAME'],
'billing' => $bill_addrs,
'default_billing' => $default_billing,
'breadcrumbs' => [
[
'url' => '/account',
'title' => 'My Account'
],
[
'url' => null,
'title' => 'Billing'
]
]
]));
}
public static function profile()
{
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (!$_SESSION['user_id']) {
http_response_code(403);
}
users::updateProfileById($_SESSION['user_id'], $_POST);
header('Location: /account');
}
}
public static function login($defaults)
{
if (isset($_SESSION['user_id'])) {
header('Location: /account');
}
echo $GLOBALS['twig']->render('lib/page/index.twig', array_merge($defaults, [
'child_template' => 'account/login.twig',
'page_title' => 'Sign In or Create an Account!',
'breadcrumbs' => [
[
'url' => null,
'title' => 'My Account'
],
]
]));
}
public static function logout()
{
session_unset();
session_destroy();
header('Location: /');
}
public static function orders($defaults)
{
if (!isset($_SESSION['user_id'])) {
header('Location: /account/login');
}
echo $GLOBALS['twig']->render('lib/page/index.twig', array_merge($defaults, [
'child_template' => 'account/orders.twig',
'page_title' => 'View ' . $_ENV['APP_NAME'] . ' Orders',
'breadcrumbs' => [
[
'url' => '/account',
'title' => 'My Account'
],
[
'url' => null,
'title' => 'Orders'
]
]
]));
}
public static function returns($defaults)
{
echo $GLOBALS['twig']->render('lib/page/index.twig', array_merge($defaults, [
'child_template' => 'account/returns.twig',
'page_title' => 'View ' . $_ENV['APP_NAME'] . ' Returns',
'breadcrumbs' => [
[
'url' => '/account',
'title' => 'My Account'
],
[
'url' => null,
'title' => 'Returns'
]
]
]));
}
public static function shipping($defaults)
{
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (!$_SESSION['user_id']) {
http_response_code(403);
}
$ship = addresses::validatePost("shipping");
$ship_id = addresses::add(
$ship['name'],
$ship['company'],
$ship['street'],
$ship['boxapt'],
$ship['city'],
$ship['state'],
$ship['zip'],
$ship['phone'],
0,
1
);
user_addresses::add(
$_SESSION['user_id'],
$ship_id
);
$_SESSION['success'] = "Shipping address saved!";
header('Location: /account/shipping');
}
$email = $_SESSION['user_email'];
$user = users::getByEmail($email);
$addresses = user_addresses::getShippingByUserId($user['id']);
$default_shipping = null;
$ship_addrs = [];
foreach ($addresses as $addr) {
if ($addr['id'] == $user['shipping_address_id']){
$default_shipping = $addr;
} else {
$ship_addrs[] = $addr;
}
}
echo $GLOBALS['twig']->render('lib/page/index.twig', array_merge($defaults, [
'child_template' => 'account/shipping.twig',
'page_title' => $_ENV['APP_NAME'] . ' Shipping',
'shipping' => $ship_addrs,
'default_shipping' => $default_shipping,
'breadcrumbs' => [
[
'url' => '/account',
'title' => 'My Account'
],
[
'url' => null,
'title' => 'Shipping'
]
]
]));
}
public static function signup($defaults)
{
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$email = $_POST['email'];
$existingUser = users::getByEmail($email);
if ($existingUser) {
$_SESSION['error'] = 'Email already exists. Please choose a different email or log in.';
header('Location: /account/signup');
exit;
}
if (empty($email)) {
$_SESSION['error'] = 'Email is required.';
}
if (isset($_SESSION['error'])) {
header('Location: /account/signup');
}
$useShipping = $_POST['use_shipping'] ?? false;
if ($useShipping) {
$ship = addresses::validatePost("shipping");
} else {
$ship = addresses::validatePost("shipping");
$bill = addresses::validatePost("billing");
}
if (empty($email)) {
$_SESSION['error'] = 'Email is required.';
}
if (isset($_SESSION['error'])) {
$_SESSION['last_post'] = $_POST;
header('Location: /account/signup');
}
$ship_id = addresses::add(
$ship['name'],
$ship['company'],
$ship['street'],
$ship['boxapt'],
$ship['city'],
$ship['state'],
$ship['zip'],
$ship['phone'],
$useShipping == 'on',
1
);
$bill_id = $ship_id;
if (!$useShipping) {
$bill_id = addresses::add(
$bill['name'],
$bill['company'],
$bill['street'],
$bill['boxapt'],
$bill['city'],
$bill['state'],
$bill['zip'],
$bill['phone'],
1,
0
);
}
$opt_in_promotional = $_POST['opt_in_promotional'] ?? false;
$verified = isset($_SESSION['user_email']);
$dark_theme = $defaults['theme'] == 'dark';
$user_id = users::add(
$email,
$ship_id,
$bill_id,
$opt_in_promotional,
$verified,
$dark_theme
);
user_addresses::add(
user_id: $user_id,
address_id: $ship_id
);
if (!$useShipping) {
user_addresses::add(
user_id: $user_id,
address_id: $bill_id
);
}
$_SESSION['user_id'] = $user_id;
if (!$verified) {
header("Location: /magic-link?email=$email&signup=1");
exit;
}
header('Location: /account');
exit;
} // endif request === POST
if (isset($_SESSION['user_id'])) {
header('Location: /account');
exit;
}
echo $GLOBALS['twig']->render('lib/page/index.twig', array_merge($defaults, [
'child_template' => 'account/signup.twig',
'page_title' => 'Create an Account - ' . $_ENV['APP_NAME']
]));
}
}

18
src/controllers/cart.php Normal file
View file

@ -0,0 +1,18 @@
<?php
namespace app\controllers;
class cart
{
public static function index($defaults)
{
echo $GLOBALS['twig']->render('lib/page/index.twig', array_merge($defaults, [
'child_template' => 'cart.twig',
'page_title' => $_ENV['APP_NAME'] . ' Cart',
'breadcrumbs' => [
[
'url' => null,
'title' => 'Cart'
]
],
]));
}
}

View file

@ -0,0 +1,14 @@
<?php
namespace app\controllers;
class category
{
public static function power_meters($defaults)
{
echo $GLOBALS['twig']->render('lib/page/index.twig', context: array_merge($defaults, [
'child_template' => 'lib/page/category.twig',
'page_title' => 'Power Meters - ' . $_ENV['APP_NAME'],
'product_category' => 'power_meters',
]));
}
}

View file

@ -0,0 +1,26 @@
<?php
namespace app\controllers;
class checkout
{
public static function shipping_billing($defaults)
{
echo $GLOBALS['twig']->render('lib/page/flow.twig', array_merge($defaults, [
'child_template' => 'checkout/shipping_billing.twig',
'page_title' => 'Checkout with ' . $_ENV['APP_NAME'],
]));
}
public static function review_pay($defaults)
{
echo $GLOBALS['twig']->render('lib/page/flow.twig', array_merge($defaults, [
'child_template' => 'checkout/review_pay.twig',
'page_title' => 'Review & Payment | ' . $_ENV['APP_NAME']
]));
}
public static function confirmed($defaults)
{
echo $GLOBALS['twig']->render('lib/page/flow.twig', array_merge($defaults, [
'child_template' => 'checkout/confirmed.twig',
'page_title' => 'Order Recieved! - Thank You'
]));
}
}

12
src/controllers/home.php Normal file
View file

@ -0,0 +1,12 @@
<?php
namespace app\controllers;
class home
{
public static function index($defaults)
{
echo $GLOBALS['twig']->render(name: 'lib/page/index.twig', context: array_merge($defaults, [
'child_template' => 'home.twig',
'page_title' => $_ENV['APP_NAME'] . ": Specialty Hardware"
]));
}
}

View file

@ -0,0 +1,90 @@
<?php
namespace app\controllers;
use app\app;
class lnurlp
{
public static function index()
{
header(header: 'Content-Type: application/json');
$host = $_SERVER['HTTP_HOST'];
$user = $_GET["username"] ?? false;
$paymentRequest = $_GET["pay"] ?? false;
$verify = $_GET["verify"] ?? false;
function returnJson($arr): never
{
echo json_encode(value: $arr);
exit();
}
// for when the callback is used incorrectly
if ($paymentRequest != 1 && $paymentRequest != false) {
returnJson([
'status' => 'ERROR',
'reason' => 'invalid value for `pay` param (set `pay=1` or exclude `pay` from the url)',
]);
}
// for when the user is missing
if ($user == false && $verify == false) {
returnJson([
'status' => 'ERROR',
'reason' => 'no user specified (set `username=<name>` in the url)',
]);
}
list($proxy_user, $proxy_host) = explode("@", $_ENV['LN_ADDRESS']);
// for when the client makes it's first call (querying the lightning address)
$metadata = "[[\"text/plain\",\"Funding @$user on $host\"],[\"text/identifier\",\"$user@$host\"]]";
if ($paymentRequest == false && $verify == false) {
$res = json_decode(file_get_contents("https://$proxy_host/.well-known/lnurlp/$proxy_user"), true);
returnJson(
[
'callback' => "https://$host/lnurlp?pay=1&username=$user",
'maxSendable' => $res['maxSendable'],
'minSendable' => $res['minSendable'],
'metadata' => $metadata,
'commentAllowed' => $res['commentAllowed'],
'payerData' => $res['payerData'],
'tag' => "payRequest",
]
);
}
// for when the client makes it's second call (callback)
if ($paymentRequest == "1") {
$proxy_url = "https://$proxy_host/lnurlp?pay=1&username=$proxy_user";
if (isset($_GET["amount"])) {
$proxy_url .= "&amount=" . urlencode($_GET["amount"]);
}
$res = json_decode(file_get_contents($proxy_url), true);
if ($res['status'] === 'OK'){
$boom = explode("=", $res['verify']);
$proxy_verify = end($boom);
returnJson([
'status' => 'OK',
'pr' => $res['pr'],
'routes' => $res['routes'],
'verify' => "https://$host/lnurlp?verify=$proxy_verify"
]);
} else {
returnJson($res);
}
}
// for when they want to verify the payment succeeded
if ($verify) {
$res = json_decode(file_get_contents("https://$proxy_host/lnurlp?verify=$verify"), true);
returnJson($res);
}
// for when none of the above conditions are met
returnJson([
'status' => 'ERROR',
'reason' => 'unhandled error (how did you get here?)',
]);
}
}

12
src/controllers/lost.php Normal file
View file

@ -0,0 +1,12 @@
<?php
namespace app\controllers;
class lost
{
public static function index($defaults)
{
echo $GLOBALS['twig']->render('lib/page/index.twig', array_merge($defaults, [
'child_template' => '404.twig',
'page_title' => 'Not Found - ' . $_ENV['APP_NAME'],
]));
}
}

View file

@ -0,0 +1,67 @@
<?php
namespace app\controllers;
use app\app;
use app\models\users;
use app\models\magic_links;
class magic_link
{
public static function index()
{
$email = $_GET['email'] ?? null;
$token = $_GET['token'] ?? null;
$signup = $_GET['signup'] ?? null;
if (empty($email) && empty($token)) {
$_SESSION['error'] = "Enter your email to get a login link";
header('Location: /account/login');
exit;
}
if ($email && empty($token) && empty($signup)) {
$link = magic_links::add(email: $email);
$subject = "Your Magic Sign-In Link";
$message = "Copy and paste the link into your browser: $link";
$HTML_message = "Click the link to sign in: <a href='$link'>$link</a>";
app::send_mail(to: $email, from: $_ENV['SMTP_FROM'], from_name: $_ENV['APP_NAME'], subject: $subject, message: $message, HTML_message: $HTML_message);
$_SESSION['success'] = 'Link sent to your email!';
header('Location: /account/login');
exit;
}
if ($email && empty($token) && $signup == "1") {
$link = magic_links::add(email: $email);
$subject = "Your Magic Sign-In Link";
$message = "Copy and paste the link into your browser: $link";
$HTML_message = "Click the link to sign in: <a href='$link'>$link</a>";
app::send_mail(to: $email, from: $_ENV['SMTP_FROM'], from_name: $_ENV['APP_NAME'], subject: $subject, message: $message, HTML_message: $HTML_message);
$_SESSION['success'] = 'Account created! Please check your email inbox for the verification link.';
header('Location: /account/login');
exit;
}
if ($token && empty($email)) {
$link = magic_links::validate(token: $token);
if (!$link) {
$_SESSION['error'] = "Invalid or expired link.";
header('Location: /account/login');
}
// handle signup vs. login
$user = users::getByEmail($link['email']);
if ($user) {
$_SESSION['user_email'] = $link['email'];
$_SESSION['user_id'] = $user['id'];
if (!$user['verified']) {
users::verify($link['email']);
}
header('Location: /account');
} else {
// used to pre-fill email signup field
$_SESSION['user_email'] = $link['email'];
header('Location: /account/signup');
}
exit();
}
}
}

View file

@ -0,0 +1,35 @@
<?php
namespace app\controllers;
class support
{
public static function index($defaults)
{
$GLOBALS['twig']->render('lib/page/index.twig', array_merge($defaults, [
'child_template' => 'support/ask.twig',
'page_title' => $_ENV['APP_NAME'] . ': Frequently Asked Questions',
'breadcrumbs' => [
[
'url' => null,
'title' => 'Support'
]
]
]));
}
public static function bitcoin($defaults)
{
$GLOBALS['twig']->render('lib/page/index.twig', array_merge($defaults, [
'child_template' => 'support/bitcoin.twig',
'page_title' => $_ENV['APP_NAME'] . ' Bitcoin Accepted',
'breadcrumbs' => [
[
'url' => '/support/ask',
'title' => 'Support'
],
[
'url' => null,
'title' => 'Bitcoin'
]
],
]));
}
}