]> git.mxchange.org Git - friendica.git/blob - src/Module/Attach.php
Move mod/attach to src, add Attach model, update attach table to use storage backends
[friendica.git] / src / Module / Attach.php
1 <?php
2 /**
3  * @file src/Module/Attach.php
4  */
5
6
7 namespace Friendica\Module;
8
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;
14
15 /**
16  * @brief Attach Module
17  */
18 class Attach extends BaseModule
19 {
20         /**
21          * @brief Module initializer
22          *
23          * Fetch an attached file given the id
24          */
25         public static function init()
26         {
27                 $a = self::getApp();
28                 if ($a->argc != 2) {
29                         System::httpExit(400); // Bad Request.
30                 }
31         
32
33                 $item_id = intval($a->argv[1]);
34                 
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.')]);
39                 }
40
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.')]);
45                 }
46
47                 $data = MAttach::getData($item);
48                 if (is_null($data)) {
49                         Logger::log('NULL data for attachment with id ' . $item['id']);
50                         System::httpExit(404, ['description' => L10n::t('Item was not found.')]);
51                 }
52
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'] . '"');
59                 } else {
60                         header('Content-disposition: attachment; filename="' . $item['filename'] . '"');
61                 }
62
63                 echo $data;
64                 exit();
65                 // NOTREACHED
66         }
67 }