vendor/guzzlehttp/guzzle/src/Cookie/FileCookieJar.php
121 lines · 3582 bytes
1<?php
2
3namespace GuzzleHttp\Cookie;
4
5use GuzzleHttp\Exception\InvalidArgumentException;
6
7/**
8 * Persists non-session cookies using a JSON formatted file
9 */
10class FileCookieJar extends CookieJar
11{
12 /**
13 * @var string filename
14 */
15 private $filename;
16
17 /**
18 * @var bool Control whether to persist session cookies or not.
19 */
20 private $storeSessionCookies;
21
22 /**
23 * Create a new FileCookieJar object
24 *
25 * @param string $cookieFile File to store the cookie data
26 * @param bool $storeSessionCookies Set to true to store session cookies
27 * in the cookie jar.
28 *
29 * @throws \RuntimeException if the file cannot be found or created
30 */
31 public function __construct(string $cookieFile, bool $storeSessionCookies = false)
32 {
33 parent::__construct();
34 $this->filename = $cookieFile;
35 $this->storeSessionCookies = $storeSessionCookies;
36
37 if (\file_exists($cookieFile)) {
38 $this->load($cookieFile);
39 }
40 }
41
42 /**
43 * Saves the file when shutting down
44 */
45 public function __destruct()
46 {
47 $this->save($this->filename);
48 }
49
50 /**
51 * Saves the cookies to a file.
52 *
53 * @param string $filename File to save
54 *
55 * @throws \RuntimeException if the file cannot be found or created
56 */
57 public function save(string $filename): void
58 {
59 $json = [];
60 /** @var SetCookie $cookie */
61 foreach ($this as $cookie) {
62 if (CookieJar::shouldPersist($cookie, $this->storeSessionCookies)) {
63 $data = $cookie->toArray();
64 $data['HostOnly'] = $cookie->getHostOnly();
65 $json[] = $data;
66 }
67 }
68
69 $jsonStr = \json_encode($json);
70 if (\JSON_ERROR_NONE !== \json_last_error()) {
71 throw new InvalidArgumentException('json_encode error: '.\json_last_error_msg());
72 }
73
74 /** @var non-empty-string $jsonStr */
75 if (false === \file_put_contents($filename, $jsonStr, \LOCK_EX)) {
76 throw new \RuntimeException("Unable to save file {$filename}");
77 }
78 }
79
80 /**
81 * Load cookies from a JSON formatted file.
82 *
83 * Old cookies are kept unless overwritten by newly loaded ones.
84 *
85 * @param string $filename Cookie file to load.
86 *
87 * @throws \RuntimeException if the file cannot be loaded.
88 */
89 public function load(string $filename): void
90 {
91 $json = \file_get_contents($filename);
92 if (false === $json) {
93 throw new \RuntimeException("Unable to load file {$filename}");
94 }
95 if ($json === '') {
96 return;
97 }
98
99 $data = \json_decode($json, true);
100 if (\JSON_ERROR_NONE !== \json_last_error()) {
101 throw new InvalidArgumentException('json_decode error: '.\json_last_error_msg());
102 }
103
104 if (\is_array($data)) {
105 $cookies = [];
106 foreach ($data as $cookie) {
107 if (!\is_array($cookie) || !\array_key_exists('HostOnly', $cookie) || !\is_bool($cookie['HostOnly'])) {
108 throw new \RuntimeException("Invalid cookie file: {$filename}");
109 }
110
111 $cookies[] = new SetCookie($cookie);
112 }
113
114 foreach ($cookies as $cookie) {
115 $this->setCookie($cookie);
116 }
117 } elseif (\is_scalar($data) && !empty($data)) {
118 throw new \RuntimeException("Invalid cookie file: {$filename}");
119 }
120 }
121}