dropbox_download.php 6.1 KB

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