3 * @copyright Copyright (C) 2010-2021, the Friendica project
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;
27 use Friendica\Model\Contact;
28 use Friendica\Network\HTTPException\NotModifiedException;
34 * Return raw post data from a post request
36 * @return string post data
38 public static function postdata()
40 return file_get_contents('php://input');
44 * Check URL to see if it's real
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)
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
53 public static function isUrlValid(string $url)
55 if (DI::config()->get('system', 'disable_url_validation')) {
59 // no naked subdomains (allow localhost for tests)
60 if (strpos($url, '.') === false && strpos($url, '/localhost/') === false) {
64 if (substr($url, 0, 4) != 'http') {
65 $url = 'http://' . $url;
68 /// @TODO Really suppress function outcomes? Why not find them + debug them?
69 $h = @parse_url($url);
71 if (!empty($h['host']) && (@dns_get_record($h['host'], DNS_A + DNS_CNAME) || filter_var($h['host'], FILTER_VALIDATE_IP))) {
79 * Checks that email is an actual resolvable internet address
81 * @param string $addr The email address
82 * @return boolean True if it's a valid email address, false if it's not
84 public static function isEmailDomainValid(string $addr)
86 if (DI::config()->get('system', 'disable_email_validation')) {
90 if (! strpos($addr, '@')) {
94 $h = substr($addr, strpos($addr, '@') + 1);
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))) {
100 if ($h && @dns_get_record($h, DNS_CNAME + DNS_MX)) {
107 * Check if URL is allowed
109 * Check $url against our list of allowed sites,
110 * wildcards allowed. If allowed_sites is unset return true;
112 * @param string $url URL which get tested
113 * @return boolean True if url is allowed otherwise return false
115 public static function isUrlAllowed(string $url)
117 $h = @parse_url($url);
123 $str_allowed = DI::config()->get('system', 'allowed_sites');
124 if (! $str_allowed) {
130 $host = strtolower($h['host']);
132 // always allow our own site
133 if ($host == strtolower($_SERVER['SERVER_NAME'])) {
137 $fnmatch = function_exists('fnmatch');
138 $allowed = explode(',', $str_allowed);
140 if (count($allowed)) {
141 foreach ($allowed as $a) {
142 $pat = strtolower(trim($a));
143 if (($fnmatch && fnmatch($pat, $host)) || ($pat == $host)) {
153 * Checks if the provided url domain is on the domain blocklist.
154 * Returns true if it is or malformed URL, false if not.
156 * @param string $url The url to check the domain from
160 public static function isUrlBlocked(string $url)
162 $host = @parse_url($url, PHP_URL_HOST);
167 $domain_blocklist = DI::config()->get('system', 'blocklist', []);
168 if (!$domain_blocklist) {
172 foreach ($domain_blocklist as $domain_block) {
173 if (fnmatch(strtolower($domain_block['domain']), strtolower($host))) {
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.
185 * @param string $url The url to check the domain from
189 public static function isRedirectBlocked(string $url)
191 $host = @parse_url($url, PHP_URL_HOST);
196 $no_redirect_list = DI::config()->get('system', 'no_redirect_list', []);
197 if (!$no_redirect_list) {
201 foreach ($no_redirect_list as $no_redirect) {
202 if (fnmatch(strtolower($no_redirect), strtolower($host))) {
211 * Check if email address is allowed to register here.
213 * Compare against our list (wildcards allowed).
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
220 public static function isEmailDomainAllowed(string $email)
222 $domain = strtolower(substr($email, strpos($email, '@') + 1));
227 $str_allowed = DI::config()->get('system', 'allowed_email', '');
228 if (empty($str_allowed)) {
232 $allowed = explode(',', $str_allowed);
234 return self::isDomainAllowed($domain, $allowed);
238 * Checks for the existence of a domain in a domain list
240 * @param string $domain
241 * @param array $domain_list
244 public static function isDomainAllowed(string $domain, array $domain_list)
248 foreach ($domain_list as $item) {
249 $pat = strtolower(trim($item));
250 if (fnmatch($pat, $domain) || ($pat == $domain)) {
259 public static function lookupAvatarByEmail(string $email)
261 $avatar['size'] = 300;
262 $avatar['email'] = $email;
264 $avatar['success'] = false;
266 Hook::callAll('avatar_lookup', $avatar);
268 if (! $avatar['success']) {
269 $avatar['url'] = DI::baseUrl() . Contact::DEFAULT_AVATAR_PHOTO;
272 Logger::info('Avatar: ' . $avatar['email'] . ' ' . $avatar['url']);
273 return $avatar['url'];
277 * Remove Google Analytics and other tracking platforms params from URL
279 * @param string $url Any user-submitted URL that may contain tracking params
280 * @return string The same URL stripped of tracking parameters
282 public static function stripTrackingQueryParams(string $url)
284 $urldata = parse_url($url);
285 if (!empty($urldata["query"])) {
286 $query = $urldata["query"];
287 parse_str($query, $querydata);
289 if (is_array($querydata)) {
290 foreach ($querydata as $param => $value) {
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",
298 "woo_campaign", "woo_source", "woo_medium", "woo_content", "woo_term"]
301 $pair = $param . "=" . urlencode($value);
302 $url = str_replace($pair, "", $url);
304 // Second try: if the url isn't encoded completely
305 $pair = $param . "=" . str_replace(" ", "+", $value);
306 $url = str_replace($pair, "", $url);
308 // Third try: Maybey the url isn't encoded at all
309 $pair = $param . "=" . $value;
310 $url = str_replace($pair, "", $url);
312 $url = str_replace(["?&", "&&"], ["?", ""], $url);
317 if (substr($url, -1, 1) == "?") {
318 $url = substr($url, 0, -1);
326 * Add a missing base path (scheme and host) to a given url
329 * @param string $basepath
332 public static function addBasePath(string $url, string $basepath)
334 if (!empty(parse_url($url, PHP_URL_SCHEME)) || empty(parse_url($basepath, PHP_URL_SCHEME)) || empty($url) || empty(parse_url($url))) {
338 $base = ['scheme' => parse_url($basepath, PHP_URL_SCHEME),
339 'host' => parse_url($basepath, PHP_URL_HOST)];
341 $parts = array_merge($base, parse_url('/' . ltrim($url, '/')));
342 return self::unparseURL($parts);
346 * Find the matching part between two url
348 * @param string $url1
349 * @param string $url2
350 * @return string The matching part
352 public static function getUrlMatch(string $url1, string $url2)
354 if (($url1 == "") || ($url2 == "")) {
358 $url1 = Strings::normaliseLink($url1);
359 $url2 = Strings::normaliseLink($url2);
361 $parts1 = parse_url($url1);
362 $parts2 = parse_url($url2);
364 if (!isset($parts1["host"]) || !isset($parts2["host"])) {
368 if (empty($parts1["scheme"])) {
369 $parts1["scheme"] = '';
371 if (empty($parts2["scheme"])) {
372 $parts2["scheme"] = '';
375 if ($parts1["scheme"] != $parts2["scheme"]) {
379 if (empty($parts1["host"])) {
380 $parts1["host"] = '';
382 if (empty($parts2["host"])) {
383 $parts2["host"] = '';
386 if ($parts1["host"] != $parts2["host"]) {
390 if (empty($parts1["port"])) {
391 $parts1["port"] = '';
393 if (empty($parts2["port"])) {
394 $parts2["port"] = '';
397 if ($parts1["port"] != $parts2["port"]) {
401 $match = $parts1["scheme"]."://".$parts1["host"];
403 if ($parts1["port"]) {
404 $match .= ":".$parts1["port"];
407 if (empty($parts1["path"])) {
408 $parts1["path"] = '';
410 if (empty($parts2["path"])) {
411 $parts2["path"] = '';
414 $pathparts1 = explode("/", $parts1["path"]);
415 $pathparts2 = explode("/", $parts2["path"]);
420 $path1 = $pathparts1[$i] ?? '';
421 $path2 = $pathparts2[$i] ?? '';
423 if ($path1 == $path2) {
426 } while (($path1 == $path2) && ($i++ <= count($pathparts1)));
430 return Strings::normaliseLink($match);
434 * Glue url parts together
436 * @param array $parsed URL parts
438 * @return string The glued URL.
439 * @deprecated since version 2021.12, use a UriInterface object like GuzzleHttp\Psr7\Uri instead
441 public static function unparseURL(array $parsed)
443 $get = function ($key) use ($parsed) {
444 return isset($parsed[$key]) ? $parsed[$key] : null;
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."@" : '') .
456 ($port ? ":$port" : '');
458 return (strlen($scheme) ? $scheme.":" : '') .
459 (strlen($authority) ? "//".$authority : '') .
461 (strlen($query) ? "?".$query : '') .
462 (strlen($fragment) ? "#".$fragment : '');
467 * Switch the scheme of an url between http and https
469 * @param string $url URL
471 * @return string switched URL
473 public static function switchScheme(string $url)
475 $scheme = parse_url($url, PHP_URL_SCHEME);
476 if (empty($scheme)) {
480 if ($scheme === 'http') {
481 $url = str_replace('http://', 'https://', $url);
482 } elseif ($scheme === 'https') {
483 $url = str_replace('https://', 'http://', $url);
490 * Adds query string parameters to the provided URI. Replace the value of existing keys.
492 * @param string $path
493 * @param array $additionalParams Associative array of parameters
496 public static function appendQueryParam(string $path, array $additionalParams)
498 $parsed = parse_url($path);
501 if (!empty($parsed['query'])) {
502 parse_str($parsed['query'], $params);
505 $params = array_merge($params, $additionalParams);
507 $parsed['query'] = http_build_query($params);
509 return self::unparseURL($parsed);
513 * Generates ETag and Last-Modified response headers and checks them against
514 * If-None-Match and If-Modified-Since request headers if present.
516 * Blocking function, sends 304 headers and exits if check passes.
518 * @param string $etag The page etag
519 * @param string $last_modified The page last modification UTC date
522 public static function checkEtagModified(string $etag, string $last_modified)
524 $last_modified = DateTimeFormat::utc($last_modified, 'D, d M Y H:i:s') . ' GMT';
527 * @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.26
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) {
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;
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;
545 header('Etag: ' . $etag);
546 header('Last-Modified: ' . $last_modified);
548 if ($flag_not_modified) {
549 throw new NotModifiedException();
554 * Check if the given URL is a local link
559 public static function isLocalLink(string $url)
561 return (strpos(Strings::normaliseLink($url), Strings::normaliseLink(DI::baseUrl())) !== false);