2026-01-20 09:55:01 -05:00
|
|
|
<?php
|
2026-04-13 20:56:10 -04:00
|
|
|
|
2026-01-20 09:55:01 -05:00
|
|
|
/**
|
|
|
|
|
* Custom Fields Management API
|
|
|
|
|
* CRUD operations for custom field definitions
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
ini_set('display_errors', 0);
|
|
|
|
|
error_reporting(E_ALL);
|
|
|
|
|
|
|
|
|
|
require_once dirname(__DIR__) . '/middleware/RateLimitMiddleware.php';
|
|
|
|
|
RateLimitMiddleware::apply('api');
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
require_once dirname(__DIR__) . '/config/config.php';
|
2026-01-30 14:39:13 -05:00
|
|
|
require_once dirname(__DIR__) . '/helpers/Database.php';
|
2026-01-20 09:55:01 -05:00
|
|
|
require_once dirname(__DIR__) . '/models/CustomFieldModel.php';
|
2026-07-10 12:26:39 -04:00
|
|
|
require_once dirname(__DIR__) . '/models/AuditLogModel.php';
|
2026-01-20 09:55:01 -05:00
|
|
|
|
|
|
|
|
// Check authentication
|
2026-04-13 20:56:10 -04:00
|
|
|
if (session_status() === PHP_SESSION_NONE) {
|
|
|
|
|
session_start();
|
|
|
|
|
}
|
2026-01-20 09:55:01 -05:00
|
|
|
if (!isset($_SESSION['user']) || !isset($_SESSION['user']['user_id'])) {
|
|
|
|
|
http_response_code(401);
|
|
|
|
|
echo json_encode(['success' => false, 'error' => 'Authentication required']);
|
|
|
|
|
exit;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check admin privileges for write operations
|
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] !== 'GET' && !$_SESSION['user']['is_admin']) {
|
|
|
|
|
http_response_code(403);
|
|
|
|
|
echo json_encode(['success' => false, 'error' => 'Admin privileges required']);
|
|
|
|
|
exit;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// CSRF Protection for write operations
|
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] !== 'GET') {
|
|
|
|
|
require_once dirname(__DIR__) . '/middleware/CsrfMiddleware.php';
|
|
|
|
|
$csrfToken = $_SERVER['HTTP_X_CSRF_TOKEN'] ?? '';
|
|
|
|
|
if (!CsrfMiddleware::validateToken($csrfToken)) {
|
|
|
|
|
http_response_code(403);
|
|
|
|
|
echo json_encode(['success' => false, 'error' => 'Invalid CSRF token']);
|
|
|
|
|
exit;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-30 14:39:13 -05:00
|
|
|
// Use centralized database connection
|
|
|
|
|
$conn = Database::getConnection();
|
2026-01-20 09:55:01 -05:00
|
|
|
|
|
|
|
|
header('Content-Type: application/json');
|
|
|
|
|
|
|
|
|
|
$model = new CustomFieldModel($conn);
|
2026-07-10 12:26:39 -04:00
|
|
|
$auditLog = new AuditLogModel($conn);
|
|
|
|
|
$currentUserId = $_SESSION['user']['user_id'];
|
2026-01-20 09:55:01 -05:00
|
|
|
$method = $_SERVER['REQUEST_METHOD'];
|
|
|
|
|
$id = isset($_GET['id']) ? (int)$_GET['id'] : null;
|
|
|
|
|
$category = isset($_GET['category']) ? $_GET['category'] : null;
|
|
|
|
|
|
|
|
|
|
switch ($method) {
|
|
|
|
|
case 'GET':
|
|
|
|
|
if ($id) {
|
|
|
|
|
$field = $model->getDefinition($id);
|
|
|
|
|
echo json_encode(['success' => (bool)$field, 'field' => $field]);
|
|
|
|
|
} else {
|
|
|
|
|
// Get all definitions, optionally filtered by category
|
|
|
|
|
$activeOnly = !isset($_GET['include_inactive']);
|
|
|
|
|
$fields = $model->getAllDefinitions($category, $activeOnly);
|
|
|
|
|
echo json_encode(['success' => true, 'fields' => $fields]);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 'POST':
|
|
|
|
|
$data = json_decode(file_get_contents('php://input'), true);
|
2026-03-29 18:26:01 -04:00
|
|
|
if (!is_array($data)) {
|
|
|
|
|
http_response_code(400);
|
|
|
|
|
echo json_encode(['success' => false, 'error' => 'Invalid JSON']);
|
|
|
|
|
exit;
|
|
|
|
|
}
|
2026-01-20 09:55:01 -05:00
|
|
|
$result = $model->createDefinition($data);
|
2026-07-10 12:26:39 -04:00
|
|
|
if (!empty($result['success'])) {
|
|
|
|
|
$auditLog->log($currentUserId, 'create', 'custom_field', (string)($result['field_id'] ?? ''), [
|
|
|
|
|
'field_name' => $data['field_name'] ?? null,
|
|
|
|
|
'field_label' => $data['field_label'] ?? null,
|
|
|
|
|
'field_type' => $data['field_type'] ?? null
|
|
|
|
|
]);
|
|
|
|
|
}
|
2026-01-20 09:55:01 -05:00
|
|
|
echo json_encode($result);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 'PUT':
|
|
|
|
|
if (!$id) {
|
2026-03-29 18:26:01 -04:00
|
|
|
http_response_code(400);
|
2026-01-20 09:55:01 -05:00
|
|
|
echo json_encode(['success' => false, 'error' => 'ID required']);
|
|
|
|
|
exit;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$data = json_decode(file_get_contents('php://input'), true);
|
2026-03-29 18:26:01 -04:00
|
|
|
if (!is_array($data)) {
|
|
|
|
|
http_response_code(400);
|
|
|
|
|
echo json_encode(['success' => false, 'error' => 'Invalid JSON']);
|
|
|
|
|
exit;
|
|
|
|
|
}
|
2026-01-20 09:55:01 -05:00
|
|
|
$result = $model->updateDefinition($id, $data);
|
2026-07-10 12:26:39 -04:00
|
|
|
if (!empty($result['success'])) {
|
|
|
|
|
$auditLog->log($currentUserId, 'update', 'custom_field', (string)$id, [
|
|
|
|
|
'entity' => 'custom_field',
|
|
|
|
|
'field_name' => $data['field_name'] ?? null,
|
|
|
|
|
'field_label' => $data['field_label'] ?? null,
|
|
|
|
|
'field_type' => $data['field_type'] ?? null
|
|
|
|
|
]);
|
|
|
|
|
}
|
2026-01-20 09:55:01 -05:00
|
|
|
echo json_encode($result);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 'DELETE':
|
|
|
|
|
if (!$id) {
|
2026-03-29 18:26:01 -04:00
|
|
|
http_response_code(400);
|
2026-01-20 09:55:01 -05:00
|
|
|
echo json_encode(['success' => false, 'error' => 'ID required']);
|
|
|
|
|
exit;
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-10 12:26:39 -04:00
|
|
|
$toDelete = $model->getDefinition($id);
|
2026-01-20 09:55:01 -05:00
|
|
|
$result = $model->deleteDefinition($id);
|
2026-07-10 12:26:39 -04:00
|
|
|
if (!empty($result['success'])) {
|
|
|
|
|
$auditLog->log($currentUserId, 'delete', 'custom_field', (string)$id, [
|
|
|
|
|
'entity' => 'custom_field',
|
|
|
|
|
'field_name' => $toDelete['field_name'] ?? 'unknown'
|
|
|
|
|
]);
|
|
|
|
|
}
|
2026-01-20 09:55:01 -05:00
|
|
|
echo json_encode($result);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
http_response_code(405);
|
|
|
|
|
echo json_encode(['success' => false, 'error' => 'Method not allowed']);
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception $e) {
|
2026-01-30 18:56:29 -05:00
|
|
|
error_log("Custom fields API error: " . $e->getMessage());
|
2026-01-20 09:55:01 -05:00
|
|
|
http_response_code(500);
|
2026-01-30 18:56:29 -05:00
|
|
|
echo json_encode(['success' => false, 'error' => 'An internal error occurred']);
|
2026-01-20 09:55:01 -05:00
|
|
|
}
|