2019-01-02 15:13:05 +01:00
|
|
|
<?php
|
|
|
|
/**
|
2021-03-29 08:40:20 +02:00
|
|
|
* @copyright Copyright (C) 2010-2021, the Friendica project
|
2020-02-09 16:18:46 +01: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 15:13:05 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Friendica\Module;
|
|
|
|
|
|
|
|
use Friendica\BaseModule;
|
|
|
|
use Friendica\Core\Logger;
|
2019-12-15 22:34:11 +01:00
|
|
|
use Friendica\DI;
|
2019-01-02 15:13:05 +01:00
|
|
|
use Friendica\Model\Attach as MAttach;
|
|
|
|
|
|
|
|
/**
|
2020-01-19 07:05:23 +01:00
|
|
|
* Attach Module
|
2019-01-02 15:13:05 +01:00
|
|
|
*/
|
|
|
|
class Attach extends BaseModule
|
|
|
|
{
|
|
|
|
/**
|
2020-01-19 07:05:23 +01:00
|
|
|
* Return to user an attached file given the id
|
2019-01-02 15:13:05 +01:00
|
|
|
*/
|
2019-11-05 22:48:54 +01:00
|
|
|
public static function rawContent(array $parameters = [])
|
2019-01-02 15:13:05 +01:00
|
|
|
{
|
2019-12-15 22:34:11 +01:00
|
|
|
$a = DI::app();
|
2019-01-02 15:13:05 +01:00
|
|
|
if ($a->argc != 2) {
|
2019-05-02 05:16:10 +02:00
|
|
|
throw new \Friendica\Network\HTTPException\BadRequestException();
|
2019-01-02 15:13:05 +01:00
|
|
|
}
|
|
|
|
|
2019-05-01 20:16:21 +02:00
|
|
|
// @TODO: Replace with parameter from router
|
2019-01-02 15:13:05 +01:00
|
|
|
$item_id = intval($a->argv[1]);
|
|
|
|
|
|
|
|
// Check for existence
|
|
|
|
$item = MAttach::exists(['id' => $item_id]);
|
|
|
|
if ($item === false) {
|
2020-01-18 20:52:34 +01:00
|
|
|
throw new \Friendica\Network\HTTPException\NotFoundException(DI::l10n()->t('Item was not found.'));
|
2019-01-02 15:13:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now we'll fetch the item, if we have enough permisson
|
|
|
|
$item = MAttach::getByIdWithPermission($item_id);
|
|
|
|
if ($item === false) {
|
2020-01-18 20:52:34 +01:00
|
|
|
throw new \Friendica\Network\HTTPException\ForbiddenException(DI::l10n()->t('Permission denied.'));
|
2019-01-02 15:13:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$data = MAttach::getData($item);
|
|
|
|
if (is_null($data)) {
|
|
|
|
Logger::log('NULL data for attachment with id ' . $item['id']);
|
2020-01-18 20:52:34 +01:00
|
|
|
throw new \Friendica\Network\HTTPException\NotFoundException(DI::l10n()->t('Item was not found.'));
|
2019-01-02 15:13:05 +01: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 07:45:08 +01:00
|
|
|
}
|