Typesetter/addons/--AntiSpamMath/AntiSpamMath.php
2021-10-24 15:22:01 +02:00

93 lines
2.2 KiB
PHP

<?php
defined('is_running') or die('Not an entry point...');
class AntiSpamMath{
var array $operators = array(1=>'plus',2=>'minus',3=>'divided by',4=>'times');
/**
* @throws Exception
*/
function Form($html){
$operator_key = array_rand($this->operators);
$operator = $this->operators[$operator_key];
$asm_1 = random_int(1,10);
$asm_3 = random_int(1,10);
if ($operator_key == 3) {
$asm_1 = $asm_1 * $asm_3;
}
$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);
if ( !ob_start('ob_gzhandler') ) {
ob_start();
}
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');
echo '<input type="text" name="asm_4" value="" size="4" maxlength="6" /> ';
echo '</span>';
$html .= ob_get_clean();
return $html;
}
function message_alert($msg) {
echo "<script type='text/javascript'>alert('$msg');</script>";
}
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']) ){
$this->message_alert($message . ' (1)');
return false;
}
$operator_key = $_POST['asm_2'];
if( !isset($this->operators[$operator_key]) ){
$this->message_alert($message . ' (2)');
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 ){
$this->message_alert($message . ' (3)');
return false;
}
//message('passed');
return $passed;
}
}