79 lines
2.6 KiB
PHP
79 lines
2.6 KiB
PHP
<?php
|
|
|
|
require __DIR__ . "/Classes/BaseHandler.php";
|
|
|
|
use App\Classes\BaseHandler;
|
|
use GuzzleHttp\Client;
|
|
|
|
class OembedHandler extends BaseHandler
|
|
{
|
|
public function handleRequest(): void
|
|
{
|
|
$requestUrl = $_GET['url'] ?? null;
|
|
$globals = $this->fetchGlobals();
|
|
|
|
if (!$requestUrl) {
|
|
$this->sendResponse([
|
|
'version' => '1.0',
|
|
'type' => 'link',
|
|
'title' => $globals['site_name'],
|
|
'author_name' => $globals['author'],
|
|
'provider_name' => $globals['site_name'],
|
|
'provider_url' => $globals['url'],
|
|
'thumbnail_url' => $globals['url'] . '/og/w800' . $globals['avatar'],
|
|
'html' => '<a href="' . $globals['url'] . '">' . $globals['site_name'] . '</a>',
|
|
]);
|
|
}
|
|
|
|
$parsed = parse_url($requestUrl);
|
|
$relativePath = $parsed['path'] ?? null;
|
|
if (!$relativePath) $this->sendErrorResponse('Invalid url', 400);
|
|
if ($relativePath !== '/' && str_ends_with($relativePath, '/')) $relativePath = rtrim($relativePath, '/');
|
|
|
|
$results = $this->fetchFromApi('optimized_oembed', 'url=eq.' . urlencode($relativePath));
|
|
if (!empty($results)) {
|
|
$item = $results[0];
|
|
$this->sendResponse([
|
|
'version' => '1.0',
|
|
'type' => 'link',
|
|
'title' => $item['title'],
|
|
'author_name' => $globals['author'],
|
|
'provider_name' => $globals['site_name'],
|
|
'provider_url' => $globals['url'],
|
|
'thumbnail_url' => $globals['url'] . '/og/w800' . $item['image_url'],
|
|
'html' => '<a href="' . htmlspecialchars($item['url']) . '">' . htmlspecialchars($item['title']) . '</a>',
|
|
]);
|
|
}
|
|
|
|
$segments = explode('/', trim($relativePath, '/'));
|
|
if (count($segments) === 1 && $segments[0] !== '') {
|
|
$title = ucwords(str_replace('-', ' ', $segments[0])) . ' • ' . $globals['author'];
|
|
$this->sendResponse([
|
|
'version' => '1.0',
|
|
'type' => 'link',
|
|
'title' => $title,
|
|
'author_name' => $globals['author'],
|
|
'provider_name' => $globals['site_name'],
|
|
'provider_url' => $globals['url'],
|
|
'thumbnail_url' => $globals['url'] . '/og/w800' . $globals['avatar'],
|
|
'html' => '<a href="' . htmlspecialchars($relativePath) . '">' . htmlspecialchars($title) . '</a>',
|
|
]);
|
|
}
|
|
|
|
$this->sendErrorResponse('No match found', 404);
|
|
}
|
|
|
|
private function fetchGlobals(): array
|
|
{
|
|
$globals = $this->fetchFromApi('optimized_globals', 'limit=1');
|
|
return $globals[0] ?? [
|
|
'author' => 'Cory Dransfeldt',
|
|
'site_name' => 'coryd.dev',
|
|
'url' => 'https://www.coryd.dev',
|
|
'avatar' => ''
|
|
];
|
|
}
|
|
}
|
|
|
|
$handler = new OembedHandler();
|
|
$handler->handleRequest();
|