-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1ca7193
commit 94a1b64
Showing
7 changed files
with
104 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
assets/snippets/FormLister/lib/captcha/yandexCaptcha/wrapper.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
<?php | ||
|
||
use FormLister\CaptchaInterface; | ||
use FormLister\Core; | ||
|
||
/** | ||
* Class ReCaptchaWrapper | ||
*/ | ||
class YandexCaptchaWrapper implements CaptchaInterface | ||
{ | ||
/** | ||
* @var array $cfg | ||
* id, secretKey, siteKey | ||
*/ | ||
public $cfg = null; | ||
protected $modx = null; | ||
|
||
/** | ||
* modxCaptchaWrapper constructor. | ||
* @param $modx | ||
* @param $cfg | ||
*/ | ||
public function __construct(\DocumentParser $modx, $cfg = []) | ||
{ | ||
$this->cfg = $cfg; | ||
$this->modx = $modx; | ||
} | ||
|
||
/** | ||
* Устанавливает значение капчи | ||
* @return mixed | ||
*/ | ||
public function init() | ||
{ | ||
return; | ||
} | ||
|
||
/** | ||
* Плейсхолдер капчи для вывода в шаблон | ||
* Может быть ссылкой на коннектор (чтобы можно было обновлять c помощью js), может быть сразу картинкой в base64 | ||
* @return string | ||
*/ | ||
public function getPlaceholder() | ||
{ | ||
$siteKey = \APIhelpers::getkey($this->cfg, 'siteKey'); | ||
$id = \APIhelpers::getkey($this->cfg, 'id'); | ||
$id = $id ? ('id="' . $id . '-captcha"') : ''; | ||
$out = ''; | ||
if (!empty($siteKey)) { | ||
$out = "<div {$id} data-sitekey=\"{$siteKey}\" class=\"smart-captcha\"></div>"; | ||
} | ||
|
||
return $out; | ||
} | ||
|
||
/** | ||
* @param \FormLister\Core $FormLister | ||
* @param $value | ||
* @param \FormLister\CaptchaInterface $captcha | ||
* @return bool|string | ||
*/ | ||
public static function validate(Core $FormLister, $value, CaptchaInterface $captcha) | ||
{ | ||
$secretKey = \APIhelpers::getkey($captcha->cfg, 'secretKey'); | ||
$params = http_build_query([ | ||
'secret' => $secretKey, | ||
'token' => $value, | ||
'ip' => \APIhelpers::getUserIP() | ||
]); | ||
$url = "https://smartcaptcha.yandexcloud.net/validate?{$params}"; | ||
$out = false; | ||
if (!empty($value)) { | ||
$curl = curl_init(); | ||
curl_setopt($curl, CURLOPT_URL, $url); | ||
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); | ||
curl_setopt($curl, CURLOPT_TIMEOUT, 10); | ||
curl_setopt($curl, CURLOPT_USERAGENT, | ||
"Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.16) Gecko/20110319 Firefox/3.6.16"); | ||
$response = curl_exec($curl); | ||
curl_close($curl); | ||
$response = json_decode($response, true) ?? []; | ||
$out = isset($response['status']) && $response['status'] === 'ok'; | ||
} | ||
if (!$out) { | ||
$out = \APIhelpers::getkey($captcha->cfg, 'errorCodeFailed', 'Вы не прошли проверку'); | ||
} | ||
$FormLister->log('YandexCaptcha validation result: ' . $out); | ||
|
||
return $out; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters