download.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php // $Id: $
  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.calendar
  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 api_get_path(LIBRARY_PATH).'document.lib.php';
  18. require_once 'agenda.inc.php';
  19. // IMPORTANT to avoid caching of documents
  20. header('Expires: Wed, 01 Jan 1990 00:00:00 GMT');
  21. header('Cache-Control: public');
  22. header('Pragma: no-cache');
  23. //protection
  24. api_protect_course_script(true);
  25. $doc_url = $_GET['file'];
  26. //change the '&' that got rewritten to '///' by mod_rewrite back to '&'
  27. $doc_url = str_replace('///', '&', $doc_url);
  28. //still a space present? it must be a '+' (that got replaced by mod_rewrite)
  29. $doc_url = str_replace(' ', '+', $doc_url);
  30. $doc_url = str_replace('/..', '', $doc_url); //echo $doc_url;
  31. if (!isset($_course)) {
  32. api_not_allowed(true);
  33. }
  34. $full_file_name = api_get_path(SYS_COURSE_PATH).api_get_course_path().'/upload/calendar/'.$doc_url;
  35. //if the rewrite rule asks for a directory, we redirect to the document explorer
  36. if (is_dir($full_file_name))
  37. {
  38. //remove last slash if present
  39. //$doc_url = ($doc_url{strlen($doc_url)-1}=='/')?substr($doc_url,0,strlen($doc_url)-1):$doc_url;
  40. //mod_rewrite can change /some/path/ to /some/path// in some cases, so clean them all off (René)
  41. while ($doc_url{$dul = strlen($doc_url)-1}=='/') $doc_url = substr($doc_url,0,$dul);
  42. //create the path
  43. $document_explorer = api_get_path(WEB_COURSE_PATH).api_get_course_path(); // home course path
  44. //redirect
  45. header('Location: '.$document_explorer);
  46. exit;
  47. }
  48. $tbl_agenda_attachment = Database::get_course_table(TABLE_AGENDA_ATTACHMENT);
  49. // launch event
  50. event_download($doc_url);
  51. $sql='SELECT filename FROM '.$tbl_agenda_attachment.'
  52. WHERE path LIKE BINARY "'.Database::escape_string($doc_url).'"';
  53. $result = Database::query($sql);
  54. if (Database::num_rows($result) > 0) {
  55. $row = Database::fetch_array($result);
  56. $title = str_replace(' ','_', $row['filename']);
  57. if (Security::check_abs_path($full_file_name, api_get_path(SYS_COURSE_PATH).api_get_course_path().'/upload/calendar/')) {
  58. DocumentManager::file_send_for_download($full_file_name,TRUE, $title);
  59. }
  60. }
  61. exit;