download.php 4.2 KB

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