]> git.mxchange.org Git - friendica.git/blob - src/Util/Network.php
add more doc
[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): bool
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): bool
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): bool
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): bool
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): bool
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): bool
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): string
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): string
284         {
285                 $urldata = parse_url($url);
286
287                 if (!empty($urldata['query'])) {
288                         $query = $urldata['query'];
289                         parse_str($query, $querydata);
290
291                         if (is_array($querydata)) {
292                                 foreach ($querydata as $param => $value) {
293                                         if (in_array(
294                                                 $param,
295                                                 [
296                                                         'utm_source', 'utm_medium', 'utm_term', 'utm_content', 'utm_campaign',
297                                                         // As seen from Purism
298                                                         'mtm_source', 'mtm_medium', 'mtm_term', 'mtm_content', 'mtm_campaign',
299                                                         'wt_mc', 'pk_campaign', 'pk_kwd', 'mc_cid', 'mc_eid',
300                                                         'fb_action_ids', 'fb_action_types', 'fb_ref',
301                                                         'awesm', 'wtrid',
302                                                         'woo_campaign', 'woo_source', 'woo_medium', 'woo_content', 'woo_term']
303                                                 )
304                                         ) {
305                                                 $pair = $param . '=' . urlencode($value);
306                                                 $url = str_replace($pair, '', $url);
307
308                                                 // Second try: if the url isn't encoded completely
309                                                 $pair = $param . '=' . str_replace(' ', '+', $value);
310                                                 $url = str_replace($pair, '', $url);
311
312                                                 // Third try: Maybey the url isn't encoded at all
313                                                 $pair = $param . '=' . $value;
314                                                 $url = str_replace($pair, '', $url);
315
316                                                 $url = str_replace(['?&', '&&'], ['?', ''], $url);
317                                         }
318                                 }
319                         }
320
321                         if (substr($url, -1, 1) == '?') {
322                                 $url = substr($url, 0, -1);
323                         }
324                 }
325
326                 return $url;
327         }
328
329         /**
330          * Add a missing base path (scheme and host) to a given url
331          *
332          * @param string $url
333          * @param string $basepath
334          * @return string url
335          */
336         public static function addBasePath(string $url, string $basepath)
337         {
338                 if (!empty(parse_url($url, PHP_URL_SCHEME)) || empty(parse_url($basepath, PHP_URL_SCHEME)) || empty($url) || empty(parse_url($url))) {
339                         return $url;
340                 }
341
342                 $base = [
343                         'scheme' => parse_url($basepath, PHP_URL_SCHEME),
344                         'host' => parse_url($basepath, PHP_URL_HOST),
345                 ];
346
347                 $parts = array_merge($base, parse_url('/' . ltrim($url, '/')));
348                 return self::unparseURL($parts);
349         }
350
351         /**
352          * Find the matching part between two url
353          *
354          * @param string $url1
355          * @param string $url2
356          * @return string The matching part or empty string on error
357          */
358         public static function getUrlMatch(string $url1, string $url2): string
359         {
360                 if (($url1 == '') || ($url2 == '')) {
361                         return '';
362                 }
363
364                 $url1 = Strings::normaliseLink($url1);
365                 $url2 = Strings::normaliseLink($url2);
366
367                 $parts1 = parse_url($url1);
368                 $parts2 = parse_url($url2);
369
370                 if (!isset($parts1['host']) || !isset($parts2['host'])) {
371                         return '';
372                 }
373
374                 if (empty($parts1['scheme'])) {
375                         $parts1['scheme'] = '';
376                 }
377                 if (empty($parts2['scheme'])) {
378                         $parts2['scheme'] = '';
379                 }
380
381                 if ($parts1['scheme'] != $parts2['scheme']) {
382                         return '';
383                 }
384
385                 if (empty($parts1['host'])) {
386                         $parts1['host'] = '';
387                 }
388                 if (empty($parts2['host'])) {
389                         $parts2['host'] = '';
390                 }
391
392                 if ($parts1['host'] != $parts2['host']) {
393                         return '';
394                 }
395
396                 if (empty($parts1['port'])) {
397                         $parts1['port'] = '';
398                 }
399                 if (empty($parts2['port'])) {
400                         $parts2['port'] = '';
401                 }
402
403                 if ($parts1['port'] != $parts2['port']) {
404                         return '';
405                 }
406
407                 $match = $parts1['scheme'] . '://' . $parts1['host'];
408
409                 if ($parts1['port']) {
410                         $match .= ':' . $parts1['port'];
411                 }
412
413                 if (empty($parts1['path'])) {
414                         $parts1['path'] = '';
415                 }
416                 if (empty($parts2['path'])) {
417                         $parts2['path'] = '';
418                 }
419
420                 $pathparts1 = explode('/', $parts1['path']);
421                 $pathparts2 = explode('/', $parts2['path']);
422
423                 $i = 0;
424                 $path = '';
425                 do {
426                         $path1 = $pathparts1[$i] ?? '';
427                         $path2 = $pathparts2[$i] ?? '';
428
429                         if ($path1 == $path2) {
430                                 $path .= $path1 . '/';
431                         }
432                 } while (($path1 == $path2) && ($i++ <= count($pathparts1)));
433
434                 $match .= $path;
435
436                 return Strings::normaliseLink($match);
437         }
438
439         /**
440          * Glue url parts together
441          *
442          * @param array $parsed URL parts
443          * @return string|null The glued URL or null on error
444          * @deprecated since version 2021.12, use GuzzleHttp\Psr7\Uri::fromParts($parts) instead
445          */
446         public static function unparseURL(array $parsed): string
447         {
448                 $get = function ($key) use ($parsed) {
449                         return isset($parsed[$key]) ? $parsed[$key] : null;
450                 };
451
452                 $pass      = $get('pass');
453                 $user      = $get('user');
454                 $userinfo  = $pass !== null ? "$user:$pass" : $user;
455                 $port      = $get('port');
456                 $scheme    = $get('scheme');
457                 $query     = $get('query');
458                 $fragment  = $get('fragment');
459                 $authority = ($userinfo !== null ? $userinfo . '@' : '') . 
460                                                 $get('host') .
461                                                 ($port ? ":$port" : '');
462
463                 return  (strlen($scheme) ? $scheme . ':' : '') .
464                         (strlen($authority) ? '//' . $authority : '') .
465                         $get('path') .
466                         (strlen($query) ? '?' . $query : '') .
467                         (strlen($fragment) ? '#' . $fragment : '');
468         }
469
470         /**
471          * Convert an URI to an IDN compatible URI
472          *
473          * @param string $uri
474          * @return string
475          */
476         public static function convertToIdn(string $uri): string
477         {
478                 $parts = parse_url($uri);
479                 if (!empty($parts['scheme']) && !empty($parts['host'])) {
480                         $parts['host'] = idn_to_ascii($parts['host']);
481                         $uri = Uri::fromParts($parts);
482                 } else {
483                         $parts = explode('@', $uri);
484                         if (count($parts) == 2) {
485                                 $uri = $parts[0] . '@' . idn_to_ascii($parts[1]);
486                         } else {
487                                 $uri = idn_to_ascii($uri);
488                         }
489                 }
490
491                 return $uri;
492         }
493
494         /**
495          * Switch the scheme of an url between http and https
496          *
497          * @param string $url
498          * @return string Switched URL
499          */
500         public static function switchScheme(string $url): string
501         {
502                 $scheme = parse_url($url, PHP_URL_SCHEME);
503                 if (empty($scheme)) {
504                         return $url;
505                 }
506
507                 if ($scheme === 'http') {
508                         $url = str_replace('http://', 'https://', $url);
509                 } elseif ($scheme === 'https') {
510                         $url = str_replace('https://', 'http://', $url);
511                 }
512
513                 return $url;
514         }
515
516         /**
517          * Adds query string parameters to the provided URI. Replace the value of existing keys.
518          *
519          * @param string $path
520          * @param array  $additionalParams Associative array of parameters
521          * @return string
522          */
523         public static function appendQueryParam(string $path, array $additionalParams): string
524         {
525                 $parsed = parse_url($path);
526
527                 $params = [];
528                 if (!empty($parsed['query'])) {
529                         parse_str($parsed['query'], $params);
530                 }
531
532                 $params = array_merge($params, $additionalParams);
533
534                 $parsed['query'] = http_build_query($params);
535
536                 return self::unparseURL($parsed);
537         }
538
539         /**
540          * Generates ETag and Last-Modified response headers and checks them against
541          * If-None-Match and If-Modified-Since request headers if present.
542          *
543          * Blocking function, sends 304 headers and exits if check passes.
544          *
545          * @param string $etag          The page etag
546          * @param string $last_modified The page last modification UTC date
547          * @return void
548          * @throws \Exception
549          */
550         public static function checkEtagModified(string $etag, string $last_modified)
551         {
552                 $last_modified = DateTimeFormat::utc($last_modified, 'D, d M Y H:i:s') . ' GMT';
553
554                 /**
555                  * @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.26
556                  */
557                 $if_none_match     = filter_input(INPUT_SERVER, 'HTTP_IF_NONE_MATCH');
558                 $if_modified_since = filter_input(INPUT_SERVER, 'HTTP_IF_MODIFIED_SINCE');
559                 $flag_not_modified = null;
560                 if ($if_none_match) {
561                         $result = [];
562                         preg_match('/^(?:W\/")?([^"]+)"?$/i', $etag, $result);
563                         $etagTrimmed = $result[1];
564                         // Lazy exact ETag match, could check weak/strong ETags
565                         $flag_not_modified = $if_none_match == '*' || strpos($if_none_match, $etagTrimmed) !== false;
566                 }
567
568                 if ($if_modified_since && (!$if_none_match || $flag_not_modified)) {
569                         // Lazy exact Last-Modified match, could check If-Modified-Since validity
570                         $flag_not_modified = $if_modified_since == $last_modified;
571                 }
572
573                 header('Etag: ' . $etag);
574                 header('Last-Modified: ' . $last_modified);
575
576                 if ($flag_not_modified) {
577                         throw new NotModifiedException();
578                 }
579         }
580
581         /**
582          * Check if the given URL is a local link
583          *
584          * @param string $url
585          * @return bool
586          */
587         public static function isLocalLink(string $url): bool
588         {
589                 return (strpos(Strings::normaliseLink($url), Strings::normaliseLink(DI::baseUrl())) !== false);
590         }
591
592         /**
593          * Check if the given URL is a valid HTTP/HTTPS URL
594          *
595          * @param string $url
596          * @return bool
597          */
598         public static function isValidHttpUrl(string $url): bool
599         {
600                 $scheme = parse_url($url, PHP_URL_SCHEME);
601                 return !empty($scheme) && in_array($scheme, ['http', 'https']) && parse_url($url, PHP_URL_HOST);
602         }
603 }