download.php 2.4 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.document
  10. */
  11. session_cache_limiter('public');
  12. require_once __DIR__.'/../inc/global.inc.php';
  13. api_protect_course_script(true);
  14. $this_section = SECTION_COURSES;
  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. $doc_url = $_GET['file'];
  20. //change the '&' that got rewritten to '///' by mod_rewrite back to '&'
  21. $doc_url = str_replace('///', '&', $doc_url);
  22. //still a space present? it must be a '+' (that got replaced by mod_rewrite)
  23. $doc_url = str_replace(' ', '+', $doc_url);
  24. $doc_url = str_replace('/..', '', $doc_url);
  25. $tbl_forum_attachment = Database::get_course_table(TABLE_FORUM_ATTACHMENT);
  26. $tbl_forum_post = Database::get_course_table(TABLE_FORUM_POST);
  27. $course_id = api_get_course_int_id();
  28. $courseInfo = api_get_course_info_by_id($course_id);
  29. $sql = 'SELECT thread_id, forum_id,filename
  30. FROM '.$tbl_forum_post.' f
  31. INNER JOIN '.$tbl_forum_attachment.' a
  32. ON a.post_id=f.post_id
  33. WHERE
  34. f.c_id = '.$course_id.' AND
  35. a.c_id = '.$course_id.' AND
  36. path LIKE BINARY "'.$doc_url.'"';
  37. $result = Database::query($sql);
  38. $row = Database::fetch_array($result);
  39. if (empty($row)) {
  40. api_not_allowed();
  41. }
  42. $forum_thread_visibility = api_get_item_visibility(
  43. $courseInfo,
  44. TOOL_FORUM_THREAD,
  45. $row['thread_id'],
  46. api_get_session_id()
  47. );
  48. $forum_forum_visibility = api_get_item_visibility(
  49. $courseInfo,
  50. TOOL_FORUM,
  51. $row['forum_id'],
  52. api_get_session_id()
  53. );
  54. if ($forum_thread_visibility == 1 && $forum_forum_visibility == 1) {
  55. $full_file_name = api_get_path(SYS_COURSE_PATH).api_get_course_path().'/upload/forum/'.$doc_url;
  56. if (Security::check_abs_path(
  57. $full_file_name,
  58. api_get_path(SYS_COURSE_PATH).$courseInfo['path'].'/upload/forum/'
  59. )) {
  60. // launch event
  61. Event::event_download($doc_url);
  62. $result = DocumentManager::file_send_for_download(
  63. $full_file_name,
  64. true,
  65. $row['filename']
  66. );
  67. if ($result === false) {
  68. api_not_allowed(true);
  69. }
  70. }
  71. }
  72. api_not_allowed();