download.php 2.8 KB

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