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