3 * @file src/Module/Attach.php
7 namespace Friendica\Module;
9 use Friendica\BaseModule;
10 use Friendica\Core\L10n;
11 use Friendica\Core\System;
12 use Friendica\Core\Logger;
13 use Friendica\Model\Attach as MAttach;
16 * @brief Attach Module
18 class Attach extends BaseModule
21 * @brief Module initializer
23 * Fetch an attached file given the id
25 public static function init()
29 System::httpExit(400); // Bad Request.
33 $item_id = intval($a->argv[1]);
35 // Check for existence
36 $item = MAttach::exists(['id' => $item_id]);
37 if ($item === false) {
38 System::httpExit(404, ['description' => L10n::t('Item was not found.')]);
41 // Now we'll fetch the item, if we have enough permisson
42 $item = MAttach::getByIdWithPermission($item_id);
43 if ($item === false) {
44 System::httpExit(403, ['description' => L10n::t('Permission denied.')]);
47 $data = MAttach::getData($item);
49 Logger::log('NULL data for attachment with id ' . $item['id']);
50 System::httpExit(404, ['description' => L10n::t('Item was not found.')]);
53 // Use quotes around the filename to prevent a "multiple Content-Disposition"
54 // error in Chrome for filenames with commas in them
55 header('Content-type: ' . $item['filetype']);
56 header('Content-length: ' . $item['filesize']);
57 if (isset($_GET['attachment']) && $_GET['attachment'] === '0') {
58 header('Content-disposition: filename="' . $item['filename'] . '"');
60 header('Content-disposition: attachment; filename="' . $item['filename'] . '"');