if (!isset($_SESSION["cart"])) { $_SESSION["cart"] = []; } $notice = ""; $validProducts = array_column($products, null, "id"); $shopData = loadShopData($storePath); if ($_SERVER["REQUEST_METHOD"] === "POST") { $action = $_POST["action"] ?? ""; if (in_array($action, ["register", "login", "logout", "favorite"], true) && !hash_equals($_SESSION["csrf_token"], $_POST["csrf_token"] ?? "")) { $notice = "Formulář vypršel. Zkus to prosím znovu."; } elseif ($action === "register") { $email = mb_strtolower(trim($_POST["email"] ?? ""), "UTF-8"); $password = $_POST["password"] ?? ""; $confirm = $_POST["password_confirm"] ?? ""; $name = trim($_POST["name"] ?? ""); if (!$name || !filter_var($email, FILTER_VALIDATE_EMAIL)) { $notice = "Vyplň jméno a platný e-mail."; } elseif (strlen($password) < 8) { $notice = "Heslo musí mít alespoň 8 znaků."; } elseif ($password !== $confirm) { $notice = "Zadaná hesla se neshodují."; } elseif (isset($shopData["users"][$email])) { $notice = "Účet s tímto e-mailem už existuje. Přihlas se."; } else { $shopData["users"][$email] = [ "name" => $name, "email" => $email, "password" => password_hash($password, PASSWORD_DEFAULT), "favorites" => [], "created_at" => date(DATE_ATOM) ]; if (saveShopData($storePath, $shopData)) { session_regenerate_id(true); $_SESSION["user_email"] = $email; attachPendingFavorite($storePath, $email); header("Location: ?page=favorites"); exit; } $notice = "Účet se nepodařilo uložit. Zkontroluj prosím oprávnění složky private-data."; } } elseif ($action === "login") { $email = mb_strtolower(trim($_POST["email"] ?? ""), "UTF-8"); $password = $_POST["password"] ?? ""; $user = $shopData["users"][$email] ?? null; if (!$user || !password_verify($password, $user["password"] ?? "")) { $notice = "E-mail nebo heslo není správné."; } else { session_regenerate_id(true); $_SESSION["user_email"] = $email; attachPendingFavorite($storePath, $email); $destination = $_SESSION["after_login_page"] ?? "orders"; unset($_SESSION["after_login_page"]); header("Location: ?page=" . urlencode($destination)); exit; } } elseif ($action === "logout") { unset($_SESSION["user_email"]); session_regenerate_id(true); header("Location: ?"); exit; } elseif ($action === "favorite") { $id = (int)($_POST["id"] ?? 0); if (!isset($validProducts[$id])) { $notice = "Produkt nebyl nalezen."; } elseif (empty($_SESSION["user_email"])) { $_SESSION["pending_favorite"] = $id; $_SESSION["after_login_page"] = "favorites"; header("Location: ?page=account"); exit; } else { $email = $_SESSION["user_email"]; $shopData = loadShopData($storePath); $favorites = array_map("intval", $shopData["users"][$email]["favorites"] ?? []); if (in_array($id, $favorites, true)) { $favorites = array_values(array_diff($favorites, [$id])); } else { $favorites[] = $id; } $shopData["users"][$email]["favorites"] = array_values(array_unique($favorites)); saveShopData($storePath, $shopData); header("Location: ?page=favorites"); exit; } } if ($action === "add") { $id = (int)($_POST["id"] ?? 0); if (isset($validProducts[$id])) { $_SESSION["cart"][$id] = min(99, ($_SESSION["cart"][$id] ?? 0) + 1); $notice = "Produkt byl přidán do košíku."; } } if ($action === "update") { foreach ($_POST["qty"] ?? [] as $id => $qty) { $id = (int)$id; $qty = max(0, min(99, (int)$qty)); if (!isset($validProducts[$id])) continue; if ($qty === 0) { unset($_SESSION["cart"][$id]); } else { $_SESSION["cart"][$id] = $qty; } } $notice = "Košík byl aktualizován."; } if ($action === "remove") { $id = (int)($_POST["id"] ?? 0); unset($_SESSION["cart"][$id]); $notice = "Produkt byl odstraněn z košíku."; } if ($action === "checkout") { if (empty($_SESSION["user_email"])) { $_SESSION["after_login_page"] = "cart"; $notice = "Před odesláním objednávky se přihlas nebo si založ účet."; header("Location: ?page=account"); exit; } $name = trim($_POST["name"] ?? ""); $email = $_SESSION["user_email"]; $phone = trim($_POST["phone"] ?? ""); $street = trim($_POST["street"] ?? ""); $city = trim($_POST["city"] ?? ""); $zip = trim($_POST["zip"] ?? ""); if ( !$name || !filter_var($email, FILTER_VALIDATE_EMAIL) || !$phone || !$street || !$city || !$zip || empty($_SESSION["cart"]) ) { $notice = "Vyplň všechna povinná pole a zkontroluj košík."; } else { $order = "NP-" . date("Ymd") . "-" . random_int(1000, 9999); $items = []; foreach ($_SESSION["cart"] as $productId => $qty) { $productId = (int)$productId; if (!isset($validProducts[$productId])) continue; $item = $validProducts[$productId]; $items[] = [ "id" => $productId, "name" => $item["name"], "qty" => (int)$qty, "price" => (int)$item["price"] ]; } $shopData = loadShopData($storePath); $shopData["orders"][] = [ "number" => $order, "email" => $email, "name" => $name, "phone" => $phone, "street" => $street, "city" => $city, "zip" => $zip, "shipping" => $_POST["shipping"] ?? "Doručení na adresu", "payment" => $_POST["payment"] ?? "Dobírka", "created_at" => date(DATE_ATOM), "total" => $total, "items" => $items ]; if (saveShopData($storePath, $shopData)) { $_SESSION["cart"] = []; header("Location: ?page=success&order=" . urlencode($order)); exit; } $notice = "Objednávku se nepodařilo uložit. Zkus to prosím znovu."; } } } function h($value) { return htmlspecialchars((string)$value, ENT_QUOTES, "UTF-8"); } $page = $_GET["page"] ?? "home"; $q = trim($_GET["q"] ?? ""); $cat = trim($_GET["cat"] ?? ""); $sort = $_GET["sort"] ?? "default"; $currentEmail = $_SESSION["user_email"] ?? ""; $shopData = loadShopData($storePath); $currentUser = $currentEmail ? ($shopData["users"][$currentEmail] ?? null) : null; $favoriteIds = $currentUser ? array_map("intval", $currentUser["favorites"] ?? []) : []; $favoriteProducts = array_values(array_filter($products, fn($product) => in_array((int)$product["id"], $favoriteIds, true))); $myOrders = $currentEmail ? array_values(array_filter($shopData["orders"], fn($order) => ($order["email"] ?? "") === $currentEmail)) : []; usort($myOrders, fn($a, $b) => strcmp($b["created_at"] ?? "", $a["created_at"] ?? "")); $cartCount = array_sum($_SESSION["cart"]); $total = 0; foreach ($products as $p) { $total += $p["price"] * ($_SESSION["cart"][$p["id"]] ?? 0); } $filtered = array_filter($products, function($p) use ($q, $cat) { $matchesCategory = !$cat || $p["cat"] === $cat; $haystack = $p["name"] . " " . $p["cat"] . " " . $p["desc"]; $matchesSearch = !$q || stripos($haystack, $q) !== false; return $matchesCategory && $matchesSearch; }); if ($sort === "low") { usort($filtered, fn($a,$b) => $a["price"] <=> $b["price"]); } elseif ($sort === "high") { usort($filtered, fn($a,$b) => $b["price"] <=> $a["price"]); } elseif ($sort === "name") { usort($filtered, fn($a,$b) => strcmp($a["name"], $b["name"])); } ?>