authentify.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. $cidReset = true;
  4. require_once __DIR__.'/../../main/inc/global.inc.php';
  5. $plugin = WhispeakAuthPlugin::create();
  6. $plugin->protectTool();
  7. $userId = ChamiloSession::read(WhispeakAuthPlugin::SESSION_2FA_USER, 0) ?: api_get_user_id();
  8. $showForm = !$userId;
  9. /** @var array $lpItemInfo */
  10. $lpItemInfo = ChamiloSession::read(WhispeakAuthPlugin::SESSION_LP_ITEM, []);
  11. /** @var learnpath $oLp */
  12. $oLp = ChamiloSession::read('oLP', null);
  13. /** @var array $lpQuestionInfo */
  14. $lpQuestionInfo = ChamiloSession::read(WhispeakAuthPlugin::SESSION_QUIZ_QUESTION, []);
  15. /** @var Exercise $objExercise */
  16. $objExercise = ChamiloSession::read('objExercise', null);
  17. $isAuthOnLp = !empty($lpItemInfo) && !empty($oLp);
  18. $isAuthOnQuiz = !empty($lpQuestionInfo) && !empty($objExercise);
  19. $showFullPage = !$isAuthOnLp && !$isAuthOnQuiz;
  20. if (ChamiloSession::read(WhispeakAuthPlugin::SESSION_AUTH_PASSWORD, false)) {
  21. ChamiloSession::erase(WhispeakAuthPlugin::SESSION_AUTH_PASSWORD);
  22. $message = Display::return_message(
  23. $plugin->get_lang('MaxAttemptsReached').'<br>'
  24. .'<strong>'.$plugin->get_lang('LoginWithUsernameAndPassword').'</strong>',
  25. 'warning'
  26. );
  27. if (empty($lpQuestionInfo)) {
  28. Display::addFlash($message);
  29. }
  30. header('Location: '.api_get_path(WEB_PLUGIN_PATH).'whispeakauth/authentify_password.php');
  31. exit;
  32. }
  33. if ($userId) {
  34. $wsid = WhispeakAuthPlugin::getAuthUidValue($userId);
  35. if (empty($wsid)) {
  36. $message = Display::return_message($plugin->get_lang('SpeechAuthNotEnrolled'), 'warning');
  37. if (!empty($lpQuestionInfo) && empty($lpItemInfo)) {
  38. echo $message;
  39. } else {
  40. Display::addFlash($message);
  41. }
  42. header('Location: '.api_get_path(WEB_PLUGIN_PATH).'whispeakauth/authentify_password.php');
  43. exit;
  44. }
  45. }
  46. if (!empty($lpQuestionInfo) && empty($lpItemInfo)) {
  47. echo api_get_js('rtc/RecordRTC.js');
  48. echo api_get_js_simple(api_get_path(WEB_PLUGIN_PATH).'whispeakauth/assets/js/RecordAudio.js');
  49. } else {
  50. $htmlHeadXtra[] = api_get_js('rtc/RecordRTC.js');
  51. $htmlHeadXtra[] = api_get_js_simple(api_get_path(WEB_PLUGIN_PATH).'whispeakauth/assets/js/RecordAudio.js');
  52. }
  53. $sampleText = '';
  54. try {
  55. $sampleText = WhispeakAuthRequest::authenticateSentence($plugin);
  56. } catch (Exception $exception) {
  57. if ($showFullPage) {
  58. api_not_allowed(
  59. true,
  60. Display::return_message($exception->getMessage(), 'error')
  61. );
  62. }
  63. if (!$showFullPage && $oLp) {
  64. api_not_allowed(
  65. false,
  66. Display::return_message($exception->getMessage(), 'error')
  67. );
  68. }
  69. WhispeakAuthPlugin::displayNotAllowedMessage($exception->getMessage());
  70. }
  71. ChamiloSession::write(WhispeakAuthPlugin::SESSION_SENTENCE_TEXT, $sampleText);
  72. $template = new Template(
  73. !$showFullPage ? '' : $plugin->get_title(),
  74. $showFullPage,
  75. $showFullPage,
  76. false,
  77. true,
  78. false
  79. );
  80. $template->assign('show_form', $showForm);
  81. $template->assign('sample_text', $sampleText);
  82. $content = $template->fetch('whispeakauth/view/authentify_recorder.html.twig');
  83. if (!empty($lpQuestionInfo) && empty($lpItemInfo)) {
  84. echo $content;
  85. exit;
  86. }
  87. $template->assign('header', $plugin->get_title());
  88. $template->assign('content', $content);
  89. $template->display_one_col_template();