OneLogin_Saml2_Utils::getSelfPort PHP Method

getSelfPort() public static method

public static getSelfPort ( ) : null | string
return null | string The port number used for the request
    public static function getSelfPort()
    {
        $portnumber = null;
        if (self::$_port) {
            $portnumber = self::$_port;
        } else {
            if (self::getProxyVars() && isset($_SERVER["HTTP_X_FORWARDED_PORT"])) {
                $portnumber = $_SERVER["HTTP_X_FORWARDED_PORT"];
            } else {
                if (isset($_SERVER["SERVER_PORT"])) {
                    $portnumber = $_SERVER["SERVER_PORT"];
                } else {
                    $currentHost = self::getRawHost();
                    // strip the port
                    if (false !== strpos($currentHost, ':')) {
                        list($currentHost, $port) = explode(':', $currentHost, 2);
                        if (is_numeric($port)) {
                            $portnumber = $port;
                        }
                    }
                }
            }
        }
        return $portnumber;
    }

Usage Example

Example #1
0
 /**
  * @covers OneLogin_Saml2_Utils::setBaseURL
  */
 public function testSetBaseURL()
 {
     $_SERVER['HTTP_HOST'] = 'sp.example.com';
     $_SERVER['HTTPS'] = 'https';
     $_SERVER['REQUEST_URI'] = '/example1/route.php?x=test';
     $_SERVER['QUERY_STRING'] = '?x=test';
     $_SERVER['SCRIPT_NAME'] = '/example1/route.php';
     unset($_SERVER['PATH_INFO']);
     $expectedUrlNQ = 'https://sp.example.com/example1/route.php';
     $expectedRoutedUrlNQ = 'https://sp.example.com/example1/route.php';
     $expectedUrl = 'https://sp.example.com/example1/route.php?x=test';
     OneLogin_Saml2_Utils::setBaseURL("no-valid-url");
     $this->assertEquals('https', OneLogin_Saml2_Utils::getSelfProtocol());
     $this->assertEquals('sp.example.com', OneLogin_Saml2_Utils::getSelfHost());
     $this->assertNull(OneLogin_Saml2_Utils::getSelfPort());
     $this->assertNull(OneLogin_Saml2_Utils::getBaseURLPath());
     $this->assertEquals($expectedUrlNQ, OneLogin_Saml2_Utils::getSelfURLNoQuery());
     $this->assertEquals($expectedRoutedUrlNQ, OneLogin_Saml2_Utils::getSelfRoutedURLNoQuery());
     $this->assertEquals($expectedUrl, OneLogin_Saml2_Utils::getSelfURL());
     OneLogin_Saml2_Utils::setBaseURL("http://anothersp.example.com:81/example2/");
     $expectedUrlNQ2 = 'http://anothersp.example.com:81/example2/route.php';
     $expectedRoutedUrlNQ2 = 'http://anothersp.example.com:81/example2/route.php';
     $expectedUrl2 = 'http://anothersp.example.com:81/example2/route.php?x=test';
     $this->assertEquals('http', OneLogin_Saml2_Utils::getSelfProtocol());
     $this->assertEquals('anothersp.example.com', OneLogin_Saml2_Utils::getSelfHost());
     $this->assertEquals('81', OneLogin_Saml2_Utils::getSelfPort());
     $this->assertEquals('/example2/', OneLogin_Saml2_Utils::getBaseURLPath());
     $this->assertEquals($expectedUrlNQ2, OneLogin_Saml2_Utils::getSelfURLNoQuery());
     $this->assertEquals($expectedRoutedUrlNQ2, OneLogin_Saml2_Utils::getSelfRoutedURLNoQuery());
     $this->assertEquals($expectedUrl2, OneLogin_Saml2_Utils::getSelfURL());
     $_SERVER['PATH_INFO'] = '/test';
     $expectedUrlNQ2 = 'http://anothersp.example.com:81/example2/route.php/test';
     $this->assertEquals($expectedUrlNQ2, OneLogin_Saml2_Utils::getSelfURLNoQuery());
     $this->assertEquals($expectedRoutedUrlNQ2, OneLogin_Saml2_Utils::getSelfRoutedURLNoQuery());
     $this->assertEquals($expectedUrl2, OneLogin_Saml2_Utils::getSelfURL());
 }