summaryrefslogtreecommitdiff
path: root/lib/util.php
diff options
context:
space:
mode:
authormillette <millette@controlyourself.ca>2008-11-06 15:59:26 -0500
committermillette <millette@controlyourself.ca>2008-11-06 15:59:26 -0500
commit15c1d4f5e4947b9c60439ab5a17694ad57d06704 (patch)
tree4139f40409a3765a9a4dbf4b6922fd7232f43441 /lib/util.php
parent1e8d26baecad6ca1088ea7815fe2615fb520a10e (diff)
trac31 url_auto_shortening by sgmurphy
darcs-hash:20081106205926-099f7-6bcfd7969a159a12b1ba6a9ee254e44a07b94761.gz
Diffstat (limited to 'lib/util.php')
-rw-r--r--lib/util.php44
1 files changed, 44 insertions, 0 deletions
diff --git a/lib/util.php b/lib/util.php
index a5eeab056..edddcd2a7 100644
--- a/lib/util.php
+++ b/lib/util.php
@@ -747,6 +747,50 @@ function common_render_uri_thingy($matches) {
return '<a href="' . $uri . '" class="extlink">' . $uri . '</a>' . $trailer;
}
+function common_shorten_links($text) {
+ $r = htmlspecialchars($text);
+ $r = preg_replace('@https?://[^)\]>\s]+@e', "common_shorten_link('\\0')", $r);
+ return $r;
+}
+
+function common_shorten_link($long_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 'is.gd':
+ curl_setopt($curlh, CURLOPT_URL, 'http://is.gd/api.php?longurl='.urlencode($long_url));
+ $short_url = curl_exec($curlh);
+ break;
+ case 'snipr.com':
+ curl_setopt($curlh, CURLOPT_URL, 'http://snipr.com/site/snip?r=simple&link='.urlencode($long_url));
+ $short_url = curl_exec($curlh);
+ break;
+ case 'metamark.net':
+ curl_setopt($curlh, CURLOPT_URL, 'http://metamark.net/api/rest/simple?long_url='.urlencode($long_url));
+ $short_url = curl_exec($curlh);
+ break;
+ case 'tinyurl.com':
+ curl_setopt($curlh, CURLOPT_URL, 'http://tinyurl.com/api-create.php?url='.urlencode($long_url));
+ $short_url = curl_exec($curlh);
+ break;
+ default:
+ $short_url = false;
+ }
+
+ curl_close($curlh);
+
+ if ($short_url) {
+ return $short_url;
+ }
+ return $long_url;
+}
+
function common_xml_safe_str($str) {
$xmlStr = htmlentities(iconv('UTF-8', 'UTF-8//IGNORE', $str), ENT_NOQUOTES, 'UTF-8');