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