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