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