2024-02-02 21:56:25 +01:00
|
|
|
<?php
|
|
|
|
|
2024-02-19 01:30:12 +01:00
|
|
|
namespace Wallabag\ExpressionLanguage;
|
2024-02-02 21:56:25 +01:00
|
|
|
|
2024-11-19 23:30:18 +01:00
|
|
|
use Symfony\Component\DomCrawler\Crawler;
|
2024-02-02 21:56:25 +01:00
|
|
|
use Symfony\Component\ExpressionLanguage\ExpressionFunction;
|
|
|
|
use Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface;
|
2024-12-23 00:02:59 +01:00
|
|
|
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
2024-02-02 21:56:25 +01:00
|
|
|
|
|
|
|
class AuthenticatorProvider implements ExpressionFunctionProviderInterface
|
|
|
|
{
|
2024-12-23 00:02:59 +01:00
|
|
|
private HttpClientInterface $requestHtmlFunctionClient;
|
2024-02-02 21:56:25 +01:00
|
|
|
|
2024-12-23 00:02:59 +01:00
|
|
|
public function __construct(HttpClientInterface $requestHtmlFunctionClient)
|
2024-02-02 21:56:25 +01:00
|
|
|
{
|
2024-12-23 00:02:59 +01:00
|
|
|
$this->requestHtmlFunctionClient = $requestHtmlFunctionClient;
|
2024-02-02 21:56:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getFunctions(): array
|
|
|
|
{
|
|
|
|
$result = [
|
|
|
|
$this->getRequestHtmlFunction(),
|
|
|
|
$this->getXpathFunction(),
|
|
|
|
$this->getPregMatchFunction(),
|
|
|
|
];
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getRequestHtmlFunction()
|
|
|
|
{
|
|
|
|
return new ExpressionFunction(
|
|
|
|
'request_html',
|
2025-04-05 13:08:13 +02:00
|
|
|
function (): void {
|
2024-02-02 21:56:25 +01:00
|
|
|
throw new \Exception('Not supported');
|
|
|
|
},
|
2024-12-22 23:51:40 +01:00
|
|
|
function (array $arguments, $uri) {
|
2024-12-23 00:02:59 +01:00
|
|
|
return $this->requestHtmlFunctionClient->request('GET', $uri)->getContent();
|
2024-02-02 21:56:25 +01:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getPregMatchFunction()
|
|
|
|
{
|
|
|
|
return new ExpressionFunction(
|
|
|
|
'preg_match',
|
2025-04-05 13:08:13 +02:00
|
|
|
function (): void {
|
2024-02-02 21:56:25 +01:00
|
|
|
throw new \Exception('Not supported');
|
|
|
|
},
|
|
|
|
function (array $arguments, $pattern, $html) {
|
|
|
|
preg_match($pattern, $html, $matches);
|
|
|
|
|
|
|
|
if (2 !== \count($matches)) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $matches[1];
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getXpathFunction()
|
|
|
|
{
|
|
|
|
return new ExpressionFunction(
|
|
|
|
'xpath',
|
2025-04-05 13:08:13 +02:00
|
|
|
function (): void {
|
2024-02-02 21:56:25 +01:00
|
|
|
throw new \Exception('Not supported');
|
|
|
|
},
|
|
|
|
function (array $arguments, $xpathQuery, $html) {
|
2024-11-19 23:30:18 +01:00
|
|
|
try {
|
|
|
|
$crawler = new Crawler((string) $html);
|
2024-02-02 21:56:25 +01:00
|
|
|
|
2024-11-19 23:30:18 +01:00
|
|
|
$crawler = $crawler->filterXPath($xpathQuery);
|
|
|
|
} catch (\Throwable $e) {
|
2024-02-02 21:56:25 +01:00
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2024-11-19 23:30:18 +01:00
|
|
|
if (0 === $crawler->count()) {
|
2024-02-02 21:56:25 +01:00
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2024-11-19 23:30:18 +01:00
|
|
|
return (string) $crawler->first()->attr('value');
|
2024-02-02 21:56:25 +01:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|