Created
May 12, 2010 05:24
-
-
Save davidrecordon/398239 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// $input is a string. return value is a hash. | |
function parse_input($input) { | |
$url = @parse_url($input); | |
if (!$url) { | |
die("That isn't a valid identifier!\n"); | |
} | |
if (isset($url['user']) || isset($url['pass'])) { | |
die("We don't support username:password syntax.\n"); | |
} | |
if (isset($url['fragment'])) { | |
die("We don't support URL fragments.\n"); | |
} | |
if (isset($url['query'])) { | |
die("Query parameters in OpenIDs are a bad idea!\n"); | |
} | |
$identifier = array( | |
'scheme' => NULL, | |
'user' => NULL, | |
'domain' => NULL, | |
'port' => NULL, | |
'path' => NULL, | |
); | |
// email? | |
if (isset($url['path']) && strpos($url['path'], '@') !== false) { | |
list($user, $domain) = explode('@', $url['path']); | |
$identifier['scheme'] = 'acct'; | |
$identifier['user'] = $user; | |
$identifier['domain'] = $domain; | |
} else { // some form of URL | |
if (isset($url['host'])) { | |
$identifier['scheme'] = strtolower($url['scheme']); | |
$identifier['domain'] = $url['host']; | |
$identifier['port'] = isset($url['port']) ? $url['port'] : NULL; | |
$identifier['path'] = isset($url['path']) ? $url['path'] : NULL; | |
} elseif (isset($url['path'])) { // user didn't include a scheme | |
if (strpos($url['path'], '/') !== false) { | |
list($domain, $path) = explode('/', $url['path']); | |
} else { | |
$domain = $url['path']; | |
$path = NULL; | |
} | |
$identifier['scheme'] = 'http'; | |
$identifier['domain'] = $domain; | |
$identifier['port'] = isset($url['port']) ? $url['port'] : NULL; | |
$identifier['path'] = $path ? '/' . $path : NULL; | |
} | |
} | |
// make sure we got something | |
if (!$identifier['domain']) { | |
die("Not sure what you gave us: $input\n"); | |
} | |
// make sure we ended up with a valid scheme | |
if (!in_array($identifier['scheme'], array('http', 'https', 'acct'))) { | |
die("Invalid URI scheme\n"); | |
} | |
return $identifier; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment