download.php 2.9 KB

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