3 namespace Friendica\Util;
5 use Friendica\BaseModule;
6 use Friendica\BaseObject;
7 use Friendica\Core\Config;
8 use Friendica\Core\System;
9 use Friendica\Util\Strings;
12 * @brief Proxy utilities class
18 * Default time to keep images in proxy storage
20 const DEFAULT_TIME = 86400; // 1 Day
25 const SIZE_MICRO = 'micro';
26 const SIZE_THUMB = 'thumb';
27 const SIZE_SMALL = 'small';
28 const SIZE_MEDIUM = 'medium';
29 const SIZE_LARGE = 'large';
35 * @todo Make this configurable?
37 private static $extensions = [
45 * @brief Private constructor
47 private function __construct () {
48 // No instances from utilities classes
52 * @brief Transform a remote URL into a local one.
54 * This function only performs the URL replacement on http URL and if the
55 * provided URL isn't local, "the isn't deactivated" (sic) and if the config
56 * system.proxy_disabled is set to false.
58 * @param string $url The URL to proxyfy
59 * @param bool $writemode Returns a local path the remote URL should be saved to
60 * @param string $size One of the ProxyUtils::SIZE_* constants
62 * @return string The proxyfied URL or relative path
63 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
65 public static function proxifyUrl($url, $writemode = false, $size = '')
67 // Get application instance
68 $a = BaseObject::getApp();
73 // Is no http in front of it?
74 /// @TODO To weak test for being a valid URL
75 if (substr($url, 0, 4) !== 'http') {
79 // Only continue if it isn't a local image and the isn't deactivated
80 if (self::isLocalImage($url)) {
81 $url = str_replace(Strings::normaliseLink(System::baseUrl()) . '/', System::baseUrl() . '/', $url);
85 // Is the proxy disabled?
86 if (Config::get('system', 'proxy_disabled')) {
90 // Image URL may have encoded ampersands for display which aren't desirable for proxy
91 $url = html_entity_decode($url, ENT_NOQUOTES, 'utf-8');
93 // Creating a sub directory to reduce the amount of files in the cache directory
94 $basepath = $a->getBasePath() . '/proxy';
96 $shortpath = hash('md5', $url);
97 $longpath = substr($shortpath, 0, 2);
99 if (is_dir($basepath) && $writemode && !is_dir($basepath . '/' . $longpath)) {
100 mkdir($basepath . '/' . $longpath);
101 chmod($basepath . '/' . $longpath, 0777);
104 $longpath .= '/' . strtr(base64_encode($url), '+/', '-_');
106 // Extract the URL extension
107 $extension = pathinfo(parse_url($url, PHP_URL_PATH), PATHINFO_EXTENSION);
109 if (in_array($extension, self::$extensions)) {
110 $shortpath .= '.' . $extension;
111 $longpath .= '.' . $extension;
114 $proxypath = System::baseUrl() . '/proxy/' . $longpath;
120 // Too long files aren't supported by Apache
121 // Writemode in combination with long files shouldn't be possible
122 if ((strlen($proxypath) > 250) && $writemode) {
124 } elseif (strlen($proxypath) > 250) {
125 return System::baseUrl() . '/proxy/' . $shortpath . '?url=' . urlencode($url);
126 } elseif ($writemode) {
129 return $proxypath . $size;
134 * @brief "Proxifies" HTML code's image tags
136 * "Proxifies", means replaces image URLs in given HTML code with those from
137 * proxy storage directory.
139 * @param string $html Un-proxified HTML code
141 * @return string Proxified HTML code
142 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
144 public static function proxifyHtml($html)
146 $html = str_replace(Strings::normaliseLink(System::baseUrl()) . '/', System::baseUrl() . '/', $html);
148 return preg_replace_callback('/(<img [^>]*src *= *["\'])([^"\']+)(["\'][^>]*>)/siU', 'self::replaceUrl', $html);
152 * @brief Checks if the URL is a local URL.
156 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
158 private static function isLocalImage($url)
160 if (substr($url, 0, 1) == '/') {
164 if (strtolower(substr($url, 0, 5)) == 'data:') {
168 // links normalised - bug #431
169 $baseurl = Strings::normaliseLink(System::baseUrl());
170 $url = Strings::normaliseLink($url);
172 return (substr($url, 0, strlen($baseurl)) == $baseurl);
176 * @brief Return the array of query string parameters from a URL
178 * @param string $url URL to parse
179 * @return array Associative array of query string parameters
181 private static function parseQuery($url)
183 $query = parse_url($url, PHP_URL_QUERY);
184 $query = html_entity_decode($query);
186 parse_str($query, $arr);
192 * @brief Call-back method to replace the UR
194 * @param array $matches Matches from preg_replace_callback()
195 * @return string Proxified HTML image tag
196 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
198 private static function replaceUrl(array $matches)
200 // if the picture seems to be from another picture cache then take the original source
201 $queryvar = self::parseQuery($matches[2]);
203 if (!empty($queryvar['url']) && substr($queryvar['url'], 0, 4) == 'http') {
204 $matches[2] = urldecode($queryvar['url']);
207 // Following line changed per bug #431
208 if (self::isLocalImage($matches[2])) {
209 return $matches[1] . $matches[2] . $matches[3];
212 // Return proxified HTML
213 return $matches[1] . self::proxifyUrl(htmlspecialchars_decode($matches[2])) . $matches[3];