Addon for Friendica that adds a small mathematical challenge to the registration form that needs to be solved before the registration can be performed.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

131 lines
4.8 KiB

<?php
/**
* Name: Math-Captcha
* Description: This addon adds a smal calculation, which needs to be solved correctly before a new registration can be done
* Version: 0.1
* Author: Tobias Diekershoff <tobias@social.diekershoff.de>
* License: MIT style
**/
use Friendica\App;
use Friendica\Core\Hook;
use Friendica\Core\L10n;
use Friendica\Core\Renderer;
use Friendica\Core\System;
/*
* We will provide alternate templates for the register.tpl template.
* This is done with the register_form hook. The user input has then
* be evaluated with the register_post hook. To get the content into
* the new fields of the registration.tpl we need to supply the variables
* with the template_vars hook.
*
* So 1st registration of these hooks during the installation of the
* addon and then unregistration of the hooks when the addon is
* uninstalled.
*/
function mathcaptcha_install()
{
Hook::register('register_form', 'addon/mathcaptcha/mathcaptcha.php', 'mathcaptcha_register_form');
Hook::register('register_post', 'addon/mathcaptcha/mathcaptcha.php', 'mathcaptcha_register_post');
Hook::register('template_vars', 'addon/mathcaptcha/mathcaptcha.php', 'mathcaptcha_template_vars');
}
function mathcaptcha_uninstall()
{
Hook::unregister('register_form', 'addon/mathcaptcha/mathcaptcha.php', 'mathcaptcha_register_form');
Hook::unregister('register_post', 'addon/mathcaptcha/mathcaptcha.php', 'mathcaptcha_register_post');
Hook::unregister('template_vars', 'addon/mathcaptcha/mathcaptcha.php', 'mathcaptcha_template_vars');
}
/**
* Check if the captcha was solved correctly when posting the registration form
*
* If it is, let the default registration process proceed. If not, redirect the
* user back to the registration form and show a notice why the process failed.
*
* @param b array holding the posted field from the registration form
*/
function mathcaptcha_register_post(App $a, &$b)
{
$solution = 0;
switch ($_SESSION['mathcaptcha_op']){
case 0:
$solution = $_SESSION['mathcaptcha_n1'] + $_SESSION['mathcaptcha_n2'];
break;
case 1:
$solution = $_SESSION['mathcaptcha_n1'] - $_SESSION['mathcaptcha_n2'];
break;
case 2:
$solution = $_SESSION['mathcaptcha_n1'] * $_SESSION['mathcaptcha_n2'];
break;
}
unset($_SESSION['mathcaptcha_n1']);
unset($_SESSION['mathcaptcha_n2']);
unset($_SESSION['mathcaptcha_op']);
$post = $b['post'];
if ($solution == $post['mathcaptcha']) {
return true;
} else {
notice(L10n::t('The solution for the captcha was incorrect'));
$a->internalRedirect('/register');
}
}
/**
* Load an alternative register.tpl file that contains additional fields
* for the captcha. Should there be a theme specific one, use that instead
* of the default remplate.
*
* @param b the SMARTY template object for the registration form
*/
function mathcaptcha_register_form(App $a, &$b)
{
$tpl = $b['template'];
$oldPath = $tpl->filename;
$newPath = 'addon/mathcaptcha/templates/register.tpl';
if (strpos($oldPath, 'theme') == true)
{
$theme = explode('/', $oldPath)[2];
$newThemePath = 'addon/mathcaptcha/templates/theme/'.$theme.'/register.tpl';
if (file_exists($newThemePath)) {
$newPath = $newThemePath;
}
}
if (file_exists($newPath)) {
$b['template'] = Renderer::getMarkupTemplate($newPath);
$b['template']->filename = $newPath;
}
}
/**
* Add template variables to the registration template. Additionally
* store the captcha information in the session variable of the Friendica
* PHP session.
*
* @param $arr Array holding the variables applied to the template
*/
function mathcaptcha_template_vars(App $a, &$arr)
{
// if the template currently processed is the register.tpl
// add some new variables to the list of template variables
if ($arr['template']=='register.tpl')
{
unset($_SESSION['mathcaptcha_n1']);
unset($_SESSION['mathcaptcha_n2']);
unset($_SESSION['mathcaptcha_op']);
$num1 = rand(1, 10);
$num2 = rand(1, 10);
$operator = rand(0, 2);
$_SESSION['mathcaptcha_n1'] = $num1;
$_SESSION['mathcaptcha_n2'] = $num2;
$_SESSION['mathcaptcha_op'] = $operator;
$ops = ['+','-','&times'];
$task = strval($num1)." ".$ops[$operator]." ".strval($num2);
$arr['vars'] = array_merge($arr['vars'], [
'$captchaTitle'=>L10n::t('Captcha'),
'$captchaDescription'=>L10n::t('In order to continue with the registration, please solve the following calculation.'),
'$mathcaptcha'=> ['mathcaptcha', $task, '', L10n::t('You have to type in the solution of the calculation'), 'required']
]);
}
}