2019-01-02 15:13:05 +01:00
|
|
|
<?php
|
|
|
|
/**
|
2022-01-02 08:27:47 +01:00
|
|
|
* @copyright Copyright (C) 2010-2022, 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;
|
2022-05-18 04:13:54 +02:00
|
|
|
use Friendica\Core\System;
|
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
|
|
|
*/
|
2021-11-20 15:38:03 +01:00
|
|
|
protected function rawContent(array $request = [])
|
2019-01-02 15:13:05 +01:00
|
|
|
{
|
2019-12-15 22:34:11 +01:00
|
|
|
$a = DI::app();
|
2021-11-14 23:19:25 +01:00
|
|
|
if (empty($this->parameters['item'])) {
|
2019-05-02 05:16:10 +02:00
|
|
|
throw new \Friendica\Network\HTTPException\BadRequestException();
|
2019-01-02 15:13:05 +01:00
|
|
|
}
|
|
|
|
|
2021-11-14 23:19:25 +01:00
|
|
|
$item_id = intval($this->parameters['item']);
|
2021-05-22 23:45:15 +02:00
|
|
|
|
2019-01-02 15:13:05 +01:00
|
|
|
// 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)) {
|
2021-10-20 20:53:52 +02:00
|
|
|
Logger::notice('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;
|
2022-05-18 04:13:54 +02:00
|
|
|
System::exit();
|
2019-01-02 15:13:05 +01:00
|
|
|
// NOTREACHED
|
|
|
|
}
|
2019-01-04 07:45:08 +01:00
|
|
|
}
|