简体   繁体   English

Grav 中带有 hCaptcha 的 Zammad 反馈表

[英]Zammad Feedbackform with hCaptcha in Grav

I am in the process of creating a website with Grav.我正在使用 Grav 创建网站。 I use Zammad as a ticket system and would like to include the feedback form on the page.我使用 Zammad 作为票务系统,并希望在页面上包含反馈表。 For this I use the API of Zammad "https://admin-docs.zammad.org/en/latest/channels/form.html".为此,我使用 Zammad 的 API “https://admin-docs.zammad.org/en/latest/channels/form.html”。 This works as far as it goes.这是可行的。 New tickets can be created via the form.可以通过表单创建新票证。 Now I would like to add a bot protection.现在我想添加一个机器人保护。 For this I have chosen hCaptcha.为此,我选择了 hCaptcha。 https://docs.hcaptcha.com/ (Google reCaptcha can be used without effort with the ready plugin "Form", but I don't want to use the Google reCaptcha). https://docs.hcaptcha.com/ (Google reCaptcha 可以通过现成的插件“Form”轻松使用,但我不想使用 Google reCaptcha)。 I have also already started to write a plugin for the hCaptcha, but I can not find the right entry to the API of Grav.我也已经开始写hCaptcha的插件了,但是找不到Grav的API的正确入口。

my current code:我当前的代码:

function onFormProcessed(Event $event){

    if(is_entered_data_valid()) {
        if(isset($_POST['h-captcha-response']) && !empty($_POST['h-captcha-response'])){
            $secret = "0x0000000000000000000000000000000000000000";
            $remote_address = $_SERVER['REMOTE_ADDR'];
            $verify_url = "https://hcaptcha.com/siteverify?secret=".$secret."&response=".$_POST['h-captcha-response']."&remoteip=".$remote_address;
                // This is hcaptcha url
                $response = file_get_contents($verify_url); # Get token from post data with key 'h-captcha-response' and Make a POST request with data payload to hCaptcha API endpoint
                $responseData = json_decode($response);
                $success_msg="";
                $err_msg="";
                if($responseData->success){
                    $success_msg = "You can process your login functionality";
                }else{
                    $err_msg =  "Something went wrong while hCaptcha Validation. Please try again after sometime.";
                }
            }else{
                $err_msg =  "Please fill all the required fields";
            }
        } else {
            // Server side validation failed
            $error_output = "Please fill all the required fields";
        }
        // Get the response and pass it into your ajax as a response.
        $return_msg = array(
            'error'     =>  $err_msg,
            'success'   =>  $success_msg
        );
        echo json_encode($return_msg);

    }

this function has to be executed when the form is submitted提交表单时必须执行此 function

You go in the right direction, onFormProcessed is the event you need to use.您 go 方向正确, onFormProcessed是您需要使用的事件。 You can learn from Grav's Form plugin.您可以从 Grav 的 Form 插件中学习。 However, you need to define a specific form action for your plugin, otherwise your code runs for all the forms on the site no matter the forms use your captcha or not.但是,您需要为您的插件定义一个特定的表单操作,否则您的代码将针对站点上的所有 forms 运行,无论 forms 是否使用您的验证码。

Let say your form action is hcaptcha :假设您的表单操作是hcaptcha

    public function onFormProcessed(Event $event): void
    {
        $form = $event['form'];
        $action = $event['action'];

        switch ($action) {
            case 'hcaptcha':
                // If captcha validation fails, stop the form processing.
                if ($validation_fails) {
                    $message = "Please solve the captcha!";

                    $this->grav->fireEvent('onFormValidationError', new Event([
                        'form' => $form,
                        'message' => $message
                    ]));

                    $event->stopPropagation();

                    return;
                 }
            

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM