1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* SessionStorage.php |
4
|
|
|
* |
5
|
|
|
* @copyright More in license.md |
6
|
|
|
* @license http://www.ipublikuj.eu |
7
|
|
|
* @author Adam Kadlec http://www.ipublikuj.eu |
8
|
|
|
* @package iPublikuj:Twitter! |
9
|
|
|
* @subpackage common |
10
|
|
|
* @since 5.0 |
11
|
|
|
* |
12
|
|
|
* @date 04.03.15 |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace IPub\Twitter; |
16
|
|
|
|
17
|
|
|
use Nette; |
18
|
|
|
use Nette\Http; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Stores accessToken and other critical data that should be shared across requests |
22
|
|
|
* |
23
|
|
|
* @package iPublikuj:Twitter! |
24
|
|
|
* @subpackage common |
25
|
|
|
* |
26
|
|
|
* @author Adam Kadlec <[email protected]> |
27
|
|
|
* |
28
|
|
|
* @property string $access_token |
29
|
|
|
* @property string $access_token_secret |
30
|
|
|
* @property string $request_token |
31
|
|
|
* @property string $request_token_secret |
32
|
|
|
* @property string $user_id |
33
|
|
|
*/ |
34
|
|
|
class SessionStorage extends Nette\Object |
35
|
|
|
{ |
36
|
|
|
/** |
37
|
|
|
* @var Http\SessionSection |
38
|
|
|
*/ |
39
|
|
|
protected $session; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param Http\Session $session |
43
|
|
|
* @param Configuration $config |
44
|
|
|
*/ |
45
|
|
|
public function __construct(Http\Session $session, Configuration $config) |
46
|
|
|
{ |
47
|
|
|
$this->session = $session->getSection('Twitter/' . $config->consumerKey); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Stores the given ($key, $value) pair, so that future calls to |
52
|
|
|
* getPersistentData($key) return $value. This call may be in another request. |
53
|
|
|
* |
54
|
|
|
* Provides the implementations of the inherited abstract |
55
|
|
|
* methods. |
56
|
|
|
* |
57
|
|
|
* The implementation uses PHP sessions to maintain |
58
|
|
|
* a store for authorization codes, user ids, CSRF states, and |
59
|
|
|
* access tokens. |
60
|
|
|
*/ |
61
|
|
|
public function set($key, $value) |
62
|
|
|
{ |
63
|
|
|
$this->session->$key = $value; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param string $key The key of the data to retrieve |
68
|
|
|
* @param mixed $default The default value to return if $key is not found |
69
|
|
|
* |
70
|
|
|
* @return mixed |
71
|
|
|
*/ |
72
|
|
|
public function get($key, $default = FALSE) |
73
|
|
|
{ |
74
|
|
|
return isset($this->session->$key) ? $this->session->$key : $default; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Clear the data with $key from the persistent storage |
79
|
|
|
* |
80
|
|
|
* @param string $key |
81
|
|
|
* @return void |
82
|
|
|
*/ |
83
|
|
|
public function clear($key) |
84
|
|
|
{ |
85
|
|
|
unset($this->session->$key); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Clear all data from the persistent storage |
90
|
|
|
* |
91
|
|
|
* @return void |
92
|
|
|
*/ |
93
|
|
|
public function clearAll() |
94
|
|
|
{ |
95
|
|
|
$this->session->remove(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param string $name |
100
|
|
|
* @return mixed |
101
|
|
|
*/ |
102
|
|
|
public function &__get($name) |
103
|
|
|
{ |
104
|
|
|
$value = $this->get($name); |
105
|
|
|
return $value; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param string $name |
110
|
|
|
* @param mixed $value |
111
|
|
|
*/ |
112
|
|
|
public function __set($name, $value) |
113
|
|
|
{ |
114
|
|
|
$this->set($name, $value); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param string $name |
119
|
|
|
* @return bool |
120
|
|
|
*/ |
121
|
|
|
public function __isset($name) |
122
|
|
|
{ |
123
|
|
|
return isset($this->session->{$name}); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param string $name |
128
|
|
|
*/ |
129
|
|
|
public function __unset($name) |
130
|
|
|
{ |
131
|
|
|
$this->clear($name); |
132
|
|
|
} |
133
|
|
|
} |