Initial upload to TYPO3 Forge
[TYPO3CMS/Extensions/sr_freecap.git] / pi2 / class.tx_srfreecap_pi2.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2005-2008 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 require_once(PATH_tslib.'class.tslib_pibase.php');
58
59 class tx_srfreecap_pi2 extends tslib_pibase {
60 var $prefixId = 'tx_srfreecap_pi2';
61 var $scriptRelPath = 'pi2/class.tx_srfreecap_pi2.php'; // Path to this script relative to the extension dir.
62 var $extKey = 'sr_freecap'; // The extension key.
63 var $conf = array();
64
65 function makeCaptcha() {
66 global $TSFE;
67
68 $this->tslib_pibase();
69
70 //Make sure that labels in locallang.php may be overridden
71 $this->conf = $TSFE->tmpl->setup['plugin.'][$this->prefixId.'.'];
72 $this->pi_loadLL();
73 $this->pi_USER_INT_obj = 1; // Disable caching
74
75 $L = t3lib_div::_GP('L');
76 if (isset($L)) {
77 $L = htmlspecialchars($L);
78 }
79 $fakeId = t3lib_div::shortMD5(uniqid (rand()),5);
80 $TSFE->additionalHeaderData[$this->extKey] .= '<script type="text/javascript" src="'. t3lib_extMgm::siteRelPath($this->extKey) . 'pi2/freeCap.js"></script>';
81 $audioURL = t3lib_extMgm::siteRelPath($this->extKey).'pi3/audioCaptcha.php?id=' . $GLOBALS['TSFE']->id . (isset($L)?'&amp;L='.$L:'');
82
83 $markerArray = array();
84 $markerArray['###'. strtoupper($this->extKey) . '_IMAGE###'] = '<img ' . $this->pi_classParam('image') . ' id="tx_srfreecap_pi2_captcha_image_'.$fakeId.'" src="'.t3lib_extMgm::siteRelPath($this->extKey).'pi1/captcha.php?id=' . $TSFE->id . (isset($L)?'&amp;L='.$L:'') . '" alt="' . $this->pi_getLL('altText') . '" style="vertical-align: middle; "/>';
85 $markerArray['###'. strtoupper($this->extKey) . '_NOTICE###'] = $this->pi_getLL('notice') . ' ' . $this->pi_getLL('explain');
86 $markerArray['###'. strtoupper($this->extKey) . '_CANT_READ###'] = '<span ' . $this->pi_classParam('cant-read') . '>' . $this->pi_getLL('cant_read1');
87 $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>';
88 $markerArray['###'. strtoupper($this->extKey) . '_CANT_READ###'] .= $this->pi_getLL('cant_read2') . '</span>';
89 if ($this->conf['accessibleOutput'] && in_array('mcrypt', get_loaded_extensions())) {
90 $markerArray['###'. strtoupper($this->extKey) . '_ACCESSIBLE###'] .= '<img alt="' . $this->pi_getLL('click_here_accessible') . '" title="' . $this->pi_getLL('click_here_accessible') . '" src="'.t3lib_extMgm::siteRelPath($this->extKey).'res/images/audio.png" onClick="playCaptcha(\''.$fakeId.'\', \''.$audioURL.'\', \''.$this->pi_getLL('noPlayMessage').'\');" style="cursor: pointer;"'.$this->pi_classParam('image-accessible').' /><div '.$this->pi_classParam('accessible').' id="tx_srfreecap_pi2_captcha_playAudio_'.$fakeId.'"></div>';
91 } else {
92 $markerArray['###'. strtoupper($this->extKey) . '_ACCESSIBLE###'] .= '';
93 }
94 return $markerArray;
95 }
96
97 /**
98 * Check the word that was entered against the hashed value
99 *
100 * @param string $word: hte word that was entered
101 * @return boolean true, if the word entered matches the hashes value
102 */
103 function checkWord($word) {
104 // Load session data
105 $this->sessionData = $GLOBALS['TSFE']->fe_user->getKey('ses','tx_'.$this->extKey);
106 if (!empty($this->sessionData[$this->extKey . '_word_hash']) && !empty($word)) {
107 // all freeCap words are lowercase.
108 // font #4 looks uppercase, but trust me, it's not...
109 if ($this->sessionData[$this->extKey . '_hash_func'] == 'md5') {
110 if (md5(strtolower(utf8_decode($word))) == $this->sessionData[$this->extKey . '_word_hash']) {
111 // reset freeCap session vars
112 // cannot stress enough how important it is to do this
113 // defeats re-use of known image with spoofed session id
114 $this->sessionData[$this->extKey . '_attempts'] = 0;
115 $this->sessionData[$this->extKey . '_word_hash'] = false;
116 return true;
117 }
118 }
119 }
120 return false;
121 }
122
123 /**
124 * From the 'salutationswitcher' extension.
125 *
126 * @author Oliver Klee <typo-coding@oliverklee.de>
127 */
128 // list of allowed suffixes
129 var $allowedSuffixes = array('formal', 'informal');
130
131 /**
132 * Returns the localized label of the LOCAL_LANG key, $key
133 * In $this->conf['salutation'], a suffix to the key may be set (which may be either 'formal' or 'informal').
134 * If a corresponding key exists, the formal/informal localized string is used instead.
135 * If the key doesn't exist, we just use the normal string.
136 *
137 * Example: key = 'greeting', suffix = 'informal'. If the key 'greeting_informal' exists, that string is used.
138 * If it doesn't exist, we'll try to use the string with the key 'greeting'.
139 *
140 * Notice that for debugging purposes prefixes for the output values can be set with the internal vars ->LLtestPrefixAlt and ->LLtestPrefix
141 *
142 * @param string The key from the LOCAL_LANG array for which to return the value.
143 * @param string Alternative string to return IF no value is found set for the key, neither for the local language nor the default.
144 * @param boolean If true, the output label is passed through htmlspecialchars()
145 * @return string The value from LOCAL_LANG.
146 */
147 function pi_getLL($key, $alt = '', $hsc = FALSE) {
148 // If the suffix is allowed and we have a localized string for the desired salutation, we'll take that.
149 if (isset($this->conf['salutation']) && in_array($this->conf['salutation'], $this->allowedSuffixes, 1)) {
150 $expandedKey = $key.'_'.$this->conf['salutation'];
151 if (isset($this->LOCAL_LANG[$this->LLkey][$expandedKey])) {
152 $key = $expandedKey;
153 }
154 }
155 return parent::pi_getLL($key, $alt, $hsc);
156 }
157
158 }
159
160 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/sr_freecap/pi2/class.tx_srfreecap_pi2.php']) {
161 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/sr_freecap/pi2/class.tx_srfreecap_pi2.php']);
162 }
163
164 ?>