Add performance, security, and reliability improvements
- Consolidate all 20 API files to use centralized Database helper - Add optimistic locking to ticket updates to prevent concurrent conflicts - Add caching to StatsModel (60s TTL) for dashboard performance - Add health check endpoint (api/health.php) for monitoring - Improve rate limit cleanup with cron script and efficient DirectoryIterator - Enable rate limit response headers (X-RateLimit-*) - Add audit logging for workflow transitions - Log Discord webhook failures instead of silencing - Fix visibility check on export_tickets.php - Add database migration system with performance indexes - Fix cron recurring tickets to use assignTicket method Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -13,6 +13,8 @@ RateLimitMiddleware::apply('api');
|
||||
|
||||
try {
|
||||
require_once dirname(__DIR__) . '/config/config.php';
|
||||
require_once dirname(__DIR__) . '/helpers/Database.php';
|
||||
require_once dirname(__DIR__) . '/models/AuditLogModel.php';
|
||||
|
||||
// Check authentication
|
||||
session_start();
|
||||
@@ -40,16 +42,12 @@ try {
|
||||
}
|
||||
}
|
||||
|
||||
$conn = new mysqli(
|
||||
$GLOBALS['config']['DB_HOST'],
|
||||
$GLOBALS['config']['DB_USER'],
|
||||
$GLOBALS['config']['DB_PASS'],
|
||||
$GLOBALS['config']['DB_NAME']
|
||||
);
|
||||
// Use centralized database connection
|
||||
$conn = Database::getConnection();
|
||||
|
||||
if ($conn->connect_error) {
|
||||
throw new Exception("Database connection failed");
|
||||
}
|
||||
// Initialize audit log
|
||||
$auditLog = new AuditLogModel($conn);
|
||||
$userId = $_SESSION['user']['user_id'];
|
||||
|
||||
header('Content-Type: application/json');
|
||||
|
||||
@@ -92,8 +90,18 @@ try {
|
||||
);
|
||||
|
||||
if ($stmt->execute()) {
|
||||
$transitionId = $conn->insert_id;
|
||||
WorkflowModel::clearCache(); // Clear workflow cache
|
||||
echo json_encode(['success' => true, 'transition_id' => $conn->insert_id]);
|
||||
|
||||
// Audit log: workflow transition created
|
||||
$auditLog->log($userId, 'create', 'workflow_transition', (string)$transitionId, [
|
||||
'from_status' => $data['from_status'],
|
||||
'to_status' => $data['to_status'],
|
||||
'requires_comment' => $data['requires_comment'] ?? 0,
|
||||
'requires_admin' => $data['requires_admin'] ?? 0
|
||||
]);
|
||||
|
||||
echo json_encode(['success' => true, 'transition_id' => $transitionId]);
|
||||
} else {
|
||||
echo json_encode(['success' => false, 'error' => $stmt->error]);
|
||||
}
|
||||
@@ -123,6 +131,14 @@ try {
|
||||
$success = $stmt->execute();
|
||||
if ($success) {
|
||||
WorkflowModel::clearCache(); // Clear workflow cache
|
||||
|
||||
// Audit log: workflow transition updated
|
||||
$auditLog->log($userId, 'update', 'workflow_transition', (string)$id, [
|
||||
'from_status' => $data['from_status'],
|
||||
'to_status' => $data['to_status'],
|
||||
'requires_comment' => $data['requires_comment'] ?? 0,
|
||||
'requires_admin' => $data['requires_admin'] ?? 0
|
||||
]);
|
||||
}
|
||||
echo json_encode(['success' => $success]);
|
||||
$stmt->close();
|
||||
@@ -134,11 +150,25 @@ try {
|
||||
exit;
|
||||
}
|
||||
|
||||
// Get transition details before deletion for audit log
|
||||
$getStmt = $conn->prepare("SELECT from_status, to_status FROM status_transitions WHERE transition_id = ?");
|
||||
$getStmt->bind_param('i', $id);
|
||||
$getStmt->execute();
|
||||
$getResult = $getStmt->get_result();
|
||||
$transitionData = $getResult->fetch_assoc();
|
||||
$getStmt->close();
|
||||
|
||||
$stmt = $conn->prepare("DELETE FROM status_transitions WHERE transition_id = ?");
|
||||
$stmt->bind_param('i', $id);
|
||||
$success = $stmt->execute();
|
||||
if ($success) {
|
||||
WorkflowModel::clearCache(); // Clear workflow cache
|
||||
|
||||
// Audit log: workflow transition deleted
|
||||
$auditLog->log($userId, 'delete', 'workflow_transition', (string)$id, [
|
||||
'from_status' => $transitionData['from_status'] ?? 'unknown',
|
||||
'to_status' => $transitionData['to_status'] ?? 'unknown'
|
||||
]);
|
||||
}
|
||||
echo json_encode(['success' => $success]);
|
||||
$stmt->close();
|
||||
@@ -149,8 +179,6 @@ try {
|
||||
echo json_encode(['success' => false, 'error' => 'Method not allowed']);
|
||||
}
|
||||
|
||||
$conn->close();
|
||||
|
||||
} catch (Exception $e) {
|
||||
http_response_code(500);
|
||||
echo json_encode(['success' => false, 'error' => $e->getMessage()]);
|
||||
|
||||
Reference in New Issue
Block a user