2019-01-02 09:13:05 -05:00
|
|
|
<?php
|
|
|
|
/**
|
2022-01-02 02:27:47 -05:00
|
|
|
* @copyright Copyright (C) 2010-2022, the Friendica project
|
2020-02-09 10:18:46 -05: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 09:13:05 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Friendica\Module;
|
|
|
|
|
|
|
|
use Friendica\BaseModule;
|
|
|
|
use Friendica\Core\Logger;
|
2022-05-17 22:13:54 -04:00
|
|
|
use Friendica\Core\System;
|
2019-12-15 16:34:11 -05:00
|
|
|
use Friendica\DI;
|
2019-01-02 09:13:05 -05:00
|
|
|
use Friendica\Model\Attach as MAttach;
|
|
|
|
|
|
|
|
/**
|
2020-01-19 01:05:23 -05:00
|
|
|
* Attach Module
|
2019-01-02 09:13:05 -05:00
|
|
|
*/
|
|
|
|
class Attach extends BaseModule
|
|
|
|
{
|
|
|
|
/**
|
2020-01-19 01:05:23 -05:00
|
|
|
* Return to user an attached file given the id
|
2019-01-02 09:13:05 -05:00
|
|
|
*/
|
2021-11-20 09:38:03 -05:00
|
|
|
protected function rawContent(array $request = [])
|
2019-01-02 09:13:05 -05:00
|
|
|
{
|
2019-12-15 16:34:11 -05:00
|
|
|
$a = DI::app();
|
2021-11-14 17:19:25 -05:00
|
|
|
if (empty($this->parameters['item'])) {
|
2019-05-01 23:16:10 -04:00
|
|
|
throw new \Friendica\Network\HTTPException\BadRequestException();
|
2019-01-02 09:13:05 -05:00
|
|
|
}
|
|
|
|
|
2021-11-14 17:19:25 -05:00
|
|
|
$item_id = intval($this->parameters['item']);
|
2021-05-22 17:45:15 -04:00
|
|
|
|
2019-01-02 09:13:05 -05:00
|
|
|
// Check for existence
|
|
|
|
$item = MAttach::exists(['id' => $item_id]);
|
|
|
|
if ($item === false) {
|
2020-01-18 14:52:34 -05:00
|
|
|
throw new \Friendica\Network\HTTPException\NotFoundException(DI::l10n()->t('Item was not found.'));
|
2019-01-02 09:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now we'll fetch the item, if we have enough permisson
|
|
|
|
$item = MAttach::getByIdWithPermission($item_id);
|
|
|
|
if ($item === false) {
|
2020-01-18 14:52:34 -05:00
|
|
|
throw new \Friendica\Network\HTTPException\ForbiddenException(DI::l10n()->t('Permission denied.'));
|
2019-01-02 09:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
$data = MAttach::getData($item);
|
|
|
|
if (is_null($data)) {
|
2021-10-20 14:53:52 -04:00
|
|
|
Logger::notice('NULL data for attachment with id ' . $item['id']);
|
2020-01-18 14:52:34 -05:00
|
|
|
throw new \Friendica\Network\HTTPException\NotFoundException(DI::l10n()->t('Item was not found.'));
|
2019-01-02 09:13:05 -05: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-17 22:13:54 -04:00
|
|
|
System::exit();
|
2019-01-02 09:13:05 -05:00
|
|
|
// NOTREACHED
|
|
|
|
}
|
2019-01-04 01:45:08 -05:00
|
|
|
}
|