Initial upload to TYPO3 Forge
[TYPO3CMS/Extensions/sr_freecap.git] / pi3 / class.tx_srfreecap_pi3.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2007-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 * Renders a wav audio version of the CAPTCHA
29 *
30 * @author Stanislas Rolland <typo3(arobas)sjbr.ca>
31 */
32 require_once(PATH_tslib.'class.tslib_pibase.php');
33
34 class tx_srfreecap_pi3 extends tslib_pibase {
35 public $cObj; // The backReference to the mother cObj object set at call time
36 public $prefixId = 'tx_srfreecap_pi3'; // Same as class name
37 public $scriptRelPath = 'pi3/class.tx_srfreecap_pi3.php'; // Path to this script relative to the extension dir.
38 public $extKey = 'sr_freecap'; // The extension key.
39 public $conf = array();
40 private $voicesDir;
41
42 function main($conf) {
43 $this->conf = $conf;
44 // Load session data
45 $this->sessionData = $GLOBALS['TSFE']->fe_user->getKey('ses','tx_'.$this->extKey);
46 $this->voicesDir = $this->getVoicesDir();
47 $this->output($this->getWord());
48 exit();
49 }
50
51 /**
52 * Gets the directory of wav files based on configured language
53 *
54 * @return void
55 */
56 function getVoicesDir() {
57 global $TSFE;
58 $voicesDir = t3lib_extMgm::extPath('sr_freecap') . '/res/voices/' . $TSFE->lang . '/';
59 if (!is_dir($voicesDir)) {
60 $voicesDir = t3lib_extMgm::extPath('sr_freecap') . '/res/voices/default/';
61 }
62 return $voicesDir;
63 }
64
65 /**
66 * Gets the word that was stored in session data
67 *
68 * @return string the retrieved and decoded word
69 */
70 function getWord() {
71 $code = 'accessiblemustbe007';
72 $dec_string = $this->easy_decrypt($this->sessionData[$this->extKey . '_word_accessible'], $code);
73 return implode("-", str_split($dec_string));
74 }
75
76 /**
77 * Decodes a string
78 *
79 * @param array $cyph_arr: an array as returned by easy_encrypt() (see class.tx_srfreecap_pi1.php)
80 *
81 * @return string the decoded string
82 */
83 function easy_decrypt($cyph_arr, $key){
84 return trim(mcrypt_decrypt(MCRYPT_BLOWFISH, $key, base64_decode($cyph_arr[0]), MCRYPT_MODE_CBC, base64_decode($cyph_arr[1])));
85 }
86
87 /**
88 * Outputs the wav file
89 *
90 * @param string $word: the word to be spelled and played
91 *
92 * @return void
93 */
94 function output($word) {
95 $output = $this->generate($word);
96 header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
97 header('Pragma: no-cache');
98 header('Cache-Control: no-cache, no-store, must-revalidate');
99 header('Content-Type: audio/x-wav');
100 header('Content-Length: ' . strlen($output));
101 echo $output;
102 }
103
104 /**
105 * Builds an array of letter rendering wav files
106 *
107 * @param string $word: the word to be spelled and played
108 *
109 * @return array array of wav file names
110 */
111 function generate($word) {
112 $letters = str_split($word);
113 $letterRenderingWaveFiles = array();
114
115 foreach ($letters as $letter){
116 $file = $this->voicesDir . $letter . '.wav';
117 if (is_file($file)) {
118 $letterRenderingWaveFiles[] = $file;
119 }
120 }
121 return $this->joinWaveFiles($letterRenderingWaveFiles);
122 }
123
124 /**
125 * Join multiple wav files
126 *
127 * All wave files need to have the same format and need to be uncompressed.
128 * The headers of the last file will be used (with recalculated datasize
129 * of course)
130 *
131 * @link http://ccrma.stanford.edu/CCRMA/Courses/422/projects/WaveFormat/
132 * @link http://www.thescripts.com/forum/thread3770.html
133 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
134 * @author Andreas Gohr <gohr@cosmocode.de>
135 *
136 * @param array $wavs: the array of wav files
137 *
138 * @return string the contents of joined wav file
139 */
140
141 function joinWaveFiles($wavs) {
142 $fields = join('/',array( 'H8ChunkID', 'VChunkSize', 'H8Format',
143 'H8Subchunk1ID', 'VSubchunk1Size',
144 'vAudioFormat', 'vNumChannels', 'VSampleRate',
145 'VByteRate', 'vBlockAlign', 'vBitsPerSample' ));
146 $data = '';
147 foreach ($wavs as $wav){
148 $fp = fopen($wav, 'rb');
149 $header = fread($fp, 36);
150 $info = unpack($fields, $header);
151 // read optional extra stuff
152 if($info['Subchunk1Size'] > 16){
153 $header .= fread($fp, ($info['Subchunk1Size']-16));
154 }
155 // read SubChunk2ID
156 $header .= fread($fp, 4);
157 // read Subchunk2Size
158 $size = unpack('vsize', fread($fp, 4));
159 $size = $size['size'];
160 // read data
161 $data .= fread($fp, $size);
162 }
163 return $header . pack('V', strlen($data)) . $data;
164 }
165 }
166
167 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/sr_freecap/pi3/class.tx_srfreecap_pi3.php']) {
168 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/sr_freecap/pi3/class.tx_srfreecap_pi3.php']);
169 }
170 ?>