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