dropbox_download.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 __DIR__.'/../inc/global.inc.php';
  8. // the dropbox file that contains additional functions
  9. require_once 'dropbox_functions.inc.php';
  10. /* DOWNLOAD A FOLDER */
  11. $course_id = api_get_course_int_id();
  12. $user_id = api_get_user_id();
  13. if (isset($_GET['cat_id']) &&
  14. is_numeric($_GET['cat_id']) &&
  15. $_GET['action'] == 'downloadcategory' &&
  16. isset($_GET['sent_received'])
  17. ) {
  18. /** step 1: constructing the sql statement.
  19. Due to the nature off the classes of the dropbox the categories for sent files are stored in the table
  20. dropbox_file while the categories for the received files are stored in dropbox_post.
  21. It would have been more elegant if these could be stored in dropbox_person (which stores the link file-person)
  22. Therefore we have to create to separate sql statements to find which files are in the category
  23. (depending if we zip-download a sent category or a received category)*/
  24. if ($_GET['sent_received'] == 'sent') {
  25. // here we also incorporate the person table to make sure that deleted sent documents are not included.
  26. $sql = "SELECT DISTINCT file.id, file.filename, file.title
  27. FROM ". Database::get_course_table(TABLE_DROPBOX_FILE)." file
  28. INNER JOIN ". Database::get_course_table(TABLE_DROPBOX_PERSON)." person
  29. ON (person.file_id=file.id AND file.c_id = $course_id AND person.c_id = $course_id)
  30. WHERE
  31. file.uploader_id = $user_id AND
  32. file.cat_id='".intval($_GET['cat_id'])."' AND
  33. person.user_id = $user_id";
  34. }
  35. if ($_GET['sent_received'] == 'received') {
  36. $sql = "SELECT DISTINCT file.id, file.filename, file.title
  37. FROM ". Database::get_course_table(TABLE_DROPBOX_FILE)." file
  38. INNER JOIN ". Database::get_course_table(TABLE_DROPBOX_PERSON)." person
  39. ON (person.file_id=file.id AND file.c_id = $course_id AND person.c_id = $course_id)
  40. INNER JOIN ".Database::get_course_table(TABLE_DROPBOX_POST)." post
  41. ON (post.file_id = file.id AND post.c_id = $course_id AND file.c_id = $course_id)
  42. WHERE
  43. post.cat_id = ".intval($_GET['cat_id'])." AND
  44. post.dest_user_id = $user_id";
  45. }
  46. $files_to_download = array();
  47. $result = Database::query($sql);
  48. while ($row = Database::fetch_array($result)) {
  49. $files_to_download[] = $row['id'];
  50. }
  51. if (!is_array($files_to_download) || empty($files_to_download)) {
  52. header('Location: index.php?'.api_get_cidreq().'&view='.Security::remove_XSS($_GET['sent_received']).'&error=ErrorNoFilesInFolder');
  53. exit;
  54. }
  55. zip_download($files_to_download);
  56. exit;
  57. }
  58. /* DOWNLOAD A FILE */
  59. /* AUTHORIZATION */
  60. // Check if the id makes sense
  61. if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {
  62. api_not_allowed(true);
  63. exit;
  64. }
  65. // Check if the user is allowed to download the file
  66. $allowed_to_download = false;
  67. if (user_can_download_file($_GET['id'], api_get_user_id())) {
  68. $allowed_to_download = true;
  69. }
  70. /* ERROR IF NOT ALLOWED TO DOWNLOAD */
  71. if (!$allowed_to_download) {
  72. api_not_allowed(
  73. true,
  74. Display::return_message(
  75. get_lang('YouAreNotAllowedToDownloadThisFile'),
  76. 'error'
  77. )
  78. );
  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 to file as stored on server
  86. $path = api_get_path(SYS_COURSE_PATH).$_course['path'].'/dropbox/'.$work->filename;
  87. if (!Security::check_abs_path(
  88. $path,
  89. api_get_path(SYS_COURSE_PATH).$_course['path'].'/dropbox/')
  90. ) {
  91. api_not_allowed(true);
  92. }
  93. $file = $work->title;
  94. $result = DocumentManager::file_send_for_download($path, true, $file);
  95. if ($result === false) {
  96. api_not_allowed(true);
  97. }
  98. exit;
  99. }
  100. //@todo clean this file the code below is useless there are 2 exits in previous conditions ... maybe a bad copy/paste/merge?
  101. exit;