download.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. session_cache_limiter('public');
  12. require_once __DIR__.'/../inc/global.inc.php';
  13. $this_section = SECTION_COURSES;
  14. // IMPORTANT to avoid caching of documents
  15. header('Expires: Wed, 01 Jan 1990 00:00:00 GMT');
  16. header('Cache-Control: public');
  17. header('Pragma: no-cache');
  18. //protection
  19. api_protect_course_script(true);
  20. $doc_url = $_GET['file'];
  21. //change the '&' that got rewritten to '///' by mod_rewrite back to '&'
  22. $doc_url = str_replace('///', '&', $doc_url);
  23. //still a space present? it must be a '+' (that got replaced by mod_rewrite)
  24. $doc_url = str_replace(' ', '+', $doc_url);
  25. $doc_url = str_replace('/..', '', $doc_url); //echo $doc_url;
  26. if (!isset($_course)) {
  27. api_not_allowed(true);
  28. }
  29. $full_file_name = api_get_path(SYS_COURSE_PATH).api_get_course_path().'/upload/blog/'.$doc_url;
  30. //if the rewrite rule asks for a directory, we redirect to the course view
  31. if (is_dir($full_file_name)) {
  32. //remove last slash if present
  33. while ($doc_url[$dul = strlen($doc_url) - 1] == '/') {
  34. $doc_url = substr($doc_url, 0, $dul);
  35. }
  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. exit;
  41. }
  42. $tbl_blogs_attachment = Database::get_course_table(TABLE_BLOGS_ATTACHMENT);
  43. $course_id = api_get_course_int_id();
  44. // launch event
  45. Event::event_download($doc_url);
  46. $sql = 'SELECT filename FROM '.$tbl_blogs_attachment.'
  47. WHERE
  48. c_id = '.$course_id.' AND
  49. path LIKE BINARY "'.Database::escape_string($doc_url).'"';
  50. $result = Database::query($sql);
  51. if (Database::num_rows($result) > 0) {
  52. $row = Database::fetch_array($result);
  53. if (Security::check_abs_path(
  54. $full_file_name,
  55. api_get_path(SYS_COURSE_PATH).api_get_course_path().'/upload/blog/'
  56. )
  57. ) {
  58. $result = DocumentManager::file_send_for_download(
  59. $full_file_name,
  60. true,
  61. $row['filename']
  62. );
  63. if ($result === false) {
  64. api_not_allowed(true);
  65. }
  66. }
  67. }
  68. exit;