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