Add captcha validator
[TYPO3CMS/Extensions/sr_freecap.git] / Classes / Validation / Validator / CaptchaValidator.php
1 <?php
2 namespace SJBR\SrFreecap\Validation\Validator;
3 /***************************************************************
4 * Copyright notice
5 *
6 * (c) 2012 Stanislas Rolland <typo3@sjbr.ca>
7 * All rights reserved
8 *
9 * This class is a backport of the corresponding class of FLOW3.
10 * All credits go to the v5 team.
11 *
12 * This script is part of the TYPO3 project. The TYPO3 project is
13 * free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * The GNU General Public License can be found at
19 * http://www.gnu.org/copyleft/gpl.html.
20 *
21 * This script is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * This copyright notice MUST APPEAR in all copies of the script!
27 ***************************************************************/
28 /**
29 * Captcha validator
30 */
31 class CaptchaValidator extends \TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator {
32
33 /**
34 * @var string Name of the extension this controller belongs to
35 */
36 protected $extensionName = 'SrFreecap';
37
38 /**
39 * Check the word that was entered against the hashed value
40 * Returns TRUE, if the given property ($word) matches the session captcha value.
41 *
42 * @param string $word: the word that was entered and should be validated
43 * @return boolean TRUE, if the word entered matches the hash value, FALSE if an error occured
44 */
45 public function isValid ($word) {
46 $isValid = FALSE;
47 $this->errors = array();
48 // Get session data
49 $wordRepository = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('SJBR\\SrFreecap\\Domain\\Repository\\WordRepository');
50 $wordObject = $wordRepository->getWord();
51 $wordHash = $wordObject->getWordHash();
52 // Check the word hash against the stored hash value
53 if (!empty($wordHash) && !empty($word)) {
54 if ($wordObject->getHashFunction() == 'md5') {
55 // All freeCap words are lowercase.
56 // font #4 looks uppercase, but trust me, it's not...
57 if (md5(strtolower(utf8_decode($word))) == $wordHash) {
58 // Reset freeCap session vars
59 // Cannot stress enough how important it is to do this
60 // Defeats re-use of known image with spoofed session id
61 $wordRepository->cleanUpWord();
62 $isValid = TRUE;
63 }
64 }
65 }
66 if (!$isValid) {
67 $this->addError(\TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('wrongWordWasEntered', $this->extensionName), 970320111501);
68 }
69 return $isValid;
70 }
71 }
72 ?>