download.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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} == '/') $doc_url = substr($doc_url, 0, $dul);
  34. //create the path
  35. $document_explorer = api_get_path(WEB_COURSE_PATH).api_get_course_path(); // home course path
  36. //redirect
  37. header('Location: '.$document_explorer);
  38. exit;
  39. }
  40. $tbl_blogs_attachment = Database::get_course_table(TABLE_BLOGS_ATTACHMENT);
  41. $course_id = api_get_course_int_id();
  42. // launch event
  43. Event::event_download($doc_url);
  44. $sql = 'SELECT filename FROM '.$tbl_blogs_attachment.'
  45. WHERE
  46. c_id = '.$course_id.' AND
  47. path LIKE BINARY "'.Database::escape_string($doc_url).'"';
  48. $result = Database::query($sql);
  49. if (Database::num_rows($result) > 0) {
  50. $row = Database::fetch_array($result);
  51. if (Security::check_abs_path(
  52. $full_file_name,
  53. api_get_path(SYS_COURSE_PATH).api_get_course_path().'/upload/blog/')
  54. ) {
  55. $result = DocumentManager::file_send_for_download(
  56. $full_file_name,
  57. true,
  58. $row['filename']
  59. );
  60. if ($result === false) {
  61. api_not_allowed(true);
  62. }
  63. }
  64. }
  65. exit;