Security: - Fix IDOR in delete/update comment (add ticket visibility check) - XSS defense-in-depth in DashboardView active filters - Replace innerHTML with DOM construction in toast.js - Remove redundant real_escape_string in check_duplicates - Add rate limiting to get_template, download_attachment, audit_log, saved_filters, user_preferences endpoints Bug fixes: - Session timeout now reads from config instead of hardcoded 18000 - TicketController uses $GLOBALS['config'] instead of duplicate .env parsing - Add DISCORD_WEBHOOK_URL to centralized config - Cleanup script uses hashmap for O(1) ticket ID lookups Dead code removal (~100 lines): - Remove dead getTicketComments() from TicketModel (wrong bind_param type) - Remove dead getCategories()/getTypes() from DashboardController - Remove ~80 lines dead Discord webhook code from update_ticket API Consolidation: - Create api/bootstrap.php for shared API setup (auth, CSRF, rate limit) - Convert 6 API endpoints to use bootstrap - Extract escapeHtml/getTicketIdFromUrl into shared utils.js - Batch save for user preferences (1 request instead of 7) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
92 lines
2.4 KiB
PHP
92 lines
2.4 KiB
PHP
<?php
|
|
/**
|
|
* Check for duplicate tickets API
|
|
*
|
|
* Searches for tickets with similar titles using LIKE and SOUNDEX
|
|
*/
|
|
|
|
require_once __DIR__ . '/bootstrap.php';
|
|
require_once dirname(__DIR__) . '/helpers/ResponseHelper.php';
|
|
|
|
// Only accept GET requests
|
|
if ($_SERVER['REQUEST_METHOD'] !== 'GET') {
|
|
ResponseHelper::error('Method not allowed', 405);
|
|
}
|
|
|
|
// Get title parameter
|
|
$title = isset($_GET['title']) ? trim($_GET['title']) : '';
|
|
|
|
if (strlen($title) < 5) {
|
|
ResponseHelper::success(['duplicates' => []]);
|
|
}
|
|
|
|
// Search for similar titles
|
|
// Use both LIKE for substring matching and SOUNDEX for phonetic matching
|
|
$duplicates = [];
|
|
|
|
// Prepare search term for LIKE
|
|
$searchTerm = '%' . $title . '%';
|
|
|
|
// Get SOUNDEX of title
|
|
$soundexTitle = soundex($title);
|
|
|
|
// First, search for exact substring matches (case-insensitive)
|
|
$sql = "SELECT ticket_id, title, status, priority, created_at
|
|
FROM tickets
|
|
WHERE (
|
|
title LIKE ?
|
|
OR SOUNDEX(title) = ?
|
|
)
|
|
AND status != 'Closed'
|
|
ORDER BY created_at DESC
|
|
LIMIT 10";
|
|
|
|
$stmt = $conn->prepare($sql);
|
|
$stmt->bind_param("ss", $searchTerm, $soundexTitle);
|
|
$stmt->execute();
|
|
$result = $stmt->get_result();
|
|
|
|
while ($row = $result->fetch_assoc()) {
|
|
// Calculate similarity score
|
|
$similarity = 0;
|
|
|
|
// Check for exact substring match
|
|
if (stripos($row['title'], $title) !== false) {
|
|
$similarity = 90;
|
|
}
|
|
// Check SOUNDEX match
|
|
elseif (soundex($row['title']) === $soundexTitle) {
|
|
$similarity = 70;
|
|
}
|
|
// Check word overlap
|
|
else {
|
|
$titleWords = array_map('strtolower', preg_split('/\s+/', $title));
|
|
$rowWords = array_map('strtolower', preg_split('/\s+/', $row['title']));
|
|
$matchingWords = array_intersect($titleWords, $rowWords);
|
|
$similarity = (count($matchingWords) / max(count($titleWords), 1)) * 60;
|
|
}
|
|
|
|
if ($similarity >= 30) {
|
|
$duplicates[] = [
|
|
'ticket_id' => $row['ticket_id'],
|
|
'title' => $row['title'],
|
|
'status' => $row['status'],
|
|
'priority' => $row['priority'],
|
|
'created_at' => $row['created_at'],
|
|
'similarity' => round($similarity)
|
|
];
|
|
}
|
|
}
|
|
|
|
$stmt->close();
|
|
|
|
// Sort by similarity descending
|
|
usort($duplicates, function($a, $b) {
|
|
return $b['similarity'] - $a['similarity'];
|
|
});
|
|
|
|
// Limit to top 5
|
|
$duplicates = array_slice($duplicates, 0, 5);
|
|
|
|
ResponseHelper::success(['duplicates' => $duplicates]);
|