PageRenderTime 88ms CodeModel.GetById 73ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/php/Sources/Subs-Sound.php

https://github.com/dekoza/openshift-smf-2.0.7
PHP | 114 lines | 62 code | 22 blank | 30 comment | 15 complexity | cae55051270b9181c08ab4cfc73163cf MD5 | raw file
  1<?php
  2
  3/**
  4 * Simple Machines Forum (SMF)
  5 *
  6 * @package SMF
  7 * @author Simple Machines http://www.simplemachines.org
  8 * @copyright 2011 Simple Machines
  9 * @license http://www.simplemachines.org/about/smf/license.php BSD
 10 *
 11 * @version 2.0
 12 */
 13
 14if (!defined('SMF'))
 15	die('Hacking attempt...');
 16
 17/*	This file handles sound processing. In order to make sure the visual
 18	verification is still accessible for all users, a sound clip is being addded
 19	that reads the letters that are being shown.
 20
 21	void createWaveFile(string word)
 22		- creates a wave file that spells the letters of 'word'.
 23		- Tries the user's language first, and defaults to english.
 24		- Returns false on failure.
 25		- used by VerificationCode() (Register.php).
 26*/
 27
 28function createWaveFile($word)
 29{
 30	global $settings, $user_info, $context;
 31
 32	// Allow max 2 requests per 20 seconds.
 33	if (($ip = cache_get_data('wave_file/' . $user_info['ip'], 20)) > 2 || ($ip2 = cache_get_data('wave_file/' . $user_info['ip2'], 20)) > 2)
 34		die(header('HTTP/1.1 400 Bad Request'));
 35	cache_put_data('wave_file/' . $user_info['ip'], $ip ? $ip + 1 : 1, 20);
 36	cache_put_data('wave_file/' . $user_info['ip2'], $ip2 ? $ip2 + 1 : 1, 20);
 37
 38	// Fixate randomization for this word.
 39	mt_srand(end(unpack('n', md5($word . session_id()))));
 40
 41	// Try to see if there's a sound font in the user's language.
 42	if (file_exists($settings['default_theme_dir'] . '/fonts/sound/a.' . $user_info['language'] . '.wav'))
 43		$sound_language = $user_info['language'];
 44
 45	// English should be there.
 46	elseif (file_exists($settings['default_theme_dir'] . '/fonts/sound/a.english.wav'))
 47		$sound_language = 'english';
 48
 49	// Guess not...
 50	else
 51		return false;
 52
 53	// File names are in lower case so lets make sure that we are only using a lower case string
 54	$word = strtolower($word);
 55
 56	// Loop through all letters of the word $word.
 57	$sound_word = '';
 58	for ($i = 0; $i < strlen($word); $i++)
 59	{
 60		$sound_letter = implode('', file($settings['default_theme_dir'] . '/fonts/sound/' . $word{$i} . '.' . $sound_language . '.wav'));
 61		if (strpos($sound_letter, 'data') === false)
 62			return false;
 63
 64		$sound_letter = substr($sound_letter, strpos($sound_letter, 'data') + 8);
 65		switch ($word{$i} === 's' ? 0 : mt_rand(0, 2))
 66		{
 67			case 0:
 68				for ($j = 0, $n = strlen($sound_letter); $j < $n; $j++)
 69					for ($k = 0, $m = round(mt_rand(15, 25) / 10); $k < $m; $k++)
 70						$sound_word .= $word{$i} === 's' ? $sound_letter{$j} : chr(mt_rand(max(ord($sound_letter{$j}) - 1, 0x00), min(ord($sound_letter{$j}) + 1, 0xFF)));
 71			break;
 72
 73			case 1:
 74				for ($j = 0, $n = strlen($sound_letter) - 1; $j < $n; $j += 2)
 75					$sound_word .= (mt_rand(0, 3) == 0 ? '' : $sound_letter{$j}) . (mt_rand(0, 3) === 0 ? $sound_letter{$j + 1} : $sound_letter{$j}) . (mt_rand(0, 3) === 0 ? $sound_letter{$j} : $sound_letter{$j + 1}) . $sound_letter{$j + 1} . (mt_rand(0, 3) == 0 ? $sound_letter{$j + 1} : '');
 76				$sound_word .= str_repeat($sound_letter{$n}, 2);
 77			break;
 78
 79			case 2:
 80				$shift = 0;
 81				for ($j = 0, $n = strlen($sound_letter); $j < $n; $j++)
 82				{
 83					if (mt_rand(0, 10) === 0)
 84						$shift += mt_rand(-3, 3);
 85					for ($k = 0, $m = round(mt_rand(15, 25) / 10); $k < $m; $k++)
 86						$sound_word .= chr(min(max(ord($sound_letter{$j}) + $shift, 0x00), 0xFF));
 87				}
 88			break;
 89
 90		}
 91
 92		$sound_word .= str_repeat(chr(0x80), mt_rand(10000, 10500));
 93	}
 94
 95	$data_size = strlen($sound_word);
 96	$file_size = $data_size + 0x24;
 97	$sample_rate = 16000;
 98
 99	// Disable compression.
100	ob_end_clean();
101	header('Content-Encoding: none');
102
103	// Output the wav.
104	header('Content-type: audio/x-wav');
105	header('Expires: ' . gmdate('D, d M Y H:i:s', time() + 525600 * 60) . ' GMT');
106	header('Content-Length: ' . ($file_size + 0x08));
107
108	echo pack('nnVnnnnnnnnVVnnnnV', 0x5249, 0x4646, $file_size, 0x5741, 0x5645, 0x666D, 0x7420, 0x1000, 0x0000, 0x0100, 0x0100, $sample_rate, $sample_rate, 0x0100, 0x0800, 0x6461, 0x7461, $data_size), $sound_word;
109
110	// Noting more to add.
111	die();
112}
113
114?>