Files
site_for_glpi/search_knowledge.php

37 lines
994 B
PHP

<?php
ini_set('display_errors', 0);
error_reporting(0);
require_once __DIR__ . '/config.php';
header('Content-Type: application/json; charset=utf-8');
if (empty($_SERVER['HTTP_X_REQUESTED_WITH']) || strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
http_response_code(403);
exit(json_encode(['error' => 'Forbidden']));
}
$user = checkAuth($pdo);
if (!$user) {
http_response_code(401);
exit(json_encode(['error' => 'Unauthorized']));
}
$query = trim($_GET['q'] ?? '');
if (strlen($query) < 2) {
exit(json_encode([]));
}
// Используем полнотекстовый поиск
$results = searchLocalKnowledgeFulltext($pdo, $query, 50);
$output = [];
foreach ($results as $row) {
$output[] = [
'id' => $row['id'],
'title' => htmlspecialchars($row['title']),
'preview' => htmlspecialchars($row['preview']),
'updated_at' => date('d.m.Y', strtotime($row['updated_at']))
];
}
echo json_encode($output);
exit;