PageRenderTime 37ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/sources/subs/Sound.subs.php

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