diff options
author | Jeremy Benoist <jeremy.benoist@gmail.com> | 2019-07-24 10:29:50 +0200 |
---|---|---|
committer | Jeremy Benoist <jeremy.benoist@gmail.com> | 2019-07-24 10:42:20 +0200 |
commit | 9a80dcf11e4f3ee4f4a751550afc6469e76b22d9 (patch) | |
tree | 177e4b2acfd19c5cd5f87f99a1ae592b082bce49 /src | |
parent | 3a08e81969e9deafdb4d1aa890c03d67adcc4897 (diff) | |
download | wallabag-9a80dcf11e4f3ee4f4a751550afc6469e76b22d9.tar.gz wallabag-9a80dcf11e4f3ee4f4a751550afc6469e76b22d9.tar.zst wallabag-9a80dcf11e4f3ee4f4a751550afc6469e76b22d9.zip |
Use a custom cookiejar to avoid error when the cookie is badly saved
It happens sometimes on wallabag.it, the json inside the cookie is badly saved and the json isn't valid. It generates an exception and avoid people to use the api and import contents.
To fix that, we use a dedicated `FileCookieJar`, which extends the default one from Guzzle to fix these issues.
Also updated deps
Diffstat (limited to 'src')
-rw-r--r-- | src/Wallabag/CoreBundle/Helper/FileCookieJar.php | 72 | ||||
-rw-r--r-- | src/Wallabag/CoreBundle/Resources/config/services.yml | 2 |
2 files changed, 73 insertions, 1 deletions
diff --git a/src/Wallabag/CoreBundle/Helper/FileCookieJar.php b/src/Wallabag/CoreBundle/Helper/FileCookieJar.php new file mode 100644 index 00000000..52c7f5de --- /dev/null +++ b/src/Wallabag/CoreBundle/Helper/FileCookieJar.php | |||
@@ -0,0 +1,72 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Wallabag\CoreBundle\Helper; | ||
4 | |||
5 | use GuzzleHttp\Cookie\FileCookieJar as BaseFileCookieJar; | ||
6 | use GuzzleHttp\Cookie\SetCookie; | ||
7 | use GuzzleHttp\Utils; | ||
8 | |||
9 | /** | ||
10 | * Overidden Cookie behavior to: | ||
11 | * - fix multiple concurrent writes (see https://github.com/guzzle/guzzle/pull/1884) | ||
12 | * - ignore error when the cookie file is malformatted (resulting in clearing it). | ||
13 | */ | ||
14 | class FileCookieJar extends BaseFileCookieJar | ||
15 | { | ||
16 | /** | ||
17 | * Saves the cookies to a file. | ||
18 | * | ||
19 | * @param string $filename File to save | ||
20 | * | ||
21 | * @throws \RuntimeException if the file cannot be found or created | ||
22 | */ | ||
23 | public function save($filename) | ||
24 | { | ||
25 | $json = []; | ||
26 | foreach ($this as $cookie) { | ||
27 | if ($cookie->getExpires() && !$cookie->getDiscard()) { | ||
28 | $json[] = $cookie->toArray(); | ||
29 | } | ||
30 | } | ||
31 | |||
32 | if (false === file_put_contents($filename, json_encode($json), LOCK_EX)) { | ||
33 | // @codeCoverageIgnoreStart | ||
34 | throw new \RuntimeException("Unable to save file {$filename}"); | ||
35 | // @codeCoverageIgnoreEnd | ||
36 | } | ||
37 | } | ||
38 | |||
39 | /** | ||
40 | * Load cookies from a JSON formatted file. | ||
41 | * | ||
42 | * Old cookies are kept unless overwritten by newly loaded ones. | ||
43 | * | ||
44 | * @param string $filename cookie file to load | ||
45 | * | ||
46 | * @throws \RuntimeException if the file cannot be loaded | ||
47 | */ | ||
48 | public function load($filename) | ||
49 | { | ||
50 | $json = file_get_contents($filename); | ||
51 | if (false === $json) { | ||
52 | // @codeCoverageIgnoreStart | ||
53 | throw new \RuntimeException("Unable to load file {$filename}"); | ||
54 | // @codeCoverageIgnoreEnd | ||
55 | } | ||
56 | |||
57 | try { | ||
58 | $data = Utils::jsonDecode($json, true); | ||
59 | } catch (\InvalidArgumentException $e) { | ||
60 | // cookie file is invalid, just ignore the exception and it'll reset the whole cookie file | ||
61 | $data = ''; | ||
62 | } | ||
63 | |||
64 | if (\is_array($data)) { | ||
65 | foreach (Utils::jsonDecode($json, true) as $cookie) { | ||
66 | $this->setCookie(new SetCookie($cookie)); | ||
67 | } | ||
68 | } elseif (\strlen($data)) { | ||
69 | throw new \RuntimeException("Invalid cookie file: {$filename}"); | ||
70 | } | ||
71 | } | ||
72 | } | ||
diff --git a/src/Wallabag/CoreBundle/Resources/config/services.yml b/src/Wallabag/CoreBundle/Resources/config/services.yml index 31986951..169b67e5 100644 --- a/src/Wallabag/CoreBundle/Resources/config/services.yml +++ b/src/Wallabag/CoreBundle/Resources/config/services.yml | |||
@@ -82,7 +82,7 @@ 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: ["%kernel.cache_dir%/cookiejar.json"] |
87 | 87 | ||
88 | wallabag_core.content_proxy: | 88 | wallabag_core.content_proxy: |