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