2013-01-11 105 views
0

我试图在K2前端子表单上实现reCaptcha。我已经下载了PHP库,安装了它并可以成功地以submition形式显示capcha图像,但最后一步让我感到困惑。我需要把这些代码形成行动php文件:在joomla中使用php插件安装reCaptcha K2

<?php 
    require_once(JPATH_SITE.'/libraries/recaptcha/recaptchalib.php'); 
    $privatekey = "MY PRIVATE KEY"; 
    $resp = recaptcha_check_answer ($privatekey, 
           $_SERVER["REMOTE_ADDR"], 
           $_POST["recaptcha_challenge_field"], 
           $_POST["recaptcha_response_field"]); 

    if (!$resp->is_valid) { 
    // What happens when the CAPTCHA was entered incorrectly 
    die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." . 
     "(reCAPTCHA said: " . $resp->error . ")"); 
    } else { 
    alert(0); 
    } 
    ?> 

的问题是,操作文件恰好是index.php出于某种原因。如果我只是把那段代码放在那里,主页停止加载警告,我错过了验证码键。我将在下面发布我的K2frontend php文件,希望你能帮助我,因为这对我来说是非常重要的功能。

<?php 
defined('_JEXEC') or die('Restricted access'); 

$document = & JFactory::getDocument(); 
$document->addScriptDeclaration(" 
    Joomla.submitbutton = function(pressbutton){ 
     if (pressbutton == 'cancel') { 
      submitform(pressbutton); 
      return; 
     } 
     if (\$K2.trim(\$K2('#title').val()) == '') { 
      alert('".JText::_('K2_ITEM_MUST_HAVE_A_TITLE', true)."'); 
     } 
     else if (\$K2.trim(\$K2('#catid').val()) == '0') { 
      alert('".JText::_('K2_PLEASE_SELECT_A_CATEGORY', true)."'); 
     } 
     else { 
      syncExtraFieldsEditor(); 
      \$K2('#selectedTags option').attr('selected', 'selected'); 
      submitform(pressbutton); 
     } 
    } 
"); 

?> 
<div id="overallholderarticle" style="overflow:hidden;"> 

<form action="index.php" enctype="multipart/form-data" method="post" name="adminForm" id="adminForm"> 
    <?php if($this->mainframe->isSite()): ?> 
    <div id="k2FrontendContainer"> 
     <div id="k2Frontend" style="background: url('<?php echo JURI::base(); ?>templates/<?php echo $template; ?>/images/bg_windows_bright_noise.png');"> 
      <div id="k2FrontendEditToolbar"> 
       <div class="titenewseditor"> 
        <?php echo (JRequest::getInt('cid')) ? JText::_('K2_EDIT_ITEM') : JText::_('Добавить новость'); ?> 
       </div> 
      </div> 
      <div class="clr"></div> 
      <?php if(!$this->permissions->get('publish')): ?> 
      <div id="k2FrontendPermissionsNotice"> 
       <p><?php echo JText::_('K2_FRONTEND_PERMISSIONS_NOTICE'); ?></p> 
      </div> 
      <?php endif; ?> 
      <?php endif; ?> 
      <div id="k2ToggleSidebarContainer"> <a href="#" id="k2ToggleSidebar"><?php echo JText::_('K2_TOGGLE_SIDEBAR'); ?></a> </div> 
      <table cellspacing="0" cellpadding="0" border="0" class="adminFormK2Container"> 
       <tbody> 
        <tr> 
         <td> 
          <table class="adminFormK2"> 
           <tr> 
            <td class="adminK2LeftCol"> 
             <label for="title"><?php echo JText::_('K2_TITLE'); ?></label> 
.......................... 
.........CODE HERE........ 
.......................... 

      </table> 
      <div class="clr"></div> 
      <?php if($this->mainframe->isSite()): ?> 
     </div> 
    </div> 
    <!--BOTTOM ACTION BUTTONS--> 
    <div id="k2Frontend"> 
     <!--reCAPCHA--> 
     <?php 
     require_once(JPATH_SITE.'/libraries/recaptcha/recaptchalib.php'); 
     $publickey = "MY PUBLIC KEY"; 
     echo recaptcha_get_html($publickey); 
     ?> 
      <table class="k2FrontendToolbar" cellpadding="2" cellspacing="4"> 
       <tr> 
        <td id="toolbar-save" class="button"> 
         <a class="toolbar" href="#" onclick="javascript: submitbutton('save'); return false;"> <span title="<?php echo JText::_('K2_SAVE'); ?>" class="icon-32-save"></span> <?php echo JText::_('K2_SAVE'); ?> </a> 
        </td> 
        <td id="toolbar-cancel" class="button"> 
<a class="toolbar" href="#" onclick="javascript:history.go(-1)"> <span title="<?php echo JText::_('K2_CANCEL'); ?>" class="icon-32-cancel"></span> <?php echo JText::_('K2_CLOSE'); ?> </a> 
</td> 
       </tr> 
      </table> 
    </div> 
    <?php endif; ?> 
</form> 
</div> 

回答

2

如果您在管理部分工作您可以在项目控制器的保存功能中验证验证码。 在此文件中administrator/components/com_k2/controllers/item.php检查以下功能并添加代码。

function save() { 
JRequest::checkToken() or jexit('Invalid Token'); 
//add library and captcha validation code here 
$model = & $this->getModel('item'); 
$model->save(); 
} 

最好检查或尝试插件。

希望这将有助于too-

1)How to use joomla recaptcha plugin to my custom Module

2)Using ReCaptcha with my custom form in Joomla

+0

THX u为答案,不幸的是它不工作,失败后atempts我删除保存功能和tryed保存没有它的文章,它成功保存文章,多数民众赞成在奇怪的... –

+0

对不起,我错过了部分“管理部分”...所以wheres是前端用户的保存功能? –

+0

这里的前端/components/com_k2/controllers/item.php – Irfan