xref: /plugin/captcha/wav.php (revision c2695b4064acf71a0be53fa2784565975ea97dd2)
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 * Join multiple wav files
44 *
45 * All wave files need to have the same format and need to be uncompressed.
46 * The headers of the last file will be used (with recalculated datasize
47 * of course)
48 *
49 * @link http://ccrma.stanford.edu/CCRMA/Courses/422/projects/WaveFormat/
50 * @link http://www.thescripts.com/forum/thread3770.html
51 */
52function joinwavs($wavs) {
53    $fields = join(
54        '/', array(
55                  'H8ChunkID', 'VChunkSize', 'H8Format',
56                  'H8Subchunk1ID', 'VSubchunk1Size',
57                  'vAudioFormat', 'vNumChannels', 'VSampleRate',
58                  'VByteRate', 'vBlockAlign', 'vBitsPerSample'
59             )
60    );
61
62    $data = '';
63    foreach($wavs as $wav) {
64        $fp     = fopen($wav, 'rb');
65        $header = fread($fp, 36);
66        $info   = unpack($fields, $header);
67
68        // read optional extra stuff
69        if($info['Subchunk1Size'] > 16) {
70            $header .= fread($fp, ($info['Subchunk1Size'] - 16));
71        }
72
73        // read SubChunk2ID
74        $header .= fread($fp, 4);
75
76        // read Subchunk2Size
77        $size = unpack('vsize', fread($fp, 4));
78        $size = $size['size'];
79
80        // read data
81        $data .= fread($fp, $size);
82    }
83
84    return $header.pack('V', strlen($data)).$data;
85}
86
87