false, 'errorCodes' => 'missing-input', ]; } $request = \Config\Services::request(); $remoteIp = $request->getIPAddress(); if ($this->config->captchaType === 'recaptcha') { $siteUrl = 'https://www.google.com/recaptcha/api/siteverify'; $request = $this->submitGetCaptcha( $siteUrl, [ 'secret' => $this->config->captchaSecret, 'remoteip' => $remoteIp, 'response' => $response, 'version' => 'php_1.0.0', ]); } else if ($this->config->captchaType === 'hcaptcha') { $siteUrl = 'https://hcaptcha.com/siteverify'; $request = $this->submitPostCaptcha( $siteUrl, [ 'secret' => $this->config->captchaSecret, 'response' => $response, 'remoteip' => $remoteIp, ]); } $answer = json_decode($request, true); if (ENVIRONMENT === 'testing' && $response === 'testing') { $answer = ['success' => true]; } if ($answer['success'] !== true) { return [ 'success' => false, 'errorCodes' => $answer['error-codes'], ]; } return ['success' => true]; } /** * Generate CAPTCHA HTML * * @return string */ public function generateCaptchaHtml() { $content = ''; if ($this->config->loginProtection && $this->config->captchaEnabled && $this->isCaptchaRequired()) { $siteKey = $this->config->captchaSiteKey; if ($this->config->captchaType === 'recaptcha') { $content = '
'; $content .= ''; } else if ($this->config->captchaType === 'hcaptcha') { $content = ''; $content .= ''; } } return $content; } /** * Is CAPTCHA Required * * @return boolean */ public function isCaptchaRequired() { $loginAttemptModel = $this->getModel('LoginAttempt'); return $loginAttemptModel->find() >= $this->config->captchaLoginAttempts; } /** * Submit GET CAPTCHA * * Submits an HTTP GET to a CAPTCHA server. * * @param string $url URL path to CAPTCHA server. * @param array $data Array of parameters to be sent. * * @return string */ private function submitGetCaptcha(string $url, array $data) { $client = \Config\Services::curlrequest(); $response = $client->request('GET', $url, [ 'query' => $data, ]); return $response->getBody(); } /** * Submit POST CAPTCHA * * Submits an HTTP POST to a CAPTCHA server. * * @param string $url URL path to CAPTCHA server. * @param array $data Array of parameters to be sent. * * @return string */ private function submitPostCaptcha(string $url, array $data) { $client = \Config\Services::curlrequest(); $response = $client->request('POST', $url, [ 'query' => $data, ]); return $response->getBody(); } }