diff options
Diffstat (limited to 'src/Wallabag')
3 files changed, 144 insertions, 0 deletions
diff --git a/src/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilder.php b/src/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilder.php new file mode 100644 index 00000000..6d4129e8 --- /dev/null +++ b/src/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilder.php | |||
@@ -0,0 +1,68 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Wallabag\CoreBundle\GuzzleSiteAuthenticator; | ||
4 | |||
5 | use BD\GuzzleSiteAuthenticator\SiteConfig\SiteConfig; | ||
6 | use BD\GuzzleSiteAuthenticator\SiteConfig\SiteConfigBuilder; | ||
7 | use Graby\SiteConfig\ConfigBuilder; | ||
8 | use OutOfRangeException; | ||
9 | |||
10 | class GrabySiteConfigBuilder implements SiteConfigBuilder | ||
11 | { | ||
12 | /** | ||
13 | * @var \Graby\SiteConfig\ConfigBuilder | ||
14 | */ | ||
15 | private $grabyConfigBuilder; | ||
16 | /** | ||
17 | * @var array | ||
18 | */ | ||
19 | private $credentials; | ||
20 | |||
21 | /** | ||
22 | * GrabySiteConfigBuilder constructor. | ||
23 | * | ||
24 | * @param \Graby\SiteConfig\ConfigBuilder $grabyConfigBuilder | ||
25 | * @param array $credentials | ||
26 | */ | ||
27 | public function __construct(ConfigBuilder $grabyConfigBuilder, array $credentials = []) | ||
28 | { | ||
29 | $this->grabyConfigBuilder = $grabyConfigBuilder; | ||
30 | $this->credentials = $credentials; | ||
31 | } | ||
32 | |||
33 | /** | ||
34 | * Builds the SiteConfig for a host. | ||
35 | * | ||
36 | * @param string $host The "www." prefix is ignored | ||
37 | * | ||
38 | * @return SiteConfig | ||
39 | * | ||
40 | * @throws OutOfRangeException If there is no config for $host | ||
41 | */ | ||
42 | public function buildForHost($host) | ||
43 | { | ||
44 | // required by credentials below | ||
45 | $host = strtolower($host); | ||
46 | if (substr($host, 0, 4) == 'www.') { | ||
47 | $host = substr($host, 4); | ||
48 | } | ||
49 | |||
50 | $config = $this->grabyConfigBuilder->buildForHost($host); | ||
51 | $parameters = [ | ||
52 | 'host' => $host, | ||
53 | 'requiresLogin' => $config->requires_login ?: false, | ||
54 | 'loginUri' => $config->login_uri ?: null, | ||
55 | 'usernameField' => $config->login_username_field ?: null, | ||
56 | 'passwordField' => $config->login_password_field ?: null, | ||
57 | 'extraFields' => is_array($config->login_extra_fields) ? $config->login_extra_fields : [], | ||
58 | 'notLoggedInXpath' => $config->not_logged_in_xpath ?: null, | ||
59 | ]; | ||
60 | |||
61 | if (isset($this->credentials[$host])) { | ||
62 | $parameters['username'] = $this->credentials[$host]['username']; | ||
63 | $parameters['password'] = $this->credentials[$host]['password']; | ||
64 | } | ||
65 | |||
66 | return new SiteConfig($parameters); | ||
67 | } | ||
68 | } | ||
diff --git a/src/Wallabag/CoreBundle/Helper/HttpClientFactory.php b/src/Wallabag/CoreBundle/Helper/HttpClientFactory.php new file mode 100644 index 00000000..3e1d1ed5 --- /dev/null +++ b/src/Wallabag/CoreBundle/Helper/HttpClientFactory.php | |||
@@ -0,0 +1,44 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Wallabag\CoreBundle\Helper; | ||
4 | |||
5 | use Graby\Ring\Client\SafeCurlHandler; | ||
6 | use GuzzleHttp\Client; | ||
7 | use GuzzleHttp\Cookie\CookieJar; | ||
8 | use GuzzleHttp\Event\SubscriberInterface; | ||
9 | |||
10 | /** | ||
11 | * Builds and configures the Guzzle HTTP client. | ||
12 | */ | ||
13 | class HttpClientFactory | ||
14 | { | ||
15 | /** @var \GuzzleHttp\Event\SubscriberInterface */ | ||
16 | private $authenticatorSubscriber; | ||
17 | |||
18 | /** @var \GuzzleHttp\Cookie\CookieJar */ | ||
19 | private $cookieJar; | ||
20 | |||
21 | /** | ||
22 | * HttpClientFactory constructor. | ||
23 | * | ||
24 | * @param \GuzzleHttp\Event\SubscriberInterface $authenticatorSubscriber | ||
25 | * @param \GuzzleHttp\Cookie\CookieJar $cookieJar | ||
26 | */ | ||
27 | public function __construct(SubscriberInterface $authenticatorSubscriber, CookieJar $cookieJar) | ||
28 | { | ||
29 | $this->authenticatorSubscriber = $authenticatorSubscriber; | ||
30 | $this->cookieJar = $cookieJar; | ||
31 | } | ||
32 | |||
33 | /** | ||
34 | * @return \GuzzleHttp\Client | ||
35 | */ | ||
36 | public function buildHttpClient() | ||
37 | { | ||
38 | // need to set the (shared) cookie jar | ||
39 | $client = new Client(['handler' => new SafeCurlHandler(), 'defaults' => ['cookies' => $this->cookieJar]]); | ||
40 | $client->getEmitter()->attach($this->authenticatorSubscriber); | ||
41 | |||
42 | return $client; | ||
43 | } | ||
44 | } | ||
diff --git a/src/Wallabag/CoreBundle/Resources/config/services.yml b/src/Wallabag/CoreBundle/Resources/config/services.yml index 0280bc18..1b7f39fc 100644 --- a/src/Wallabag/CoreBundle/Resources/config/services.yml +++ b/src/Wallabag/CoreBundle/Resources/config/services.yml | |||
@@ -41,11 +41,43 @@ services: | |||
41 | arguments: | 41 | arguments: |
42 | - | 42 | - |
43 | error_message: '%wallabag_core.fetching_error_message%' | 43 | error_message: '%wallabag_core.fetching_error_message%' |
44 | - "@wallabag_core.guzzle.http_client" | ||
45 | - "@wallabag_core.graby.config_builder" | ||
44 | calls: | 46 | calls: |
45 | - [ setLogger, [ "@logger" ] ] | 47 | - [ setLogger, [ "@logger" ] ] |
46 | tags: | 48 | tags: |
47 | - { name: monolog.logger, channel: graby } | 49 | - { name: monolog.logger, channel: graby } |
48 | 50 | ||
51 | wallabag_core.graby.config_builder: | ||
52 | class: Graby\SiteConfig\ConfigBuilder | ||
53 | arguments: | ||
54 | - {} | ||
55 | - "@logger" | ||
56 | |||
57 | wallabag_core.guzzle.http_client: | ||
58 | class: GuzzleHttp\ClientInterface | ||
59 | factory: ["@wallabag_core.guzzle.http_client_factory", buildHttpClient] | ||
60 | |||
61 | wallabag_core.guzzle_authenticator.config_builder: | ||
62 | class: Wallabag\CoreBundle\GuzzleSiteAuthenticator\GrabySiteConfigBuilder | ||
63 | arguments: | ||
64 | - "@wallabag_core.graby.config_builder" | ||
65 | - "%sites_credentials%" | ||
66 | |||
67 | # service alias override | ||
68 | bd_guzzle_site_authenticator.site_config_builder: | ||
69 | alias: wallabag_core.guzzle_authenticator.config_builder | ||
70 | |||
71 | wallabag_core.guzzle.http_client_factory: | ||
72 | class: Wallabag\CoreBundle\Helper\HttpClientFactory | ||
73 | arguments: | ||
74 | - "@bd_guzzle_site_authenticator.authenticator_subscriber" | ||
75 | - "@wallabag_core.guzzle.cookie_jar" | ||
76 | |||
77 | wallabag_core.guzzle.cookie_jar: | ||
78 | class: GuzzleHttp\Cookie\FileCookieJar | ||
79 | arguments: ["%kernel.cache_dir%/cookiejar.json"] | ||
80 | |||
49 | wallabag_core.content_proxy: | 81 | wallabag_core.content_proxy: |
50 | class: Wallabag\CoreBundle\Helper\ContentProxy | 82 | class: Wallabag\CoreBundle\Helper\ContentProxy |
51 | arguments: | 83 | arguments: |