Text Captcha Module for Userregistration


Last edited by BrianKoontz:
Replaces old-style internal links with new pipe-split links.
Fri, 20 May 2016 07:38 UTC [diff]


Based on comments the docs server and the URInviteCodeModule here is URTextCaptchaModule. If you use this module, users have to answer one question right before they can register.

Install


1. Save the following as libs/UR/URTextCaptchaModule/URTextCaptchaModule.php:

<?php
/**
 * Require the correct answer for a randomly choosen question for successful registration.
 *
 * Based on ideas from http://www.punbb.fr/forums/viewtopic.php?pid=18554#p18554
 *
 * This UR (user registration) verification module requires the
 * user to enter the correct answer to a question (password) for a successful
 * registration.
 *
 * To implement, set the following in wikka.config.php:
 *  'UR_validation_modules' => 'URTextCaptchaModule'
 *  'UR_captcha_questions' => array(
 *      'First question' => 'Answer',
 *      'Second question' => 'Answer',
 *      ),
 * (If combined with other modules, separate each module with a
 * comma.)
 *
 * @version 0.2
 * @see libs/userregistration.class.php
 * @see URAuth, URAuthTmpl
 *
 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License
 * @filesource
 *
 * @author {@link http://wikkawiki.org/BrianKoontz Brian Koontz}  (Invite Code module)
 * @author {@link http://wikkawiki.org/NilsLindenberg NilsLindenberg} (extended)
 *
 * @see libs/userregistration.class.php
 * @see URAuth, URAuthTmpl
 *
 * @todo    i18n
 * @todo    Add number to the md5 to get some more randomness?
 */


include_once('libs/userregistration.class.php');

class URTextCaptchaModule extends URAuthTmpl
{   
    /**
     * Array for questions (key) and answers (value) for the captcha.
     */

    var $captcha_questions;
   
    /**
     * Constructor. Load questions and answers from the config file.
     */

    function URTextCaptchaModule(&$wakka)
    {
        $this->URAuthTmpl($wakka)
        if(isset($wakka->config['UR_captcha_questions']))
        {
                $this->captcha_questions = $wakka->config['UR_captcha_questions'];
        }
    }
   
    /**
     * Display input form with question, answer and encoded question (hidden field).
     */

    function URAuthTmplDisplay()
    {
        if(!isset($this->captcha_questions) || !is_array($this->captcha_questions))
        {
            echo '<em class="error">Error in URTextCaptcha module</em>';
        }
        else {
            $questions=array_keys($this->captcha_questions);
            if (FALSE === $questions)
            {
                echo '<em class="error">Error in URTextCaptcha module</em>';
                return;
            }
            //choose random-question
            $nr = rand(0,count($this->captcha_questions)-1);
           
            echo '<label for="UR_captcha">'.$questions[$nr].'</label>'."\n";
            echo '<input type="text" name="UR_captcha" id="UR_captcha" size="10" />'."\n";
            echo '<input name="UR_cap_q" value="'.md5($questions[$nr]).'" type="hidden" />';
        }      
    }

    /**
     * Verify if the encoded question exists and if the answer is correct.
     */

    function URAuthTmplVerify()
    {
        if(!isset($this->captcha_questions))
        {
            return FALSE;
        }
        if(isset($_POST['UR_captcha']) && isset($_POST['UR_cap_q']))
        {
            foreach ($this->captcha_questions as $k => $v)
            {
                if (md5($k) == $_POST['UR_cap_q'])
                {
                    if ($v == $_POST['UR_captcha']) return TRUE;
                }
            }
        }
        return FALSE;
    }
}
?>


2. Activate it:

in wikka.config.php add:
'UR_validation_modules' => 'URTextCaptchaModule',


3a. Formulate some questions + answers and add them to wikka.config.php
'UR_captcha_questions' => array(
 'First question' => 'Answer',
 'Second question' => 'Answer',
 ),


3b. Of course it is possible to use it with only an regiter code, too:

'UR_captcha_questions' => array(
 'Please enter the registration code provided by your admin' => 'registercode',
 ),



CategoryURModules
CategoryUserContributions
There are no comments on this page.
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki