aboutsummaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/Wallabag/CoreBundle/Helper/FileCookieJar.php72
-rw-r--r--src/Wallabag/CoreBundle/Resources/config/services.yml2
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
3namespace Wallabag\CoreBundle\Helper;
4
5use GuzzleHttp\Cookie\FileCookieJar as BaseFileCookieJar;
6use GuzzleHttp\Cookie\SetCookie;
7use 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 */
14class 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: