download.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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.document
  10. */
  11. session_cache_limiter('public');
  12. require_once __DIR__.'/../inc/global.inc.php';
  13. $this_section = SECTION_COURSES;
  14. require_once 'forumconfig.inc.php';
  15. // IMPORTANT to avoid caching of documents
  16. header('Expires: Wed, 01 Jan 1990 00:00:00 GMT');
  17. header('Cache-Control: public');
  18. header('Pragma: no-cache');
  19. //protection
  20. api_protect_course_script(true);
  21. $doc_url = $_GET['file'];
  22. //change the '&' that got rewritten to '///' by mod_rewrite back to '&'
  23. $doc_url = str_replace('///', '&', $doc_url);
  24. //still a space present? it must be a '+' (that got replaced by mod_rewrite)
  25. $doc_url = str_replace(' ', '+', $doc_url);
  26. $doc_url = str_replace('/..', '', $doc_url); //echo $doc_url;
  27. if (!isset($_course)) {
  28. api_not_allowed(true);
  29. }
  30. $full_file_name = api_get_path(SYS_COURSE_PATH).api_get_course_path().'/upload/forum/'.$doc_url;
  31. //if the rewrite rule asks for a directory, we redirect to the document explorer
  32. if (is_dir($full_file_name)) {
  33. //remove last slash if present
  34. //mod_rewrite can change /some/path/ to /some/path// in some cases, so clean them all off (René)
  35. while ($doc_url{$dul = strlen($doc_url) - 1} == '/') {
  36. $doc_url = substr($doc_url, 0, $dul);
  37. }
  38. //create the path
  39. $document_explorer = api_get_path(WEB_COURSE_PATH).api_get_course_path();
  40. //redirect
  41. header('Location: '.$document_explorer);
  42. exit;
  43. }
  44. $tbl_forum_attachment = Database::get_course_table(TABLE_FORUM_ATTACHMENT);
  45. $tbl_forum_post = Database::get_course_table(TABLE_FORUM_POST);
  46. $course_id = api_get_course_int_id();
  47. $courseInfo = api_get_course_info_by_id($course_id);
  48. // launch event
  49. Event::event_download($doc_url);
  50. $sql = 'SELECT thread_id, forum_id,filename
  51. FROM '.$tbl_forum_post.' f
  52. INNER JOIN '.$tbl_forum_attachment.' a
  53. ON a.post_id=f.post_id
  54. WHERE
  55. f.c_id = '.$course_id.' AND
  56. a.c_id = '.$course_id.' AND
  57. path LIKE BINARY "'.$doc_url.'"';
  58. $result = Database::query($sql);
  59. $row = Database::fetch_array($result);
  60. $forum_thread_visibility = api_get_item_visibility(
  61. $courseInfo,
  62. TOOL_FORUM_THREAD,
  63. $row['thread_id'],
  64. api_get_session_id()
  65. );
  66. $forum_forum_visibility = api_get_item_visibility(
  67. $courseInfo,
  68. TOOL_FORUM,
  69. $row['forum_id'],
  70. api_get_session_id()
  71. );
  72. if ($forum_thread_visibility == 1 && $forum_forum_visibility == 1) {
  73. if (Security::check_abs_path(
  74. $full_file_name,
  75. api_get_path(SYS_COURSE_PATH).$courseInfo['path'].'/upload/forum/')
  76. ) {
  77. DocumentManager::file_send_for_download(
  78. $full_file_name,
  79. true,
  80. $row['filename']
  81. );
  82. }
  83. }
  84. exit;