xref: /plugin/captcha/wav.php (revision db7d7dbedbc9e69fcfb04ae7bc5b02f0f95bfcdd)
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
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();
31*db7d7dbeSAndreas Gohrfor($i = 0; $i < $plugin->getConf('lettercount'); $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 * Join multiple wav files
4442a27035SAndreas Gohr *
4542a27035SAndreas Gohr * All wave files need to have the same format and need to be uncompressed.
4642a27035SAndreas Gohr * The headers of the last file will be used (with recalculated datasize
4742a27035SAndreas Gohr * of course)
4842a27035SAndreas Gohr *
4942a27035SAndreas Gohr * @link http://ccrma.stanford.edu/CCRMA/Courses/422/projects/WaveFormat/
5042a27035SAndreas Gohr * @link http://www.thescripts.com/forum/thread3770.html
5142a27035SAndreas Gohr */
5242a27035SAndreas Gohrfunction joinwavs($wavs) {
53c2695b40SAndreas Gohr    $fields = join(
54c2695b40SAndreas Gohr        '/', array(
55c2695b40SAndreas Gohr                  'H8ChunkID', 'VChunkSize', 'H8Format',
5642a27035SAndreas Gohr                  'H8Subchunk1ID', 'VSubchunk1Size',
5742a27035SAndreas Gohr                  'vAudioFormat', 'vNumChannels', 'VSampleRate',
58c2695b40SAndreas Gohr                  'VByteRate', 'vBlockAlign', 'vBitsPerSample'
59c2695b40SAndreas Gohr             )
60c2695b40SAndreas Gohr    );
6142a27035SAndreas Gohr
6242a27035SAndreas Gohr    $data = '';
6342a27035SAndreas Gohr    foreach($wavs as $wav) {
6442a27035SAndreas Gohr        $fp     = fopen($wav, 'rb');
6542a27035SAndreas Gohr        $header = fread($fp, 36);
6642a27035SAndreas Gohr        $info   = unpack($fields, $header);
6742a27035SAndreas Gohr
6842a27035SAndreas Gohr        // read optional extra stuff
6942a27035SAndreas Gohr        if($info['Subchunk1Size'] > 16) {
7042a27035SAndreas Gohr            $header .= fread($fp, ($info['Subchunk1Size'] - 16));
7142a27035SAndreas Gohr        }
7242a27035SAndreas Gohr
7342a27035SAndreas Gohr        // read SubChunk2ID
7442a27035SAndreas Gohr        $header .= fread($fp, 4);
7542a27035SAndreas Gohr
7642a27035SAndreas Gohr        // read Subchunk2Size
7742a27035SAndreas Gohr        $size = unpack('vsize', fread($fp, 4));
7842a27035SAndreas Gohr        $size = $size['size'];
7942a27035SAndreas Gohr
8042a27035SAndreas Gohr        // read data
8142a27035SAndreas Gohr        $data .= fread($fp, $size);
8242a27035SAndreas Gohr    }
8342a27035SAndreas Gohr
8442a27035SAndreas Gohr    return $header.pack('V', strlen($data)).$data;
8542a27035SAndreas Gohr}
8642a27035SAndreas Gohr
87