xref: /plugin/captcha/wav.php (revision 9a516eda7386a5ec91d990aba0faaa514e5fa62d)
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'];
162cab8f1fSAndreas Gohr/** @var $plugin helper_plugin_captcha */
1777e00bf9SAndreas Gohr$plugin = plugin_load('helper', 'captcha');
182cab8f1fSAndreas Gohr
192cab8f1fSAndreas Gohrif($plugin->getConf('mode') != 'audio') {
202cab8f1fSAndreas Gohr    http_status(404);
212cab8f1fSAndreas Gohr    exit;
222cab8f1fSAndreas Gohr}
232cab8f1fSAndreas Gohr
24f044313dSAndreas Gohr$rand = $plugin->decrypt($_REQUEST['secret']);
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();
31*9a516edaSPatrick Brown$lettercount = $plugin->getConf('lettercount');
32*9a516edaSPatrick Brownif($lettercount > strlen($code)) $lettercount = strlen($code);
33*9a516edaSPatrick Brownfor($i = 0; $i < $lettercount; $i++) {
3442a27035SAndreas Gohr    $file = $lc.$code{$i}.'.wav';
3542a27035SAndreas Gohr    if(!@file_exists($file)) $file = $en.$code{$i}.'.wav';
3642a27035SAndreas Gohr    $wavs[] = $file;
3742a27035SAndreas Gohr}
3842a27035SAndreas Gohr
3942a27035SAndreas Gohrheader('Content-type: audio/x-wav');
4042a27035SAndreas Gohrheader('Content-Disposition: attachment;filename=captcha.wav');
4142a27035SAndreas Gohr
4242a27035SAndreas Gohrecho joinwavs($wavs);
4342a27035SAndreas Gohr
4442a27035SAndreas Gohr/**
4542a27035SAndreas Gohr * Join multiple wav files
4642a27035SAndreas Gohr *
4742a27035SAndreas Gohr * All wave files need to have the same format and need to be uncompressed.
4842a27035SAndreas Gohr * The headers of the last file will be used (with recalculated datasize
4942a27035SAndreas Gohr * of course)
5042a27035SAndreas Gohr *
5142a27035SAndreas Gohr * @link http://ccrma.stanford.edu/CCRMA/Courses/422/projects/WaveFormat/
5242a27035SAndreas Gohr * @link http://www.thescripts.com/forum/thread3770.html
5342a27035SAndreas Gohr */
5442a27035SAndreas Gohrfunction joinwavs($wavs) {
55c2695b40SAndreas Gohr    $fields = join(
56c2695b40SAndreas Gohr        '/', array(
57c2695b40SAndreas Gohr                  'H8ChunkID', 'VChunkSize', 'H8Format',
5842a27035SAndreas Gohr                  'H8Subchunk1ID', 'VSubchunk1Size',
5942a27035SAndreas Gohr                  'vAudioFormat', 'vNumChannels', 'VSampleRate',
60c2695b40SAndreas Gohr                  'VByteRate', 'vBlockAlign', 'vBitsPerSample'
61c2695b40SAndreas Gohr             )
62c2695b40SAndreas Gohr    );
6342a27035SAndreas Gohr
6442a27035SAndreas Gohr    $data = '';
6542a27035SAndreas Gohr    foreach($wavs as $wav) {
6642a27035SAndreas Gohr        $fp     = fopen($wav, 'rb');
6742a27035SAndreas Gohr        $header = fread($fp, 36);
6842a27035SAndreas Gohr        $info   = unpack($fields, $header);
6942a27035SAndreas Gohr
7042a27035SAndreas Gohr        // read optional extra stuff
7142a27035SAndreas Gohr        if($info['Subchunk1Size'] > 16) {
7242a27035SAndreas Gohr            $header .= fread($fp, ($info['Subchunk1Size'] - 16));
7342a27035SAndreas Gohr        }
7442a27035SAndreas Gohr
7542a27035SAndreas Gohr        // read SubChunk2ID
7642a27035SAndreas Gohr        $header .= fread($fp, 4);
7742a27035SAndreas Gohr
7842a27035SAndreas Gohr        // read Subchunk2Size
7942a27035SAndreas Gohr        $size = unpack('vsize', fread($fp, 4));
8042a27035SAndreas Gohr        $size = $size['size'];
8142a27035SAndreas Gohr
8242a27035SAndreas Gohr        // read data
8342a27035SAndreas Gohr        $data .= fread($fp, $size);
8442a27035SAndreas Gohr    }
8542a27035SAndreas Gohr
8642a27035SAndreas Gohr    return $header.pack('V', strlen($data)).$data;
8742a27035SAndreas Gohr}
8842a27035SAndreas Gohr
89