record_audio.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\UserBundle\Entity\User;
  4. use FFMpeg\FFMpeg;
  5. use FFMpeg\Format\Audio\Wav;
  6. $cidReset = true;
  7. require_once __DIR__.'/../../../main/inc/global.inc.php';
  8. $action = isset($_POST['action']) ? $_POST['action'] : 'enrollment';
  9. $isEnrollment = 'enrollment' === $action;
  10. $isAuthentify = 'authentify' === $action;
  11. $isAllowed = true;
  12. if ($isEnrollment) {
  13. api_block_anonymous_users(false);
  14. $isAllowed = !empty($_FILES['audio']);
  15. } elseif ($isAuthentify) {
  16. $isAllowed = !empty($_POST['username']) && !empty($_FILES['audio']);
  17. }
  18. if (!$isAllowed) {
  19. echo Display::return_message(get_lang('NotAllowed'), 'error');
  20. exit;
  21. }
  22. $plugin = WhispeakAuthPlugin::create();
  23. $plugin->protectTool(false);
  24. if ($isAuthentify) {
  25. $em = Database::getManager();
  26. /** @var User|null $user */
  27. $user = $em->getRepository('ChamiloUserBundle:User')->findOneBy(['username' => $_POST['username']]);
  28. } else {
  29. /** @var User $user */
  30. $user = api_get_user_entity(api_get_user_id());
  31. }
  32. if (empty($user)) {
  33. echo Display::return_message(get_lang('NoUser'), 'error');
  34. exit;
  35. }
  36. $path = api_upload_file('whispeakauth', $_FILES['audio'], $user->getId());
  37. if (false === $path) {
  38. echo Display::return_message(get_lang('UploadError'), 'error');
  39. exit;
  40. }
  41. $newFullPath = $originFullPath = api_get_path(SYS_UPLOAD_PATH).'whispeakauth'.$path['path_to_save'];
  42. $fileType = mime_content_type($originFullPath);
  43. if ('wav' !== substr($fileType, -3)) {
  44. $directory = dirname($originFullPath);
  45. $newFullPath = $directory.'/audio.wav';
  46. try {
  47. $ffmpeg = FFMpeg::create();
  48. $audio = $ffmpeg->open($originFullPath);
  49. $audio->save(new Wav(), $newFullPath);
  50. } catch (Exception $exception) {
  51. echo Display::return_message($exception->getMessage(), 'error');
  52. exit;
  53. }
  54. }
  55. if ($isEnrollment) {
  56. $result = $plugin->requestEnrollment($user, $newFullPath);
  57. if (empty($result)) {
  58. echo Display::return_message($plugin->get_lang('EnrollmentFailed'));
  59. exit;
  60. }
  61. $reliability = (int) $result['reliability'];
  62. if ($reliability <= 0) {
  63. echo Display::return_message($plugin->get_lang('EnrollmentSignature0'), 'error');
  64. exit;
  65. }
  66. $plugin->saveEnrollment($user, $result['uid']);
  67. $message = '<strong>'.$plugin->get_lang('EnrollmentSuccess').'</strong>';
  68. $message .= PHP_EOL;
  69. $message .= $plugin->get_lang("EnrollmentSignature$reliability");
  70. echo Display::return_message($message, 'success', false);
  71. exit;
  72. }
  73. if ($isAuthentify) {
  74. $result = $plugin->requestAuthentify($user, $newFullPath);
  75. if (empty($result)) {
  76. echo Display::return_message($plugin->get_lang('AuthentifyFailed'), 'error');
  77. exit;
  78. }
  79. $success = (bool) $result['audio'][0]['result'];
  80. if (!$success) {
  81. echo Display::return_message($plugin->get_lang('TryAgain'), 'warning');
  82. exit;
  83. }
  84. $loggedUser = [
  85. 'user_id' => $user->getId(),
  86. 'status' => $user->getStatus(),
  87. 'uidReset' => true,
  88. ];
  89. ChamiloSession::write('_user', $loggedUser);
  90. Login::init_user($user->getId(), true);
  91. echo Display::return_message($plugin->get_lang('AuthentifySuccess'), 'success');
  92. echo '<script>window.location.href = "'.api_get_path(WEB_PATH).'";</script>';
  93. exit;
  94. }