/** * Get service instance * * @param $type * @return mixed * @throws \Exception */ public function getService($type) { if (empty($this->params)) { $this->params = $this->getParams(); } if ($type == 'captcha') { if (!isset($this->params['captcha'])) { throw new ParamsNotFoundException('Google captcha params not exist'); } if (!isset($this->services['captcha'])) { if (!function_exists('curl_version')) { throw new \Exception('Curl library is not installed'); } $captchaConfig = new CaptchaConfiguration(); $captchaConfig->build($this->params['captcha']); $this->services['captcha'] = new CaptchaBuilder($captchaConfig); } return $this->services['captcha']; } else { throw new \Exception('Service type ' . $type . ' not exist'); } }
/** * Check Response with form data * * @param $formData * @return array */ public function check($formData) { $ch = curl_init(); $timeout = 5; $url = $this->config->getRequestUrl() . '?secret=' . $this->config->getSecretKey() . '&response=' . $formData . "&remoteip=" . $_SERVER['REMOTE_ADDR']; curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); if (!curl_errno($ch)) { $googleResponse = curl_exec($ch); $googleResponse = json_decode($googleResponse); if ($googleResponse->status == true) { $response = ['status' => true, 'message' => 'Checked']; } else { $response = ['status' => false, 'message' => 'Captcha security code is not valid']; } } else { $response = ['status' => false, 'message' => 'Curl error: ' . curl_error($ch)]; } curl_close($ch); return $response; }
/** * Render captcha * * @param CaptchaConfiguration $config * @return mixed */ public function setAction(CaptchaConfiguration $config) { $this->assign('siteKey', $config->getSiteKey()); return $this->renderPartial('captcha'); }