]> git.mxchange.org Git - friendica.git/blob - src/Module/Attach.php
Merge pull request #8227 from annando/daemon-checks
[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\Logger;
11 use Friendica\DI;
12 use Friendica\Model\Attach as MAttach;
13
14 /**
15  * Attach Module
16  */
17 class Attach extends BaseModule
18 {
19         /**
20          * Return to user an attached file given the id
21          */
22         public static function rawContent(array $parameters = [])
23         {
24                 $a = DI::app();
25                 if ($a->argc != 2) {
26                         throw new \Friendica\Network\HTTPException\BadRequestException();
27                 }
28
29                 // @TODO: Replace with parameter from router
30                 $item_id = intval($a->argv[1]);
31                 
32                 // Check for existence
33                 $item = MAttach::exists(['id' => $item_id]);
34                 if ($item === false) {
35                         throw new \Friendica\Network\HTTPException\NotFoundException(DI::l10n()->t('Item was not found.'));
36                 }
37
38                 // Now we'll fetch the item, if we have enough permisson
39                 $item = MAttach::getByIdWithPermission($item_id);
40                 if ($item === false) {
41                         throw new \Friendica\Network\HTTPException\ForbiddenException(DI::l10n()->t('Permission denied.'));
42                 }
43
44                 $data = MAttach::getData($item);
45                 if (is_null($data)) {
46                         Logger::log('NULL data for attachment with id ' . $item['id']);
47                         throw new \Friendica\Network\HTTPException\NotFoundException(DI::l10n()->t('Item was not found.'));
48                 }
49
50                 // Use quotes around the filename to prevent a "multiple Content-Disposition"
51                 // error in Chrome for filenames with commas in them
52                 header('Content-type: ' . $item['filetype']);
53                 header('Content-length: ' . $item['filesize']);
54                 if (isset($_GET['attachment']) && $_GET['attachment'] === '0') {
55                         header('Content-disposition: filename="' . $item['filename'] . '"');
56                 } else {
57                         header('Content-disposition: attachment; filename="' . $item['filename'] . '"');
58                 }
59
60                 echo $data;
61                 exit();
62                 // NOTREACHED
63         }
64 }