YGGo/public/api.php

175 lines
4.1 KiB
PHP
Raw Normal View History

2023-04-23 00:01:51 +00:00
<?php
2023-04-25 16:35:52 +00:00
// Current version
define('API_VERSION', 0.13);
2023-04-25 16:35:52 +00:00
2023-04-23 00:01:51 +00:00
// Load system dependencies
2023-06-30 11:38:29 +00:00
require_once(__DIR__ . '/../config/app.php');
require_once(__DIR__ . '/../library/filter.php');
require_once(__DIR__ . '/../library/mysql.php');
require_once(__DIR__ . '/../library/sphinxql.php');
2023-04-23 00:01:51 +00:00
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-25 16:35:52 +00:00
if (API_SEARCH_ENABLED) {
2023-08-05 16:39:49 +00:00
// Connect Sphinx search server
try {
$sphinx = new SphinxQL(SPHINX_HOST, SPHINX_PORT);
} catch(Exception $e) {
var_dump($e);
exit;
}
2023-04-25 16:35:52 +00:00
// Connect database
2023-08-05 16:39:49 +00:00
try {
2023-04-23 00:01:51 +00:00
2023-08-05 16:39:49 +00:00
$db = new MySQL(DB_HOST, DB_PORT, DB_NAME, DB_USERNAME, DB_PASSWORD);
2023-04-23 00:01:51 +00:00
2023-08-05 16:39:49 +00:00
} catch(Exception $e) {
var_dump($e);
exit;
}
2023-04-23 00:01:51 +00:00
2023-04-25 16:35:52 +00:00
// Filter request data
2023-05-10 22:45:36 +00:00
$type = !empty($_GET['type']) ? Filter::url($_GET['type']) : 'text';
$mode = !empty($_GET['mode']) ? Filter::url($_GET['mode']) : 'default';
2023-04-25 16:35:52 +00:00
$query = !empty($_GET['query']) ? Filter::url($_GET['query']) : '';
$page = !empty($_GET['page']) ? (int) $_GET['page'] : 1;
2023-04-23 00:01:51 +00:00
// Make search request
$sphinxResultsTotal = $sphinx->searchHostPagesTotal(Filter::searchQuery($query, $mode), $type);
$sphinxResults = $sphinx->searchHostPages(Filter::searchQuery($query, $mode), $type, $page * API_SEARCH_PAGINATION_RESULTS_LIMIT - API_SEARCH_PAGINATION_RESULTS_LIMIT, API_SEARCH_PAGINATION_RESULTS_LIMIT, $sphinxResultsTotal);
2023-04-23 00:01:51 +00:00
2023-04-25 16:35:52 +00:00
// Generate results
$dbResults = [];
2023-04-23 00:01:51 +00:00
2023-04-25 16:35:52 +00:00
foreach ($sphinxResults as $i => $sphinxResult) {
2023-04-23 00:01:51 +00:00
if ($hostPage = $db->getHostPage($sphinxResult->id)) {
2023-04-23 00:01:51 +00:00
if ($host = $db->getHost($hostPage->hostId)) {
2023-04-25 13:53:13 +00:00
$dbResults[$i] = $hostPage;
$dbResults[$i]->url = $host->url . $hostPage->uri;
$dbResults[$i]->weight = $sphinxResult->weight;
}
2023-04-25 16:35:52 +00:00
}
2023-04-23 00:25:43 +00:00
}
2023-04-23 00:01:51 +00:00
2023-04-25 16:35:52 +00:00
// Make response
$response = [
'status' => true,
'totals' => $sphinxResultsTotal,
'result' => $dbResults,
];
} else {
$response = [
'status' => false,
'result' => [],
];
}
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-25 16:35:52 +00:00
if (API_HOSTS_ENABLED) {
2023-04-23 00:01:51 +00:00
2023-04-25 16:35:52 +00:00
// Connect database
2023-08-05 16:39:49 +00:00
try {
$db = new MySQL(DB_HOST, DB_PORT, DB_NAME, DB_USERNAME, DB_PASSWORD);
} catch(Exception $e) {
var_dump($e);
exit;
}
2023-04-25 16:35:52 +00:00
$response = [
'status' => true,
'totals' => $db->getTotalHosts(),
'result' => $db->getAPIHosts(API_HOSTS_FIELDS),
];
} else {
$response = [
'status' => false,
'result' => [],
];
}
break;
// Manifest API
case 'manifest';
if (API_MANIFEST_ENABLED) {
$response = [
'status' => true,
'result' => [
2023-05-03 02:47:02 +00:00
'config' => [
'WEBSITE_DOMAIN' => WEBSITE_DOMAIN,
'DEFAULT_HOST_URL_REGEXP' => DEFAULT_HOST_URL_REGEXP,
// @TODO
2023-05-03 02:47:02 +00:00
],
'api' => [
2023-07-26 10:22:21 +00:00
'version' => (string) API_VERSION,
2023-05-03 02:47:02 +00:00
'manifest' => API_ENABLED && API_MANIFEST_ENABLED ? WEBSITE_DOMAIN . '/api.php?action=manifest' : false,
'search' => API_ENABLED && API_SEARCH_ENABLED ? WEBSITE_DOMAIN . '/api.php?action=search' : false,
'hosts' => API_ENABLED && API_HOSTS_ENABLED ? WEBSITE_DOMAIN . '/api.php?action=hosts' : false,
]
2023-04-25 16:35:52 +00:00
],
];
} else {
$response = [
'status' => false,
'result' => [],
];
}
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);