download.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This file is responsible for passing requested documents to the browser.
  5. *
  6. * @package chamilo.document
  7. */
  8. /**
  9. * Code
  10. * Many functions updated and moved to lib/document.lib.php
  11. */
  12. session_cache_limiter('none');
  13. //require_once '../inc/global.inc.php';
  14. $this_section = SECTION_COURSES;
  15. // Protection
  16. api_protect_course_script();
  17. if (!isset($_course)) {
  18. api_not_allowed(true);
  19. }
  20. $doc_url = $_GET['doc_url'];
  21. // Change the '&' that got rewritten to '///' by mod_rewrite back to '&'
  22. $doc_url = str_replace('///', '&', $doc_url);
  23. // Still a space present? it must be a '+' (that got replaced by mod_rewrite)
  24. $doc_url = str_replace(' ', '+', $doc_url);
  25. $doc_url = str_replace(array('../', '\\..', '\\0', '..\\'), array('', '', '', ''), $doc_url); //echo $doc_url;
  26. if (strpos($doc_url,'../') OR strpos($doc_url,'/..')) {
  27. $doc_url = '';
  28. }
  29. // Dealing with image included into survey: when users receive a link towards a
  30. // survey while not being authenticated on the plateform.
  31. // The administrator should probably be able to disable this code through admin
  32. // inteface.
  33. $refer_script = strrchr($_SERVER["HTTP_REFERER"], '/');
  34. $sys_course_path = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document';
  35. if (substr($refer_script, 0, 15) == '/fillsurvey.php') {
  36. $invitation = substr(strstr($refer_script, 'invitationcode='), 15);
  37. $course = strstr($refer_script, 'course=');
  38. $course = substr($course, 7, strpos($course, '&') - 7);
  39. include '../survey/survey.download.inc.php';
  40. $_course = check_download_survey($course, $invitation, $doc_url);
  41. $_course['path'] = $_course['directory'];
  42. } else {
  43. // If the rewrite rule asks for a directory, we redirect to the document explorer
  44. if (is_dir($sys_course_path.$doc_url)) {
  45. // Remove last slash if present
  46. // mod_rewrite can change /some/path/ to /some/path// in some cases, so clean them all off (René)
  47. while ($doc_url{$dul = strlen($doc_url) - 1} == '/') {
  48. $doc_url = substr($doc_url, 0, $dul);
  49. }
  50. // Group folder?
  51. $gid_req = ($_GET['gidReq']) ? '&gidReq='.Security::remove_XSS($_GET['gidReq']) : '';
  52. // Create the path
  53. $document_explorer = api_get_path(WEB_CODE_PATH).'document/document.php?curdirpath='.urlencode($doc_url).'&cidReq='.Security::remove_XSS($_GET['cidReq']).$gid_req;
  54. // Redirect
  55. header('Location: '.$document_explorer);
  56. }
  57. }
  58. //Fixes swf upload problem in chamilo 1.8.x. When uploading a file with
  59. //the character "-" the filename was changed from "-" to "_" in the DB for no reason
  60. $path_info = pathinfo($doc_url);
  61. $fix_file_name = false;
  62. if (isset($path_info['extension']) && $path_info['extension'] == 'swf') {
  63. $fixed_url = str_replace('-', '_', $doc_url);
  64. $doc_id = DocumentManager::get_document_id(api_get_course_info(), $doc_url);
  65. if (!$doc_id) {
  66. $fix_file_name = true;
  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_download($doc_url);
  88. DocumentManager::file_send_for_download($full_file_name);
  89. }
  90. exit;