dropbox_download.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.dropbox
  5. */
  6. /**
  7. * Code
  8. */
  9. // We cannot use dropbox_init.inc.php because this one already outputs data.
  10. $language_file = 'dropbox';
  11. // including the basic Chamilo initialisation file
  12. require_once '../inc/global.inc.php';
  13. // the dropbox configuration parameters
  14. require_once 'dropbox_config.inc.php';
  15. // the dropbox file that contains additional functions
  16. require_once 'dropbox_functions.inc.php';
  17. // the dropbox class
  18. require_once 'dropbox_class.inc.php';
  19. require_once api_get_path(LIBRARY_PATH).'document.lib.php';
  20. /* DOWNLOAD A FOLDER */
  21. $course_id = api_get_course_int_id();
  22. $user_id = api_get_user_id();
  23. if (isset($_GET['cat_id']) AND
  24. is_numeric($_GET['cat_id']) AND
  25. $_GET['action'] == 'downloadcategory' AND
  26. isset($_GET['sent_received'])
  27. ) {
  28. /** step 1: constructing the sql statement.
  29. Due to the nature off the classes of the dropbox the categories for sent files are stored in the table
  30. dropbox_file while the categories for the received files are stored in dropbox_post.
  31. It would have been more elegant if these could be stored in dropbox_person (which stores the link file-person)
  32. Therefore we have to create to separate sql statements to find which files are in the category
  33. (depending if we zip-download a sent category or a received category)*/
  34. if ($_GET['sent_received'] == 'sent') {
  35. // here we also incorporate the person table to make sure that deleted sent documents are not included.
  36. $sql = "SELECT DISTINCT file.id, file.filename, file.title
  37. FROM ".$dropbox_cnf['tbl_file']." file
  38. INNER JOIN ".$dropbox_cnf['tbl_person']." person
  39. ON (person.file_id=file.id AND file.c_id = $course_id AND person.c_id = $course_id)
  40. WHERE
  41. file.uploader_id = $user_id AND
  42. file.cat_id='".intval($_GET['cat_id'])."' AND
  43. person.user_id = $user_id";
  44. }
  45. if ($_GET['sent_received'] == 'received') {
  46. $sql = "SELECT DISTINCT file.id, file.filename, file.title
  47. FROM ".$dropbox_cnf['tbl_file']." file
  48. INNER JOIN ".$dropbox_cnf['tbl_person']." person
  49. ON (person.file_id=file.id AND file.c_id = $course_id AND person.c_id = $course_id)
  50. INNER JOIN ".$dropbox_cnf['tbl_post']." post
  51. ON (post.file_id = file.id AND post.c_id = $course_id AND file.c_id = $course_id)
  52. WHERE
  53. post.cat_id = ".intval($_GET['cat_id'])." AND
  54. post.dest_user_id = $user_id" ;
  55. }
  56. $files_to_download = array();
  57. $result = Database::query($sql);
  58. while ($row = Database::fetch_array($result)) {
  59. $files_to_download[] = $row['id'];
  60. }
  61. if (!is_array($files_to_download) OR empty($files_to_download)) {
  62. header('location: index.php?view='.Security::remove_XSS($_GET['sent_received']).'&error=ErrorNoFilesInFolder');
  63. exit;
  64. }
  65. zip_download($files_to_download);
  66. exit;
  67. }
  68. /* DOWNLOAD A FILE */
  69. /* AUTHORIZATION */
  70. // Check if the id makes sense
  71. if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {
  72. Display::display_header($nameTools, 'Dropbox');
  73. Display :: display_error_message(get_lang('Error'));
  74. Display::display_footer();
  75. exit;
  76. }
  77. // Check if the user is allowed to download the file
  78. $allowed_to_download = false;
  79. if (user_can_download_file($_GET['id'], api_get_user_id())) {
  80. $allowed_to_download = true;
  81. }
  82. /* ERROR IF NOT ALLOWED TO DOWNLOAD */
  83. if (!$allowed_to_download) {
  84. Display::display_header($nameTools, 'Dropbox');
  85. Display::display_error_message(get_lang('YouAreNotAllowedToDownloadThisFile'));
  86. Display::display_footer();
  87. exit;
  88. } else {
  89. /* DOWNLOAD THE FILE */
  90. // the user is allowed to download the file
  91. $_SESSION['_seen'][$_course['id']][TOOL_DROPBOX][] = intval($_GET['id']);
  92. $work = new Dropbox_work($_GET['id']);
  93. $path = dropbox_cnf('sysPath') . '/' . $work -> filename; //path to file as stored on server
  94. if (!Security::check_abs_path($path, dropbox_cnf('sysPath').'/')) {
  95. exit;
  96. }
  97. $file = $work->title;
  98. $mimetype = DocumentManager::file_get_mime_type(true);
  99. $fileinfo = pathinfo($file);
  100. $extension = $fileinfo['extension'];
  101. if (!empty($extension) && isset($mimetype[$extension]) && $_GET['action'] != 'download') {
  102. // give hint to browser about filetype
  103. header( 'Content-type: ' . $mimetype[$extension] . "\n");
  104. } else {
  105. //no information about filetype: force a download dialog window in browser
  106. header( "Content-type: application/octet-stream\n");
  107. }
  108. header('Content-Disposition: attachment; filename='.$file);
  109. /**
  110. * Note that if you use these two headers from a previous example:
  111. * header('Cache-Control: no-cache, must-revalidate');
  112. * header('Pragma: no-cache');
  113. * before sending a file to the browser, the "Open" option on Internet Explorer's file download dialog will not work properly. If the user clicks "Open" instead of "Save," the target application will open an empty file, because the downloaded file was not cached. The user will have to save the file to their hard drive in order to use it.
  114. * Make sure to leave these headers out if you'd like your visitors to be able to use IE's "Open" option.
  115. */
  116. header("Pragma: \n");
  117. header("Cache-Control: \n");
  118. header("Cache-Control: public\n"); // IE cannot download from sessions without a cache
  119. /*if (isset($_SERVER['HTTPS'])) {
  120. /**
  121. * We need to set the following headers to make downloads work using IE in HTTPS mode.
  122. *
  123. //header('Pragma: ');
  124. //header('Cache-Control: ');
  125. header("Expires: Mon, 26 Jul 1997 05:00:00 GMT\n");
  126. header("Last-Modified: " . gmdate( "D, d M Y H:i:s") . " GMT\n");
  127. header("Cache-Control: no-store, no-cache, must-revalidate\n"); // HTTP/1.1
  128. header("Cache-Control: post-check=0, pre-check=0\n", false);
  129. }*/
  130. header('Content-Description: '.trim(htmlentities($file)));
  131. header('Content-transfer-encoding: binary');
  132. header("Content-Length: " . filesize($path)."\n" );
  133. $fp = fopen( $path, 'rb');
  134. fpassthru($fp);
  135. exit();
  136. }
  137. //@todo clean this file the code below is useless there are 2 exits in previous conditions ... maybe a bad copy/paste/merge?
  138. exit;