]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/attachment_view.php
6a3b7df9f5bd905b36bbf21c43c3e579c0850598
[quix0rs-gnu-social.git] / actions / attachment_view.php
1 <?php
2
3 if (!defined('GNUSOCIAL')) { exit(1); }
4
5 /**
6  * Download notice attachment
7  *
8  * @category Personal
9  * @package  GNUsocial
10  * @author   Mikael Nordfeldth <mmn@hethane.se>
11  * @license  https://www.gnu.org/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
12  * @link     https:/gnu.io/social
13  */
14 class Attachment_viewAction extends AttachmentAction
15 {
16     public function showPage()
17     {
18         // Checks file exists or throws FileNotStoredLocallyException
19         $filepath = $this->attachment->getPath();
20
21         $filename = MediaFile::getDisplayName($this->attachment);
22
23         // Disable errors, to not mess with the file contents (suppress errors in case access to this
24         // function is blocked, like in some shared hosts). Automatically reset at the end of the
25         // script execution, and we don't want to have any more errors until then, so don't reset it
26         @ini_set('display_errors', 0);
27
28         header("Content-Description: File Transfer");
29         header("Content-Type: {$this->attachment->mimetype}");
30         if (in_array(common_get_mime_media($this->attachment->mimetype), ['image', 'video'])) {
31             header("Content-Disposition: inline; filename=\"{$filename}\"");
32         } else {
33             header("Content-Disposition: attachment; filename=\"{$filename}\"");
34         }
35         header('Expires: 0');
36         header('Content-Transfer-Encoding: binary'); // FIXME? Can this be different?
37         $filesize = $this->attachment->size;
38         // 'if available', it says, so ensure we have it
39         if (empty($filesize)) {
40             $filesize = filesize($this->attachment->filename);
41         }
42         header("Content-Length: {$filesize}");
43         // header('Cache-Control: private, no-transform, no-store, must-revalidate');
44
45         $ret = @readfile($filepath);
46
47         if ($ret === false || $ret !== $filesize) {
48             common_log(LOG_ERR, "The lengths of the file as recorded on the DB (or on disk) for the file " .
49                        "{$filepath}, with id={$this->attachment->id} differ from what was sent to the user.");
50         }
51     }
52 }