download.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. $course_id = intval($_REQUEST['course_id']);
  24. $user_id = api_get_user_id();
  25. $course_info = api_get_course_info_by_id($course_id);
  26. $doc_url = $_REQUEST['file'];
  27. $session_id = api_get_session_id();
  28. if (empty($course_id)) {
  29. $course_id = api_get_course_int_id();
  30. }
  31. if (empty($course_id) || empty($doc_url)) {
  32. api_not_allowed();
  33. }
  34. $is_user_is_subscribed = CourseManager::is_user_subscribed_in_course($user_id, $course_info['code'], true, $session_id);
  35. if (!api_is_allowed_to_edit() && !$is_user_is_subscribed) {
  36. api_not_allowed();
  37. }
  38. //change the '&' that got rewritten to '///' by mod_rewrite back to '&'
  39. $doc_url = str_replace('///', '&', $doc_url);
  40. //still a space present? it must be a '+' (that got replaced by mod_rewrite)
  41. $doc_url = str_replace(' ', '+', $doc_url);
  42. $doc_url = str_replace('/..', '', $doc_url); //echo $doc_url;
  43. $full_file_name = api_get_path(SYS_COURSE_PATH).$course_info['path'].'/upload/calendar/'.$doc_url;
  44. //if the rewrite rule asks for a directory, we redirect to the document explorer
  45. if (is_dir($full_file_name)) {
  46. //remove last slash if present
  47. //$doc_url = ($doc_url{strlen($doc_url)-1}=='/')?substr($doc_url,0,strlen($doc_url)-1):$doc_url;
  48. //mod_rewrite can change /some/path/ to /some/path// in some cases, so clean them all off (René)
  49. while ($doc_url{$dul = strlen($doc_url)-1}=='/') $doc_url = substr($doc_url,0,$dul);
  50. //create the path
  51. $document_explorer = api_get_path(WEB_COURSE_PATH).$course_info['path']; // home course path
  52. //redirect
  53. header('Location: '.$document_explorer);
  54. exit;
  55. }
  56. $tbl_agenda_attachment = Database::get_course_table(TABLE_AGENDA_ATTACHMENT);
  57. // launch event
  58. event_download($doc_url);
  59. $sql='SELECT filename FROM '.$tbl_agenda_attachment.'
  60. WHERE c_id = '.$course_id.' AND path LIKE BINARY "'.Database::escape_string($doc_url).'"';
  61. $result = Database::query($sql);
  62. if (Database::num_rows($result)) {
  63. $row = Database::fetch_array($result);
  64. $title = str_replace(' ','_', $row['filename']);
  65. if (Security::check_abs_path($full_file_name, api_get_path(SYS_COURSE_PATH).$course_info['path'].'/upload/calendar/')) {
  66. DocumentManager::file_send_for_download($full_file_name,TRUE, $title);
  67. }
  68. }
  69. api_not_allowed();