• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Small Captcha Addon

Slind

Active member
#1
Hey,

I just tried to write my first xenforo addon but I´m stuck, I did everything I could derive. Because it is quite small you may have a look into it and see what I missed. I would appreciate your support, as I´m working on this for hours and don´t get whats wrong or what I missed.



library/MyM/ForumTag/ForumTag.php
PHP:
<?php

class MyM_ForumTag_ForumTag extends XenForo_Captcha_Abstract
{
    public function isValid(array $input)
    {
        if (!empty($input['captcha_question_answer'])) {
            $captcha = $input['captcha_question_answer'];
        } else {
            return false;
        }
        if (!empty($input['username'])) {
            $username = $input['username'];
        } else {
            return false;
        }

        try {
        $dbh = new PDO("mysql:host=localhost;dbname=*****", '*****', '*****');
        $dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
        } catch (PDOException $e) {
            echo $e->getMessage();
        }
        $sth = $dbh->prepare("SELECT username, captcha, timestamp
              FROM xf_captcha
              WHERE username = :username",
              array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
        $sth->execute(array(':username' => $username));
        $row = $sth->fetch();

        if($captcha == $row['captcha']){
            return true;
        }
        else
        {
            return false;
        }

        return false;
    }

    public function renderInternal(XenForo_View $view)
    {
        return $view->createTemplateObject('mym_forumtag', array());
    }
}

Template: mym_forumtag (addon is selected)
HTML:
<div id="Captcha" data-source="{xen:link misc/captcha}">
    <input type="text" name="captcha_question_answer" value="" placeholder="{xen:phrase insert_ingame_captcha_here}" class="textCtrl OptOut" />
    <p class="explain">{xen:phrase execute_forumtag_to_get_the_answer_for_the_captcha}</p>
</div>

Code Event Listener: