| Server IP : 158.247.231.215 / Your IP : 216.73.216.33 Web Server : Apache/2.4.41 (Ubuntu) System : Linux CTMS 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64 User : www-data ( 33) PHP Version : 8.0.30 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare, MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /mnt/blockstorage/ctms/api/custom-api/ |
Upload File : |
<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: DELETE, POST, OPTIONS');
header("Access-Control-Allow-Headers: X-Requested-With, Content-Type, Authorization");
header('Content-Type: application/json');
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
http_response_code(200);
exit();
}
// Load WordPress and auth helper
require_once('/mnt/blockstorage/ctms/wp-load.php');
require_once('auth-helper.php');
include('../database.php');
// Verify JWT token
$user_id = verify_jwt_token();
if (!$user_id) {
send_error_response('User not authenticated', 401);
}
if ($_SERVER['REQUEST_METHOD'] === 'DELETE' || $_SERVER['REQUEST_METHOD'] === 'POST') {
try {
$image_ids = array();
$content_id = null;
// Check for bulk deletion (multiple IDs)
// Try JSON input first
$input = file_get_contents('php://input');
$json_data = json_decode($input, true);
if ($json_data && isset($json_data['ids']) && is_array($json_data['ids'])) {
// JSON format: {"ids": [1, 2, 3]}
$image_ids = array_map('intval', $json_data['ids']);
} elseif (isset($_POST['ids'])) {
// POST format: ids=[1,2,3] or ids=1,2,3
if (is_array($_POST['ids'])) {
$image_ids = array_map('intval', $_POST['ids']);
} else {
$ids_string = trim($_POST['ids'], '[]');
$image_ids = array_map('intval', explode(',', $ids_string));
}
} elseif (isset($_GET['ids'])) {
// GET format: ids=1,2,3
$ids_string = trim($_GET['ids'], '[]');
$image_ids = array_map('intval', explode(',', $ids_string));
} else {
// Single deletion
$image_id = 0;
if (isset($_GET['id'])) {
$image_id = intval($_GET['id']);
} elseif (isset($_POST['id'])) {
$image_id = intval($_POST['id']);
}
if ($image_id <= 0) {
throw new Exception('Valid image ID or IDs are required');
}
$image_ids = array($image_id);
}
// Remove invalid IDs
$image_ids = array_filter($image_ids, function($id) {
return $id > 0;
});
if (empty($image_ids)) {
throw new Exception('No valid image IDs provided');
}
// Start transaction for bulk operations
$conn->begin_transaction();
$deleted_count = 0;
$deleted_images = array();
$failed_deletions = array();
foreach ($image_ids as $img_id) {
// Get image details before deletion
$sql = "SELECT * FROM vr_content_images WHERE id = $img_id";
$result = $conn->query($sql);
if (!$result || $result->num_rows === 0) {
$failed_deletions[] = array(
'id' => $img_id,
'reason' => 'Image not found'
);
continue;
}
$image = $result->fetch_assoc();
$image_path = $image['image_path'];
if ($content_id === null) {
$content_id = intval($image['content_id']);
}
// Delete from database
$delete_sql = "DELETE FROM vr_content_images WHERE id = $img_id";
if ($conn->query($delete_sql) === TRUE) {
// Delete physical file
$full_path = '/mnt/blockstorage/ctms/vr-content-uploads/' . $image_path;
if (file_exists($full_path)) {
if (!unlink($full_path)) {
error_log('Failed to delete physical file: ' . $full_path);
}
}
$deleted_count++;
$deleted_images[] = array(
'id' => $img_id,
'path' => $image_path
);
} else {
$failed_deletions[] = array(
'id' => $img_id,
'reason' => 'Database deletion failed'
);
}
}
// Commit transaction
$conn->commit();
// Update content timestamp if any deletion succeeded
if ($deleted_count > 0 && $content_id !== null) {
$update_sql = "UPDATE vr_contents SET updated_at = NOW() WHERE id = $content_id";
$conn->query($update_sql);
}
$response = array(
'status' => 'success',
'message' => $deleted_count . ' image(s) deleted successfully',
'data' => array(
'deleted_count' => $deleted_count,
'total_requested' => count($image_ids),
'content_id' => $content_id,
'deleted_images' => $deleted_images,
'failed_deletions' => $failed_deletions
)
);
echo json_encode($response);
} catch (Exception $e) {
// Rollback on error
if (isset($conn)) {
$conn->rollback();
}
$response = array(
'status' => 'error',
'message' => $e->getMessage()
);
http_response_code(400);
echo json_encode($response);
}
$conn->close();
} else {
$response = array(
'status' => 'error',
'message' => 'Only DELETE or POST requests are allowed'
);
http_response_code(405);
echo json_encode($response);
}
?>