download.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. /* For licensing terms, see /dokeos_license.txt */
  3. /**
  4. ==============================================================================
  5. * This file is responsible for passing requested file attachments from messages
  6. * Html files are parsed to fix a few problems with URLs,
  7. * but this code will hopefully be replaced soon by an Apache URL
  8. * rewrite mechanism.
  9. *
  10. * @package dokeos.messages
  11. ==============================================================================
  12. */
  13. /*
  14. ==============================================================================
  15. MAIN CODE
  16. ==============================================================================
  17. */
  18. session_cache_limiter('public');
  19. require_once '../inc/global.inc.php';
  20. require_once api_get_path(LIBRARY_PATH).'usermanager.lib.php';
  21. require_once api_get_path(LIBRARY_PATH).'document.lib.php';
  22. // IMPORTANT to avoid caching of documents
  23. header('Expires: Wed, 01 Jan 1990 00:00:00 GMT');
  24. header('Cache-Control: public');
  25. header('Pragma: no-cache');
  26. $file_url = $_GET['file'];
  27. //change the '&' that got rewritten to '///' by mod_rewrite back to '&'
  28. $file_url = str_replace('///', '&', $file_url);
  29. //still a space present? it must be a '+' (that got replaced by mod_rewrite)
  30. $file_url = str_replace(' ', '+', $file_url);
  31. $file_url = str_replace('/..', '', $file_url); //echo $doc_url;
  32. $tbl_messsage = Database::get_main_table(TABLE_MESSAGE);
  33. $tbl_messsage_attachment = Database::get_main_table(TABLE_MESSAGE_ATTACHMENT);
  34. $sql= "SELECT filename,message_id FROM $tbl_messsage_attachment WHERE path LIKE BINARY '$file_url'";
  35. $result= Database::query($sql, __FILE__, __LINE__);
  36. $row= Database::fetch_array($result);
  37. $title = str_replace(' ','_', $row['filename']);
  38. $message_id = $row['message_id'];
  39. // allow download only for user sender and user receiver
  40. $sql = "SELECT user_sender_id, user_receiver_id FROM $tbl_messsage WHERE id = '$message_id'";
  41. $rs= Database::query($sql, __FILE__, __LINE__);
  42. $row_users= Database::fetch_row($rs);
  43. $current_uid = api_get_user_id();
  44. if (!in_array($current_uid,$row_users)) {
  45. api_not_allowed();
  46. exit;
  47. }
  48. $message_uid = '';
  49. $message_type = array('inbox','outbox');
  50. if (in_array($_GET['type'],$message_type)) {
  51. if ($_GET['type'] == 'inbox') {
  52. $message_uid = $row_users[1];
  53. } else {
  54. $message_uid = $row_users[0];
  55. }
  56. }
  57. $path_user_info = UserManager::get_user_picture_path_by_id($message_uid, 'system', true);
  58. $full_file_name = $path_user_info['dir'].'message_attachments/'.$file_url;
  59. // launch event
  60. event_download($file_url);
  61. DocumentManager::file_send_for_download($full_file_name,TRUE, $title);
  62. exit;
  63. ?>