· 6 years ago · Aug 07, 2019, 05:18 AM
1 public function authenticate($attribute,$params)
2 {
3 if(!$this->hasErrors())
4 {
5 if(isset($_POST['g-recaptcha-response'])){
6 $captcha=$_POST['g-recaptcha-response'];
7 }
8 if(!$captcha) {
9 return $this->addError('password',Yii::t('mc', 'Invalid Captcha Submitted'));
10 }
11 $secretKey = "";
12 $ip = $_SERVER['REMOTE_ADDR'];
13 $url = 'https://www.google.com/recaptcha/api/siteverify?secret=' . urlencode($secretKey) . '&response=' . urlencode($captcha);
14 $curl = curl_init();
15 curl_setopt_array($curl, array(
16 CURLOPT_URL => 'https://www.google.com/recaptcha/api/siteverify?secret=' . urlencode($secretKey) . '&response=' . urlencode($captcha),
17 CURLOPT_RETURNTRANSFER => true,
18 CURLOPT_ENCODING => "",
19 CURLOPT_MAXREDIRS => 10,
20 CURLOPT_TIMEOUT => 30,
21 ));
22 $response = curl_exec($curl);
23 $response2 = json_decode($response, true);
24 print_r($response2);
25 curl_close($curl);
26 if($response2['success'] = 0) {
27 $this->addError('password',Yii::t('mc', 'Invalid Captcha Submitted 2'));
28 } else {
29 $this->_identity=new UserIdentity($this->name, $this->password);
30 $this->_identity->authenticate($this->ignoreIp, $this->gauthCode);
31 if ($this->_identity->errorCode === UserIdentity::ERROR_GAUTH_CODE_INVALID)
32 $this->addError('gauthCode', Yii::t('mc', 'Invalid authentication code.'));
33 else if ($this->_identity->errorCode !== UserIdentity::ERROR_NONE)
34 $this->addError('password',Yii::t('mc', 'Incorrect username or password.'));
35 }
36 }
37 }