Skip to content

Instantly share code, notes, and snippets.

@davidrecordon
Created May 12, 2010 05:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidrecordon/398239 to your computer and use it in GitHub Desktop.
Save davidrecordon/398239 to your computer and use it in GitHub Desktop.
<?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