download.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This file is responsible for passing requested file attachments from messages
  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.messages
  10. */
  11. session_cache_limiter('public');
  12. require_once __DIR__.'/../inc/global.inc.php';
  13. $file_url = isset($_GET['file']) ? $_GET['file'] : '';
  14. $type = isset($_GET['type']) ? $_GET['type'] : '';
  15. if (empty($file_url)) {
  16. api_not_allowed();
  17. }
  18. // IMPORTANT to avoid caching of documents
  19. header('Expires: Wed, 01 Jan 1990 00:00:00 GMT');
  20. header('Cache-Control: public');
  21. header('Pragma: no-cache');
  22. //change the '&' that got rewritten to '///' by mod_rewrite back to '&'
  23. $file_url = str_replace('///', '&', $file_url);
  24. //still a space present? it must be a '+' (that got replaced by mod_rewrite)
  25. $file_url = str_replace(' ', '+', $file_url);
  26. $file_url = str_replace('/..', '', $file_url); //echo $doc_url;
  27. $tbl_messsage = Database::get_main_table(TABLE_MESSAGE);
  28. $tbl_messsage_attachment = Database::get_main_table(TABLE_MESSAGE_ATTACHMENT);
  29. $file_url = Database::escape_string($file_url);
  30. $sql = "SELECT filename, message_id
  31. FROM $tbl_messsage_attachment
  32. WHERE path LIKE BINARY '$file_url'";
  33. $result = Database::query($sql);
  34. $row = Database::fetch_array($result, 'ASSOC');
  35. $title = str_replace(' ', '_', $row['filename']);
  36. $message_id = $row['message_id'];
  37. // allow download only for user sender and user receiver
  38. $sql = "SELECT user_sender_id, user_receiver_id, group_id
  39. FROM $tbl_messsage WHERE id = '$message_id'";
  40. $rs = Database::query($sql);
  41. $row_users = Database::fetch_array($rs, 'ASSOC');
  42. $current_uid = api_get_user_id();
  43. // get message user id for inbox/outbox
  44. $message_uid = '';
  45. switch ($type) {
  46. case MessageManager::MESSAGE_TYPE_INBOX:
  47. $message_uid = $row_users['user_receiver_id'];
  48. break;
  49. case MessageManager::MESSAGE_TYPE_OUTBOX:
  50. $message_uid = $row_users['user_sender_id'];
  51. break;
  52. }
  53. // allow to the correct user for download this file
  54. $not_allowed_to_edit = false;
  55. $userGroup = new UserGroup();
  56. if (!empty($row_users['group_id'])) {
  57. $users_group = $userGroup->get_all_users_by_group($row_users['group_id']);
  58. if (!in_array($current_uid, array_keys($users_group))) {
  59. $not_allowed_to_edit = true;
  60. }
  61. } else {
  62. if ($current_uid != $message_uid) {
  63. $not_allowed_to_edit = true;
  64. }
  65. }
  66. if ($not_allowed_to_edit) {
  67. api_not_allowed(true);
  68. exit;
  69. }
  70. // set the path directory file
  71. if (!empty($row_users['group_id'])) {
  72. $path_user_info = $userGroup->get_group_picture_path_by_id(
  73. $row_users['group_id'],
  74. 'system',
  75. true
  76. );
  77. } else {
  78. $path_user_info['dir'] = UserManager::getUserPathById($message_uid, 'system');
  79. }
  80. $full_file_name = $path_user_info['dir'].'message_attachments/'.$file_url;
  81. if (Security::check_abs_path($full_file_name, $path_user_info['dir'].'message_attachments/')) {
  82. // launch event
  83. Event::event_download($file_url);
  84. $result = DocumentManager::file_send_for_download(
  85. $full_file_name,
  86. true,
  87. $title
  88. );
  89. if ($result === false) {
  90. api_not_allowed(true);
  91. }
  92. }
  93. exit;