aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlonkaars <loek@pipeframe.xyz>2023-05-08 17:08:54 +0200
committerlonkaars <loek@pipeframe.xyz>2023-05-08 17:08:54 +0200
commitcda96d31939c7ea727c114b162f43bb4d18314a2 (patch)
treeaa1aac2c746c5a1452b11af3f187d499b5a9ce5f
parent08d6281d872a8d2a496462f131cbdc860432acc0 (diff)
more website
-rw-r--r--db/data.sql22
-rw-r--r--db/init.sql3
-rw-r--r--lib/login.php36
-rw-r--r--public/cart.php2
-rw-r--r--public/global.css11
-rw-r--r--public/img/product/1-thumb.jpgbin0 -> 20762 bytes
-rw-r--r--public/img/product/2-thumb.jpgbin0 -> 16579 bytes
-rw-r--r--public/img/product/3-thumb.jpgbin0 -> 22837 bytes
-rw-r--r--public/img/product/4-thumb.jpgbin0 -> 23308 bytes
-rw-r--r--public/index.php10
-rw-r--r--public/login.css25
-rw-r--r--public/login.php39
-rw-r--r--public/product.css9
-rw-r--r--public/product.php33
-rw-r--r--public/products.css10
-rw-r--r--public/products.php5
-rw-r--r--public/register.php38
17 files changed, 212 insertions, 31 deletions
diff --git a/db/data.sql b/db/data.sql
index 4fb248e..55f59df 100644
--- a/db/data.sql
+++ b/db/data.sql
@@ -3,17 +3,17 @@ insert into webs.category (`name`) values
("Boeken"),
("Overig");
-insert into webs.product (`name`, `price`, `category`) values
- ("Appel", 0.90, 1),
- ("Banaan", 1.10, 1),
- ("Peer", 0.99, 1),
- ("ハリネズミのハリー 第1巻", 5.99, 1),
- ("ハリネズミのハリー 第2巻", 5.99, 1),
- ("ハリネズミのハリー 第3巻", 5.99, 1),
- ("イジらないで、長瀞さん 第1巻", 5.49, 1),
- ("イジらないで、長瀞さん 第2巻", 5.49, 1),
- ("Kip", 9.95, 3),
- ("Kikker", 19.95, 3);
+insert into webs.product (`name`, `price`, `category`, `image`) values
+ ("Appel", 0.90, 1, true),
+ ("Banaan", 1.10, 1, true),
+ ("Peer", 0.99, 1, true),
+ ("ハリネズミのハリー 第1巻", 5.99, 1, true),
+ ("ハリネズミのハリー 第2巻", 5.99, 1, false),
+ ("ハリネズミのハリー 第3巻", 5.99, 1, false),
+ ("イジらないで、長瀞さん 第1巻", 5.49, 1, false),
+ ("イジらないで、長瀞さん 第2巻", 5.49, 1, false),
+ ("Kip", 9.95, 3, false),
+ ("Kikker", 19.95, 3, false);
insert into webs.customer (`name`) values
("loek"),
diff --git a/db/init.sql b/db/init.sql
index d84d4e9..a9fc25b 100644
--- a/db/init.sql
+++ b/db/init.sql
@@ -15,7 +15,8 @@ create table if not exists webs.product (
`ID` int not null auto_increment,
`name` varchar(255) not null,
`price` decimal(5, 2) not null,
- `image_path` varchar(255) not null default "img/placeholder.png",
+ `image` boolean not null default false,
+ `description` mediumtext null default null,
`category` int not null,
primary key (`ID`),
constraint `product_category_fk`
diff --git a/lib/login.php b/lib/login.php
new file mode 100644
index 0000000..1da5a02
--- /dev/null
+++ b/lib/login.php
@@ -0,0 +1,36 @@
+<?php
+function check_login() {
+ if(!isset($_COOKIE['username'])) return false;
+ if(!isset($_COOKIE['password'])) return false;
+
+ return true;
+}
+
+$logged_in = check_login();
+
+function if_logged_in($is, $redirect, $back = false) {
+ global $logged_in;
+ if ($logged_in != $is) return;
+ if ($back) {
+ $prev = $_SERVER['HTTP_REFERER'];
+ $ONE_HOUR = time() + (60 * 60);
+ setcookie("prev", $prev, $ONE_HOUR, "/");
+ }
+ http_response_code(302);
+ header("Location: ".$redirect);
+ die();
+}
+
+function cookie_redir($username, $password, $url = "") {
+ $ONE_YEAR = time() + (60 * 60 * 24 * 365);
+ setcookie("username", $username, $ONE_YEAR, "/");
+ setcookie("password", $password, $ONE_YEAR, "/"); // TODO: use tokens to login
+ if (!$url) {
+ $prev = $_COOKIE['prev'];
+ if(!$prev) $url = "/";
+ else $url = $prev;
+ }
+ header("Location: ".$url);
+ die();
+}
+?>
diff --git a/public/cart.php b/public/cart.php
index 1ad48cd..e1c7907 100644
--- a/public/cart.php
+++ b/public/cart.php
@@ -1,4 +1,6 @@
<!DOCTYPE html>
+<?php require "../lib/login.php" ?>
+<?php if_logged_in(false, "/login.php", true) ?>
<html>
<head>
<?php include 'head.php' ?>
diff --git a/public/global.css b/public/global.css
index 82dc446..c813911 100644
--- a/public/global.css
+++ b/public/global.css
@@ -20,3 +20,14 @@ body, html {
text-decoration: none !important;
color: currentColor;
}
+
+.main {
+ margin-top: 2rem;
+}
+
+.price::before {
+ content: "\20ac";
+ margin-right: 0.3ex;
+ font-size: 80%;
+}
+
diff --git a/public/img/product/1-thumb.jpg b/public/img/product/1-thumb.jpg
new file mode 100644
index 0000000..ef1de8b
--- /dev/null
+++ b/public/img/product/1-thumb.jpg
Binary files differ
diff --git a/public/img/product/2-thumb.jpg b/public/img/product/2-thumb.jpg
new file mode 100644
index 0000000..dc8de24
--- /dev/null
+++ b/public/img/product/2-thumb.jpg
Binary files differ
diff --git a/public/img/product/3-thumb.jpg b/public/img/product/3-thumb.jpg
new file mode 100644
index 0000000..e6a0b47
--- /dev/null
+++ b/public/img/product/3-thumb.jpg
Binary files differ
diff --git a/public/img/product/4-thumb.jpg b/public/img/product/4-thumb.jpg
new file mode 100644
index 0000000..2295dc2
--- /dev/null
+++ b/public/img/product/4-thumb.jpg
Binary files differ
diff --git a/public/index.php b/public/index.php
index 0f268cb..653962c 100644
--- a/public/index.php
+++ b/public/index.php
@@ -1,4 +1,5 @@
<!DOCTYPE html>
+<?php require "db.php"; ?>
<html>
<head>
<?php include 'head.php' ?>
@@ -11,9 +12,12 @@
<div class="s1">
<h1>hier zijn de aanbiedingen</h1>
<ul>
- <li>Lorem ipsum dolor sit amet consectetur adipisicing elit. Fuga iure id repudiandae sapiente voluptatibus soluta ducimus? Molestiae pariatur optio saepe nihil fugit, commodi atque! Consequatur at omnis recusandae nostrum tenetur!</li>
- <li>Ut, repudiandae? Iusto pariatur cupiditate, ipsam magni accusamus asperiores possimus nulla, magnam quas consectetur fugit vero quibusdam officiis quod, vitae soluta nostrum placeat itaque sit beatae quos laudantium? Temporibus, magni.</li>
- <li>Ipsum fugiat corporis impedit iusto possimus neque alias at sunt commodi deserunt! Numquam perferendis rem ea provident velit dicta quae. Eveniet officia iure porro aliquid minus alias at commodi nam?</li>
+ <?php
+ $res = $cursor->query("select product.ID as id, product.name as name from promotion join product on product.ID = promotion.product");
+ while ($product = $res->fetch_object()) {
+ echo "<li><a href=\"/product.php?id=$product->id\">$product->name</a></li>";
+ }
+ ?>
</ul>
</div>
</div>
diff --git a/public/login.css b/public/login.css
new file mode 100644
index 0000000..c4826e2
--- /dev/null
+++ b/public/login.css
@@ -0,0 +1,25 @@
+.modal {
+ max-width: 300px;
+ margin: 0 auto;
+}
+
+.modal form {
+ display: grid;
+ margin-block-end: 0;
+}
+
+.modal input {
+ margin-bottom: 16px;
+ padding: 6px 8px;
+ border-radius: 8px;
+ border: 2px solid canvastext;
+ background-color: transparent;
+}
+
+.modal input[type="submit"] {
+ background-color: canvastext;
+ color: canvas;
+ font-size: 1rem;
+ font-weight: bold;
+ cursor: pointer;
+}
diff --git a/public/login.php b/public/login.php
new file mode 100644
index 0000000..22ac164
--- /dev/null
+++ b/public/login.php
@@ -0,0 +1,39 @@
+<!DOCTYPE html>
+<?php include "../lib/login.php" ?>
+<?php // if_logged_in(true, "/") ?>
+<?php
+do {
+ if ($_SERVER['REQUEST_METHOD'] !== 'POST') break;
+ if (!$_POST['username']) break;
+ if (!$_POST['password']) break;
+
+ //TODO: check if user exists in database
+
+ // if all guards passed, successful login occurred
+ cookie_redir($_POST['username'], $_POST['password']);
+} while (false);
+?>
+<html>
+<head>
+ <?php include 'head.php' ?>
+ <title>login</title>
+ <link rel='stylesheet' type='text/css' media='screen' href='login.css'>
+</head>
+<body>
+ <?php include 'navbar.php' ?>
+ <div class="main limwidth">
+ <h1>Inloggen</h1>
+ <div class="modal">
+ <form action="/login.php" method="post">
+ <label for="username">Gebruikersnaam</label>
+ <input id="username" name="username" type="text" placeholder="gebruikersnaam">
+ <label for="password">Wachtwoord</label>
+ <input id="password" name="password" type="password" placeholder="wachtwoord">
+ <input type="submit" value="Inloggen">
+ </form>
+ <span class="register">Of <a href="/register.php">een nieuw account maken</a></span>
+ </div>
+ </div>
+ <?php include 'footer.php' ?>
+</body>
+</html>
diff --git a/public/product.css b/public/product.css
new file mode 100644
index 0000000..5b718c9
--- /dev/null
+++ b/public/product.css
@@ -0,0 +1,9 @@
+.twocolumn {
+ display: grid;
+ grid-template-columns: 200px 1fr;
+ gap: 16px;
+}
+
+.twocolumn .left {
+ text-align: center;
+}
diff --git a/public/product.php b/public/product.php
index 8733562..3e6a89f 100644
--- a/public/product.php
+++ b/public/product.php
@@ -1,17 +1,40 @@
<!DOCTYPE html>
+<?php require "db.php"; ?>
+<?php
+function refuse() {
+ http_response_code(404);
+ die();
+}
+
+$statement = $cursor->prepare("select ID as id, image, price, name, description from webs.product where ID = ?");
+$statement->bind_param("i", $_GET['id']);
+if (!$statement->execute()) refuse();
+$res = $statement->get_result();
+if (!mysqli_num_rows($res)) refuse();
+$product = $res->fetch_object();
+?>
<html>
<head>
<?php include 'head.php' ?>
<title>dit is product</title>
+ <link rel='stylesheet' type='text/css' media='screen' href='product.css'>
</head>
<body>
<?php include 'navbar.php' ?>
<div class="main limwidth">
- <h2>yeah product</h2>
- <img src="img/placeholder.png" alt="">
- <span class="price">3,45</span>
- <span class="name">courgette</span>
- <span class="info">Lorem ipsum dolor sit amet consectetur adipisicing elit. Nulla dignissimos laboriosam voluptatem facilis tempore quos, asperiores eos molestiae voluptates commodi animi enim quae deleniti? Ratione optio eligendi rem eveniet reiciendis.</span>
+ <div class="twocolumn">
+ <div class="column left">
+ <img src="<?php echo "/img/product/".$product->id."-thumb.jpg" ?>" alt="">
+ </div>
+ <div class="column right">
+ <h2><?php echo $product->name ?></h2>
+ <span class="price"><?php echo $product->price ?></span>
+ <span class="info"><?php echo $product->description ?></span>
+ <form action="/cart.php" method="post">
+ <input type="submit" value="Toevoegen aan winkelwagen">
+ </form>
+ </div>
+ </div>
</div>
<?php include 'footer.php' ?>
</body>
diff --git a/public/products.css b/public/products.css
index 7a4f3ab..4d7342f 100644
--- a/public/products.css
+++ b/public/products.css
@@ -21,12 +21,4 @@
}
.product span { display: block; }
-
-.product .price::before {
- content: "\20ac";
- margin-right: 0.3ex;
- font-size: 80%;
-}
-.product .price {
- font-size: 1.5rem;
-}
+.product .price { font-size: 1.5rem; }
diff --git a/public/products.php b/public/products.php
index e9c7cdf..bffaeb0 100644
--- a/public/products.php
+++ b/public/products.php
@@ -2,9 +2,10 @@
<?php require "db.php"; ?>
<?php
function product_template($product) {
+ $image_path = $product->img ? "/img/product/$product->id-thumb.jpg" : "/img/placeholder.png";
echo <<<"EOF"
<a href="/product.php?id=$product->id" class="product nolinkstyle">
- <img src="$product->img" alt="">
+ <img src="$image_path" alt="">
<span class="price">$product->price</span>
<span class="name">$product->name</span>
</a>
@@ -23,7 +24,7 @@ EOF;
<h2>lijst met producten:</h2>
<div class="products">
<?php
- $res = $cursor->query("select ID as id, image_path as img, price, name from webs.product");
+ $res = $cursor->query("select ID as id, image as img, price, name from webs.product");
while ($product = $res->fetch_object()) product_template($product);
?>
</div>
diff --git a/public/register.php b/public/register.php
new file mode 100644
index 0000000..9f04b1c
--- /dev/null
+++ b/public/register.php
@@ -0,0 +1,38 @@
+<?php include "../lib/login.php" ?>
+<?php // if_logged_in(true, "/") ?>
+<?php
+do {
+ if ($_SERVER['REQUEST_METHOD'] !== 'POST') break;
+ if (!$_POST['username']) break;
+ if (!$_POST['password']) break;
+
+ //TODO: create new user in database
+
+ // if all guards passed, successful login occurred
+ cookie_redir($_POST['username'], $_POST['password'], "/");
+} while (false);
+?>
+<html>
+<head>
+ <?php include 'head.php' ?>
+ <title>registeren</title>
+ <link rel='stylesheet' type='text/css' media='screen' href='login.css'>
+</head>
+<body>
+ <?php include 'navbar.php' ?>
+ <div class="main limwidth">
+ <h1>Registreren</h1>
+ <div class="modal">
+ <form action="/register.php" method="post">
+ <label for="username">Gebruikersnaam</label>
+ <input id="username" name="username" type="text" placeholder="gebruikersnaam">
+ <label for="password">Wachtwoord</label>
+ <input id="password" name="password" type="password" placeholder="wachtwoord">
+ <input type="submit" value="Registreren">
+ </form>
+ <span class="register">Of <a href="/login.php">inloggen</a></span>
+ </div>
+ </div>
+ <?php include 'footer.php' ?>
+</body>
+</html>