From 4f8b1d07bc1e21b1c7b89166dcc9dd9a4a0a4f24 Mon Sep 17 00:00:00 2001 From: Marcel Peterkau Date: Fri, 25 Nov 2022 01:50:02 +0100 Subject: [PATCH] Add List Functions working and changed image-URL --- add_item.php | 9 +++++- config/config.php | 1 + include/delete_unused.php | 32 +++++++++++++++++++++ include/listgenerator.php | 31 ++++++++++++++++++--- index.php | 58 ++++++++++++++++++++++++++++++++------- 5 files changed, 116 insertions(+), 15 deletions(-) create mode 100644 include/delete_unused.php diff --git a/add_item.php b/add_item.php index f0547fb..032eeef 100644 --- a/add_item.php +++ b/add_item.php @@ -10,6 +10,13 @@ $ItemPrice = $_POST['ItemPrice']; $ItemLink = $_POST['ItemLink']; $ItemImage = $_POST['ItemImage']; +#--- check if the provided Link is a valid URL + +if (filter_var($ItemLink, FILTER_VALIDATE_URL) === FALSE) { + die('Not a valid URL'); +} + +#--- #--- check if the provided Image-Link is a real image: @@ -17,7 +24,7 @@ $headers = array_change_key_case(get_headers($ItemImage, 1), CASE_LOWER); if (strpos($headers['content-type'], 'image/') !== false) { $strippedimagepath = strtok($ItemImage, '?'); - $imageLocalLink = 'data/images/' . uniqid() . '.' . pathinfo($strippedimagepath, PATHINFO_EXTENSION); + $imageLocalLink = $imagedir . '/' . uniqid() . '.' . pathinfo($strippedimagepath, PATHINFO_EXTENSION); echo "ImageLink: " . $imageLocalLink; file_put_contents($imageLocalLink, fopen($strippedimagepath, 'r')); } else { diff --git a/config/config.php b/config/config.php index b64b653..a659a69 100644 --- a/config/config.php +++ b/config/config.php @@ -4,5 +4,6 @@ $servername = 'localhost'; $username = 'wishlist'; $db = 'wishlist'; $password = 'R!6CIb-KxM96EC]6'; +$imagedir = 'data/images'; ?> \ No newline at end of file diff --git a/include/delete_unused.php b/include/delete_unused.php new file mode 100644 index 0000000..1ec6b81 --- /dev/null +++ b/include/delete_unused.php @@ -0,0 +1,32 @@ +connect_error) + die('Connection failed: ' . $conn->connect_error); + +$sql = 'SELECT image FROM whishes'; +$result = $conn->query($sql); + + if ($result !== false && $result->num_rows > 0) + { + if ($rows = $result->fetch_all()) + { + foreach ($dir as $fileinfo) { + if (!$fileinfo->isDot()) { + $filename = $fileinfo->getFilename(); + + if (!in_array($filename, $rows)) + { + $deletepath = '../' . $imagedir . '/' . $filename; + unset($deletepath); + } + } + } + } + } + +$conn->close(); \ No newline at end of file diff --git a/include/listgenerator.php b/include/listgenerator.php index fe0f9c3..90dee25 100644 --- a/include/listgenerator.php +++ b/include/listgenerator.php @@ -4,7 +4,7 @@ include 'config/config.php'; function generateListItem($ListItemID, $ItemImage, $ItemTitle, $ItemLink, $ItemPrice, $ItemComment, $ItemReserved, $ItemDate) { - global $loggedin; + global $loggedin, $imagedir; $formatter = new NumberFormatter('de_DE', NumberFormatter::CURRENCY); @@ -18,7 +18,7 @@ function generateListItem($ListItemID, $ItemImage, $ItemTitle, $ItemLink, $ItemP
' . $ItemTitle . '
- +

' . $ItemComment . '

@@ -81,8 +81,31 @@ function wishlistMainBuilder($ListID, $sortby) } } else { echo (' -

Das tut mir leid...

-

Diese Liste gibt es nicht mehr

+
+ '); } ?> @@ -364,7 +395,7 @@ if (isset($_POST['delete']) && $loggedin == true) { - +