download.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 '../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. }
  43. $tbl_forum_attachment = Database::get_course_table(TABLE_FORUM_ATTACHMENT);
  44. $tbl_forum_post = Database::get_course_table(TABLE_FORUM_POST);
  45. $course_id = api_get_course_int_id();
  46. $courseInfo = api_get_course_info_by_id($course_id);
  47. // launch event
  48. Event::event_download($doc_url);
  49. $sql = 'SELECT thread_id, forum_id,filename
  50. FROM '.$tbl_forum_post.' f
  51. INNER JOIN '.$tbl_forum_attachment.' a
  52. ON a.post_id=f.post_id
  53. WHERE
  54. f.c_id = '.$course_id.' AND
  55. a.c_id = '.$course_id.' AND
  56. path LIKE BINARY "'.$doc_url.'"';
  57. $result = Database::query($sql);
  58. $row = Database::fetch_array($result);
  59. $forum_thread_visibility = api_get_item_visibility(
  60. $courseInfo,
  61. TOOL_FORUM_THREAD,
  62. $row['thread_id'],
  63. api_get_session_id()
  64. );
  65. $forum_forum_visibility = api_get_item_visibility(
  66. $courseInfo,
  67. TOOL_FORUM,
  68. $row['forum_id'],
  69. api_get_session_id()
  70. );
  71. if ($forum_thread_visibility==1 && $forum_forum_visibility==1) {
  72. if (Security::check_abs_path(
  73. $full_file_name,
  74. api_get_path(SYS_COURSE_PATH).$courseInfo['path'].'/upload/forum/')
  75. ) {
  76. DocumentManager::file_send_for_download(
  77. $full_file_name,
  78. true,
  79. $row['filename']
  80. );
  81. }
  82. }
  83. exit;