PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / trunk
Yoast SEO – Advanced SEO with real-time guidance and built-in AI vtrunk
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / vendor_prefixed / guzzlehttp / guzzle / src / Cookie / FileCookieJar.php

FileCookieJar.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI trunk, at vendor_prefixed/guzzlehttp/guzzle/src/Cookie/FileCookieJar.php

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