From e18fdf30a0b70032ccc5ca9b3e78a355c3a46759 Mon Sep 17 00:00:00 2001 From: Tobias Diekershoff Date: Sun, 19 Feb 2012 20:52:19 +0100 Subject: [PATCH] SN addon short the links in the text we are about to send to SN --- statusnet/statusnet.php | 62 +++++++++++++++++++++++++---------------- 1 file changed, 38 insertions(+), 24 deletions(-) diff --git a/statusnet/statusnet.php b/statusnet/statusnet.php index 66a1db814..5d893d6b0 100755 --- a/statusnet/statusnet.php +++ b/statusnet/statusnet.php @@ -368,6 +368,30 @@ function statusnet_post_local(&$a,&$b) { } } +function short($url) { + require_once('library/slinky.php'); + $slinky = new Slinky( $url ); + $yourls_url = get_config('yourls','url1'); + if ($yourls_url) { + $yourls_username = get_config('yourls','username1'); + $yourls_password = get_config('yourls', 'password1'); + $yourls_ssl = get_config('yourls', 'ssl1'); + $yourls = new Slinky_YourLS(); + $yourls->set( 'username', $yourls_username ); + $yourls->set( 'password', $yourls_password ); + $yourls->set( 'ssl', $yourls_ssl ); + $yourls->set( 'yourls-url', $yourls_url ); + $slinky->set_cascade( array( $yourls, new Slinky_UR1ca(), new Slinky_Trim(), new Slinky_IsGd(), new Slinky_TinyURL() ) ); + } + else { + // setup a cascade of shortening services + // try to get a short link from these services + // in the order ur1.ca, trim, id.gd, tinyurl + $slinky->set_cascade( array( new Slinky_UR1ca(), new Slinky_Trim(), new Slinky_IsGd(), new Slinky_TinyURL() ) ); + } + return $slinky->short(); +} + function statusnet_post_hook(&$a,&$b) { /** @@ -396,7 +420,6 @@ function statusnet_post_hook(&$a,&$b) { $tmp = $b['body']; // if [url=bla][img]blub.png[/img][/url] get blub.png $tmp = preg_replace( '/\[url\=(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)\]\[img\](\\w+.*?)\\[\\/img\]\\[\\/url\]/i', '$2', $tmp); - logger($tmp); // $tmp = preg_replace( '/\[url\=(\w+.*?)\]\[img\](\w+.*?)\[\/img\]\[\/url\]/i', '$2', $tmp); // preserve links to images, videos and audios $tmp = preg_replace( '/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism', '$3', $tmp); @@ -413,33 +436,24 @@ function statusnet_post_hook(&$a,&$b) { // preserve links to webpages $tmp = preg_replace( '/\[url\=(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)\](\w+.*?)\[\/url\]/i', '$2 $1', $tmp); $tmp = preg_replace( '/\[bookmark\=(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)\](\w+.*?)\[\/bookmark\]/i', '$2 $1', $tmp); - // TODO apply the shortener to the URLs in the releyed dent + // find all http or https links in the body of the entry and + // apply the shortener if the link is longer then 20 characters + preg_match_all ( '/(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/i', $tmp, $allurls ); + foreach ($allurls as $url) { + foreach ($url as $u) { + if (strlen($u)>20) { + $sl = short($u); + $tmp = str_replace( $u, $sl, $tmp ); + } + } + } + // ok, all the links we want to send out are save, now strip + // away the remaining bbcode $msg = strip_tags(bbcode($tmp)); // quotes not working - let's try this $msg = html_entity_decode($msg); if (( strlen($msg) > $max_char) && $max_char > 0) { - $shortlink = ""; - require_once('library/slinky.php'); - $slinky = new Slinky( $b['plink'] ); - $yourls_url = get_config('yourls','url1'); - if ($yourls_url) { - $yourls_username = get_config('yourls','username1'); - $yourls_password = get_config('yourls', 'password1'); - $yourls_ssl = get_config('yourls', 'ssl1'); - $yourls = new Slinky_YourLS(); - $yourls->set( 'username', $yourls_username ); - $yourls->set( 'password', $yourls_password ); - $yourls->set( 'ssl', $yourls_ssl ); - $yourls->set( 'yourls-url', $yourls_url ); - $slinky->set_cascade( array( $yourls, new Slinky_UR1ca(), new Slinky_Trim(), new Slinky_IsGd(), new Slinky_TinyURL() ) ); - } - else { - // setup a cascade of shortening services - // try to get a short link from these services - // in the order ur1.ca, trim, id.gd, tinyurl - $slinky->set_cascade( array( new Slinky_UR1ca(), new Slinky_Trim(), new Slinky_IsGd(), new Slinky_TinyURL() ) ); - } - $shortlink = $slinky->short(); + $shortlink = short( $b['plink'] ); // the new message will be shortened such that "... $shortlink" // will fit into the character limit $msg = substr($msg, 0, $max_char-strlen($shortlink)-4);