1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Vperyod Simple Lock |
4
|
|
|
* |
5
|
|
|
* PHP version 5 |
6
|
|
|
* |
7
|
|
|
* Copyright (C) 2016 Jake Johns |
8
|
|
|
* |
9
|
|
|
* This software may be modified and distributed under the terms |
10
|
|
|
* of the MIT license. See the LICENSE file for details. |
11
|
|
|
* |
12
|
|
|
* @category Session |
13
|
|
|
* @package Vperyod\SimpleLock |
14
|
|
|
* @author Jake Johns <[email protected]> |
15
|
|
|
* @copyright 2016 Jake Johns |
16
|
|
|
* @license http://jnj.mit-license.org/2016 MIT License |
17
|
|
|
* @link http://jakejohns.net |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace Vperyod\SimpleLock; |
21
|
|
|
|
22
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Lock |
26
|
|
|
* |
27
|
|
|
* @category Session |
28
|
|
|
* @package Vperyod\SimpleLock |
29
|
|
|
* @author Jake Johns <[email protected]> |
30
|
|
|
* @license http://jnj.mit-license.org/ MIT License |
31
|
|
|
* @link https://github.com/vperyod/vperyod.simple-lock |
32
|
|
|
*/ |
33
|
|
|
class Lock |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* Cookie |
37
|
|
|
* |
38
|
|
|
* @var array |
39
|
|
|
* |
40
|
|
|
* @access protected |
41
|
|
|
*/ |
42
|
|
|
protected $cookie; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Session key for lock status |
46
|
|
|
* |
47
|
|
|
* @var string |
48
|
|
|
* |
49
|
|
|
* @access protected |
50
|
|
|
*/ |
51
|
|
|
protected $key = 'vperyod/simplelock:unlocked'; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Session key for intent |
55
|
|
|
* |
56
|
|
|
* @var string |
57
|
|
|
* |
58
|
|
|
* @access protected |
59
|
|
|
*/ |
60
|
|
|
protected $intent = 'vperyod/simplelock:intent'; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Create a session lock |
64
|
|
|
* |
65
|
|
|
* @access public |
66
|
|
|
*/ |
67
|
2 |
|
public function __construct() |
68
|
|
|
{ |
69
|
2 |
|
$this->session(); |
70
|
2 |
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Unlock a session |
74
|
|
|
* |
75
|
|
|
* @return void |
76
|
|
|
* |
77
|
|
|
* @access public |
78
|
|
|
*/ |
79
|
1 |
|
public function unlock() |
80
|
|
|
{ |
81
|
1 |
|
$_SESSION[$this->key] = true; |
82
|
1 |
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Lock a session |
86
|
|
|
* |
87
|
|
|
* @return void |
88
|
|
|
* |
89
|
|
|
* @access public |
90
|
|
|
*/ |
91
|
1 |
|
public function lock() |
|
|
|
|
92
|
|
|
{ |
93
|
1 |
|
$_SESSION[$this->key] = false; |
94
|
1 |
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Set intent |
98
|
|
|
* |
99
|
|
|
* @param Request $request intended request |
100
|
|
|
* |
101
|
|
|
* @return void |
102
|
|
|
* |
103
|
|
|
* @access public |
104
|
|
|
*/ |
105
|
1 |
|
public function setIntent(Request $request) |
106
|
|
|
{ |
107
|
1 |
|
$_SESSION[$this->intent] = $request; |
108
|
1 |
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Get and clear intent |
112
|
|
|
* |
113
|
|
|
* @return null | Request |
114
|
|
|
* |
115
|
|
|
* @access public |
116
|
|
|
*/ |
117
|
1 |
|
public function getIntent() |
118
|
|
|
{ |
119
|
1 |
|
if (isset($_SESSION[$this->intent])) { |
120
|
1 |
|
$intent = $_SESSION[$this->intent]; |
121
|
1 |
|
unset($_SESSION[$this->intent]); |
122
|
1 |
|
return $intent; |
123
|
|
|
} |
124
|
1 |
|
return null; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Is session unlocked? |
129
|
|
|
* |
130
|
|
|
* @return bool |
131
|
|
|
* |
132
|
|
|
* @access public |
133
|
|
|
*/ |
134
|
1 |
|
public function isUnlocked() |
135
|
|
|
{ |
136
|
1 |
|
return isset($_SESSION[$this->key]) |
137
|
1 |
|
&& true === $_SESSION[$this->key]; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Resume session |
142
|
|
|
* |
143
|
|
|
* @return bool |
144
|
|
|
* |
145
|
|
|
* @access protected |
146
|
|
|
*/ |
147
|
2 |
|
protected function session() |
148
|
|
|
{ |
149
|
2 |
|
return (session_id() !== '') ?: session_start(); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|