attachment_download.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <?php
  2. if (!defined('GNUSOCIAL')) { exit(1); }
  3. /**
  4. * Download notice attachment
  5. *
  6. * @category Personal
  7. * @package GNUsocial
  8. * @author Mikael Nordfeldth <mmn@hethane.se>
  9. * @license https://www.gnu.org/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  10. * @link https:/gnu.io/social
  11. */
  12. class Attachment_downloadAction extends AttachmentAction
  13. {
  14. public function showPage()
  15. {
  16. // Checks file exists or throws FileNotFoundException
  17. $filepath = $this->attachment->getFileOrThumbnailPath();
  18. $filesize = $this->attachment->getFileOrThumbnailSize();
  19. $mimetype = $this->attachment->getFileOrThumbnailMimetype();
  20. if (empty($filepath)) {
  21. $this->clientError(_('No such attachment'), 404);
  22. }
  23. $filename = MediaFile::getDisplayName($this->attachment);
  24. // Disable errors, to not mess with the file contents (suppress errors in case access to this
  25. // function is blocked, like in some shared hosts). Automatically reset at the end of the
  26. // script execution, and we don't want to have any more errors until then, so don't reset it
  27. @ini_set('display_errors', 0);
  28. header("Content-Description: File Transfer");
  29. header("Content-Type: {$mimetype}");
  30. header("Content-Disposition: attachment; filename=\"{$filename}\"");
  31. header('Expires: 0');
  32. header('Content-Transfer-Encoding: binary'); // FIXME? Can this be different?
  33. AttachmentAction::sendFile($filepath, $filesize);
  34. }
  35. }