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