attachment_view.php 1.6 KB

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