get_var("SELECT blog_id FROM $wpdb->blogs WHERE domain = '$the_domain'"); if ($blog_id) { switch_to_blog($blog_id); // Call the function from rewrite.php to get the best chance of finding the $post_id = url_to_postid($url); restore_current_blog(); // passing 0 for post_id is ok, because in that case we just // make a short url for the blog! $out = kmxt_shorturl_from_ids($blog_id,$post_id); } // now, if somehow the short url is longer than the original // then, well, we return the original. if (strlen($out) > strlen($url)) $out=$url; return $out; } /****************************************************************** * Function: kmxt_shorturl_from_ids($blog_id,$post_id) * Purpose: creates a short url for the blog/post combination */ function kmxt_shorturl_from_ids($blog_id,$post_id) { $blog_encode = kmxt_base35encode($blog_id); // blog part only // Note: leading ! is used to differentiate from actual // urls in the master domain name space. $out = ( $_SERVER['HTTPS'] ? 'https://' : 'http://'). ( KMXT_SHORTURL_MASTER_DOMAIN ). ( $_SERVER['SERVER_PORT']==80 ? '': $_SERVER['SERVER_PORT'] ). ( '/'.KMXT_SHORTURL_STUB.$blog_encode ); if ($post_id>0) { $post_encode = kmxt_base35encode($post_id); // Note: leading ! is used to indicat that this // is a post. In the future we may use this // leading character to indicate links to // tags, categories, or date ranges. But for // now we only support posts $out .= '/'.KMXT_SHORTURL_POSTID_STUB.$post_encode; } return $out; } /****************************************************************** * Function: kmxt_getShortURL_from_ids($blog_id,$post_id) * Purpose: creates a short url for the blog/post combination */ function kmxt_shorturl_to_url($shorturl) { $out = ''; $parsed = parse_url($shorturl); $host = $parsed['host']; $path = $parsed['path']; // Check that we match our expected format, if not, then return // the original url to our caller if (0 !== stripos($path,'/'.KMXT_SHORTURL_STUB) ) return $shorturl; list($stub,$blog_part,$post_part) = explode('/',$path); $blog_part_trimmed = substr($blog_part,strlen(KMXT_SHORTURL_STUB)); $post_part_trimmed = substr($post_part,strlen(KMXT_SHORTURL_POSTID_STUB)); $blog_id = kmxt_base35decode($blog_part_trimmed); $post_id = kmxt_base35decode($post_part_trimmed); // VHOST ONLY! global $wpdb; $domain = $wpdb->get_var("SELECT domain FROM $wpdb->blogs WHERE blog_id = '$blog_id'"); if ($domain) { $out = "http://$domain/"; if ($post_id) { $out .= "?p=$post_id"; } } return $out; } /****************************************************************** * Function: kmxt_base35encode() * Purpose: converts a number into a 'base35' representation. */ function kmxt_base35encode($value) { $out = ''; $base35chars = KMXT_SHORTURL_BASE_CHARS; while($value > 0) { $floor = floor($value / KMXT_SHORTURL_BASE_NUM); $times = $floor*KMXT_SHORTURL_BASE_NUM; $remainder = $value - $times; $char = substr($base35chars,$remainder,1); $out .= $char; $value = ($value-$remainder)/KMXT_SHORTURL_BASE_NUM; } return $out; } /****************************************************************** * Function: kmxt_base35decode() * Purpose: converts a number into a 'base35' representation. */ function kmxt_base35decode($value) { $out = 0; $base35chars = KMXT_SHORTURL_BASE_CHARS; $len = strlen($value); for ($i=($len-1); $i >= 0; $i--) { $char = substr($value,$i,1); $charvalue = strpos($base35chars,$char); $out = ($out*KMXT_SHORTURL_BASE_NUM) + $charvalue; } return $out; } /****************************************************************** * Function: kmxt_test_shorturl() * Purpose: testing function */ //add_action('wp_footer','kmxt_test_shorturl'); function kmxt_test_shorturl() { echo "
"; $tests = Array(9999999999,999999999,99999999,9999999,999999,99999,9999,999); foreach ($tests as $test) { $encode_result = kmxt_base35encode($test); echo "

kmxt_base35encode($test)=$encode_result

"; $decode_result = kmxt_base35decode($encode_result); echo "

kmxt_base35decode($encode_result)=$decode_result

"; } $tests = Array( 'qwrty','bcdfg','brd','102'); foreach ($tests as $test) { $decode_result = kmxt_base35decode($test); echo "

kmxt_base35decode($test)=$decode_result

"; $encode_result = kmxt_base35encode($decode_result); echo "

kmxt_base35encode($decode_result)=$encode_result

"; } $url_tests = Array( 'http://'.KMXT_SHORTURL_MASTER_DOMAIN.'/about/' ); foreach ($url_tests as $url) { $short_url = kmxt_url_to_shorturl($url); echo "

kmxt_url_to_shorturl($url)=$short_url

"; $long_url = kmxt_shorturl_to_url($short_url); echo "

kmxt_shorturl_to_url($short_url)=$long_url

"; } echo "
"; } ?>