set(
"module_maptcha",
array("maptcha_hashkey" => random(32))
);
}
static function __uninstall(): void {
Config::current()->remove("module_maptcha");
}
static function generateCaptcha(): string {
$hashkey = Config::current()->module_maptcha["maptcha_hashkey"];
$t = time();
$x = rand(1,9);
$y = rand(1,$x);
$z = rand(1,9);
$n = array($x, $y);
switch ($z) {
case 1:
$label = _f("How much is %d + %d ?", $n, "maptcha");
$value = sha1(strval($x + $y).$t.$hashkey);
break;
case 2:
$label = _f("How much is %d − %d ?", $n, "maptcha");
$value = sha1(strval($x - $y).$t.$hashkey);
break;
case 3:
$label = _f("How much is %d × %d ?", $n, "maptcha");
$value = sha1(strval($x * $y).$t.$hashkey);
break;
case 4:
$label = _f("How much is %d + %d ?", $n, "maptcha");
$value = sha1(strval($x + $y).$t.$hashkey);
break;
case 5:
$label = _f("How much is %d − %d ?", $n, "maptcha");
$value = sha1(strval($x - $y).$t.$hashkey);
break;
case 6:
$label = _f("How much is %d × %d ?", $n, "maptcha");
$value = sha1(strval($x * $y).$t.$hashkey);
break;
case 7:
$label = _f("How much is %d + %d ?", $n, "maptcha");
$value = sha1(strval($x + $y).$t.$hashkey);
break;
case 8:
$label = _f("How much is %d − %d ?", $n, "maptcha");
$value = sha1(strval($x - $y).$t.$hashkey);
break;
case 9:
$label = _f("How much is %d × %d ?", $n, "maptcha");
$value = sha1(strval($x * $y).$t.$hashkey);
break;
}
return ''."\n".
''."\n".
''."\n".
''."\n";
}
static function checkCaptcha(): bool {
$config = Config::current();
# Constant: MAPTCHA_MIN_ELAPSED
# The minimum elapsed timed in seconds
# allowed between challenge and response.
if (!defined('MAPTCHA_MIN_ELAPSED'))
define('MAPTCHA_MIN_ELAPSED', 10);
if (
!isset($_POST['maptcha_response']) or
!isset($_POST['maptcha_challenge'])
)
return false;
if (
empty($_POST['maptcha_requested']) or
!is_numeric($_POST['maptcha_requested'])
)
return false;
$hashkey = $config->module_maptcha["maptcha_hashkey"];
$requested = $_POST['maptcha_requested'];
$challenge = $_POST['maptcha_challenge'];
$response = preg_replace(
"/[^0-9]/", "", $_POST['maptcha_response']
);
$response = sha1($response.$requested.$hashkey);
if ((time() - (int) $requested) < MAPTCHA_MIN_ELAPSED)
return false;
return ($response == $challenge);
}
}