download.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This file is responsible for passing requested documents to the browser.
  5. * Html files are parsed to fix a few problems with URLs,
  6. * but this code will hopefully be replaced soon by an Apache URL
  7. * rewrite mechanism.
  8. *
  9. * @package chamilo.calendar
  10. */
  11. session_cache_limiter('public');
  12. require_once __DIR__.'/../inc/global.inc.php';
  13. $this_section = SECTION_COURSES;
  14. // IMPORTANT to avoid caching of documents
  15. header('Expires: Wed, 01 Jan 1990 00:00:00 GMT');
  16. header('Cache-Control: public');
  17. header('Pragma: no-cache');
  18. $course_id = isset($_REQUEST['course_id']) ? (int) $_REQUEST['course_id'] : api_get_course_int_id();
  19. $user_id = api_get_user_id();
  20. $course_info = api_get_course_info_by_id($course_id);
  21. $doc_url = $_REQUEST['file'];
  22. if (empty($course_id) || empty($doc_url)) {
  23. api_not_allowed();
  24. }
  25. $session_id = api_get_session_id();
  26. $is_user_is_subscribed = CourseManager::is_user_subscribed_in_course(
  27. $user_id,
  28. $course_info['code'],
  29. true,
  30. $session_id
  31. );
  32. if (!api_is_allowed_to_edit() && !$is_user_is_subscribed) {
  33. api_not_allowed();
  34. }
  35. //change the '&' that got rewritten to '///' by mod_rewrite back to '&'
  36. $doc_url = str_replace('///', '&', $doc_url);
  37. //still a space present? it must be a '+' (that got replaced by mod_rewrite)
  38. $doc_url = str_replace(' ', '+', $doc_url);
  39. $doc_url = str_replace('/..', '', $doc_url); //echo $doc_url;
  40. $full_file_name = api_get_path(SYS_COURSE_PATH).$course_info['path'].'/upload/calendar/'.$doc_url;
  41. //if the rewrite rule asks for a directory, we redirect to the document explorer
  42. if (is_dir($full_file_name)) {
  43. while ($doc_url[$dul = strlen($doc_url) - 1] == '/') {
  44. $doc_url = substr($doc_url, 0, $dul);
  45. }
  46. // create the path
  47. $document_explorer = api_get_path(WEB_COURSE_PATH).$course_info['path']; // home course path
  48. // redirect
  49. header('Location: '.$document_explorer);
  50. exit;
  51. }
  52. $tbl_agenda_attachment = Database::get_course_table(TABLE_AGENDA_ATTACHMENT);
  53. // launch event
  54. Event::event_download($doc_url);
  55. $sql = 'SELECT filename FROM '.$tbl_agenda_attachment.'
  56. WHERE
  57. c_id = '.$course_id.' AND
  58. path LIKE BINARY "'.Database::escape_string($doc_url).'"';
  59. $result = Database::query($sql);
  60. if (Database::num_rows($result)) {
  61. $row = Database::fetch_array($result);
  62. $title = str_replace(' ', '_', $row['filename']);
  63. if (Security::check_abs_path(
  64. $full_file_name,
  65. api_get_path(SYS_COURSE_PATH).$course_info['path'].'/upload/calendar/'
  66. )) {
  67. $result = DocumentManager::file_send_for_download($full_file_name, true, $title);
  68. if ($result === false) {
  69. api_not_allowed(true);
  70. }
  71. }
  72. }
  73. api_not_allowed();