]> git.mxchange.org Git - friendica.git/blob - src/Util/Network.php
Apply feedback and describe the encoding method
[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: Maybey 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                 if (!empty(parse_url($url, PHP_URL_SCHEME)) || empty(parse_url($basepath, PHP_URL_SCHEME)) || empty($url) || empty(parse_url($url))) {
380                         return $url;
381                 }
382
383                 $base = [
384                         'scheme' => parse_url($basepath, PHP_URL_SCHEME),
385                         'host' => parse_url($basepath, PHP_URL_HOST),
386                 ];
387
388                 $parts = array_merge($base, parse_url('/' . ltrim($url, '/')));
389                 return self::unparseURL($parts);
390         }
391
392         /**
393          * Find the matching part between two url
394          *
395          * @param string $url1
396          * @param string $url2
397          *
398          * @return string The matching part or empty string on error
399          */
400         public static function getUrlMatch(string $url1, string $url2): string
401         {
402                 if (($url1 == '') || ($url2 == '')) {
403                         return '';
404                 }
405
406                 $url1 = Strings::normaliseLink($url1);
407                 $url2 = Strings::normaliseLink($url2);
408
409                 $parts1 = parse_url($url1);
410                 $parts2 = parse_url($url2);
411
412                 if (!isset($parts1['host']) || !isset($parts2['host'])) {
413                         return '';
414                 }
415
416                 if (empty($parts1['scheme'])) {
417                         $parts1['scheme'] = '';
418                 }
419                 if (empty($parts2['scheme'])) {
420                         $parts2['scheme'] = '';
421                 }
422
423                 if ($parts1['scheme'] != $parts2['scheme']) {
424                         return '';
425                 }
426
427                 if (empty($parts1['host'])) {
428                         $parts1['host'] = '';
429                 }
430                 if (empty($parts2['host'])) {
431                         $parts2['host'] = '';
432                 }
433
434                 if ($parts1['host'] != $parts2['host']) {
435                         return '';
436                 }
437
438                 if (empty($parts1['port'])) {
439                         $parts1['port'] = '';
440                 }
441                 if (empty($parts2['port'])) {
442                         $parts2['port'] = '';
443                 }
444
445                 if ($parts1['port'] != $parts2['port']) {
446                         return '';
447                 }
448
449                 $match = $parts1['scheme'] . '://' . $parts1['host'];
450
451                 if ($parts1['port']) {
452                         $match .= ':' . $parts1['port'];
453                 }
454
455                 if (empty($parts1['path'])) {
456                         $parts1['path'] = '';
457                 }
458                 if (empty($parts2['path'])) {
459                         $parts2['path'] = '';
460                 }
461
462                 $pathparts1 = explode('/', $parts1['path']);
463                 $pathparts2 = explode('/', $parts2['path']);
464
465                 $i = 0;
466                 $path = '';
467                 do {
468                         $path1 = $pathparts1[$i] ?? '';
469                         $path2 = $pathparts2[$i] ?? '';
470
471                         if ($path1 == $path2) {
472                                 $path .= $path1 . '/';
473                         }
474                 } while (($path1 == $path2) && ($i++ <= count($pathparts1)));
475
476                 $match .= $path;
477
478                 return Strings::normaliseLink($match);
479         }
480
481         /**
482          * Glue url parts together
483          *
484          * @param array $parsed URL parts
485          *
486          * @return string|null The glued URL or null on error
487          * @deprecated since version 2021.12, use GuzzleHttp\Psr7\Uri::fromParts($parts) instead
488          */
489         public static function unparseURL(array $parsed): string
490         {
491                 $get = function ($key) use ($parsed) {
492                         return isset($parsed[$key]) ? $parsed[$key] : null;
493                 };
494
495                 $pass      = $get('pass');
496                 $user      = $get('user');
497                 $userinfo  = $pass !== null ? "$user:$pass" : $user;
498                 $port      = $get('port');
499                 $scheme    = $get('scheme');
500                 $query     = $get('query');
501                 $fragment  = $get('fragment');
502                 $authority = ($userinfo !== null ? $userinfo . '@' : '') .
503                                                 $get('host') .
504                                                 ($port ? ":$port" : '');
505
506                 return  (!empty($scheme) ? $scheme . ':' : '') .
507                         (!empty($authority) ? '//' . $authority : '') .
508                         $get('path') .
509                         (!empty($query) ? '?' . $query : '') .
510                         (!empty($fragment) ? '#' . $fragment : '');
511         }
512
513         /**
514          * Convert an URI to an IDN compatible URI
515          *
516          * @param string $uri
517          *
518          * @return string
519          */
520         public static function convertToIdn(string $uri): string
521         {
522                 $parts = parse_url($uri);
523                 if (!empty($parts['scheme']) && !empty($parts['host'])) {
524                         $parts['host'] = idn_to_ascii($parts['host']);
525                         $uri = (string)Uri::fromParts($parts);
526                 } else {
527                         $parts = explode('@', $uri);
528                         if (count($parts) == 2) {
529                                 $uri = $parts[0] . '@' . idn_to_ascii($parts[1]);
530                         } else {
531                                 $uri = idn_to_ascii($uri);
532                         }
533                 }
534
535                 return $uri;
536         }
537
538         /**
539          * Switch the scheme of an url between http and https
540          *
541          * @param string $url
542          *
543          * @return string Switched URL
544          */
545         public static function switchScheme(string $url): string
546         {
547                 $scheme = parse_url($url, PHP_URL_SCHEME);
548                 if (empty($scheme)) {
549                         return $url;
550                 }
551
552                 if ($scheme === 'http') {
553                         $url = str_replace('http://', 'https://', $url);
554                 } elseif ($scheme === 'https') {
555                         $url = str_replace('https://', 'http://', $url);
556                 }
557
558                 return $url;
559         }
560
561         /**
562          * Adds query string parameters to the provided URI. Replace the value of existing keys.
563          *
564          * @param string $path
565          * @param array  $additionalParams Associative array of parameters
566          *
567          * @return string
568          */
569         public static function appendQueryParam(string $path, array $additionalParams): string
570         {
571                 $parsed = parse_url($path);
572
573                 $params = [];
574                 if (!empty($parsed['query'])) {
575                         parse_str($parsed['query'], $params);
576                 }
577
578                 $params = array_merge($params, $additionalParams);
579
580                 $parsed['query'] = http_build_query($params);
581
582                 return self::unparseURL($parsed);
583         }
584
585         /**
586          * Generates ETag and Last-Modified response headers and checks them against
587          * If-None-Match and If-Modified-Since request headers if present.
588          *
589          * Blocking function, sends 304 headers and exits if check passes.
590          *
591          * @param string $etag          The page etag
592          * @param string $last_modified The page last modification UTC date
593          *
594          * @return void
595          * @throws \Exception
596          */
597         public static function checkEtagModified(string $etag, string $last_modified)
598         {
599                 $last_modified = DateTimeFormat::utc($last_modified, 'D, d M Y H:i:s') . ' GMT';
600
601                 /**
602                  * @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.26
603                  */
604                 $if_none_match     = filter_input(INPUT_SERVER, 'HTTP_IF_NONE_MATCH');
605                 $if_modified_since = filter_input(INPUT_SERVER, 'HTTP_IF_MODIFIED_SINCE');
606                 $flag_not_modified = null;
607                 if ($if_none_match) {
608                         $result = [];
609                         preg_match('/^(?:W\/")?([^"]+)"?$/i', $etag, $result);
610                         $etagTrimmed = $result[1];
611                         // Lazy exact ETag match, could check weak/strong ETags
612                         $flag_not_modified = $if_none_match == '*' || strpos($if_none_match, $etagTrimmed) !== false;
613                 }
614
615                 if ($if_modified_since && (!$if_none_match || $flag_not_modified)) {
616                         // Lazy exact Last-Modified match, could check If-Modified-Since validity
617                         $flag_not_modified = $if_modified_since == $last_modified;
618                 }
619
620                 header('Etag: ' . $etag);
621                 header('Last-Modified: ' . $last_modified);
622
623                 if ($flag_not_modified) {
624                         throw new NotModifiedException();
625                 }
626         }
627
628         /**
629          * Check if the given URL is a local link
630          *
631          * @param string $url
632          *
633          * @return bool
634          */
635         public static function isLocalLink(string $url): bool
636         {
637                 return (strpos(Strings::normaliseLink($url), Strings::normaliseLink(DI::baseUrl())) !== false);
638         }
639
640         /**
641          * Check if the given URL is a valid HTTP/HTTPS URL
642          *
643          * @param string $url
644          * @return bool
645          */
646         public static function isValidHttpUrl(string $url): bool
647         {
648                 $scheme = parse_url($url, PHP_URL_SCHEME);
649                 return !empty($scheme) && in_array($scheme, ['http', 'https']) && parse_url($url, PHP_URL_HOST);
650         }
651 }