YGGo/public/api.php

88 lines
2.1 KiB
PHP
Raw Normal View History

2023-04-23 00:01:51 +00:00
<?php
// Load system dependencies
require_once('../config/app.php');
require_once('../library/curl.php');
require_once('../library/robots.php');
require_once('../library/filter.php');
require_once('../library/parser.php');
require_once('../library/mysql.php');
require_once('../library/sphinxql.php');
2023-04-23 00:25:43 +00:00
if (API_ENABLED) {
2023-04-23 00:01:51 +00:00
2023-04-23 00:25:43 +00:00
// Action
switch (!empty($_GET['action']) ? $_GET['action'] : false) {
2023-04-23 00:01:51 +00:00
2023-04-23 00:25:43 +00:00
// Search API
case 'search';
2023-04-23 00:01:51 +00:00
2023-04-23 00:25:43 +00:00
// Connect database
$db = new MySQL(DB_HOST, DB_PORT, DB_NAME, DB_USERNAME, DB_PASSWORD);
2023-04-23 00:01:51 +00:00
2023-04-23 00:25:43 +00:00
// Connect Sphinx search server
$sphinx = new SphinxQL(SPHINX_HOST, SPHINX_PORT);
2023-04-23 00:01:51 +00:00
2023-04-23 00:25:43 +00:00
// Filter request data
$query = !empty($_GET['query']) ? Filter::url($_GET['query']) : '';
$page = !empty($_GET['page']) ? Filter::url($_GET['page']) : 1;
2023-04-23 00:01:51 +00:00
2023-04-23 00:25:43 +00:00
// Make search request
$sphinxResults = $sphinx->searchHostPages('"' . $query . '"', $page * API_SEARCH_PAGINATION_RESULTS_LIMIT - API_SEARCH_PAGINATION_RESULTS_LIMIT, API_SEARCH_PAGINATION_RESULTS_LIMIT);
$sphinxResultsTotal = $sphinx->searchHostPagesTotal('"' . $query . '"');
2023-04-23 00:01:51 +00:00
2023-04-23 00:25:43 +00:00
// Generate results
$dbResults = [];
2023-04-23 00:01:51 +00:00
2023-04-23 00:25:43 +00:00
foreach ($sphinxResults as $sphinxResult) {
2023-04-23 00:01:51 +00:00
2023-04-23 00:25:43 +00:00
if ($hostPage = $db->getFoundHostPage($sphinxResult->id)) {
2023-04-23 00:01:51 +00:00
2023-04-23 00:25:43 +00:00
$dbResults[] = $hostPage;
}
2023-04-23 00:01:51 +00:00
}
2023-04-23 00:25:43 +00:00
// Make response
$response = [
'status' => true,
'totals' => $sphinxResultsTotal,
'result' => $dbResults,
];
2023-04-23 00:01:51 +00:00
2023-04-23 00:25:43 +00:00
break;
2023-04-23 00:01:51 +00:00
2023-04-23 00:25:43 +00:00
// Host API
case 'hosts';
2023-04-23 00:01:51 +00:00
2023-04-23 00:25:43 +00:00
// Connect database
$db = new MySQL(DB_HOST, DB_PORT, DB_NAME, DB_USERNAME, DB_PASSWORD);
2023-04-23 00:01:51 +00:00
2023-04-23 00:25:43 +00:00
$response = [
'status' => true,
'totals' => $db->getTotalHosts(),
'result' => $db->getAPIHosts(API_HOSTS_FIELDS),
];
2023-04-23 00:01:51 +00:00
2023-04-23 00:25:43 +00:00
break;
2023-04-23 00:01:51 +00:00
2023-04-23 00:25:43 +00:00
default:
2023-04-23 00:01:51 +00:00
2023-04-23 00:25:43 +00:00
$response = [
'status' => false,
'message' => _('Undefined API action request.'),
];
}
} else {
$response = [
'status' => false,
'message' => _('API requests disabled by the node owner.'),
];
2023-04-23 00:01:51 +00:00
}
// Output
header('Content-Type: application/json; charset=utf-8');
echo json_encode($response);