download.php 3.1 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. /* MAIN CODE */
  12. session_cache_limiter('public');
  13. require_once '../inc/global.inc.php';
  14. require_once api_get_path(LIBRARY_PATH).'usermanager.lib.php';
  15. require_once api_get_path(LIBRARY_PATH).'group_portal_manager.lib.php';
  16. require_once api_get_path(LIBRARY_PATH).'document.lib.php';
  17. // IMPORTANT to avoid caching of documents
  18. header('Expires: Wed, 01 Jan 1990 00:00:00 GMT');
  19. header('Cache-Control: public');
  20. header('Pragma: no-cache');
  21. $file_url = $_GET['file'];
  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 FROM $tbl_messsage_attachment WHERE path LIKE BINARY '$file_url'";
  31. $result = Database::query($sql);
  32. $row = Database::fetch_array($result, 'ASSOC');
  33. $title = str_replace(' ','_', $row['filename']);
  34. $message_id = $row['message_id'];
  35. // allow download only for user sender and user receiver
  36. $sql = "SELECT user_sender_id, user_receiver_id, group_id FROM $tbl_messsage WHERE id = '$message_id'";
  37. $rs = Database::query($sql);
  38. $row_users = Database::fetch_array($rs, 'ASSOC');
  39. $current_uid = api_get_user_id();
  40. // get message user id for inbox/outbox
  41. $message_uid = '';
  42. $message_type = array('inbox','outbox');
  43. if (in_array($_GET['type'],$message_type)) {
  44. if ($_GET['type'] == 'inbox') {
  45. $message_uid = $row_users['user_receiver_id'];
  46. } else {
  47. $message_uid = $row_users['user_sender_id'];
  48. }
  49. }
  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 = GroupPortalManager::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 = GroupPortalManager::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;