1
|
|
|
<?php |
2
|
|
|
namespace Sesshin\Id; |
3
|
|
|
|
4
|
|
|
use Sesshin\EntropyGenerator\EntropyGeneratorInterface; |
5
|
|
|
use Sesshin\EntropyGenerator\Uniq; |
6
|
|
|
use Sesshin\Exception; |
7
|
|
|
use Sesshin\Id\Store\StoreInterface; |
8
|
|
|
use Sesshin\Id\Store\Cookie as CookieStore; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Session id handler (store, entropy generator, hash algo...). |
12
|
|
|
*/ |
13
|
|
|
class Handler |
14
|
|
|
{ |
15
|
|
|
/** @var StoreInterface */ |
16
|
|
|
private $idStore; |
17
|
|
|
|
18
|
|
|
/** @var EntropyGeneratorInterface */ |
19
|
|
|
private $entropyGenerator; |
20
|
|
|
|
21
|
|
|
/** @var string Hash algo used to generate session ID (it hashes entropy). */ |
22
|
|
|
private $hashAlgo = 'sha1'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param StoreInterface $idStore |
26
|
|
|
*/ |
27
|
|
|
public function setIdStore(StoreInterface $idStore) |
28
|
|
|
{ |
29
|
|
|
$this->idStore = $idStore; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @return StoreInterface |
34
|
|
|
*/ |
35
|
|
|
public function getIdStore() |
36
|
|
|
{ |
37
|
|
|
if (!$this->idStore) { |
38
|
|
|
$this->idStore = new CookieStore(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
return $this->idStore; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Sets entropy that is used to generate session id. |
46
|
|
|
* |
47
|
|
|
* @param EntropyGeneratorInterface $entropyGenerator |
48
|
|
|
*/ |
49
|
|
|
public function setEntropyGenerator(EntropyGeneratorInterface $entropyGenerator) |
50
|
|
|
{ |
51
|
|
|
$this->entropyGenerator = $entropyGenerator; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return EntropyGeneratorInterface |
56
|
|
|
*/ |
57
|
|
|
public function getEntropyGenerator() |
58
|
|
|
{ |
59
|
|
|
if (!$this->entropyGenerator) { |
60
|
|
|
$this->entropyGenerator = new Uniq(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return $this->entropyGenerator; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param string $algo Hash algorith accepted by hash extension. |
68
|
|
|
* @throws Exception |
69
|
|
|
*/ |
70
|
|
|
public function setHashAlgo($algo) |
71
|
|
|
{ |
72
|
|
|
if (in_array($algo, hash_algos())) { |
73
|
|
|
$this->hashAlgo = $algo; |
74
|
|
|
} else { |
75
|
|
|
throw new Exception('Provided algo is not valid (not on hash_algos() list)'); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
|
|
public function getHashAlgo() |
83
|
|
|
{ |
84
|
|
|
return $this->hashAlgo; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
|
|
public function generateId() |
91
|
|
|
{ |
92
|
|
|
$id = hash($this->getHashAlgo(), $this->getEntropyGenerator()->generate()); |
93
|
|
|
$this->setId($id); |
94
|
|
|
|
95
|
|
|
return $this->getId(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param string $id |
100
|
|
|
*/ |
101
|
|
|
public function setId($id) |
102
|
|
|
{ |
103
|
|
|
$this->getIdStore()->setId($id); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return string |
108
|
|
|
*/ |
109
|
|
|
public function getId() |
110
|
|
|
{ |
111
|
|
|
return $this->getIdStore()->getId(); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return bool |
116
|
|
|
*/ |
117
|
|
|
public function issetId() |
118
|
|
|
{ |
119
|
|
|
return $this->getIdStore()->issetId(); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @return void |
124
|
|
|
*/ |
125
|
|
|
public function unsetId() |
126
|
|
|
{ |
127
|
|
|
$this->getIdStore()->unsetId(); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|