// CS_ExtractURL() swiped from Ultimate Web Scraper Toolkit. Renamed for no-conflict.
// RFC 3986 delimeter splitting implementation.
function CS_ExtractURL($url)
{
$result = array(
"scheme" => "",
"authority" => "",
"login" => "",
"loginusername" => "",
"loginpassword" => "",
"host" => "",
"port" => "",
"path" => "",
"query" => "",
"queryvars" => array(),
"fragment" => ""
);
$pos = strpos($url, "#");
if ($pos !== false)
{
$result["fragment"] = substr($url, $pos + 1);
$url = substr($url, 0, $pos);
}
$pos = strpos($url, "?");
if ($pos !== false)
{
$result["query"] = substr($url, $pos + 1);
$url = substr($url, 0, $pos);
$vars = explode("&", $result["query"]);
foreach ($vars as $var)
{
$pos = strpos($var, "=");
if ($pos === false)
{
$name = $var;
$value = "";
}
else
{
$name = substr($var, 0, $pos);
$value = substr($var, $pos + 1);
}
if (!isset($result["queryvars"][urldecode($name)])) $result["queryvars"][urldecode($name)] = array();
$result["queryvars"][urldecode($name)][] = urldecode($value);
}
}
$url = str_replace("\\", "/", $url);
$pos = strpos($url, ":");
if ($pos !== false)
{
$result["scheme"] = substr($url, 0, $pos);
$url = substr($url, $pos + 1);
}
if (substr($url, 0, 2) != "//") $result["path"] = $url;
else
{
$url = substr($url, 2);
$pos = strpos($url, "/");
if ($pos !== false)
{
$result["path"] = substr($url, $pos);
$url = substr($url, 0, $pos);
}
$result["authority"] = $url;
$pos = strpos($url, "@");
if ($pos !== false)
{
$result["login"] = substr($url, 0, $pos);
$url = substr($url, $pos + 1);
$pos = strpos($result["login"], ":");
if ($pos === false) $result["loginusername"] = $result["login"];
else
{
$result["loginusername"] = substr($result["login"], 0, $pos);
$result["loginpassword"] = substr($result["login"], $pos + 1);
}
}
$pos = strpos($url, ":");
if ($pos !== false)
{
$result["port"] = substr($url, $pos + 1);
$url = substr($url, 0, $pos);
}
$result["host"] = $url;
}
return $result;
}
// Converts WP permalink to useful, structured information.
function TNI_url_to_internal_info($url)
{
global $wpdb;
$url2 = CS_ExtractURL($url);
$path = explode("/", $url2["path"]);
array_shift($path);
// Rip out the name from subdomain or path to get the blog ID.
if ( is_subdomain_install() ) {
if (strpos($url2["host"], $current_site->domain) !== false) $name = substr($url2["host"], 0, strpos($url2["host"], $current_site->domain));
else $name = "";
if (substr($name, -1) == ".") $name = substr($name, 0, -1);
} else if ( count($path) ) {
$name = array_shift($path);
} else {
$name = "";
}
$blog_id = get_id_from_blogname($name);
// Determine type.
$id = 0;
$type = "";
if (count($path))
{
if ($path[0] == "files") {
$type = "file";
} else if ($blog_id) {
// Try assuming blog and getting post ID.
switch_to_blog($blog_id);
$id = url_to_postid($url);
if ($id > 0)
{
$type2 = $wpdb->get_var($wpdb->prepare("SELECT post_type FROM {$wpdb->posts} WHERE ID = %d", $id));
if (is_string($type2)) $type = $type2;
}
restore_current_blog();
}
}
$result = array(
"blog_id" => $blog_id,
"type" => $type,
"type_id" => $id
);
return $result;
}