set_temp_password.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. /* For license terms, see /license.txt */
  3. /**
  4. * This page aims at requesting a password from a user to access a course
  5. * protected by password. If the password matches the course password, we
  6. * store the fact that user can access it during its session
  7. */
  8. use ChamiloSession as Session;
  9. $cidReset = true;
  10. require_once '../inc/global.inc.php';
  11. $this_section = SECTION_COURSES;
  12. $course_id = isset($_GET['course_id']) ? intval($_GET['course_id']) : null;
  13. $session_id = isset($_GET['session_id']) ? intval($_GET['session_id']) : null;
  14. $user_id = api_get_user_id();
  15. /**
  16. * Security check
  17. */
  18. if (empty($course_id)) {
  19. api_not_allowed();
  20. }
  21. /**
  22. * Code
  23. */
  24. $course_info = api_get_course_info_by_id($course_id);
  25. $tpl = new Template(null);
  26. // Build the form
  27. $form = new FormValidator('set_temp_password', 'POST', api_get_self().'?course_id='.$course_id.'&session_id='.$session_id);
  28. $form->addElement('header', get_lang('CourseRequiresPassword'));
  29. $form->addElement('hidden', 'course_id', $course_id);
  30. $form->addElement('hidden', 'session_id', $session_id);
  31. $form->addElement('password', 'course_password', get_lang('Password'));
  32. $form->addButtonSave(get_lang('Accept'));
  33. if ($form->validate()) {
  34. $form_values = $form->exportValues();
  35. if (sha1($form_values['course_password']) === $course_info['registration_code']) {
  36. Session::write('course_password_'.$course_info['real_id'], true);
  37. header('Location: '.api_get_course_url($course_info['code'], $session_id));
  38. exit;
  39. } else {
  40. $tpl->assign('error_message', Display::display_error_message(get_lang('CourseRegistrationCodeIncorrect'), true, true));
  41. }
  42. }
  43. $tpl->assign('form', $form->toHtml());
  44. $content = $tpl->get_template('auth/set_temp_password.tpl');
  45. $tpl->assign('content', $tpl->fetch($content));
  46. $tpl->display_one_col_template();