download.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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).'&cidReq='.Security::remove_XSS($_GET['cidReq']).$gid_req;
  50. // Redirect
  51. header('Location: '.$document_explorer);
  52. }
  53. }
  54. //Fixes swf upload problem in chamilo 1.8.x. When uploading a file with
  55. //the character "-" the filename was changed from "-" to "_" in the DB for no reason
  56. $path_info = pathinfo($doc_url);
  57. $fix_file_name = false;
  58. if (isset($path_info['extension']) && $path_info['extension'] == 'swf') {
  59. $fixed_url = str_replace('-', '_', $doc_url);
  60. $doc_id = DocumentManager::get_document_id(api_get_course_info(), $doc_url);
  61. if (!$doc_id) {
  62. $fix_file_name = true;
  63. }
  64. }
  65. if (Security::check_abs_path($sys_course_path.$doc_url, $sys_course_path.'/')) {
  66. $full_file_name = $sys_course_path.$doc_url;
  67. if ($fix_file_name) {
  68. $doc_url = $fixed_url;
  69. }
  70. // Check visibility of document and paths
  71. $is_visible = DocumentManager::is_visible($doc_url, $_course, api_get_session_id());
  72. //Document's slideshow thumbnails
  73. //correct $is_visible used in below and ??. Now the students can view the thumbnails too
  74. if (preg_match('/\.thumbs\/\./', $doc_url)) {
  75. $doc_url_thumbs = str_replace('.thumbs/.', '', $doc_url);
  76. $is_visible = DocumentManager::is_visible($doc_url_thumbs, $_course, api_get_session_id());
  77. }
  78. if (!api_is_allowed_to_edit() && !$is_visible) {
  79. Display::display_error_message(get_lang('ProtectedDocument'));//api_not_allowed backbutton won't work.
  80. exit; // You shouldn't be here anyway.
  81. }
  82. // Launch event
  83. Event::event_download($doc_url);
  84. DocumentManager::file_send_for_download($full_file_name);
  85. }
  86. exit;