| Total Complexity | 9 |
| Total Lines | 51 |
| Duplicated Lines | 0 % |
| Coverage | 21.05% |
| Changes | 0 | ||
| 1 | <?php |
||
| 22 | class NoCaptcha extends ACaptcha |
||
| 23 | { |
||
| 24 | protected static string $publicKey = ''; |
||
| 25 | protected static string $privateKey = ''; |
||
| 26 | |||
| 27 | public static function init(string $privateKey, string $publicKey): void |
||
| 28 | { |
||
| 29 | static::$publicKey = $publicKey; |
||
| 30 | static::$privateKey = $privateKey; |
||
| 31 | } |
||
| 32 | |||
| 33 | 2 | public function set(string $alias, string $errorMessage): self |
|
| 34 | { |
||
| 35 | 2 | $this->setEntry($alias); |
|
| 36 | 2 | parent::addRule(IRules::SATISFIES_CALLBACK, $errorMessage, [$this, 'checkNoCaptcha']); |
|
| 37 | 2 | return $this; |
|
| 38 | } |
||
| 39 | |||
| 40 | public function addRule(/** @scrutinizer ignore-unused */ string $ruleName, /** @scrutinizer ignore-unused */ string $errorText, /** @scrutinizer ignore-unused */ ...$args): void |
||
| 41 | { |
||
| 42 | // no additional rules applicable |
||
| 43 | } |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @param mixed $value |
||
| 47 | * @return bool |
||
| 48 | */ |
||
| 49 | public function checkNoCaptcha($value): bool |
||
| 50 | { |
||
| 51 | // entry has key: g-recaptcha-response |
||
| 52 | $response = strval(file_get_contents(NOCAPTCHA_API_SECURE_SERVER . '?secret=' . static::$privateKey . '&response=' . strval($value))); |
||
| 53 | $responseStructure = json_decode($response, true); |
||
| 54 | return !is_null($responseStructure) && !empty($responseStructure['success']) && true === $responseStructure['success']; |
||
| 55 | } |
||
| 56 | |||
| 57 | public function renderInput($attributes = null): string |
||
| 58 | { |
||
| 59 | return $this->canPass() ? '' : $this->getHtml(); |
||
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Gets the challenge HTML (javascript only version). |
||
| 64 | * This is called from the browser, and the resulting NoReCAPTCHA HTML widget |
||
| 65 | * is embedded within the HTML form it was called from. |
||
| 66 | * |
||
| 67 | * @return string - The HTML to be embedded in the user's form. |
||
| 68 | */ |
||
| 69 | protected function getHtml(): string |
||
| 73 | } |
||
| 74 | } |
||
| 75 |