PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/imp-h3-4.3.10/attachment.php

#
PHP | 145 lines | 101 code | 14 blank | 30 comment | 24 complexity | 2fca19293640b3be6a3558776e0210ce MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. /**
  3. * $Horde: imp/attachment.php,v 2.5.10.23 2010/10/01 07:11:22 slusarz Exp $
  4. *
  5. * Copyright 2004-2007 Andrew Coleman <mercury@appisolutions.net>
  6. *
  7. * See the enclosed file COPYING for license information (GPL). If you
  8. * did not receive this file, see http://www.fsf.org/copyleft/gpl.html.
  9. *
  10. * This file should be the basis for serving hosted attachments. It
  11. * should fetch the file from the VFS and funnel it to the client
  12. * wishing to download the attachment. This will allow for the
  13. * exchange of massive attachments without causing mail server havoc.
  14. */
  15. // Set up initial includes.
  16. // This does *not* include IMP's base.php because we do not need to be
  17. // authenticated to get the file. Most users won't send linked
  18. // attachments just to other IMP users.
  19. if (!defined('HORDE_BASE')) {
  20. @define('HORDE_BASE', dirname(__FILE__) . '/..');
  21. }
  22. @define('IMP_BASE', dirname(__FILE__));
  23. require_once HORDE_BASE . '/lib/core.php';
  24. require_once IMP_BASE . '/lib/Compose.php';
  25. require_once 'Horde/MIME/Magic.php';
  26. require_once 'VFS.php';
  27. $registry = &Registry::singleton();
  28. $registry->importConfig('imp');
  29. $_self_url = Horde::selfUrl(false, true, true);
  30. // Lets see if we are even able to send the user an attachment.
  31. if (!$conf['compose']['link_attachments']) {
  32. Horde::fatal(_("Linked attachments are forbidden."), $_self_url, __LINE__);
  33. }
  34. // Gather required form variables.
  35. $mail_user = Util::getFormData('u');
  36. $time_stamp = Util::getFormData('t');
  37. $file_name = Util::getFormData('f');
  38. if (!isset($mail_user) || !isset($time_stamp) || !isset($file_name) ||
  39. $mail_user == '' || $time_stamp == '' || $file_name == '') {
  40. Horde::fatal(_("The attachment was not found."),
  41. $_self_url, __LINE__);
  42. }
  43. // Initialize the VFS.
  44. $vfsroot = &VFS::singleton($conf['vfs']['type'], Horde::getDriverConfig('vfs', $conf['vfs']['type']));
  45. if (is_a($vfsroot, 'PEAR_Error')) {
  46. Horde::fatal(sprintf(_("Could not create the VFS backend: %s"), $vfsroot->getMessage()), $_self_url, __LINE__);
  47. }
  48. // Check if the file exists.
  49. $mail_user = basename($mail_user);
  50. $time_stamp = basename($time_stamp);
  51. $file_name = escapeshellcmd(basename($file_name));
  52. $full_path = sprintf(IMP_VFS_LINK_ATTACH_PATH . '/%s/%d', $mail_user, $time_stamp);
  53. if (!$vfsroot->exists($full_path, $file_name)) {
  54. Horde::fatal(_("The specified attachment does not exist. It may have been deleted by the original sender."), $_self_url, __LINE__);
  55. }
  56. // Check to see if we need to send a verification message.
  57. if ($conf['compose']['link_attachments_notify']) {
  58. if ($vfsroot->exists($full_path, $file_name . '.notify')) {
  59. $delete_id = Util::getFormData('d');
  60. $read_id = $vfsroot->read($full_path, $file_name . '.notify');
  61. if (is_a($read_id, 'PEAR_Error')) {
  62. Horde::logMessage($read_id, __FILE__, __LINE__, PEAR_LOG_ERR);
  63. } elseif ($delete_id == $read_id) {
  64. $vfsroot->deleteFile($full_path, $file_name);
  65. $vfsroot->deleteFile($full_path, $file_name . '.notify');
  66. printf(_("Attachment %s deleted."), $file_name);
  67. exit;
  68. }
  69. } else {
  70. /* Create a random identifier for this file. */
  71. $id = base_convert($file_name . mt_rand(), 10, 36);
  72. $res = $vfsroot->writeData($full_path, $file_name . '.notify' , $id, true);
  73. if (is_a($res, 'PEAR_Error')) {
  74. Horde::logMessage($res, __FILE__, __LINE__, PEAR_LOG_ERR);
  75. } else {
  76. /* Load $mail_user's preferences so that we can use their
  77. * locale information for the notification message. */
  78. include_once 'Horde/Prefs.php';
  79. $prefs = &Prefs::singleton($conf['prefs']['driver'],
  80. 'horde', $mail_user);
  81. $prefs->retrieve();
  82. include_once 'Horde/Identity.php';
  83. $mail_identity = &Identity::singleton('none', $mail_user);
  84. $mail_address = $mail_identity->getDefaultFromAddress();
  85. /* Ignore missing addresses, which are returned as <>. */
  86. if (strlen($mail_address) > 2) {
  87. $mail_address_full = $mail_identity->getDefaultFromAddress(true);
  88. NLS::setTimeZone();
  89. NLS::setLang($prefs->getValue('language'));
  90. NLS::setTextdomain('imp', IMP_BASE . '/locale', NLS::getCharset());
  91. String::setDefaultCharset(NLS::getCharset());
  92. /* Set up the mail headers and read the log file. */
  93. include_once 'Horde/MIME/Headers.php';
  94. $msg_headers = new MIME_Headers();
  95. $msg_headers->addReceivedHeader();
  96. $msg_headers->addMessageIdHeader();
  97. $msg_headers->addAgentHeader();
  98. $msg_headers->addHeader('Date', date('r'));
  99. $msg_headers->addHeader('From', $mail_address_full);
  100. $msg_headers->addHeader('To', $mail_address_full);
  101. $msg_headers->addHeader('Subject', _("Notification: Linked attachment downloaded"));
  102. include_once 'Horde/MIME/Message.php';
  103. $msg = new MIME_Message();
  104. $msg->setType('text/plain');
  105. $msg->setCharset(NLS::getCharset());
  106. $msg->setContents(String::wrap(sprintf(_("Your linked attachment has been downloaded by at least one user.\n\nAttachment name: %s\nAttachment date: %s\n\nClick on the following link to permanently delete the attachment:\n%s"), $file_name, date('r', $time_stamp), Util::addParameter(Horde::selfUrl(true, false, true), 'd', $id))));
  107. $msg_headers->addMIMEHeaders($msg);
  108. $msg->send($mail_address, $msg_headers);
  109. }
  110. }
  111. }
  112. }
  113. // Find the file's mime-type.
  114. $file_data = $vfsroot->read($full_path, $file_name);
  115. if (is_a($file_data, 'PEAR_Error')) {
  116. Horde::logMessage($file_data, __FILE__, __LINE__, PEAR_LOG_ERR);
  117. Horde::fatal(_("The specified file cannot be read."), $_self_url, __LINE__);
  118. }
  119. $mime_type = MIME_Magic::analyzeData($file_data, isset($conf['mime']['magic_db']) ? $conf['mime']['magic_db'] : null);
  120. if ($mime_type === false) {
  121. $mime_type = MIME_Magic::filenameToMIME($file_name, false);
  122. }
  123. // Prevent 'jar:' attacks on Firefox. See Ticket #5892.
  124. if ($browser->isBrowser('mozilla')) {
  125. if (in_array(String::lower($mime_type), array('application/java-archive', 'application/x-jar'))) {
  126. $mime_type = 'application/octet-stream';
  127. }
  128. }
  129. // Send the client the file.
  130. $browser->downloadHeaders($file_name, $mime_type, false, strlen($file_data));
  131. echo $file_data;