forked from leftypol/leftypol
65 lines
1.6 KiB
PHP
65 lines
1.6 KiB
PHP
|
<?php
|
||
|
namespace Vichan\Services\Embed;
|
||
|
|
||
|
use RuntimeException;
|
||
|
use Vichan\Context;
|
||
|
use Vichan\Data\Driver\LogDriver;
|
||
|
|
||
|
class EmbedService {
|
||
|
private array $tuples;
|
||
|
private LogDriver $log;
|
||
|
|
||
|
public function __construct(LogDriver $log) {
|
||
|
$this->log = $log;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Undocumented function
|
||
|
*
|
||
|
* @param Context $ctx
|
||
|
* @param string $rawText
|
||
|
* @return void
|
||
|
*/
|
||
|
public function matchEmbed(Context $ctx, string $rawText) {
|
||
|
if (\filter_var($rawText, \FILTER_VALIDATE_URL) === false) {
|
||
|
return null;
|
||
|
}
|
||
|
$rawText = \trim($rawText);
|
||
|
|
||
|
foreach ($this->tuples as $cfg) {
|
||
|
if (!isset($cfg['match_regex'])) {
|
||
|
throw new \RuntimeException('Missing \'match_regex\' field');
|
||
|
}
|
||
|
$match_regex = $cfg['match_regex'];
|
||
|
|
||
|
if (\preg_match($match_regex, $rawText, $matches)) {
|
||
|
if (!isset($cfg['type'])) {
|
||
|
throw new \RuntimeException('Missing \'type\' field');
|
||
|
}
|
||
|
$type = $cfg['type'];
|
||
|
|
||
|
if ($type === 'oembed') {
|
||
|
if (!isset($cfg['provider'])) {
|
||
|
throw new \RuntimeException('Missing \'provider\' field');
|
||
|
}
|
||
|
$provider = $cfg['provider'];
|
||
|
|
||
|
$extractor = $ctx->get(OembedExtractor::class);
|
||
|
$oembed_resp = $extractor->fetch($provider, $rawText);
|
||
|
|
||
|
|
||
|
} elseif ($type === 'regex') {
|
||
|
if (!isset($cfg['thumbnail_url'])) {
|
||
|
throw new \RuntimeException('Missing \'thumbnail_url\' field');
|
||
|
}
|
||
|
$thumbnail_url_regex = $cfg['thumbnail_url'];
|
||
|
// Plz somebody review this.
|
||
|
$thumbnail_url = \preg_replace($match_regex, $thumbnail_url_regex, $rawText);
|
||
|
} else {
|
||
|
$this->log->log(LogDriver::ERROR, "Unknown embed type '$type', ignoring");
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|