<?php
class ftxCaptcha {
	#private $secret = 'ChangeThis!'; #Note: Change this!
	private $secret = '--ChangeThis!'; #Note: Change this!
	private $Question = 'Question';
	private $HashedAnswer = 'HashedAnswer';
	private $lang = 'en';

	private $ops = [ '+', '-', '*' ];
   
	# Configure your languages:
   
	private $opsWords = [
		'en' => [ '+' => 'plus', '-' => 'minus'     , '*' => 'times'    ],
		'de' => [ '+' => 'plus', '-' => 'minus'     , '*' => 'mal'      ],
		'fr' => [ '+' => 'plus', '-' => 'temps'     , '*' => 'moins'    ],
		'gr' => [ '+' => 'συν' , '-' => 'μειωμένους', '*' => 'chrónous' ]
	];
   
	private $numberWords = [
		'en' => [ 'one' , 'two' , 'three', 'for'    , 'fife' , 'six'  , 'seven' , 'eight', 'nine' , 'ten'  ],
		'de' => [ 'eins', 'zwei', 'drei' , 'vier'   , 'fünf' , 'sechs', 'sieben', 'acht' , 'neun' , 'zehn' ],
		'fr' => [ 'un'  , 'deux', 'trois', 'quatre' , 'cinq' , 'six'  , 'sept'  , 'huit' , 'neuf' , 'dix'  ],
		'gr' => [ 'ένα' , 'δύο' , 'τρία' , 'τέσσερα', 'πέντε', 'έξι'  , 'επτά'  , 'οκτώ' , 'εννέα', 'δέκα' ]
	];

	public $buttonMessage = [
		'en' => 'solve',
		'de' => 'lösen',
		'fr' => 'desserrer',
		'gr' => 'λύσει'
	];

	function __construct( $lang = 'en' ) {

		if ( $this -> secret == 'ChangeThis!' ) {
			trigger_error( 'You have to change the secret in „' . __FILE__ . '“ line 3.', E_USER_ERROR );
		}

		$lang = trim( $lang );
		$this -> setLanguage( $lang );
	}

	function setLanguage( $lang ) {

		if (
			! isset( $this -> opsWords[ $lang ] )
			or ! isset( $this -> numberWords[ $lang ] )
			or ! isset( $this -> buttonMessage[ $lang ] )
		) {
			trigger_error( 'The language „' . $lang . '“ is not enabled. In this case i using „en“. Show the file „' . __FILE__ . '“ and edit lines 12:34', E_USER_NOTICE );
			$lang = 'en';
		}
		$this -> lang = $lang;
		$z1  = random_int( 0, count( $this->numberWords[ $lang ] ) - 1 );
		$z2  = random_int( 0, count( $this->numberWords[ $lang ] ) - 1 );
		$op  = random_int( 0, 2 );
		switch ( $this -> ops[ $op ] ) {
			 case '+':
				$erg = $z1 + $z2 + 2; break;
			 case '-':
				$erg = $z1 - $z2; break;
			 case '*':
				$erg = ( $z1 + 1 ) * ( $z2 + 1 ); break;
		}
		$this->HashedAnswer = $this -> mkHash( $erg );
		$this-> Question = $this -> numberWords[ $lang ] [ $z1 ]
						  . ' '
						  . $this -> opsWords[ $lang ][ $this -> ops[ $op ] ]
						  . ' '
						  . $this -> numberWords[ $lang ][ $z2 ];
	}
   
	function mkHash( $s ) {
		return md5( ( $this -> secret . trim( $s ) ) );
	}
   
	function getQuestion() {
		return $this -> Question;
	}
   
	function getHashedAnswer() {
		return $this -> HashedAnswer;
	}      
   
	function validateAnswer( $answer, $hash )  {
		$sHashed = $this -> mkHash( $answer );
		$hash    = trim ( $hash );
		return $sHashed ==  $hash;
	}
   
	function getForm( $method = false, $target = false ) {
		
		$r = '';

		if ( $target ) {
			$target = " target='{$target}'";
		}

		if ( $method and ( strtoupper( $method ) == 'POST' or strtoupper( $method ) == 'GET' ) ) {
			$r .= "<form class='ftxCaptcha' method='{$method}'{$target}>" . PHP_EOL;
		}

		$r .=  "<input type='hidden' name='ftxHash' value='" . $this->getHashedAnswer() . "'>";
		$r .=  "<label class='ftxCaptcha'>" . $this -> getQuestion() . " ist<input type='text' class='ftxCaptcha' required pattern='[0-9-]{1,4}' title='-999 … 9999' name='ftxAnswer' /></label>";

		if ( $method and ( strtoupper( $method ) == 'POST' or strtoupper( $method ) == 'GET' ) ) {
			$r .=  "<button class='ftxCaptcha'>" . $this -> buttonMessage[ $this -> lang ] . "</button>" . PHP_EOL;
			$r .=  "</form>" . PHP_EOL;
		}
		return $r;
	}
	
	function printForm( $method = false, $target = false ) {
		echo $this -> getForm( $method, $target );
	}
}
 
/*
** Usage and examples:**
*/
 
/*
	   
require_once 'ftxCaptcha.php';
 
$captcha = new ftxCaptcha();
$captcha -> getForm( 'POST' );
echo PHP_EOL;
 
$captcha = new ftxCaptcha( 'de' );
$captcha -> getForm(false);
echo PHP_EOL;
 
$captcha -> setLanguage ( 'fr' );
$captcha -> getForm(false);
echo PHP_EOL;
 
$captcha -> setLanguage ( 'en' );
$captcha -> getForm( false );
echo PHP_EOL;
#*/
 
/*
** Usage ofe the returns
*
* require_once 'ftxCaptcha.php';
* $captcha = new ftxCaptcha();
* if ( $captcha -> validateAnswer( $_POST['ftxAnswer'], $_POST['hash'] ) ) { ... }
*/
