3 * @copyright Copyright (C) 2010-2022, 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\Util;
24 use Friendica\Core\Logger;
25 use Friendica\Core\System;
29 * Proxy utilities class
36 const SIZE_MICRO = 'micro'; // 48
37 const SIZE_THUMB = 'thumb'; // 80
38 const SIZE_SMALL = 'small'; // 300
39 const SIZE_MEDIUM = 'medium'; // 600
40 const SIZE_LARGE = 'large'; // 1024
45 const PIXEL_MICRO = 48;
46 const PIXEL_THUMB = 80;
47 const PIXEL_SMALL = 300;
48 const PIXEL_MEDIUM = 600;
49 const PIXEL_LARGE = 1024;
55 * @todo Make this configurable?
57 private static $extensions = [
67 private function __construct () {
68 // No instances from utilities classes
72 * Transform a remote URL into a local one.
74 * This function only performs the URL replacement on http URL and if the
75 * provided URL isn't local
77 * @param string $url The URL to proxyfy
78 * @param string $size One of the Proxy::SIZE_* constants
80 * @return string The proxyfied URL or relative path
81 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
83 public static function proxifyUrl($url, $size = '')
85 if (!DI::config()->get('system', 'proxify_content')) {
92 // Quit if not an HTTP/HTTPS link or if local
93 if (!in_array(parse_url($url, PHP_URL_SCHEME), ['http', 'https']) || self::isLocalImage($url)) {
97 // Image URL may have encoded ampersands for display which aren't desirable for proxy
98 $url = html_entity_decode($url, ENT_NOQUOTES, 'utf-8');
100 $shortpath = hash('md5', $url);
101 $longpath = substr($shortpath, 0, 2);
103 $longpath .= '/' . strtr(base64_encode($url), '+/', '-_');
105 // Extract the URL extension
106 $extension = pathinfo(parse_url($url, PHP_URL_PATH), PATHINFO_EXTENSION);
108 if (in_array($extension, self::$extensions)) {
109 $shortpath .= '.' . $extension;
110 $longpath .= '.' . $extension;
113 $proxypath = DI::baseUrl() . '/proxy/' . $longpath;
119 Logger::info('Created proxy link', ['url' => $url, 'callstack' => System::callstack(20)]);
121 // Too long files aren't supported by Apache
122 if (strlen($proxypath) > 250) {
123 return DI::baseUrl() . '/proxy/' . $shortpath . '?url=' . urlencode($url);
125 return $proxypath . $size;
130 * "Proxifies" HTML code's image tags
132 * "Proxifies", means replaces image URLs in given HTML code with those from
133 * proxy storage directory.
135 * @param string $html Un-proxified HTML code
137 * @return string Proxified HTML code
138 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
140 public static function proxifyHtml($html)
142 $html = str_replace(Strings::normaliseLink(DI::baseUrl()) . '/', DI::baseUrl() . '/', $html);
144 return preg_replace_callback('/(<img [^>]*src *= *["\'])([^"\']+)(["\'][^>]*>)/siU', 'self::replaceUrl', $html);
148 * Checks if the URL is a local URL.
152 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
154 public static function isLocalImage($url)
156 if (substr($url, 0, 1) == '/') {
160 if (strtolower(substr($url, 0, 5)) == 'data:') {
164 return Network::isLocalLink($url);
168 * Return the array of query string parameters from a URL
170 * @param string $url URL to parse
171 * @return array Associative array of query string parameters
173 private static function parseQuery($url)
175 $query = parse_url($url, PHP_URL_QUERY);
176 $query = html_entity_decode($query);
178 parse_str($query, $arr);
184 * Call-back method to replace the UR
186 * @param array $matches Matches from preg_replace_callback()
187 * @return string Proxified HTML image tag
188 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
190 private static function replaceUrl(array $matches)
192 // if the picture seems to be from another picture cache then take the original source
193 $queryvar = self::parseQuery($matches[2]);
195 if (!empty($queryvar['url']) && substr($queryvar['url'], 0, 4) == 'http') {
196 $matches[2] = urldecode($queryvar['url']);
199 // Following line changed per bug #431
200 if (self::isLocalImage($matches[2])) {
201 return $matches[1] . $matches[2] . $matches[3];
204 // Return proxified HTML
205 return $matches[1] . self::proxifyUrl(htmlspecialchars_decode($matches[2])) . $matches[3];