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