From b59a613511c182add1624cf58452882298fe0500 Mon Sep 17 00:00:00 2001 From: Dominik <163560221+Dominik-developer@users.noreply.github.com> Date: Tue, 11 Mar 2025 20:42:24 +0100 Subject: [PATCH 01/12] Cookies for users in /public --- CHANGELOG.md | 8 +++ admin/panel.connect.php | 2 +- database_SQL/blog_DB_data.sql | 17 +---- database_SQL/blog_DB_structure.sql | 97 +++++++++++---------------- public/{ => CSS}/main.css | 0 public/{ => CSS}/single.css | 0 public/JS/popout.js | 0 public/error.html | 9 ++- public/error_404.php | 16 +++-- public/{ => handlers}/connect.php | 2 +- public/handlers/cookies.php | 19 ++++++ public/{ => handlers}/functions.php | 24 +++---- public/handlers/index.php | 4 ++ public/{ => handlers}/service.alg.php | 4 +- public/{ => handlers}/single.alg.php | 4 +- public/handlers/visits.alg.php | 47 +++++++++++++ public/index.php | 2 +- public/main.php | 12 +++- public/service.html | 7 +- public/single.php | 18 +++-- 20 files changed, 184 insertions(+), 108 deletions(-) rename public/{ => CSS}/main.css (100%) rename public/{ => CSS}/single.css (100%) create mode 100644 public/JS/popout.js rename public/{ => handlers}/connect.php (76%) create mode 100644 public/handlers/cookies.php rename public/{ => handlers}/functions.php (82%) create mode 100644 public/handlers/index.php rename public/{ => handlers}/service.alg.php (93%) rename public/{ => handlers}/single.alg.php (94%) create mode 100644 public/handlers/visits.alg.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 21f10d9..7b08597 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,10 +10,18 @@ The project is ready for use but is still evolving. Work is ongoing to fix bugs ### Added + +`/public` +1 - cookies for users, db collects data for analytics +2 - tables for cookies added +3 - JS folder ..... ### Changed + +`/public` +1 - structure of /public fixes ..... ### Fixed diff --git a/admin/panel.connect.php b/admin/panel.connect.php index 8171e0b..c2727bb 100644 --- a/admin/panel.connect.php +++ b/admin/panel.connect.php @@ -2,7 +2,7 @@ $host = "localhost"; $db_user = "root"; - $db_password =""; + $db_password ="root"; $db_name = "blog"; diff --git a/database_SQL/blog_DB_data.sql b/database_SQL/blog_DB_data.sql index 55147d4..6d51d41 100644 --- a/database_SQL/blog_DB_data.sql +++ b/database_SQL/blog_DB_data.sql @@ -1,22 +1,11 @@ -SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"; START TRANSACTION; -SET time_zone = "+00:00"; - -USE `blog`; - --- --- Inserting data into table `admin` --- INSERT INTO `admin` (`id`, `login`, `password`) VALUES -(1, 'admin', '$2y$10$L9fQlnPTTuYkNhLnXh68..F8R.bJdLaAJBJjXU8RjhiHUHFVVJyCe'); +(1, 'admin', '$2y$10$w9GkWVGXxSTjw4A9QyjasuqyeJyUPp2JlWYnFGFEen7e2..YUXxNC'); --- --- Inserting data into table `service` --- +-- -------------------------------------------------------- INSERT INTO `service` (`id`, `service_status`) VALUES -(1, 1); - +(1, 0); COMMIT; diff --git a/database_SQL/blog_DB_structure.sql b/database_SQL/blog_DB_structure.sql index 20d0c42..77c857b 100644 --- a/database_SQL/blog_DB_structure.sql +++ b/database_SQL/blog_DB_structure.sql @@ -1,79 +1,64 @@ +-- Databse 'blog': structure SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"; START TRANSACTION; SET time_zone = "+00:00"; -CREATE DATABASE IF NOT EXISTS `blog`; -USE `blog`; - -/*!40101 SET NAMES utf8mb4 */; - --- --- Table structure for table `admin` --- +-- -------------------------------------------------------- CREATE TABLE `admin` ( - `id` int(11) NOT NULL, - `login` varchar(25) NOT NULL, - `password` varchar(25) NOT NULL + `id` int NOT NULL AUTO_INCREMENT, + `login` varchar(25) COLLATE utf8mb4_general_ci NOT NULL, + `password` varchar(80) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL, + PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci; --- --- Table structure for table `articles` --- +-- -------------------------------------------------------- CREATE TABLE `articles` ( - `ID` int(11) NOT NULL, - `title` varchar(255) NOT NULL, - `text` text NOT NULL, - `photo_path` varchar(255) NOT NULL, - `date_of_publish` timestamp NOT NULL DEFAULT current_timestamp() + `ID` int NOT NULL AUTO_INCREMENT, + `title` varchar(255) COLLATE utf8mb4_general_ci NOT NULL, + `text` text COLLATE utf8mb4_general_ci NOT NULL, + `photo_path` varchar(255) COLLATE utf8mb4_general_ci NOT NULL, + `date_of_publish` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, + PRIMARY KEY (`ID`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci; --- --- Table structure for table `service` --- +-- -------------------------------------------------------- + +CREATE TABLE `page_views_daily` ( + `id` int NOT NULL AUTO_INCREMENT, + `page` varchar(191) NOT NULL, + `visit_date` date NOT NULL DEFAULT (curdate()), + `visit_count` int NOT NULL DEFAULT '1', + PRIMARY KEY (`id`), + UNIQUE KEY `unique_page_date` (`page`,`visit_date`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; + +-- -------------------------------------------------------- CREATE TABLE `service` ( - `id` int(11) NOT NULL, - `service_status` tinyint(1) NOT NULL + `id` int NOT NULL AUTO_INCREMENT, + `service_status` tinyint(1) NOT NULL, + PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci; --- --- Table structure for table `settings` --- +-- -------------------------------------------------------- CREATE TABLE `settings` ( - `id` int(11) NOT NULL, - `is_active` tinyint(1) NOT NULL + `id` int NOT NULL AUTO_INCREMENT, + `is_active` tinyint(1) NOT NULL, + PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci; --- --- Indexes for tables --- - -ALTER TABLE `admin` - ADD PRIMARY KEY (`id`); - -ALTER TABLE `articles` - ADD PRIMARY KEY (`ID`); - -ALTER TABLE `service` - ADD PRIMARY KEY (`id`); - -ALTER TABLE `settings` - ADD PRIMARY KEY (`id`); - --- --- AUTO_INCREMENT for tables --- - -ALTER TABLE `admin` - MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2; - -ALTER TABLE `articles` - MODIFY `ID` int(11) NOT NULL AUTO_INCREMENT; +-- -------------------------------------------------------- -ALTER TABLE `settings` - MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=1; +CREATE TABLE `visitors` ( + `id` int NOT NULL AUTO_INCREMENT, + `cookie_id` varchar(64) DEFAULT NULL, + `visit_count` int DEFAULT '1', + `first_visit` datetime DEFAULT CURRENT_TIMESTAMP, + PRIMARY KEY (`id`), + UNIQUE KEY `cookie_id` (`cookie_id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; COMMIT; diff --git a/public/main.css b/public/CSS/main.css similarity index 100% rename from public/main.css rename to public/CSS/main.css diff --git a/public/single.css b/public/CSS/single.css similarity index 100% rename from public/single.css rename to public/CSS/single.css diff --git a/public/JS/popout.js b/public/JS/popout.js new file mode 100644 index 0000000..e69de29 diff --git a/public/error.html b/public/error.html index 81ed519..06c2f4c 100644 --- a/public/error.html +++ b/public/error.html @@ -6,7 +6,7 @@ Error page - + - + + + + @@ -34,7 +37,7 @@

Error page

Something went wrong.

Try to search what you have been looking for on main page:

- Link: Blog main page +

Link: Blog main page

diff --git a/public/error_404.php b/public/error_404.php index d0c8e59..567a3d2 100644 --- a/public/error_404.php +++ b/public/error_404.php @@ -3,12 +3,16 @@ session_start(); -include_once 'functions.php'; -require_once 'service.alg.php'; -include 'connect.php'; +include_once './handlers/functions.php'; +require_once './handlers/service.alg.php'; +require_once './handlers/cookies.php'; service(); +$page = 'error_404.php'; +cookie($page); + + ?> @@ -16,8 +20,10 @@ 404 error - - + + + + diff --git a/public/connect.php b/public/handlers/connect.php similarity index 76% rename from public/connect.php rename to public/handlers/connect.php index aaa1736..655b0af 100644 --- a/public/connect.php +++ b/public/handlers/connect.php @@ -3,7 +3,7 @@ $host = "localhost"; $db_user = "root"; - $db_password =""; + $db_password ="root"; $db_name = "blog"; \ No newline at end of file diff --git a/public/handlers/cookies.php b/public/handlers/cookies.php new file mode 100644 index 0000000..1da2cfc --- /dev/null +++ b/public/handlers/cookies.php @@ -0,0 +1,19 @@ + Blog '; + echo '

Blog

'; } // main @@ -11,14 +11,12 @@ function all_articles(): void{ require 'connect.php'; - $conn = @new mysqli($host, $db_user, $db_password, $db_name); - error_reporting(E_ALL); - ini_set('display_errors', 1); + //error_reporting(E_ALL); + //ini_set('display_errors', 1); if ($conn->connect_errno) { - //echo "Error: " . $conn->connect_error; - echo 'error'; + echo 'Error'; return; } else { @@ -56,7 +54,7 @@ function all_articles(): void{ } } else { - echo "0 results"; + echo "No results found."; } $conn->close(); @@ -65,13 +63,15 @@ function all_articles(): void{ //footer -function foot(): string{ +function foot(): void{ + + $date = date("Y"); - return ' + echo "
- © 2024 - Dominik-developer + © 2024 - {$date} Dominik-developer

Contact: www.blog@example.com -
'; + "; } diff --git a/public/handlers/index.php b/public/handlers/index.php new file mode 100644 index 0000000..b0159fd --- /dev/null +++ b/public/handlers/index.php @@ -0,0 +1,4 @@ +connect_error; echo 'Error'; - header('Location: error.html'); + header('Location: ./error.html'); } else { @@ -44,7 +44,7 @@ function service(): void //unset($_SESSION['status']); echo 'last error '; - echo('Location: error.html'); + echo('Location: ./error.html'); } } diff --git a/public/single.alg.php b/public/handlers/single.alg.php similarity index 94% rename from public/single.alg.php rename to public/handlers/single.alg.php index f71b7c7..1c5888a 100644 --- a/public/single.alg.php +++ b/public/handlers/single.alg.php @@ -9,7 +9,7 @@ function articles($restored_title): void { if ($conn->connect_errno) { header("HTTP/1.1 500 Internal Server Error"); - header("Location: error_404.php"); + header("Location: ./error_404.php"); exit(); } @@ -45,7 +45,7 @@ function articles($restored_title): void { '; } else { header("HTTP/1.1 404 Not Found"); - header("Location: error_404.php"); + header("Location: ./error_404.php"); exit(); } diff --git a/public/handlers/visits.alg.php b/public/handlers/visits.alg.php new file mode 100644 index 0000000..72a0900 --- /dev/null +++ b/public/handlers/visits.alg.php @@ -0,0 +1,47 @@ +connect_errno) { + die("Connection failed: " . $conn->connect_error); + } + + // Sprawdzamy, czy w tabeli visitors istnieje już taki rekord (unikalny cookie_id) + $query = "SELECT * FROM visitors WHERE cookie_id = ?"; + $stmt = $conn->prepare($query); + $stmt->bind_param('s', $cookie_id); // Łączymy parametr cookie_id + $stmt->execute(); + $result = $stmt->get_result(); + + // Jeśli użytkownik nie istnieje, dodajemy nowy rekord + if ($result->num_rows === 0) { + $query = "INSERT INTO visitors (cookie_id, visit_count, first_visit) + VALUES (?, 1, CURRENT_TIMESTAMP)"; + $stmt = $conn->prepare($query); + $stmt->bind_param('s', $cookie_id); + $stmt->execute(); + } else { + // Jeśli już istnieje, aktualizujemy licznik odwiedzin + $query = "UPDATE visitors SET visit_count = visit_count + 1 + WHERE cookie_id = ?"; + $stmt = $conn->prepare($query); + $stmt->bind_param('s', $cookie_id); + $stmt->execute(); + } + + // Teraz aktualizujemy liczbę odwiedzin na stronie + $query = "INSERT INTO page_views_daily (page, visit_date, visit_count) + VALUES (?, CURRENT_DATE, 1) + ON DUPLICATE KEY UPDATE visit_count = visit_count + 1"; + $stmt = $conn->prepare($query); + $stmt->bind_param('s', $page); // Łączymy parametr strony + $stmt->execute(); + + $stmt->close(); +} +?> + diff --git a/public/index.php b/public/index.php index be04767..668d195 100644 --- a/public/index.php +++ b/public/index.php @@ -2,7 +2,7 @@ session_start(); -require_once 'service.alg.php'; +require_once './handlers/service.alg.php'; service(); diff --git a/public/main.php b/public/main.php index 22c7a54..e1eff6b 100644 --- a/public/main.php +++ b/public/main.php @@ -3,11 +3,15 @@ session_start(); -include_once 'functions.php'; -require_once 'service.alg.php'; +include './handlers/functions.php'; +require_once './handlers/service.alg.php'; +require_once './handlers/cookies.php'; service(); +$page = 'main.php'; +cookie($page); + ?> @@ -15,7 +19,9 @@ Blog Home Page - + + + diff --git a/public/service.html b/public/service.html index 502fbd8..eb593e5 100644 --- a/public/service.html +++ b/public/service.html @@ -11,7 +11,7 @@ - + - + + + + diff --git a/public/single.php b/public/single.php index 267eed8..806ee0d 100644 --- a/public/single.php +++ b/public/single.php @@ -3,10 +3,10 @@ session_start(); -include_once 'functions.php'; -require_once 'service.alg.php'; -include_once 'single.alg.php'; -include 'connect.php'; +include './handlers/functions.php'; +require_once './handlers/service.alg.php'; +require_once './handlers/cookies.php'; +include_once './handlers/single.alg.php'; service(); @@ -16,6 +16,10 @@ exit(); } +//$page = 'single.php?title='.$_GET['title']; +$page = 'single.php'; +cookie($page); + $restored_title = str_replace('_', ' ', filter_var($_GET['title'], FILTER_SANITIZE_SPECIAL_CHARS)); //optionally use - insted of _ ?> @@ -25,8 +29,10 @@ <?php echo htmlspecialchars($restored_title); ?> - - + + + + From 1e346d7779b63e411495e619e57dfc90d2161201 Mon Sep 17 00:00:00 2001 From: Dominik <163560221+Dominik-developer@users.noreply.github.com> Date: Tue, 11 Mar 2025 22:20:09 +0100 Subject: [PATCH 02/12] Cookies done --- CHANGELOG.md | 1 + HOW_TO_RUN.md | 6 ++--- public/CSS/popout.css | 41 +++++++++++++++++++++++++++++++++++ public/JS/popout.js | 39 +++++++++++++++++++++++++++++++++ public/error_404.php | 4 ++++ public/handlers/cookies.php | 20 +++++++++-------- public/handlers/functions.php | 11 ++++++++++ public/main.php | 6 +++++ public/single.php | 4 ++++ 9 files changed, 120 insertions(+), 12 deletions(-) create mode 100644 public/CSS/popout.css diff --git a/CHANGELOG.md b/CHANGELOG.md index 7b08597..d7d22a8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ The project is ready for use but is still evolving. Work is ongoing to fix bugs 1 - cookies for users, db collects data for analytics 2 - tables for cookies added 3 - JS folder +4 - popout for cookies ..... ### Changed diff --git a/HOW_TO_RUN.md b/HOW_TO_RUN.md index d7890b3..6575289 100644 --- a/HOW_TO_RUN.md +++ b/HOW_TO_RUN.md @@ -8,9 +8,9 @@ CorelyPHP is a ready-to-deploy blogging platform designed for efficient web deve Before you begin, ensure you have the following installed: -- PHP (>= 8.0) -- MySQL (or another compatible database) -- XAMPP +- PHP (>= 8.0) +- MySQL (or another compatible database) +- XAMPP / MAMP - Git ## Installation diff --git a/public/CSS/popout.css b/public/CSS/popout.css new file mode 100644 index 0000000..1b8da71 --- /dev/null +++ b/public/CSS/popout.css @@ -0,0 +1,41 @@ +.cookie-popup { + position: fixed; + bottom: 20px; + left: 50%; + transform: translateX(-50%); + background-color: #333; + color: #fff; + padding: 15px 20px; + border-radius: 8px; + box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2); + display: none; +} + +.cookie-popup a { + color: #fff; + text-decoration: underline; +} + +.cookie-popup button { + background-color: #fff; + color: #333; + border: none; + padding: 8px 12px; + margin-left: 10px; + cursor: pointer; + border-radius: 5px; + font-weight: bold; +} + +.cookie-popup button:hover { + background-color: #ddd; +} + +.cookie-popup button#reject-cookies { + background-color: #ff4d4d; /* Czerwony dla opcji odrzucenia */ + color: white; +} + +.cookie-popup button#reject-cookies:hover { + background-color: #cc0000 +} \ No newline at end of file diff --git a/public/JS/popout.js b/public/JS/popout.js index e69de29..0119f42 100644 --- a/public/JS/popout.js +++ b/public/JS/popout.js @@ -0,0 +1,39 @@ + +document.addEventListener("DOMContentLoaded", function () { + const popup = document.getElementById("cookie-popup"); + const acceptButton = document.getElementById("accept-cookies"); + const rejectButton = document.getElementById("reject-cookies"); + + const cookiesAccepted = document.cookie.includes("cookiesAccepted=true"); + const cookiesRejected = document.cookie.includes("cookiesAccepted=false"); + + if (!cookiesAccepted && !cookiesRejected) { + popup.style.display = "block"; + } + + function setCookie(name, value, days) { + let expires = ""; + if (days) { + const date = new Date(); + date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000); + expires = "; expires=" + date.toUTCString(); + } + document.cookie = name + "=" + value + expires + "; path=/server/CorelyPHP-1.1.0/public/"; + } + + // Obsługa zgody na ciasteczka + acceptButton.addEventListener("click", function () { + setCookie("cookiesAccepted", "true", 365); + popup.style.display = "none"; + location.reload(); + }); + + // Obsługa odrzucenia ciasteczek + rejectButton.addEventListener("click", function () { + setCookie("cookiesAccepted", "false", 365); + popup.style.display = "none"; + location.reload(); + }); +}); + + diff --git a/public/error_404.php b/public/error_404.php index 567a3d2..3c4cee2 100644 --- a/public/error_404.php +++ b/public/error_404.php @@ -59,5 +59,9 @@ ?> + + diff --git a/public/handlers/cookies.php b/public/handlers/cookies.php index 1da2cfc..60214ed 100644 --- a/public/handlers/cookies.php +++ b/public/handlers/cookies.php @@ -3,17 +3,19 @@ require 'visits.alg.php'; function cookie($page): void { + if (isset($_COOKIE['cookiesAccepted']) && $_COOKIE['cookiesAccepted'] === 'true') { + setcookie("cookiesAccepted", "true", time() + (3600 * 24 * 365), "/server/CorelyPHP-1.1.0/public/"); - if (!isset($_COOKIE['visitor_id'])) { + if (!isset($_COOKIE['visitor_id'])) { + $cookie_id = bin2hex(random_bytes(16)); + setcookie('visitor_id', $cookie_id, time() + (3600 * 24 * 365), "/server/CorelyPHP-1.1.0/public/"); + } else { + $cookie_id = $_COOKIE['visitor_id']; + } - $cookie_id = bin2hex(random_bytes(16)); - setcookie('visitor_id', $cookie_id, time() + (3600 * 24 * 365), "/CorelyPHP-1.1.0/public/"); // path may need to be changed + updateVisitCount($cookie_id, $page); } else { - $cookie_id = $_COOKIE['visitor_id']; + setcookie('visitor_id', "", time() - 3600, "/server/CorelyPHP-1.1.0/public/"); } - - updateVisitCount($cookie_id, $page); - } - - //$page = basename($_SERVER['PHP_SELF']); \ No newline at end of file + \ No newline at end of file diff --git a/public/handlers/functions.php b/public/handlers/functions.php index 4cc9297..de34bf8 100644 --- a/public/handlers/functions.php +++ b/public/handlers/functions.php @@ -75,3 +75,14 @@ function foot(): void{ "; } + +function cookie_popout(): void { + + echo ' + diff --git a/public/single.php b/public/single.php index 806ee0d..e19bb97 100644 --- a/public/single.php +++ b/public/single.php @@ -64,5 +64,9 @@ ?> + + From b70c8a6a4a7ccc75161f738c27660a5a97cf1ec8 Mon Sep 17 00:00:00 2001 From: Dominik <163560221+Dominik-developer@users.noreply.github.com> Date: Tue, 11 Mar 2025 21:31:04 +0000 Subject: [PATCH 03/12] Fix --- public/JS/popout.js | 2 +- public/handlers/cookies.php | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/public/JS/popout.js b/public/JS/popout.js index 0119f42..4e03a48 100644 --- a/public/JS/popout.js +++ b/public/JS/popout.js @@ -18,7 +18,7 @@ document.addEventListener("DOMContentLoaded", function () { date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000); expires = "; expires=" + date.toUTCString(); } - document.cookie = name + "=" + value + expires + "; path=/server/CorelyPHP-1.1.0/public/"; + document.cookie = name + "=" + value + expires + "; path=/public/"; //path may need to be changed for client } // Obsługa zgody na ciasteczka diff --git a/public/handlers/cookies.php b/public/handlers/cookies.php index 60214ed..0a0f284 100644 --- a/public/handlers/cookies.php +++ b/public/handlers/cookies.php @@ -4,18 +4,18 @@ function cookie($page): void { if (isset($_COOKIE['cookiesAccepted']) && $_COOKIE['cookiesAccepted'] === 'true') { - setcookie("cookiesAccepted", "true", time() + (3600 * 24 * 365), "/server/CorelyPHP-1.1.0/public/"); + setcookie("cookiesAccepted", "true", time() + (3600 * 24 * 365), "/public/"); //path may need to be changed for client if (!isset($_COOKIE['visitor_id'])) { $cookie_id = bin2hex(random_bytes(16)); - setcookie('visitor_id', $cookie_id, time() + (3600 * 24 * 365), "/server/CorelyPHP-1.1.0/public/"); + setcookie('visitor_id', $cookie_id, time() + (3600 * 24 * 365), "/public/"); } else { $cookie_id = $_COOKIE['visitor_id']; } updateVisitCount($cookie_id, $page); } else { - setcookie('visitor_id', "", time() - 3600, "/server/CorelyPHP-1.1.0/public/"); + setcookie('visitor_id', "", time() - 3600, "/public/"); } } \ No newline at end of file From 23f12b43e823912730f3b228f2103c8e1e3c84bb Mon Sep 17 00:00:00 2001 From: Dominik <163560221+Dominik-developer@users.noreply.github.com> Date: Wed, 12 Mar 2025 16:59:33 +0100 Subject: [PATCH 04/12] Added themes on user side --- public/JS/popout.js | 2 +- public/{error.html => error.php} | 17 +++++------- public/error_404.php | 8 ++++-- public/handlers/cookies.php | 6 ++--- public/main.php | 8 +++--- public/{service.html => service.php} | 16 +++++------ public/single.php | 11 ++++---- themes/handlers/connect.php | 9 +++++++ themes/handlers/index.php | 27 +++++++++++++++++++ themes/index.php | 22 +++++++++++++++ {public => themes/purple-show}/CSS/main.css | 0 {public => themes/purple-show}/CSS/single.css | 0 12 files changed, 91 insertions(+), 35 deletions(-) rename public/{error.html => error.php} (81%) rename public/{service.html => service.php} (83%) create mode 100644 themes/handlers/connect.php create mode 100644 themes/handlers/index.php create mode 100644 themes/index.php rename {public => themes/purple-show}/CSS/main.css (100%) rename {public => themes/purple-show}/CSS/single.css (100%) diff --git a/public/JS/popout.js b/public/JS/popout.js index 4e03a48..0119f42 100644 --- a/public/JS/popout.js +++ b/public/JS/popout.js @@ -18,7 +18,7 @@ document.addEventListener("DOMContentLoaded", function () { date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000); expires = "; expires=" + date.toUTCString(); } - document.cookie = name + "=" + value + expires + "; path=/public/"; //path may need to be changed for client + document.cookie = name + "=" + value + expires + "; path=/server/CorelyPHP-1.1.0/public/"; } // Obsługa zgody na ciasteczka diff --git a/public/error.html b/public/error.php similarity index 81% rename from public/error.html rename to public/error.php index 06c2f4c..9419488 100644 --- a/public/error.html +++ b/public/error.php @@ -5,10 +5,13 @@ Error page - - + - + + + - - - - - - diff --git a/public/error_404.php b/public/error_404.php index 3c4cee2..66bb957 100644 --- a/public/error_404.php +++ b/public/error_404.php @@ -20,8 +20,12 @@ 404 error - - + + + + diff --git a/public/handlers/cookies.php b/public/handlers/cookies.php index 0a0f284..60214ed 100644 --- a/public/handlers/cookies.php +++ b/public/handlers/cookies.php @@ -4,18 +4,18 @@ function cookie($page): void { if (isset($_COOKIE['cookiesAccepted']) && $_COOKIE['cookiesAccepted'] === 'true') { - setcookie("cookiesAccepted", "true", time() + (3600 * 24 * 365), "/public/"); //path may need to be changed for client + setcookie("cookiesAccepted", "true", time() + (3600 * 24 * 365), "/server/CorelyPHP-1.1.0/public/"); if (!isset($_COOKIE['visitor_id'])) { $cookie_id = bin2hex(random_bytes(16)); - setcookie('visitor_id', $cookie_id, time() + (3600 * 24 * 365), "/public/"); + setcookie('visitor_id', $cookie_id, time() + (3600 * 24 * 365), "/server/CorelyPHP-1.1.0/public/"); } else { $cookie_id = $_COOKIE['visitor_id']; } updateVisitCount($cookie_id, $page); } else { - setcookie('visitor_id', "", time() - 3600, "/public/"); + setcookie('visitor_id', "", time() - 3600, "/server/CorelyPHP-1.1.0/public/"); } } \ No newline at end of file diff --git a/public/main.php b/public/main.php index 96d344b..0dcaace 100644 --- a/public/main.php +++ b/public/main.php @@ -19,11 +19,13 @@ Blog Home Page - - + + + -
diff --git a/public/service.html b/public/service.php similarity index 83% rename from public/service.html rename to public/service.php index eb593e5..8e506e5 100644 --- a/public/service.html +++ b/public/service.php @@ -10,10 +10,13 @@ - - + - + + + - - - - - - diff --git a/public/single.php b/public/single.php index e19bb97..7920e18 100644 --- a/public/single.php +++ b/public/single.php @@ -16,8 +16,7 @@ exit(); } -//$page = 'single.php?title='.$_GET['title']; -$page = 'single.php'; +$page = 'single.php?title='.$_GET['title']; cookie($page); $restored_title = str_replace('_', ' ', filter_var($_GET['title'], FILTER_SANITIZE_SPECIAL_CHARS)); //optionally use - insted of _ @@ -29,11 +28,13 @@ <?php echo htmlspecialchars($restored_title); ?> - - + + + -
diff --git a/themes/handlers/connect.php b/themes/handlers/connect.php new file mode 100644 index 0000000..655b0af --- /dev/null +++ b/themes/handlers/connect.php @@ -0,0 +1,9 @@ + +connect_errno) { + die("Connection failed: " . $conn->connect_error); + } + + $query = "SELECT * FROM settings WHERE name = 'active_theme' "; + $stmt = $conn->prepare($query); + $stmt->execute(); + $result = $stmt->get_result(); + + if ($result->num_rows === 1) { + $row = $result->fetch_assoc(); + $result = $row['value']; + + return $result; + } else { + return 'default'; + } +} \ No newline at end of file diff --git a/themes/index.php b/themes/index.php new file mode 100644 index 0000000..b959c27 --- /dev/null +++ b/themes/index.php @@ -0,0 +1,22 @@ + + ' . PHP_EOL;; + echo ' ' . PHP_EOL; + echo ' ' . PHP_EOL; + + //echo ' ' . PHP_EOL; + //echo " ". PHP_EOL; + + //echo ' ' . PHP_EOL;; + //echo ' ' . PHP_EOL; + + //echo ' ' . PHP_EOL; + //echo ' ' . PHP_EOL; + diff --git a/public/CSS/main.css b/themes/purple-show/CSS/main.css similarity index 100% rename from public/CSS/main.css rename to themes/purple-show/CSS/main.css diff --git a/public/CSS/single.css b/themes/purple-show/CSS/single.css similarity index 100% rename from public/CSS/single.css rename to themes/purple-show/CSS/single.css From a35046f75baa0a0eebda1a36a545971b25ed743f Mon Sep 17 00:00:00 2001 From: Dominik <163560221+Dominik-developer@users.noreply.github.com> Date: Wed, 12 Mar 2025 17:12:26 +0100 Subject: [PATCH 05/12] Fix bug and paths --- public/handlers/functions.php | 2 +- public/handlers/service.alg.php | 10 +++++----- public/handlers/visits.alg.php | 3 ++- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/public/handlers/functions.php b/public/handlers/functions.php index de34bf8..27b943b 100644 --- a/public/handlers/functions.php +++ b/public/handlers/functions.php @@ -71,7 +71,7 @@ function foot(): void{
© 2024 - {$date} Dominik-developer

- Contact: www.blog@example.com + Contact: www.blog@example.com
"; } diff --git a/public/handlers/service.alg.php b/public/handlers/service.alg.php index 6c6c634..fbfe315 100644 --- a/public/handlers/service.alg.php +++ b/public/handlers/service.alg.php @@ -10,8 +10,8 @@ function service(): void if ($conn->connect_errno!=0) { //echo "Error: ".$conn->connect_error; - echo 'Error'; - header('Location: ./error.html'); + echo 'Error.'; + header('Location: ./error.php'); } else { @@ -31,7 +31,7 @@ function service(): void if(!$_SESSION['service_status'] == 0) { //unset($_SESSION['status']); - header('Location: service.html'); + header('Location: service.php'); } else { @@ -43,8 +43,8 @@ function service(): void //$_SESSION['bigError']; //unset($_SESSION['status']); - echo 'last error '; - echo('Location: ./error.html'); + echo 'Last error.'; + header('Location: ./error.php'); } } diff --git a/public/handlers/visits.alg.php b/public/handlers/visits.alg.php index 72a0900..abe3055 100644 --- a/public/handlers/visits.alg.php +++ b/public/handlers/visits.alg.php @@ -7,7 +7,8 @@ function updateVisitCount($cookie_id, $page): void { $conn = new mysqli($host, $db_user, $db_password, $db_name); if ($conn->connect_errno) { - die("Connection failed: " . $conn->connect_error); + echo("Connection failed: " . $conn->connect_error); + header('Location: error.php'); } // Sprawdzamy, czy w tabeli visitors istnieje już taki rekord (unikalny cookie_id) From 7dacbfe347e1088c99ed6fb5a84ef7f09c0bba51 Mon Sep 17 00:00:00 2001 From: Dominik <163560221+Dominik-developer@users.noreply.github.com> Date: Wed, 12 Mar 2025 17:23:01 +0100 Subject: [PATCH 06/12] added window for themes and analytics --- admin/algo/theme.alg.php | 4 ++++ admin/panel.php | 21 ++++++++++++++---- admin/window_functions.php | 44 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 4 deletions(-) create mode 100644 admin/algo/theme.alg.php diff --git a/admin/algo/theme.alg.php b/admin/algo/theme.alg.php new file mode 100644 index 0000000..b1ffe50 --- /dev/null +++ b/admin/algo/theme.alg.php @@ -0,0 +1,4 @@ + +
+

'; + + include './v_auth/version.php'; + + echo '

'; + if (version_compare(PHP_VERSION, PHP_VERSION_REQ, '<')) { + echo('The required PHP version is ' . PHP_VERSION_REQ . ' or higher. The installed version is: ' . PHP_VERSION . '

'); + echo('Version: ' . PHP_VERSION_ADVICE); + } + echo '

'; + ?> +
diff --git a/admin/v_auth/version.php b/admin/v_auth/version.php new file mode 100644 index 0000000..b16028a --- /dev/null +++ b/admin/v_auth/version.php @@ -0,0 +1,11 @@ + Date: Fri, 14 Mar 2025 00:01:08 +0100 Subject: [PATCH 08/12] Fix version file, update login for admin, build adding themes --- admin/panel.login.php | 4 ++-- admin/v_auth/version.php | 6 +++--- themes/index.php | 37 +++++++++++++++++++++++++++---------- 3 files changed, 32 insertions(+), 15 deletions(-) diff --git a/admin/panel.login.php b/admin/panel.login.php index 346d2ac..0a04506 100644 --- a/admin/panel.login.php +++ b/admin/panel.login.php @@ -63,9 +63,9 @@

'; - include './v_auth/version.php'; + include_once "./v_auth/version.php"; - echo '

'; + echo '

'; if (version_compare(PHP_VERSION, PHP_VERSION_REQ, '<')) { echo('The required PHP version is ' . PHP_VERSION_REQ . ' or higher. The installed version is: ' . PHP_VERSION . '

'); echo('Version: ' . PHP_VERSION_ADVICE); diff --git a/admin/v_auth/version.php b/admin/v_auth/version.php index b16028a..6b3f138 100644 --- a/admin/v_auth/version.php +++ b/admin/v_auth/version.php @@ -1,11 +1,11 @@ ' . PHP_EOL;; - echo ' ' . PHP_EOL; - echo ' ' . PHP_EOL; +function load_assets(string $type, string $themePath): void { + + $dir = $themePath.'/'.$type; + + if (!is_dir($dir)) { + echo ''; + } + + $files = glob($dir . '/*.'.$type); + if (!$files) { + echo ''; + } - //echo ' ' . PHP_EOL; - //echo " ". PHP_EOL; + foreach ($files as $file) { + $url = str_replace($_SERVER['DOCUMENT_ROOT'], '', realpath($file)); + $url = str_replace('\\', '/', $url); // fix for Windows - //echo ' ' . PHP_EOL;; - //echo ' ' . PHP_EOL; + if ($type === 'css') { + echo "\n"; + } elseif ($type === 'js') { + echo "\n"; + } else { + echo "\n"; + } + } + echo PHP_EOL; +} - //echo ' ' . PHP_EOL; - //echo ' ' . PHP_EOL; +load_assets('css', '../themes/'.$theme); +load_assets('js', '../themes/'.$theme); From 5fe6ef3c5f7cf17b1d56cc2e64bade0bce674638 Mon Sep 17 00:00:00 2001 From: Dominik <163560221+Dominik-developer@users.noreply.github.com> Date: Fri, 14 Mar 2025 16:49:50 +0100 Subject: [PATCH 09/12] Update, themes algo added, updated blog db --- admin/algo/dashboard.alg.php | 4 +- admin/algo/new_theme.alg.php | 6 +++ admin/algo/password.alg.php | 30 ++++++----- admin/algo/service_status.alg.php | 6 ++- admin/algo/theme.alg.php | 62 ++++++++++++++++++++- admin/panel.login.php | 86 ------------------------------ admin/panel.php | 23 ++++---- admin/window_functions.php | 58 ++++++++++++-------- database_SQL/blog_DB_data.sql | 10 ++-- database_SQL/blog_DB_structure.sql | 38 +++++++------ themes/default/CSS/style.css | 0 themes/default/CSS/style2.css | 0 themes/default/JS/script.js | 0 themes/handlers/index.php | 20 ++++++- themes/purple-show/index.js | 0 15 files changed, 185 insertions(+), 158 deletions(-) create mode 100644 admin/algo/new_theme.alg.php delete mode 100644 admin/panel.login.php create mode 100644 themes/default/CSS/style.css create mode 100644 themes/default/CSS/style2.css create mode 100644 themes/default/JS/script.js create mode 100644 themes/purple-show/index.js diff --git a/admin/algo/dashboard.alg.php b/admin/algo/dashboard.alg.php index 07af2ca..b098086 100644 --- a/admin/algo/dashboard.alg.php +++ b/admin/algo/dashboard.alg.php @@ -7,9 +7,9 @@ exit(); } -function dashboard_data(): string { +function dashboard_data(): void { - return' + echo'

Welcome on admin panel for your blog!
diff --git a/admin/algo/new_theme.alg.php b/admin/algo/new_theme.alg.php new file mode 100644 index 0000000..abb55d4 --- /dev/null +++ b/admin/algo/new_theme.alg.php @@ -0,0 +1,6 @@ +connect_errno!=0) { - $_SESSION['message'] = 'connection to db fail'; + $_SESSION['message'] = 'Connection to db fail.'; #echo 'Error: '.$conn->connect_error; - header('Location: ../panel.php?window=settings'); + header('Location: ../panel.php?window=password'); exit(); }else{ @@ -48,44 +48,46 @@ $stmt = $conn->prepare($sql); if (!$stmt) { - die("Error during prepearing statement: " . $conn->error); + $_SESSION['message'] = 'Error during prepearing statement:' . $conn->error . '.'; + header('Location: ../panel.php?window=password'); } $stmt->bind_param("si", $password_hash, $ID); if ($stmt->execute()) { $_SESSION['message'] = 'Password changed successfully.'; - header('Location: ../panel.php?window=settings'); - echo $_SERVER['message']; + header('Location: ../panel.php?window=password'); } else { $_SESSION['message'] = 'Error: something went wrong during updating password.
'; //$stmt->error; - echo $_SERVER['message']; + header('Location: ../panel.php?window=password'); } $stmt->close(); $conn->close(); } else { - $_SESSION['message'] = 'Old password is wrong'; - header('Location: ../panel.php?window=settings'); + $_SESSION['message'] = 'Old password is wrong.'; + header('Location: ../panel.php?window=password'); } } else { - $_SESSION['message'] = 'more rows found than needed'; - header('Location: ../panel.php?window=settings'); + $_SESSION['message'] = 'More rows found than needed.'; + header('Location: ../panel.php?window=password'); } } $conn->close(); + $_SESSION['message'] = 'Error fetching data.'; + header('Location: ../panel.php?window=password'); exit(); } } else { - $_SESSION['message'] = 'new password different than one written again '; - header('Location: ../panel.php?window=settings'); + $_SESSION['message'] = 'New password different than one written again.'; + header('Location: ../panel.php?window=password'); exit(); } } else { - $_SESSION['message'] = 'POST table doesnt have all data'; - header('Location: panel.php?window=settings'); + $_SESSION['message'] = 'POST table doesnt have all data.'; + header('Location: panel.php?window=password'); exit(); } \ No newline at end of file diff --git a/admin/algo/service_status.alg.php b/admin/algo/service_status.alg.php index 258aa59..a921b70 100644 --- a/admin/algo/service_status.alg.php +++ b/admin/algo/service_status.alg.php @@ -17,7 +17,7 @@ $conn = @new mysqli($host, $db_user, $db_password, $db_name); if ($conn->connect_errno!=0) { - $_SESSION['message'] = 'connection do db fail'; + $_SESSION['message'] = 'Connection do db fail.'; header('Location: ../panel.php?window=service-break'); exit(); } @@ -33,15 +33,19 @@ if ($stmt->execute()) { if ($stmt->affected_rows > 0) { $_SESSION['message'] = 'Service status value changed successfully.'; + header('Location: ../panel.php?window=service-break'); } else { $_SESSION['message'] = 'Error during updating: row ID error.'; + header('Location: ../panel.php?window=service-break'); } } else { $_SESSION['message'] = 'Something went wrong during updating status.'; + header('Location: ../panel.php?window=service-break'); } $stmt->close(); } else { $_SESSION['message'] = 'Failed to prepare the SQL statement.'; + header('Location: ../panel.php?window=service-break'); } } else { diff --git a/admin/algo/theme.alg.php b/admin/algo/theme.alg.php index b1ffe50..ccdbc3a 100644 --- a/admin/algo/theme.alg.php +++ b/admin/algo/theme.alg.php @@ -1,4 +1,64 @@ connect_errno) { + $_SESSION['message'] = 'Connection failed: '.$conn->connect_error; + header('Location: ../panel.php?window=themes'); + exit(); + } + + $query = "UPDATE `settings` SET `value` = ? WHERE `name` = 'active_theme'"; + + $stmt = $conn->prepare($query); + if (!$stmt) { + $_SESSION['message'] = 'Error prep query: '.$conn->error; + header('Location: ../panel.php?window=themes'); + exit(); + } + + $stmt->bind_param("s", $newTheme); + if (!$stmt->execute()) { + $_SESSION['message'] = 'Error during query: '.$stmt->error; + header('Location: ../panel.php?window=themes'); + exit(); + } + + $stmt->close(); + $conn->close(); +} + +include dirname(__DIR__, 2) . '/themes/handlers/index.php'; + +if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['theme'])) { + $newTheme = $_POST['theme']; + + if (in_array($newTheme, getAvailableThemes())) { + + updateUserTheme($newTheme); + + $_SESSION['message'] = 'Theme changed to: '.htmlspecialchars($newTheme); + header('Location: ../panel.php?window=themes'); + exit(); + } else { + $_SESSION['message'] = 'Invalid theme!'; + header('Location: ../panel.php?window=themes'); + exit(); + } +} diff --git a/admin/panel.login.php b/admin/panel.login.php deleted file mode 100644 index 0a04506..0000000 --- a/admin/panel.login.php +++ /dev/null @@ -1,86 +0,0 @@ - - - - - - - Login Page - - - - - - -
-
- -
-
- - -
- -
- -

-
- -
-
-

'; - - include_once "./v_auth/version.php"; - - echo '

'; - if (version_compare(PHP_VERSION, PHP_VERSION_REQ, '<')) { - echo('The required PHP version is ' . PHP_VERSION_REQ . ' or higher. The installed version is: ' . PHP_VERSION . '

'); - echo('Version: ' . PHP_VERSION_ADVICE); - } - echo '

'; - ?> -
-
-
- - - - Screen to small to use admin panel - - - diff --git a/admin/panel.php b/admin/panel.php index fb94205..ad0fa90 100644 --- a/admin/panel.php +++ b/admin/panel.php @@ -20,6 +20,7 @@ //additional files require_once 'panel.connect.php'; include 'window_functions.php'; +include './v_auth/version.php'; ?> @@ -77,13 +78,14 @@