2018-07-30 22:06:22 -04:00
|
|
|
<?php
|
|
|
|
/**
|
2023-01-01 09:36:24 -05:00
|
|
|
* @copyright Copyright (C) 2010-2023, 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/>.
|
|
|
|
*
|
2018-07-30 22:06:22 -04:00
|
|
|
*/
|
2020-02-09 10:18:46 -05:00
|
|
|
|
2018-07-30 22:06:22 -04:00
|
|
|
namespace Friendica\Module;
|
|
|
|
|
|
|
|
use Friendica\BaseModule;
|
2019-10-06 14:48:33 -04:00
|
|
|
use Friendica\Core\Logger;
|
2021-07-04 07:53:49 -04:00
|
|
|
use Friendica\Core\System;
|
2021-09-09 08:40:19 -04:00
|
|
|
use Friendica\DI;
|
2022-04-02 14:26:11 -04:00
|
|
|
use Friendica\Network\HTTPClient\Client\HttpClientAccept;
|
2022-03-29 04:57:38 -04:00
|
|
|
use Friendica\Network\HTTPClient\Client\HttpClientOptions;
|
2021-10-29 19:21:07 -04:00
|
|
|
use Friendica\Network\HTTPException\NotModifiedException;
|
2018-07-30 22:06:22 -04:00
|
|
|
use Friendica\Object\Image;
|
2019-03-18 18:33:20 -04:00
|
|
|
use Friendica\Util\HTTPSignature;
|
2021-07-04 17:24:49 -04:00
|
|
|
use Friendica\Util\Images;
|
2018-07-30 22:06:22 -04:00
|
|
|
use Friendica\Util\Proxy as ProxyUtils;
|
|
|
|
|
|
|
|
/**
|
2020-01-19 01:05:23 -05:00
|
|
|
* Module Proxy
|
2018-12-09 06:33:38 -05:00
|
|
|
*
|
|
|
|
* urls:
|
|
|
|
* /proxy/[sub1/[sub2/]]<base64url image url>[.ext][:size]
|
|
|
|
* /proxy?url=<image url>
|
2018-07-30 22:06:22 -04:00
|
|
|
*/
|
|
|
|
class Proxy extends BaseModule
|
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
2021-07-05 00:16:02 -04:00
|
|
|
* Fetch remote image content
|
2018-07-30 22:06:22 -04:00
|
|
|
*/
|
2021-11-20 09:38:03 -05:00
|
|
|
protected function rawContent(array $request = [])
|
2018-07-30 22:06:22 -04:00
|
|
|
{
|
2021-11-14 17:19:25 -05:00
|
|
|
$request = $this->getRequestInfo();
|
2021-09-09 08:40:19 -04:00
|
|
|
|
|
|
|
if (!DI::config()->get('system', 'proxify_content')) {
|
|
|
|
Logger::notice('Proxy access is forbidden', ['request' => $request, 'agent' => $_SERVER['HTTP_USER_AGENT'] ?? '', 'accept' => $_SERVER['HTTP_ACCEPT'] ?? '']);
|
|
|
|
throw new \Friendica\Network\HTTPException\NotFoundException();
|
|
|
|
}
|
|
|
|
|
2022-06-23 05:39:45 -04:00
|
|
|
if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
|
|
|
|
header('Last-Modified: ' . gmdate('D, d M Y H:i:s', time()) . ' GMT');
|
|
|
|
if (!empty($_SERVER['HTTP_IF_NONE_MATCH'])) {
|
|
|
|
header('Etag: ' . $_SERVER['HTTP_IF_NONE_MATCH']);
|
2018-07-30 22:06:22 -04:00
|
|
|
}
|
2022-06-23 05:39:45 -04:00
|
|
|
header('Expires: ' . gmdate('D, d M Y H:i:s', time() + (31536000)) . ' GMT');
|
|
|
|
header('Cache-Control: max-age=31536000');
|
|
|
|
if (function_exists('header_remove')) {
|
|
|
|
header_remove('Last-Modified');
|
|
|
|
header_remove('Expires');
|
|
|
|
header_remove('Cache-Control');
|
2021-07-04 17:24:49 -04:00
|
|
|
}
|
2021-10-29 19:21:07 -04:00
|
|
|
throw new NotModifiedException();
|
2018-07-30 22:06:22 -04:00
|
|
|
}
|
|
|
|
|
2018-12-09 06:33:38 -05:00
|
|
|
if (empty($request['url'])) {
|
2019-05-01 23:16:10 -04:00
|
|
|
throw new \Friendica\Network\HTTPException\BadRequestException();
|
2018-07-30 22:06:22 -04:00
|
|
|
}
|
|
|
|
|
2022-10-20 16:59:12 -04:00
|
|
|
if (!DI::userSession()->getLocalUserId()) {
|
2022-08-31 01:01:22 -04:00
|
|
|
Logger::debug('Redirecting not logged in user to original address', ['url' => $request['url']]);
|
2021-07-04 07:53:49 -04:00
|
|
|
System::externalRedirect($request['url']);
|
|
|
|
}
|
|
|
|
|
2018-12-09 06:33:38 -05:00
|
|
|
// It shouldn't happen but it does - spaces in URL
|
|
|
|
$request['url'] = str_replace(' ', '+', $request['url']);
|
2021-07-04 17:24:49 -04:00
|
|
|
|
|
|
|
// Fetch the content with the local user
|
2023-02-26 09:08:33 -05:00
|
|
|
try {
|
|
|
|
$fetchResult = HTTPSignature::fetchRaw($request['url'], DI::userSession()->getLocalUserId(), [HttpClientOptions::ACCEPT_CONTENT => [HttpClientAccept::IMAGE], 'timeout' => 10]);
|
|
|
|
$img_str = $fetchResult->getBody();
|
2018-12-09 06:33:38 -05:00
|
|
|
|
2023-02-26 09:08:33 -05:00
|
|
|
if (!$fetchResult->isSuccess() || empty($img_str)) {
|
|
|
|
Logger::notice('Error fetching image', ['image' => $request['url'], 'return' => $fetchResult->getReturnCode(), 'empty' => empty($img_str)]);
|
|
|
|
self::responseError();
|
|
|
|
// stop.
|
|
|
|
}
|
2023-02-26 17:43:45 -05:00
|
|
|
} catch (\Exception $exception) {
|
|
|
|
Logger::notice('Error fetching image', ['image' => $request['url'], 'exception' => $exception]);
|
2019-01-07 12:09:10 -05:00
|
|
|
self::responseError();
|
2018-12-09 06:33:38 -05:00
|
|
|
}
|
|
|
|
|
2022-10-20 16:59:12 -04:00
|
|
|
Logger::debug('Got picture', ['Content-Type' => $fetchResult->getHeader('Content-Type'), 'uid' => DI::userSession()->getLocalUserId(), 'image' => $request['url']]);
|
2022-03-29 02:24:20 -04:00
|
|
|
|
2021-07-04 17:24:49 -04:00
|
|
|
$mime = Images::getMimeTypeByData($img_str);
|
2019-03-18 18:33:20 -04:00
|
|
|
|
2018-12-09 06:33:38 -05:00
|
|
|
$image = new Image($img_str, $mime);
|
|
|
|
if (!$image->isValid()) {
|
2022-08-31 01:01:22 -04:00
|
|
|
Logger::notice('The image is invalid', ['image' => $request['url'], 'mime' => $mime]);
|
2019-01-07 12:09:10 -05:00
|
|
|
self::responseError();
|
2018-12-09 06:33:38 -05:00
|
|
|
// stop.
|
|
|
|
}
|
2019-03-18 18:33:20 -04:00
|
|
|
|
2018-12-09 06:33:38 -05:00
|
|
|
// reduce quality - if it isn't a GIF
|
|
|
|
if ($image->getType() != 'image/gif') {
|
|
|
|
$image->scaleDown($request['size']);
|
|
|
|
}
|
|
|
|
|
|
|
|
self::responseImageHttpCache($image);
|
|
|
|
// stop.
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-19 01:05:23 -05:00
|
|
|
* Build info about requested image to be proxied
|
2018-12-09 06:33:38 -05:00
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
* [
|
|
|
|
* 'url' => requested url,
|
|
|
|
* 'size' => requested image size (int)
|
|
|
|
* 'sizetype' => requested image size (string): ':micro', ':thumb', ':small', ':medium', ':large'
|
|
|
|
* ]
|
2019-01-06 16:06:53 -05:00
|
|
|
* @throws \Exception
|
2018-12-09 06:33:38 -05:00
|
|
|
*/
|
2022-06-23 05:39:45 -04:00
|
|
|
private function getRequestInfo(): array
|
2018-12-09 06:33:38 -05:00
|
|
|
{
|
2021-06-27 07:50:10 -04:00
|
|
|
$size = ProxyUtils::PIXEL_LARGE;
|
2018-12-09 06:33:38 -05:00
|
|
|
$sizetype = '';
|
2019-03-18 18:33:20 -04:00
|
|
|
|
2021-11-14 17:19:25 -05:00
|
|
|
if (!empty($this->parameters['url']) && empty($_REQUEST['url'])) {
|
|
|
|
$url = $this->parameters['url'];
|
2018-07-30 22:06:22 -04:00
|
|
|
|
|
|
|
// thumb, small, medium and large.
|
|
|
|
if (substr($url, -6) == ':micro') {
|
2021-06-27 07:50:10 -04:00
|
|
|
$size = ProxyUtils::PIXEL_MICRO;
|
2018-07-30 22:06:22 -04:00
|
|
|
$sizetype = ':micro';
|
|
|
|
$url = substr($url, 0, -6);
|
|
|
|
} elseif (substr($url, -6) == ':thumb') {
|
2021-06-27 07:50:10 -04:00
|
|
|
$size = ProxyUtils::PIXEL_THUMB;
|
2018-07-30 22:06:22 -04:00
|
|
|
$sizetype = ':thumb';
|
|
|
|
$url = substr($url, 0, -6);
|
|
|
|
} elseif (substr($url, -6) == ':small') {
|
2021-06-27 07:50:10 -04:00
|
|
|
$size = ProxyUtils::PIXEL_SMALL;
|
2018-07-30 22:06:22 -04:00
|
|
|
$url = substr($url, 0, -6);
|
|
|
|
$sizetype = ':small';
|
|
|
|
} elseif (substr($url, -7) == ':medium') {
|
2021-06-27 07:50:10 -04:00
|
|
|
$size = ProxyUtils::PIXEL_MEDIUM;
|
2018-07-30 22:06:22 -04:00
|
|
|
$url = substr($url, 0, -7);
|
|
|
|
$sizetype = ':medium';
|
|
|
|
} elseif (substr($url, -6) == ':large') {
|
2021-06-27 07:50:10 -04:00
|
|
|
$size = ProxyUtils::PIXEL_LARGE;
|
2018-07-30 22:06:22 -04:00
|
|
|
$url = substr($url, 0, -6);
|
|
|
|
$sizetype = ':large';
|
|
|
|
}
|
|
|
|
|
|
|
|
$pos = strrpos($url, '=.');
|
|
|
|
if ($pos) {
|
|
|
|
$url = substr($url, 0, $pos + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
$url = str_replace(['.jpg', '.jpeg', '.gif', '.png'], ['','','',''], $url);
|
|
|
|
|
|
|
|
$url = base64_decode(strtr($url, '-_', '+/'), true);
|
|
|
|
} else {
|
2019-10-15 09:20:32 -04:00
|
|
|
$url = $_REQUEST['url'] ?? '';
|
2018-07-30 22:06:22 -04:00
|
|
|
}
|
2019-03-18 18:33:20 -04:00
|
|
|
|
2018-12-09 06:33:38 -05:00
|
|
|
return [
|
|
|
|
'url' => $url,
|
|
|
|
'size' => $size,
|
|
|
|
'sizetype' => $sizetype,
|
|
|
|
];
|
|
|
|
}
|
2019-01-06 16:06:53 -05:00
|
|
|
|
2018-12-09 06:33:38 -05:00
|
|
|
/**
|
2019-10-01 08:41:49 -04:00
|
|
|
* In case of an error just stop. We don't return content to avoid caching problems
|
2018-12-09 06:33:38 -05:00
|
|
|
*
|
2019-10-01 08:41:49 -04:00
|
|
|
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
|
2018-12-09 06:33:38 -05:00
|
|
|
*/
|
2019-03-18 18:33:20 -04:00
|
|
|
private static function responseError()
|
|
|
|
{
|
2019-10-01 08:41:49 -04:00
|
|
|
throw new \Friendica\Network\HTTPException\InternalServerErrorException();
|
2018-12-09 06:33:38 -05:00
|
|
|
}
|
2019-01-06 16:06:53 -05:00
|
|
|
|
2018-12-09 06:33:38 -05:00
|
|
|
/**
|
2020-01-19 01:05:23 -05:00
|
|
|
* Output the image with cache headers
|
2018-12-09 06:33:38 -05:00
|
|
|
*
|
2019-01-06 16:06:53 -05:00
|
|
|
* @param Image $img
|
2022-06-23 05:39:45 -04:00
|
|
|
* @return void
|
2019-01-06 16:06:53 -05:00
|
|
|
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
|
2018-12-09 06:33:38 -05:00
|
|
|
*/
|
|
|
|
private static function responseImageHttpCache(Image $img)
|
|
|
|
{
|
|
|
|
if (is_null($img) || !$img->isValid()) {
|
2022-08-31 01:01:22 -04:00
|
|
|
Logger::notice('The cached image is invalid');
|
2018-12-09 06:33:38 -05:00
|
|
|
self::responseError();
|
|
|
|
// stop.
|
2018-07-30 22:06:22 -04:00
|
|
|
}
|
2018-12-09 06:33:38 -05:00
|
|
|
header('Content-type: ' . $img->getType());
|
|
|
|
header('Last-Modified: ' . gmdate('D, d M Y H:i:s', time()) . ' GMT');
|
|
|
|
header('Etag: "' . md5($img->asString()) . '"');
|
|
|
|
header('Expires: ' . gmdate('D, d M Y H:i:s', time() + (31536000)) . ' GMT');
|
|
|
|
header('Cache-Control: max-age=31536000');
|
|
|
|
echo $img->asString();
|
2022-05-17 22:13:54 -04:00
|
|
|
System::exit();
|
2018-07-30 22:06:22 -04:00
|
|
|
}
|
|
|
|
}
|