diff options
author | tailor <cygnus@janrain.com> | 2006-02-14 17:39:07 +0000 |
---|---|---|
committer | tailor <cygnus@janrain.com> | 2006-02-14 17:39:07 +0000 |
commit | 68b90212a91b021a94891f95fd47c4e18071eeaa (patch) | |
tree | 6d14c43dff9e5a48dd93e9062e615de78e4300d6 | |
parent | fe006ba8487441889c961d9e73bd9e7117ef03c8 (diff) | |
download | php-openid-68b90212a91b021a94891f95fd47c4e18071eeaa.zip php-openid-68b90212a91b021a94891f95fd47c4e18071eeaa.tar.gz php-openid-68b90212a91b021a94891f95fd47c4e18071eeaa.tar.bz2 |
[project @ Moved quoteMinimal, urlunparse, and normalizeUrl from Util to OpenID]
-rw-r--r-- | Auth/OpenID.php | 155 | ||||
-rw-r--r-- | Auth/OpenID/HTTPFetcher.php | 5 | ||||
-rw-r--r-- | Auth/OpenID/Util.php | 152 | ||||
-rw-r--r-- | Tests/Auth/OpenID/Util.php | 28 | ||||
-rw-r--r-- | examples/server/lib/actions.php | 4 |
5 files changed, 175 insertions, 169 deletions
diff --git a/Auth/OpenID.php b/Auth/OpenID.php index 67593b4..38d51be 100644 --- a/Auth/OpenID.php +++ b/Auth/OpenID.php @@ -226,6 +226,161 @@ class Auth_OpenID { return $url . $sep . Auth_OpenID::httpBuildQuery($args); } + + /** + * Turn a string into an ASCII string. + * + * Replace non-ascii characters with a %-encoded, UTF-8 + * encoding. This function will fail if the input is a string and + * there are non-7-bit-safe characters. It is assumed that the + * caller will have already translated the input into a Unicode + * character sequence, according to the encoding of the HTTP POST + * or GET. + * + * Do not escape anything that is already 7-bit safe, so we do the + * minimal transform on the identity URL + * + * @access private + */ + function quoteMinimal($s) + { + $res = array(); + for ($i = 0; $i < strlen($s); $i++) { + $c = $s[$i]; + if ($c >= "\x80") { + for ($j = 0; $j < count(utf8_encode($c)); $j++) { + array_push($res, sprintf("%02X", ord($c[$j]))); + } + } else { + array_push($res, $c); + } + } + + return implode('', $res); + } + + /** + * Implements python's urlunparse, which is not available in PHP. + * Given the specified components of a URL, this function rebuilds + * and returns the URL. + * + * @access private + * @param string $scheme The scheme (e.g. 'http'). Defaults to 'http'. + * @param string $host The host. Required. + * @param string $port The port. + * @param string $path The path. + * @param string $query The query. + * @param string $fragment The fragment. + * @return string $url The URL resulting from assembling the + * specified components. + */ + function urlunparse($scheme, $host, $port = null, $path = '/', + $query = '', $fragment = '') + { + + if (!$scheme) { + $scheme = 'http'; + } + + if (!$host) { + return false; + } + + if (!$path) { + $path = '/'; + } + + $result = $scheme . "://" . $host; + + if ($port) { + $result .= ":" . $port; + } + + $result .= $path; + + if ($query) { + $result .= "?" . $query; + } + + if ($fragment) { + $result .= "#" . $fragment; + } + + return $result; + } + + /** + * Given a URL, this "normalizes" it by adding a trailing slash + * and / or a leading http:// scheme where necessary. Returns + * null if the original URL is malformed and cannot be normalized. + * + * @access private + * @param string $url The URL to be normalized. + * @return mixed $new_url The URL after normalization, or null if + * $url was malformed. + */ + function normalizeUrl($url) + { + if ($url === null) { + return null; + } + + assert(is_string($url)); + + $old_url = $url; + $url = trim($url); + + if (strpos($url, "://") === false) { + $url = "http://" . $url; + } + + $parsed = @parse_url($url); + + if ($parsed === false) { + return null; + } + + $defaults = array( + 'scheme' => '', + 'host' => '', + 'path' => '', + 'query' => '', + 'fragment' => '', + 'port' => '' + ); + + $parsed = array_merge($defaults, $parsed); + + if (($parsed['scheme'] == '') || + ($parsed['host'] == '')) { + if ($parsed['path'] == '' && + $parsed['query'] == '' && + $parsed['fragment'] == '') { + return null; + } + + $url = 'http://' + $url; + $parsed = parse_url($url); + + $parsed = array_merge($defaults, $parsed); + } + + $tail = array_map(array('Auth_OpenID', 'quoteMinimal'), + array($parsed['path'], + $parsed['query'], + $parsed['fragment'])); + if ($tail[0] == '') { + $tail[0] = '/'; + } + + $url = Auth_OpenID::urlunparse($parsed['scheme'], $parsed['host'], + $parsed['port'], $tail[0], $tail[1], + $tail[2]); + + assert(is_string($url)); + + return $url; + } } ?>
\ No newline at end of file diff --git a/Auth/OpenID/HTTPFetcher.php b/Auth/OpenID/HTTPFetcher.php index 7fac490..1755873 100644 --- a/Auth/OpenID/HTTPFetcher.php +++ b/Auth/OpenID/HTTPFetcher.php @@ -13,9 +13,10 @@ */ /** - * Require the parser. + * Require the parser and OpenID util functions. */ require_once "Auth/OpenID/Parse.php"; +require_once "Auth/OpenID.php"; /** * This is the status code beginAuth returns when it is unable to @@ -123,7 +124,7 @@ class Auth_OpenID_HTTPFetcher { */ function findIdentityInfo($identity_url) { - $url = Auth_OpenID_normalizeURL($identity_url); + $url = Auth_OpenID::normalizeURL($identity_url); $ret = @$this->get($url); if ($ret === null) { return array(Auth_OpenID_HTTP_FAILURE, null); diff --git a/Auth/OpenID/Util.php b/Auth/OpenID/Util.php index 8b9b3bc..0121c21 100644 --- a/Auth/OpenID/Util.php +++ b/Auth/OpenID/Util.php @@ -20,157 +20,5 @@ $_Auth_OpenID_letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; $_Auth_OpenID_digits = "0123456789"; $_Auth_OpenID_punct = "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"; -/** - * Turn a string into an ASCII string. - * - * Replace non-ascii characters with a %-encoded, UTF-8 encoding. This - * function will fail if the input is a string and there are - * non-7-bit-safe characters. It is assumed that the caller will have - * already translated the input into a Unicode character sequence, - * according to the encoding of the HTTP POST or GET. - * - * Do not escape anything that is already 7-bit safe, so we do the - * minimal transform on the identity URL - * - * @access private - */ -function Auth_OpenID_quoteMinimal($s) -{ - $res = array(); - for ($i = 0; $i < strlen($s); $i++) { - $c = $s[$i]; - if ($c >= "\x80") { - for ($j = 0; $j < count(utf8_encode($c)); $j++) { - array_push($res, sprintf("%02X", ord($c[$j]))); - } - } else { - array_push($res, $c); - } - } - - return implode('', $res); -} - -/** - * Implements python's urlunparse, which is not available in PHP. - * Given the specified components of a URL, this function rebuilds and - * returns the URL. - * - * @access private - * @param string $scheme The scheme (e.g. 'http'). Defaults to 'http'. - * @param string $host The host. Required. - * @param string $port The port. - * @param string $path The path. - * @param string $query The query. - * @param string $fragment The fragment. - * @return string $url The URL resulting from assembling the specified - * components. - */ -function Auth_OpenID_urlunparse($scheme, $host, $port = null, $path = '/', - $query = '', $fragment = '') -{ - - if (!$scheme) { - $scheme = 'http'; - } - - if (!$host) { - return false; - } - - if (!$path) { - $path = '/'; - } - - $result = $scheme . "://" . $host; - - if ($port) { - $result .= ":" . $port; - } - - $result .= $path; - - if ($query) { - $result .= "?" . $query; - } - - if ($fragment) { - $result .= "#" . $fragment; - } - - return $result; -} - -/** - * Given a URL, this "normalizes" it by adding a trailing slash and / - * or a leading http:// scheme where necessary. Returns null if the - * original URL is malformed and cannot be normalized. - * - * @access private - * @param string $url The URL to be normalized. - * @return mixed $new_url The URL after normalization, or null if $url - * was malformed. - */ -function Auth_OpenID_normalizeUrl($url) -{ - if ($url === null) { - return null; - } - - assert(is_string($url)); - - $old_url = $url; - $url = trim($url); - - if (strpos($url, "://") === false) { - $url = "http://" . $url; - } - - $parsed = @parse_url($url); - - if ($parsed === false) { - return null; - } - - $defaults = array( - 'scheme' => '', - 'host' => '', - 'path' => '', - 'query' => '', - 'fragment' => '', - 'port' => '' - ); - - $parsed = array_merge($defaults, $parsed); - - if (($parsed['scheme'] == '') || - ($parsed['host'] == '')) { - if ($parsed['path'] == '' && - $parsed['query'] == '' && - $parsed['fragment'] == '') { - return null; - } - - $url = 'http://' + $url; - $parsed = parse_url($url); - - $parsed = array_merge($defaults, $parsed); - } - - $tail = array_map('Auth_OpenID_quoteMinimal', array($parsed['path'], - $parsed['query'], - $parsed['fragment'])); - if ($tail[0] == '') { - $tail[0] = '/'; - } - - $url = Auth_OpenID_urlunparse($parsed['scheme'], $parsed['host'], - $parsed['port'], $tail[0], $tail[1], - $tail[2]); - - assert(is_string($url)); - - return $url; -} ?>
\ No newline at end of file diff --git a/Tests/Auth/OpenID/Util.php b/Tests/Auth/OpenID/Util.php index 659e7cf..9ad024e 100644 --- a/Tests/Auth/OpenID/Util.php +++ b/Tests/Auth/OpenID/Util.php @@ -78,35 +78,35 @@ class Tests_Auth_OpenID_Util extends PHPUnit_TestCase { function test_normalizeUrl() { $this->assertEquals("http://foo.com/", - Auth_OpenID_normalizeUrl("foo.com")); + Auth_OpenID::normalizeUrl("foo.com")); $this->assertEquals("http://foo.com/", - Auth_OpenID_normalizeUrl("http://foo.com")); + Auth_OpenID::normalizeUrl("http://foo.com")); $this->assertEquals("https://foo.com/", - Auth_OpenID_normalizeUrl("https://foo.com")); + Auth_OpenID::normalizeUrl("https://foo.com")); $this->assertEquals("http://foo.com/bar", - Auth_OpenID_normalizeUrl("foo.com/bar")); + Auth_OpenID::normalizeUrl("foo.com/bar")); $this->assertEquals("http://foo.com/bar", - Auth_OpenID_normalizeUrl("http://foo.com/bar")); + Auth_OpenID::normalizeUrl("http://foo.com/bar")); $this->assertEquals("http://foo.com/", - Auth_OpenID_normalizeUrl("http://foo.com/")); + Auth_OpenID::normalizeUrl("http://foo.com/")); $this->assertEquals("https://foo.com/", - Auth_OpenID_normalizeUrl("https://foo.com/")); + Auth_OpenID::normalizeUrl("https://foo.com/")); $this->assertEquals("https://foo.com/bar" , - Auth_OpenID_normalizeUrl("https://foo.com/bar")); + Auth_OpenID::normalizeUrl("https://foo.com/bar")); if (0) { $this->assertEquals("http://foo.com/%E8%8D%89", - Auth_OpenID_normalizeUrl("foo.com/\u8349")); + Auth_OpenID::normalizeUrl("foo.com/\u8349")); $this->assertEquals("http://foo.com/%E8%8D%89", - Auth_OpenID_normalizeUrl("http://foo.com/\u8349")); + Auth_OpenID::normalizeUrl("http://foo.com/\u8349")); } $non_ascii_domain_cases = array( @@ -141,16 +141,16 @@ class Tests_Auth_OpenID_Util extends PHPUnit_TestCase { for expected, case in non_ascii_domain_cases: try: -actual = Auth_OpenID_normalizeUrl(case) +actual = Auth_OpenID::normalizeUrl(case) except UnicodeError: assert should_raise else: assert not should_raise and actual == expected, case */ - $this->assertNull(Auth_OpenID_normalizeUrl(null)); - $this->assertNull(Auth_OpenID_normalizeUrl('')); - $this->assertNull(Auth_OpenID_normalizeUrl('http://')); + $this->assertNull(Auth_OpenID::normalizeUrl(null)); + $this->assertNull(Auth_OpenID::normalizeUrl('')); + $this->assertNull(Auth_OpenID::normalizeUrl('http://')); } function test_appendArgs() diff --git a/examples/server/lib/actions.php b/examples/server/lib/actions.php index b7194c7..546bdc6 100644 --- a/examples/server/lib/actions.php +++ b/examples/server/lib/actions.php @@ -7,6 +7,8 @@ require_once "lib/render.php"; require_once "lib/render/login.php"; require_once "lib/render/sites.php"; +require_once "Auth/OpenID.php"; + /** * Handle a standard OpenID server request */ @@ -42,7 +44,7 @@ function login_checkInput($input) } if (count($errors) == 0) { $openid_url = $input['openid_url']; - $openid_url = Auth_OpenID_normalizeUrl($openid_url); + $openid_url = Auth_OpenID::normalizeUrl($openid_url); $password = $input['password']; if (!checkLogin($openid_url, $password)) { $errors[] = 'The entered password does not match the ' . |