download.php 3.0 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. /* 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. $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. // allow to the correct user for download this file
  50. $not_allowed_to_edit = false;
  51. if (!empty($row_users['group_id'])) {
  52. $users_group = GroupPortalManager::get_all_users_by_group($row_users['group_id']);
  53. if (!in_array($current_uid,array_keys($users_group))) {
  54. $not_allowed_to_edit = true;
  55. }
  56. } else {
  57. if ($current_uid != $message_uid) {
  58. $not_allowed_to_edit = true;
  59. }
  60. }
  61. if ($not_allowed_to_edit) {
  62. api_not_allowed();
  63. exit;
  64. }
  65. // set the path directory file
  66. if (!empty($row_users['group_id'])) {
  67. $path_user_info = GroupPortalManager::get_group_picture_path_by_id($row_users['group_id'], 'system', true);
  68. } else {
  69. $path_user_info = UserManager::get_user_picture_path_by_id($message_uid, 'system', true);
  70. }
  71. $full_file_name = $path_user_info['dir'].'message_attachments/'.$file_url;
  72. if (Security::check_abs_path($full_file_name, $path_user_info['dir'].'message_attachments/')) {
  73. // launch event
  74. event_download($file_url);
  75. DocumentManager::file_send_for_download($full_file_name,TRUE, $title);
  76. }
  77. exit;