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