aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag
diff options
context:
space:
mode:
authorJérémy Benoist <j0k3r@users.noreply.github.com>2019-07-24 16:57:04 +0200
committerGitHub <noreply@github.com>2019-07-24 16:57:04 +0200
commit3a9aa28558313acdf917b07abceffcd3622c3059 (patch)
tree9ceebc0956bdd03aa58e8267149cfbb585b04f8b /src/Wallabag
parent3a08e81969e9deafdb4d1aa890c03d67adcc4897 (diff)
parent51d7f62b316abfc14e770b3108edb0e3f48e38dd (diff)
downloadwallabag-3a9aa28558313acdf917b07abceffcd3622c3059.tar.gz
wallabag-3a9aa28558313acdf917b07abceffcd3622c3059.tar.zst
wallabag-3a9aa28558313acdf917b07abceffcd3622c3059.zip
Merge pull request #4063 from wallabag/fix/cookiejar
Use a custom cookiejar to avoid error when the cookie is badly saved
Diffstat (limited to 'src/Wallabag')
-rw-r--r--src/Wallabag/CoreBundle/Helper/FileCookieJar.php91
-rw-r--r--src/Wallabag/CoreBundle/Resources/config/services.yml6
2 files changed, 95 insertions, 2 deletions
diff --git a/src/Wallabag/CoreBundle/Helper/FileCookieJar.php b/src/Wallabag/CoreBundle/Helper/FileCookieJar.php
new file mode 100644
index 00000000..9a63e949
--- /dev/null
+++ b/src/Wallabag/CoreBundle/Helper/FileCookieJar.php
@@ -0,0 +1,91 @@
1<?php
2
3namespace Wallabag\CoreBundle\Helper;
4
5use GuzzleHttp\Cookie\FileCookieJar as BaseFileCookieJar;
6use GuzzleHttp\Cookie\SetCookie;
7use GuzzleHttp\Utils;
8use Psr\Log\LoggerInterface;
9
10/**
11 * Overidden Cookie behavior to:
12 * - fix multiple concurrent writes (see https://github.com/guzzle/guzzle/pull/1884)
13 * - ignore error when the cookie file is malformatted (resulting in clearing it).
14 */
15class FileCookieJar extends BaseFileCookieJar
16{
17 private $logger;
18
19 /**
20 * @param LoggerInterface $logger Only used to log info when something goes wrong
21 * @param string $cookieFile File to store the cookie data
22 */
23 public function __construct(LoggerInterface $logger, $cookieFile)
24 {
25 parent::__construct($cookieFile);
26
27 $this->logger = $logger;
28 }
29
30 /**
31 * Saves the cookies to a file.
32 *
33 * @param string $filename File to save
34 *
35 * @throws \RuntimeException if the file cannot be found or created
36 */
37 public function save($filename)
38 {
39 $json = [];
40 foreach ($this as $cookie) {
41 if ($cookie->getExpires() && !$cookie->getDiscard()) {
42 $json[] = $cookie->toArray();
43 }
44 }
45
46 if (false === file_put_contents($filename, json_encode($json), LOCK_EX)) {
47 // @codeCoverageIgnoreStart
48 throw new \RuntimeException("Unable to save file {$filename}");
49 // @codeCoverageIgnoreEnd
50 }
51 }
52
53 /**
54 * Load cookies from a JSON formatted file.
55 *
56 * Old cookies are kept unless overwritten by newly loaded ones.
57 *
58 * @param string $filename cookie file to load
59 *
60 * @throws \RuntimeException if the file cannot be loaded
61 */
62 public function load($filename)
63 {
64 $json = file_get_contents($filename);
65 if (false === $json) {
66 // @codeCoverageIgnoreStart
67 throw new \RuntimeException("Unable to load file {$filename}");
68 // @codeCoverageIgnoreEnd
69 }
70
71 try {
72 $data = Utils::jsonDecode($json, true);
73 } catch (\InvalidArgumentException $e) {
74 $this->logger->error('JSON inside the cookie is broken', [
75 'json' => $json,
76 'error_msg' => $e->getMessage(),
77 ]);
78
79 // cookie file is invalid, just ignore the exception and it'll reset the whole cookie file
80 $data = '';
81 }
82
83 if (\is_array($data)) {
84 foreach (Utils::jsonDecode($json, true) as $cookie) {
85 $this->setCookie(new SetCookie($cookie));
86 }
87 } elseif (\strlen($data)) {
88 throw new \RuntimeException("Invalid cookie file: {$filename}");
89 }
90 }
91}
diff --git a/src/Wallabag/CoreBundle/Resources/config/services.yml b/src/Wallabag/CoreBundle/Resources/config/services.yml
index 31986951..3f3d4de7 100644
--- a/src/Wallabag/CoreBundle/Resources/config/services.yml
+++ b/src/Wallabag/CoreBundle/Resources/config/services.yml
@@ -82,8 +82,10 @@ services:
82 - ["addSubscriber", ["@bd_guzzle_site_authenticator.authenticator_subscriber"]] 82 - ["addSubscriber", ["@bd_guzzle_site_authenticator.authenticator_subscriber"]]
83 83
84 wallabag_core.guzzle.cookie_jar: 84 wallabag_core.guzzle.cookie_jar:
85 class: GuzzleHttp\Cookie\FileCookieJar 85 class: Wallabag\CoreBundle\Helper\FileCookieJar
86 arguments: ["%kernel.cache_dir%/cookiejar.json"] 86 arguments:
87 - "@logger"
88 - "%kernel.cache_dir%/cookiejar.json"
87 89
88 wallabag_core.content_proxy: 90 wallabag_core.content_proxy:
89 class: Wallabag\CoreBundle\Helper\ContentProxy 91 class: Wallabag\CoreBundle\Helper\ContentProxy