From 2c870b56f43a7b1a1d7df72beabc56af0e0b709d Mon Sep 17 00:00:00 2001
From: Michael RICOIS
+ +
- diff --git a/override/controllers/ContactController.php b/override/controllers/ContactController.php index b32b1391..f963ec77 100755 --- a/override/controllers/ContactController.php +++ b/override/controllers/ContactController.php @@ -56,10 +56,16 @@ class ContactController extends ContactControllerCore { $fileAttachment['mime'] = $_FILES['fileUpload']['type']; } + $mathCaptcha = new MathCaptcha\MathCaptcha(); + $captcha_ans = Tools::getValue('cans'); $message = Tools::htmlentitiesUTF8(Tools::getValue('message')); + if (Tools::getValue('email2') != '') { $this->errors[] = Tools::displayError('Invalid'); } + elseif ($mathCaptcha->check($captcha_ans) !== true) { + $this->errors[] = Tools::displayError('Invalid'); + } elseif (preg_match("/\p{Han}+/u", $message)) { $this->errors[] = Tools::displayError('Invalid message'); } diff --git a/themes/site/contact-form.tpl b/themes/site/contact-form.tpl index 0f1db096..004fe844 100755 --- a/themes/site/contact-form.tpl +++ b/themes/site/contact-form.tpl @@ -117,6 +117,9 @@ +
+ +
diff --git a/themes/site_mobile/contact-form.tpl b/themes/site_mobile/contact-form.tpl index 39ba5d1b..a13f91af 100755 --- a/themes/site_mobile/contact-form.tpl +++ b/themes/site_mobile/contact-form.tpl @@ -119,6 +119,9 @@ +
+ +
diff --git a/tools/math-captcha/README.md b/tools/math-captcha/README.md new file mode 100644 index 00000000..09592bbd --- /dev/null +++ b/tools/math-captcha/README.md @@ -0,0 +1,41 @@ +## Description: +This is a PHP class for generating images with simple mathematical questions (Math CAPTCHAs) to protect the forms of your website from spambots. + +## How to Use: + +To generate a captcha you simply: + +```PHP +session_start(); + +$mathCaptcha = new MathCaptcha\MathCaptcha(); + +$mathCaptcha->generate(); +$mathCaptcha->output(); +``` + +The `MathCaptcha` class makes use of session variables so you have to call the `session_start()` function before instantiating a `MathCaptcha` object. + +You can optionally supply an identifier for the captcha, to the constructor of the `MathCaptcha` class, if you want to use multiple captchas in your website. + +To verify the user's answer you simply: + +```PHP +session_start(); + +$mathCaptcha = new MathCaptcha\MathCaptcha(); + +if ( $mathCaptcha->check($captcha_answer) === true ) { + // Correct answer +} +else { + // Incorrect answer +} +``` + +If you use more than one captchas in your website you need also to supply the identifier of the captcha, to the constructor of the `MathCaptcha` class. + +Check out the `test_form.php` and `math_captcha.php` files for a working example. + +## Requirements: +PHP 5, GD 2.0.1 or later (2.0.28 or later is recommended) diff --git a/tools/math-captcha/autoloadPrestashop.php b/tools/math-captcha/autoloadPrestashop.php new file mode 100644 index 00000000..19266710 --- /dev/null +++ b/tools/math-captcha/autoloadPrestashop.php @@ -0,0 +1,27 @@ +=5.0.0", + "ext-gd": "*" + }, + "autoload": { + "psr-0": { + "MathCaptcha": "src/" + } + } +} diff --git a/tools/math-captcha/math_captcha.php b/tools/math-captcha/math_captcha.php new file mode 100644 index 00000000..8ac41cd8 --- /dev/null +++ b/tools/math-captcha/math_captcha.php @@ -0,0 +1,15 @@ +generate(); + $mathCaptcha->output(); +} +catch ( MathCaptcha\MathCaptchaException $e ) { + // Here you normally log the error, and you can output an error image + // to notify the user that something went wrong, if you want. +} \ No newline at end of file diff --git a/tools/math-captcha/src/MathCaptcha/MathCaptcha.php b/tools/math-captcha/src/MathCaptcha/MathCaptcha.php new file mode 100644 index 00000000..5c26e102 --- /dev/null +++ b/tools/math-captcha/src/MathCaptcha/MathCaptcha.php @@ -0,0 +1,79 @@ +captchaID = 'math_captcha_' . $captchaID; + + // Set the captcha result from last generated captcha and unset it from the session + if ( isset($_SESSION[$this->captchaID]) ) { + $this->answer = $_SESSION[$this->captchaID]; + unset($_SESSION[$this->captchaID]); + } + + } + + public function generate () + { + $this->addNum1 = rand(0, 10) * rand(1, 3); + $this->addNum2 = rand(0, 10) * rand(1, 3); + + // Set the captcha result for current captcha and set it to the session for later check + $_SESSION[$this->captchaID] = $this->answer = $this->addNum1 + $this->addNum2; + + // Create a canvas + if ( ($this->captchaImg = @imagecreatetruecolor(99, 19)) === false ) { + throw new MathCaptchaException('Creation of true color image failed'); + } + + // Allocate black and white colors + $color_black = imagecolorallocate($this->captchaImg, 0, 0, 0); + $color_white = imagecolorallocate($this->captchaImg, 255, 255, 255); + + // Make the background of the image white + imagefilledrectangle($this->captchaImg, 0, 0, 99, 19, $color_white); + + // Draw the math question on the image using black color + imagestring($this->captchaImg, 10, 2, 2, $this->addNum1 . ' + ' . $this->addNum2 . ' = ', $color_black); + + } + + public function output () + { + if ( $this->captchaImg === null ) { + throw new MathCaptchaException('Captcha image has not been generated'); + } + + header('Content-Disposition: Attachment;filename=captcha.png'); + header('Content-Type: image/png'); + + imagepng($this->captchaImg); + imagedestroy($this->captchaImg); + } + + public function check ( $answer ) + { + // Check if math captcha has been generated + if ( $this->answer === null ) { + return false; + } + + // Validate captcha + if ( $this->answer === (int) trim($answer) ) { + return true; + } + else { + return false; + } + + } + +} \ No newline at end of file diff --git a/tools/math-captcha/src/MathCaptcha/MathCaptchaException.php b/tools/math-captcha/src/MathCaptcha/MathCaptchaException.php new file mode 100644 index 00000000..0656a15a --- /dev/null +++ b/tools/math-captcha/src/MathCaptcha/MathCaptchaException.php @@ -0,0 +1,4 @@ +Please fill the answer to the math question'; + + } + else { + + $mathCaptcha = new MathCaptcha\MathCaptcha(); + + // Validate the answer + if ( $mathCaptcha->check($_POST['captcha_ans']) === true ) { + + // In a real application here you can register/login the user, insert a comment in the database etc + $msg = 'SUCCESS'; + + } + else { + + $msg = 'You didn\'t answered the question correctly'; + + } + + } + +} +?> + + + + +
Answer to this simple math question:
+ + + \ No newline at end of file