download.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. require_once api_get_path(LIBRARY_PATH).'group_portal_manager.lib.php';
  17. require_once api_get_path(LIBRARY_PATH).'document.lib.php';
  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. $file_url = $_GET['file'];
  23. //change the '&' that got rewritten to '///' by mod_rewrite back to '&'
  24. $file_url = str_replace('///', '&', $file_url);
  25. //still a space present? it must be a '+' (that got replaced by mod_rewrite)
  26. $file_url = str_replace(' ', '+', $file_url);
  27. $file_url = str_replace('/..', '', $file_url); //echo $doc_url;
  28. $tbl_messsage = Database::get_main_table(TABLE_MESSAGE);
  29. $tbl_messsage_attachment = Database::get_main_table(TABLE_MESSAGE_ATTACHMENT);
  30. $file_url = Database::escape_string($file_url);
  31. $sql= "SELECT filename, message_id FROM $tbl_messsage_attachment WHERE path LIKE BINARY '$file_url'";
  32. $result = Database::query($sql);
  33. $row = Database::fetch_array($result, 'ASSOC');
  34. $title = str_replace(' ','_', $row['filename']);
  35. $message_id = $row['message_id'];
  36. // allow download only for user sender and user receiver
  37. $sql = "SELECT user_sender_id, user_receiver_id, group_id FROM $tbl_messsage WHERE id = '$message_id'";
  38. $rs = Database::query($sql);
  39. $row_users = Database::fetch_array($rs, 'ASSOC');
  40. $current_uid = api_get_user_id();
  41. // get message user id for inbox/outbox
  42. $message_uid = '';
  43. $message_type = array('inbox','outbox');
  44. if (in_array($_GET['type'],$message_type)) {
  45. if ($_GET['type'] == 'inbox') {
  46. $message_uid = $row_users['user_receiver_id'];
  47. } else {
  48. $message_uid = $row_users['user_sender_id'];
  49. }
  50. }
  51. // allow to the correct user for download this file
  52. $not_allowed_to_edit = false;
  53. if (!empty($row_users['group_id'])) {
  54. $users_group = GroupPortalManager::get_all_users_by_group($row_users['group_id']);
  55. if (!in_array($current_uid,array_keys($users_group))) {
  56. $not_allowed_to_edit = true;
  57. }
  58. } else {
  59. if ($current_uid != $message_uid) {
  60. $not_allowed_to_edit = true;
  61. }
  62. }
  63. if ($not_allowed_to_edit) {
  64. api_not_allowed();
  65. exit;
  66. }
  67. // set the path directory file
  68. if (!empty($row_users['group_id'])) {
  69. $path_user_info = GroupPortalManager::get_group_picture_path_by_id($row_users['group_id'], 'system', true);
  70. } else {
  71. $path_user_info = UserManager::get_user_picture_path_by_id($message_uid, 'system', true);
  72. }
  73. $full_file_name = $path_user_info['dir'].'message_attachments/'.$file_url;
  74. if (Security::check_abs_path($full_file_name, $path_user_info['dir'].'message_attachments/')) {
  75. // launch event
  76. event_download($file_url);
  77. DocumentManager::file_send_for_download($full_file_name,TRUE, $title);
  78. }
  79. exit;