2024-02-02 21:56:25 +01:00
|
|
|
<?php
|
|
|
|
|
|
2024-11-23 20:07:07 +01:00
|
|
|
namespace Wallabag\SiteConfig;
|
2024-02-02 21:56:25 +01:00
|
|
|
|
|
|
|
|
use GuzzleHttp\ClientInterface;
|
|
|
|
|
use GuzzleHttp\Cookie\CookieJar;
|
2024-11-19 23:30:49 +01:00
|
|
|
use Symfony\Component\DomCrawler\Crawler;
|
2024-02-02 21:56:25 +01:00
|
|
|
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
|
2024-02-19 01:30:12 +01:00
|
|
|
use Wallabag\ExpressionLanguage\AuthenticatorProvider;
|
2024-02-02 21:56:25 +01:00
|
|
|
|
2024-11-23 19:55:29 +01:00
|
|
|
class LoginFormAuthenticator
|
2024-02-02 21:56:25 +01:00
|
|
|
{
|
2024-12-23 00:02:59 +01:00
|
|
|
private AuthenticatorProvider $authenticatorProvider;
|
|
|
|
|
|
|
|
|
|
public function __construct(AuthenticatorProvider $authenticatorProvider)
|
|
|
|
|
{
|
|
|
|
|
$this->authenticatorProvider = $authenticatorProvider;
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-23 19:55:29 +01:00
|
|
|
/**
|
|
|
|
|
* Logs the configured user on the given Guzzle client.
|
|
|
|
|
*
|
|
|
|
|
* @return self
|
|
|
|
|
*/
|
2024-11-23 11:27:18 +01:00
|
|
|
public function login(SiteConfig $siteConfig, ClientInterface $guzzle)
|
2024-02-02 21:56:25 +01:00
|
|
|
{
|
|
|
|
|
$postFields = [
|
2024-11-23 11:27:18 +01:00
|
|
|
$siteConfig->getUsernameField() => $siteConfig->getUsername(),
|
|
|
|
|
$siteConfig->getPasswordField() => $siteConfig->getPassword(),
|
2024-12-23 00:02:59 +01:00
|
|
|
] + $this->getExtraFields($siteConfig);
|
2024-02-02 21:56:25 +01:00
|
|
|
|
|
|
|
|
$guzzle->post(
|
2024-11-23 11:27:18 +01:00
|
|
|
$siteConfig->getLoginUri(),
|
2024-02-02 21:56:25 +01:00
|
|
|
['body' => $postFields, 'allow_redirects' => true, 'verify' => false]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-23 19:55:29 +01:00
|
|
|
/**
|
|
|
|
|
* Checks if we are logged into the site, but without calling the server (e.g. do we have a Cookie).
|
|
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2024-11-23 11:27:18 +01:00
|
|
|
public function isLoggedIn(SiteConfig $siteConfig, ClientInterface $guzzle)
|
2024-02-02 21:56:25 +01:00
|
|
|
{
|
|
|
|
|
if (($cookieJar = $guzzle->getDefaultOption('cookies')) instanceof CookieJar) {
|
|
|
|
|
/** @var \GuzzleHttp\Cookie\SetCookie $cookie */
|
|
|
|
|
foreach ($cookieJar as $cookie) {
|
|
|
|
|
// check required cookies
|
2024-11-23 11:27:18 +01:00
|
|
|
if ($cookie->getDomain() === $siteConfig->getHost()) {
|
2024-02-02 21:56:25 +01:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-23 19:55:29 +01:00
|
|
|
/**
|
|
|
|
|
* Checks from the HTML of a page if authentication is requested by a grabbed page.
|
|
|
|
|
*
|
|
|
|
|
* @param string $html
|
|
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2024-11-23 11:27:18 +01:00
|
|
|
public function isLoginRequired(SiteConfig $siteConfig, $html)
|
2024-02-02 21:56:25 +01:00
|
|
|
{
|
|
|
|
|
// need to check for the login dom element ($options['not_logged_in_xpath']) in the HTML
|
2024-11-19 23:30:49 +01:00
|
|
|
try {
|
|
|
|
|
$crawler = new Crawler((string) $html);
|
2024-02-02 21:56:25 +01:00
|
|
|
|
2024-11-23 11:27:18 +01:00
|
|
|
$loggedIn = $crawler->evaluate((string) $siteConfig->getNotLoggedInXpath());
|
2024-11-19 23:30:49 +01:00
|
|
|
} catch (\Throwable $e) {
|
2024-02-02 21:56:25 +01:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-19 23:30:49 +01:00
|
|
|
return \count($loggedIn) > 0;
|
2024-02-02 21:56:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns extra fields from the configuration.
|
|
|
|
|
* Evaluates any field value that is an expression language string.
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
2024-12-23 00:02:59 +01:00
|
|
|
private function getExtraFields(SiteConfig $siteConfig)
|
2024-02-02 21:56:25 +01:00
|
|
|
{
|
|
|
|
|
$extraFields = [];
|
|
|
|
|
|
2024-11-23 11:27:18 +01:00
|
|
|
foreach ($siteConfig->getExtraFields() as $fieldName => $fieldValue) {
|
2024-02-02 21:56:25 +01:00
|
|
|
if ('@=' === substr($fieldValue, 0, 2)) {
|
2024-12-23 00:02:59 +01:00
|
|
|
$expressionLanguage = $this->getExpressionLanguage();
|
2024-02-02 21:56:25 +01:00
|
|
|
$fieldValue = $expressionLanguage->evaluate(
|
|
|
|
|
substr($fieldValue, 2),
|
|
|
|
|
[
|
2024-11-23 11:27:18 +01:00
|
|
|
'config' => $siteConfig,
|
2024-02-02 21:56:25 +01:00
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$extraFields[$fieldName] = $fieldValue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $extraFields;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return ExpressionLanguage
|
|
|
|
|
*/
|
2024-12-23 00:02:59 +01:00
|
|
|
private function getExpressionLanguage()
|
2024-02-02 21:56:25 +01:00
|
|
|
{
|
|
|
|
|
return new ExpressionLanguage(
|
|
|
|
|
null,
|
2024-12-23 00:02:59 +01:00
|
|
|
[$this->authenticatorProvider]
|
2024-02-02 21:56:25 +01:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|