PluginProbe
Media Cloud Sync / 1.1.0
Media Cloud Sync v1.1.0
1.4.1 1.4.0 1.3.12 1.3.11 1.3.10 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.2.0 1.2.10 1.2.11 1.2.12 1.2.13 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 All 35 releases
media-cloud-sync / includes / sdk / s3 / GuzzleHttp / Cookie / FileCookieJar.php

FileCookieJar.php in Media Cloud Sync 1.1.0, at includes/sdk/s3/GuzzleHttp/Cookie/FileCookieJar.php

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