download.php 2.8 KB

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