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