xref: /plugin/captcha/wav.php (revision 08f248e40c0e155880defbf4b95d145cf9080d3c)
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    $fields = join(
56        '/', array(
57                  'H8ChunkID', 'VChunkSize', 'H8Format',
58                  'H8Subchunk1ID', 'VSubchunk1Size',
59                  'vAudioFormat', 'vNumChannels', 'VSampleRate',
60                  'VByteRate', 'vBlockAlign', 'vBitsPerSample'
61             )
62    );
63
64    $data = '';
65    foreach($wavs as $wav) {
66        $fp     = fopen($wav, 'rb');
67        $header = fread($fp, 36);
68        $info   = unpack($fields, $header);
69
70        // read optional extra stuff
71        if($info['Subchunk1Size'] > 16) {
72            $header .= fread($fp, ($info['Subchunk1Size'] - 16));
73        }
74
75        // read SubChunk2ID
76        $header .= fread($fp, 4);
77
78        // read Subchunk2Size
79        $size = unpack('vsize', fread($fp, 4));
80        $size = $size['size'];
81
82        // read data
83        $data .= fread($fp, $size);
84    }
85
86    return $header.pack('V', strlen($data)).$data;
87}
88
89