Update copyright
[friendica.git/.git] / src / Module / Attach.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Module;
23
24 use Friendica\BaseModule;
25 use Friendica\Core\Logger;
26 use Friendica\DI;
27 use Friendica\Model\Attach as MAttach;
28
29 /**
30  * Attach Module
31  */
32 class Attach extends BaseModule
33 {
34         /**
35          * Return to user an attached file given the id
36          */
37         public static function rawContent(array $parameters = [])
38         {
39                 $a = DI::app();
40                 if ($a->argc != 2) {
41                         throw new \Friendica\Network\HTTPException\BadRequestException();
42                 }
43
44                 // @TODO: Replace with parameter from router
45                 $item_id = intval($a->argv[1]);
46                 
47                 // Check for existence
48                 $item = MAttach::exists(['id' => $item_id]);
49                 if ($item === false) {
50                         throw new \Friendica\Network\HTTPException\NotFoundException(DI::l10n()->t('Item was not found.'));
51                 }
52
53                 // Now we'll fetch the item, if we have enough permisson
54                 $item = MAttach::getByIdWithPermission($item_id);
55                 if ($item === false) {
56                         throw new \Friendica\Network\HTTPException\ForbiddenException(DI::l10n()->t('Permission denied.'));
57                 }
58
59                 $data = MAttach::getData($item);
60                 if (is_null($data)) {
61                         Logger::log('NULL data for attachment with id ' . $item['id']);
62                         throw new \Friendica\Network\HTTPException\NotFoundException(DI::l10n()->t('Item was not found.'));
63                 }
64
65                 // Use quotes around the filename to prevent a "multiple Content-Disposition"
66                 // error in Chrome for filenames with commas in them
67                 header('Content-type: ' . $item['filetype']);
68                 header('Content-length: ' . $item['filesize']);
69                 if (isset($_GET['attachment']) && $_GET['attachment'] === '0') {
70                         header('Content-disposition: filename="' . $item['filename'] . '"');
71                 } else {
72                         header('Content-disposition: attachment; filename="' . $item['filename'] . '"');
73                 }
74
75                 echo $data;
76                 exit();
77                 // NOTREACHED
78         }
79 }