download.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This file is responsible for passing requested documents to the browser.
  5. * Many functions updated and moved to lib/document.lib.php.
  6. *
  7. * @package chamilo.document
  8. */
  9. session_cache_limiter('none');
  10. require_once __DIR__.'/../inc/global.inc.php';
  11. $this_section = SECTION_COURSES;
  12. // Protection
  13. api_protect_course_script();
  14. $_course = api_get_course_info();
  15. if (!isset($_course)) {
  16. api_not_allowed(true);
  17. }
  18. $doc_url = $_GET['doc_url'];
  19. // Change the '&' that got rewritten to '///' by mod_rewrite back to '&'
  20. $doc_url = str_replace('///', '&', $doc_url);
  21. // Still a space present? it must be a '+' (that got replaced by mod_rewrite)
  22. $doc_url = str_replace(' ', '+', $doc_url);
  23. $docUrlParts = preg_split('/\/|\\\/', $doc_url);
  24. $doc_url = '';
  25. foreach ($docUrlParts as $docUrlPart) {
  26. if (empty($docUrlPart) || in_array($docUrlPart, ['.', '..', '0'])) {
  27. continue;
  28. }
  29. $doc_url .= '/'.$docUrlPart;
  30. }
  31. if (empty($doc_url)) {
  32. api_not_allowed(
  33. !empty($_GET['origin']) && $_GET['origin'] === 'learnpath'
  34. );
  35. }
  36. // Dealing with image included into survey: when users receive a link towards a
  37. // survey while not being authenticated on the platform.
  38. // The administrator should probably be able to disable this code through admin
  39. // inteface.
  40. $refer_script = isset($_SERVER["HTTP_REFERER"]) ? strrchr($_SERVER["HTTP_REFERER"], '/') : null;
  41. $sys_course_path = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document';
  42. if (substr($refer_script, 0, 15) == '/fillsurvey.php') {
  43. $invitation = substr(strstr($refer_script, 'invitationcode='), 15);
  44. $course = strstr($refer_script, 'course=');
  45. $course = substr($course, 7, strpos($course, '&') - 7);
  46. include '../survey/survey.download.inc.php';
  47. $_course = check_download_survey($course, $invitation, $doc_url);
  48. $_course['path'] = $_course['directory'];
  49. } else {
  50. // If the rewrite rule asks for a directory, we redirect to the document explorer
  51. if (is_dir($sys_course_path.$doc_url)) {
  52. // Remove last slash if present
  53. // mod_rewrite can change /some/path/ to /some/path// in some cases, so clean them all off (René)
  54. while ($doc_url[$dul = strlen($doc_url) - 1] == '/') {
  55. $doc_url = substr($doc_url, 0, $dul);
  56. }
  57. // Group folder?
  58. $gid_req = ($_GET['gidReq']) ? '&gidReq='.intval($_GET['gidReq']) : '';
  59. // Create the path
  60. $document_explorer = api_get_path(WEB_CODE_PATH).'document/document.php?curdirpath='.urlencode($doc_url).'&'.api_get_cidreq_params(Security::remove_XSS($_GET['cidReq'], 0, $gid_req));
  61. // Redirect
  62. header('Location: '.$document_explorer);
  63. exit;
  64. }
  65. }
  66. //Fixes swf upload problem in chamilo 1.8.x. When uploading a file with
  67. //the character "-" the filename was changed from "-" to "_" in the DB for no reason
  68. $path_info = pathinfo($doc_url);
  69. $fix_file_name = false;
  70. if (isset($path_info['extension']) && $path_info['extension'] == 'swf') {
  71. $fixed_url = str_replace('-', '_', $doc_url);
  72. $doc_id = DocumentManager::get_document_id(api_get_course_info(), $doc_url);
  73. if (!$doc_id) {
  74. $doc_id = DocumentManager::get_document_id(api_get_course_info(), $doc_url, '0');
  75. if (!$doc_id) {
  76. $fix_file_name = true;
  77. }
  78. }
  79. }
  80. if (Security::check_abs_path($sys_course_path.$doc_url, $sys_course_path.'/')) {
  81. $fullFileName = $sys_course_path.$doc_url;
  82. if ($fix_file_name) {
  83. $doc_url = $fixed_url;
  84. }
  85. // Check visibility of document and paths
  86. $is_visible = DocumentManager::is_visible($doc_url, $_course, api_get_session_id());
  87. //Document's slideshow thumbnails
  88. //correct $is_visible used in below and ??. Now the students can view the thumbnails too
  89. if (preg_match('/\.thumbs\/\./', $doc_url)) {
  90. $doc_url_thumbs = str_replace('.thumbs/.', '', $doc_url);
  91. $is_visible = DocumentManager::is_visible($doc_url_thumbs, $_course, api_get_session_id());
  92. }
  93. if (!api_is_allowed_to_edit() && !$is_visible) {
  94. echo Display::return_message(get_lang('Protected Document'), 'error'); //api_not_allowed backbutton won't work.
  95. exit; // You shouldn't be here anyway.
  96. }
  97. // Launch event
  98. Event::event_download($doc_url);
  99. $download = !empty($_GET['dl']) ? true : false;
  100. $result = DocumentManager::file_send_for_download($fullFileName, $download);
  101. if ($result === false) {
  102. api_not_allowed(true);
  103. }
  104. }
  105. exit;