Quick Wins: - Feature 1: Ticket linking in comments (#123456789 auto-links) - Feature 6: Checkbox click area fix (click anywhere in cell) - Feature 7: User groups display in settings modal UI Enhancements: - Feature 4: Collapsible sidebar with localStorage persistence - Feature 5: Inline ticket preview popup on hover (300ms delay) - Feature 2: Mobile responsive improvements (44px touch targets, iOS zoom fix) Major Features: - Feature 3: Kanban card view with status columns (toggle with localStorage) - Feature 9: API key generation admin panel (/admin/api-keys) - Feature 8: Ticket visibility levels (public/internal/confidential) New files: - views/admin/ApiKeysView.php - api/generate_api_key.php - api/revoke_api_key.php - migrations/008_ticket_visibility.sql Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
217 lines
7.1 KiB
PHP
217 lines
7.1 KiB
PHP
<?php
|
|
/**
|
|
* Ticket Dependencies API
|
|
*/
|
|
|
|
// Immediately set JSON header and start output buffering
|
|
ob_start();
|
|
header('Content-Type: application/json');
|
|
|
|
// Register shutdown function to catch fatal errors
|
|
register_shutdown_function(function() {
|
|
$error = error_get_last();
|
|
if ($error && in_array($error['type'], [E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR])) {
|
|
// Log detailed error server-side
|
|
error_log('Fatal error in ticket_dependencies.php: ' . $error['message'] . ' in ' . $error['file'] . ':' . $error['line']);
|
|
ob_end_clean();
|
|
http_response_code(500);
|
|
header('Content-Type: application/json');
|
|
echo json_encode([
|
|
'success' => false,
|
|
'error' => 'A server error occurred'
|
|
]);
|
|
}
|
|
});
|
|
|
|
ini_set('display_errors', 0);
|
|
error_reporting(E_ALL);
|
|
|
|
// Custom error handler
|
|
set_error_handler(function($errno, $errstr, $errfile, $errline) {
|
|
// Log detailed error server-side
|
|
error_log("PHP Error in ticket_dependencies.php: $errstr in $errfile:$errline");
|
|
ob_end_clean();
|
|
http_response_code(500);
|
|
header('Content-Type: application/json');
|
|
echo json_encode([
|
|
'success' => false,
|
|
'error' => 'A server error occurred'
|
|
]);
|
|
exit;
|
|
});
|
|
|
|
// Custom exception handler
|
|
set_exception_handler(function($e) {
|
|
// Log detailed error server-side
|
|
error_log('Exception in ticket_dependencies.php: ' . $e->getMessage() . ' in ' . $e->getFile() . ':' . $e->getLine());
|
|
ob_end_clean();
|
|
http_response_code(500);
|
|
header('Content-Type: application/json');
|
|
echo json_encode([
|
|
'success' => false,
|
|
'error' => 'A server error occurred'
|
|
]);
|
|
exit;
|
|
});
|
|
|
|
// 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__) . '/models/DependencyModel.php';
|
|
require_once dirname(__DIR__) . '/models/AuditLogModel.php';
|
|
require_once dirname(__DIR__) . '/helpers/ResponseHelper.php';
|
|
|
|
header('Content-Type: application/json');
|
|
|
|
// Check authentication
|
|
if (!isset($_SESSION['user']) || !isset($_SESSION['user']['user_id'])) {
|
|
ResponseHelper::unauthorized();
|
|
}
|
|
|
|
$userId = $_SESSION['user']['user_id'];
|
|
|
|
// CSRF Protection for POST/DELETE
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST' || $_SERVER['REQUEST_METHOD'] === 'DELETE') {
|
|
require_once dirname(__DIR__) . '/middleware/CsrfMiddleware.php';
|
|
$csrfToken = $_SERVER['HTTP_X_CSRF_TOKEN'] ?? '';
|
|
if (!CsrfMiddleware::validateToken($csrfToken)) {
|
|
ResponseHelper::forbidden('Invalid CSRF token');
|
|
}
|
|
}
|
|
|
|
// Create database connection
|
|
$conn = new mysqli(
|
|
$GLOBALS['config']['DB_HOST'],
|
|
$GLOBALS['config']['DB_USER'],
|
|
$GLOBALS['config']['DB_PASS'],
|
|
$GLOBALS['config']['DB_NAME']
|
|
);
|
|
|
|
if ($conn->connect_error) {
|
|
ResponseHelper::serverError('Database connection failed');
|
|
}
|
|
|
|
// Check if ticket_dependencies table exists
|
|
$tableCheck = $conn->query("SHOW TABLES LIKE 'ticket_dependencies'");
|
|
if ($tableCheck->num_rows === 0) {
|
|
ResponseHelper::serverError('Ticket dependencies feature not available. The ticket_dependencies table does not exist. Please run the migration.');
|
|
}
|
|
|
|
try {
|
|
$dependencyModel = new DependencyModel($conn);
|
|
$auditLog = new AuditLogModel($conn);
|
|
} catch (Exception $e) {
|
|
error_log('Failed to initialize models in ticket_dependencies.php: ' . $e->getMessage());
|
|
ResponseHelper::serverError('Failed to initialize required components');
|
|
}
|
|
|
|
$method = $_SERVER['REQUEST_METHOD'];
|
|
|
|
try {
|
|
switch ($method) {
|
|
case 'GET':
|
|
// Get dependencies for a ticket
|
|
$ticketId = $_GET['ticket_id'] ?? null;
|
|
|
|
if (!$ticketId) {
|
|
ResponseHelper::error('Ticket ID required');
|
|
}
|
|
|
|
try {
|
|
$dependencies = $dependencyModel->getDependencies($ticketId);
|
|
$dependents = $dependencyModel->getDependentTickets($ticketId);
|
|
} catch (Exception $e) {
|
|
error_log('Query error in ticket_dependencies.php GET: ' . $e->getMessage());
|
|
ResponseHelper::serverError('Failed to retrieve dependencies');
|
|
}
|
|
|
|
ResponseHelper::success([
|
|
'dependencies' => $dependencies,
|
|
'dependents' => $dependents
|
|
]);
|
|
break;
|
|
|
|
case 'POST':
|
|
// Add a new dependency
|
|
$data = json_decode(file_get_contents('php://input'), true);
|
|
|
|
$ticketId = $data['ticket_id'] ?? null;
|
|
$dependsOnId = $data['depends_on_id'] ?? null;
|
|
$type = $data['dependency_type'] ?? 'blocks';
|
|
|
|
if (!$ticketId || !$dependsOnId) {
|
|
ResponseHelper::error('Both ticket_id and depends_on_id are required');
|
|
}
|
|
|
|
$result = $dependencyModel->addDependency($ticketId, $dependsOnId, $type, $userId);
|
|
|
|
if ($result['success']) {
|
|
// Log to audit
|
|
$auditLog->log($userId, 'create', 'dependency', (string)$result['dependency_id'], [
|
|
'ticket_id' => $ticketId,
|
|
'depends_on_id' => $dependsOnId,
|
|
'type' => $type
|
|
]);
|
|
|
|
ResponseHelper::created($result);
|
|
} else {
|
|
ResponseHelper::error($result['error']);
|
|
}
|
|
break;
|
|
|
|
case 'DELETE':
|
|
// Remove a dependency
|
|
$data = json_decode(file_get_contents('php://input'), true);
|
|
|
|
$dependencyId = $data['dependency_id'] ?? null;
|
|
|
|
// Alternative: delete by ticket IDs
|
|
if (!$dependencyId && isset($data['ticket_id']) && isset($data['depends_on_id'])) {
|
|
$ticketId = $data['ticket_id'];
|
|
$dependsOnId = $data['depends_on_id'];
|
|
$type = $data['dependency_type'] ?? 'blocks';
|
|
|
|
$result = $dependencyModel->removeDependencyByTickets($ticketId, $dependsOnId, $type);
|
|
|
|
if ($result) {
|
|
$auditLog->log($userId, 'delete', 'dependency', null, [
|
|
'ticket_id' => $ticketId,
|
|
'depends_on_id' => $dependsOnId,
|
|
'type' => $type
|
|
]);
|
|
ResponseHelper::success([], 'Dependency removed');
|
|
} else {
|
|
ResponseHelper::error('Failed to remove dependency');
|
|
}
|
|
} elseif ($dependencyId) {
|
|
$result = $dependencyModel->removeDependency($dependencyId);
|
|
|
|
if ($result) {
|
|
$auditLog->log($userId, 'delete', 'dependency', (string)$dependencyId);
|
|
ResponseHelper::success([], 'Dependency removed');
|
|
} else {
|
|
ResponseHelper::error('Failed to remove dependency');
|
|
}
|
|
} else {
|
|
ResponseHelper::error('Dependency ID or ticket IDs required');
|
|
}
|
|
break;
|
|
|
|
default:
|
|
ResponseHelper::error('Method not allowed', 405);
|
|
}
|
|
} catch (Exception $e) {
|
|
// Log detailed error server-side
|
|
error_log('Ticket dependencies API error: ' . $e->getMessage() . ' in ' . $e->getFile() . ':' . $e->getLine());
|
|
ResponseHelper::serverError('An error occurred while processing the dependency request');
|
|
}
|
|
|
|
$conn->close();
|