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
|
|
|
|
|
|
|
use GuzzleHttp\ClientInterface;
|
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;
|
|
|
|
|
|
|
|
class AuthenticatorProvider implements ExpressionFunctionProviderInterface
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var ClientInterface
|
|
|
|
*/
|
|
|
|
private $guzzle;
|
|
|
|
|
|
|
|
public function __construct(ClientInterface $guzzle)
|
|
|
|
{
|
|
|
|
$this->guzzle = $guzzle;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getFunctions(): array
|
|
|
|
{
|
|
|
|
$result = [
|
|
|
|
$this->getRequestHtmlFunction(),
|
|
|
|
$this->getXpathFunction(),
|
|
|
|
$this->getPregMatchFunction(),
|
|
|
|
];
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getRequestHtmlFunction()
|
|
|
|
{
|
|
|
|
return new ExpressionFunction(
|
|
|
|
'request_html',
|
|
|
|
function () {
|
|
|
|
throw new \Exception('Not supported');
|
|
|
|
},
|
|
|
|
function (array $arguments, $uri, array $options = []) {
|
|
|
|
return $this->guzzle->get($uri, $options)->getBody();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getPregMatchFunction()
|
|
|
|
{
|
|
|
|
return new ExpressionFunction(
|
|
|
|
'preg_match',
|
|
|
|
function () {
|
|
|
|
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',
|
|
|
|
function () {
|
|
|
|
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
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|