2025-05-16 20:02:49 -04:00
|
|
|
<?php
|
2026-04-13 20:56:10 -04:00
|
|
|
|
2025-05-16 20:02:49 -04:00
|
|
|
// Enable error reporting for debugging
|
2026-09-11 12:17:15 -04:00
|
|
|
require_once dirname(__DIR__) . '/helpers/ErrorHandler.php';
|
|
|
|
|
ErrorHandler::init();
|
2025-05-16 20:02:49 -04:00
|
|
|
|
2026-01-20 09:55:01 -05:00
|
|
|
// Apply rate limiting
|
|
|
|
|
require_once dirname(__DIR__) . '/middleware/RateLimitMiddleware.php';
|
|
|
|
|
RateLimitMiddleware::apply('api');
|
2025-05-16 20:02:49 -04:00
|
|
|
|
|
|
|
|
// Start output buffering to capture any errors
|
|
|
|
|
ob_start();
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// Load config
|
|
|
|
|
$configPath = dirname(__DIR__) . '/config/config.php';
|
|
|
|
|
require_once $configPath;
|
2026-01-30 14:39:13 -05:00
|
|
|
require_once dirname(__DIR__) . '/helpers/Database.php';
|
2026-01-20 09:55:01 -05:00
|
|
|
|
2025-05-16 20:02:49 -04:00
|
|
|
// Load models directly with absolute paths
|
|
|
|
|
$ticketModelPath = dirname(__DIR__) . '/models/TicketModel.php';
|
|
|
|
|
$commentModelPath = dirname(__DIR__) . '/models/CommentModel.php';
|
2026-01-01 15:40:32 -05:00
|
|
|
$auditLogModelPath = dirname(__DIR__) . '/models/AuditLogModel.php';
|
2026-01-01 18:57:23 -05:00
|
|
|
$workflowModelPath = dirname(__DIR__) . '/models/WorkflowModel.php';
|
2026-01-01 15:40:32 -05:00
|
|
|
|
2025-05-16 20:02:49 -04:00
|
|
|
require_once $ticketModelPath;
|
|
|
|
|
require_once $commentModelPath;
|
2026-01-01 15:40:32 -05:00
|
|
|
require_once $auditLogModelPath;
|
2026-01-01 18:57:23 -05:00
|
|
|
require_once $workflowModelPath;
|
2026-03-29 21:34:16 -04:00
|
|
|
require_once dirname(__DIR__) . '/helpers/NotificationHelper.php';
|
2026-01-01 15:40:32 -05:00
|
|
|
|
|
|
|
|
// Check authentication via session
|
2026-03-17 23:22:24 -04:00
|
|
|
if (session_status() === PHP_SESSION_NONE) {
|
|
|
|
|
session_start();
|
|
|
|
|
}
|
2026-01-01 15:40:32 -05:00
|
|
|
if (!isset($_SESSION['user']) || !isset($_SESSION['user']['user_id'])) {
|
2026-07-10 11:48:34 -04:00
|
|
|
ob_end_clean();
|
|
|
|
|
http_response_code(401);
|
|
|
|
|
header('Content-Type: application/json');
|
|
|
|
|
echo json_encode(['success' => false, 'error' => 'Authentication required']);
|
|
|
|
|
exit;
|
2026-01-01 15:40:32 -05:00
|
|
|
}
|
2026-01-09 12:32:34 -05:00
|
|
|
|
|
|
|
|
// CSRF Protection
|
|
|
|
|
require_once dirname(__DIR__) . '/middleware/CsrfMiddleware.php';
|
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST' || $_SERVER['REQUEST_METHOD'] === 'PUT') {
|
|
|
|
|
$csrfToken = $_SERVER['HTTP_X_CSRF_TOKEN'] ?? '';
|
|
|
|
|
if (!CsrfMiddleware::validateToken($csrfToken)) {
|
|
|
|
|
http_response_code(403);
|
|
|
|
|
header('Content-Type: application/json');
|
2026-07-14 23:19:26 -04:00
|
|
|
echo json_encode([
|
2026-07-15 14:46:04 -04:00
|
|
|
'success' => false,
|
2026-07-14 23:19:26 -04:00
|
|
|
'error' => 'Invalid CSRF token',
|
|
|
|
|
'csrf_token' => CsrfMiddleware::getToken()
|
|
|
|
|
]);
|
2026-01-09 12:32:34 -05:00
|
|
|
exit;
|
|
|
|
|
}
|
2026-07-14 23:19:26 -04:00
|
|
|
$GLOBALS['newCsrfToken'] = CsrfMiddleware::rotateToken();
|
2026-01-09 12:32:34 -05:00
|
|
|
}
|
|
|
|
|
|
2026-01-01 15:40:32 -05:00
|
|
|
$currentUser = $_SESSION['user'];
|
|
|
|
|
$userId = $currentUser['user_id'];
|
2026-01-01 18:57:23 -05:00
|
|
|
$isAdmin = $currentUser['is_admin'] ?? false;
|
2026-01-01 15:40:32 -05:00
|
|
|
|
2026-09-24 19:13:31 -04:00
|
|
|
require_once dirname(__DIR__) . '/controllers/ApiTicketController.php';
|
2026-01-20 09:55:01 -05:00
|
|
|
|
2026-01-30 14:39:13 -05:00
|
|
|
// Use centralized database connection
|
|
|
|
|
$conn = Database::getConnection();
|
2026-01-20 09:55:01 -05:00
|
|
|
|
2025-09-05 11:08:56 -04:00
|
|
|
// Check request method
|
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
|
|
|
|
throw new Exception("Method not allowed. Expected POST, got " . $_SERVER['REQUEST_METHOD']);
|
|
|
|
|
}
|
2026-04-13 20:56:10 -04:00
|
|
|
|
2025-05-16 20:02:49 -04:00
|
|
|
// Get POST data
|
|
|
|
|
$input = file_get_contents('php://input');
|
|
|
|
|
$data = json_decode($input, true);
|
2026-01-20 09:55:01 -05:00
|
|
|
|
2025-05-16 20:02:49 -04:00
|
|
|
if (!$data) {
|
2026-07-10 11:48:34 -04:00
|
|
|
ob_end_clean();
|
|
|
|
|
http_response_code(400);
|
|
|
|
|
header('Content-Type: application/json');
|
|
|
|
|
echo json_encode(['success' => false, 'error' => 'Invalid JSON data received']);
|
|
|
|
|
exit;
|
2025-05-16 20:02:49 -04:00
|
|
|
}
|
2026-04-13 20:56:10 -04:00
|
|
|
|
2025-05-16 20:02:49 -04:00
|
|
|
if (!isset($data['ticket_id'])) {
|
2026-07-10 11:48:34 -04:00
|
|
|
ob_end_clean();
|
|
|
|
|
http_response_code(400);
|
|
|
|
|
header('Content-Type: application/json');
|
|
|
|
|
echo json_encode(['success' => false, 'error' => 'Missing ticket_id parameter']);
|
|
|
|
|
exit;
|
2025-05-16 20:02:49 -04:00
|
|
|
}
|
2026-04-13 20:56:10 -04:00
|
|
|
|
2026-04-11 12:43:18 -04:00
|
|
|
$ticketId = trim((string)$data['ticket_id']);
|
2026-01-20 09:55:01 -05:00
|
|
|
|
2025-05-16 20:02:49 -04:00
|
|
|
// Initialize controller
|
2026-03-20 21:42:47 -04:00
|
|
|
$controller = new ApiTicketController($conn, $userId, $isAdmin, $currentUser);
|
2026-01-20 09:55:01 -05:00
|
|
|
|
2025-05-16 20:02:49 -04:00
|
|
|
// Update ticket
|
|
|
|
|
$result = $controller->update($ticketId, $data);
|
2026-04-13 20:56:10 -04:00
|
|
|
|
2025-05-16 20:02:49 -04:00
|
|
|
// Discard any output that might have been generated
|
|
|
|
|
ob_end_clean();
|
2026-04-05 12:39:24 -04:00
|
|
|
|
|
|
|
|
// Invalidate stats cache on successful ticket update
|
|
|
|
|
if (!empty($result['success'])) {
|
|
|
|
|
require_once dirname(__DIR__) . '/models/StatsModel.php';
|
|
|
|
|
(new StatsModel($conn))->invalidateCache();
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-16 20:02:49 -04:00
|
|
|
// Return response
|
2026-03-20 21:42:47 -04:00
|
|
|
if (!empty($result['http_status'])) {
|
|
|
|
|
http_response_code($result['http_status']);
|
|
|
|
|
unset($result['http_status']);
|
|
|
|
|
}
|
2025-05-16 20:02:49 -04:00
|
|
|
header('Content-Type: application/json');
|
|
|
|
|
echo json_encode($result);
|
|
|
|
|
} catch (Exception $e) {
|
|
|
|
|
// Discard any output that might have been generated
|
|
|
|
|
ob_end_clean();
|
2026-01-30 18:56:29 -05:00
|
|
|
|
|
|
|
|
// Log error details but don't expose to client
|
|
|
|
|
error_log("Update ticket API error: " . $e->getMessage());
|
|
|
|
|
|
2025-05-16 20:02:49 -04:00
|
|
|
// Return error response
|
|
|
|
|
header('Content-Type: application/json');
|
2025-09-05 11:08:56 -04:00
|
|
|
http_response_code(500);
|
2025-05-16 20:02:49 -04:00
|
|
|
echo json_encode([
|
|
|
|
|
'success' => false,
|
2026-01-30 18:56:29 -05:00
|
|
|
'error' => 'An internal error occurred'
|
2025-05-16 20:02:49 -04:00
|
|
|
]);
|
|
|
|
|
}
|