From be80166795a3713bedbb5fe7a08c841f4365ec99 Mon Sep 17 00:00:00 2001 From: Fluffy-Bean <michal-gdula@protonmail.com> Date: Sat, 24 Sep 2022 14:08:14 +0000 Subject: [PATCH] Cleaning up Groups code --- app/app.php | 73 +++++++- assets/icons/package.svg | 1 + assets/icons/plus.svg | 1 + assets/ui/nav.php | 2 + group.php | 366 ++++++++++++++++++++------------------- 5 files changed, 260 insertions(+), 183 deletions(-) create mode 100644 assets/icons/package.svg create mode 100644 assets/icons/plus.svg diff --git a/app/app.php b/app/app.php index 17934b9..7588f35 100644 --- a/app/app.php +++ b/app/app.php @@ -135,14 +135,20 @@ class Image { Returns array with image info */ function get_image_info($conn, $id) { - // Setting SQL query - $sql = "SELECT * FROM images WHERE id = ".$id; - // Getting results - $query = mysqli_query($conn, $sql); - // Fetching associated info - $image_array = mysqli_fetch_assoc($query); + $sql = "SELECT * FROM images WHERE id = ?"; + + if ($stmt = mysqli_prepare($conn, $sql)) { + // Bind variables to the prepared statement as parameters + mysqli_stmt_bind_param($stmt, "i", $id); + + $stmt->execute(); + $query = $stmt->get_result(); + + // Fetching associated info + $group_array = mysqli_fetch_assoc($query); + } - return($image_array); + return($group_array); } /* Check if user is image owner @@ -164,6 +170,57 @@ class Image { } } +class Group { + function get_group_info($conn, $id) { + // Setting SQL query + $sql = "SELECT * FROM groups WHERE id = ?"; + + if ($stmt = mysqli_prepare($conn, $sql)) { + // Bind variables to the prepared statement as parameters + mysqli_stmt_bind_param($stmt, "i", $id); + + $stmt->execute(); + $query = $stmt->get_result(); + + // Fetching associated info + $group_array = mysqli_fetch_assoc($query); + } + + return($group_array); + } + + function get_group_members($conn, $id){ + $user_array = array(); + + $sql = "SELECT * FROM groups WHERE id = ?"; + + if ($stmt = mysqli_prepare($conn, $sql)) { + // Bind variables to the prepared statement as parameters + mysqli_stmt_bind_param($stmt, "i", $id); + + $stmt->execute(); + $query = $stmt->get_result(); + + // Fetching associated info + $group_array = mysqli_fetch_assoc($query); + } + + $image_list = explode(" ", $group_array['image_list']); + + foreach ($image_list as $image) { + $image_request = mysqli_query($conn, "SELECT author FROM images WHERE id = ".$image); + + while ($author = mysqli_fetch_column($image_request)) { + if (!in_array($author, $user_array)) { + array_push($user_array, $author); + } + } + } + + return($user_array); + } +} + class Diff { function time($past_time, $full_date = false) { $now = new \DateTime; @@ -193,4 +250,4 @@ class Diff { if (!$full_date) $string = array_slice($string, 0, 1); return $string ? implode(', ', $string) . ' ago' : 'just now'; } -} \ No newline at end of file +} diff --git a/assets/icons/package.svg b/assets/icons/package.svg new file mode 100644 index 0000000..f2ebf0d --- /dev/null +++ b/assets/icons/package.svg @@ -0,0 +1 @@ +<svg xmlns="http://www.w3.org/2000/svg" width="192" height="192" fill="#e8e3e3" viewBox="0 0 256 256"><rect width="256" height="256" fill="none"></rect><path d="M224,177.3V78.7a8.1,8.1,0,0,0-4.1-7l-88-49.5a7.8,7.8,0,0,0-7.8,0l-88,49.5a8.1,8.1,0,0,0-4.1,7v98.6a8.1,8.1,0,0,0,4.1,7l88,49.5a7.8,7.8,0,0,0,7.8,0l88-49.5A8.1,8.1,0,0,0,224,177.3Z" fill="none" stroke="#e8e3e3" stroke-linecap="round" stroke-linejoin="round" stroke-width="16"></path><polyline points="177 152.5 177 100.5 80 47" fill="none" stroke="#e8e3e3" stroke-linecap="round" stroke-linejoin="round" stroke-width="16"></polyline><polyline points="222.9 74.6 128.9 128 33.1 74.6" fill="none" stroke="#e8e3e3" stroke-linecap="round" stroke-linejoin="round" stroke-width="16"></polyline><line x1="128.9" y1="128" x2="128" y2="234.8" fill="none" stroke="#e8e3e3" stroke-linecap="round" stroke-linejoin="round" stroke-width="16"></line></svg> \ No newline at end of file diff --git a/assets/icons/plus.svg b/assets/icons/plus.svg new file mode 100644 index 0000000..aa4b3b2 --- /dev/null +++ b/assets/icons/plus.svg @@ -0,0 +1 @@ +<svg xmlns="http://www.w3.org/2000/svg" width="192" height="192" fill="#e8e3e3" viewBox="0 0 256 256"><rect width="256" height="256" fill="none"></rect><line x1="40" y1="128" x2="216" y2="128" fill="none" stroke="#e8e3e3" stroke-linecap="round" stroke-linejoin="round" stroke-width="16"></line><line x1="128" y1="40" x2="128" y2="216" fill="none" stroke="#e8e3e3" stroke-linecap="round" stroke-linejoin="round" stroke-width="16"></line></svg> \ No newline at end of file diff --git a/assets/ui/nav.php b/assets/ui/nav.php index 15427b2..597bca4 100644 --- a/assets/ui/nav.php +++ b/assets/ui/nav.php @@ -17,6 +17,8 @@ $loggedin = new Account(); ?> <a class='btn' href='upload.php'><img class='svg' src='assets/icons/upload.svg'><span class='nav-hide'>Upload</span></a> <hr> + <a class='btn' href='group.php'><img class='svg' src='assets/icons/package.svg'><span class='nav-hide'>Groups</span></a> + <hr> <a class='btn' href='account.php'><img class='svg' src='assets/icons/gear.svg'><span class='nav-hide'><?php echo substr($_SESSION["username"], 0, 15); ?></span></a> <?php } else { diff --git a/group.php b/group.php index 48283db..5e6b833 100644 --- a/group.php +++ b/group.php @@ -2,202 +2,118 @@ require_once __DIR__."/app/required.php"; use App\Account; + use App\Image; + use App\Group; use App\Diff; $user_info = new Account; - $diff = new Diff(); + $image_info = new Image; + $group_info = new Group; + $diff = new Diff(); ?> <!DOCTYPE html> <html> - -<head> - <?php require_once __DIR__."/assets/ui/header.php"; ?> -</head> - + <head> + <?php require_once __DIR__."/assets/ui/header.php"; ?> + </head> <body> <?php require_once __DIR__."/assets/ui/nav.php"; ?> - <div class="defaultDecoration defaultSpacing defaultFonts"> - <?php - if (!isset($_GET['id']) || empty($_GET['id'])) { - header("Location: index.php"); - } elseif (isset($_GET['id'])) { - $sql = "SELECT * FROM groups WHERE id = ?"; + <?php + if (isset($_GET['id'])) { + $group = $group_info->get_group_info($conn, $_GET['id']); + if (!isset($group) || empty($group)) header("Location: group.php"); + $image_list = array_reverse(explode(" ", $group['image_list'])); - if ($stmt = mysqli_prepare($conn, $sql)) { - // Bind variables to the prepared statement as parameters - mysqli_stmt_bind_param($stmt, "i", $param_group_id); - - $param_group_id = $_GET['id']; - - $stmt->execute(); - $query = $stmt->get_result(); + echo "<div class='defaultDecoration defaultSpacing defaultFonts'>"; - $group = mysqli_fetch_array($query); + echo "<h2>".$group['group_name']."</h2>"; - $image_list = array_reverse(explode(" ", $group['image_list'])); - } - } - ?> - <h2><?php echo $group['group_name']; ?></h2> - <?php - $user = $user_info->get_user_info($conn, $group['author']); - - if (isset($user['username'])) { - echo "<p>By: ".$user['username']."</p>"; - } else { - echo "<p>By: Deleted User</p>"; + $author_info = $user_info->get_user_info($conn, $group['author']); + echo "<p>By: ".$author_info['username']."</p>"; + + $group_members = $group_info->get_group_members($conn, $_GET['id']); + $members_array = array(); + foreach ($group_members as $member) { + $member_info = $user_info->get_user_info($conn, $member); + array_push($members_array, $member_info['username']); } + echo "<p>Members: ".implode(", ", $members_array)."</p>"; $upload_time = new DateTime($group['created_on']); - echo "<p id='updateTime'>Created at: ".$upload_time->format('d/m/Y H:i:s T')."</p>"; - ?> - <script> - // Updating time to Viewers local - var updateDate = new Date('<?php echo $upload_time->format('m/d/Y H:i:s T'); ?>'); - var format = {year: 'numeric', - month: 'short', - day: 'numeric' - }; - - updateDate = updateDate.toLocaleDateString('en-GB', format); + echo "<p id='updateTime'>Created at: ".$upload_time->format('d/m/Y H:i:s T')."</p;>"; + ?> + <script> + var updateDate = new Date('<?php echo $upload_time->format('m/d/Y H:i:s T'); ?>'); + updateDate = updateDate.toLocaleDateString('en-GB', {year: 'numeric', month: 'short', day: 'numeric'}); + $("#updateTime").html("Created at: "+updateDate); + </script> + <?php - $("#updateTime").html("Created at: "+updateDate); - </script> + echo "<p>Last Modified: ".$diff->time($group['last_modified'])."</p>"; - <p>Last Modified: <?php echo $diff->time($group['last_modified']); ?></p> - <?php - if ($_SESSION['id'] == $group['author'] || $user_info->is_admin($conn, $_SESSION['id'])) { - $privilaged = True; - } else { - $privilaged = False; - } + if ($_GET['mode'] == "edit") { + if ($_SESSION['id'] == $group['author'] || $user_info->is_admin($conn, $_SESSION['id'])) { + echo "<button class='btn btn-bad'>Delete</button>"; - if (isset($_GET['mode']) && $_GET['mode'] == "edit") { - if (!$privilaged) header("Location: group.php?id=".$_GET['id']); + echo "<button id='editTitle' class='btn btn-bad'>Update title</button>"; + ?> + <script> + $('#editTitle').click(function() { + var header = "Enter new Description/Alt"; + var description = "Newwww photo group name!"; + var actionBox = "<form id='titleForm' action='app/image/edit_description.php' method='POST'>\ + <input id='titleText' class='btn btn-neutral' type='text' placeholder='New title'>\ + <button id='titleSubmit' class='btn btn-bad' type='submit'><img class='svg' src='assets/icons/edit.svg'>Update title</button>\ + </form>"; + flyoutShow(header, description, actionBox); + + $("#titleForm").submit(function(event) { + event.preventDefault(); + var titleText = $("#titleText").val(); + var titleSubmit = $("#titleSubmit").val(); + $("#sniffle").load("app/image/group.php", { + group_id: <?php echo $_GET['id']; ?>, + group_title: titleText, + title_submit: titleSubmit + }); + }); + }); + </script> + <?php - echo "<button class='btn btn-bad'>Delete</button>"; + $image_request = mysqli_query($conn, "SELECT * FROM images"); - ?> - <button id='editTitle' class='btn btn-bad'>Update title</button> - <script> - $('#editTitle').click(function() { - var header = "Enter new Description/Alt"; - var description = "Newwww photo group name!"; - var actionBox = "<form id='titleForm' action='app/image/edit_description.php' method='POST'>\ - <input id='titleText' class='btn btn-neutral' type='text' placeholder='New title'>\ - <button id='titleSubmit' class='btn btn-bad' type='submit'><img class='svg' src='assets/icons/edit.svg'>Update title</button>\ - </form>"; - flyoutShow(header, description, actionBox); - - $("#titleForm").submit(function(event) { - event.preventDefault(); - var titleText = $("#titleText").val(); - var titleSubmit = $("#titleSubmit").val(); - $("#sniffle").load("app/image/group.php", { - group_id: <?php echo $_GET['id']; ?>, - group_title: titleText, - title_submit: titleSubmit - }); - }); - }); - </script> - <?php + echo "<form id='groupForm'>"; + while ($image = mysqli_fetch_array($image_request)) { + if (in_array($image['id'], $image_list)) { + echo "<input style='display: none;' type='checkbox' id='".$image['id']."' name='".$image['id']."' checked/>"; + } else { + echo "<input style='display: none;' type='checkbox' id='".$image['id']."' name='".$image['id']."'/>"; + } + } + echo "<button id='groupSubmit' class='btn btn-good' type='submit'>Update Images</button> + </form>"; - $image_request = mysqli_query($conn, "SELECT * FROM images"); - echo "<form id='groupForm'>"; - while ($image = mysqli_fetch_array($image_request)) { - if (in_array($image['id'], $image_list)) { - echo "<input style='display: none;' type='checkbox' id='".$image['id']."' name='".$image['id']."' checked/>"; - } else { - echo "<input style='display: none;' type='checkbox' id='".$image['id']."' name='".$image['id']."'/>"; - } + echo "<a href='group.php?id=".$_GET['id']."' class='btn btn-neutral'>Back</a>"; } - echo "<button id='groupSubmit' class='btn btn-good' type='submit'>Update Images</button></form>"; - - echo "<a href='group.php?id=".$_GET['id']."' class='btn btn-neutral'>Back</a>"; } else { - if ($privilaged) echo "<a href='group.php?id=".$_GET['id']."&mode=edit' class='btn btn-neutral'>Edit</a>"; + if ($_SESSION['id'] == $group['author'] || $user_info->is_admin($conn, $_SESSION['id'])) { + echo "<a href='group.php?id=".$_GET['id']."&mode=edit' class='btn btn-neutral'>Edit</a>"; + } } - ?> - </div> + + echo "</div>"; + } + ?> <div class="gallery-root defaultDecoration"> <?php - if (isset($_GET['mode']) && $_GET['mode'] == "edit") { - $image_request = mysqli_query($conn, "SELECT * FROM images ORDER BY id DESC"); + if (isset($_GET['id']) && !empty($_GET['id'])) { + if (isset($_GET['mode']) && $_GET['mode'] == "edit") { + $image_request = mysqli_query($conn, "SELECT * FROM images ORDER BY id DESC"); - while ($image = mysqli_fetch_array($image_request)) { - // Getting thumbnail - if (file_exists("images/thumbnails/".$image['imagename'])) { - $image_path = "images/thumbnails/".$image['imagename']; - } else { - $image_path = "images/".$image['imagename']; - } - - if (in_array($image['id'], $image_list)) { - echo "<div id='".$image['id']."' class='gallery-item selectedImage'> - <img class='gallery-image' loading='lazy' src='".$image_path."' id='".$image['id']."'> - </div>"; - } else { - echo "<div id='".$image['id']."' class='gallery-item'> - <img class='gallery-image' loading='lazy' src='".$image_path."' id='".$image['id']."'> - </div>"; - } - } - - ?> - <script> - $(".gallery-item").click(function() { - if (this.classList.contains("selectedImage")) { - deselect(this); - } else { - select(this); - } - }); - - function select(item) { - document.getElementById(item.id).checked = true; - - item.classList.add("selectedImage"); - } - function deselect(item) { - document.getElementById(item.id).checked = false; - - item.classList.remove("selectedImage"); - } - - function getList() { - var checkedBoxes = document.querySelectorAll('input[type=checkbox]:checked'); - var images = []; - - checkedBoxes.forEach(element => { - images.push(element.id); - }); - - return images; - } - - $("#groupForm").submit(function(event) { - event.preventDefault(); - - var groupSubmit = $("#groupSubmit").val(); - var images = getList(); - - $("#sniffle").load("app/image/group.php", { - group_images: images, - group_id: <?php echo $_GET['id']; ?>, - group_submit: groupSubmit - }); - }); - </script> - <?php - } else { - foreach ($image_list as $image) { - // Reading images from table - $image_request = mysqli_query($conn, "SELECT * FROM images WHERE id = ".$image); - while ($image = mysqli_fetch_array($image_request)) { // Getting thumbnail if (file_exists("images/thumbnails/".$image['imagename'])) { @@ -205,20 +121,120 @@ } else { $image_path = "images/".$image['imagename']; } - - // Check for NSFW tag - if (str_contains($image['tags'], "nsfw")) { - echo "<div class='gallery-item'> - <a href='image.php?id=".$image['id']."' class='nsfw-warning'><img class='svg' src='assets/icons/warning_red.svg'><span>NSFW</span></a> - <a href='image.php?id=".$image['id']."'><img class='gallery-image nsfw-blur' loading='lazy' src='".$image_path."' id='".$image['id']."'></a> + + if (in_array($image['id'], $image_list)) { + echo "<div id='".$image['id']."' class='gallery-item selectedImage'> + <img class='gallery-image' loading='lazy' src='".$image_path."' id='".$image['id']."'> </div>"; } else { - echo "<div class='gallery-item'> - <a href='image.php?id=".$image['id']."'><img class='gallery-image' loading='lazy' src='".$image_path."' id='".$image['id']."'></a> + echo "<div id='".$image['id']."' class='gallery-item'> + <img class='gallery-image' loading='lazy' src='".$image_path."' id='".$image['id']."'> </div>"; + } + } + + ?> + <script> + $(".gallery-item").click(function() { + if (this.classList.contains("selectedImage")) { + deselect(this); + } else { + select(this); + } + }); + + function select(item) { + document.getElementById(item.id).checked = true; + item.classList.add("selectedImage"); + } + function deselect(item) { + document.getElementById(item.id).checked = false; + item.classList.remove("selectedImage"); + } + + function getList() { + var checkedBoxes = document.querySelectorAll('input[type=checkbox]:checked'); + var images = []; + + checkedBoxes.forEach(element => { images.push(element.id); }); + + return images; + } + + $("#groupForm").submit(function(event) { + event.preventDefault(); + + var groupSubmit = $("#groupSubmit").val(); + var images = getList(); + + $("#sniffle").load("app/image/group.php", { + group_images: images, + group_id: <?php echo $_GET['id']; ?>, + group_submit: groupSubmit + }); + }); + </script> + <?php + } else { + foreach ($image_list as $image) { + // Reading images from table + try { + $image_request = mysqli_query($conn, "SELECT * FROM images WHERE id = ".$image); + + while ($image = mysqli_fetch_array($image_request)) { + // Getting thumbnail + if (file_exists("images/thumbnails/".$image['imagename'])) { + $image_path = "images/thumbnails/".$image['imagename']; + } else { + $image_path = "images/".$image['imagename']; + } + + // Check for NSFW tag + if (str_contains($image['tags'], "nsfw")) { + echo "<div class='gallery-item'> + <a href='image.php?id=".$image['id']."' class='nsfw-warning'><img class='svg' src='assets/icons/warning_red.svg'><span>NSFW</span></a> + <a href='image.php?id=".$image['id']."'><img class='gallery-image nsfw-blur' loading='lazy' src='".$image_path."' id='".$image['id']."'></a> + </div>"; + } else { + echo "<div class='gallery-item'> + <a href='image.php?id=".$image['id']."'><img class='gallery-image' loading='lazy' src='".$image_path."' id='".$image['id']."'></a> + </div>"; + } + } + } catch(Exception $e) { + $e; } } } + } elseif (!isset($_GET['id']) && empty($_GET['id'])) { + $group_list = mysqli_query($conn, "SELECT * FROM groups ORDER BY id DESC"); + + foreach ($group_list as $group) { + $image_list = array_reverse(explode(" ", $group['image_list'])); + $image = $image_info->get_image_info($conn, $image_list[array_rand($image_list, 1)]); + + // Getting thumbnail + if (file_exists("images/thumbnails/".$image['imagename'])) { + $image_path = "images/thumbnails/".$image['imagename']; + } else { + $image_path = "images/".$image['imagename']; + } + + // Check for NSFW tag + if (str_contains($image['tags'], "nsfw")) { + echo "<div class='gallery-item group-item'> + <a href='group.php?id=".$group['id']."' class='nsfw-warning gallery-group'><img class='svg' src='assets/icons/warning_red.svg'><span>NSFW</span></a> + <a href='group.php?id=".$group['id']."'><img class='gallery-image nsfw-blur' loading='lazy' src='".$image_path."' id='".$group['id']."'></a> + <a href='group.php?id=".$group['id']."' class='group-name'>".$group['group_name']."</a> + </div>"; + } else { + echo "<div class='gallery-item group-item'> + <a href='group.php?id=".$group['id']."' class='gallery-group'></a> + <a href='group.php?id=".$group['id']."'><img class='gallery-image' loading='lazy' src='".$image_path."' id='".$group['id']."'></a> + <a href='group.php?id=".$group['id']."' class='group-name'>".$group['group_name']."</a> + </div>"; + } + } } ?> </div>