Skip to content

Commit

Permalink
Refactor remove.php and moderation.js scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
judemont committed Sep 26, 2024
1 parent 059daf7 commit f310eeb
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
21 changes: 16 additions & 5 deletions api/remove.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,31 @@
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

include_once(dirname(__FILE__) . "/secrets.php");

if (!isset($_POST['adminPassword'], $_POST['mediaPath'])) {
echo json_encode(['error' => 'Missing required parameters.']);
exit();
}

$adminPassword = $_POST['adminPassword'];
$mediaPath = $_POST['mediaPath'];
$mediaPath = dirname(__FILE__) . "/../" . $_POST['mediaPath'];

if($adminPassword !== ADMIN_PASSWORD){
if ($adminPassword !== ADMIN_PASSWORD) {
echo json_encode(['error' => 'Unauthorized']);
exit();
}

if(unlink($mediaPath)){
if (!file_exists($mediaPath)) {
echo json_encode(['error' => 'Media file does not exist.']);
exit();
}

}else{
if (unlink($mediaPath)) {
echo json_encode(['success' => 'Media removed successfully.']);
} else {
echo json_encode(['error' => 'Error while removing media.']);
exit();
}
?>
?>
14 changes: 12 additions & 2 deletions scripts/moderation.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@ function removeMedia() {
const adminPassword = prompt("Enter the admin password to remove this media:");
fetch("api/remove.php", {
method: 'POST',
body: JSON.stringify({ mediaPath: mediaPath, adminPassword: adminPassword }),
body: objectToFormData({ mediaPath: mediaPath, adminPassword: adminPassword }),
}).catch(error => console.error('Error :', error));
}
}

function objectToFormData(obj) {
const formData = new FormData();
for (const key in obj) {
if (obj.hasOwnProperty(key)) {
formData.append(key, obj[key]);
}
}
return formData;
}
2 changes: 1 addition & 1 deletion scripts/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ document.addEventListener('DOMContentLoaded', () => {
.then(response => response.json())
.then(data => {
if (data.success) {
localStorage.setItem("mediaPath", data.file);
localStorage.setItem("mediaPath", "medias/"+data.file);
const mediaUrl = mediaPath + data.file;
mediaDisplay.innerHTML = `
${data.file.endsWith('.mp4') ?
Expand Down

0 comments on commit f310eeb

Please sign in to comment.