/
easy_captcha_persistent_grant.php
86 lines (76 loc) · 2.48 KB
/
easy_captcha_persistent_grant.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
/*
* vim:set softtabstop=4 shiftwidth=4 expandtab:
*
* LICENSE: GNU Affero General Public License, version 3 (AGPL-3.0-or-later)
* Copyright 2001 - 2020 Ampache.org
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
declare(strict_types=0);
namespace Ampache\Module\Util\Captcha;
use Ampache\Config\AmpConfig;
/**
* Class easy_captcha_persistent_grant
* shortcut, allow access for an user if captcha was previously solved
* (should be identical in each instantiation, cookie is time-bombed)
*/
class easy_captcha_persistent_grant extends easy_captcha
{
public function __construct($captcha_id = null, $ignore_expiration = 0)
{
}
/**
* @param integer $input
* @return boolean
*/
public function solved($input = 0)
{
if (CAPTCHA_PERSISTENT && filter_has_var(INPUT_COOKIE, $this->cookie())) {
return in_array($_COOKIE[$this->cookie()], array($this->validity_token(), $this->validity_token(-1)));
}
return false;
}
#-- set captcha persistence cookie
public function grant()
{
if (!headers_sent()) {
$cookie_options = [
'expires' => time() + 175 * CAPTCHA_TIMEOUT,
'path' => AmpConfig::get('cookie_path'),
'domain' => AmpConfig::get('cookie_domain'),
'secure' => make_bool(AmpConfig::get('cookie_secure')),
'samesite' => 'Strict'
];
setcookie($this->cookie(), $this->validity_token(), $cookie_options);
}
}
#-- pseudo password (time-bombed)
/**
* @param integer $deviation
* @return string
*/
public function validity_token($deviation = 0)
{
return easy_captcha::hash("PERSISTENCE", $deviation, $length = 100);
}
/**
* @return string
*/
public function cookie()
{
return "captcha_pass";
}
}