Hpdownload.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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(array('\\',$_configuration['root_sys']."courses/".$_course['path'].'/document'),array('/',''),$directory_name);
  62. if($dir[strlen($dir)-1] != '/') {
  63. $dir.='/';
  64. }
  65. //Parse whole file at one
  66. $fp = fopen($full_file_name, "r");
  67. $file_content = fread ($fp, filesize ($full_file_name));
  68. fclose($fp);
  69. //$file_content = api_replace_parameter($dir, $file_content, "src");
  70. //$file_content = api_replace_parameter($dir, $file_content, "href");
  71. /*
  72. //parse line per line
  73. $file_content_array = file($full_file_name);
  74. foreach($file_content_array as $line)
  75. {
  76. $line = api_replace_parameter($dir, $line, "src");
  77. $line = api_replace_parameter($dir, $line, "href");
  78. $file_content .= $line;
  79. }
  80. */
  81. $exercisePath = api_get_self();
  82. $exfile = explode('/',$exercisePath);
  83. $exfile = $exfile[sizeof($exfile)-1];
  84. $exercisePath = substr($exercisePath,0,strpos($exercisePath,$exfile));
  85. $exercisePath = $exercisePath;
  86. $content = $file_content;
  87. $mit = "function Finish(){";
  88. $js_content = "var SaveScoreVariable = 0; // This variable included by Dokeos System\n".
  89. "function mySaveScore() // This function included by Dokeos System\n".
  90. "{\n".
  91. " if (SaveScoreVariable==0)\n".
  92. " {\n".
  93. " SaveScoreVariable = 1;\n".
  94. " if (C.ie)\n".
  95. " {\n".
  96. " document.location.href = \"".$exercisePath."savescores.php?origin=$origin&time=$time&test=".$doc_url."&uid=".$_user['user_id']."&cid=".$cid."&score=\"+Score;\n".
  97. " //window.alert(Score);\n".
  98. " }\n".
  99. " else\n".
  100. " {\n".
  101. " }\n".
  102. " }\n".
  103. "}\n".
  104. "// Must be included \n".
  105. "function Finish(){\n".
  106. " mySaveScore();";
  107. $newcontent = str_replace($mit,$js_content,$content);
  108. $prehref="javascript:void(0);";
  109. $posthref=$_configuration['root_web']."main/exercice/Hpdownload.php?doc_url=".$doc_url."&cid=".$cid."&uid=".$uid;
  110. $newcontent = str_replace($prehref,$posthref,$newcontent);
  111. $prehref="class=\"GridNum\" onclick=";
  112. $posthref="class=\"GridNum\" onMouseover=";
  113. $newcontent = str_replace($prehref,$posthref,$newcontent);
  114. header('Content-length: '.strlen($newcontent));
  115. // Dipsp.
  116. echo $newcontent;
  117. exit();
  118. }
  119. //normal case, all non-html files
  120. //header('Content-length: '.filesize($full_file_name));
  121. $fp=fopen($full_file_name,'rb');
  122. fpassthru($fp);
  123. fclose($fp);
  124. ?>