download.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php // $Id: download.php 12218 2007-05-01 18:27:14Z yannoo $
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. ==============================================================================
  5. * This file is responsible for passing requested documents to the browser.
  6. * Html files are parsed to fix a few problems with URLs,
  7. * but this code will hopefully be replaced soon by an Apache URL
  8. * rewrite mechanism.
  9. *
  10. * @package dokeos.document
  11. ==============================================================================
  12. */
  13. /*
  14. ==============================================================================
  15. MAIN CODE
  16. ==============================================================================
  17. */
  18. session_cache_limiter('public');
  19. include('../inc/global.inc.php');
  20. $this_section=SECTION_COURSES;
  21. include(api_get_path(LIBRARY_PATH).'document.lib.php');
  22. require_once('forumconfig.inc.php');
  23. // IMPORTANT to avoid caching of documents
  24. header('Expires: Wed, 01 Jan 1990 00:00:00 GMT');
  25. header('Cache-Control: public');
  26. header('Pragma: no-cache');
  27. //protection
  28. api_protect_course_script(true);
  29. $doc_url = $_GET['file'];
  30. //change the '&' that got rewritten to '///' by mod_rewrite back to '&'
  31. $doc_url = str_replace('///', '&', $doc_url);
  32. //still a space present? it must be a '+' (that got replaced by mod_rewrite)
  33. $doc_url = str_replace(' ', '+', $doc_url);
  34. $doc_url = str_replace('/..', '', $doc_url); //echo $doc_url;
  35. if (! isset($_course))
  36. {
  37. api_not_allowed(true);
  38. }
  39. $full_file_name = api_get_path(SYS_COURSE_PATH).api_get_course_path().'/upload/forum/'.$doc_url;
  40. //if the rewrite rule asks for a directory, we redirect to the document explorer
  41. if (is_dir($full_file_name))
  42. {
  43. //remove last slash if present
  44. //$doc_url = ($doc_url{strlen($doc_url)-1}=='/')?substr($doc_url,0,strlen($doc_url)-1):$doc_url;
  45. //mod_rewrite can change /some/path/ to /some/path// in some cases, so clean them all off (René)
  46. while ($doc_url{$dul = strlen($doc_url)-1}=='/') $doc_url = substr($doc_url,0,$dul);
  47. //create the path
  48. $document_explorer = api_get_path(WEB_COURSE_PATH).api_get_course_path(); // home course path
  49. //redirect
  50. header('Location: '.$document_explorer);
  51. }
  52. $tbl_forum_attachment = Database::get_course_table(TABLE_FORUM_ATTACHMENT);
  53. $tbl_forum_post = Database::get_course_table(TABLE_FORUM_POST);
  54. // launch event
  55. event_download($doc_url);
  56. $sql='SELECT thread_id, forum_id,filename FROM '.$tbl_forum_post.' f INNER JOIN '.$tbl_forum_attachment.' a
  57. ON a.post_id=f.post_id WHERE path LIKE BINARY "'.$doc_url.'"';
  58. $result= Database::query($sql);
  59. $row= Database::fetch_array($result);
  60. $forum_thread_visibility=api_get_item_visibility(api_get_course_info($course_code),TOOL_FORUM_THREAD,$row['thread_id']);
  61. $forum_forum_visibility=api_get_item_visibility(api_get_course_info($course_code),TOOL_FORUM,$row['forum_id']);
  62. if ($forum_thread_visibility==1 && $forum_forum_visibility==1)
  63. {
  64. DocumentManager::file_send_for_download($full_file_name,TRUE, $row['filename']);
  65. }
  66. exit;
  67. ?>