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