xref: /plugin/captcha/wav.php (revision 77e00bf9777c569c0942655691c32ae8fd03dbb5)
142a27035SAndreas Gohr<?php
242a27035SAndreas Gohr/**
342a27035SAndreas Gohr * CAPTCHA antispam plugin - sound generator
442a27035SAndreas Gohr *
542a27035SAndreas Gohr * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
642a27035SAndreas Gohr * @author     Andreas Gohr <gohr@cosmocode.de>
742a27035SAndreas Gohr */
842a27035SAndreas Gohr
942a27035SAndreas Gohrif(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../../').'/');
1042a27035SAndreas Gohrdefine('NOSESSION',true);
1142a27035SAndreas Gohrdefine('DOKU_DISABLE_GZIP_OUTPUT', 1);
1242a27035SAndreas Gohrrequire_once(DOKU_INC.'inc/init.php');
1342a27035SAndreas Gohrrequire_once(DOKU_INC.'inc/auth.php');
1442a27035SAndreas Gohr
1542a27035SAndreas Gohr$ID = $_REQUEST['id'];
16*77e00bf9SAndreas Gohr$plugin = plugin_load('helper','captcha');
1742a27035SAndreas Gohr$rand = PMA_blowfish_decrypt($_REQUEST['secret'],auth_cookiesalt());
1842a27035SAndreas Gohr$code = strtolower($plugin->_generateCAPTCHA($plugin->_fixedIdent(),$rand));
1942a27035SAndreas Gohr
2042a27035SAndreas Gohr// prepare an array of wavfiles
2142a27035SAndreas Gohr$lc = dirname(__FILE__).'/lang/'.$conf['lang'].'/audio/';
2242a27035SAndreas Gohr$en = dirname(__FILE__).'/lang/en/audio/';
2342a27035SAndreas Gohr$wavs = array();
2442a27035SAndreas Gohrfor($i=0;$i<5;$i++){
2542a27035SAndreas Gohr    $file = $lc.$code{$i}.'.wav';
2642a27035SAndreas Gohr    if(!@file_exists($file)) $file = $en.$code{$i}.'.wav';
2742a27035SAndreas Gohr    $wavs[] = $file;
2842a27035SAndreas Gohr}
2942a27035SAndreas Gohr
3042a27035SAndreas Gohrheader('Content-type: audio/x-wav');
3142a27035SAndreas Gohrheader('Content-Disposition: attachment;filename=captcha.wav');
3242a27035SAndreas Gohr
3342a27035SAndreas Gohrecho joinwavs($wavs);
3442a27035SAndreas Gohr
3542a27035SAndreas Gohr
3642a27035SAndreas Gohr/**
3742a27035SAndreas Gohr * Join multiple wav files
3842a27035SAndreas Gohr *
3942a27035SAndreas Gohr * All wave files need to have the same format and need to be uncompressed.
4042a27035SAndreas Gohr * The headers of the last file will be used (with recalculated datasize
4142a27035SAndreas Gohr * of course)
4242a27035SAndreas Gohr *
4342a27035SAndreas Gohr * @link http://ccrma.stanford.edu/CCRMA/Courses/422/projects/WaveFormat/
4442a27035SAndreas Gohr * @link http://www.thescripts.com/forum/thread3770.html
4542a27035SAndreas Gohr */
4642a27035SAndreas Gohrfunction joinwavs($wavs){
4742a27035SAndreas Gohr    $fields = join('/',array( 'H8ChunkID', 'VChunkSize', 'H8Format',
4842a27035SAndreas Gohr                              'H8Subchunk1ID', 'VSubchunk1Size',
4942a27035SAndreas Gohr                              'vAudioFormat', 'vNumChannels', 'VSampleRate',
5042a27035SAndreas Gohr                              'VByteRate', 'vBlockAlign', 'vBitsPerSample' ));
5142a27035SAndreas Gohr
5242a27035SAndreas Gohr    $data = '';
5342a27035SAndreas Gohr    foreach($wavs as $wav){
5442a27035SAndreas Gohr        $fp     = fopen($wav,'rb');
5542a27035SAndreas Gohr        $header = fread($fp,36);
5642a27035SAndreas Gohr        $info   = unpack($fields,$header);
5742a27035SAndreas Gohr
5842a27035SAndreas Gohr        // read optional extra stuff
5942a27035SAndreas Gohr        if($info['Subchunk1Size'] > 16){
6042a27035SAndreas Gohr            $header .= fread($fp,($info['Subchunk1Size']-16));
6142a27035SAndreas Gohr        }
6242a27035SAndreas Gohr
6342a27035SAndreas Gohr        // read SubChunk2ID
6442a27035SAndreas Gohr        $header .= fread($fp,4);
6542a27035SAndreas Gohr
6642a27035SAndreas Gohr        // read Subchunk2Size
6742a27035SAndreas Gohr        $size  = unpack('vsize',fread($fp, 4));
6842a27035SAndreas Gohr        $size  = $size['size'];
6942a27035SAndreas Gohr
7042a27035SAndreas Gohr        // read data
7142a27035SAndreas Gohr        $data .= fread($fp,$size);
7242a27035SAndreas Gohr    }
7342a27035SAndreas Gohr
7442a27035SAndreas Gohr    return $header.pack('V',strlen($data)).$data;
7542a27035SAndreas Gohr}
7642a27035SAndreas Gohr
77