Hpdownload.php 4.5 KB

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