xref: /plugin/captcha/wav.php (revision 42a2703562299943bcc8eae0ecff13d55fd9061c)
1<?php
2/**
3 * CAPTCHA antispam plugin - sound generator
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Andreas Gohr <gohr@cosmocode.de>
7 */
8
9if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../../').'/');
10define('NOSESSION',true);
11define('DOKU_DISABLE_GZIP_OUTPUT', 1);
12require_once(DOKU_INC.'inc/init.php');
13require_once(DOKU_INC.'inc/auth.php');
14require_once(dirname(__FILE__).'/action.php');
15
16$ID = $_REQUEST['id'];
17$plugin = new action_plugin_captcha();
18$rand = PMA_blowfish_decrypt($_REQUEST['secret'],auth_cookiesalt());
19$code = strtolower($plugin->_generateCAPTCHA($plugin->_fixedIdent(),$rand));
20
21// prepare an array of wavfiles
22$lc = dirname(__FILE__).'/lang/'.$conf['lang'].'/audio/';
23$en = dirname(__FILE__).'/lang/en/audio/';
24$wavs = array();
25for($i=0;$i<5;$i++){
26    $file = $lc.$code{$i}.'.wav';
27    if(!@file_exists($file)) $file = $en.$code{$i}.'.wav';
28    $wavs[] = $file;
29}
30
31header('Content-type: audio/x-wav');
32header('Content-Disposition: attachment;filename=captcha.wav');
33
34echo joinwavs($wavs);
35
36
37/**
38 * Join multiple wav files
39 *
40 * All wave files need to have the same format and need to be uncompressed.
41 * The headers of the last file will be used (with recalculated datasize
42 * of course)
43 *
44 * @link http://ccrma.stanford.edu/CCRMA/Courses/422/projects/WaveFormat/
45 * @link http://www.thescripts.com/forum/thread3770.html
46 */
47function joinwavs($wavs){
48    $fields = join('/',array( 'H8ChunkID', 'VChunkSize', 'H8Format',
49                              'H8Subchunk1ID', 'VSubchunk1Size',
50                              'vAudioFormat', 'vNumChannels', 'VSampleRate',
51                              'VByteRate', 'vBlockAlign', 'vBitsPerSample' ));
52
53    $data = '';
54    foreach($wavs as $wav){
55        $fp     = fopen($wav,'rb');
56        $header = fread($fp,36);
57        $info   = unpack($fields,$header);
58
59        // read optional extra stuff
60        if($info['Subchunk1Size'] > 16){
61            $header .= fread($fp,($info['Subchunk1Size']-16));
62        }
63
64        // read SubChunk2ID
65        $header .= fread($fp,4);
66
67        // read Subchunk2Size
68        $size  = unpack('vsize',fread($fp, 4));
69        $size  = $size['size'];
70
71        // read data
72        $data .= fread($fp,$size);
73    }
74
75    return $header.pack('V',strlen($data)).$data;
76}
77
78