Add captcha validator
[TYPO3CMS/Extensions/sr_freecap.git] / pi2 / class.tx_srfreecap_pi2.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2005-2012 Stanislas Rolland <typo3(arobas)sjbr.ca>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
18 *
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27 /**
28 * Integrates freeCap v1.4 into TYPO3 and checks the freeCap CAPTCHA word.
29 *
30 *
31 * @author Stanislas Rolland <typo3(arobas)sjbr.ca>
32 */
33 /************************************************************\
34 *
35 * freeCap v1.4 Copyright 2005 Howard Yeend
36 * www.puremango.co.uk
37 *
38 * This file is part of freeCap.
39 *
40 * freeCap is free software; you can redistribute it and/or modify
41 * it under the terms of the GNU General Public License as published by
42 * the Free Software Foundation; either version 2 of the License, or
43 * (at your option) any later version.
44 *
45 * freeCap is distributed in the hope that it will be useful,
46 * but WITHOUT ANY WARRANTY; without even the implied warranty of
47 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
48 * GNU General Public License for more details.
49 *
50 * You should have received a copy of the GNU General Public License
51 * along with freeCap; if not, write to the Free Software
52 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
53 *
54 *
55 \************************************************************/
56
57 class tx_srfreecap_pi2 extends tslib_pibase {
58 var $prefixId = 'tx_srfreecap_pi2';
59 var $scriptRelPath = 'pi2/class.tx_srfreecap_pi2.php'; // Path to this script relative to the extension dir.
60 var $extKey = 'sr_freecap'; // The extension key.
61 var $conf = array();
62
63 function makeCaptcha() {
64
65 parent::__construct();
66
67 //Make sure that labels in locallang.php may be overridden
68 $this->conf = $GLOBALS['TSFE']->tmpl->setup['plugin.'][$this->prefixId.'.'];
69 $this->pi_loadLL();
70 // Disable caching
71 $this->pi_USER_INT_obj = 1;
72 $siteURL = \TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('TYPO3_SITE_URL');
73
74 $fakeId = \TYPO3\CMS\Core\Utility\GeneralUtility::shortMD5(uniqid (rand()),5);
75 $GLOBALS['TSFE']->additionalHeaderData[$this->extKey] .= '<script type="text/javascript" src="'. \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::siteRelPath($this->extKey) . 'Resources/Public/JavaScript/freeCap.js"></script>';
76
77 $urlParams = array(
78 'eID' => 'sr_freecap_EidDispatcher',
79 'id' => $GLOBALS['TSFE']->id,
80 'extensionName' => 'SrFreecap',
81 'pluginName' => 'AudioPlayer',
82 'controllerName' => 'AudioPlayer',
83 'actionName' => 'play',
84 'formatName' => 'wav',
85 );
86 $L = \TYPO3\CMS\Core\Utility\GeneralUtility::_GP('L');
87 if (isset($L)) {
88 $urlParams['L'] = htmlspecialchars($L);
89 }
90 if ($GLOBALS['TSFE']->MP) {
91 $urlParams['MP'] = $GLOBALS['TSFE']->MP;
92 }
93 $audioURL = $siteURL . 'index.php?' . ltrim(\TYPO3\CMS\Core\Utility\GeneralUtility::implodeArrayForUrl('', $urlParams), '&');
94
95 $urlParams = array(
96 'eID' => 'sr_freecap_EidDispatcher',
97 'id' => $GLOBALS['TSFE']->id,
98 'extensionName' => 'SrFreecap',
99 'pluginName' => 'ImageGenerator',
100 'controllerName' => 'ImageGenerator',
101 'actionName' => 'show',
102 'formatName' => 'png',
103 );
104 if (isset($L)) {
105 $urlParams['L'] = htmlspecialchars($L);
106 }
107 if ($GLOBALS['TSFE']->MP) {
108 $urlParams['MP'] = $GLOBALS['TSFE']->MP;
109 }
110 $imgUrl = $siteURL . 'index.php?' . ltrim(\TYPO3\CMS\Core\Utility\GeneralUtility::implodeArrayForUrl('', $urlParams), '&');
111
112 $markerArray = array();
113 $markerArray['###'. strtoupper($this->extKey) . '_IMAGE###'] = '<img' . $this->pi_classParam('image') . ' id="tx_srfreecap_pi2_captcha_image_'.$fakeId.'" src="' . htmlspecialchars($imgUrl) . '" alt="' . $this->pi_getLL('altText') . ' "/>';
114 $markerArray['###'. strtoupper($this->extKey) . '_NOTICE###'] = $this->pi_getLL('notice') . ' ' . $this->pi_getLL('explain');
115 $markerArray['###'. strtoupper($this->extKey) . '_CANT_READ###'] = '<span' . $this->pi_classParam('cant-read') . '>' . $this->pi_getLL('cant_read1');
116 $markerArray['###'. strtoupper($this->extKey) . '_CANT_READ###'] .= ' <a href="#" onclick="this.blur();newFreeCap(\''.$fakeId.'\', \''.$this->pi_getLL('noImageMessage').'\');return false;">' . $this->pi_getLL('click_here') . '</a>';
117 $markerArray['###'. strtoupper($this->extKey) . '_CANT_READ###'] .= $this->pi_getLL('cant_read2') . '</span>';
118 if ($this->conf['accessibleOutput'] && in_array('mcrypt', get_loaded_extensions())) {
119 if ($this->conf['accessibleOutputImage']) {
120 $markerArray['###'. strtoupper($this->extKey) . '_ACCESSIBLE###'] .= '<input type="image" alt="' . $this->pi_getLL('click_here_accessible') . '" title="' . $this->pi_getLL('click_here_accessible') . '" src="' . $siteURL . str_replace(PATH_site, '', \TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName($this->conf['accessibleOutputImage'])) . '" onclick="playCaptcha(\''.$fakeId.'\', \''.$audioURL.'\', \''.$this->pi_getLL('noPlayMessage').'\');return false;" style="cursor: pointer;"' . $this->pi_classParam('image-accessible') . ' /><span'.$this->pi_classParam('accessible').' id="tx_srfreecap_pi2_captcha_playAudio_'.$fakeId.'"></span>';
121 } else {
122 $markerArray['###'. strtoupper($this->extKey) . '_ACCESSIBLE###'] .= '<span id="tx_srfreecap_pi2_captcha_playLink_'.$fakeId.'"' . $this->pi_classParam('accessible-link') . '>'.$this->pi_getLL('click_here_accessible_before_link');
123 $markerArray['###'. strtoupper($this->extKey) . '_ACCESSIBLE###'] .= '<a onClick="playCaptcha(\''.$fakeId.'\', \''.$audioURL.'\', \''.$this->pi_getLL('noPlayMessage').'\');" style="cursor: pointer;" title="' . $this->pi_getLL('click_here_accessible') . '">'.$this->pi_getLL('click_here_accessible_link').'</a>';
124 $markerArray['###'. strtoupper($this->extKey) . '_ACCESSIBLE###'] .= $this->pi_getLL('click_here_accessible_after_link').'</span>';
125 $markerArray['###'. strtoupper($this->extKey) . '_ACCESSIBLE###'] .= '<span ' .$this->pi_classParam('accessible').' id="tx_srfreecap_pi2_captcha_playAudio_'.$fakeId.'"></span>';
126 }
127 } else {
128 $markerArray['###'. strtoupper($this->extKey) . '_ACCESSIBLE###'] .= '';
129 }
130 return $markerArray;
131 }
132
133 /**
134 * Check the word that was entered against the hashed value
135 *
136 * @param string $word: hte word that was entered
137 * @return boolean true, if the word entered matches the hashes value
138 */
139 function checkWord ($word) {
140 // Get validator
141 $validator = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('SJBR\\SrFreecap\\Validation\\Validator\\CaptchaValidator');
142 // Check word
143 return $validator->isValid($word);
144 }
145
146 /**
147 * From the 'salutationswitcher' extension.
148 *
149 * @author Oliver Klee <typo-coding@oliverklee.de>
150 */
151 // List of allowed suffixes
152 var $allowedSuffixes = array('formal', 'informal');
153
154 /**
155 * Returns the localized label of the LOCAL_LANG key, $key
156 * In $this->conf['salutation'], a suffix to the key may be set (which may be either 'formal' or 'informal').
157 * If a corresponding key exists, the formal/informal localized string is used instead.
158 * If the key doesn't exist, we just use the normal string.
159 *
160 * Example: key = 'greeting', suffix = 'informal'. If the key 'greeting_informal' exists, that string is used.
161 * If it doesn't exist, we'll try to use the string with the key 'greeting'.
162 *
163 * Notice that for debugging purposes prefixes for the output values can be set with the internal vars ->LLtestPrefixAlt and ->LLtestPrefix
164 *
165 * @param string The key from the LOCAL_LANG array for which to return the value.
166 * @param string Alternative string to return IF no value is found set for the key, neither for the local language nor the default.
167 * @param boolean If true, the output label is passed through htmlspecialchars()
168 * @return string The value from LOCAL_LANG.
169 */
170 function pi_getLL($key, $alt = '', $hsc = FALSE) {
171 // If the suffix is allowed and we have a localized string for the desired salutation, we'll take that.
172 if (isset($this->conf['salutation']) && in_array($this->conf['salutation'], $this->allowedSuffixes, 1)) {
173 $expandedKey = $key.'_'.$this->conf['salutation'];
174 if (isset($this->LOCAL_LANG[$this->LLkey][$expandedKey])) {
175 $key = $expandedKey;
176 }
177 }
178 return parent::pi_getLL($key, $alt, $hsc);
179 }
180
181 }
182 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/sr_freecap/pi2/class.tx_srfreecap_pi2.php']) {
183 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/sr_freecap/pi2/class.tx_srfreecap_pi2.php']);
184 }
185 ?>