download.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. /**
  12. * MAIN CODE
  13. */
  14. session_cache_limiter('public');
  15. require_once '../inc/global.inc.php';
  16. // IMPORTANT to avoid caching of documents
  17. header('Expires: Wed, 01 Jan 1990 00:00:00 GMT');
  18. header('Cache-Control: public');
  19. header('Pragma: no-cache');
  20. $file_url = $_GET['file'];
  21. //change the '&' that got rewritten to '///' by mod_rewrite back to '&'
  22. $file_url = str_replace('///', '&', $file_url);
  23. //still a space present? it must be a '+' (that got replaced by mod_rewrite)
  24. $file_url = str_replace(' ', '+', $file_url);
  25. $file_url = str_replace('/..', '', $file_url); //echo $doc_url;
  26. $tbl_messsage = Database::get_main_table(TABLE_MESSAGE);
  27. $tbl_messsage_attachment = Database::get_main_table(TABLE_MESSAGE_ATTACHMENT);
  28. $file_url = Database::escape_string($file_url);
  29. $sql= "SELECT filename, message_id FROM $tbl_messsage_attachment WHERE path LIKE BINARY '$file_url'";
  30. $result = Database::query($sql);
  31. $row = Database::fetch_array($result, 'ASSOC');
  32. $title = str_replace(' ','_', $row['filename']);
  33. $message_id = $row['message_id'];
  34. // allow download only for user sender and user receiver
  35. $sql = "SELECT user_sender_id, user_receiver_id, group_id 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. $usergroup = new UserGroup();
  50. // allow to the correct user for download this file
  51. $not_allowed_to_edit = false;
  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($row_users['group_id'], 'system', true);
  69. } else {
  70. $path_user_info = UserManager::get_user_picture_path_by_id($message_uid, 'system', true);
  71. }
  72. $full_file_name = $path_user_info['dir'].'message_attachments/'.$file_url;
  73. if (Security::check_abs_path($full_file_name, $path_user_info['dir'].'message_attachments/')) {
  74. // launch event
  75. event_download($file_url);
  76. DocumentManager::file_send_for_download($full_file_name,TRUE, $title);
  77. }
  78. exit;