SN addon short the links in the text we are about to send to SN

This commit is contained in:
Tobias Diekershoff 2012-02-19 20:52:19 +01:00
parent 680fe89fa8
commit e18fdf30a0

View file

@ -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) { function statusnet_post_hook(&$a,&$b) {
/** /**
@ -396,7 +420,6 @@ function statusnet_post_hook(&$a,&$b) {
$tmp = $b['body']; $tmp = $b['body'];
// if [url=bla][img]blub.png[/img][/url] get blub.png // 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); $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); // $tmp = preg_replace( '/\[url\=(\w+.*?)\]\[img\](\w+.*?)\[\/img\]\[\/url\]/i', '$2', $tmp);
// preserve links to images, videos and audios // preserve links to images, videos and audios
$tmp = preg_replace( '/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism', '$3', $tmp); $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 // preserve links to webpages
$tmp = preg_replace( '/\[url\=(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)\](\w+.*?)\[\/url\]/i', '$2 $1', $tmp); $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); $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)); $msg = strip_tags(bbcode($tmp));
// quotes not working - let's try this // quotes not working - let's try this
$msg = html_entity_decode($msg); $msg = html_entity_decode($msg);
if (( strlen($msg) > $max_char) && $max_char > 0) { if (( strlen($msg) > $max_char) && $max_char > 0) {
$shortlink = ""; $shortlink = short( $b['plink'] );
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();
// the new message will be shortened such that "... $shortlink" // the new message will be shortened such that "... $shortlink"
// will fit into the character limit // will fit into the character limit
$msg = substr($msg, 0, $max_char-strlen($shortlink)-4); $msg = substr($msg, 0, $max_char-strlen($shortlink)-4);