1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\kw_forms\Controls\Security\Captcha; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use kalanis\kw_rules\Interfaces\IRules; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* The NOCAPTCHA server URL's |
10
|
|
|
*/ |
11
|
1 |
|
define('NOCAPTCHA_API_SERVER', 'https://www.google.com/recaptcha/api.js'); |
12
|
1 |
|
define('NOCAPTCHA_API_SECURE_SERVER', 'https://www.google.com/recaptcha/api/siteverify'); |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class NoCaptcha |
17
|
|
|
* @package kalanis\kw_forms\Controls\Security\Captcha |
18
|
|
|
* Define NoCaptcha service to render captcha |
19
|
|
|
* Uses service from Google to check if response contains correct answer |
20
|
|
|
* @codeCoverageIgnore remote service |
21
|
|
|
*/ |
22
|
|
|
class NoCaptcha extends ACaptcha |
23
|
|
|
{ |
24
|
|
|
/** @var string */ |
25
|
|
|
protected static $publicKey = ''; |
26
|
|
|
/** @var string */ |
27
|
|
|
protected static $privateKey = ''; |
28
|
|
|
|
29
|
|
|
public static function init(string $privateKey, string $publicKey): void |
30
|
|
|
{ |
31
|
|
|
static::$publicKey = $publicKey; |
32
|
|
|
static::$privateKey = $privateKey; |
33
|
|
|
} |
34
|
|
|
|
35
|
2 |
|
public function set(string $alias, string $errorMessage): self |
36
|
|
|
{ |
37
|
2 |
|
$this->setEntry($alias); |
38
|
2 |
|
parent::addRule(IRules::SATISFIES_CALLBACK, $errorMessage, [$this, 'checkNoCaptcha']); |
39
|
2 |
|
return $this; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function addRule(/** @scrutinizer ignore-unused */ string $ruleName, /** @scrutinizer ignore-unused */ string $errorText, /** @scrutinizer ignore-unused */ ...$args): void |
43
|
|
|
{ |
44
|
|
|
// no additional rules applicable |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param mixed $value |
49
|
|
|
* @return bool |
50
|
|
|
*/ |
51
|
|
|
public function checkNoCaptcha($value): bool |
52
|
|
|
{ |
53
|
|
|
// entry has key: g-recaptcha-response |
54
|
|
|
$response = strval(file_get_contents(NOCAPTCHA_API_SECURE_SERVER . '?secret=' . static::$privateKey . '&response=' . strval($value))); |
55
|
|
|
$responseStructure = json_decode($response, true); |
56
|
|
|
return !is_null($responseStructure) && !empty($responseStructure['success']) && true === $responseStructure['success']; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function renderInput($attributes = null): string |
60
|
|
|
{ |
61
|
|
|
return $this->canPass() ? '' : $this->getHtml(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Gets the challenge HTML (javascript only version). |
66
|
|
|
* This is called from the browser, and the resulting NoReCAPTCHA HTML widget |
67
|
|
|
* is embedded within the HTML form it was called from. |
68
|
|
|
* |
69
|
|
|
* @return string - The HTML to be embedded in the user's form. |
70
|
|
|
*/ |
71
|
|
|
protected function getHtml(): string |
72
|
|
|
{ |
73
|
|
|
return '<script src="' . NOCAPTCHA_API_SERVER . '"></script> |
74
|
|
|
<div class="g-recaptcha" data-sitekey="' . static::$publicKey. '"></div>'; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|