download.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This file is responsible for passing requested documents to the browser.
  5. * Html files are parsed to fix a few problems with URLs,
  6. * but this code will hopefully be replaced soon by an Apache URL
  7. * rewrite mechanism.
  8. *
  9. * @package chamilo.blogs
  10. */
  11. /* MAIN CODE */
  12. session_cache_limiter('public');
  13. require_once '../inc/global.inc.php';
  14. $this_section=SECTION_COURSES;
  15. require_once api_get_path(LIBRARY_PATH).'document.lib.php';
  16. // IMPORTANT to avoid caching of documents
  17. header('Expires: Wed, 01 Jan 1990 00:00:00 GMT');
  18. header('Cache-Control: public');
  19. header('Pragma: no-cache');
  20. //protection
  21. api_protect_course_script(true);
  22. $doc_url = $_GET['file'];
  23. //change the '&' that got rewritten to '///' by mod_rewrite back to '&'
  24. $doc_url = str_replace('///', '&', $doc_url);
  25. //still a space present? it must be a '+' (that got replaced by mod_rewrite)
  26. $doc_url = str_replace(' ', '+', $doc_url);
  27. $doc_url = str_replace('/..', '', $doc_url); //echo $doc_url;
  28. if (! isset($_course)) {
  29. api_not_allowed(true);
  30. }
  31. $full_file_name = api_get_path(SYS_COURSE_PATH).api_get_course_path().'/upload/blog/'.$doc_url;
  32. //if the rewrite rule asks for a directory, we redirect to the course view
  33. if (is_dir($full_file_name)) {
  34. //remove last slash if present
  35. while ($doc_url{$dul = strlen($doc_url)-1}=='/') $doc_url = substr($doc_url,0,$dul);
  36. //create the path
  37. $document_explorer = api_get_path(WEB_COURSE_PATH).api_get_course_path(); // home course path
  38. //redirect
  39. header('Location: '.$document_explorer);
  40. }
  41. $tbl_blogs_attachment = Database::get_course_table(TABLE_BLOGS_ATTACHMENT);
  42. // launch event
  43. event_download($doc_url);
  44. $sql = 'SELECT filename FROM '.$tbl_blogs_attachment.' WHERE path LIKE BINARY "'.Database::escape_string($doc_url).'"';
  45. $result = Database::query($sql);
  46. if (Database::num_rows($result) > 0) {
  47. $row = Database::fetch_array($result);
  48. if (Security::check_abs_path($full_file_name, api_get_path(SYS_COURSE_PATH).api_get_course_path().'/upload/blog/')) {
  49. DocumentManager::file_send_for_download($full_file_name, TRUE, $row['filename']);
  50. }
  51. }
  52. exit;