3 * @copyright Copyright (C) 2020, Friendica
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Util;
24 use Friendica\Core\Hook;
25 use Friendica\Core\Logger;
32 * Return raw post data from a post request
34 * @return string post data
36 public static function postdata()
38 return file_get_contents('php://input');
42 * Check URL to see if it's real
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)
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
51 public static function isUrlValid(string $url)
53 if (DI::config()->get('system', 'disable_url_validation')) {
57 // no naked subdomains (allow localhost for tests)
58 if (strpos($url, '.') === false && strpos($url, '/localhost/') === false) {
62 if (substr($url, 0, 4) != 'http') {
63 $url = 'http://' . $url;
66 /// @TODO Really suppress function outcomes? Why not find them + debug them?
67 $h = @parse_url($url);
69 if (!empty($h['host']) && (@dns_get_record($h['host'], DNS_A + DNS_CNAME) || filter_var($h['host'], FILTER_VALIDATE_IP))) {
77 * Checks that email is an actual resolvable internet address
79 * @param string $addr The email address
80 * @return boolean True if it's a valid email address, false if it's not
82 public static function isEmailDomainValid(string $addr)
84 if (DI::config()->get('system', 'disable_email_validation')) {
88 if (! strpos($addr, '@')) {
92 $h = substr($addr, strpos($addr, '@') + 1);
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))) {
98 if ($h && @dns_get_record($h, DNS_CNAME + DNS_MX)) {
105 * Check if URL is allowed
107 * Check $url against our list of allowed sites,
108 * wildcards allowed. If allowed_sites is unset return true;
110 * @param string $url URL which get tested
111 * @return boolean True if url is allowed otherwise return false
113 public static function isUrlAllowed(string $url)
115 $h = @parse_url($url);
121 $str_allowed = DI::config()->get('system', 'allowed_sites');
122 if (! $str_allowed) {
128 $host = strtolower($h['host']);
130 // always allow our own site
131 if ($host == strtolower($_SERVER['SERVER_NAME'])) {
135 $fnmatch = function_exists('fnmatch');
136 $allowed = explode(',', $str_allowed);
138 if (count($allowed)) {
139 foreach ($allowed as $a) {
140 $pat = strtolower(trim($a));
141 if (($fnmatch && fnmatch($pat, $host)) || ($pat == $host)) {
151 * Checks if the provided url domain is on the domain blocklist.
152 * Returns true if it is or malformed URL, false if not.
154 * @param string $url The url to check the domain from
158 public static function isUrlBlocked(string $url)
160 $host = @parse_url($url, PHP_URL_HOST);
165 $domain_blocklist = DI::config()->get('system', 'blocklist', []);
166 if (!$domain_blocklist) {
170 foreach ($domain_blocklist as $domain_block) {
171 if (fnmatch(strtolower($domain_block['domain']), strtolower($host))) {
180 * Check if email address is allowed to register here.
182 * Compare against our list (wildcards allowed).
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
189 public static function isEmailDomainAllowed(string $email)
191 $domain = strtolower(substr($email, strpos($email, '@') + 1));
196 $str_allowed = DI::config()->get('system', 'allowed_email', '');
197 if (empty($str_allowed)) {
201 $allowed = explode(',', $str_allowed);
203 return self::isDomainAllowed($domain, $allowed);
207 * Checks for the existence of a domain in a domain list
209 * @param string $domain
210 * @param array $domain_list
213 public static function isDomainAllowed(string $domain, array $domain_list)
217 foreach ($domain_list as $item) {
218 $pat = strtolower(trim($item));
219 if (fnmatch($pat, $domain) || ($pat == $domain)) {
228 public static function lookupAvatarByEmail(string $email)
230 $avatar['size'] = 300;
231 $avatar['email'] = $email;
233 $avatar['success'] = false;
235 Hook::callAll('avatar_lookup', $avatar);
237 if (! $avatar['success']) {
238 $avatar['url'] = DI::baseUrl() . '/images/person-300.jpg';
241 Logger::log('Avatar: ' . $avatar['email'] . ' ' . $avatar['url'], Logger::DEBUG);
242 return $avatar['url'];
246 * Remove Google Analytics and other tracking platforms params from URL
248 * @param string $url Any user-submitted URL that may contain tracking params
249 * @return string The same URL stripped of tracking parameters
251 public static function stripTrackingQueryParams(string $url)
253 $urldata = parse_url($url);
254 if (!empty($urldata["query"])) {
255 $query = $urldata["query"];
256 parse_str($query, $querydata);
258 if (is_array($querydata)) {
259 foreach ($querydata as $param => $value) {
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",
267 "woo_campaign", "woo_source", "woo_medium", "woo_content", "woo_term"]
270 $pair = $param . "=" . urlencode($value);
271 $url = str_replace($pair, "", $url);
273 // Second try: if the url isn't encoded completely
274 $pair = $param . "=" . str_replace(" ", "+", $value);
275 $url = str_replace($pair, "", $url);
277 // Third try: Maybey the url isn't encoded at all
278 $pair = $param . "=" . $value;
279 $url = str_replace($pair, "", $url);
281 $url = str_replace(["?&", "&&"], ["?", ""], $url);
286 if (substr($url, -1, 1) == "?") {
287 $url = substr($url, 0, -1);
295 * Add a missing base path (scheme and host) to a given url
298 * @param string $basepath
301 public static function addBasePath(string $url, string $basepath)
303 if (!empty(parse_url($url, PHP_URL_SCHEME)) || empty(parse_url($basepath, PHP_URL_SCHEME)) || empty($url) || empty(parse_url($url))) {
307 $base = ['scheme' => parse_url($basepath, PHP_URL_SCHEME),
308 'host' => parse_url($basepath, PHP_URL_HOST)];
310 $parts = array_merge($base, parse_url('/' . ltrim($url, '/')));
311 return self::unparseURL($parts);
315 * Find the matching part between two url
317 * @param string $url1
318 * @param string $url2
319 * @return string The matching part
321 public static function getUrlMatch(string $url1, string $url2)
323 if (($url1 == "") || ($url2 == "")) {
327 $url1 = Strings::normaliseLink($url1);
328 $url2 = Strings::normaliseLink($url2);
330 $parts1 = parse_url($url1);
331 $parts2 = parse_url($url2);
333 if (!isset($parts1["host"]) || !isset($parts2["host"])) {
337 if (empty($parts1["scheme"])) {
338 $parts1["scheme"] = '';
340 if (empty($parts2["scheme"])) {
341 $parts2["scheme"] = '';
344 if ($parts1["scheme"] != $parts2["scheme"]) {
348 if (empty($parts1["host"])) {
349 $parts1["host"] = '';
351 if (empty($parts2["host"])) {
352 $parts2["host"] = '';
355 if ($parts1["host"] != $parts2["host"]) {
359 if (empty($parts1["port"])) {
360 $parts1["port"] = '';
362 if (empty($parts2["port"])) {
363 $parts2["port"] = '';
366 if ($parts1["port"] != $parts2["port"]) {
370 $match = $parts1["scheme"]."://".$parts1["host"];
372 if ($parts1["port"]) {
373 $match .= ":".$parts1["port"];
376 if (empty($parts1["path"])) {
377 $parts1["path"] = '';
379 if (empty($parts2["path"])) {
380 $parts2["path"] = '';
383 $pathparts1 = explode("/", $parts1["path"]);
384 $pathparts2 = explode("/", $parts2["path"]);
389 $path1 = $pathparts1[$i] ?? '';
390 $path2 = $pathparts2[$i] ?? '';
392 if ($path1 == $path2) {
395 } while (($path1 == $path2) && ($i++ <= count($pathparts1)));
399 return Strings::normaliseLink($match);
403 * Glue url parts together
405 * @param array $parsed URL parts
407 * @return string The glued URL
409 public static function unparseURL(array $parsed)
411 $get = function ($key) use ($parsed) {
412 return isset($parsed[$key]) ? $parsed[$key] : null;
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."@" : '') .
424 ($port ? ":$port" : '');
426 return (strlen($scheme) ? $scheme.":" : '') .
427 (strlen($authority) ? "//".$authority : '') .
429 (strlen($query) ? "?".$query : '') .
430 (strlen($fragment) ? "#".$fragment : '');
435 * Switch the scheme of an url between http and https
437 * @param string $url URL
439 * @return string switched URL
441 public static function switchScheme(string $url)
443 $scheme = parse_url($url, PHP_URL_SCHEME);
444 if (empty($scheme)) {
448 if ($scheme === 'http') {
449 $url = str_replace('http://', 'https://', $url);
450 } elseif ($scheme === 'https') {
451 $url = str_replace('https://', 'http://', $url);
458 * Adds query string parameters to the provided URI. Replace the value of existing keys.
460 * @param string $path
461 * @param array $additionalParams Associative array of parameters
464 public static function appendQueryParam(string $path, array $additionalParams)
466 $parsed = parse_url($path);
469 if (!empty($parsed['query'])) {
470 parse_str($parsed['query'], $params);
473 $params = array_merge($params, $additionalParams);
475 $parsed['query'] = http_build_query($params);
477 return self::unparseURL($parsed);
481 * Generates ETag and Last-Modified response headers and checks them against
482 * If-None-Match and If-Modified-Since request headers if present.
484 * Blocking function, sends 304 headers and exits if check passes.
486 * @param string $etag The page etag
487 * @param string $last_modified The page last modification UTC date
490 public static function checkEtagModified(string $etag, string $last_modified)
492 $last_modified = DateTimeFormat::utc($last_modified, 'D, d M Y H:i:s') . ' GMT';
495 * @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.26
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) {
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;
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;
513 header('Etag: ' . $etag);
514 header('Last-Modified: ' . $last_modified);
516 if ($flag_not_modified) {
517 header("HTTP/1.1 304 Not Modified");