Retrieves the Uri port using incoming data, ignores a forced port.
If unable to retrieve port, returns 80 by default.

@param array $server Optional; null by default or empty array means
global $_SERVER. The source data

@return int The Uri port
333493
public static function retrieve(array $server = null)
    {
        if (empty($server)) {
            $server = $_SERVER;
        }

        $port = '80';

        if (isset($server['SERVER_PORT'])) {
            $port = $server['SERVER_PORT'];
        }

        return (int) $port;
    }
1058023
public function getServer($server)
    {
        if (! array_key_exists($server, $this->servers)) {
            throw new \Exception('Server ['.$server.'] is not defined.');
        }

        return Arr::get($this->servers, $server);
    }
117885
protected function getServerParam($key, $default = null)
    {
        if (isset($this->server[$key])) {
            return $this->server[$key];
        }

        return $default;
    }
469760
public static function getProtocol(array $server): string
    {
        if (!isset($server['SERVER_PROTOCOL'])) {
            return '1.1';
        }

        if (!\preg_match('#^(HTTP/)?(?P<version>[1-9]\d*(?:\.\d)?)$#', $server['SERVER_PROTOCOL'], $matches)) {
            throw new \UnexpectedValueException("Unrecognized protocol version ({$server['SERVER_PROTOCOL']})");
        }

        return $matches['version'];
    }
638937
public function get(string $server)
  {
    return isset($this->all[$server]) ? $this->all[$server] : null;
  }
10306
public static function serverPort()
    {
        if ( empty( $GLOBALS['eZSysServerPort'] ) )
        {
            $hostname = self::hostname();
            if ( preg_match( "/.*:([0-9]+)/", $hostname, $regs ) )
            {
                $port = (int) $regs[1];
            }
            else
            {
                $port = (int) self::serverVariable( 'SERVER_PORT', true );
            }

            if ( !$port )
            {
                $port = 80;
            }

            $GLOBALS['eZSysServerPort'] = $port;
        }
        return $GLOBALS['eZSysServerPort'];
    }
764980
public function createUriFromServer(array $server): UriInterface
        {
            // scheme, host, post, user, pass
            $scheme = (empty($server['HTTPS']) || $server['HTTPS'] === 'off') ? 'http' : 'https';
            $host   = (isset($server['HTTP_HOST'])) ? $server['HTTP_HOST'] : $server['SERVER_NAME'];
            $port   = (isset($server['SERVER_PORT'])) ? (int) $server['SERVER_PORT'] : 80;
            $user   = (isset($server['PHP_AUTH_USER'])) ? $server['PHP_AUTH_USER'] : '';
            $pass   = (isset($server['PHP_AUTH_PW'])) ? $server['PHP_AUTH_PW'] : '';
            
            // path
            $path = parse_url('http://example.com' . $server['REQUEST_URI'], PHP_URL_PATH);
            $path = rawurldecode($path);

            // query
            $query = (isset($server['QUERY_STRING'])) ? $server['QUERY_STRING'] : '';

            // fragment (not available in $_SERVER)
            $fragment = '';

            $uri = new Uri('');
            $uri = $uri->withScheme($scheme);
            $uri = $uri->withHost($host);
            $uri = $uri->withPort($port);
            $uri = $uri->withUserInfo($user, $pass);
            $uri = $uri->withPath($path);
            $uri = $uri->withQuery($query);
            $uri = $uri->withFragment($fragment);
            return $uri;
        }
508365
public function getPort($port_num)
    {
        $ports = $this->server->ports;
        foreach ($ports as $port) {
            if ($port->port == $port_num) {
                return $port;
            }
        }

        return null;
    }
141411
private static function fetchHostname(array $server): array
    {
        $server += ['SERVER_PORT' => null];
        if (null !== $server['SERVER_PORT']) {
            $server['SERVER_PORT'] = (int) $server['SERVER_PORT'];
        }

        if (isset($server['HTTP_HOST'])) {
            preg_match(',^(?<host>(\[.*\]|[^:])*)(\:(?<port>[^/?\#]*))?$,x', $server['HTTP_HOST'], $matches);

            return [
                $matches['host'],
                isset($matches['port']) ? (int) $matches['port'] : $server['SERVER_PORT'],
            ];
        }

        if (!isset($server['SERVER_ADDR'])) {
            throw new SyntaxError('The host could not be detected');
        }

        if (false === filter_var($server['SERVER_ADDR'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
            $server['SERVER_ADDR'] = '['.$server['SERVER_ADDR'].']';
        }

        return [$server['SERVER_ADDR'], $server['SERVER_PORT']];
    }
510911
private function determineRemoteIpAddress(array $server): string
    {
        if (array_key_exists('HTTP_X_FORWARDED_FOR', $server) && filter_var($server['HTTP_X_FORWARDED_FOR'], FILTER_VALIDATE_IP)) {
            return $server['HTTP_X_FORWARDED_FOR'];
        } elseif (array_key_exists('HTTP_CLIENT_IP', $server) && filter_var($server['HTTP_CLIENT_IP'], FILTER_VALIDATE_IP)) {
            return $server['HTTP_CLIENT_IP'];
        } elseif (array_key_exists('HTTP_TRUE_CLIENT_IP', $server) && filter_var($server['HTTP_TRUE_CLIENT_IP'], FILTER_VALIDATE_IP)) {
            return $server['HTTP_TRUE_CLIENT_IP'];
        }

        return $server['REMOTE_ADDR'];
    }
577893
protected static function resolveHostPort($server)
    {
        if (isset($server['HTTP_HOST'])) {
            $host = $server['HTTP_HOST'];
        } elseif (isset($server['SERVER_NAME'])) {
            $host = $server['SERVER_NAME'];
        } else {
            throw new InvalidArgumentException(
                'Undefined HTTP host in server params'
            );
        }

        $port = null;

        if (isset($server['SERVER_PORT'])) {
            $port = $server['SERVER_PORT'];
        }

        if (preg_match('/:(\d+)$/', $host, $matches)) {
            $host = rtrim($host, ":{$matches[1]}");
            $port = (int)$matches[1];
        }

        return $host . ($port !== null ? ":$port" : '');
    }
1217634
public static function createUri(array $server = [])
    {
        $server += $_SERVER;
        $server = static::normalizeServer($server);
        $headers = static::marshalHeaders($server);

        return static::marshalUriFromServer($server, $headers);
    }
909672
protected static function getInputParams(array $server, array $post)
    {
        static $result;

        if ($result !== null) {
            return $result ?: null;
        }

        $result = $post ?: static::parseInput($server, static::readInput());

        return $result ?: null;
    }
67171
public function parseUrl(array $server) {
        $protocol = array_has($server, 'HTTPS') ? 'https' : 'http';
        $host = array_get($server, 'HTTP_HOST');
        $uri = array_get($server, 'REQUEST_URI');

        return new Url(s('%s://%s%s', $protocol, $host, $uri));
    }
806348
public static function make(array $server = null)
    {
        if (empty($server)) {
            $server = $_SERVER;
        }

        if (isset($server['SERVER_PROTOCOL'])) {
            return $server['SERVER_PROTOCOL'];
        }

        return 'HTTP/1.1';
    }
512171
public static function port(HTTPRequest $request = null)
    {
        $host = static::host($request);
        return (int)parse_url($host, PHP_URL_PORT) ?: null;
    }
886032
protected function getPort($default = 3000)
    {
        $ports = [];
        foreach ($this->getServerInfo() as $address => $server) {
            $ports[] = $server['port'];
        }

        return PortUtil::getPort($ports ? max($ports) + 1 : $default);
    }