3 * @copyright Copyright (C) 2010-2021, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Module;
24 use Friendica\BaseModule;
25 use Friendica\Core\Logger;
26 use Friendica\Core\System;
27 use Friendica\Object\Image;
28 use Friendica\Util\HTTPSignature;
29 use Friendica\Util\Images;
30 use Friendica\Util\Proxy as ProxyUtils;
36 * /proxy/[sub1/[sub2/]]<base64url image url>[.ext][:size]
37 * /proxy?url=<image url>
39 class Proxy extends BaseModule
43 * Fetch remote image content
45 public static function rawContent(array $parameters = [])
47 if (isset($_SERVER["HTTP_IF_MODIFIED_SINCE"])) {
48 header("HTTP/1.1 304 Not Modified");
49 header("Last-Modified: " . gmdate("D, d M Y H:i:s", time()) . " GMT");
50 if (!empty($_SERVER["HTTP_IF_NONE_MATCH"])) {
51 header("Etag: " . $_SERVER["HTTP_IF_NONE_MATCH"]);
53 header("Expires: " . gmdate("D, d M Y H:i:s", time() + (31536000)) . " GMT");
54 header("Cache-Control: max-age=31536000");
55 if (function_exists("header_remove")) {
56 header_remove("Last-Modified");
57 header_remove("Expires");
58 header_remove("Cache-Control");
63 $request = self::getRequestInfo($parameters);
65 if (empty($request['url'])) {
66 throw new \Friendica\Network\HTTPException\BadRequestException();
70 Logger::info('Redirecting not logged in user to original address', ['url' => $request['url']]);
71 System::externalRedirect($request['url']);
74 // It shouldn't happen but it does - spaces in URL
75 $request['url'] = str_replace(' ', '+', $request['url']);
77 // Fetch the content with the local user
78 $fetchResult = HTTPSignature::fetchRaw($request['url'], local_user(), ['timeout' => 10]);
79 $img_str = $fetchResult->getBody();
81 if (!$fetchResult->isSuccess() || empty($img_str)) {
82 Logger::info('Error fetching image', ['image' => $request['url'], 'return' => $fetchResult->getReturnCode(), 'empty' => empty($img_str)]);
83 self::responseError();
87 $mime = Images::getMimeTypeByData($img_str);
89 $image = new Image($img_str, $mime);
90 if (!$image->isValid()) {
91 Logger::info('The image is invalid', ['image' => $request['url'], 'mime' => $mime]);
92 self::responseError();
96 // reduce quality - if it isn't a GIF
97 if ($image->getType() != 'image/gif') {
98 $image->scaleDown($request['size']);
101 self::responseImageHttpCache($image);
106 * Build info about requested image to be proxied
110 * 'url' => requested url,
111 * 'size' => requested image size (int)
112 * 'sizetype' => requested image size (string): ':micro', ':thumb', ':small', ':medium', ':large'
116 private static function getRequestInfo(array $parameters)
118 $size = ProxyUtils::PIXEL_LARGE;
121 if (!empty($parameters['url']) && empty($_REQUEST['url'])) {
122 $url = $parameters['url'];
124 // thumb, small, medium and large.
125 if (substr($url, -6) == ':micro') {
126 $size = ProxyUtils::PIXEL_MICRO;
127 $sizetype = ':micro';
128 $url = substr($url, 0, -6);
129 } elseif (substr($url, -6) == ':thumb') {
130 $size = ProxyUtils::PIXEL_THUMB;
131 $sizetype = ':thumb';
132 $url = substr($url, 0, -6);
133 } elseif (substr($url, -6) == ':small') {
134 $size = ProxyUtils::PIXEL_SMALL;
135 $url = substr($url, 0, -6);
136 $sizetype = ':small';
137 } elseif (substr($url, -7) == ':medium') {
138 $size = ProxyUtils::PIXEL_MEDIUM;
139 $url = substr($url, 0, -7);
140 $sizetype = ':medium';
141 } elseif (substr($url, -6) == ':large') {
142 $size = ProxyUtils::PIXEL_LARGE;
143 $url = substr($url, 0, -6);
144 $sizetype = ':large';
147 $pos = strrpos($url, '=.');
149 $url = substr($url, 0, $pos + 1);
152 $url = str_replace(['.jpg', '.jpeg', '.gif', '.png'], ['','','',''], $url);
154 $url = base64_decode(strtr($url, '-_', '+/'), true);
156 $url = $_REQUEST['url'] ?? '';
162 'sizetype' => $sizetype,
167 * In case of an error just stop. We don't return content to avoid caching problems
169 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
171 private static function responseError()
173 throw new \Friendica\Network\HTTPException\InternalServerErrorException();
177 * Output the image with cache headers
180 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
182 private static function responseImageHttpCache(Image $img)
184 if (is_null($img) || !$img->isValid()) {
185 Logger::info('The cached image is invalid');
186 self::responseError();
189 header('Content-type: ' . $img->getType());
190 header('Last-Modified: ' . gmdate('D, d M Y H:i:s', time()) . ' GMT');
191 header('Etag: "' . md5($img->asString()) . '"');
192 header('Expires: ' . gmdate('D, d M Y H:i:s', time() + (31536000)) . ' GMT');
193 header('Cache-Control: max-age=31536000');
194 echo $img->asString();