chore(*.php): use pint for php formatting
This commit is contained in:
parent
bd1855a65e
commit
753f3433ce
40 changed files with 2261 additions and 1900 deletions
183
api/oembed.php
183
api/oembed.php
|
@ -1,111 +1,124 @@
|
|||
<?php
|
||||
|
||||
require_once __DIR__ . '/../bootstrap.php';
|
||||
require_once __DIR__.'/../bootstrap.php';
|
||||
|
||||
use App\Classes\BaseHandler;
|
||||
use GuzzleHttp\Client;
|
||||
|
||||
class OembedHandler extends BaseHandler
|
||||
{
|
||||
public function handleRequest(): void
|
||||
{
|
||||
$requestUrl = $_GET['url'] ?? null;
|
||||
$globals = $this->fetchGlobals();
|
||||
$parsed = $requestUrl ? parse_url($requestUrl) : null;
|
||||
$relativePath = $parsed['path'] ?? null;
|
||||
public function handleRequest(): void
|
||||
{
|
||||
$requestUrl = $_GET['url'] ?? null;
|
||||
$globals = $this->fetchGlobals();
|
||||
$parsed = $requestUrl ? parse_url($requestUrl) : null;
|
||||
$relativePath = $parsed['path'] ?? null;
|
||||
|
||||
if (!$requestUrl || $relativePath === '/') $this->sendResponse($this->buildResponse(
|
||||
$globals['site_name'],
|
||||
$globals['url'],
|
||||
$globals['metadata']['open_graph_image'],
|
||||
$globals,
|
||||
$globals['site_description']
|
||||
));
|
||||
if (! $requestUrl || $relativePath === '/') {
|
||||
$this->sendResponse($this->buildResponse(
|
||||
$globals['site_name'],
|
||||
$globals['url'],
|
||||
$globals['metadata']['open_graph_image'],
|
||||
$globals,
|
||||
$globals['site_description']
|
||||
));
|
||||
}
|
||||
|
||||
if (!$relativePath) $this->sendErrorResponse('Invalid url', 400);
|
||||
if (! $relativePath) {
|
||||
$this->sendErrorResponse('Invalid url', 400);
|
||||
}
|
||||
|
||||
$relativePath = '/' . ltrim($relativePath ?? '', '/');
|
||||
$relativePath = '/'.ltrim($relativePath ?? '', '/');
|
||||
|
||||
if ($relativePath !== '/' && str_ends_with($relativePath, '/')) $relativePath = rtrim($relativePath, '/');
|
||||
if ($relativePath !== '/' && str_ends_with($relativePath, '/')) {
|
||||
$relativePath = rtrim($relativePath, '/');
|
||||
}
|
||||
|
||||
$cacheKey = 'oembed:' . md5($relativePath);
|
||||
$cacheKey = 'oembed:'.md5($relativePath);
|
||||
|
||||
if ($this->cache && $this->cache->exists($cacheKey)) {
|
||||
$cachedItem = json_decode($this->cache->get($cacheKey), true);
|
||||
if ($this->cache && $this->cache->exists($cacheKey)) {
|
||||
$cachedItem = json_decode($this->cache->get($cacheKey), true);
|
||||
|
||||
$this->sendResponse($this->buildResponse(
|
||||
$cachedItem['title'],
|
||||
$cachedItem['url'],
|
||||
$cachedItem['image_url'],
|
||||
$globals,
|
||||
$cachedItem['description'] ?? ''
|
||||
));
|
||||
$this->sendResponse($this->buildResponse(
|
||||
$cachedItem['title'],
|
||||
$cachedItem['url'],
|
||||
$cachedItem['image_url'],
|
||||
$globals,
|
||||
$cachedItem['description'] ?? ''
|
||||
));
|
||||
}
|
||||
|
||||
$results = $this->fetchFromApi('optimized_oembed', 'url=eq.'.urlencode($relativePath));
|
||||
|
||||
if (! empty($results)) {
|
||||
$item = $results[0];
|
||||
|
||||
if ($this->cache) {
|
||||
$this->cache->setex($cacheKey, 300, json_encode($item));
|
||||
}
|
||||
|
||||
$this->sendResponse($this->buildResponse(
|
||||
$item['title'],
|
||||
$item['url'],
|
||||
$item['image_url'],
|
||||
$globals,
|
||||
$item['description'] ?? ''
|
||||
));
|
||||
}
|
||||
|
||||
$segments = explode('/', trim($relativePath, '/'));
|
||||
|
||||
if (count($segments) === 1 && $segments[0] !== '') {
|
||||
$title = ucwords(str_replace('-', ' ', $segments[0])).' • '.$globals['author'];
|
||||
|
||||
$this->sendResponse($this->buildResponse(
|
||||
$title,
|
||||
$relativePath,
|
||||
$globals['metadata']['open_graph_image'],
|
||||
$globals
|
||||
));
|
||||
}
|
||||
|
||||
$this->sendErrorResponse('No match found', 404);
|
||||
}
|
||||
|
||||
$results = $this->fetchFromApi('optimized_oembed', 'url=eq.' . urlencode($relativePath));
|
||||
private function buildResponse(string $title, string $url, string $imagePath, array $globals, string $description = ''): array
|
||||
{
|
||||
$safeDescription = truncateText(strip_tags(parseMarkdown($description)), 175);
|
||||
$html = '<p><a href="'.htmlspecialchars($url).'">'.htmlspecialchars($title).'</a></p>';
|
||||
|
||||
if (!empty($results)) {
|
||||
$item = $results[0];
|
||||
if ($description) {
|
||||
$html .= '<p>'.htmlspecialchars($safeDescription, ENT_QUOTES, 'UTF-8').'</p>';
|
||||
}
|
||||
|
||||
if ($this->cache) $this->cache->setex($cacheKey, 300, json_encode($item));
|
||||
|
||||
$this->sendResponse($this->buildResponse(
|
||||
$item['title'],
|
||||
$item['url'],
|
||||
$item['image_url'],
|
||||
$globals,
|
||||
$item['description'] ?? ''
|
||||
));
|
||||
return [
|
||||
'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'.$imagePath,
|
||||
'html' => $html ?? $globals['site_description'],
|
||||
'description' => $safeDescription ?? $globals['site_description'],
|
||||
];
|
||||
}
|
||||
|
||||
$segments = explode('/', trim($relativePath, '/'));
|
||||
private function fetchGlobals(): array
|
||||
{
|
||||
$cacheKey = 'globals_data';
|
||||
|
||||
if (count($segments) === 1 && $segments[0] !== '') {
|
||||
$title = ucwords(str_replace('-', ' ', $segments[0])) . ' • ' . $globals['author'];
|
||||
if ($this->cache && $this->cache->exists($cacheKey)) {
|
||||
return json_decode($this->cache->get($cacheKey), true);
|
||||
}
|
||||
|
||||
$this->sendResponse($this->buildResponse(
|
||||
$title,
|
||||
$relativePath,
|
||||
$globals['metadata']['open_graph_image'],
|
||||
$globals
|
||||
));
|
||||
$globals = $this->fetchFromApi('optimized_globals', 'limit=1')[0];
|
||||
|
||||
if ($this->cache) {
|
||||
$this->cache->setex($cacheKey, 3600, json_encode($globals));
|
||||
}
|
||||
|
||||
return $globals;
|
||||
}
|
||||
|
||||
$this->sendErrorResponse('No match found', 404);
|
||||
}
|
||||
|
||||
private function buildResponse(string $title, string $url, string $imagePath, array $globals, string $description = ''): array
|
||||
{
|
||||
$safeDescription = truncateText(strip_tags(parseMarkdown($description)), 175);
|
||||
$html = '<p><a href="' . htmlspecialchars($url) . '">' . htmlspecialchars($title) . '</a></p>';
|
||||
|
||||
if ($description) $html .= '<p>' . htmlspecialchars($safeDescription, ENT_QUOTES, 'UTF-8') . '</p>';
|
||||
|
||||
return [
|
||||
'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' . $imagePath,
|
||||
'html' => $html ?? $globals['site_description'],
|
||||
'description' => $safeDescription ?? $globals['site_description'],
|
||||
];
|
||||
}
|
||||
|
||||
private function fetchGlobals(): array
|
||||
{
|
||||
$cacheKey = 'globals_data';
|
||||
|
||||
if ($this->cache && $this->cache->exists($cacheKey)) return json_decode($this->cache->get($cacheKey), true);
|
||||
|
||||
$globals = $this->fetchFromApi('optimized_globals', 'limit=1')[0];
|
||||
|
||||
if ($this->cache) $this->cache->setex($cacheKey, 3600, json_encode($globals));
|
||||
|
||||
return $globals;
|
||||
}
|
||||
}
|
||||
|
||||
$handler = new OembedHandler();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue