download.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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.announcements
  10. */
  11. session_cache_limiter('nocache');
  12. require_once __DIR__.'/../inc/global.inc.php';
  13. // IMPORTANT to avoid caching of documents
  14. header('Expires: Wed, 01 Jan 1990 00:00:00 GMT');
  15. header('Cache-Control: public');
  16. header('Pragma: no-cache');
  17. //protection
  18. api_protect_course_script(true);
  19. $doc_url = $_GET['file'];
  20. //change the '&' that got rewritten to '///' by mod_rewrite back to '&'
  21. $doc_url = str_replace('///', '&', $doc_url);
  22. //still a space present? it must be a '+' (that got replaced by mod_rewrite)
  23. $doc_url = str_replace(' ', '+', $doc_url);
  24. $doc_url = str_replace('/..', '', $doc_url); //echo $doc_url;
  25. if (strpos($doc_url, '../') || strpos($doc_url, '/..')) {
  26. $doc_url = '';
  27. }
  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/announcements/'.$doc_url;
  32. //if the rewrite rule asks for a directory, we redirect to the document explorer
  33. if (is_dir($full_file_name)) {
  34. //remove last slash if present
  35. //$doc_url = ($doc_url{strlen($doc_url)-1}=='/')?substr($doc_url,0,strlen($doc_url)-1):$doc_url;
  36. //mod_rewrite can change /some/path/ to /some/path// in some cases, so clean them all off (René)
  37. while ($doc_url[$dul = strlen($doc_url) - 1] == '/') {
  38. $doc_url = substr($doc_url, 0, $dul);
  39. }
  40. //create the path
  41. $document_explorer = api_get_path(WEB_COURSE_PATH).api_get_course_path(); // home course path
  42. //redirect
  43. header('Location: '.$document_explorer);
  44. exit;
  45. }
  46. $table = Database::get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT);
  47. // launch event
  48. Event::event_download($doc_url);
  49. $course_id = api_get_course_int_id();
  50. $doc_url = Database::escape_string($doc_url);
  51. $sql = "SELECT filename FROM $table
  52. WHERE c_id = $course_id AND path LIKE BINARY '$doc_url'";
  53. $result = Database::query($sql);
  54. if (Database::num_rows($result) > 0) {
  55. $row = Database::fetch_array($result);
  56. $title = str_replace(' ', '_', $row['filename']);
  57. if (Security::check_abs_path(
  58. $full_file_name,
  59. api_get_path(SYS_COURSE_PATH).api_get_course_path().'/upload/announcements/'
  60. )
  61. ) {
  62. $result = DocumentManager::file_send_for_download($full_file_name, true, $title);
  63. if ($result === false) {
  64. api_not_allowed(true);
  65. }
  66. }
  67. }
  68. exit;