download.php 2.9 KB

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