2017-04-06 273 views
0

這是我第一次使用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>添加到頭標記中。 有了這個ReCaptcha顯示正常。Joomla,Sp頁面構建器和ReCaptcha

我的問題是驗證。 我曾嘗試在site.php中添加一些驗證代碼,但我相信SP Page Builder使用JFactory :: getMailer();準備好電子郵件併發送,我對此一無所知。因此,我不知道我可以在哪裏添加我的ReCaptcha驗證代碼,並且因爲我在網上找到了幾個版本的代碼,所以我真的不知道要使用哪一個。 我一直在尋找一些解決辦法,以便如何做這個驗證...我嘗試了很多東西,但它仍然無法正常工作。

任何人都可以幫助我解決這個問題嗎?

非常感謝!

編輯

我想我的問題是不夠清楚:

我想補充的Recaptcha,這已經在我的網站工作的其他形式的罰款(所以它不是與Joomla問題的結構)。我想在網站的其他部分使用以下SP頁面生成器聯繫表單,而不是RSFormPro。驗證過程應圍繞這部分來完成,但我想添加谷歌驗證碼,我試過它的幾個版本,我發現在互聯網,並且它不工作:

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>'; 
    } 
} 

任何建議?

謝謝!

回答

0

我想你錯過了正確設置ReCaptcha憑據。默認的Joomla登記表上 Setup captcha

檢查驗證碼領域,如果它的存在,那麼你已經正確配置它 -

你可以在這裏檢查。對於sp頁面生成器,您必須與擴展開發人員覈對是否由Recaptcha字段支持。

+0

我這樣做。從我從SP頁面生成器中發現的,他們不支持ReCaptcha,所以他們不會幫助。我確實設法將ReCaptcha添加到他們的表單中,並且正確顯示。問題是無論我嘗試了什麼,無論ReCaptcha是否被選中,表單都會通過。我不知道如何添加驗證代碼......我發現SP頁面生成器使用JFactory :: getMailler()來準備和發送表單,但我對此一無所知。 – StS

+0

要在組件中添加自定義ReCaptcha,請選中此項 - http://stackoverflow.com/questions/12840015/how-to-use-joomla-recaptcha-plugin-to-my-custom-module/12860744#12860744 –

+0

我看過並嘗試,但正如我所說,我無法弄清楚把驗證碼放在哪裏。我相信他們使用Jmail(因爲:JFactory :: getMailer()),但我不知道在哪裏找到或添加驗證代碼。 – StS

0

我已成功將驗證碼添加到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

這有很大的幫助!我仍在努力適應谷歌的驗證碼,我會讓你知道它是否成功:) – StS

+0

嗯...我一定非常想念這個,因爲我真的不知道該怎麼做,而不是: '$ captchaset ='n3tseznamcaptcha'; if($ captchaset ==='n3tseznamcaptcha') { $ captcha_field_hash ='n3t_seznam_captcha_hash'; $ captcha_field_answer ='n3t_seznam_captcha'; }' 而不是: '$ jinput-> set('n3t_seznam_captcha_hash',$ captcha_hash); $ jinput-> set('n3t_seznam_captcha',$ captcha_answer); $ res = $ dispatcher-> trigger('onCheckAnswer',$ captcha_answer);' 爲了讓我的谷歌ReCaptcha工作:/ – StS

+0

這裏實現已安裝的驗證碼的問題是,ajax表單不填寫get參數,它會填充POST變量並返回AJAX響應,因此插件將無法工作,所以我們要做的就是從生成的captcha字段中獲取這些值並將其傳遞給jinput(JFactory :: getApplication-> input)當插件被觸發時,它可以檢索這些值並檢查它們是正確還是錯誤,但是每個captcha都有它自己的字段,所以我們需要找出我們正在使用哪一個讀取這些值,我將在我的回覆recaptcha插件工作 – spacebiker

0

所以在這裏你有完整的解決方案。請注意,我也更改了管理員一些內容。此版本將顯示並驗證在Joomla config中選擇的默認驗證碼,但它只適用於recaptcha和n3tsezam ..其他應手動添加。原因是這個插件沒有在請求中添加挑戰和響應字段,captcha插件使用它來進行驗證,因此我們需要檢索它並寫入我們的ajax函數(ajax_contact_get_ajax)中的jinput,以及使整個事情是,每個captcha插件使用不同的領域。無論如何..如果你需要任何其他captcha插件只是可以將其添加到交換機,你應該完成。

功能ajax_contact_addon($的ATT) {

 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; 

} 
+0

我試過你的代碼:/com_sppagebuilder/addons/ajax_contact/site.php,這是一個進步,因爲現在它阻止每一個聯繫消息而不是發送他們所有的XD我一定是做錯了什麼。 – StS