set_temp_password.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. $courseId = isset($_GET['course_id']) ? intval($_GET['course_id']) : null;
  13. $sessionId = isset($_GET['session_id']) ? intval($_GET['session_id']) : null;
  14. $userId = api_get_user_id();
  15. /**
  16. * Security check.
  17. */
  18. if (empty($courseId)) {
  19. api_not_allowed();
  20. }
  21. $courseInfo = api_get_course_info_by_id($courseId);
  22. // Build the form
  23. $form = new FormValidator(
  24. 'set_temp_password',
  25. 'POST',
  26. api_get_self().'?course_id='.$courseId.'&session_id='.$sessionId
  27. );
  28. $form->addElement('header', get_lang('CourseRequiresPassword'));
  29. $form->addElement('hidden', 'course_id', $courseId);
  30. $form->addElement('hidden', 'session_id', $sessionId);
  31. $form->addElement('password', 'course_password', get_lang('Password'));
  32. $form->addButtonSave(get_lang('Accept'));
  33. if ($form->validate()) {
  34. $formValues = $form->exportValues();
  35. if (sha1($formValues['course_password']) === $courseInfo['registration_code']) {
  36. Session::write('course_password_'.$courseInfo['real_id'], true);
  37. header('Location: '.api_get_course_url($courseInfo['code'], $sessionId).
  38. '&action=subscribe&sec_token='.Security::get_existing_token());
  39. exit;
  40. } else {
  41. Display::addFlash(
  42. Display::return_message(get_lang('CourseRegistrationCodeIncorrect'), 'error')
  43. );
  44. }
  45. }
  46. $tpl = new Template(null);
  47. $tpl->assign('content', $form->toHtml());
  48. $tpl->display_one_col_template();