xref: /plugin/captcha/wav.php (revision 2cab8f1f0c66a841546f1ba4e2c4e4f169c6e027)
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
9265a0b9eSAndreas Gohrif(!defined('DOKU_INC')) define('DOKU_INC',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*2cab8f1fSAndreas Gohr/** @var $plugin helper_plugin_captcha */
1777e00bf9SAndreas Gohr$plugin = plugin_load('helper','captcha');
18*2cab8f1fSAndreas Gohr
19*2cab8f1fSAndreas Gohrif($plugin->getConf('mode') != 'audio'){
20*2cab8f1fSAndreas Gohr    http_status(404);
21*2cab8f1fSAndreas Gohr    exit;
22*2cab8f1fSAndreas Gohr}
23*2cab8f1fSAndreas Gohr
2442a27035SAndreas Gohr$rand = PMA_blowfish_decrypt($_REQUEST['secret'],auth_cookiesalt());
2542a27035SAndreas Gohr$code = strtolower($plugin->_generateCAPTCHA($plugin->_fixedIdent(),$rand));
2642a27035SAndreas Gohr
2742a27035SAndreas Gohr// prepare an array of wavfiles
2842a27035SAndreas Gohr$lc = dirname(__FILE__).'/lang/'.$conf['lang'].'/audio/';
2942a27035SAndreas Gohr$en = dirname(__FILE__).'/lang/en/audio/';
3042a27035SAndreas Gohr$wavs = array();
3142a27035SAndreas Gohrfor($i=0;$i<5;$i++){
3242a27035SAndreas Gohr    $file = $lc.$code{$i}.'.wav';
3342a27035SAndreas Gohr    if(!@file_exists($file)) $file = $en.$code{$i}.'.wav';
3442a27035SAndreas Gohr    $wavs[] = $file;
3542a27035SAndreas Gohr}
3642a27035SAndreas Gohr
3742a27035SAndreas Gohrheader('Content-type: audio/x-wav');
3842a27035SAndreas Gohrheader('Content-Disposition: attachment;filename=captcha.wav');
3942a27035SAndreas Gohr
4042a27035SAndreas Gohrecho joinwavs($wavs);
4142a27035SAndreas Gohr
4242a27035SAndreas Gohr
4342a27035SAndreas Gohr/**
4442a27035SAndreas Gohr * Join multiple wav files
4542a27035SAndreas Gohr *
4642a27035SAndreas Gohr * All wave files need to have the same format and need to be uncompressed.
4742a27035SAndreas Gohr * The headers of the last file will be used (with recalculated datasize
4842a27035SAndreas Gohr * of course)
4942a27035SAndreas Gohr *
5042a27035SAndreas Gohr * @link http://ccrma.stanford.edu/CCRMA/Courses/422/projects/WaveFormat/
5142a27035SAndreas Gohr * @link http://www.thescripts.com/forum/thread3770.html
5242a27035SAndreas Gohr */
5342a27035SAndreas Gohrfunction joinwavs($wavs){
5442a27035SAndreas Gohr    $fields = join('/',array( 'H8ChunkID', 'VChunkSize', 'H8Format',
5542a27035SAndreas Gohr                              'H8Subchunk1ID', 'VSubchunk1Size',
5642a27035SAndreas Gohr                              'vAudioFormat', 'vNumChannels', 'VSampleRate',
5742a27035SAndreas Gohr                              'VByteRate', 'vBlockAlign', 'vBitsPerSample' ));
5842a27035SAndreas Gohr
5942a27035SAndreas Gohr    $data = '';
6042a27035SAndreas Gohr    foreach($wavs as $wav){
6142a27035SAndreas Gohr        $fp     = fopen($wav,'rb');
6242a27035SAndreas Gohr        $header = fread($fp,36);
6342a27035SAndreas Gohr        $info   = unpack($fields,$header);
6442a27035SAndreas Gohr
6542a27035SAndreas Gohr        // read optional extra stuff
6642a27035SAndreas Gohr        if($info['Subchunk1Size'] > 16){
6742a27035SAndreas Gohr            $header .= fread($fp,($info['Subchunk1Size']-16));
6842a27035SAndreas Gohr        }
6942a27035SAndreas Gohr
7042a27035SAndreas Gohr        // read SubChunk2ID
7142a27035SAndreas Gohr        $header .= fread($fp,4);
7242a27035SAndreas Gohr
7342a27035SAndreas Gohr        // read Subchunk2Size
7442a27035SAndreas Gohr        $size  = unpack('vsize',fread($fp, 4));
7542a27035SAndreas Gohr        $size  = $size['size'];
7642a27035SAndreas Gohr
7742a27035SAndreas Gohr        // read data
7842a27035SAndreas Gohr        $data .= fread($fp,$size);
7942a27035SAndreas Gohr    }
8042a27035SAndreas Gohr
8142a27035SAndreas Gohr    return $header.pack('V',strlen($data)).$data;
8242a27035SAndreas Gohr}
8342a27035SAndreas Gohr
84