]> git.mxchange.org Git - friendica.git/blob - src/Module/Attach.php
e9af90facc3fb1b0cc053acb93909e238c962e91
[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\Logger;
12 use Friendica\Core\System;
13 use Friendica\Model\Attach as MAttach;
14
15 /**
16  * @brief Attach Module
17  */
18 class Attach extends BaseModule
19 {
20         /**
21          * @brief Return to user an attached file given the id
22          */
23         public static function rawContent()
24         {
25                 $a = self::getApp();
26                 if ($a->argc != 2) {
27                         throw new \Friendica\Network\HTTPException\BadRequestException();
28                 }
29
30                 // @TODO: Replace with parameter from router
31                 $item_id = intval($a->argv[1]);
32                 
33                 // Check for existence
34                 $item = MAttach::exists(['id' => $item_id]);
35                 if ($item === false) {
36                         throw new \Friendica\Network\HTTPException\NotFoundException(L10n::t('Item was not found.'));
37                 }
38
39                 // Now we'll fetch the item, if we have enough permisson
40                 $item = MAttach::getByIdWithPermission($item_id);
41                 if ($item === false) {
42                         throw new \Friendica\Network\HTTPException\ForbiddenException(L10n::t('Permission denied.'));
43                 }
44
45                 $data = MAttach::getData($item);
46                 if (is_null($data)) {
47                         Logger::log('NULL data for attachment with id ' . $item['id']);
48                         throw new \Friendica\Network\HTTPException\NotFoundException(L10n::t('Item was not found.'));
49                 }
50
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'] . '"');
57                 } else {
58                         header('Content-disposition: attachment; filename="' . $item['filename'] . '"');
59                 }
60
61                 echo $data;
62                 exit();
63                 // NOTREACHED
64         }
65 }