Security fixes: - Add HTTP method validation to delete_comment.php (block CSRF via GET) - Remove $_GET fallback in comment deletion (was CSRF bypass vector) - Guard session_start() with session_status() check across API files - Escape json_encode() data attributes with htmlspecialchars in views - Escape inline APP_TIMEZONE config values in DashboardView/TicketView - Validate timezone param against DateTimeZone::listIdentifiers() in index.php - Remove Database::escape() (was using real_escape_string, not safe) - Fix AttachmentModel hardcoded connection; inject via constructor Backend fixes: - Fix CommentModel bind_param type for ticket_id (s→i) - Fix buildCommentThread orphan parent guard - Fix StatsModel JOIN→LEFT JOIN so unassigned tickets aren't excluded - Add ticket ID validation in BulkOperationsModel before implode() - Add duplicate key retry in TicketModel::createTicket() for race conditions - Wrap SavedFiltersModel default filter changes in transactions - Add null result guards in WorkflowModel query methods Frontend JS: - Rewrite toast.js as lt.toast shim (base.js dependency) - Delegate escapeHtml() to lt.escHtml() - Rewrite keyboard-shortcuts.js using lt.keys.on() - Migrate settings.js to lt.api.* and lt.modal.open/close() - Migrate advanced-search.js to lt.api.* and lt.modal.open/close() - Migrate dashboard.js fetch calls to lt.api.*; update all dynamic modals (bulk ops, quick actions, confirm/input) to lt-modal structure - Migrate ticket.js fetchMentionUsers to lt.api.get() - Remove console.log/error/warn calls from JS files Views: - Add /web_template/base.css and base.js to all 10 view files - Call lt.keys.initDefaults() in DashboardView, TicketView, admin views - Migrate all modal HTML from settings-modal/settings-content to lt-modal-overlay/lt-modal/lt-modal-header/lt-modal-body/lt-modal-footer - Replace style="display:none" with aria-hidden="true" on all modals - Replace modal open/close style.display with lt.modal.open/close() - Update modal buttons to lt-btn lt-btn-primary/lt-btn-ghost classes - Remove manual ESC keydown handlers (replaced by lt.keys.initDefaults) - Fix unescaped timezone values in TicketView inline script Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
110 lines
3.3 KiB
PHP
110 lines
3.3 KiB
PHP
<?php
|
|
/**
|
|
* Delete Attachment API
|
|
*
|
|
* Handles deletion of ticket attachments
|
|
*/
|
|
|
|
// Capture errors for debugging
|
|
ini_set('display_errors', 0);
|
|
error_reporting(E_ALL);
|
|
|
|
// Apply rate limiting (also starts session)
|
|
require_once dirname(__DIR__) . '/middleware/RateLimitMiddleware.php';
|
|
RateLimitMiddleware::apply('api');
|
|
|
|
// Ensure session is started
|
|
if (session_status() === PHP_SESSION_NONE) {
|
|
session_start();
|
|
}
|
|
|
|
require_once dirname(__DIR__) . '/config/config.php';
|
|
require_once dirname(__DIR__) . '/helpers/Database.php';
|
|
require_once dirname(__DIR__) . '/helpers/ResponseHelper.php';
|
|
require_once dirname(__DIR__) . '/models/AttachmentModel.php';
|
|
require_once dirname(__DIR__) . '/models/AuditLogModel.php';
|
|
require_once dirname(__DIR__) . '/middleware/CsrfMiddleware.php';
|
|
|
|
header('Content-Type: application/json');
|
|
|
|
// Check authentication
|
|
if (!isset($_SESSION['user']) || !isset($_SESSION['user']['user_id'])) {
|
|
ResponseHelper::unauthorized();
|
|
}
|
|
|
|
// Only accept DELETE or POST requests
|
|
if (!in_array($_SERVER['REQUEST_METHOD'], ['DELETE', 'POST'])) {
|
|
ResponseHelper::error('Method not allowed', 405);
|
|
}
|
|
|
|
// Get request body
|
|
$input = json_decode(file_get_contents('php://input'), true);
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$input = array_merge($_POST, $input ?? []);
|
|
}
|
|
|
|
// Verify CSRF token
|
|
$csrfToken = $input['csrf_token'] ?? $_SERVER['HTTP_X_CSRF_TOKEN'] ?? '';
|
|
if (!CsrfMiddleware::validateToken($csrfToken)) {
|
|
ResponseHelper::forbidden('Invalid CSRF token');
|
|
}
|
|
|
|
// Get attachment ID
|
|
$attachmentId = $input['attachment_id'] ?? null;
|
|
if (!$attachmentId || !is_numeric($attachmentId)) {
|
|
ResponseHelper::error('Valid attachment ID is required');
|
|
}
|
|
|
|
$attachmentId = (int)$attachmentId;
|
|
|
|
try {
|
|
$attachmentModel = new AttachmentModel(Database::getConnection());
|
|
|
|
// Get attachment details
|
|
$attachment = $attachmentModel->getAttachment($attachmentId);
|
|
if (!$attachment) {
|
|
ResponseHelper::notFound('Attachment not found');
|
|
}
|
|
|
|
// Check permission
|
|
$isAdmin = $_SESSION['user']['is_admin'] ?? false;
|
|
if (!$attachmentModel->canUserDelete($attachmentId, $_SESSION['user']['user_id'], $isAdmin)) {
|
|
ResponseHelper::forbidden('You do not have permission to delete this attachment');
|
|
}
|
|
|
|
// Delete the file
|
|
$uploadDir = $GLOBALS['config']['UPLOAD_DIR'] ?? dirname(__DIR__) . '/uploads';
|
|
$filePath = $uploadDir . '/' . $attachment['ticket_id'] . '/' . $attachment['filename'];
|
|
|
|
if (file_exists($filePath)) {
|
|
if (!unlink($filePath)) {
|
|
ResponseHelper::serverError('Failed to delete file');
|
|
}
|
|
}
|
|
|
|
// Delete from database
|
|
if (!$attachmentModel->deleteAttachment($attachmentId)) {
|
|
ResponseHelper::serverError('Failed to delete attachment record');
|
|
}
|
|
|
|
// Log the deletion
|
|
$conn = Database::getConnection();
|
|
$auditLog = new AuditLogModel($conn);
|
|
$auditLog->log(
|
|
$_SESSION['user']['user_id'],
|
|
'attachment_delete',
|
|
'ticket_attachments',
|
|
(string)$attachmentId,
|
|
[
|
|
'ticket_id' => $attachment['ticket_id'],
|
|
'filename' => $attachment['original_filename'],
|
|
'size' => $attachment['file_size']
|
|
]
|
|
);
|
|
|
|
ResponseHelper::success([], 'Attachment deleted successfully');
|
|
|
|
} catch (Exception $e) {
|
|
ResponseHelper::serverError('Failed to delete attachment');
|
|
}
|