receiving response reCaptcha 2

40 Views Asked by At

how i can integrate checking-receiving answer from google server about captcha, i cant find sending code. i readed manuals, but dont understand yet i using Joomla 3

<?php

if(!defined("DS_FORM_LOAD") || DS_FORM_LOAD!==true) die();

class DSFormSend extends DSMain {

    public $error = array();
    public $im = 0;
    public $mail;
    
    function __construct() {
        $this->request();
        $this->getConfig();
    }

    public function index() {

        if (isset($this->post['formid']) && !empty($this->post['formid'])) {
            $this->formID = $this->post['formid'];
        } else {
            throw new Exception("form ID", 1);
        }

        if (!$this->formConfig->getConfig($this->formID)) throw new Exception("file config form", 1);

            foreach ($this->formConfig->formFields as $field) {
                if(isset($field['attributs']['required'])) {
                    if(!isset($field['attributs']['pattern'])) $field['attributs']['pattern'] = '';
                    if(!isset($field['error'])) $field['error'] = '';

                    if(!isset($this->post[$field['attributs']['name']])) {
                        $this->error[$name] = 1;
                    } else {
                        $this->validate($field['attributs']['pattern'], $this->post[$field['attributs']['name']],$field['attributs']['name'],$field['error']);
                    }
                }
            }

            if (isset($this->error) && sizeof($this->error)) {
                $this->error['error'] = 1;
                $this->error['formid'] = $this->formID;
                $this->responseJson($this->error);
                exit();
            }

            $this->mail = new PHPMailer;

            $message = array();

            foreach ($this->formConfig->formFields as $field) {

                if(isset($field['formail']) && !empty($field['formail'])) {
                    $field['attributs']['name'] = preg_replace('|\[[^\]]*\]|siU', '', $field['attributs']['name']);

                    $field_name = $field['attributs']['name'];
                    if (!isset($this->post[$field_name]) && !isset($this->files[$field_name])) {
                        continue;
                    } else if (isset($this->post[$field_name]) && is_array($this->post[$field_name]) && !isset($this->post[$field_name][0])) {
                        array_shift($this->post[$field_name]);
                        continue;
                    }


                    if(isset($field['name_mail']) && !empty($field['name_mail'])) {
                        $message[$this->im]['name'] = $field['name_mail'];

                    } elseif(isset($field['label']) && !empty($field['label'])) {
                        $field['label'] = str_replace('(*)', '', $field['label']);
                        $message[$this->im]['name'] = trim($field['label']);
                    } else $message[$this->im]['name'] = '';

        
                    switch ($field['type']) {
                        case 'input':
                             $message = $this->inputModel($field['attributs']['type'],$field['attributs']['name'],$message);
                        break;
                        case 'textarea':
                             $message = $this->textareaModel($field['attributs']['name'],$message);
                        break;
                        case 'freearea':
                            $message[$this->im]['message'] = $field['value'];
                        break;
                        case 'select':
                            $message = $this->selectModel($field['attributs']['name'], $message);
                        break;
                    }
                    $this->im++;
                }
            }

        

        

        if(@$this->mail->send()){
            $this->responseJson(
                    array(
                        'error'      => 0,
                        'formid'     => $this->formID,
                        'error_text' => $this->renderTemplate('goodmail', $data),
                    )
            );
        } else {
            $this->responseJson(
                    array(
                        'error'      => 2,
                        'formid'     => $this->formID,
                        'error_text' => $this->renderTemplate('badmail', $data),
                    )
            );
        }

        if($this->formConfig->mailReverseEmail && isset($this->post['email']) && !empty($this->post['email'])) {
            $this->mail->clearAddresses();
            $this->mail->clearCCs();
            $this->mail->Body = $this->renderTemplate('reversemail', $data);
            $this->mail->addAddress($_POST['email']);
            @$this->mail->send();
        }
    }

    public function validate($pattern, $value, $name, $errorField) {
        if (extension_loaded('mbstring')) {
            $valueStrlen = mb_strlen($value, $this->formConfig->charset);
        } else {
            $valueStrlen = strlen($value);
        }
        if ((empty($value) 
            || ($this->formConfig->validateStrlen > 0 && $valueStrlen < $this->formConfig->validateStrlen))
            || (!empty($pattern) && !preg_match('/'.$pattern.'/', $value))) {

            $this->error[$name] = 1;
        }

        if(!empty($errorField) && !empty($this->error[$name])) {
            $this->error[$name] = $errorField;
        } elseif(!empty($this->error[$name])) {
            $this->error[$name] = $this->formConfig->validateError;
        }
        return;
    }

    

    
}


?>

i try put this code, but i dont understand in which place i should to put( maybe i search in the not right place

$error = true;
$secret = 'СЕКРЕТНЫЙ_КЛЮЧ';
 
if (!empty($_POST['g-recaptcha-response'])) {
    $curl = curl_init('https://www.google.com/recaptcha/api/siteverify');
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, 'secret=' . $secret . '&response=' . $_POST['g-recaptcha-response']);
    $out = curl_exec($curl);
    curl_close($curl);
    
    $out = json_decode($out);
    if ($out->success == true) {
        $error = false;
    } 
}    
 
if ($error) {
    echo 'Ошибка заполнения капчи.';
}

i trying integration, but dont find right place

0

There are 0 best solutions below