Hpdownload.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This script shows the list of exercises for administrators and students.
  5. *
  6. * @package chamilo.exercise
  7. *
  8. * @author Istvan Mandak
  9. *
  10. * @version $Id: Hpdownload.php 22201 2009-07-17 19:57:03Z cfasanando $
  11. */
  12. session_cache_limiter('public');
  13. require_once __DIR__.'/../inc/global.inc.php';
  14. api_protect_course_script(true);
  15. $this_section = SECTION_COURSES;
  16. $tbl_document = Database::get_course_table(TABLE_DOCUMENT);
  17. $doc_url = str_replace(['../', '\\..', '\\0', '..\\'], ['', '', '', ''], urldecode($_GET['doc_url']));
  18. $filename = basename($doc_url);
  19. // launch event
  20. //Event::event_download($doc_url);
  21. if (isset($_course['path'])) {
  22. $course_path = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document';
  23. $full_file_name = $course_path.Security::remove_XSS($doc_url);
  24. } else {
  25. $course_path = api_get_path(SYS_COURSE_PATH).$cid.'/document';
  26. $full_file_name = $course_path.Security::remove_XSS($doc_url);
  27. }
  28. if (!is_file($full_file_name)) {
  29. exit;
  30. }
  31. if (!Security::check_abs_path($full_file_name, $course_path.'/')) {
  32. exit;
  33. }
  34. $extension = explode('.', $filename);
  35. $extension = strtolower($extension[sizeof($extension) - 1]);
  36. switch ($extension) {
  37. case 'gz':
  38. $content_type = 'application/x-gzip';
  39. break;
  40. case 'zip':
  41. $content_type = 'application/zip';
  42. break;
  43. case 'pdf':
  44. $content_type = 'application/pdf';
  45. break;
  46. case 'png':
  47. $content_type = 'image/png';
  48. break;
  49. case 'gif':
  50. $content_type = 'image/gif';
  51. break;
  52. case 'jpg':
  53. $content_type = 'image/jpeg';
  54. break;
  55. case 'txt':
  56. $content_type = 'text/plain';
  57. break;
  58. case 'htm':
  59. $content_type = 'text/html';
  60. break;
  61. case 'html':
  62. $content_type = 'text/html';
  63. break;
  64. default:
  65. $content_type = 'application/octet-stream';
  66. break;
  67. }
  68. header('Content-disposition: filename='.$filename);
  69. header('Content-Type: '.$content_type);
  70. header('Expires: '.gmdate('D, d M Y H:i:s', time() + 10).' GMT');
  71. header('Last-Modified: '.gmdate('D, d M Y H:i:s', time() + 10).' GMT');
  72. /*
  73. Dynamic parsing section
  74. is activated whenever a user views an html file
  75. work in progress
  76. - question: we could also parse per line,
  77. perhaps this would be faster.
  78. ($file_content = file($full_file_name) returns file in array)
  79. */
  80. if ($content_type == 'text/html') {
  81. $directory_name = dirname($full_file_name);
  82. $coursePath = api_get_path(SYS_COURSE_PATH);
  83. $dir = str_replace(['\\', $coursePath.$_course['path'].'/document'], ['/', ''], $directory_name);
  84. if ($dir[strlen($dir) - 1] != '/') {
  85. $dir .= '/';
  86. }
  87. //Parse whole file at one
  88. $fp = fopen($full_file_name, "r");
  89. $file_content = fread($fp, filesize($full_file_name));
  90. fclose($fp);
  91. $exercisePath = api_get_self();
  92. $exfile = explode('/', $exercisePath);
  93. $exfile = $exfile[sizeof($exfile) - 1];
  94. $exercisePath = substr($exercisePath, 0, strpos($exercisePath, $exfile));
  95. $content = $file_content;
  96. $mit = "function Finish(){";
  97. $js_content = "var SaveScoreVariable = 0; // This variable included by Chamilo\n".
  98. "function mySaveScore() // This function included by Chamilo\n".
  99. "{\n".
  100. " if (SaveScoreVariable==0)\n".
  101. " {\n".
  102. " SaveScoreVariable = 1;\n".
  103. " if (C.ie)\n".
  104. " {\n".
  105. " document.location.href = \"".$exercisePath."savescores.php?origin=$origin&time=$time&test=".$doc_url."&uid=".$_user['user_id']."&cid=".$cid."&score=\"+Score;\n".
  106. " //window.alert(Score);\n".
  107. " }\n".
  108. " else\n".
  109. " {\n".
  110. " }\n".
  111. " }\n".
  112. "}\n".
  113. "// Must be included \n".
  114. "function Finish(){\n".
  115. " mySaveScore();";
  116. $newcontent = str_replace($mit, $js_content, $content);
  117. $prehref = "javascript:void(0);";
  118. $posthref = api_get_path(WEB_CODE_PATH)."main/exercise/Hpdownload.php?doc_url=".$doc_url."&cid=".$cid."&uid=".$uid;
  119. $newcontent = str_replace($prehref, $posthref, $newcontent);
  120. $prehref = "class=\"GridNum\" onclick=";
  121. $posthref = "class=\"GridNum\" onMouseover=";
  122. $newcontent = str_replace($prehref, $posthref, $newcontent);
  123. header('Content-length: '.strlen($newcontent));
  124. // Dipsp.
  125. echo $newcontent;
  126. exit();
  127. }
  128. //normal case, all non-html files
  129. //header('Content-length: ' . filesize($full_file_name));
  130. $fp = fopen($full_file_name, 'rb');
  131. fpassthru($fp);
  132. fclose($fp);