diff --git a/mathcaptcha.php b/mathcaptcha.php new file mode 100644 index 0000000..fd00523 --- /dev/null +++ b/mathcaptcha.php @@ -0,0 +1,107 @@ + + * License: MIT style + **/ + +use Friendica\App; +use Friendica\Core\Hook; +use Friendica\Core\L10n; +use Friendica\Core\Renderer; +use Friendica\Core\System; + +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 + */ +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'); + } +} + +/** + * Add the captcha to the register 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 + */ +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 = ['+','-','×']; + $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'] + ]); + } +}