]> git.mxchange.org Git - friendica.git/blob - src/Util/Network.php
Merge pull request #10093 from urbalazs/copyright-2021
[friendica.git] / src / Util / Network.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, the Friendica project
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\Core\Hook;
25 use Friendica\Core\Logger;
26 use Friendica\DI;
27 use Friendica\Model\Contact;
28
29 class Network
30 {
31
32         /**
33          * Return raw post data from a post request
34          *
35          * @return string post data
36          */
37         public static function postdata()
38         {
39                 return file_get_contents('php://input');
40         }
41
42         /**
43          * Check URL to see if it's real
44          *
45          * Take a URL from the wild, prepend http:// if necessary
46          * and check DNS to see if it's real (or check if is a valid IP address)
47          *
48          * @param string $url The URL to be validated
49          * @return string|boolean The actual working URL, false else
50          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
51          */
52         public static function isUrlValid(string $url)
53         {
54                 if (DI::config()->get('system', 'disable_url_validation')) {
55                         return $url;
56                 }
57
58                 // no naked subdomains (allow localhost for tests)
59                 if (strpos($url, '.') === false && strpos($url, '/localhost/') === false) {
60                         return false;
61                 }
62
63                 if (substr($url, 0, 4) != 'http') {
64                         $url = 'http://' . $url;
65                 }
66
67                 /// @TODO Really suppress function outcomes? Why not find them + debug them?
68                 $h = @parse_url($url);
69
70                 if (!empty($h['host']) && (@dns_get_record($h['host'], DNS_A + DNS_CNAME) || filter_var($h['host'], FILTER_VALIDATE_IP))) {
71                         return $url;
72                 }
73
74                 return false;
75         }
76
77         /**
78          * Checks that email is an actual resolvable internet address
79          *
80          * @param string $addr The email address
81          * @return boolean True if it's a valid email address, false if it's not
82          */
83         public static function isEmailDomainValid(string $addr)
84         {
85                 if (DI::config()->get('system', 'disable_email_validation')) {
86                         return true;
87                 }
88
89                 if (! strpos($addr, '@')) {
90                         return false;
91                 }
92
93                 $h = substr($addr, strpos($addr, '@') + 1);
94
95                 // Concerning the @ see here: https://stackoverflow.com/questions/36280957/dns-get-record-a-temporary-server-error-occurred
96                 if ($h && (@dns_get_record($h, DNS_A + DNS_MX) || filter_var($h, FILTER_VALIDATE_IP))) {
97                         return true;
98                 }
99                 if ($h && @dns_get_record($h, DNS_CNAME + DNS_MX)) {
100                         return true;
101                 }
102                 return false;
103         }
104
105         /**
106          * Check if URL is allowed
107          *
108          * Check $url against our list of allowed sites,
109          * wildcards allowed. If allowed_sites is unset return true;
110          *
111          * @param string $url URL which get tested
112          * @return boolean True if url is allowed otherwise return false
113          */
114         public static function isUrlAllowed(string $url)
115         {
116                 $h = @parse_url($url);
117
118                 if (! $h) {
119                         return false;
120                 }
121
122                 $str_allowed = DI::config()->get('system', 'allowed_sites');
123                 if (! $str_allowed) {
124                         return true;
125                 }
126
127                 $found = false;
128
129                 $host = strtolower($h['host']);
130
131                 // always allow our own site
132                 if ($host == strtolower($_SERVER['SERVER_NAME'])) {
133                         return true;
134                 }
135
136                 $fnmatch = function_exists('fnmatch');
137                 $allowed = explode(',', $str_allowed);
138
139                 if (count($allowed)) {
140                         foreach ($allowed as $a) {
141                                 $pat = strtolower(trim($a));
142                                 if (($fnmatch && fnmatch($pat, $host)) || ($pat == $host)) {
143                                         $found = true;
144                                         break;
145                                 }
146                         }
147                 }
148                 return $found;
149         }
150
151         /**
152          * Checks if the provided url domain is on the domain blocklist.
153          * Returns true if it is or malformed URL, false if not.
154          *
155          * @param string $url The url to check the domain from
156          *
157          * @return boolean
158          */
159         public static function isUrlBlocked(string $url)
160         {
161                 $host = @parse_url($url, PHP_URL_HOST);
162                 if (!$host) {
163                         return false;
164                 }
165
166                 $domain_blocklist = DI::config()->get('system', 'blocklist', []);
167                 if (!$domain_blocklist) {
168                         return false;
169                 }
170
171                 foreach ($domain_blocklist as $domain_block) {
172                         if (fnmatch(strtolower($domain_block['domain']), strtolower($host))) {
173                                 return true;
174                         }
175                 }
176
177                 return false;
178         }
179
180         /**
181          * Checks if the provided url is on the list of domains where redirects are blocked.
182          * Returns true if it is or malformed URL, false if not.
183          *
184          * @param string $url The url to check the domain from
185          *
186          * @return boolean
187          */
188         public static function isRedirectBlocked(string $url)
189         {
190                 $host = @parse_url($url, PHP_URL_HOST);
191                 if (!$host) {
192                         return false;
193                 }
194
195                 $no_redirect_list = DI::config()->get('system', 'no_redirect_list', []);
196                 if (!$no_redirect_list) {
197                         return false;
198                 }
199
200                 foreach ($no_redirect_list as $no_redirect) {
201                         if (fnmatch(strtolower($no_redirect), strtolower($host))) {
202                                 return true;
203                         }
204                 }
205
206                 return false;
207         }
208
209         /**
210          * Check if email address is allowed to register here.
211          *
212          * Compare against our list (wildcards allowed).
213          *
214          * @param  string $email email address
215          * @return boolean False if not allowed, true if allowed
216          *                       or if allowed list is not configured
217          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
218          */
219         public static function isEmailDomainAllowed(string $email)
220         {
221                 $domain = strtolower(substr($email, strpos($email, '@') + 1));
222                 if (!$domain) {
223                         return false;
224                 }
225
226                 $str_allowed = DI::config()->get('system', 'allowed_email', '');
227                 if (empty($str_allowed)) {
228                         return true;
229                 }
230
231                 $allowed = explode(',', $str_allowed);
232
233                 return self::isDomainAllowed($domain, $allowed);
234         }
235
236         /**
237          * Checks for the existence of a domain in a domain list
238          *
239          * @param string $domain
240          * @param array  $domain_list
241          * @return boolean
242          */
243         public static function isDomainAllowed(string $domain, array $domain_list)
244         {
245                 $found = false;
246
247                 foreach ($domain_list as $item) {
248                         $pat = strtolower(trim($item));
249                         if (fnmatch($pat, $domain) || ($pat == $domain)) {
250                                 $found = true;
251                                 break;
252                         }
253                 }
254
255                 return $found;
256         }
257
258         public static function lookupAvatarByEmail(string $email)
259         {
260                 $avatar['size'] = 300;
261                 $avatar['email'] = $email;
262                 $avatar['url'] = '';
263                 $avatar['success'] = false;
264
265                 Hook::callAll('avatar_lookup', $avatar);
266
267                 if (! $avatar['success']) {
268                         $avatar['url'] = DI::baseUrl() . Contact::DEFAULT_AVATAR_PHOTO;
269                 }
270
271                 Logger::log('Avatar: ' . $avatar['email'] . ' ' . $avatar['url'], Logger::DEBUG);
272                 return $avatar['url'];
273         }
274
275         /**
276          * Remove Google Analytics and other tracking platforms params from URL
277          *
278          * @param string $url Any user-submitted URL that may contain tracking params
279          * @return string The same URL stripped of tracking parameters
280          */
281         public static function stripTrackingQueryParams(string $url)
282         {
283                 $urldata = parse_url($url);
284                 if (!empty($urldata["query"])) {
285                         $query = $urldata["query"];
286                         parse_str($query, $querydata);
287
288                         if (is_array($querydata)) {
289                                 foreach ($querydata as $param => $value) {
290                                         if (in_array(
291                                                 $param,
292                                                 [
293                                                         "utm_source", "utm_medium", "utm_term", "utm_content", "utm_campaign",
294                                                         "wt_mc", "pk_campaign", "pk_kwd", "mc_cid", "mc_eid",
295                                                         "fb_action_ids", "fb_action_types", "fb_ref",
296                                                         "awesm", "wtrid",
297                                                         "woo_campaign", "woo_source", "woo_medium", "woo_content", "woo_term"]
298                                                 )
299                                         ) {
300                                                 $pair = $param . "=" . urlencode($value);
301                                                 $url = str_replace($pair, "", $url);
302
303                                                 // Second try: if the url isn't encoded completely
304                                                 $pair = $param . "=" . str_replace(" ", "+", $value);
305                                                 $url = str_replace($pair, "", $url);
306
307                                                 // Third try: Maybey the url isn't encoded at all
308                                                 $pair = $param . "=" . $value;
309                                                 $url = str_replace($pair, "", $url);
310
311                                                 $url = str_replace(["?&", "&&"], ["?", ""], $url);
312                                         }
313                                 }
314                         }
315
316                         if (substr($url, -1, 1) == "?") {
317                                 $url = substr($url, 0, -1);
318                         }
319                 }
320
321                 return $url;
322         }
323
324         /**
325          * Add a missing base path (scheme and host) to a given url
326          *
327          * @param string $url
328          * @param string $basepath
329          * @return string url
330          */
331         public static function addBasePath(string $url, string $basepath)
332         {
333                 if (!empty(parse_url($url, PHP_URL_SCHEME)) || empty(parse_url($basepath, PHP_URL_SCHEME)) || empty($url) || empty(parse_url($url))) {
334                         return $url;
335                 }
336
337                 $base = ['scheme' => parse_url($basepath, PHP_URL_SCHEME),
338                         'host' => parse_url($basepath, PHP_URL_HOST)];
339
340                 $parts = array_merge($base, parse_url('/' . ltrim($url, '/')));
341                 return self::unparseURL($parts);
342         }
343
344         /**
345          * Find the matching part between two url
346          *
347          * @param string $url1
348          * @param string $url2
349          * @return string The matching part
350          */
351         public static function getUrlMatch(string $url1, string $url2)
352         {
353                 if (($url1 == "") || ($url2 == "")) {
354                         return "";
355                 }
356
357                 $url1 = Strings::normaliseLink($url1);
358                 $url2 = Strings::normaliseLink($url2);
359
360                 $parts1 = parse_url($url1);
361                 $parts2 = parse_url($url2);
362
363                 if (!isset($parts1["host"]) || !isset($parts2["host"])) {
364                         return "";
365                 }
366
367                 if (empty($parts1["scheme"])) {
368                         $parts1["scheme"] = '';
369                 }
370                 if (empty($parts2["scheme"])) {
371                         $parts2["scheme"] = '';
372                 }
373
374                 if ($parts1["scheme"] != $parts2["scheme"]) {
375                         return "";
376                 }
377
378                 if (empty($parts1["host"])) {
379                         $parts1["host"] = '';
380                 }
381                 if (empty($parts2["host"])) {
382                         $parts2["host"] = '';
383                 }
384
385                 if ($parts1["host"] != $parts2["host"]) {
386                         return "";
387                 }
388
389                 if (empty($parts1["port"])) {
390                         $parts1["port"] = '';
391                 }
392                 if (empty($parts2["port"])) {
393                         $parts2["port"] = '';
394                 }
395
396                 if ($parts1["port"] != $parts2["port"]) {
397                         return "";
398                 }
399
400                 $match = $parts1["scheme"]."://".$parts1["host"];
401
402                 if ($parts1["port"]) {
403                         $match .= ":".$parts1["port"];
404                 }
405
406                 if (empty($parts1["path"])) {
407                         $parts1["path"] = '';
408                 }
409                 if (empty($parts2["path"])) {
410                         $parts2["path"] = '';
411                 }
412
413                 $pathparts1 = explode("/", $parts1["path"]);
414                 $pathparts2 = explode("/", $parts2["path"]);
415
416                 $i = 0;
417                 $path = "";
418                 do {
419                         $path1 = $pathparts1[$i] ?? '';
420                         $path2 = $pathparts2[$i] ?? '';
421
422                         if ($path1 == $path2) {
423                                 $path .= $path1."/";
424                         }
425                 } while (($path1 == $path2) && ($i++ <= count($pathparts1)));
426
427                 $match .= $path;
428
429                 return Strings::normaliseLink($match);
430         }
431
432         /**
433          * Glue url parts together
434          *
435          * @param array $parsed URL parts
436          *
437          * @return string The glued URL
438          */
439         public static function unparseURL(array $parsed)
440         {
441                 $get = function ($key) use ($parsed) {
442                         return isset($parsed[$key]) ? $parsed[$key] : null;
443                 };
444
445                 $pass      = $get('pass');
446                 $user      = $get('user');
447                 $userinfo  = $pass !== null ? "$user:$pass" : $user;
448                 $port      = $get('port');
449                 $scheme    = $get('scheme');
450                 $query     = $get('query');
451                 $fragment  = $get('fragment');
452                 $authority = ($userinfo !== null ? $userinfo."@" : '') .
453                                                 $get('host') .
454                                                 ($port ? ":$port" : '');
455
456                 return  (strlen($scheme) ? $scheme.":" : '') .
457                         (strlen($authority) ? "//".$authority : '') .
458                         $get('path') .
459                         (strlen($query) ? "?".$query : '') .
460                         (strlen($fragment) ? "#".$fragment : '');
461         }
462
463
464         /**
465          * Switch the scheme of an url between http and https
466          *
467          * @param string $url URL
468          *
469          * @return string switched URL
470          */
471         public static function switchScheme(string $url)
472         {
473                 $scheme = parse_url($url, PHP_URL_SCHEME);
474                 if (empty($scheme)) {
475                         return $url;
476                 }
477
478                 if ($scheme === 'http') {
479                         $url = str_replace('http://', 'https://', $url);
480                 } elseif ($scheme === 'https') {
481                         $url = str_replace('https://', 'http://', $url);
482                 }
483
484                 return $url;
485         }
486
487         /**
488          * Adds query string parameters to the provided URI. Replace the value of existing keys.
489          *
490          * @param string $path
491          * @param array  $additionalParams Associative array of parameters
492          * @return string
493          */
494         public static function appendQueryParam(string $path, array $additionalParams)
495         {
496                 $parsed = parse_url($path);
497
498                 $params = [];
499                 if (!empty($parsed['query'])) {
500                         parse_str($parsed['query'], $params);
501                 }
502
503                 $params = array_merge($params, $additionalParams);
504
505                 $parsed['query'] = http_build_query($params);
506
507                 return self::unparseURL($parsed);
508         }
509
510         /**
511          * Generates ETag and Last-Modified response headers and checks them against
512          * If-None-Match and If-Modified-Since request headers if present.
513          *
514          * Blocking function, sends 304 headers and exits if check passes.
515          *
516          * @param string $etag          The page etag
517          * @param string $last_modified The page last modification UTC date
518          * @throws \Exception
519          */
520         public static function checkEtagModified(string $etag, string $last_modified)
521         {
522                 $last_modified = DateTimeFormat::utc($last_modified, 'D, d M Y H:i:s') . ' GMT';
523
524                 /**
525                  * @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.26
526                  */
527                 $if_none_match     = filter_input(INPUT_SERVER, 'HTTP_IF_NONE_MATCH');
528                 $if_modified_since = filter_input(INPUT_SERVER, 'HTTP_IF_MODIFIED_SINCE');
529                 $flag_not_modified = null;
530                 if ($if_none_match) {
531                         $result = [];
532                         preg_match('/^(?:W\/")?([^"]+)"?$/i', $etag, $result);
533                         $etagTrimmed = $result[1];
534                         // Lazy exact ETag match, could check weak/strong ETags
535                         $flag_not_modified = $if_none_match == '*' || strpos($if_none_match, $etagTrimmed) !== false;
536                 }
537
538                 if ($if_modified_since && (!$if_none_match || $flag_not_modified)) {
539                         // Lazy exact Last-Modified match, could check If-Modified-Since validity
540                         $flag_not_modified = $if_modified_since == $last_modified;
541                 }
542
543                 header('Etag: ' . $etag);
544                 header('Last-Modified: ' . $last_modified);
545
546                 if ($flag_not_modified) {
547                         header("HTTP/1.1 304 Not Modified");
548                         exit;
549                 }
550         }
551 }