Joomla,Sp Page Builder和ReCaptcha

问题描述 投票:0回答:2

这是我第一次将SP Page Builder组件与Joomla一起使用。我想使用他们的联系表单,但它不支持Google ReCaptcha。我已经足够好了编码,以为我可以手动将其添加到:/com_sppagebuilder/addons/ajax_contact/site.php并让它工作。我添加了:<div class="g-recaptcha" data-sitekey="My_Key"></div>;和Joomla ReCaptcha插件被激活。我不知道是否必须这样做,但是我将<script src='https://www.google.com/recaptcha/api.js'></script>添加到了head标签中。有了这个,ReCaptcha显示正常。

我的问题在于验证。我确实尝试在site.php中添加一些验证代码,但我相信SP Page Builder使用JFactory :: getMailer();把电子邮件准备好并发送给我,我对此一无所知。

因此,我不知道在哪里可以添加我的ReCaptcha验证代码,因为我在网上找到了该代码的几个版本,我真的不知道使用哪一个。我一直在寻找如何进行验证的一些答案...我尝试了很多东西,但它仍然没有用。

任何人都可以帮我解决这个问题吗?

非常感谢你 !

编辑

我认为我的问题不够明确:

我想添加Recaptcha,它已经在我的网站上以其他形式工作正常(因此它不是Joomla问题的配置)。我想使用以下SP Page Builder联系表单而不是本网站其他部分的RSFormPro。验证过程应围绕此部分进行,但我尝试添加Google验证码,并尝试了我在互联网上找到的几个版本,并且它根本不起作用:

public static function getAjax() {
    $input = JFactory::getApplication()->input;
    $mail = JFactory::getMailer();

    //inputs
    $inputs             = $input->get('data', array(), 'ARRAY');

    foreach ($inputs as $input) {

        if( $input['name'] == 'recipient' ) {
            $recipient          = base64_decode($input['value']);
        }

        if( $input['name'] == 'email' ) {
            $email          = $input['value'];
        }

        if( $input['name'] == 'name' ) {
            $name           = $input['value'];
        }

        if( $input['name'] == 'subject' ) {
            $subject            = $input['value'];
        }

        if( $input['name'] == 'message' ) {
            $message            = nl2br( $input['value'] );
        }
    }

    /*Try at the validation*/
    $captcha_plugin = JFactory::getConfig()->get('captcha');
    if ($captcha_plugin != '0') {
    $captcha = JCaptcha::getInstance($captcha_plugin);
    $field_id = 'google-recaptcha';
     print $captcha->display($field_id, $field_id, 'g-recaptcha');
    }


    $sender = array($email, $name);
    $mail->setSender($sender);
    $mail->addRecipient($recipient);
    $mail->setSubject($subject);
    $mail->isHTML(true);
    $mail->Encoding = 'base64';
    $mail->setBody($message);

    if ($mail->Send()) {
        return '<span class="sppb-text-success">'. JText::_('COM_SPPAGEBUILDER_ADDON_AJAX_CONTACT_SUCCESS') .'</span>';
    } else {
        return '<span class="sppb-text-danger">'. JText::_('COM_SPPAGEBUILDER_ADDON_AJAX_CONTACT_FAILED') .'</span>';
    }
}

有什么建议 ?

谢谢 !

joomla captcha contact-form recaptcha
2个回答
0
投票

我已成功将captcha添加到Ajax联系表单中。虽然代码仍然需要一些改进,但这就是我所做的:

看到我正在使用n3tseznamcaptcha captcha插件,我仍然需要实现其他验证码的更改才能工作(recaptcha等),但您可以根据自己的需要进行调整。

SP Pagebuilder插件包含两个文件:admin和site。在管理站点中,我删除了默认的验证码,缺少许多安全措施。然后在网站部分我添加了以下内容:

function ajax_contact_addon($atts)
{
    global $formcaptcha;
    (...)
    if($formcaptcha) 
    {
            // TODO: Add Joomla's captcha:
            JPluginHelper::importPlugin('captcha');
            $dispatcher = JDispatcher::getInstance();

            // This will put the code to load CAPTCHA's JavaScript file into your <head>
            $dispatcher->trigger('onInit', 'dynamic_captcha_1');

            // This will return the array of HTML code.
            $captcha = $dispatcher->trigger('onDisplay', array(null, 'dynamic_captcha_1', 'class=""'));
            // I have only 1 recaptcha plugin enabled so the HTML is at 0 index, this will be improved in next version, following the contact component
            $output .= (isset($captcha[0])) ? $captcha[0] : '';
            $output .= '<div class="clearfix"></div><p></p>';
    }
    (...)
}

function ajax_contact_get_ajax() 
{
    global $formcaptcha;

    $jinput         = JFactory::getApplication()->input;
    $mail           = JFactory::getMailer();
    $config         = JFactory::getConfig();

    // TODO: CHECK CAPTCHA and add a Helper Class to get the captchas fields
    $captchaset = 'n3tseznamcaptcha';
    if ($captchaset === 'n3tseznamcaptcha')
    {   
        $captcha_field_hash = 'n3t_seznam_captcha_hash';
        $captcha_field_answer = 'n3t_seznam_captcha';
    }

    //inputs
    $inputs             = $jinput->get('data', array(), 'ARRAY');
    foreach ($inputs as $input) 
    {
            if( $input['name'] == 'title' ) 
            {
                $title                  = $input['value'];
            }
            if( $input['name'] == 'recipient' ) 
            {
                    $recipient      = base64_decode($input['value']);
            }

            if( $input['name'] == 'email' ) 
            {
                    $email          = $input['value'];
            }

            (...)

            if( $input['name'] == $captcha_field_hash ) 
            {
                    $captcha_hash   = $input['value'];
            }

            if( $input['name'] == $captcha_field_answer ) 
            {
                    $captcha_answer     = $input['value'];
            }
    }

    if($formcaptcha)
    {
            // get the plugin
            JPluginHelper::importPlugin('captcha');
            $dispatcher = JEventDispatcher::getInstance();

            // In order the plugin can check the code, we have to insert it into the request data:
            $jinput->set('n3t_seznam_captcha_hash', $captcha_hash);
            $jinput->set('n3t_seznam_captcha', $captcha_answer);

            // Here we check for the code:
            $res = $dispatcher->trigger('onCheckAnswer', $captcha_answer);

            if(!$res[0])
            {
                    // There is a problem with pagebuilder cache and captchas, so we need to clean the cache, to renew the captcha code:
                    $cache = JFactory::getCache('page');
                    $cache->clean();
                    return '<span class="pb-text-danger">'. JText::_('COM_PAGEBUILDER_ADDON_AJAX_FORM_WRONG_CAPTCHA') .'</span>';
            }
}

}

我认为就是这样。希望它能帮助你找到答案。


0
投票

所以在这里你有完整的解决方案。请注意,我也改变了管理员方面的一些事情。此版本将显示并验证在Joomla配置中选择的默认验证码,但它仅适用于recaptcha和n3tsezam ..其他应手动添加。原因是这个插件没有在请求中添加挑战和响应字段,验证码用于验证,因此我们需要检索它并写入我们的ajax函数(ajax_contact_get_ajax)中的jinput,以及整个复杂化的原因事实是每个验证码插件都使用不同的字段。无论如何..如果你需要任何其他验证码插件只是可以将它添加到交换机,你应该完成。

function ajax_contact_addon($ atts){

        extract(AddonAtts(array(
                "title"             => '',
                "show_title"                    => '',
                "heading_selector"      => 'h3',
                "title_fontsize"        => '',
                "title_fontweight"      => '',
                "title_text_color"      => '',
                "title_margin_top"      => '',
                "title_margin_bottom"           => '',  
                "recipient_email"       => '',
                "formcaptcha"           => '',
                "class"             => '',
                ), $atts));

        JHtml::script('media/com_pagebuilder/js/ajax-contact.js');        

//      There is a problem with pagebuilder cache and captchas
        $cache = JFactory::getCache('page');
        $cache->clean();

        $output  = '<div class="pb-addon pb-addon-ajax-contact ' . $class . '">';

        if(boolval($show_title) && $title) 
        {

                $title_style = '';
                if($title_margin_top !='') $title_style .= 'margin-top:' . (int) $title_margin_top . 'px;';
                if($title_margin_bottom !='') $title_style .= 'margin-bottom:' . (int) $title_margin_bottom . 'px;';
                if($title_text_color) $title_style .= 'color:' . $title_text_color  . ';';
                if($title_fontsize) $title_style .= 'font-size:'.$title_fontsize.'px;line-height:'.$title_fontsize.'px;';
                if($title_fontweight) $title_style .= 'font-weight:'.$title_fontweight.';';

                $output .= '<'.$heading_selector.' class="pb-addon-title" style="' . $title_style . '">' . $title . '</'.$heading_selector.'>';
        }

        $output .= '<div class="pb-addon-content">';
        $output .= '<form class="pb-ajax-contact-form">';

        $output .= '<div class="pb-form-group">';
        $output .= '<input type="text" name="name" class="pb-form-control" placeholder="'. JText::_('COM_PAGEBUILDER_ADDON_AJAX_CONTACT_NAME') .'" required="required">';
        $output .= '</div>';

        $output .= '<div class="pb-form-group">';
        $output .= '<input type="email" name="email" class="pb-form-control" placeholder="'. JText::_('COM_PAGEBUILDER_ADDON_AJAX_CONTACT_EMAIL') .'" required="required">';
        $output .= '</div>';

        $output .= '<div class="pb-form-group">';
        $output .= '<input type="text" name="subject" class="pb-form-control" placeholder="'. JText::_('COM_PAGEBUILDER_ADDON_AJAX_CONTACT_SUBJECT') .'" required="required">';
        $output .= '</div>';

        $output .= '<div class="pb-form-group">';
        $output .= '<textarea type="text" name="message" rows="5" class="pb-form-control" placeholder="'. JText::_('COM_PAGEBUILDER_ADDON_AJAX_CONTACT_MESSAGE') .'" required="required"></textarea>';
        $output .= '</div>';

        if($formcaptcha) 
        {
                JPluginHelper::importPlugin('captcha');
                $dispatcher = JDispatcher::getInstance();
                $dispatcher->trigger('onInit', 'dynamic_captcha_1');

                $captchas = $dispatcher->trigger('onDisplay', array(null, 'dynamic_captcha_1', 'class=""'));
                $index = 0;

                foreach (JPluginHelper::getPlugin('captcha') as $plugin)
                {
                        if (JFactory::getApplication()->get('captcha', '0') === $plugin->name)
                        {
                                $captcha = $captchas[$index];
                                break;
                        }
                        $index++;
                }
                $output .= (isset($captcha)) ? $captcha : '';
                $output .= '<div class="clearfix"></div><p></p>';
        }

        $output .= '<input type="hidden" name="recipient" value="'. base64_encode($recipient_email) .'">';
        $output .= '<input type="hidden" name="title" value="'. $title .'">';

        $output .= '<button type="submit" class="btn btn-default"><i class="fa"></i> '. JText::_('COM_PAGEBUILDER_ADDON_AJAX_CONTACT_SEND') .'</button>';

        $output .= '</form>';

        $output .= '<div style="display:none;margin-top:10px;" class="pb-ajax-contact-status"></div>';

        $output .= '</div>';

        $output .= '</div>';

        return $output;

}

function ajax_contact_get_ajax() 
{
        $config                         = JFactory::getConfig();
        $jinput             = JFactory::getApplication()->input;
        //inputs
        $inputs             = $jinput->get('data', array(), 'ARRAY');

        $mail               = JFactory::getMailer();

        // TODO: Find the way to check if captcha is enabled in the addon
        $formcaptcha = true;
        $message = "";

        // TODO: CHECK CAPTCHA and add a Helper Class to get the captchas
        switch (JFactory::getApplication()->get('captcha', '0'))
        {
            case 'recaptcha':
                // v.1:
                //$captcha_challenge_field = 'recaptcha_challenge_field';
                //$captcha_answer_field = 'recaptcha_response_field';
                // v.2:
                $captcha_challenge_field = '';
                $captcha_answer_field = 'g-recaptcha-response';
                break;
            case 'n3tseznamcaptcha':
                $captcha_challenge_field = 'n3t_seznam_captcha_hash';
                $captcha_answer_field = 'n3t_seznam_captcha';
                break;
            default:
                // disable captcha as we could not find the right fields
                $formcaptcha = false;
        }

        foreach ($inputs as $input) 
        {

                if( $input['name'] == 'title' ) 
                {
                        $title                  = $input['value'];
                }
                if( $input['name'] == 'recipient' ) 
                {
                        $recipient      = base64_decode($input['value']);
                }

                if( $input['name'] == 'email' ) 
                {
                        $email          = $input['value'];
                }

                if( $input['name'] == 'name' ) 
                {
                        $name           = $input['value'];
                }

                if( $input['name'] == 'subject' ) 
                {
                        $subject            = $input['value'];
                }

                if( $input['name'] == 'message' ) 
                {
                        $message            = nl2br( $input['value'] );
                }

                if( $input['name'] == $captcha_challenge_field ) 
                {
                        $captcha_challenge  = $input['value'];
                }

                if( $input['name'] == $captcha_answer_field ) 
                {
                        $captcha_answer     = $input['value'];
                }

        }

        $valid_captcha = true;
        if($formcaptcha)
        {
                // get the plugin
                JPluginHelper::importPlugin('captcha');
                $dispatcher = JEventDispatcher::getInstance();

                $jinput->set($captcha_challenge_field,  $captcha_challenge);
                $jinput->set($captcha_answer_field,     $captcha_answer);

                $res = $dispatcher->trigger('onCheckAnswer', $captcha_answer);

                $index = 0;
                foreach (JPluginHelper::getPlugin('captcha') as $plugin)
                {
                        if (JFactory::getApplication()->get('captcha', '0') === $plugin->name)
                        {
                                $valid_captcha = $res[$index];
                                break;
                        }
                        $index++;
                }
                if(!$valid_captcha)
                {
                    $msg = '<span class="pb-text-danger">'. JText::_('COM_PAGEBUILDER_ADDON_AJAX_FORM_WRONG_CAPTCHA') .'</span>';                        
                }
        }

        if ($valid_captcha)
        {
                // We do not want to send the email as a fake user, it may cause spam problems
                $sender = array( 
                        $config->get( 'mailfrom' ),
                        $config->get( 'fromname' ) 
                );

                $subject = (($title)? '['.$title.'] ' : '') . $subject;
                $message .= JText::sprintf('COM_PAGEBUILDER_ADDON_AJAX_CONTACT_EMAIL_SIGNATURE', JUri::getInstance()->toString(),  JUri::getInstance());

                $mail->setSender($sender);
                $mail->addRecipient($recipient);
                $mail->setSubject($subject);
                $mail->AddReplyTo($email);
                $mail->isHTML(true);
                $mail->Encoding = 'base64'; 
                $mail->setBody($message);

                if ($mail->Send()) 
                {
                        $msg = '<span class="pb-text-success">'. JText::_('COM_PAGEBUILDER_ADDON_AJAX_CONTACT_SUCCESS') .'</span>';
                } else {
                        $msg = '<span class="pb-text-danger">'. JText::_('COM_PAGEBUILDER_ADDON_AJAX_CONTACT_FAILED') .'</span>';
                }
        }

//      There is a problem with pagebuilder cache and captchas
        $cache = JFactory::getCache('page');
        $cache->clean();

        return $msg;

}
© www.soinside.com 2019 - 2024. All rights reserved.