public static function getCurrentUrl(bool $withQuery = true): string
    {
        static $filter, $scheme, $host, $port;
        if ($filter == null) {
            $filter = function($input) {
                // decode first @important
                $input = rawurldecode($input);

                // encode quotes and html tags
                return html_encode(
                    // remove NUL-byte, ctrl-z, vertical tab
                    preg_replace('~[\x00\x1a\x0b]|%(?:00|1a|0b)~i', '', trim(
                        // slice at \n or \r
                        substr($input, 0, strcspn($input, "\n\r"))
                    ))
                );
            };

            [$scheme, $host, $port] = [
                $_SERVER['REQUEST_SCHEME'], $_SERVER['SERVER_NAME'], $_SERVER['SERVER_PORT']
            ];
        }


        $path = $_SERVER['REQUEST_URI'];
        $query = '';
        if (strpos($path, '?')) {
            [$path, $query] = explode('?', $path, 2);
        }

        $port = ($scheme != 'https' && $port != '80') ? ':'. $port : '';
        $path = $filter($path);
        if (strpos($path, '//') !== false) {
            $path = preg_replace('~/+~', '/', $path);
        }

        $url = sprintf('%s://%s%s%s', $scheme, $host, $port, $path);
        if ($withQuery && $query != '') {
            $url .= '?'. $filter($query);
        }

        return $url;
    }