+function common_render_uri_thingy($matches) {
+ $uri = $matches[0];
+ $trailer = '';
+
+ # Some heuristics for extracting URIs from surrounding punctuation
+ # Strip from trailing text...
+ if (preg_match('/^(.*)([,.:"\']+)$/', $uri, $matches)) {
+ $uri = $matches[1];
+ $trailer = $matches[2];
+ }
+
+ $pairs = array(
+ ']' => '[', # technically disallowed in URIs, but used in Java docs
+ ')' => '(', # far too frequent in Wikipedia and MSDN
+ );
+ $final = substr($uri, -1, 1);
+ if (isset($pairs[$final])) {
+ $openers = substr_count($uri, $pairs[$final]);
+ $closers = substr_count($uri, $final);
+ if ($closers > $openers) {
+ // Assume the paren was opened outside the URI
+ $uri = substr($uri, 0, -1);
+ $trailer = $final . $trailer;
+ }
+ }
+ if ($longurl = common_longurl($uri)) {
+ $longurl = htmlentities($longurl, ENT_QUOTES, 'UTF-8');
+ $title = " title='$longurl'";
+ }
+ else $title = '';
+
+ return '<a href="' . $uri . '"' . $title . ' class="extlink">' . $uri . '</a>' . $trailer;
+}
+
+function common_longurl($short_url) {
+ $long_url = common_shorten_link($short_url, true);
+ if ($long_url === $short_url) return false;
+ return $long_url;
+}
+
+function common_longurl2($uri) {
+ $uri_e = urlencode($uri);
+ $longurl = unserialize(file_get_contents("http://api.longurl.org/v1/expand?format=php&url=$uri_e"));
+ if (empty($longurl['long_url']) || $uri === $longurl['long_url']) return false;
+ return stripslashes($longurl['long_url']);
+}
+
+function common_shorten_links($text) {
+ if (mb_strlen($text) <= 140) return $text;
+ static $cache = array();
+ if (isset($cache[$text])) return $cache[$text];
+ // \s = not a horizontal whitespace character (since PHP 5.2.4)
+ return $cache[$text] = preg_replace('@https?://[^)\]>\s]+@e', "common_shorten_link('\\0')", $text);
+}
+
+function common_shorten_link($url, $reverse = false) {
+ static $url_cache = array();
+ if ($reverse) return isset($url_cache[$url]) ? $url_cache[$url] : $url;
+
+ $user = common_current_user();
+
+ $curlh = curl_init();
+ curl_setopt($curlh, CURLOPT_CONNECTTIMEOUT, 20); // # seconds to wait
+ curl_setopt($curlh, CURLOPT_USERAGENT, 'Laconica');
+ curl_setopt($curlh, CURLOPT_RETURNTRANSFER, true);
+
+ switch($user->urlshorteningservice) {
+ case 'ur1.ca':
+ $short_url_service = new LilUrl;
+ $short_url = $short_url_service->shorten($url);
+ break;
+
+ case '2tu.us':
+ $short_url_service = new TightUrl;
+ $short_url = $short_url_service->shorten($url);
+ break;
+
+ case 'ptiturl.com':
+ $short_url_service = new PtitUrl;
+ $short_url = $short_url_service->shorten($url);
+ break;
+
+ case 'bit.ly':
+ curl_setopt($curlh, CURLOPT_URL, 'http://bit.ly/api?method=shorten&long_url='.urlencode($url));
+ $short_url = current(json_decode(curl_exec($curlh))->results)->hashUrl;
+ break;
+
+ case 'is.gd':
+ curl_setopt($curlh, CURLOPT_URL, 'http://is.gd/api.php?longurl='.urlencode($url));
+ $short_url = curl_exec($curlh);
+ break;
+ case 'snipr.com':
+ curl_setopt($curlh, CURLOPT_URL, 'http://snipr.com/site/snip?r=simple&link='.urlencode($url));
+ $short_url = curl_exec($curlh);
+ break;
+ case 'metamark.net':
+ curl_setopt($curlh, CURLOPT_URL, 'http://metamark.net/api/rest/simple?long_url='.urlencode($url));
+ $short_url = curl_exec($curlh);
+ break;
+ case 'tinyurl.com':
+ curl_setopt($curlh, CURLOPT_URL, 'http://tinyurl.com/api-create.php?url='.urlencode($url));
+ $short_url = curl_exec($curlh);
+ break;
+ default:
+ $short_url = false;