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