|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* Library for confirmation tokens |
|
5
|
|
|
* |
|
6
|
|
|
* @link https://github.com/hiqdev/php-confirmator |
|
7
|
|
|
* @package php-confirmator |
|
8
|
|
|
* @license BSD-3-Clause |
|
9
|
|
|
* @copyright Copyright (c) 2016, HiQDev (http://hiqdev.com/) |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace hiqdev\php\confirmator; |
|
13
|
|
|
|
|
14
|
|
|
class Token |
|
15
|
|
|
{ |
|
16
|
|
|
protected $data = []; |
|
17
|
|
|
|
|
18
|
|
|
protected $string; |
|
19
|
|
|
|
|
20
|
3 |
|
public function __construct(array $data = [], $string = null) |
|
21
|
|
|
{ |
|
22
|
3 |
|
$this->data = $this->prepareData($data); |
|
23
|
3 |
|
$this->string = $string ?: $this->genString(); |
|
24
|
3 |
|
} |
|
25
|
|
|
|
|
26
|
3 |
|
protected function prepareData(array $data) |
|
27
|
|
|
{ |
|
28
|
3 |
|
if (empty($data['notAfter'])) { |
|
29
|
1 |
|
$data['notAfter'] = '+ 1 week'; |
|
30
|
1 |
|
} |
|
31
|
3 |
|
foreach ($data as $key => &$value) { |
|
32
|
3 |
|
if ($key === 'notAfter' || $key === 'notBefore') { |
|
33
|
3 |
|
$value = date('Y-m-d H:i:s', strtotime($value)); |
|
34
|
3 |
|
} |
|
35
|
3 |
|
} |
|
36
|
|
|
|
|
37
|
3 |
|
return $data; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
3 |
|
public function __toString() |
|
41
|
|
|
{ |
|
42
|
3 |
|
return $this->string; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
3 |
|
public function genString($length = 32) |
|
46
|
|
|
{ |
|
47
|
3 |
|
$res = ''; |
|
48
|
3 |
|
$chars = 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
|
49
|
3 |
|
$last = strlen($chars) - 1; |
|
50
|
3 |
|
for ($i = 0; $i < $length; ++$i) { |
|
51
|
3 |
|
$res .= $chars[mt_rand(0, $last)]; |
|
52
|
3 |
|
} |
|
53
|
|
|
|
|
54
|
3 |
|
return $res; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
2 |
|
public function get($key, $default = null) |
|
58
|
|
|
{ |
|
59
|
2 |
|
return isset($this->data[$key]) ? $this->data[$key] : $default; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
3 |
|
public function mget() |
|
63
|
|
|
{ |
|
64
|
3 |
|
return $this->data; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
2 |
|
public function check(array $data) |
|
68
|
|
|
{ |
|
69
|
2 |
|
return $this->checkNotAfter() && $this->checkNotBefore() && $this->checkData($data); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
1 |
|
public function checkData(array $data) |
|
73
|
|
|
{ |
|
74
|
1 |
|
foreach ($data as $key => $value) { |
|
75
|
1 |
|
if ($value !== $this->get($key)) { |
|
76
|
1 |
|
return false; |
|
77
|
|
|
} |
|
78
|
1 |
|
} |
|
79
|
|
|
|
|
80
|
1 |
|
return true; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
2 |
|
public function checkNotBefore() |
|
84
|
|
|
{ |
|
85
|
2 |
|
if (empty($this->data['notBefore'])) { |
|
86
|
|
|
return true; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
2 |
|
return time() > strtotime($this->data['notBefore']); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
2 |
|
public function checkNotAfter() |
|
93
|
|
|
{ |
|
94
|
2 |
|
if (empty($this->data['notAfter'])) { |
|
95
|
|
|
return true; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
2 |
|
return time() < strtotime($this->data['notAfter']); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|