Files
SimpleNav/admin/fetch_meta.php
T

37 lines
1.2 KiB
PHP
Raw Normal View History

<?php
/**
* 简航 SimpleNav —— 添加/编辑网站时自动抓取元信息(供后台 AJAX 调用)
* 返回:{ ok, name, short, icon, url }
*/
declare(strict_types=1);
require_once __DIR__ . '/_auth.php';
require_login();
header('Content-Type: application/json; charset=utf-8');
$url = isset($_GET['url']) ? norm_url((string)$_GET['url']) : '';
if ($url === '') {
echo json_encode(['ok' => false, 'msg' => '缺少网址'], JSON_UNESCAPED_UNICODE);
exit;
}
$meta = fetch_site_meta($url);
$domain = domain_of($meta['final_url'] ?: $url);
$iconFile = '';
if (!empty($meta['icon'])) {
$iconFile = download_icon($meta['icon'], $domain);
}
if ($iconFile === '' && $domain !== '') {
// 兜底再试一次根目录 favicon.ico
$iconFile = download_icon('http://' . $domain . '/favicon.ico', $domain);
}
echo json_encode([
'ok' => true,
'url' => $url,
'name' => $meta['name'] !== '' ? $meta['name'] : ($meta['title'] !== '' ? $meta['title'] : $domain),
'short' => $meta['name'] !== '' ? $meta['name'] : $domain,
'icon' => $iconFile,
'icon_url' => $iconFile !== '' ? icon_src($iconFile) : '',
], JSON_UNESCAPED_UNICODE);