From 096c239715f64800227ee69f1687152daa51dbf0 Mon Sep 17 00:00:00 2001 From: "giandonato.inverso@edempg.it" Date: Thu, 18 Apr 2024 20:33:20 +0200 Subject: [PATCH] bug fix in read.php, bump version in footer, NEW: bulk delete --- src/bulk_action.php | 118 ++++++++++++++++-------- src/dist/js/custom.js | 10 +- src/forms/table_dynamic.php | 16 +++- src/forms/table_static.php | 16 +++- src/includes/footer.php | 7 +- src/lib/DynamicQrcode/DynamicQrcode.php | 6 +- src/lib/Qrcode/Qrcode.php | 5 +- src/lib/StaticQrcode/StaticQrcode.php | 6 +- src/read.php | 2 +- 9 files changed, 129 insertions(+), 57 deletions(-) diff --git a/src/bulk_action.php b/src/bulk_action.php index e59d302..d97ccf7 100644 --- a/src/bulk_action.php +++ b/src/bulk_action.php @@ -6,52 +6,96 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') { $db = getDbInstance(); $json = json_decode(file_get_contents('php://input'), true); - $params = $json['params']; - $files = []; + if($json["action"] == "download") { + $params = $json['params']; + $files = []; + + if (isset($json['type'])) { + $type = filter_var($json['type'], FILTER_SANITIZE_FULL_SPECIAL_CHARS); + } else { + echo json_encode([ + 'data' => 'Type action field in the request.', + 'status' => 400 + ]); + exit(); + } + + if (count($params) == 0) { + echo json_encode([ + 'data' => 'No qrcodes were selected.', + 'status' => 400 + ]); + exit(); + } + + foreach ($params as $param) { + $row = $db->where('id', $param); + $row = $db->getOne("{$type}_qrcodes"); + @$files[] = SAVED_QRCODE_FOLDER . $row['qrcode']; + } + + $zip = new ZipArchive(); + $uniqid = uniqid(); + $relative_dir = SAVED_QRCODE_FOLDER . 'zip/qrcodes_' . $uniqid . '.zip'; + @unlink($relative_dir); + $url_path = SAVED_QRCODE_URL . 'zip/qrcodes_' . $uniqid . '.zip'; + $zip->open($relative_dir, ZipArchive::CREATE); + + foreach ($files as $file) { + $download_file = @file_get_contents($file, true); + $zip->addFromString(basename($file), $download_file); + } + + $zip->close(); - if (isset($json['type'])) { - $type = filter_var($json['type'], FILTER_SANITIZE_FULL_SPECIAL_CHARS); - } else { echo json_encode([ - 'data' => 'Type action field in the request.', - 'status' => 400 + 'data' => $url_path, + 'status' => 200 ]); exit(); - } + } else if($json["action"] == "delete") { + $params = $json['params']; + $files = []; + + if (isset($json['type'])) { + $type = filter_var($json['type'], FILTER_SANITIZE_FULL_SPECIAL_CHARS); + } else { + echo json_encode([ + 'data' => 'Type action field in the request.', + 'status' => 400 + ]); + exit(); + } + + if (count($params) == 0) { + echo json_encode([ + 'data' => 'No qrcodes were selected.', + 'status' => 400 + ]); + exit(); + } + + if($type == "dynamic") + $instance = new DynamicQrcode(); + else if($type == "static") + $instance = new StaticQrcode(); + else + die("Type not allowed"); + + foreach ($params as $param) { + $a = 0; + $instance->deleteQrcode($param, true); + } - if (count($params) == 0) { echo json_encode([ - 'data' => 'No qrcodes were selected.', - 'status' => 400 + 'action' => "delete", + 'data' => "Qrcode deleted", + 'status' => 200 ]); exit(); - } - foreach ($params as $param) { - $row = $db->where('id', $param); - $row = $db->getOne("{$type}_qrcodes"); - @$files[] = SAVED_QRCODE_FOLDER . $row['qrcode']; - } - - $zip = new ZipArchive(); - $uniqid = uniqid(); - $relative_dir = SAVED_QRCODE_FOLDER. 'zip/qrcodes_'. $uniqid .'.zip'; - @unlink($relative_dir); - $url_path = SAVED_QRCODE_URL . 'zip/qrcodes_'. $uniqid .'.zip'; - $zip->open($relative_dir, ZipArchive::CREATE); - - foreach ($files as $file) { - $download_file = @file_get_contents($file, true); - $zip->addFromString(basename($file), $download_file); - } - - $zip->close(); - - echo json_encode([ - 'data' => $url_path, - 'status' => 200 - ]); - exit(); + } else + exit("Action not allowed"); } else { exit('Direct access to this script not allowed.'); } diff --git a/src/dist/js/custom.js b/src/dist/js/custom.js index 4ebb9d3..7653209 100644 --- a/src/dist/js/custom.js +++ b/src/dist/js/custom.js @@ -459,7 +459,7 @@ }); const data = { - action: 'download', + action: $('select[name=action]').val(), params: params, type: $('input[name=type]').val() } @@ -472,8 +472,12 @@ contentType: 'application/json', success: (res) => { if (res.status == 200) { - window.location.href = res.data; - $('#err-msg').css('display', 'none'); + if(data["action"] === "download") { + window.location.href = res.data; + $('#err-msg').css('display', 'none'); + } else if(data["action"] === "delete") { + location.reload(); + } } if (res.status == 400) { diff --git a/src/forms/table_dynamic.php b/src/forms/table_dynamic.php index 472c5e6..24e590f 100644 --- a/src/forms/table_dynamic.php +++ b/src/forms/table_dynamic.php @@ -3,8 +3,20 @@
- - +
+
+
+
+ + + +
+
+
+
diff --git a/src/forms/table_static.php b/src/forms/table_static.php index 5ec31f0..4fa8e18 100644 --- a/src/forms/table_static.php +++ b/src/forms/table_static.php @@ -3,8 +3,20 @@
- - +
+
+
+
+ + + +
+
+
+
diff --git a/src/includes/footer.php b/src/includes/footer.php index 9aa7a36..e116740 100644 --- a/src/includes/footer.php +++ b/src/includes/footer.php @@ -6,10 +6,9 @@ @@ -23,7 +22,7 @@ - + diff --git a/src/lib/DynamicQrcode/DynamicQrcode.php b/src/lib/DynamicQrcode/DynamicQrcode.php index 5b1e880..75c5cb1 100644 --- a/src/lib/DynamicQrcode/DynamicQrcode.php +++ b/src/lib/DynamicQrcode/DynamicQrcode.php @@ -89,9 +89,9 @@ class DynamicQrcode { * Delete qr code * */ - public function deleteQrcode($id) { + public function deleteQrcode($id, $async = false) { if($_SESSION['type'] === "super") { - $this->qrcode_instance->deleteQrcode($id); + $this->qrcode_instance->deleteQrcode($id, $async); } else if ($_SESSION['type'] === "admin") { $qrcode = $this->getQrcode($id); @@ -103,7 +103,7 @@ class DynamicQrcode { $user = $users->getUser($_SESSION['user_id']); if($user["id"] === $qrcode["id_owner"]) - $this->qrcode_instance->deleteQrcode($id); + $this->qrcode_instance->deleteQrcode($id, $async); else $this->failure("You cannot delete this qrcode because it's of another user"); } diff --git a/src/lib/Qrcode/Qrcode.php b/src/lib/Qrcode/Qrcode.php index a74e8a2..4a292bf 100644 --- a/src/lib/Qrcode/Qrcode.php +++ b/src/lib/Qrcode/Qrcode.php @@ -155,7 +155,7 @@ class Qrcode { * Delete qr code * */ - public function deleteQrcode($id) { + public function deleteQrcode($id, $async = false) { $db = getDbInstance(); $qrcode = $this->getQrcode($id); @@ -171,7 +171,8 @@ class Qrcode { } if ($status) - $this->info('Qr code deleted successfully!'); + if (!$async) + $this->info('Qr code deleted successfully!'); else $this->failure('Unable to delete qr code'); } diff --git a/src/lib/StaticQrcode/StaticQrcode.php b/src/lib/StaticQrcode/StaticQrcode.php index 5bfe02b..0e1ed6b 100644 --- a/src/lib/StaticQrcode/StaticQrcode.php +++ b/src/lib/StaticQrcode/StaticQrcode.php @@ -390,9 +390,9 @@ class StaticQrcode { * Delete qr code * */ - public function deleteQrcode($id) { + public function deleteQrcode($id, $async = false) { if($_SESSION['type'] === "super") { - $this->qrcode_instance->deleteQrcode($id); + $this->qrcode_instance->deleteQrcode($id, $async); } else if ($_SESSION['type'] === "admin") { $qrcode = $this->getQrcode($id); @@ -404,7 +404,7 @@ class StaticQrcode { $user = $users->getUser($_SESSION['user_id']); if($user["id"] === $qrcode["id_owner"]) - $this->qrcode_instance->deleteQrcode($id); + $this->qrcode_instance->deleteQrcode($id, $async); else $this->failure("You cannot delete this qrcode because it's of another user"); } diff --git a/src/read.php b/src/read.php index 2a99ee5..d415ea2 100644 --- a/src/read.php +++ b/src/read.php @@ -13,7 +13,7 @@ $data = array ( 'scan' => $db->inc(1) ); $db->where("identifier", $_GET['id']); -$db->update (DATABASE_PREFIX.'dynamic_qrcodes', $data); +$db->update ('dynamic_qrcodes', $data); if($qrcode['state'] == 'enable'){ echo '';