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->_submitGet( $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->_submitPost( $siteUrl, [ 'secret' => $this->config->captchaSecret, 'response' => $response, 'remoteip' => $remoteIp, ]); } $answer = json_decode($request, true); if (trim($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 = new LoginAttemptModel(); return $loginAttemptModel->find() >= $this->config->captchaLoginAttempts; } /** * Submit GET * * 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 array response */ private function _submitGet($url, $data) { $client = \Config\Services::curlrequest(); $response = $client->request('GET', $url, [ 'query' => $data, ]); return $response->getBody(); } /** * Submit POST * * 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 array response */ private function _submitPost($url, $data) { $client = \Config\Services::curlrequest(); $response = $client->request('POST', $url, [ 'query' => $data, ]); return $response->getBody(); } }