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