From 2028564cc66f15684fd7b55bf1f5ed383aa4cb33 Mon Sep 17 00:00:00 2001 From: Tobias Diekershoff Date: Mon, 13 Feb 2012 19:34:14 +0100 Subject: [PATCH 01/10] regular expressions for preserving img and url links from stripping --- statusnet/statusnet.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/statusnet/statusnet.php b/statusnet/statusnet.php index f558cbec..eef1e294 100755 --- a/statusnet/statusnet.php +++ b/statusnet/statusnet.php @@ -1,7 +1,7 @@ */ @@ -400,7 +400,9 @@ function statusnet_post_hook(&$a,&$b) { require_once('include/bbcode.php'); $dent = new StatusNetOAuth($api,$ckey,$csecret,$otoken,$osecret); $max_char = $dent->get_maxlength(); // max. length for a dent - $msg = strip_tags(bbcode($b['body'])); + $tmp = preg_match_all( '/\[\\/?img(\\s+.*?\]|\])/i', '', $b['body']); + $tmp = preg_match_all( '/\[url\=(\w+.*?)\](\w+.*?)\[\/url\]/i', '$2 $1', $tmp) + $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) { From facd15c77975b7879288a780c653f3e29cf1f7e8 Mon Sep 17 00:00:00 2001 From: Tobias Diekershoff Date: Tue, 14 Feb 2012 08:31:43 +0100 Subject: [PATCH 02/10] m) copy and paste error --- statusnet/statusnet.php | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/statusnet/statusnet.php b/statusnet/statusnet.php index eef1e294..44a1e24f 100755 --- a/statusnet/statusnet.php +++ b/statusnet/statusnet.php @@ -400,8 +400,15 @@ function statusnet_post_hook(&$a,&$b) { require_once('include/bbcode.php'); $dent = new StatusNetOAuth($api,$ckey,$csecret,$otoken,$osecret); $max_char = $dent->get_maxlength(); // max. length for a dent - $tmp = preg_match_all( '/\[\\/?img(\\s+.*?\]|\])/i', '', $b['body']); - $tmp = preg_match_all( '/\[url\=(\w+.*?)\](\w+.*?)\[\/url\]/i', '$2 $1', $tmp) + // preserve links to images + $tmp = preg_replace( '/\[\\/?img(\\s+.*?\]|\])/i', '', $b['body']); + // preserve links to webpages + $tmp = preg_replace( '/\[url\=(\w+.*?)\](\w+.*?)\[\/url\]/i', '$2 $1', $tmp); + // TODO if you insert an image with ~f it inserts a link tag to + // the image - thus we have two potential identical links + // following each other... need to strip one of them + // TODO apply the shortener to the URLs in the releyed dent + logger($tmp); $msg = strip_tags(bbcode($tmp)); // quotes not working - let's try this $msg = html_entity_decode($msg); From 94e9759b507005b4105b4fe25669d9358a280779 Mon Sep 17 00:00:00 2001 From: Tobias Diekershoff Date: Tue, 14 Feb 2012 17:31:22 +0100 Subject: [PATCH 03/10] added audio and video regex to the SN addon --- statusnet/statusnet.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/statusnet/statusnet.php b/statusnet/statusnet.php index 44a1e24f..20a848df 100755 --- a/statusnet/statusnet.php +++ b/statusnet/statusnet.php @@ -400,8 +400,12 @@ function statusnet_post_hook(&$a,&$b) { require_once('include/bbcode.php'); $dent = new StatusNetOAuth($api,$ckey,$csecret,$otoken,$osecret); $max_char = $dent->get_maxlength(); // max. length for a dent - // preserve links to images + // preserve links to images, videos and audios $tmp = preg_replace( '/\[\\/?img(\\s+.*?\]|\])/i', '', $b['body']); + $tmp = preg_replace( '/\[\\/?video(\\s+.*?\]|\])/i', '', $tmp); + $tmp = preg_replace( '/\[\\/?youtube(\\s+.*?\]|\])/i', '', $tmp); + $tmp = preg_replace( '/\[\\/?vimeo(\\s+.*?\]|\])/i', '', $tmp); + $tmp = preg_replace( '/\[\\/?audio(\\s+.*?\]|\])/i', '', $tmp); // preserve links to webpages $tmp = preg_replace( '/\[url\=(\w+.*?)\](\w+.*?)\[\/url\]/i', '$2 $1', $tmp); // TODO if you insert an image with ~f it inserts a link tag to From 55e99a28bf1a070532355f4c55666f457d0150e4 Mon Sep 17 00:00:00 2001 From: Tobias Diekershoff Date: Tue, 14 Feb 2012 19:07:42 +0100 Subject: [PATCH 04/10] added more regex to SN addon to deal with tag links and linked images --- statusnet/statusnet.php | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/statusnet/statusnet.php b/statusnet/statusnet.php index 20a848df..1975277a 100755 --- a/statusnet/statusnet.php +++ b/statusnet/statusnet.php @@ -22,13 +22,6 @@ * Documentation: http://diekershoff.homeunix.net/redmine/wiki/friendikaplugin/StatusNet_Plugin */ -/* __TODO__ - * - * - what about multimedia content? - * so far we just strip HTML tags from the message - */ - - /*** * We have to alter the TwitterOAuth class a little bit to work with any StatusNet * installation abroad. Basically it's only make the API path variable and be happy. @@ -153,7 +146,6 @@ function statusnet_settings_post ($a,$post) { if (isset($_POST['statusnet-disconnect'])) { /*** * if the statusnet-disconnect checkbox is set, clear the statusnet configuration - * TODO can we revoke the access tokens at Twitter and do we need to do so? */ del_pconfig( local_user(), 'statusnet', 'consumerkey' ); del_pconfig( local_user(), 'statusnet', 'consumersecret' ); @@ -400,17 +392,18 @@ function statusnet_post_hook(&$a,&$b) { require_once('include/bbcode.php'); $dent = new StatusNetOAuth($api,$ckey,$csecret,$otoken,$osecret); $max_char = $dent->get_maxlength(); // max. length for a dent + // if [url=bla][img]blub.png[/img][/url] get blub.png + $tmp = preg_replace( '/\[url\=(\w+.*?)\]\[img\](\w+.*?)\[\/img\]\[\/url\]/i', '$2', $tmp); // preserve links to images, videos and audios $tmp = preg_replace( '/\[\\/?img(\\s+.*?\]|\])/i', '', $b['body']); $tmp = preg_replace( '/\[\\/?video(\\s+.*?\]|\])/i', '', $tmp); $tmp = preg_replace( '/\[\\/?youtube(\\s+.*?\]|\])/i', '', $tmp); $tmp = preg_replace( '/\[\\/?vimeo(\\s+.*?\]|\])/i', '', $tmp); $tmp = preg_replace( '/\[\\/?audio(\\s+.*?\]|\])/i', '', $tmp); + // if a #tag is linked, don't send the [url] over to SN + $tmp = preg_replace( '/#\[url\=(\w+.*?)\](\w+.*?)\[\/url\]/i', '#$2', $tmp); // preserve links to webpages $tmp = preg_replace( '/\[url\=(\w+.*?)\](\w+.*?)\[\/url\]/i', '$2 $1', $tmp); - // TODO if you insert an image with ~f it inserts a link tag to - // the image - thus we have two potential identical links - // following each other... need to strip one of them // TODO apply the shortener to the URLs in the releyed dent logger($tmp); $msg = strip_tags(bbcode($tmp)); From 884034d1206b40acab9b99ea08b9c3f771d05431 Mon Sep 17 00:00:00 2001 From: Tobias Diekershoff Date: Thu, 16 Feb 2012 07:57:41 +0100 Subject: [PATCH 05/10] sn commented out some regex for various reasons --- statusnet/statusnet.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/statusnet/statusnet.php b/statusnet/statusnet.php index 1975277a..1bbe6427 100755 --- a/statusnet/statusnet.php +++ b/statusnet/statusnet.php @@ -1,6 +1,7 @@ */ @@ -393,15 +394,18 @@ function statusnet_post_hook(&$a,&$b) { $dent = new StatusNetOAuth($api,$ckey,$csecret,$otoken,$osecret); $max_char = $dent->get_maxlength(); // max. length for a dent // if [url=bla][img]blub.png[/img][/url] get blub.png - $tmp = preg_replace( '/\[url\=(\w+.*?)\]\[img\](\w+.*?)\[\/img\]\[\/url\]/i', '$2', $tmp); +// $tmp = preg_replace( '/\[url\=(\w+.*?)\]\[img\](\w+.*?)\[\/img\]\[\/url\]/i', '$2', $b['body']); // preserve links to images, videos and audios - $tmp = preg_replace( '/\[\\/?img(\\s+.*?\]|\])/i', '', $b['body']); + $tmp = preg_replace( '/\[\\/?img(\\s+.*?\]|\])/i', '', $tmp); $tmp = preg_replace( '/\[\\/?video(\\s+.*?\]|\])/i', '', $tmp); $tmp = preg_replace( '/\[\\/?youtube(\\s+.*?\]|\])/i', '', $tmp); $tmp = preg_replace( '/\[\\/?vimeo(\\s+.*?\]|\])/i', '', $tmp); $tmp = preg_replace( '/\[\\/?audio(\\s+.*?\]|\])/i', '', $tmp); // if a #tag is linked, don't send the [url] over to SN - $tmp = preg_replace( '/#\[url\=(\w+.*?)\](\w+.*?)\[\/url\]/i', '#$2', $tmp); + // this is commented out by default as it means backlinks + // to friendica, if you don't like this feel free to + // uncomment the following line +// $tmp = preg_replace( '/#\[url\=(\w+.*?)\](\w+.*?)\[\/url\]/i', '#$2', $tmp); // preserve links to webpages $tmp = preg_replace( '/\[url\=(\w+.*?)\](\w+.*?)\[\/url\]/i', '$2 $1', $tmp); // TODO apply the shortener to the URLs in the releyed dent From 026d7bf3c378af51564a38549d767ad0723dd8a7 Mon Sep 17 00:00:00 2001 From: Tobias Diekershoff Date: Sun, 19 Feb 2012 15:05:04 +0100 Subject: [PATCH 06/10] corrected regex for finding [url][img[bla[/img][/url] stuff --- statusnet/statusnet.php | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/statusnet/statusnet.php b/statusnet/statusnet.php index 1bbe6427..54aca484 100755 --- a/statusnet/statusnet.php +++ b/statusnet/statusnet.php @@ -393,23 +393,26 @@ function statusnet_post_hook(&$a,&$b) { require_once('include/bbcode.php'); $dent = new StatusNetOAuth($api,$ckey,$csecret,$otoken,$osecret); $max_char = $dent->get_maxlength(); // max. length for a dent + $tmp = $b['body']; // if [url=bla][img]blub.png[/img][/url] get blub.png -// $tmp = preg_replace( '/\[url\=(\w+.*?)\]\[img\](\w+.*?)\[\/img\]\[\/url\]/i', '$2', $b['body']); + $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); $tmp = preg_replace( '/\[\\/?img(\\s+.*?\]|\])/i', '', $tmp); $tmp = preg_replace( '/\[\\/?video(\\s+.*?\]|\])/i', '', $tmp); $tmp = preg_replace( '/\[\\/?youtube(\\s+.*?\]|\])/i', '', $tmp); $tmp = preg_replace( '/\[\\/?vimeo(\\s+.*?\]|\])/i', '', $tmp); $tmp = preg_replace( '/\[\\/?audio(\\s+.*?\]|\])/i', '', $tmp); // if a #tag is linked, don't send the [url] over to SN - // this is commented out by default as it means backlinks - // to friendica, if you don't like this feel free to - // uncomment the following line + // this is commented out by default as it means backlinks + // to friendica, if you don't like this feel free to + // uncomment the following line // $tmp = preg_replace( '/#\[url\=(\w+.*?)\](\w+.*?)\[\/url\]/i', '#$2', $tmp); // preserve links to webpages - $tmp = preg_replace( '/\[url\=(\w+.*?)\](\w+.*?)\[\/url\]/i', '$2 $1', $tmp); + $tmp = preg_replace( '/\[url\=(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)\](\w+.*?)\[\/url\]/i', '$2 $1', $tmp); // TODO apply the shortener to the URLs in the releyed dent - logger($tmp); $msg = strip_tags(bbcode($tmp)); // quotes not working - let's try this $msg = html_entity_decode($msg); From 680fe89fa869c05f5d73c2146a30aa594fe29a60 Mon Sep 17 00:00:00 2001 From: Tobias Diekershoff Date: Sun, 19 Feb 2012 19:42:30 +0100 Subject: [PATCH 07/10] SN addon: added handling for [bookmark] tag --- statusnet/statusnet.php | 1 + 1 file changed, 1 insertion(+) diff --git a/statusnet/statusnet.php b/statusnet/statusnet.php index 54aca484..66a1db81 100755 --- a/statusnet/statusnet.php +++ b/statusnet/statusnet.php @@ -412,6 +412,7 @@ function statusnet_post_hook(&$a,&$b) { // $tmp = preg_replace( '/#\[url\=(\w+.*?)\](\w+.*?)\[\/url\]/i', '#$2', $tmp); // 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 $msg = strip_tags(bbcode($tmp)); // quotes not working - let's try this From e18fdf30a0b70032ccc5ca9b3e78a355c3a46759 Mon Sep 17 00:00:00 2001 From: Tobias Diekershoff Date: Sun, 19 Feb 2012 20:52:19 +0100 Subject: [PATCH 08/10] 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 66a1db81..5d893d6b 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); From d61ce7709a992f9c1798fd82c906fd83fee4b635 Mon Sep 17 00:00:00 2001 From: Tobias Diekershoff Date: Tue, 21 Feb 2012 18:07:30 +0100 Subject: [PATCH 09/10] SN addon, only work with the 1st 2*max_char chars during preparation of denting --- statusnet/statusnet.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/statusnet/statusnet.php b/statusnet/statusnet.php index 5d893d6b..2972f662 100755 --- a/statusnet/statusnet.php +++ b/statusnet/statusnet.php @@ -416,8 +416,12 @@ function statusnet_post_hook(&$a,&$b) { require_once('include/bbcode.php'); $dent = new StatusNetOAuth($api,$ckey,$csecret,$otoken,$osecret); - $max_char = $dent->get_maxlength(); // max. length for a dent - $tmp = $b['body']; + $max_char = $dent->get_maxlength(); // max. length for a dent + // we will only work with up to two time the length of the dent + // we can later send to StatusNet. This way we can "gain" some + // information during shortening of potential links but do not + // shorten all the links in a 200000 character long essay. + $tmp = substr($b['body'], 0, 2*$max_char); // 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\=(\w+.*?)\]\[img\](\w+.*?)\[\/img\]\[\/url\]/i', '$2', $tmp); From 439e62d1d487fd48e36775b0ae01939cb516e517 Mon Sep 17 00:00:00 2001 From: Tobias Diekershoff Date: Tue, 21 Feb 2012 18:39:55 +0100 Subject: [PATCH 10/10] SN addon: only shorteb links if neede --- statusnet/statusnet.php | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/statusnet/statusnet.php b/statusnet/statusnet.php index 2972f662..38202483 100755 --- a/statusnet/statusnet.php +++ b/statusnet/statusnet.php @@ -442,12 +442,14 @@ function statusnet_post_hook(&$a,&$b) { $tmp = preg_replace( '/\[bookmark\=(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)\](\w+.*?)\[\/bookmark\]/i', '$2 $1', $tmp); // 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 ); + if (( strlen($tmp)>$max_char ) && ( $max_char > 0 )) { + 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 ); + } } } }