| 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 |
|
| 55 |
$transient = get_transient($this->key); |
| 56 |
if(!empty($transient)){ |
| 57 |
$this->transient = $transient; |
| 58 |
} |
| 59 |
return; |
| 60 |
} |
| 61 |
|
| 62 |
if(headers_sent()){ |
| 63 |
// phpcs:ignore |
| 64 |
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.'); |
| 65 |
} |
| 66 |
|
| 67 |
$this->key = 'lz_' . bin2hex(random_bytes(12)); |
| 68 |
|
| 69 |
if(!setcookie('lz_social_login', $this->key, time() + 90, COOKIEPATH, COOKIE_DOMAIN, is_ssl(), true)){ |
| 70 |
throw new RuntimeException('LZ session failed to start.'); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* {@inheritdoc} |
| 76 |
*/ |
| 77 |
public function get($key) |
| 78 |
{ |
| 79 |
if(empty($this->transient)){ |
| 80 |
$this->transient = get_transient($this->key); |
| 81 |
|
| 82 |
if(empty($this->transient) || !is_array($this->transient)){ |
| 83 |
return null; |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
if(isset($this->transient[$key])){ |
| 88 |
$value = $this->transient[$key]; |
| 89 |
|
| 90 |
if(is_array($value) && array_key_exists('lateObject', $value)){ |
| 91 |
$value = unserialize($value['lateObject']); |
| 92 |
} |
| 93 |
|
| 94 |
return $value; |
| 95 |
} |
| 96 |
|
| 97 |
return null; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* {@inheritdoc} |
| 102 |
*/ |
| 103 |
public function set($key, $value) |
| 104 |
{ |
| 105 |
if(is_object($value)){ |
| 106 |
// We encapsulate as our classes may be defined after session is initialized. |
| 107 |
$value = ['lateObject' => serialize($value)]; |
| 108 |
} |
| 109 |
|
| 110 |
$this->transient[$key] = $value; |
| 111 |
set_transient($this->key, $this->transient, 60); |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* {@inheritdoc} |
| 116 |
*/ |
| 117 |
public function clear() |
| 118 |
{ |
| 119 |
delete_transient($this->key); |
| 120 |
setcookie('lz_social_login', '', time() - 3600, COOKIEPATH, COOKIE_DOMAIN, is_ssl(), true); |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* {@inheritdoc} |
| 125 |
*/ |
| 126 |
public function delete($key) |
| 127 |
{ |
| 128 |
if(isset($this->transient, $this->transient[$key])){ |
| 129 |
unset($this->transient[$key]); |
| 130 |
set_transient($this->key, $this->transient, 60); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* {@inheritdoc} |
| 136 |
*/ |
| 137 |
public function deleteMatch($key) |
| 138 |
{ |
| 139 |
if(isset($this->transient) && count($this->transient)){ |
| 140 |
foreach($this->transient as $k => $v) { |
| 141 |
if(strstr($k, $key)){ |
| 142 |
unset($this->transient[$k]); |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
set_transient($this->key, $this->transient, 60); |
| 147 |
} |
| 148 |
} |
| 149 |
} |
| 150 |
|