0) { $baseUrl = substr(ACYM_LIVE, 0, strrpos(ACYM_LIVE, $urls['path'])).'/'; $otherArguments = trim(str_replace($baseUrl, '', ACYM_LIVE), '/'); if (strlen($otherArguments) > 0) { $otherArguments .= '/'; } } else { $baseUrl = ACYM_LIVE; } } if ($otherArguments && strpos($link, $otherArguments) === false) { $link = $otherArguments.$link; } return $baseUrl; } function acym_currentURL(): string { $protocol = isset($_SERVER['HTTPS']) || !empty($_SERVER['HTTP_UPGRADE_INSECURE_REQUESTS']) ? 'https' : 'http'; $httpHost = acym_getVar('string', 'HTTP_HOST', '', 'SERVER'); $serverName = acym_getVar('string', 'SERVER_NAME', '', 'SERVER'); $host = empty($httpHost) ? ( empty($serverName) ? getenv('HTTP_HOST') : $serverName ) : $httpHost; $requestUri = acym_getVar('string', 'REQUEST_URI', '', 'SERVER'); return $protocol.'://'.$host.$requestUri; } function acym_cleanUrl(string $url, array $parametersToRemove): string { $parts = acym_parseUrl($url); if (empty($parts['query'])) { return $url; } $queryParams = []; parse_str($parts['query'], $queryParams); foreach ($parametersToRemove as $parameter) { unset($queryParams[$parameter]); } return $parts['scheme'].'://'.$parts['host'].$parts['path'].'?'.http_build_query($queryParams); } function acym_isLocalWebsite(): bool { return strpos(ACYM_LIVE, 'localhost') !== false || strpos(ACYM_LIVE, '127.0.0.1') !== false; } function acym_internalUrlToPath(string $url): string { $base = str_replace(['http://www.', 'https://www.', 'http://', 'https://'], '', ACYM_LIVE); $replacements = ['https://www.'.$base, 'http://www.'.$base, 'https://'.$base, 'http://'.$base]; foreach ($replacements as $oneReplacement) { $position = strpos($url, $oneReplacement); if ($position === false) { continue; } // Remove parameters $relativeUrl = substr($url, $position + strlen($oneReplacement)); $relativeUrl = preg_replace('/[?#].*$/', '', $relativeUrl); // Remove encoded / and . $segments = []; foreach (explode('/', $relativeUrl) as $oneSegment) { $decodedSegment = urldecode($oneSegment); if (preg_match('#[/\\\\?\#]#', $decodedSegment) === 1 || in_array($decodedSegment, ['.', '..'], true)) { return $url; } $segments[] = $decodedSegment; } return ACYM_ROOT.implode(DS, $segments); } return $url; } function acym_isValidUrl(string $url): bool { // If this option is not activated in the php.ini file, we return true because the function get_headers will not work if (empty(ini_get('allow_url_fopen'))) { return true; } // If we detect youtu.be url, we don't check the headers because it's a redirection to a valid Url if (strpos($url, 'youtu.be') !== false) { return true; } $headers = @get_headers($url); return !empty($headers) && strpos($headers[0], '200'); } function acym_isImageUrl(string $url): bool { $extension = strtolower(pathinfo($url, PATHINFO_EXTENSION)); return in_array($extension, acym_getImageFileExtensions(true)); } function acym_checkRedirect(string $redirectUrl): bool { $config = acym_config(); $allowedHosts = $config->get('allowed_hosts', ''); $allowedHosts = str_replace(',', '|', $allowedHosts); $allowedHosts = str_replace(['https://', 'http://', 'www.'], '', $allowedHosts); $regex = trim(preg_replace('#[^a-z0-9\|\.]#i', '', $allowedHosts), '|'); if (empty($regex) || $regex == 'all' || empty($redirectUrl)) return true; preg_match('#^(https?://)?(www.)?([^/]*)#i', $redirectUrl, $resultsurl); $domainredirect = preg_replace('#[^a-z0-9\.]#i', '', @$resultsurl[3]); if (preg_match('#^'.$regex.'$#i', $domainredirect)) return true; return false; } // A redirect target coming from the request may only leave the site when the admin listed the host, acym_checkRedirect alone allows everything as long as allowed_hosts is empty function acym_isTrustedRedirect(string $redirectUrl): bool { if ($redirectUrl === '') { return true; } // Browsers read //host and /\host as protocol relative, parse_url only reports a host for the first one if (preg_match('~^/[/\\\\]~', $redirectUrl)) { return false; } $parsedUrl = acym_parseUrl($redirectUrl); if ($parsedUrl === false) { return false; } $host = $parsedUrl['host'] ?? ''; if (empty($host)) { return true; } $siteUrl = acym_parseUrl(ACYM_LIVE); if (!empty($siteUrl['host']) && strcasecmp($host, $siteUrl['host']) === 0) { return true; } return trim(acym_config()->get('allowed_hosts', '')) !== '' && acym_checkRedirect($redirectUrl); }