Typesetter-Original-gtbu/addons/--AntiSpamMath/AntiSpamMath.php

94 lines
2.2 KiB
PHP
Raw Normal View History

2021-09-08 19:52:21 +02:00
<?php
defined('is_running') or die('Not an entry point...');
class AntiSpamMath{
2021-10-24 15:22:01 +02:00
var array $operators = array(1=>'plus',2=>'minus',3=>'divided by',4=>'times');
2021-09-08 19:52:21 +02:00
2021-10-24 15:22:01 +02:00
/**
* @throws Exception
*/
2021-09-08 19:52:21 +02:00
function Form($html){
$operator_key = array_rand($this->operators);
$operator = $this->operators[$operator_key];
2021-10-24 15:22:01 +02:00
$asm_1 = random_int(1,10);
$asm_3 = random_int(1,10);
if ($operator_key == 3) {
$asm_1 = $asm_1 * $asm_3;
}
2021-09-08 19:52:21 +02:00
$inputs = array();
$inputs[] = ' <input type="hidden" name="asm_1" value="'.$asm_1.'" /> ';
$inputs[] = ' <input type="hidden" name="asm_2" value="'.$operator_key.'" /> ';
$inputs[] = ' <input type="hidden" name="asm_3" value="'.$asm_3.'" /> ';
shuffle($inputs);
2021-10-24 15:22:01 +02:00
if ( !ob_start('ob_gzhandler') ) {
ob_start();
}
2021-09-08 19:52:21 +02:00
echo implode('',$inputs);
echo '<span class="anti_spam_math">';
echo $asm_1;
echo ' ';
echo gpOutput::GetAddonText($operator);
echo ' ';
echo $asm_3;
echo ' ';
echo gpOutput::GetAddonText('equals');
2021-10-24 15:22:01 +02:00
echo '<input type="text" name="asm_4" value="" size="4" maxlength="6" /> ';
2021-09-08 19:52:21 +02:00
echo '</span>';
$html .= ob_get_clean();
return $html;
}
2021-10-24 15:22:01 +02:00
function message_alert($msg) {
2021-10-24 16:15:52 +02:00
echo "<script>window.alert('$msg');</script>";
2021-10-24 15:22:01 +02:00
}
2021-09-08 19:52:21 +02:00
function Check($passed){
$message = gpOutput::SelectText('Sorry, your answer to the verification challenge was incorrect. Please try again.');
if( empty($_POST['asm_1']) || empty($_POST['asm_2']) || empty($_POST['asm_3']) ){
2021-10-24 15:22:01 +02:00
$this->message_alert($message . ' (1)');
2021-09-08 19:52:21 +02:00
return false;
}
$operator_key = $_POST['asm_2'];
if( !isset($this->operators[$operator_key]) ){
2021-10-24 15:22:01 +02:00
$this->message_alert($message . ' (2)');
2021-09-08 19:52:21 +02:00
return false;
}
switch($operator_key){
case 1:
$result = $_POST['asm_1'] + $_POST['asm_3'];
break;
case 2:
$result = $_POST['asm_1'] - $_POST['asm_3'];
break;
case 3:
$result = $_POST['asm_1'] / $_POST['asm_3'];
break;
case 4:
$result = $_POST['asm_1'] * $_POST['asm_3'];
break;
}
$compare = $_POST['asm_4'];
//message('result: '.$result.' vs submitted: '.$compare);
if( $compare != $result ){
2021-10-24 15:22:01 +02:00
$this->message_alert($message . ' (3)');
2021-09-08 19:52:21 +02:00
return false;
}
//message('passed');
return $passed;
}
}