dropbox_download.php 6.4 KB

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