download.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. $doc_url = str_replace(['../', '\\..', '\\0', '..\\'], ['', '', '', ''], $doc_url); //echo $doc_url;
  24. if (strpos($doc_url, '../') || strpos($doc_url, '/..')) {
  25. $doc_url = '';
  26. }
  27. // Dealing with image included into survey: when users receive a link towards a
  28. // survey while not being authenticated on the platform.
  29. // The administrator should probably be able to disable this code through admin
  30. // inteface.
  31. $refer_script = isset($_SERVER["HTTP_REFERER"]) ? strrchr($_SERVER["HTTP_REFERER"], '/') : null;
  32. $sys_course_path = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document';
  33. if (substr($refer_script, 0, 15) == '/fillsurvey.php') {
  34. $invitation = substr(strstr($refer_script, 'invitationcode='), 15);
  35. $course = strstr($refer_script, 'course=');
  36. $course = substr($course, 7, strpos($course, '&') - 7);
  37. include '../survey/survey.download.inc.php';
  38. $_course = check_download_survey($course, $invitation, $doc_url);
  39. $_course['path'] = $_course['directory'];
  40. } else {
  41. // If the rewrite rule asks for a directory, we redirect to the document explorer
  42. if (is_dir($sys_course_path.$doc_url)) {
  43. // Remove last slash if present
  44. // mod_rewrite can change /some/path/ to /some/path// in some cases, so clean them all off (René)
  45. while ($doc_url[$dul = strlen($doc_url) - 1] == '/') {
  46. $doc_url = substr($doc_url, 0, $dul);
  47. }
  48. // Group folder?
  49. $gid_req = ($_GET['gidReq']) ? '&gidReq='.intval($_GET['gidReq']) : '';
  50. // Create the path
  51. $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));
  52. // Redirect
  53. header('Location: '.$document_explorer);
  54. exit;
  55. }
  56. }
  57. //Fixes swf upload problem in chamilo 1.8.x. When uploading a file with
  58. //the character "-" the filename was changed from "-" to "_" in the DB for no reason
  59. $path_info = pathinfo($doc_url);
  60. $fix_file_name = false;
  61. if (isset($path_info['extension']) && $path_info['extension'] == 'swf') {
  62. $fixed_url = str_replace('-', '_', $doc_url);
  63. $doc_id = DocumentManager::get_document_id(api_get_course_info(), $doc_url);
  64. if (!$doc_id) {
  65. $doc_id = DocumentManager::get_document_id(api_get_course_info(), $doc_url, '0');
  66. if (!$doc_id) {
  67. $fix_file_name = true;
  68. }
  69. }
  70. }
  71. if (Security::check_abs_path($sys_course_path.$doc_url, $sys_course_path.'/')) {
  72. $fullFileName = $sys_course_path.$doc_url;
  73. if ($fix_file_name) {
  74. $doc_url = $fixed_url;
  75. }
  76. // Check visibility of document and paths
  77. $is_visible = DocumentManager::is_visible($doc_url, $_course, api_get_session_id());
  78. //Document's slideshow thumbnails
  79. //correct $is_visible used in below and ??. Now the students can view the thumbnails too
  80. if (preg_match('/\.thumbs\/\./', $doc_url)) {
  81. $doc_url_thumbs = str_replace('.thumbs/.', '', $doc_url);
  82. $is_visible = DocumentManager::is_visible($doc_url_thumbs, $_course, api_get_session_id());
  83. }
  84. if (!api_is_allowed_to_edit() && !$is_visible) {
  85. echo Display::return_message(get_lang('ProtectedDocument'), 'error'); //api_not_allowed backbutton won't work.
  86. exit; // You shouldn't be here anyway.
  87. }
  88. // Launch event
  89. Event::event_download($doc_url);
  90. $download = !empty($_GET['dl']) ? true : false;
  91. $result = DocumentManager::file_send_for_download($fullFileName, $download);
  92. if ($result === false) {
  93. api_not_allowed(true);
  94. }
  95. }
  96. exit;