set_temp_password.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. /* For license terms, see /license.txt */
  3. use ChamiloSession as Session;
  4. /**
  5. * This page aims at requesting a password from a user to access a course
  6. * protected by password. If the password matches the course password, we
  7. * store the fact that user can access it during its session
  8. */
  9. $cidReset = true;
  10. require_once __DIR__.'/../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. $course_info = api_get_course_info_by_id($course_id);
  22. $tpl = new Template(null);
  23. // Build the form
  24. $form = new FormValidator(
  25. 'set_temp_password',
  26. 'POST',
  27. api_get_self().'?course_id='.$course_id.'&session_id='.$session_id
  28. );
  29. $form->addElement('header', get_lang('CourseRequiresPassword'));
  30. $form->addElement('hidden', 'course_id', $course_id);
  31. $form->addElement('hidden', 'session_id', $session_id);
  32. $form->addElement('password', 'course_password', get_lang('Password'));
  33. $form->addButtonSave(get_lang('Accept'));
  34. if ($form->validate()) {
  35. $form_values = $form->exportValues();
  36. if (sha1($form_values['course_password']) === $course_info['registration_code']) {
  37. Session::write('course_password_'.$course_info['real_id'], true);
  38. header('Location: '.api_get_course_url($course_info['code'], $session_id));
  39. exit;
  40. } else {
  41. $tpl->assign('error_message', Display::return_message(get_lang('CourseRegistrationCodeIncorrect'), 'error', true));
  42. }
  43. }
  44. $tpl->assign('form', $form->toHtml());
  45. $content = $tpl->get_template('auth/set_temp_password.tpl');
  46. $tpl->assign('content', $tpl->fetch($content));
  47. $tpl->display_one_col_template();