From 7d078cb9541aa1de77f26683f3494d0b911dbc29 Mon Sep 17 00:00:00 2001 From: Pinto Date: Tue, 2 Dec 2025 17:41:24 +0200 Subject: [PATCH] WIP: Blogposts --- .htaccess | 2 +- admin_blogs.php | 144 +++++++++++++++++++++++ autosave.php | 101 ++++++++++++++++ blog.php | 8 +- blog_create.php | 33 ++++++ blog_delete.php | 36 ++++++ blog_edit.php | 265 ++++++++++++++++++++++++++++++++++++++++++ blog_read.php | 176 ++++++++++++++++++++++++++++ blog_unpublish.php | 31 +++++ functions.php | 4 +- header01.php | 2 + header02.php | 4 + index.php | 12 +- phpinfo.php | 3 + publish_blog.php | 31 +++++ submit_blog.php | 24 ++++ upload.php | 27 +++++ upload_blog_image.php | 24 ++++ upload_debug.log | 0 user_blogs.php | 124 ++++++++++++++++++++ 20 files changed, 1038 insertions(+), 13 deletions(-) create mode 100644 admin_blogs.php create mode 100644 autosave.php create mode 100644 blog_create.php create mode 100644 blog_delete.php create mode 100644 blog_edit.php create mode 100644 blog_read.php create mode 100644 blog_unpublish.php create mode 100644 phpinfo.php create mode 100644 publish_blog.php create mode 100644 submit_blog.php create mode 100644 upload.php create mode 100644 upload_blog_image.php create mode 100644 upload_debug.log create mode 100644 user_blogs.php diff --git a/.htaccess b/.htaccess index b73f49b4..2b5271b3 100644 --- a/.htaccess +++ b/.htaccess @@ -1,4 +1,4 @@ -php_flag display_errors Off +php_flag display_errors On # php_value error_reporting -1 RedirectMatch 403 ^/\.well-known Options -Indexes diff --git a/admin_blogs.php b/admin_blogs.php new file mode 100644 index 00000000..63099286 --- /dev/null +++ b/admin_blogs.php @@ -0,0 +1,144 @@ +prepare(" + SELECT + b.blog_id, + b.title, + b.description, + b.status, + b.date, + b.image, + CONCAT(u.first_name, ' ', u.last_name) AS author_name, + u.email AS author_email, + u.profile_pic + FROM blogs b + JOIN users u ON b.author = u.user_id + WHERE b.status != 'deleted' + ORDER BY b.date DESC +"); + +$result->execute(); +$posts = $result->get_result(); + + +?> + + + + + + +
+ + +
+ +
+
+ + + + + +
+
+
+
+ +

My Posts

+ +
+ + × +
+ + + + New Post + + fetch_assoc()): + // Output the HTML structure with dynamic data + echo ' +
+
+ ' . $post[ +
+
+
+ Author +
+ ' . strtoupper($post["status"]) . ' +
' . $post["title"] . '
+ ' . $post["author_name"] . ' +
+
+

' . $post["description"] . '

+ +
+
+ '; + endwhile; ?> + + +
+ +
+
+
+ + + + + \ No newline at end of file diff --git a/autosave.php b/autosave.php new file mode 100644 index 00000000..f9b42257 --- /dev/null +++ b/autosave.php @@ -0,0 +1,101 @@ + 'The uploaded file exceeds the upload_max_filesize directive in php.ini.', + UPLOAD_ERR_FORM_SIZE => 'The uploaded file exceeds the MAX_FILE_SIZE directive in the HTML form.', + UPLOAD_ERR_PARTIAL => 'The uploaded file was only partially uploaded.', + UPLOAD_ERR_NO_FILE => 'No file was uploaded.', + UPLOAD_ERR_NO_TMP_DIR => 'Missing a temporary folder.', + UPLOAD_ERR_CANT_WRITE => 'Failed to write file to disk.', + UPLOAD_ERR_EXTENSION => 'A PHP extension stopped the upload.', + ]; + $errorMessage = $errorMessages[$fileError] ?? 'Unknown upload error.'; + http_response_code(500); + echo "Upload error: $errorMessage"; + exit; + } + + // Skip upload if identical file already exists + if (file_exists($targetPath)) { + $cover_image_path = $publicPath; + } else { + if (move_uploaded_file($_FILES['cover_image']['tmp_name'], $targetPath)) { + $cover_image_path = $publicPath; + } else { + http_response_code(500); + echo "Failed to move uploaded file."; + exit; + } + } +} + +// Prepare SQL with/without image update +if ($cover_image_path) { + $stmt = $conn->prepare(" + UPDATE blogs + SET title = ?, content = ?, description = ?, category = ?, image = ?, author = ? + WHERE blog_id = ? + "); + $stmt->bind_param("ssssssi", $title, $content, $description, $category, $cover_image_path, $author_id, $article_id); +} else { + $stmt = $conn->prepare(" + UPDATE blogs + SET title = ?, content = ?, description = ?, category = ?, author = ? + WHERE blog_id = ? + "); + $stmt->bind_param("ssssii", $title, $content, $description, $category, $author_id, $article_id); +} + +if ($stmt->execute()) { + echo "Saved"; +} else { + http_response_code(500); + echo "Database update failed: " . $stmt->error; +} diff --git a/blog.php b/blog.php index 633d4db9..a602710f 100644 --- a/blog.php +++ b/blog.php @@ -64,7 +64,7 @@ if (!empty($bannerImages)) {
query($sql); if ($result->num_rows > 0) { @@ -86,7 +86,7 @@ if (!empty($bannerImages)) { $icon = "fa-lock"; } else { if (getUserMemberStatus($_SESSION['user_id'])) { - $blog_link = $row['link']; + $blog_link = "blog_read.php?token=".encryptData($blog_id, $salt); $button_hover = "Read More"; $icon = "fa-arrow-right"; } else { @@ -96,7 +96,7 @@ if (!empty($bannerImages)) { } } } else { - $blog_link = $row['link']; + $blog_link = "blog_read.php?token=".encryptData($blog_id, $salt); $button_hover = "Read More"; $icon = "fa-arrow-right"; } @@ -105,7 +105,7 @@ if (!empty($bannerImages)) { echo '
- Blog List + Blog List
' . $category . ' diff --git a/blog_create.php b/blog_create.php new file mode 100644 index 00000000..3bb3b221 --- /dev/null +++ b/blog_create.php @@ -0,0 +1,33 @@ +prepare("INSERT INTO blogs (author, title, category, description, content, date, status) + VALUES (?, '', '', '', '', ?, ?)"); +$stmt->bind_param("iss", $user_id, $date, $status); +$stmt->execute(); + +$blog_id = $stmt->insert_id; +header("Location: blog_edit.php?token=" . encryptData($blog_id, $salt)); +exit; diff --git a/blog_delete.php b/blog_delete.php new file mode 100644 index 00000000..fa3df29a --- /dev/null +++ b/blog_delete.php @@ -0,0 +1,36 @@ +prepare("UPDATE blogs SET status = 'deleted' WHERE blog_id = ? AND author = ?"); +$stmt->bind_param("ii", $article_id, $user_id); + +if ($stmt->execute()) { + $_SESSION['message'] = "Blog deleted!"; +} else { + $_SESSION['message'] = "Failed to delete blog: " . $stmt->error; +} + +header("Location: user_blogs.php"); +exit; +?> diff --git a/blog_edit.php b/blog_edit.php new file mode 100644 index 00000000..e175e98d --- /dev/null +++ b/blog_edit.php @@ -0,0 +1,265 @@ +prepare("SELECT * FROM blogs WHERE blog_id = ?"); +$stmt->bind_param("i", $blog_id); +$stmt->execute(); +$result = $stmt->get_result(); +if ($result->num_rows === 0) { + die("Blog post not found."); +} +$article = $result->fetch_assoc(); +$stmt->close(); +?> + + + + +
+ + + + + + + + + + + + \ No newline at end of file diff --git a/blog_read.php b/blog_read.php new file mode 100644 index 00000000..00b45124 --- /dev/null +++ b/blog_read.php @@ -0,0 +1,176 @@ +prepare(" + SELECT a.title, a.category, a.description, a.content, a.date, + u.first_name, u.last_name + FROM blogs a + JOIN users u ON a.author = u.user_id + WHERE a.blog_id = ? +"); +$stmt->bind_param("i", $blog_id); +$stmt->execute(); +$result = $stmt->get_result(); + +if ($result->num_rows === 0) { + die("Article not found."); +} + +$row = $result->fetch_assoc(); +$author = htmlspecialchars($row['first_name'] . ' ' . $row['last_name']); +?> + + + + + + +
+ +
+ +
+
+ + + + +
+
+
+
+
+ + + + +
+
+
+
+
Tags
+
+ +
+
+
+ +
+
+
+ +
+
+
+
+
+ \ No newline at end of file diff --git a/blog_unpublish.php b/blog_unpublish.php new file mode 100644 index 00000000..0ad08559 --- /dev/null +++ b/blog_unpublish.php @@ -0,0 +1,31 @@ +prepare("UPDATE blogs SET status = 'draft' WHERE blog_id = ? AND author = ?"); +$stmt->bind_param("ii", $article_id, $user_id); + +if ($stmt->execute()) { + echo "Published"; +} else { + http_response_code(500); + echo "Failed to publish: " . $stmt->error; +} +?> diff --git a/functions.php b/functions.php index fb94792c..0ffeac8f 100644 --- a/functions.php +++ b/functions.php @@ -1784,8 +1784,8 @@ function getCommentCount($page_id) { $conn = openDatabaseConnection(); // Prepare statement to avoid SQL injection - $stmt = $conn->prepare("SELECT COUNT(*) FROM comments WHERE page_id = ?"); - $stmt->bind_param("i", $page_id); + $stmt = $conn->prepare("SELECT COUNT(*) FROM comments WHERE `page_id` = ?"); + $stmt->bind_param("s", $page_id); $stmt->execute(); // Get result diff --git a/header01.php b/header01.php index d6ad8dcf..73aa0483 100644 --- a/header01.php +++ b/header01.php @@ -211,6 +211,7 @@ logVisitor();
  • EFT Payments
  • Process Payments
  • +
  • Manage Blogs
  • Visitor Log
  • @@ -232,6 +233,7 @@ logVisitor();
  • Account Settings
  • Membership
  • My Bookings
  • +
  • My Blogs
  • Submit P.O.P
  • Log Out
  • diff --git a/header02.php b/header02.php index 17bb2814..b868c27a 100644 --- a/header02.php +++ b/header02.php @@ -33,6 +33,8 @@ logVisitor(); + + @@ -224,6 +226,7 @@ logVisitor();
  • EFT Payments
  • Process Payments
  • +
  • Manage Blogs
  • Visitor Log
  • @@ -238,6 +241,7 @@ logVisitor();
  • Account Settings
  • Membership
  • My Bookings
  • +
  • My Blogs
  • Submit P.O.P
  • Log Out
  • diff --git a/index.php b/index.php index b03534a0..cbbdddde 100644 --- a/index.php +++ b/index.php @@ -51,7 +51,7 @@ if (!empty($bannerImages)) {
    Logo

    - Welcome to
    the Four Wheel Drive Club
    of Southern Africa + Welcome to
    the 4 Wheel Drive Club
    of Southern Africa

    Become a Member @@ -112,7 +112,7 @@ if (countUpcomingTrips() > 0) { ?>
    ' . $location . ' -
    ' . $trip_name . '
    +
    ' . $trip_name . '
    ' . convertDate($start_date) . ' - ' . convertDate($end_date) . '
    ' . calculateDaysAndNights($start_date, $end_date) . '
    @@ -541,7 +541,7 @@ if (countUpcomingTrips() > 0) { ?>
    query($sql); if ($result->num_rows > 0) { @@ -562,7 +562,7 @@ if (countUpcomingTrips() > 0) { ?> $icon = "fa-lock"; }else{ if (getUserMemberStatus($_SESSION['user_id'])) { - $blog_link = $row['link']; + $blog_link = "blog_read.php?token=".encryptData($blog_id, $salt); $button_hover = "Read More"; $icon = "fa-arrow-right"; }else{ @@ -572,7 +572,7 @@ if (countUpcomingTrips() > 0) { ?> } } }else{ - $blog_link = $row['link']; + $blog_link = "blog_read.php?token=".encryptData($blog_id, $salt); $button_hover = "Read More"; $icon = "fa-arrow-right"; } @@ -591,7 +591,7 @@ if (countUpcomingTrips() > 0) { ?>
    - Blog List + Blog List
    Read More diff --git a/phpinfo.php b/phpinfo.php new file mode 100644 index 00000000..79e7d9d2 --- /dev/null +++ b/phpinfo.php @@ -0,0 +1,3 @@ +prepare("UPDATE blogs SET status = 'published' WHERE blog_id = ? AND author = ?"); +$stmt->bind_param("ii", $article_id, $user_id); + +if ($stmt->execute()) { + echo "Published"; +} else { + http_response_code(500); + echo "Failed to publish: " . $stmt->error; +} +?> diff --git a/submit_blog.php b/submit_blog.php new file mode 100644 index 00000000..01717680 --- /dev/null +++ b/submit_blog.php @@ -0,0 +1,24 @@ +prepare("INSERT INTO blogs (author, title, content, description, category, date) VALUES (?, ?, ?, ?, ?, ?)"); +$stmt->bind_param("isssss", $user_id, $title, $content, $description, $category, $date); +$stmt->execute(); + +header("Location: blog.php"); diff --git a/upload.php b/upload.php new file mode 100644 index 00000000..cc1581c6 --- /dev/null +++ b/upload.php @@ -0,0 +1,27 @@ + 'No file uploaded']); + http_response_code(400); + exit; +} + +$targetDir = "uploads/blogs/".$blog_id."/images/"; +if (!file_exists($targetDir)) { + mkdir($targetDir, 0777, true); +} + +$tmp = $_FILES['file']['tmp_name']; +$name = basename($_FILES['file']['name']); +$targetFile = $targetDir . uniqid() . "-" . $name; + +if (move_uploaded_file($tmp, $targetFile)) { + echo json_encode(['location' => $targetFile]); +} else { + echo json_encode(['error' => 'Failed to move uploaded file']); + http_response_code(500); +} diff --git a/upload_blog_image.php b/upload_blog_image.php new file mode 100644 index 00000000..b663eac2 --- /dev/null +++ b/upload_blog_image.php @@ -0,0 +1,24 @@ + 'No file uploaded']); + http_response_code(400); + exit; +} + +$targetDir = "uploads/blogs/images/"; +if (!file_exists($targetDir)) { + mkdir($targetDir, 0777, true); +} + +$tmp = $_FILES['file']['tmp_name']; +$name = basename($_FILES['file']['name']); +$targetFile = $targetDir . uniqid() . "-" . $name; + +if (move_uploaded_file($tmp, $targetFile)) { + echo json_encode(['location' => $targetFile]); +} else { + echo json_encode(['error' => 'Failed to move uploaded file']); + http_response_code(500); +} diff --git a/upload_debug.log b/upload_debug.log new file mode 100644 index 00000000..e69de29b diff --git a/user_blogs.php b/user_blogs.php new file mode 100644 index 00000000..4d82e5f7 --- /dev/null +++ b/user_blogs.php @@ -0,0 +1,124 @@ +prepare("SELECT blog_id, title, description, status, date, image FROM blogs WHERE author = ? AND status != 'deleted' ORDER BY date DESC"); + +$result->bind_param("i", $user_id); +$result->execute(); +$posts = $result->get_result(); +?> + + + + + + +
    + + + +
    + + + + + +
    +
    +
    +
    + +

    My Posts

    + +
    + + × +
    + + + + New Post + + fetch_assoc()): + // Output the HTML structure with dynamic data + echo ' +
    +
    ' . $post[
    +
    + +
    + ' . strtoupper($post["status"]) . ' +
    + +
    ' . $post["title"] . '
    +

    ' . $post["description"] . '

    + +
    +
    '; + endwhile; ?> + + +
    + +
    +
    +
    + + + + + \ No newline at end of file