Hpdownload.php 4.1 KB

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