xref: /plugin/captcha/wav.php (revision 42a2703562299943bcc8eae0ecff13d55fd9061c)
1*42a27035SAndreas Gohr<?php
2*42a27035SAndreas Gohr/**
3*42a27035SAndreas Gohr * CAPTCHA antispam plugin - sound generator
4*42a27035SAndreas Gohr *
5*42a27035SAndreas Gohr * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6*42a27035SAndreas Gohr * @author     Andreas Gohr <gohr@cosmocode.de>
7*42a27035SAndreas Gohr */
8*42a27035SAndreas Gohr
9*42a27035SAndreas Gohrif(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../../').'/');
10*42a27035SAndreas Gohrdefine('NOSESSION',true);
11*42a27035SAndreas Gohrdefine('DOKU_DISABLE_GZIP_OUTPUT', 1);
12*42a27035SAndreas Gohrrequire_once(DOKU_INC.'inc/init.php');
13*42a27035SAndreas Gohrrequire_once(DOKU_INC.'inc/auth.php');
14*42a27035SAndreas Gohrrequire_once(dirname(__FILE__).'/action.php');
15*42a27035SAndreas Gohr
16*42a27035SAndreas Gohr$ID = $_REQUEST['id'];
17*42a27035SAndreas Gohr$plugin = new action_plugin_captcha();
18*42a27035SAndreas Gohr$rand = PMA_blowfish_decrypt($_REQUEST['secret'],auth_cookiesalt());
19*42a27035SAndreas Gohr$code = strtolower($plugin->_generateCAPTCHA($plugin->_fixedIdent(),$rand));
20*42a27035SAndreas Gohr
21*42a27035SAndreas Gohr// prepare an array of wavfiles
22*42a27035SAndreas Gohr$lc = dirname(__FILE__).'/lang/'.$conf['lang'].'/audio/';
23*42a27035SAndreas Gohr$en = dirname(__FILE__).'/lang/en/audio/';
24*42a27035SAndreas Gohr$wavs = array();
25*42a27035SAndreas Gohrfor($i=0;$i<5;$i++){
26*42a27035SAndreas Gohr    $file = $lc.$code{$i}.'.wav';
27*42a27035SAndreas Gohr    if(!@file_exists($file)) $file = $en.$code{$i}.'.wav';
28*42a27035SAndreas Gohr    $wavs[] = $file;
29*42a27035SAndreas Gohr}
30*42a27035SAndreas Gohr
31*42a27035SAndreas Gohrheader('Content-type: audio/x-wav');
32*42a27035SAndreas Gohrheader('Content-Disposition: attachment;filename=captcha.wav');
33*42a27035SAndreas Gohr
34*42a27035SAndreas Gohrecho joinwavs($wavs);
35*42a27035SAndreas Gohr
36*42a27035SAndreas Gohr
37*42a27035SAndreas Gohr/**
38*42a27035SAndreas Gohr * Join multiple wav files
39*42a27035SAndreas Gohr *
40*42a27035SAndreas Gohr * All wave files need to have the same format and need to be uncompressed.
41*42a27035SAndreas Gohr * The headers of the last file will be used (with recalculated datasize
42*42a27035SAndreas Gohr * of course)
43*42a27035SAndreas Gohr *
44*42a27035SAndreas Gohr * @link http://ccrma.stanford.edu/CCRMA/Courses/422/projects/WaveFormat/
45*42a27035SAndreas Gohr * @link http://www.thescripts.com/forum/thread3770.html
46*42a27035SAndreas Gohr */
47*42a27035SAndreas Gohrfunction joinwavs($wavs){
48*42a27035SAndreas Gohr    $fields = join('/',array( 'H8ChunkID', 'VChunkSize', 'H8Format',
49*42a27035SAndreas Gohr                              'H8Subchunk1ID', 'VSubchunk1Size',
50*42a27035SAndreas Gohr                              'vAudioFormat', 'vNumChannels', 'VSampleRate',
51*42a27035SAndreas Gohr                              'VByteRate', 'vBlockAlign', 'vBitsPerSample' ));
52*42a27035SAndreas Gohr
53*42a27035SAndreas Gohr    $data = '';
54*42a27035SAndreas Gohr    foreach($wavs as $wav){
55*42a27035SAndreas Gohr        $fp     = fopen($wav,'rb');
56*42a27035SAndreas Gohr        $header = fread($fp,36);
57*42a27035SAndreas Gohr        $info   = unpack($fields,$header);
58*42a27035SAndreas Gohr
59*42a27035SAndreas Gohr        // read optional extra stuff
60*42a27035SAndreas Gohr        if($info['Subchunk1Size'] > 16){
61*42a27035SAndreas Gohr            $header .= fread($fp,($info['Subchunk1Size']-16));
62*42a27035SAndreas Gohr        }
63*42a27035SAndreas Gohr
64*42a27035SAndreas Gohr        // read SubChunk2ID
65*42a27035SAndreas Gohr        $header .= fread($fp,4);
66*42a27035SAndreas Gohr
67*42a27035SAndreas Gohr        // read Subchunk2Size
68*42a27035SAndreas Gohr        $size  = unpack('vsize',fread($fp, 4));
69*42a27035SAndreas Gohr        $size  = $size['size'];
70*42a27035SAndreas Gohr
71*42a27035SAndreas Gohr        // read data
72*42a27035SAndreas Gohr        $data .= fread($fp,$size);
73*42a27035SAndreas Gohr    }
74*42a27035SAndreas Gohr
75*42a27035SAndreas Gohr    return $header.pack('V',strlen($data)).$data;
76*42a27035SAndreas Gohr}
77*42a27035SAndreas Gohr
78