| 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 Session 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 |
* Initiate a new session |
| 33 |
* |
| 34 |
* @throws RuntimeException |
| 35 |
*/ |
| 36 |
public function __construct() |
| 37 |
{ |
| 38 |
if (session_id()) { |
| 39 |
return; |
| 40 |
} |
| 41 |
|
| 42 |
if (headers_sent()) { |
| 43 |
// phpcs:ignore |
| 44 |
throw new RuntimeException('HTTP headers already sent to browser and Hybridauth won\'t be able to start/resume PHP session. To resolve this, session_start() must be called before outputing any data.'); |
| 45 |
} |
| 46 |
|
| 47 |
if (!session_start()) { |
| 48 |
throw new RuntimeException('PHP session failed to start.'); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* {@inheritdoc} |
| 54 |
*/ |
| 55 |
public function get($key) |
| 56 |
{ |
| 57 |
$key = $this->keyPrefix . strtolower($key); |
| 58 |
|
| 59 |
if (isset($_SESSION[$this->storeNamespace], $_SESSION[$this->storeNamespace][$key])) { |
| 60 |
$value = $_SESSION[$this->storeNamespace][$key]; |
| 61 |
|
| 62 |
if (is_array($value) && array_key_exists('lateObject', $value)) { |
| 63 |
$value = unserialize($value['lateObject']); |
| 64 |
} |
| 65 |
|
| 66 |
return $value; |
| 67 |
} |
| 68 |
|
| 69 |
return null; |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* {@inheritdoc} |
| 74 |
*/ |
| 75 |
public function set($key, $value) |
| 76 |
{ |
| 77 |
$key = $this->keyPrefix . strtolower($key); |
| 78 |
|
| 79 |
if (is_object($value)) { |
| 80 |
// We encapsulate as our classes may be defined after session is initialized. |
| 81 |
$value = ['lateObject' => serialize($value)]; |
| 82 |
} |
| 83 |
|
| 84 |
$_SESSION[$this->storeNamespace][$key] = $value; |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* {@inheritdoc} |
| 89 |
*/ |
| 90 |
public function clear() |
| 91 |
{ |
| 92 |
$_SESSION[$this->storeNamespace] = []; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* {@inheritdoc} |
| 97 |
*/ |
| 98 |
public function delete($key) |
| 99 |
{ |
| 100 |
$key = $this->keyPrefix . strtolower($key); |
| 101 |
|
| 102 |
if (isset($_SESSION[$this->storeNamespace], $_SESSION[$this->storeNamespace][$key])) { |
| 103 |
$tmp = $_SESSION[$this->storeNamespace]; |
| 104 |
|
| 105 |
unset($tmp[$key]); |
| 106 |
|
| 107 |
$_SESSION[$this->storeNamespace] = $tmp; |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* {@inheritdoc} |
| 113 |
*/ |
| 114 |
public function deleteMatch($key) |
| 115 |
{ |
| 116 |
$key = $this->keyPrefix . strtolower($key); |
| 117 |
|
| 118 |
if (isset($_SESSION[$this->storeNamespace]) && count($_SESSION[$this->storeNamespace])) { |
| 119 |
$tmp = $_SESSION[$this->storeNamespace]; |
| 120 |
|
| 121 |
foreach ($tmp as $k => $v) { |
| 122 |
if (strstr($k, $key)) { |
| 123 |
unset($tmp[$k]); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
$_SESSION[$this->storeNamespace] = $tmp; |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
|