2019-01-02 14:13:05 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2021-03-29 06:40:20 +00:00
|
|
|
* @copyright Copyright (C) 2010-2021, the Friendica project
|
2020-02-09 15:18:46 +00:00
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
2019-01-02 14:13:05 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Friendica\Module;
|
|
|
|
|
|
|
|
use Friendica\BaseModule;
|
|
|
|
use Friendica\Core\Logger;
|
2019-12-15 21:34:11 +00:00
|
|
|
use Friendica\DI;
|
2019-01-02 14:13:05 +00:00
|
|
|
use Friendica\Model\Attach as MAttach;
|
|
|
|
|
|
|
|
/**
|
2020-01-19 06:05:23 +00:00
|
|
|
* Attach Module
|
2019-01-02 14:13:05 +00:00
|
|
|
*/
|
|
|
|
class Attach extends BaseModule
|
|
|
|
{
|
|
|
|
/**
|
2020-01-19 06:05:23 +00:00
|
|
|
* Return to user an attached file given the id
|
2019-01-02 14:13:05 +00:00
|
|
|
*/
|
2019-11-05 21:48:54 +00:00
|
|
|
public static function rawContent(array $parameters = [])
|
2019-01-02 14:13:05 +00:00
|
|
|
{
|
2019-12-15 21:34:11 +00:00
|
|
|
$a = DI::app();
|
2019-01-02 14:13:05 +00:00
|
|
|
if ($a->argc != 2) {
|
2019-05-02 03:16:10 +00:00
|
|
|
throw new \Friendica\Network\HTTPException\BadRequestException();
|
2019-01-02 14:13:05 +00:00
|
|
|
}
|
|
|
|
|
2019-05-01 18:16:21 +00:00
|
|
|
// @TODO: Replace with parameter from router
|
2019-01-02 14:13:05 +00:00
|
|
|
$item_id = intval($a->argv[1]);
|
|
|
|
|
|
|
|
// Check for existence
|
|
|
|
$item = MAttach::exists(['id' => $item_id]);
|
|
|
|
if ($item === false) {
|
2020-01-18 19:52:34 +00:00
|
|
|
throw new \Friendica\Network\HTTPException\NotFoundException(DI::l10n()->t('Item was not found.'));
|
2019-01-02 14:13:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now we'll fetch the item, if we have enough permisson
|
|
|
|
$item = MAttach::getByIdWithPermission($item_id);
|
|
|
|
if ($item === false) {
|
2020-01-18 19:52:34 +00:00
|
|
|
throw new \Friendica\Network\HTTPException\ForbiddenException(DI::l10n()->t('Permission denied.'));
|
2019-01-02 14:13:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$data = MAttach::getData($item);
|
|
|
|
if (is_null($data)) {
|
|
|
|
Logger::log('NULL data for attachment with id ' . $item['id']);
|
2020-01-18 19:52:34 +00:00
|
|
|
throw new \Friendica\Network\HTTPException\NotFoundException(DI::l10n()->t('Item was not found.'));
|
2019-01-02 14:13:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Use quotes around the filename to prevent a "multiple Content-Disposition"
|
|
|
|
// error in Chrome for filenames with commas in them
|
|
|
|
header('Content-type: ' . $item['filetype']);
|
|
|
|
header('Content-length: ' . $item['filesize']);
|
|
|
|
if (isset($_GET['attachment']) && $_GET['attachment'] === '0') {
|
|
|
|
header('Content-disposition: filename="' . $item['filename'] . '"');
|
|
|
|
} else {
|
|
|
|
header('Content-disposition: attachment; filename="' . $item['filename'] . '"');
|
|
|
|
}
|
|
|
|
|
|
|
|
echo $data;
|
|
|
|
exit();
|
|
|
|
// NOTREACHED
|
|
|
|
}
|
2019-01-04 06:45:08 +00:00
|
|
|
}
|