download.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. api_block_anonymous_users();
  14. // IMPORTANT to avoid caching of documents
  15. header('Expires: Wed, 01 Jan 1990 00:00:00 GMT');
  16. header('Cache-Control: public');
  17. header('Pragma: no-cache');
  18. $messageId = isset($_GET['message_id']) ? $_GET['message_id'] : 0;
  19. $attachmentId = isset($_GET['attachment_id']) ? $_GET['attachment_id'] : 0;
  20. $messageInfo = MessageManager::get_message_by_id($messageId);
  21. $attachmentInfo = MessageManager::getAttachment($attachmentId);
  22. if (empty($messageInfo) || empty($attachmentInfo)) {
  23. api_not_allowed();
  24. }
  25. // Attachment belongs to the message?
  26. if ($messageInfo['id'] != $attachmentInfo['message_id']) {
  27. api_not_allowed();
  28. }
  29. // Do not process group items
  30. if (!empty($messageInfo['group_id'])) {
  31. api_not_allowed();
  32. }
  33. // Only process wall messages
  34. if (!in_array($messageInfo['msg_status'], [MESSAGE_STATUS_WALL, MESSAGE_STATUS_WALL_POST, MESSAGE_STATUS_PROMOTED])) {
  35. api_not_allowed();
  36. }
  37. $dir = UserManager::getUserPathById($messageInfo['user_sender_id'], 'system');
  38. if (empty($dir)) {
  39. api_not_allowed();
  40. }
  41. $file = $dir.'message_attachments/'.$attachmentInfo['path'];
  42. $title = api_replace_dangerous_char($attachmentInfo['filename']);
  43. if (Security::check_abs_path($file, $dir.'message_attachments/')) {
  44. // launch event
  45. Event::event_download($file);
  46. $result = DocumentManager::file_send_for_download(
  47. $file,
  48. false,
  49. $title
  50. );
  51. if ($result === false) {
  52. api_not_allowed(true);
  53. }
  54. }
  55. exit;