From df5630a701e246f2d648dec5b57a8d5918f344d1 Mon Sep 17 00:00:00 2001 From: Matthieu Bessat Date: Tue, 17 Aug 2021 15:08:06 +0200 Subject: [PATCH] feat(Logator): add delete and reset --- modules/logator/logator.php | 25 +++++++++++++++++++++++-- utils.php | 9 +++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/modules/logator/logator.php b/modules/logator/logator.php index 68b9d7e..4f951c2 100644 --- a/modules/logator/logator.php +++ b/modules/logator/logator.php @@ -18,7 +18,6 @@ if ($_SERVER['REQUEST_URI'] === '/') { } if ($_SERVER['REQUEST_URI'] === '/loggers') { - echo json_encode([ 'success' => true, 'data' => getDirsAsArray($logAt) @@ -26,12 +25,13 @@ if ($_SERVER['REQUEST_URI'] === '/loggers') { exit(); } -if (strpos($_SERVER['REQUEST_URI'], '/logger-details') === 0) { +if (strpos($_SERVER['REQUEST_URI'], '/logger') === 0) { if (!isset($_GET['id']) || $_GET['id'] === '') { echo json_encode([ 'success' => false, 'message' => 'Bro, I need an ID bro. Why are you doing that to me?' ]); + exit(); } $id = preg_replace('/[^0-9a-zA-Z]/', '', $_GET['id']); $path = $logAt . '/' . $id; @@ -44,6 +44,27 @@ if (strpos($_SERVER['REQUEST_URI'], '/logger-details') === 0) { exit(); } + if (isset($_GET['delete']) && ($_GET['delete'] === 'true' || $_GET['delete'] === 'yes')) { + $count = deleteAllFiles($path); + rmdir($path); + echo json_encode([ + 'success' => true, + 'message' => 'Logger deleted!', + 'records_deleted_count' => $count + ]); + exit(); + } + + if (isset($_GET['reset']) && ($_GET['reset'] === 'true' || $_GET['reset'] === 'yes')) { + $count = deleteAllFiles($path); + echo json_encode([ + 'success' => true, + 'message' => 'Logger reseted!', + 'records_deleted_count' => $count + ]); + exit(); + } + $data = []; foreach (getFilesAsArray($path) as $file) { $data[] = json_decode(file_get_contents($path . '/' . $file), true); diff --git a/utils.php b/utils.php index 00ec232..ed66647 100644 --- a/utils.php +++ b/utils.php @@ -54,3 +54,12 @@ function getDirsAsArray($path) { }, $dirs); return array_values($dirs); } + +function deleteAllFiles($path) { + $count = 0; + foreach (getFilesAsArray($path) as $file) { + unlink($path . '/' . $file); + $count++; + } + return $count; +}