Add captcha validator
authorstan <stan@735d13b6-9817-0410-8766-e36946ffe9aa>
Sun, 16 Dec 2012 01:13:58 +0000 (01:13 +0000)
committerstan <stan@735d13b6-9817-0410-8766-e36946ffe9aa>
Sun, 16 Dec 2012 01:13:58 +0000 (01:13 +0000)
git-svn-id: https://svn.typo3.org/TYPO3v4/Extensions/sr_freecap/trunk@68898 735d13b6-9817-0410-8766-e36946ffe9aa

ChangeLog
Classes/Validation/Validator/CaptchaValidator.php [new file with mode: 0644]
Resources/Private/Language/fr.locallang.xlf
Resources/Private/Language/locallang.xlf
ext_autoload.php
pi2/class.tx_srfreecap_pi2.php

index 16a3c5c..93ea63f 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+2012-12-15  Stanislas Rolland  <typo3@sjbr.ca>
+
+       * Add captcha validator class
+
 2012-12-13  Stanislas Rolland  <typo3@sjbr.ca>
 
        * Migrate backend GD font maker to extbase
diff --git a/Classes/Validation/Validator/CaptchaValidator.php b/Classes/Validation/Validator/CaptchaValidator.php
new file mode 100644 (file)
index 0000000..dd4a20b
--- /dev/null
@@ -0,0 +1,72 @@
+<?php
+namespace SJBR\SrFreecap\Validation\Validator;
+/***************************************************************
+*  Copyright notice
+*
+*  (c) 2012 Stanislas Rolland <typo3@sjbr.ca>
+*  All rights reserved
+*
+*  This class is a backport of the corresponding class of FLOW3. 
+*  All credits go to the v5 team.
+*
+*  This script is part of the TYPO3 project. The TYPO3 project is
+*  free software; you can redistribute it and/or modify
+*  it under the terms of the GNU General Public License as published by
+*  the Free Software Foundation; either version 2 of the License, or
+*  (at your option) any later version.
+*
+*  The GNU General Public License can be found at
+*  http://www.gnu.org/copyleft/gpl.html.
+*
+*  This script is distributed in the hope that it will be useful,
+*  but WITHOUT ANY WARRANTY; without even the implied warranty of
+*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+*  GNU General Public License for more details.
+*
+*  This copyright notice MUST APPEAR in all copies of the script!
+***************************************************************/
+/**
+ * Captcha validator
+ */
+class CaptchaValidator extends \TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator {
+
+       /**
+        * @var string Name of the extension this controller belongs to
+        */
+       protected $extensionName = 'SrFreecap';
+
+       /**
+        * Check the word that was entered against the hashed value
+        * Returns TRUE, if the given property ($word) matches the session captcha value.
+        *
+        * @param string $word: the word that was entered and should be validated
+        * @return boolean TRUE, if the word entered matches the hash value, FALSE if an error occured
+        */
+       public function isValid ($word) {
+               $isValid = FALSE;
+               $this->errors = array();
+               // Get session data
+               $wordRepository = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('SJBR\\SrFreecap\\Domain\\Repository\\WordRepository');
+               $wordObject = $wordRepository->getWord();
+               $wordHash = $wordObject->getWordHash();
+               // Check the word hash against the stored hash value
+               if (!empty($wordHash) && !empty($word)) {
+                       if ($wordObject->getHashFunction() == 'md5') {
+                               // All freeCap words are lowercase.
+                               // font #4 looks uppercase, but trust me, it's not...
+                               if (md5(strtolower(utf8_decode($word))) == $wordHash) {
+                                       // Reset freeCap session vars
+                                       // Cannot stress enough how important it is to do this
+                                       // Defeats re-use of known image with spoofed session id
+                                       $wordRepository->cleanUpWord();
+                                       $isValid = TRUE;
+                               }
+                       }
+               }
+               if (!$isValid) {
+                       $this->addError(\TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('wrongWordWasEntered', $this->extensionName), 970320111501);
+               }
+               return $isValid;
+       }
+}
+?>
\ No newline at end of file
index 5040e3f..bad9a85 100644 (file)
                                <source>The specified file is not a True Type Font file.</source>
                                <target>Le fichier spécifié ne contient pas une police de caractères True Type Font.</target>
                        </trans-unit>
+                       <trans-unit id="wrongWordWasEntered" xml:space="preserve">
+                               <source>Please enter the word or number as it appears in the image. The entered value was incorrect.</source>
+                               <target>Veuillez entrer le mot ou le nombre tel qu'il apparaît dans l'image. La valeur entrée était incorrecte.</target>
+                       </trans-unit>
                </body>
        </file>
 </xliff>
\ No newline at end of file
index e56b92b..0d4424b 100644 (file)
                        <trans-unit id="9221561047" xml:space="preserve">
                                <source>The specified file is not a True Type Font file.</source>
                        </trans-unit>
+                       <trans-unit id="wrongWordWasEntered" xml:space="preserve">
+                               <source>Please enter the word or number as it appears in the image. The entered value was incorrect.</source>
+                       </trans-unit>
                </body>
        </file>
 </xliff>
\ No newline at end of file
index 94da29e..dfbd19e 100644 (file)
@@ -23,6 +23,7 @@ return array(
        'TYPO3\CMS\Extbase\MVC\View\ViewInterface' => \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath('extbase') . 'Classes/Mvc/View/ViewInterface.php',
        'SJBR\SrFreecap\View\AudioPlayer\PlayWav' => $extensionClassesPath . 'View/AudioPlayer/PlayWav.php',
        'SJBR\SrFreecap\View\ImageGenerator\ShowPng' => $extensionClassesPath . 'View/ImageGenerator/ShowPng.php',
+       'SJBR\SrFreecap\Validation\Validator\CaptchaValidator' => $extensionClassesPath . 'Validation/Validator/CaptchaValidator.php',
        'tx_srfreecap_pi2' => $extensionPath . 'pi2/class.tx_srfreecap_pi2.php',
 );
 unset($extensionPath);       
index 203f599..a71dac9 100644 (file)
@@ -137,24 +137,10 @@ class tx_srfreecap_pi2 extends tslib_pibase {
         * @return      boolean         true, if the word entered matches the hashes value
         */
        function checkWord ($word) {
-               // Get session data
-               $wordRepository = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('SJBR\\SrFreecap\\Domain\\Repository\\WordRepository');
-               $wordObject = $wordRepository->getWord();
-               $wordHash = $wordObject->getWordHash();
-               if (!empty($wordHash) && !empty($word)) {
-                       // All freeCap words are lowercase.
-                       // font #4 looks uppercase, but trust me, it's not...
-                       if ($wordObject->getHashFunction() == 'md5') {
-                               if (md5(strtolower(utf8_decode($word))) == $wordHash) {
-                                       // Reset freeCap session vars
-                                       // Cannot stress enough how important it is to do this
-                                       // Defeats re-use of known image with spoofed session id
-                                       $wordRepository->cleanUpWord();
-                                       return true;
-                               }
-                       }
-               }
-               return false;
+               // Get validator
+               $validator = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('SJBR\\SrFreecap\\Validation\\Validator\\CaptchaValidator');
+               // Check word
+               return $validator->isValid($word);
        }
 
        /**