]> git.mxchange.org Git - friendica.git/blob - src/Util/Proxy.php
Merge pull request #8900 from tobiasd/20200718-serverblocklistcsv
[friendica.git] / src / Util / Proxy.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Util;
23
24 use Friendica\DI;
25
26 /**
27  * Proxy utilities class
28  */
29 class Proxy
30 {
31
32         /**
33          * Default time to keep images in proxy storage
34          */
35         const DEFAULT_TIME = 86400; // 1 Day
36
37         /**
38          * Sizes constants
39          */
40         const SIZE_MICRO  = 'micro';
41         const SIZE_THUMB  = 'thumb';
42         const SIZE_SMALL  = 'small';
43         const SIZE_MEDIUM = 'medium';
44         const SIZE_LARGE  = 'large';
45
46         /**
47          * Accepted extensions
48          *
49          * @var array
50          * @todo Make this configurable?
51          */
52         private static $extensions = [
53                 'jpg',
54                 'jpeg',
55                 'gif',
56                 'png',
57         ];
58
59         /**
60          * Private constructor
61          */
62         private function __construct () {
63                 // No instances from utilities classes
64         }
65
66         /**
67          * Transform a remote URL into a local one.
68          *
69          * This function only performs the URL replacement on http URL and if the
70          * provided URL isn't local, "the isn't deactivated" (sic) and if the config
71          * system.proxy_disabled is set to false.
72          *
73          * @param string $url       The URL to proxyfy
74          * @param bool   $writemode Returns a local path the remote URL should be saved to
75          * @param string $size      One of the ProxyUtils::SIZE_* constants
76          *
77          * @return string The proxyfied URL or relative path
78          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
79          */
80         public static function proxifyUrl($url, $writemode = false, $size = '')
81         {
82                 // Get application instance
83                 $a = DI::app();
84
85                 // Trim URL first
86                 $url = trim($url);
87
88                 // Is no http in front of it?
89                 /// @TODO To weak test for being a valid URL
90                 if (substr($url, 0, 4) !== 'http') {
91                         return $url;
92                 }
93
94                 // Only continue if it isn't a local image and the isn't deactivated
95                 if (self::isLocalImage($url)) {
96                         $url = str_replace(Strings::normaliseLink(DI::baseUrl()) . '/', DI::baseUrl() . '/', $url);
97                         return $url;
98                 }
99
100                 // Is the proxy disabled?
101                 if (DI::config()->get('system', 'proxy_disabled')) {
102                         return $url;
103                 }
104
105                 // Image URL may have encoded ampersands for display which aren't desirable for proxy
106                 $url = html_entity_decode($url, ENT_NOQUOTES, 'utf-8');
107
108                 // Creating a sub directory to reduce the amount of files in the cache directory
109                 $basepath = $a->getBasePath() . '/proxy';
110
111                 $shortpath = hash('md5', $url);
112                 $longpath = substr($shortpath, 0, 2);
113
114                 if (is_dir($basepath) && $writemode && !is_dir($basepath . '/' . $longpath)) {
115                         mkdir($basepath . '/' . $longpath);
116                         chmod($basepath . '/' . $longpath, 0777);
117                 }
118
119                 $longpath .= '/' . strtr(base64_encode($url), '+/', '-_');
120
121                 // Extract the URL extension
122                 $extension = pathinfo(parse_url($url, PHP_URL_PATH), PATHINFO_EXTENSION);
123
124                 if (in_array($extension, self::$extensions)) {
125                         $shortpath .= '.' . $extension;
126                         $longpath .= '.' . $extension;
127                 }
128
129                 $proxypath = DI::baseUrl() . '/proxy/' . $longpath;
130
131                 if ($size != '') {
132                         $size = ':' . $size;
133                 }
134
135                 // Too long files aren't supported by Apache
136                 // Writemode in combination with long files shouldn't be possible
137                 if ((strlen($proxypath) > 250) && $writemode) {
138                         return $shortpath;
139                 } elseif (strlen($proxypath) > 250) {
140                         return DI::baseUrl() . '/proxy/' . $shortpath . '?url=' . urlencode($url);
141                 } elseif ($writemode) {
142                         return $longpath;
143                 } else {
144                         return $proxypath . $size;
145                 }
146         }
147
148         /**
149          * "Proxifies" HTML code's image tags
150          *
151          * "Proxifies", means replaces image URLs in given HTML code with those from
152          * proxy storage directory.
153          *
154          * @param string $html Un-proxified HTML code
155          *
156          * @return string Proxified HTML code
157          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
158          */
159         public static function proxifyHtml($html)
160         {
161                 $html = str_replace(Strings::normaliseLink(DI::baseUrl()) . '/', DI::baseUrl() . '/', $html);
162
163                 return preg_replace_callback('/(<img [^>]*src *= *["\'])([^"\']+)(["\'][^>]*>)/siU', 'self::replaceUrl', $html);
164         }
165
166         /**
167          * Checks if the URL is a local URL.
168          *
169          * @param string $url
170          * @return boolean
171          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
172          */
173         public static function isLocalImage($url)
174         {
175                 if (substr($url, 0, 1) == '/') {
176                         return true;
177                 }
178
179                 if (strtolower(substr($url, 0, 5)) == 'data:') {
180                         return true;
181                 }
182
183                 // links normalised - bug #431
184                 $baseurl = Strings::normaliseLink(DI::baseUrl());
185                 $url = Strings::normaliseLink($url);
186
187                 return (substr($url, 0, strlen($baseurl)) == $baseurl);
188         }
189
190         /**
191          * Return the array of query string parameters from a URL
192          *
193          * @param string $url URL to parse
194          * @return array Associative array of query string parameters
195          */
196         private static function parseQuery($url)
197         {
198                 $query = parse_url($url, PHP_URL_QUERY);
199                 $query = html_entity_decode($query);
200
201                 parse_str($query, $arr);
202
203                 return $arr;
204         }
205
206         /**
207          * Call-back method to replace the UR
208          *
209          * @param array $matches Matches from preg_replace_callback()
210          * @return string Proxified HTML image tag
211          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
212          */
213         private static function replaceUrl(array $matches)
214         {
215                 // if the picture seems to be from another picture cache then take the original source
216                 $queryvar = self::parseQuery($matches[2]);
217
218                 if (!empty($queryvar['url']) && substr($queryvar['url'], 0, 4) == 'http') {
219                         $matches[2] = urldecode($queryvar['url']);
220                 }
221
222                 // Following line changed per bug #431
223                 if (self::isLocalImage($matches[2])) {
224                         return $matches[1] . $matches[2] . $matches[3];
225                 }
226
227                 // Return proxified HTML
228                 return $matches[1] . self::proxifyUrl(htmlspecialchars_decode($matches[2])) . $matches[3];
229         }
230
231 }