PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/forum/Sources/Subs-Sound.php

https://github.com/leftnode/nooges.com
PHP | 125 lines | 62 code | 21 blank | 42 comment | 15 complexity | 4c64197355dd277715ebd99013f690ae MD5 | raw file
  1. <?php
  2. /**********************************************************************************
  3. * Subs-Sound.php *
  4. ***********************************************************************************
  5. * SMF: Simple Machines Forum *
  6. * Open-Source Project Inspired by Zef Hemel (zef@zefhemel.com) *
  7. * =============================================================================== *
  8. * Software Version: SMF 2.0 RC2 *
  9. * Software by: Simple Machines (http://www.simplemachines.org) *
  10. * Copyright 2006-2009 by: Simple Machines LLC (http://www.simplemachines.org) *
  11. * 2001-2006 by: Lewis Media (http://www.lewismedia.com) *
  12. * Support, News, Updates at: http://www.simplemachines.org *
  13. ***********************************************************************************
  14. * This program is free software; you may redistribute it and/or modify it under *
  15. * the terms of the provided license as published by Simple Machines LLC. *
  16. * *
  17. * This program is distributed in the hope that it is and will be useful, but *
  18. * WITHOUT ANY WARRANTIES; without even any implied warranty of MERCHANTABILITY *
  19. * or FITNESS FOR A PARTICULAR PURPOSE. *
  20. * *
  21. * See the "license.txt" file for details of the Simple Machines license. *
  22. * The latest version can always be found at http://www.simplemachines.org. *
  23. **********************************************************************************/
  24. if (!defined('SMF'))
  25. die('Hacking attempt...');
  26. /* This file handles sound processing. In order to make sure the visual
  27. verification is still accessible for all users, a sound clip is being addded
  28. that reads the letters that are being shown.
  29. void createWaveFile(string word)
  30. - creates a wave file that spells the letters of 'word'.
  31. - Tries the user's language first, and defaults to english.
  32. - Returns false on failure.
  33. - used by VerificationCode() (Register.php).
  34. */
  35. function createWaveFile($word)
  36. {
  37. global $settings, $user_info, $context;
  38. // Allow max 2 requests per 20 seconds.
  39. if (($ip = cache_get_data('wave_file/' . $user_info['ip'], 20)) > 2 || ($ip2 = cache_get_data('wave_file/' . $user_info['ip2'], 20)) > 2)
  40. die(header('HTTP/1.1 400 Bad Request'));
  41. cache_put_data('wave_file/' . $user_info['ip'], $ip ? $ip + 1 : 1, 20);
  42. cache_put_data('wave_file/' . $user_info['ip2'], $ip2 ? $ip2 + 1 : 1, 20);
  43. // Fixate randomization for this word.
  44. mt_srand(end(unpack('n', md5($word . session_id()))));
  45. // Try to see if there's a sound font in the user's language.
  46. if (file_exists($settings['default_theme_dir'] . '/fonts/sound/a.' . $user_info['language'] . '.wav'))
  47. $sound_language = $user_info['language'];
  48. // English should be there.
  49. elseif (file_exists($settings['default_theme_dir'] . '/fonts/sound/a.english.wav'))
  50. $sound_language = 'english';
  51. // Guess not...
  52. else
  53. return false;
  54. // File names are in lower case so lets make sure that we are only using a lower case string
  55. $word = strtolower($word);
  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. $sound_letter = substr($sound_letter, strpos($sound_letter, 'data') + 8);
  64. switch ($word{$i} === 's' ? 0 : mt_rand(0, 2))
  65. {
  66. case 0:
  67. for ($j = 0, $n = strlen($sound_letter); $j < $n; $j++)
  68. for ($k = 0, $m = round(mt_rand(15, 25) / 10); $k < $m; $k++)
  69. $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)));
  70. break;
  71. case 1:
  72. for ($j = 0, $n = strlen($sound_letter) - 1; $j < $n; $j += 2)
  73. $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} : '');
  74. $sound_word .= str_repeat($sound_letter{$n}, 2);
  75. break;
  76. case 2:
  77. $shift = 0;
  78. for ($j = 0, $n = strlen($sound_letter); $j < $n; $j++)
  79. {
  80. if (mt_rand(0, 10) === 0)
  81. $shift += mt_rand(-3, 3);
  82. for ($k = 0, $m = round(mt_rand(15, 25) / 10); $k < $m; $k++)
  83. $sound_word .= chr(min(max(ord($sound_letter{$j}) + $shift, 0x00), 0xFF));
  84. }
  85. break;
  86. }
  87. $sound_word .= str_repeat(chr(0x80), mt_rand(10000, 10500));
  88. }
  89. $data_size = strlen($sound_word);
  90. $file_size = $data_size + 0x24;
  91. $sample_rate = 16000;
  92. // Disable compression.
  93. ob_end_clean();
  94. header('Content-Encoding: none');
  95. // Output the wav.
  96. header('Content-type: audio/x-wav');
  97. header('Expires: ' . gmdate('D, d M Y H:i:s', time() + 525600 * 60) . ' GMT');
  98. header('Content-Length: ' . ($file_size + 0x08));
  99. 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;
  100. // Noting more to add.
  101. die();
  102. }
  103. ?>