set_temp_password.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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 '../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('set_temp_password', 'POST', api_get_self().'?course_id='.$course_id.'&session_id='.$session_id);
  25. $form->addElement('header', get_lang('CourseRequiresPassword'));
  26. $form->addElement('hidden', 'course_id', $course_id);
  27. $form->addElement('hidden', 'session_id', $session_id);
  28. $form->addElement('password', 'course_password', get_lang('Password'));
  29. $form->addButtonSave(get_lang('Accept'));
  30. if ($form->validate()) {
  31. $form_values = $form->exportValues();
  32. if (sha1($form_values['course_password']) === $course_info['registration_code']) {
  33. Session::write('course_password_'.$course_info['real_id'], true);
  34. header('Location: '.api_get_course_url($course_info['code'], $session_id));
  35. exit;
  36. } else {
  37. $tpl->assign('error_message', Display::display_error_message(get_lang('CourseRegistrationCodeIncorrect'), true, true));
  38. }
  39. }
  40. $tpl->assign('form', $form->toHtml());
  41. $content = $tpl->get_template('auth/set_temp_password.tpl');
  42. $tpl->assign('content', $tpl->fetch($content));
  43. $tpl->display_one_col_template();