PluginProbe
Loginizer / 1.9.9
Loginizer v1.9.9
2.1.0 2.0.9 2.0.8 1.9.8 1.9.9 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 trunk 1.0 1.0.1 1.0.2 1.1.0 1.1.1 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 All 74 releases
loginizer / lib / hybridauth / Storage / Transient.php

Transient.php in Loginizer 1.9.9, at lib/hybridauth/Storage/Transient.php

145 lines 2.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*!
3 * Hybridauth
4 * https://hybridauth.github.io | https://github.com/hybridauth/hybridauth
5 * (c) 2017 Hybridauth authors | https://hybridauth.github.io/license.html
6 */
7
8 namespace Hybridauth\Storage;
9
10 use Hybridauth\Exception\RuntimeException;
11
12 /**
13 * Hybridauth storage manager
14 */
15 class Transient implements StorageInterface
16 {
17 /**
18 * Namespace
19 *
20 * @var string
21 */
22 protected $storeNamespace = 'HYBRIDAUTH::STORAGE';
23
24 /**
25 * Key prefix
26 *
27 * @var string
28 */
29 protected $keyPrefix = '';
30
31 /**
32 * We need short term data storage, so we will be using WordPress transient
33 *
34 * @var array
35 */
36 protected $transient = [];
37
38 /**
39 * Key used to identify the Transient
40 *
41 * @var string
42 */
43 protected $key = '';
44
45 /**
46 * Initiate a new session
47 *
48 * @throws RuntimeException
49 */
50 public function __construct()
51 {
52 if(!empty($_COOKIE['lz_social_login'])){
53 $this->key = sanitize_text_field(wp_unslash($_COOKIE['lz_social_login']));
54 return;
55 }
56
57 if(headers_sent()){
58 // phpcs:ignore
59 throw new RuntimeException('HTTP headers already sent to browser and Hybridauth won\'t be able to start/resume LZ session. To resolve this, cookie must be set before outputing any data.');
60 }
61
62 $this->key = 'lz_' . bin2hex(random_bytes(12));
63
64 if(!setcookie('lz_social_login', $this->key, time() + 90, COOKIEPATH, COOKIE_DOMAIN, is_ssl(), true)){
65 throw new RuntimeException('LZ session failed to start.');
66 }
67 }
68
69 /**
70 * {@inheritdoc}
71 */
72 public function get($key)
73 {
74 if(empty($this->transient)){
75 $this->transient = get_transient($this->key);
76
77 if(empty($this->transient) || !is_array($this->transient)){
78 return null;
79 }
80 }
81
82 if(isset($this->transient[$key])){
83 $value = $this->transient[$key];
84
85 if(is_array($value) && array_key_exists('lateObject', $value)){
86 $value = unserialize($value['lateObject']);
87 }
88
89 return $value;
90 }
91
92 return null;
93 }
94
95 /**
96 * {@inheritdoc}
97 */
98 public function set($key, $value)
99 {
100 if(is_object($value)){
101 // We encapsulate as our classes may be defined after session is initialized.
102 $value = ['lateObject' => serialize($value)];
103 }
104
105 $this->transient[$key] = $value;
106 set_transient($this->key, $this->transient, 60);
107 }
108
109 /**
110 * {@inheritdoc}
111 */
112 public function clear()
113 {
114 delete_transient($this->key);
115 setcookie('lz_social_login', '', time() - 3600, COOKIEPATH, COOKIE_DOMAIN, is_ssl(), true);
116 }
117
118 /**
119 * {@inheritdoc}
120 */
121 public function delete($key)
122 {
123 if(isset($this->transient, $this->transient[$key])){
124 unset($this->transient[$key]);
125 set_transient($this->key, $this->transient, 60);
126 }
127 }
128
129 /**
130 * {@inheritdoc}
131 */
132 public function deleteMatch($key)
133 {
134 if(isset($this->transient) && count($this->transient)){
135 foreach($this->transient as $k => $v) {
136 if(strstr($k, $key)){
137 unset($this->transient[$k]);
138 }
139 }
140
141 set_transient($this->key, $this->transient, 60);
142 }
143 }
144 }
145