Merge pull request #194 from annando/master

Export connectors now are using the "plaintext" function, improvements to tumblr, short post detection for wordpress
This commit is contained in:
fabrixxm 2014-05-29 14:06:09 +02:00
commit 51b364aed4
12 changed files with 311 additions and 1134 deletions

View file

@ -5,6 +5,7 @@
* Description: Posts to app.net with the help of ifttt.com
* Version: 0.1
* Author: Michael Vogel <https://pirati.ca/profile/heluecht>
* Status: Unsupported
*/
function appnetpost_install() {
@ -198,35 +199,12 @@ function appnetpost_init() {
killme();
}
if (! function_exists( 'short_link' )) {
function short_link($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 appnetpost_feeditem($pid, $uid) {
global $a;
require_once('include/bbcode.php');
require_once("include/html2plain.php");
require_once("include/network.php");
$items = q("SELECT `uri`, `plink`, `author-link`, `author-name`, `created`, `edited`, `id`, `title`, `body` from `item` WHERE id=%d", intval($pid));
foreach ($items AS $item) {

View file

@ -1,8 +1,8 @@
<?php
/**
* Name: Buffer Post Connector
* Description: Post to Buffer
* Version: 0.1 alpha
* Description: Post to Buffer (Linkedin, App.net, Google+, Facebook, Twitter)
* Version: 0.2
* Author: Michael Vogel <http://pirati.ca/profile/heluecht>
*/
require('addon/buffer/bufferapp.php');
@ -195,23 +195,8 @@ function buffer_settings_post(&$a,&$b) {
set_pconfig(local_user(),'buffer','post',false);
set_pconfig(local_user(),'buffer','post_by_default',false);
} else {
// filtering the username if it is filled wrong
$user = $_POST['buffer_user'];
if (strstr($user, "@")) {
$pos = strpos($user, "@");
if ($pos > 0)
$user = substr($user, 0, $pos);
}
// Filtering the hostname if someone is entering it with "http"
$host = $_POST['buffer_host'];
$host = trim($host);
$host = str_replace(array("https://", "http://"), array("", ""), $host);
set_pconfig(local_user(),'buffer','post',intval($_POST['buffer']));
set_pconfig(local_user(),'buffer','post_by_default',intval($_POST['buffer_bydefault']));
//header("Location: ".$a->get_baseurl()."/buffer/connect");
}
}
}

View file

@ -408,133 +408,6 @@ function fbpost_jot_nets(&$a,&$b) {
}
}
function fbpost_createmsg($b) {
require_once("include/bbcode.php");
require_once("include/html2plain.php");
$b['body'] = bb_CleanPictureLinks($b['body']);
// Looking for the first image
$image = '';
if(preg_match("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/is",$b['body'],$matches))
$image = $matches[3];
if ($image == '')
if(preg_match("/\[img\](.*?)\[\/img\]/is",$b['body'],$matches))
$image = $matches[1];
$multipleimages = (strpos($b['body'], "[img") != strrpos($b['body'], "[img"));
// When saved into the database the content is sent through htmlspecialchars
// That means that we have to decode all image-urls
$image = htmlspecialchars_decode($image);
$body = $b["body"];
if ($b["title"] != "")
$body = $b["title"]."\n\n".$body;
if (strpos($body, "[bookmark") !== false) {
// splitting the text in two parts:
// before and after the bookmark
$pos = strpos($body, "[bookmark");
$body1 = substr($body, 0, $pos);
$body2 = substr($body, $pos);
// Removing all quotes after the bookmark
// they are mostly only the content after the bookmark.
$body2 = preg_replace("/\[quote\=([^\]]*)\](.*?)\[\/quote\]/ism",'',$body2);
$body2 = preg_replace("/\[quote\](.*?)\[\/quote\]/ism",'',$body2);
$pos2 = strpos($body2, "[/bookmark]");
if ($pos2)
$body2 = substr($body2, $pos2 + 11);
$body = $body1.$body2;
}
// Add some newlines so that the message could be cut better
$body = str_replace(array("[quote", "[bookmark", "[/bookmark]", "[/quote]"),
array("\n[quote", "\n[bookmark", "[/bookmark]\n", "[/quote]\n"), $body);
// remove the recycle signs and the names since they aren't helpful on twitter
// $recycle = html_entity_decode("&#x2672; ", ENT_QUOTES, 'UTF-8');
// $body = preg_replace( '/'.$recycle.'\[url\=(\w+.*?)\](\w+.*?)\[\/url\]/i', "\n", $body);
// At first convert the text to html
$html = bbcode($body, false, false, 2);
// Then convert it to plain text
//$msg = trim($b['title']." \n\n".html2plain($html, 0, true));
$msg = trim(html2plain($html, 0, true));
$msg = html_entity_decode($msg,ENT_QUOTES,'UTF-8');
// Removing multiple newlines
while (strpos($msg, "\n\n\n") !== false)
$msg = str_replace("\n\n\n", "\n\n", $msg);
// Removing multiple spaces
while (strpos($msg, " ") !== false)
$msg = str_replace(" ", " ", $msg);
// Removing URLs
$msg = preg_replace('/(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/i', "", $msg);
$msg = trim($msg);
$link = '';
$linkname = '';
// look for bookmark-bbcode and handle it with priority
if(preg_match("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/is",$b['body'],$matches)) {
$link = $matches[1];
$linkname = $matches[2];
}
$multiplelinks = (strpos($b['body'], "[bookmark") != strrpos($b['body'], "[bookmark"));
if ($multiplelinks)
$linkname = '';
// If there is no bookmark element then take the first link
if ($link == '') {
$links = collecturls($html);
if (sizeof($links) > 0) {
reset($links);
$link = current($links);
}
$multiplelinks = (sizeof($links) > 1);
}
$msglink = "";
if ($multiplelinks)
$msglink = $b["plink"];
else if ($link != "")
$msglink = $link;
else if ($multipleimages)
$msglink = $b["plink"];
else if ($image != "")
$msglink = $image;
// Removing multiple spaces - again
while (strpos($msg, " ") !== false)
$msg = str_replace(" ", " ", $msg);
if ($msglink != "") {
// Looking if the link points to an image
$img_str = fetch_url($msglink);
$tempfile = tempnam(get_config("system","temppath"), "cache");
file_put_contents($tempfile, $img_str);
$mime = image_type_to_mime_type(exif_imagetype($tempfile));
unlink($tempfile);
} else
$mime = "";
if (($image == $msglink) OR (substr($mime, 0, 6) == "image/"))
return(array("msg"=>trim($msg), "link"=>"", "linkname"=>$linkname, "image"=>$msglink));
else
return(array("msg"=>trim($msg), "link"=>$msglink, "linkname"=>$linkname,"image"=>$image));
}
/**
* @param App $a
* @param object $b
@ -685,11 +558,12 @@ function fbpost_post_hook(&$a,&$b) {
logger('fbpost_post_hook: original msg=' . $msg, LOGGER_DATA);
if ($toplevel) {
$msgarr = fbpost_createmsg($b);
$msg = $msgarr["msg"];
$link = $msgarr["link"];
require_once("include/plaintext.php");
$msgarr = plaintext($a, $b, 0, false);
$msg = $msgarr["text"];
$link = $msgarr["url"];
$image = $msgarr["image"];
$linkname = $msgarr["linkname"];
$linkname = $msgarr["title"];
// Fallback - if message is empty
if(!strlen($msg))

View file

@ -243,7 +243,7 @@ function fromgplus_cleantext($text) {
$trash = html_entity_decode("&#xFEFF;", ENT_QUOTES, 'UTF-8');
$text = strip_tags($text);
$text = html_entity_decode($text);
$text = html_entity_decode($text, ENT_QUOTES);
$text = trim($text);
$text = str_replace(array("\n", "\r", " ", $trash), array("", "", "", ""), $text);
return($text);

View file

@ -256,15 +256,26 @@ function gpluspost_send(&$a,&$b) {
if($username && $password) {
require_once("addon/gpluspost/postToGooglePlus.php");
require_once("include/plaintext.php");
$data = gpluspost_createmsg($b);
$item = $b;
// Markup for Google+
if ($item["title"] != "")
$item["title"] = "*".$item["title"]."*";
$item["body"] = preg_replace("(\[b\](.*?)\[\/b\])ism",'*$1*',$item["body"]);
$item["body"] = preg_replace("(\[i\](.*?)\[\/i\])ism",'_$1_',$item["body"]);
$item["body"] = preg_replace("(\[s\](.*?)\[\/s\])ism",'-$1-',$item["body"]);
$data = plaintext($a, $item, 0, false);
logger('gpluspost_send: data: '.print_r($data, true), LOGGER_DEBUG);
$loginError = doConnectToGooglePlus2($username, $password);
if (!$loginError) {
if ($data["link"] != "")
$lnk = doGetGoogleUrlInfo2($data["link"]);
if ($data["url"] != "")
$lnk = doGetGoogleUrlInfo2($data["url"]);
elseif ($data["image"] != "")
$lnk = array('img'=>$data["image"]);
else
@ -273,7 +284,7 @@ function gpluspost_send(&$a,&$b) {
// Send a special blank to identify the post through the "fromgplus" addon
$blank = html_entity_decode("&#x00A0;", ENT_QUOTES, 'UTF-8');
doPostToGooglePlus2($data["msg"].$blank, $lnk, $page);
doPostToGooglePlus2($data["text"].$blank, $lnk, $page);
$success = true;
@ -299,136 +310,6 @@ function gpluspost_send(&$a,&$b) {
}
function gpluspost_createmsg($b) {
require_once("include/bbcode.php");
require_once("include/html2plain.php");
$b['body'] = bb_CleanPictureLinks($b['body']);
// Looking for the first image
$image = '';
if(preg_match("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/is",$b['body'],$matches))
$image = $matches[3];
if ($image == '')
if(preg_match("/\[img\](.*?)\[\/img\]/is",$b['body'],$matches))
$image = $matches[1];
$multipleimages = (strpos($b['body'], "[img") != strrpos($b['body'], "[img"));
// When saved into the database the content is sent through htmlspecialchars
// That means that we have to decode all image-urls
$image = htmlspecialchars_decode($image);
$body = $b["body"];
if ($b["title"] != "")
$body = "*".$b["title"]."*\n\n".$body;
if (strpos($body, "[bookmark") !== false) {
// splitting the text in two parts:
// before and after the bookmark
$pos = strpos($body, "[bookmark");
$body1 = substr($body, 0, $pos);
$body2 = substr($body, $pos);
// Removing all quotes after the bookmark
// they are mostly only the content after the bookmark.
$body2 = preg_replace("/\[quote\=([^\]]*)\](.*?)\[\/quote\]/ism",'',$body2);
$body2 = preg_replace("/\[quote\](.*?)\[\/quote\]/ism",'',$body2);
$pos2 = strpos($body2, "[/bookmark]");
if ($pos2)
$body2 = substr($body2, $pos2 + 11);
$body = $body1.$body2;
}
// Add some newlines so that the message could be cut better
$body = str_replace(array("[quote", "[bookmark", "[/bookmark]", "[/quote]"),
array("\n[quote", "\n[bookmark", "[/bookmark]\n", "[/quote]\n"), $body);
// remove the recycle signs and the names since they aren't helpful on twitter
// recycle 1
$recycle = html_entity_decode("&#x2672; ", ENT_QUOTES, 'UTF-8');
$body = preg_replace( '/'.$recycle.'\[url\=(\w+.*?)\](\w+.*?)\[\/url\]/i', "\n", $body);
// remove the share element
//$body = preg_replace("/\[share(.*?)\](.*?)\[\/share\]/ism","\n\n$2\n\n",$body);
$body = preg_replace("(\[b\](.*?)\[\/b\])ism",'*$1*',$body);
$body = preg_replace("(\[i\](.*?)\[\/i\])ism",'_$1_',$body);
$body = preg_replace("(\[s\](.*?)\[\/s\])ism",'-$1-',$body);
// At first convert the text to html
$html = bbcode($body, false, false, 2);
// Then convert it to plain text
//$msg = trim($b['title']." \n\n".html2plain($html, 0, true));
$msg = trim(html2plain($html, 0, true));
$msg = html_entity_decode($msg,ENT_QUOTES,'UTF-8');
// Removing multiple newlines
while (strpos($msg, "\n\n\n") !== false)
$msg = str_replace("\n\n\n", "\n\n", $msg);
// Removing multiple spaces
while (strpos($msg, " ") !== false)
$msg = str_replace(" ", " ", $msg);
// Removing URLs
$msg = preg_replace('/(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/i', "", $msg);
$msg = trim($msg);
$link = '';
// look for bookmark-bbcode and handle it with priority
if(preg_match("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/is",$b['body'],$matches))
$link = $matches[1];
$multiplelinks = (strpos($b['body'], "[bookmark") != strrpos($b['body'], "[bookmark"));
// If there is no bookmark element then take the first link
if ($link == '') {
$links = collecturls($html);
if (sizeof($links) > 0) {
reset($links);
$link = current($links);
}
$multiplelinks = (sizeof($links) > 1);
}
$msglink = "";
if ($multiplelinks)
$msglink = $b["plink"];
else if ($link != "")
$msglink = $link;
else if ($multipleimages)
$msglink = $b["plink"];
else if ($image != "")
$msglink = $image;
// Removing multiple spaces - again
while (strpos($msg, " ") !== false)
$msg = str_replace(" ", " ", $msg);
if ($msglink != "") {
// Looking if the link points to an image
$img_str = fetch_url($msglink);
$tempfile = tempnam(get_config("system","temppath"), "cache");
file_put_contents($tempfile, $img_str);
$mime = image_type_to_mime_type(exif_imagetype($tempfile));
unlink($tempfile);
} else
$mime = "";
if (($image == $msglink) OR (substr($mime, 0, 6) == "image/"))
return(array("msg"=>trim($msg), "link"=>"", "image"=>$msglink));
else
return(array("msg"=>trim($msg), "link"=>$msglink, "image"=>""));
}
function gpluspost_queue_hook(&$a,&$b) {
$qi = q("SELECT * FROM `queue` WHERE `network` = '%s'",
@ -476,8 +357,8 @@ function gpluspost_queue_hook(&$a,&$b) {
$loginError = doConnectToGooglePlus2($username, $password);
if (!$loginError) {
if ($data["link"] != "")
$lnk = doGetGoogleUrlInfo2($data["link"]);
if ($data["url"] != "")
$lnk = doGetGoogleUrlInfo2($data["url"]);
elseif ($data["image"] != "")
$lnk = array('img'=>$data["image"]);
else
@ -486,7 +367,7 @@ function gpluspost_queue_hook(&$a,&$b) {
// Send a special blank to identify the post through the "fromgplus" addon
$blank = html_entity_decode("&#x00A0;", ENT_QUOTES, 'UTF-8');
doPostToGooglePlus2($data["msg"].$blank, $lnk, $page);
doPostToGooglePlus2($data["text"].$blank, $lnk, $page);
logger('gpluspost_queue: send '.$userdata['uid'].' success', LOGGER_DEBUG);

View file

@ -67,12 +67,12 @@ function openstreetmap_location($a, &$item) {
if($item['coord']) {
$coords = explode(' ', $item['coord']);
if(count($coords) > 1) {
$coord = '<a target="map" class="OSMMapLink" title="' . $item['coord'] . '" href="'.$tmsserver.'?lat=' . urlencode($coords[0]) . '&lon=' . urlencode($coords[1]) . '&zoom='.$zoom.'"> Map </a>' ;
$coord = '<a target="map" class="OSMMapLink" title="'.$item['coord'].'" href="'.$tmsserver.'?lat='.urlencode($coords[0]).'&lon=' . urlencode($coords[1]).'&zoom='.$zoom.'">'.t("Map").'</a>' ;
}
}
if(strlen($coord)) {
if($location)
$location .= '<br /><span class="smalltext">(' . $coord . ')</span>';
$location .= ' <span class="smalltext">('.$coord.')</span>';
else
$location = '<span class="smalltext">'.$coord.'</span>';
}

View file

@ -1333,7 +1333,7 @@ function pumpio_getallusers(&$a, $uid) {
}
foreach ($users->items AS $user)
echo pumpio_get_contact($uid, $user)."\n";
pumpio_get_contact($uid, $user);
}
function pumpio_queue_hook(&$a,&$b) {

View file

@ -168,10 +168,8 @@ function statusnet_settings_post ($a,$post) {
del_pconfig(local_user(), 'statusnet', 'oauthtoken');
del_pconfig(local_user(), 'statusnet', 'oauthsecret');
del_pconfig(local_user(), 'statusnet', 'baseapi');
del_pconfig(local_user(), 'statusnet', 'post_taglinks');
del_pconfig(local_user(), 'statusnet', 'lastid');
del_pconfig(local_user(), 'statusnet', 'mirror_posts');
del_pconfig(local_user(), 'statusnet', 'intelligent_shortening');
del_pconfig(local_user(), 'statusnet', 'import');
del_pconfig(local_user(), 'statusnet', 'create_user');
del_pconfig(local_user(), 'statusnet', 'own_id');
@ -249,9 +247,7 @@ function statusnet_settings_post ($a,$post) {
// to post a dent for every new __public__ posting to the wall
set_pconfig(local_user(),'statusnet','post',intval($_POST['statusnet-enable']));
set_pconfig(local_user(),'statusnet','post_by_default',intval($_POST['statusnet-default']));
set_pconfig(local_user(),'statusnet','post_taglinks',intval($_POST['statusnet-sendtaglinks']));
set_pconfig(local_user(), 'statusnet', 'mirror_posts', intval($_POST['statusnet-mirror']));
set_pconfig(local_user(), 'statusnet', 'intelligent_shortening', intval($_POST['statusnet-shortening']));
set_pconfig(local_user(), 'statusnet', 'import', intval($_POST['statusnet-import']));
set_pconfig(local_user(), 'statusnet', 'create_user', intval($_POST['statusnet-create_user']));
info( t('StatusNet settings updated.') . EOL);
@ -276,12 +272,8 @@ function statusnet_settings(&$a,&$s) {
$checked = (($enabled) ? ' checked="checked" ' : '');
$defenabled = get_pconfig(local_user(),'statusnet','post_by_default');
$defchecked = (($defenabled) ? ' checked="checked" ' : '');
$linksenabled = get_pconfig(local_user(),'statusnet','post_taglinks');
$linkschecked = (($linksenabled) ? ' checked="checked" ' : '');
$mirrorenabled = get_pconfig(local_user(),'statusnet','mirror_posts');
$mirrorchecked = (($mirrorenabled) ? ' checked="checked" ' : '');
$shorteningenabled = get_pconfig(local_user(),'statusnet','intelligent_shortening');
$shorteningchecked = (($shorteningenabled) ? ' checked="checked" ' : '');
$importenabled = get_pconfig(local_user(),'statusnet','import');
$importchecked = (($importenabled) ? ' checked="checked" ' : '');
$create_userenabled = get_pconfig(local_user(),'statusnet','create_user');
@ -289,9 +281,6 @@ function statusnet_settings(&$a,&$s) {
$css = (($enabled) ? '' : '-disabled');
$globalshortening = get_config('statusnet','intelligent_shortening');
$s .= '<span id="settings_statusnet_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_statusnet_expanded\'); openClose(\'settings_statusnet_inflated\');">';
$s .= '<img class="connector'.$css.'" src="images/gnusocial.png" /><h3 class="connector">'. t('StatusNet Import/Export/Mirror').'</h3>';
$s .= '</span>';
@ -394,16 +383,6 @@ function statusnet_settings(&$a,&$s) {
$s .= '<label id="statusnet-mirror-label" for="statusnet-mirror">'.t('Mirror all posts from statusnet that are no replies or repeated messages').'</label>';
$s .= '<input id="statusnet-mirror" type="checkbox" name="statusnet-mirror" value="1" '. $mirrorchecked . '/>';
$s .= '<div class="clear"></div>';
if (!$globalshortening) {
$s .= '<label id="statusnet-shortening-label" for="statusnet-shortening">'.t('Shortening method that optimizes the post').'</label>';
$s .= '<input id="statusnet-shortening" type="checkbox" name="statusnet-shortening" value="1" '. $shorteningchecked . '/>';
$s .= '<div class="clear"></div>';
$s .= '<label id="statusnet-sendtaglinks-label" for="statusnet-sendtaglinks">'.t('Send linked #-tags and @-names to StatusNet').'</label>';
$s .= '<input id="statusnet-sendtaglinks" type="checkbox" name="statusnet-sendtaglinks" value="1" '. $linkschecked . '/>';
$s .= '<div class="clear"></div>';
}
$s .= '</div>';
$s .= '<label id="statusnet-import-label" for="statusnet-import">'.t('Import the remote timeline').'</label>';
@ -447,192 +426,6 @@ function statusnet_post_local(&$a,&$b) {
}
}
if (! function_exists( 'short_link' )) {
function short_link($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_shortenmsg($b, $max_char) {
require_once("include/api.php");
require_once("include/bbcode.php");
require_once("include/html2plain.php");
$b['body'] = bb_CleanPictureLinks($b['body']);
// Looking for the first image
$cleaned_body = api_clean_plain_items($b['body']);
$image = '';
if(preg_match("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/is",$cleaned_body,$matches))
$image = $matches[3];
if ($image == '')
if(preg_match("/\[img\](.*?)\[\/img\]/is",$cleaned_body,$matches))
$image = $matches[1];
$multipleimages = (strpos($cleaned_body, "[img") != strrpos($cleaned_body, "[img"));
// When saved into the database the content is sent through htmlspecialchars
// That means that we have to decode all image-urls
$image = htmlspecialchars_decode($image);
$body = $b["body"];
if ($b["title"] != "")
$body = $b["title"]."\n\n".$body;
if (strpos($body, "[bookmark") !== false) {
// splitting the text in two parts:
// before and after the bookmark
$pos = strpos($body, "[bookmark");
$body1 = substr($body, 0, $pos);
$body2 = substr($body, $pos);
// Removing all quotes after the bookmark
// they are mostly only the content after the bookmark.
$body2 = preg_replace("/\[quote\=([^\]]*)\](.*?)\[\/quote\]/ism",'',$body2);
$body2 = preg_replace("/\[quote\](.*?)\[\/quote\]/ism",'',$body2);
$body = $body1.$body2;
}
// Add some newlines so that the message could be cut better
$body = str_replace(array("[quote", "[bookmark", "[/bookmark]", "[/quote]"),
array("\n[quote", "\n[bookmark", "[/bookmark]\n", "[/quote]\n"), $body);
// remove the recycle signs and the names since they aren't helpful on statusnet
// recycle 1
$recycle = html_entity_decode("&#x2672; ", ENT_QUOTES, 'UTF-8');
$body = preg_replace( '/'.$recycle.'\[url\=(\w+.*?)\](\w+.*?)\[\/url\]/i', "\n", $body);
// recycle 2 (Test)
$recycle = html_entity_decode("&#x25CC; ", ENT_QUOTES, 'UTF-8');
$body = preg_replace( '/'.$recycle.'\[url\=(\w+.*?)\](\w+.*?)\[\/url\]/i', "\n", $body);
// remove the share element
//$body = preg_replace("/\[share(.*?)\](.*?)\[\/share\]/ism","\n\n$2\n\n",$body);
// At first convert the text to html
$html = bbcode(api_clean_plain_items($body), false, false, 2);
// Then convert it to plain text
//$msg = trim($b['title']." \n\n".html2plain($html, 0, true));
$msg = trim(html2plain($html, 0, true));
$msg = html_entity_decode($msg,ENT_QUOTES,'UTF-8');
// Removing multiple newlines
while (strpos($msg, "\n\n\n") !== false)
$msg = str_replace("\n\n\n", "\n\n", $msg);
// Removing multiple spaces
while (strpos($msg, " ") !== false)
$msg = str_replace(" ", " ", $msg);
$origmsg = $msg;
// Removing URLs
$msg = preg_replace('/(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/i', "", $msg);
$msg = trim($msg);
$link = '';
// look for bookmark-bbcode and handle it with priority
if(preg_match("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/is",$b['body'],$matches))
$link = $matches[1];
$multiplelinks = (strpos($b['body'], "[bookmark") != strrpos($b['body'], "[bookmark"));
// If there is no bookmark element then take the first link
if ($link == '') {
$links = collecturls($html);
if (sizeof($links) > 0) {
reset($links);
$link = current($links);
}
$multiplelinks = (sizeof($links) > 1);
}
$msglink = "";
if ($multiplelinks)
$msglink = $b["plink"];
else if ($link != "")
$msglink = $link;
else if ($multipleimages)
$msglink = $b["plink"];
else if ($image != "")
$msglink = $image;
if (($msglink == "") and strlen($msg) > $max_char)
$msglink = $b["plink"];
// If the message is short enough then don't modify it. (if the link exists in the original message)
if ((strlen(trim($origmsg)) <= $max_char) AND (($msglink == "") OR strpos($origmsg, $msglink)))
return(array("msg"=>trim($origmsg), "image"=>""));
// If the message is short enough and contains a picture then post the picture as well
if ((strlen(trim($origmsg)) <= ($max_char - 20)) AND strpos($origmsg, $msglink))
return(array("msg"=>trim($origmsg), "image"=>$image));
// If the message is short enough and the link exists in the original message don't modify it as well
if ((strlen(trim($origmsg)) <= $max_char) AND strpos($origmsg, $msglink))
return(array("msg"=>trim($origmsg), "image"=>""));
// Preserve the unshortened link
$orig_link = $msglink;
if (strlen($msglink) > 20)
$msglink = short_link($msglink);
if (strlen(trim($msg." ".$msglink)) > $max_char) {
$msg = substr($msg, 0, $max_char - (strlen($msglink)));
$lastchar = substr($msg, -1);
$msg = substr($msg, 0, -1);
$pos = strrpos($msg, "\n");
if ($pos > 0)
$msg = substr($msg, 0, $pos);
else if ($lastchar != "\n")
$msg = substr($msg, 0, -3)."...";
}
//$msg = str_replace("\n", " ", $msg);
// Removing multiple spaces - again
while (strpos($msg, " ") !== false)
$msg = str_replace(" ", " ", $msg);
//return(array("msg"=>trim($msg."\n".$msglink), "image"=>$image));
// Looking if the link points to an image
$img_str = fetch_url($orig_link);
$tempfile = tempnam(get_config("system","temppath"), "cache");
file_put_contents($tempfile, $img_str);
$mime = image_type_to_mime_type(exif_imagetype($tempfile));
unlink($tempfile);
if (($image == $orig_link) OR (substr($mime, 0, 6) == "image/"))
return(array("msg"=>trim($msg), "image"=>$orig_link));
else if (($image != $orig_link) AND ($image != "") AND (strlen($msg." ".$msglink) <= ($max_char - 20)))
return(array("msg"=>trim($msg." ".$msglink)."\n", "image"=>$image));
else
return(array("msg"=>trim($msg." ".$msglink), "image"=>""));
}
function statusnet_action($a, $uid, $pid, $action) {
$api = get_pconfig($uid, 'statusnet', 'baseapi');
$ckey = get_pconfig($uid, 'statusnet', 'consumerkey');
@ -740,11 +533,6 @@ function statusnet_post_hook(&$a,&$b) {
$csecret = get_pconfig($b['uid'], 'statusnet', 'consumersecret');
$otoken = get_pconfig($b['uid'], 'statusnet', 'oauthtoken');
$osecret = get_pconfig($b['uid'], 'statusnet', 'oauthsecret');
$intelligent_shortening = get_pconfig($b['uid'], 'statusnet', 'intelligent_shortening');
// Global setting overrides this
if (get_config('statusnet','intelligent_shortening'))
$intelligent_shortening = get_config('statusnet','intelligent_shortening');
if($ckey && $csecret && $otoken && $osecret) {
@ -755,91 +543,27 @@ 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
// we will only work with up to two times 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.
$tempfile = "";
$intelligent_shortening = get_config('statusnet','intelligent_shortening');
if (!$intelligent_shortening) {
if (! $b['title']=='') {
$tmp = $b['title'].": \n".$b['body'];
// $tmp = substr($tmp, 0, 4*$max_char);
} else {
$tmp = $b['body']; // substr($b['body'], 0, 3*$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);
// 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);
$linksenabled = get_pconfig($b['uid'],'statusnet','post_taglinks');
// if a #tag is linked, don't send the [url] over to SN
// that is, don't send if the option is not set in the
// connector settings
if ($linksenabled=='0') {
// #-tags
$tmp = preg_replace( '/#\[url\=(\w+.*?)\](\w+.*?)\[\/url\]/i', '#$2', $tmp);
// @-mentions
$tmp = preg_replace( '/@\[url\=(\w+.*?)\](\w+.*?)\[\/url\]/i', '@$2', $tmp);
// recycle 1
$recycle = html_entity_decode("&#x2672; ", ENT_QUOTES, 'UTF-8');
$tmp = preg_replace( '/'.$recycle.'\[url\=(\w+.*?)\](\w+.*?)\[\/url\]/i', $recycle.'$2', $tmp);
// recycle 2 (test)
$recycle = html_entity_decode("&#x25CC; ", ENT_QUOTES, 'UTF-8');
$tmp = preg_replace( '/'.$recycle.'\[url\=(\w+.*?)\](\w+.*?)\[\/url\]/i', $recycle.'$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);
// find all http or https links in the body of the entry and
// apply the shortener if the link is longer then 20 characters
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_link($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, false, false));
$msg = bbcode($tmp, false, false, true);
$msg = str_replace(array('<br>','<br />'),"\n",$msg);
$msg = strip_tags($msg);
require_once("include/plaintext.php");
require_once("include/network.php");
$msgarr = plaintext($a, $b, $max_char, true);
$msg = $msgarr["text"];
// quotes not working - let's try this
$msg = html_entity_decode($msg);
if (($msg == "") AND isset($msgarr["title"]))
$msg = shortenmsg($msgarr["title"], $max_char - 50);
if (( strlen($msg) > $max_char) && $max_char > 0) {
$shortlink = short_link( $b['plink'] );
// the new message will be shortened such that "... $shortlink"
// will fit into the character limit
$msg = nl2br(substr($msg, 0, $max_char-strlen($shortlink)-4));
$msg = str_replace(array('<br>','<br />'),' ',$msg);
$e = explode(' ', $msg);
// remove the last word from the cut down message to
// avoid sending cut words to the MicroBlog
array_pop($e);
$msg = implode(' ', $e);
$msg .= '... ' . $shortlink;
}
$image = "";
$msg = trim($msg);
$postdata = array('status' => $msg);
} else {
$msgarr = statusnet_shortenmsg($b, $max_char);
$msg = $msgarr["msg"];
if (isset($msgarr["url"])) {
if ((strlen($msgarr["url"]) > 20) AND
((strlen($msg." \n".$msgarr["url"]) > $max_char)))
$msg .= " \n".short_link($msgarr["url"]);
else
$msg .= " \n".$msgarr["url"];
} elseif (isset($msgarr["image"]))
$image = $msgarr["image"];
if ($image != "") {
$img_str = fetch_url($image);
$tempfile = tempnam(get_config("system","temppath"), "cache");
@ -847,7 +571,6 @@ function statusnet_post_hook(&$a,&$b) {
$postdata = array("status" => $msg, "media[]" => $tempfile);
} else
$postdata = array("status"=>$msg);
}
// and now dent it :-)
if(strlen($msg)) {
@ -1661,6 +1384,7 @@ function statusnet_complete_conversation($a, $uid, $self, $create_user, $nick, $
$parameters["count"] = 200;
$items = $connection->get('statusnet/conversation/'.$conversation, $parameters);
if (is_array($items)) {
$posts = array_reverse($items);
foreach($posts AS $post) {
@ -1678,6 +1402,7 @@ function statusnet_complete_conversation($a, $uid, $self, $create_user, $nick, $
statusnet_checknotification($a, $uid, $nick, $item, $postarray);
}
}
}
function statusnet_convertmsg($a, $body, $no_tags = false) {
@ -1690,6 +1415,7 @@ function statusnet_convertmsg($a, $body, $no_tags = false) {
$footer = "";
$footerurl = "";
$footerlink = "";
$type = "";
if ($links) {
@ -1698,14 +1424,18 @@ function statusnet_convertmsg($a, $body, $no_tags = false) {
$expanded_url = original_url($match[1]);
$oembed_data = oembed_fetch_url($expanded_url);
$oembed_data = oembed_fetch_url($expanded_url, true);
print_r($oembed_data);
if ($type == "")
$type = $oembed_data->type;
if ($oembed_data->type == "video") {
//$body = str_replace($search, "[video]".$expanded_url."[/video]", $body);
$type = $oembed_data->type;
$footerurl = $expanded_url;
$footerlink = "[url=".$expanded_url."]".$expanded_url."[/url]";
if ($oembed_data->type == "video")
$body = str_replace($search, "[video]".$expanded_url."[/video]", $body);
elseif (($oembed_data->type == "photo") AND isset($oembed_data->url) AND !$dontincludemedia)
$body = str_replace($search, $footerlink, $body);
} elseif (($oembed_data->type == "photo") AND isset($oembed_data->url) AND !$dontincludemedia)
$body = str_replace($search, "[url=".$expanded_url."][img]".$oembed_data->url."[/img][/url]", $body);
elseif ($oembed_data->type != "link")
$body = str_replace($search, "[url=".$expanded_url."]".$expanded_url."[/url]", $body);
@ -1725,7 +1455,7 @@ function statusnet_convertmsg($a, $body, $no_tags = false) {
$footerurl = $expanded_url;
$footerlink = "[url=".$expanded_url."]".$expanded_url."[/url]";
$body = str_replace($search, $expanded_url, $body);
$body = str_replace($search, $footerlink, $body);
}
}
}

View file

@ -3,8 +3,9 @@
/**
* Name: Tumblr Post Connector
* Description: Post to Tumblr
* Version: 1.0
* Version: 2.0
* Author: Mike Macgirvin <http://macgirvin.com/profile/mike>
* Author: Michael Vogel <https://pirati.ca/profile/heluecht>
*/
require_once('library/OAuth1.php');
@ -316,54 +317,68 @@ function tumblr_send(&$a,&$b) {
if(count($tag_arr))
$tags = implode(',',$tag_arr);
$link = "";
$video = false;
$title = trim($b['title']);
require_once('include/plaintext.php');
// Checking for a bookmark
if(preg_match("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/is",$b['body'],$matches)) {
$link = $matches[1];
if ($title == '')
$title = html_entity_decode($matches[2],ENT_QUOTES,'UTF-8');
$body = $b['body'];
// splitting the text in two parts:
// before and after the bookmark
$pos = strpos($body, "[bookmark");
$body1 = substr($body, 0, $pos);
$body2 = substr($body, $pos);
// Removing the bookmark
$body2 = preg_replace("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/ism",'',$body2);
$body = $body1.$body2;
$video = ((stristr($link,'youtube')) || (stristr($link,'youtu.be')) || (stristr($mtch[1],'vimeo')));
}
$siteinfo = get_attached_data($b["body"]);
$params = array(
'format' => 'html',
'state' => 'published',
'tags' => $tags,
'tweet' => 'off',
'tags' => $tags);
'format' => 'html');
if (($link != '') and $video) {
$params['type'] = "video";
$params['embed'] = $link;
if ($title != '')
$params['caption'] = '<h1><a href="'.$link.'">'.$title.
"</a></h1><p>".bbcode($body, false, false, 4)."</p>";
if (!isset($siteinfo["type"]))
$siteinfo["type"] = "";
if (($title == "") AND isset($siteinfo["title"]))
$title = $siteinfo["title"];
if (isset($siteinfo["text"]))
$body = $siteinfo["text"];
else
$body = bb_remove_share_information($b["body"]);
switch ($siteinfo["type"]) {
case "photo":
$params['type'] = "photo";
$params['caption'] = bbcode($body, false, false, 4);
} else if (($link != '') and !$video) {
if (isset($siteinfo["url"]))
$params['link'] = $siteinfo["url"];
$params['source'] = $siteinfo["image"];
break;
case "link":
$params['type'] = "link";
$params['title'] = $title;
$params['url'] = $link;
$params['description'] = bbcode($b["body"], false, false, 4);
} else {
$params['url'] = $siteinfo["url"];
$params['description'] = bbcode($body, false, false, 4);
break;
case "audio":
$params['type'] = "audio";
$params['external_url'] = $siteinfo["url"];
$params['caption'] = bbcode($body, false, false, 4);
break;
case "video":
$params['type'] = "video";
$params['embed'] = $siteinfo["url"];
$params['caption'] = bbcode($body, false, false, 4);
break;
default:
$params['type'] = "text";
$params['title'] = $title;
$params['body'] = bbcode($b['body'], false, false, 4);
break;
}
if (isset($params['caption']) AND (trim($title) != ""))
$params['caption'] = '<h1>'.$title."</h1>".
"<p>".$params['caption']."</p>";
if (trim($params['caption']) == "")
$params['caption'] = bbcode("[quote]".$siteinfo["description"]."[/quote]", false, false, 4);
$consumer_key = get_config('tumblr','consumer_key');
$consumer_secret = get_config('tumblr','consumer_secret');
@ -371,9 +386,8 @@ function tumblr_send(&$a,&$b) {
// Make an API call with the TumblrOAuth instance.
$x = $tum_oauth->post($tmbl_blog,$params);
$ret_code = $tum_oauth->http_code;
//print_r($params);
if($ret_code == 201)
logger('tumblr_send: success');
elseif($ret_code == 403)

View file

@ -163,10 +163,8 @@ function twitter_settings_post ($a,$post) {
del_pconfig(local_user(), 'twitter', 'oauthsecret');
del_pconfig(local_user(), 'twitter', 'post');
del_pconfig(local_user(), 'twitter', 'post_by_default');
del_pconfig(local_user(), 'twitter', 'post_taglinks');
del_pconfig(local_user(), 'twitter', 'lastid');
del_pconfig(local_user(), 'twitter', 'mirror_posts');
del_pconfig(local_user(), 'twitter', 'intelligent_shortening');
del_pconfig(local_user(), 'twitter', 'import');
del_pconfig(local_user(), 'twitter', 'create_user');
del_pconfig(local_user(), 'twitter', 'own_id');
@ -186,7 +184,6 @@ function twitter_settings_post ($a,$post) {
set_pconfig(local_user(),'twitter', 'oauthtoken', $token['oauth_token']);
set_pconfig(local_user(),'twitter', 'oauthsecret', $token['oauth_token_secret']);
set_pconfig(local_user(),'twitter', 'post', 1);
set_pconfig(local_user(),'twitter', 'post_taglinks', 1);
// reload the Addon Settings page, if we don't do it see Bug #42
goaway($a->get_baseurl().'/settings/connectors');
} else {
@ -194,9 +191,7 @@ function twitter_settings_post ($a,$post) {
// to post a tweet for every new __public__ posting to the wall
set_pconfig(local_user(),'twitter','post',intval($_POST['twitter-enable']));
set_pconfig(local_user(),'twitter','post_by_default',intval($_POST['twitter-default']));
set_pconfig(local_user(),'twitter','post_taglinks',intval($_POST['twitter-sendtaglinks']));
set_pconfig(local_user(), 'twitter', 'mirror_posts', intval($_POST['twitter-mirror']));
set_pconfig(local_user(), 'twitter', 'intelligent_shortening', intval($_POST['twitter-shortening']));
set_pconfig(local_user(), 'twitter', 'import', intval($_POST['twitter-import']));
set_pconfig(local_user(), 'twitter', 'create_user', intval($_POST['twitter-create_user']));
info( t('Twitter settings updated.') . EOL);
@ -219,12 +214,8 @@ function twitter_settings(&$a,&$s) {
$checked = (($enabled) ? ' checked="checked" ' : '');
$defenabled = get_pconfig(local_user(),'twitter','post_by_default');
$defchecked = (($defenabled) ? ' checked="checked" ' : '');
$linksenabled = get_pconfig(local_user(),'twitter','post_taglinks');
$linkschecked = (($linksenabled) ? ' checked="checked" ' : '');
$mirrorenabled = get_pconfig(local_user(),'twitter','mirror_posts');
$mirrorchecked = (($mirrorenabled) ? ' checked="checked" ' : '');
$shorteningenabled = get_pconfig(local_user(),'twitter','intelligent_shortening');
$shorteningchecked = (($shorteningenabled) ? ' checked="checked" ' : '');
$importenabled = get_pconfig(local_user(),'twitter','import');
$importchecked = (($importenabled) ? ' checked="checked" ' : '');
$create_userenabled = get_pconfig(local_user(),'twitter','create_user');
@ -232,8 +223,6 @@ function twitter_settings(&$a,&$s) {
$css = (($enabled) ? '' : '-disabled');
$globalshortening = get_config('twitter','intelligent_shortening');
$s .= '<span id="settings_twitter_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_twitter_expanded\'); openClose(\'settings_twitter_inflated\');">';
$s .= '<img class="connector'.$css.'" src="images/twitter.png" /><h3 class="connector">'. t('Twitter Import/Export/Mirror').'</h3>';
$s .= '</span>';
@ -299,16 +288,6 @@ function twitter_settings(&$a,&$s) {
$s .= '<label id="twitter-mirror-label" for="twitter-mirror">'.t('Mirror all posts from twitter that are no replies').'</label>';
$s .= '<input id="twitter-mirror" type="checkbox" name="twitter-mirror" value="1" '. $mirrorchecked . '/>';
$s .= '<div class="clear"></div>';
if (!$globalshortening) {
$s .= '<label id="twitter-shortening-label" for="twitter-shortening">'.t('Shortening method that optimizes the tweet').'</label>';
$s .= '<input id="twitter-shortening" type="checkbox" name="twitter-shortening" value="1" '. $shorteningchecked . '/>';
$s .= '<div class="clear"></div>';
$s .= '<label id="twitter-sendtaglinks-label" for="twitter-sendtaglinks">'.t('Send linked #-tags and @-names to Twitter').'</label>';
$s .= '<input id="twitter-sendtaglinks" type="checkbox" name="twitter-sendtaglinks" value="1" '. $linkschecked . '/>';
$s .= '<div class="clear"></div>';
}
$s .= '</div>';
$s .= '<label id="twitter-import-label" for="twitter-import">'.t('Import the remote timeline').'</label>';
@ -353,234 +332,6 @@ function twitter_post_local(&$a,&$b) {
}
}
if (! function_exists('short_link')) {
function short_link ($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 twitter_shortenmsg($b, $shortlink = false) {
require_once("include/api.php");
require_once("include/bbcode.php");
require_once("include/html2plain.php");
$max_char = 140;
$b['body'] = bb_CleanPictureLinks($b['body']);
// Looking for the first image
$cleaned_body = api_clean_plain_items($b['body']);
$image = '';
if(preg_match("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/is",$cleaned_body,$matches))
$image = $matches[3];
if ($image == '')
if(preg_match("/\[img\](.*?)\[\/img\]/is",$cleaned_body,$matches))
$image = $matches[1];
$multipleimages = (strpos($cleaned_body, "[img") != strrpos($cleaned_body, "[img"));
// When saved into the database the content is sent through htmlspecialchars
// That means that we have to decode all image-urls
$image = htmlspecialchars_decode($image);
$body = $b["body"];
if ($b["title"] != "")
$body = $b["title"]."\n\n".$body;
if (strpos($body, "[bookmark") !== false) {
// splitting the text in two parts:
// before and after the bookmark
$pos = strpos($body, "[bookmark");
$body1 = substr($body, 0, $pos);
$body2 = substr($body, $pos);
// Removing all quotes after the bookmark
// they are mostly only the content after the bookmark.
$body2 = preg_replace("/\[quote\=([^\]]*)\](.*?)\[\/quote\]/ism",'',$body2);
$body2 = preg_replace("/\[quote\](.*?)\[\/quote\]/ism",'',$body2);
$body = $body1.$body2;
}
// Add some newlines so that the message could be cut better
$body = str_replace(array("[quote", "[bookmark", "[/bookmark]", "[/quote]"),
array("\n[quote", "\n[bookmark", "[/bookmark]\n", "[/quote]\n"), $body);
// remove the recycle signs and the names since they aren't helpful on twitter
// recycle 1
$recycle = html_entity_decode("&#x2672; ", ENT_QUOTES, 'UTF-8');
$body = preg_replace( '/'.$recycle.'\[url\=(\w+.*?)\](\w+.*?)\[\/url\]/i', "\n", $body);
// recycle 2 (Test)
$recycle = html_entity_decode("&#x25CC; ", ENT_QUOTES, 'UTF-8');
$body = preg_replace( '/'.$recycle.'\[url\=(\w+.*?)\](\w+.*?)\[\/url\]/i', "\n", $body);
// remove the share element
//$body = preg_replace("/\[share(.*?)\](.*?)\[\/share\]/ism","\n\n$2\n\n",$body);
// At first convert the text to html
$html = bbcode(api_clean_plain_items($body), false, false, 2);
// Then convert it to plain text
$msg = trim(html2plain($html, 0, true));
$msg = html_entity_decode($msg,ENT_QUOTES,'UTF-8');
// Removing multiple newlines
while (strpos($msg, "\n\n\n") !== false)
$msg = str_replace("\n\n\n", "\n\n", $msg);
// Removing multiple spaces
while (strpos($msg, " ") !== false)
$msg = str_replace(" ", " ", $msg);
$origmsg = trim($msg);
// Removing URLs
$msg = preg_replace('/(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/i', "", $msg);
$msg = trim($msg);
$link = '';
// look for bookmark-bbcode and handle it with priority
if(preg_match("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/is",$b['body'],$matches))
$link = $matches[1];
$multiplelinks = (strpos($b['body'], "[bookmark") != strrpos($b['body'], "[bookmark"));
// If there is no bookmark element then take the first link
if ($link == '') {
$links = collecturls($html);
foreach($links AS $singlelink) {
$img_str = fetch_url($singlelink);
$tempfile = tempnam(get_config("system","temppath"), "cache");
file_put_contents($tempfile, $img_str);
$mime = image_type_to_mime_type(exif_imagetype($tempfile));
unlink($tempfile);
if (substr($mime, 0, 6) == "image/") {
$image = $singlelink;
unset($links[$singlelink]);
}
}
if (sizeof($links) > 0) {
reset($links);
$link = current($links);
}
$multiplelinks = (sizeof($links) > 1);
}
$msglink = "";
if ($multiplelinks)
$msglink = $b["plink"];
else if ($link != "")
$msglink = $link;
else if ($multipleimages)
$msglink = $b["plink"];
else if ($image != "")
$msglink = $image;
if (($msglink == "") and strlen($msg) > $max_char)
$msglink = $b["plink"];
// If the message is short enough then don't modify it.
if ((strlen($origmsg) <= $max_char) AND ($msglink == ""))
return(array("msg"=>$origmsg, "image"=>""));
// If the message is short enough and contains a picture then post the picture as well
if ((strlen($origmsg) <= ($max_char - 23)) AND strpos($origmsg, $msglink))
return(array("msg"=>$origmsg, "image"=>$image));
// If the message is short enough and the link exists in the original message don't modify it as well
// -3 because of the bad shortener of twitter
if ((strlen($origmsg) <= ($max_char - 3)) AND strpos($origmsg, $msglink))
return(array("msg"=>$origmsg, "image"=>""));
// Preserve the unshortened link
$orig_link = $msglink;
// Just replace the message link with a 22 character long string
// Twitter calculates with this length
if (trim($msglink) <> '')
$msglink = "1234567890123456789012";
if (strlen(trim($msg." ".$msglink)) > ($max_char)) {
$msg = substr($msg, 0, ($max_char) - (strlen($msglink)));
$lastchar = substr($msg, -1);
$msg = substr($msg, 0, -1);
$pos = strrpos($msg, "\n");
if ($pos > 0)
$msg = substr($msg, 0, $pos);
else if ($lastchar != "\n")
$msg = substr($msg, 0, -3)."...";
// if the post contains a picture and a link then the system tries to cut the post earlier.
// So the link and the picture can be posted.
if (($image != "") AND ($orig_link != $image)) {
$msg2 = substr($msg, 0, ($max_char - 20) - (strlen($msglink)));
$lastchar = substr($msg2, -1);
$msg2 = substr($msg2, 0, -1);
$pos = strrpos($msg2, "\n");
if ($pos > 0)
$msg = substr($msg2, 0, $pos);
else if ($lastchar == "\n")
$msg = trim($msg2);
}
}
// Removing multiple spaces - again
while (strpos($msg, " ") !== false)
$msg = str_replace(" ", " ", $msg);
$msg = trim($msg);
// Removing multiple newlines
//while (strpos($msg, "\n\n") !== false)
// $msg = str_replace("\n\n", "\n", $msg);
// Looking if the link points to an image
$img_str = fetch_url($orig_link);
$tempfile = tempnam(get_config("system","temppath"), "cache");
file_put_contents($tempfile, $img_str);
$mime = image_type_to_mime_type(exif_imagetype($tempfile));
unlink($tempfile);
if (($image == $orig_link) OR (substr($mime, 0, 6) == "image/"))
return(array("msg"=>$msg, "image"=>$orig_link));
else if (($image != $orig_link) AND ($image != "") AND (strlen($msg." ".$msglink) <= ($max_char - 23))) {
if ($shortlink)
$orig_link = short_link($orig_link);
return(array("msg"=>$msg." ".$orig_link, "image"=>$image));
} else {
if ($shortlink)
$orig_link = short_link($orig_link);
return(array("msg"=>$msg." ".$orig_link, "image"=>""));
}
}
function twitter_action($a, $uid, $pid, $action) {
$ckey = get_config('twitter', 'consumerkey');
@ -618,6 +369,8 @@ function twitter_post_hook(&$a,&$b) {
* Post to Twitter
*/
require_once("include/network.php");
if (!get_pconfig($b["uid"],'twitter','import')) {
if($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited']))
return;
@ -687,11 +440,6 @@ function twitter_post_hook(&$a,&$b) {
$csecret = get_config('twitter', 'consumersecret');
$otoken = get_pconfig($b['uid'], 'twitter', 'oauthtoken');
$osecret = get_pconfig($b['uid'], 'twitter', 'oauthsecret');
$intelligent_shortening = get_pconfig($b['uid'], 'twitter', 'intelligent_shortening');
// Global setting overrides this
if (get_config('twitter','intelligent_shortening'))
$intelligent_shortening = get_config('twitter','intelligent_shortening');
if($ckey && $csecret && $otoken && $osecret) {
logger('twitter: we have customer key and oauth stuff, going to send.', LOGGER_DEBUG);
@ -704,90 +452,20 @@ function twitter_post_hook(&$a,&$b) {
require_once('include/bbcode.php');
$tweet = new TwitterOAuth($ckey,$csecret,$otoken,$osecret);
// in theory max char is 140 but T. uses t.co to make links
// longer so we give them 10 characters extra
if (!$intelligent_shortening) {
$max_char = 130; // max. length for a tweet
// we will only work with up to two times the length of the dent
// we can later send to Twitter. 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.
if (! $b['title']=='') {
$tmp = $b['title'] . ' : '. $b['body'];
// $tmp = substr($tmp, 0, 4*$max_char);
} else {
$tmp = $b['body']; // substr($b['body'], 0, 3*$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);
// 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);
$linksenabled = get_pconfig($b['uid'],'twitter','post_taglinks');
// if a #tag is linked, don't send the [url] over to SN
// that is, don't send if the option is not set in the
// connector settings
if ($linksenabled=='0') {
// #-tags
$tmp = preg_replace( '/#\[url\=(\w+.*?)\](\w+.*?)\[\/url\]/i', '#$2', $tmp);
// @-mentions
$tmp = preg_replace( '/@\[url\=(\w+.*?)\](\w+.*?)\[\/url\]/i', '@$2', $tmp);
// recycle 1
$recycle = html_entity_decode("&#x2672; ", ENT_QUOTES, 'UTF-8');
$tmp = preg_replace( '/'.$recycle.'\[url\=(\w+.*?)\](\w+.*?)\[\/url\]/i', $recycle.'$2', $tmp);
// recycle 2 (Test)
$recycle = html_entity_decode("&#x25CC; ", ENT_QUOTES, 'UTF-8');
$tmp = preg_replace( '/'.$recycle.'\[url\=(\w+.*?)\](\w+.*?)\[\/url\]/i', $recycle.'$2', $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);
// find all http or https links in the body of the entry and
// apply the shortener if the link is longer then 20 characters
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_link($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, false, false));
$msg = bbcode($tmp, false, false, true);
$msg = str_replace(array('<br>','<br />'),"\n",$msg);
$msg = strip_tags($msg);
$max_char = 140;
require_once("include/plaintext.php");
$msgarr = plaintext($a, $b, $max_char, true);
$msg = $msgarr["text"];
// quotes not working - let's try this
$msg = html_entity_decode($msg);
if (( strlen($msg) > $max_char) && $max_char > 0) {
$shortlink = short_link( $b['plink'] );
// the new message will be shortened such that "... $shortlink"
// will fit into the character limit
$msg = nl2br(substr($msg, 0, $max_char-strlen($shortlink)-4));
$msg = str_replace(array('<br>','<br />'),' ',$msg);
$e = explode(' ', $msg);
// remove the last word from the cut down message to
// avoid sending cut words to the MicroBlog
array_pop($e);
$msg = implode(' ', $e);
$msg .= '... ' . $shortlink;
}
if (($msg == "") AND isset($msgarr["title"]))
$msg = shortenmsg($msgarr["title"], $max_char - 50);
$msg = trim($msg);
$image = "";
} else {
$msgarr = twitter_shortenmsg($b);
$msg = $msgarr["msg"];
if (isset($msgarr["url"]))
$msg .= "\n".$msgarr["url"];
elseif (isset($msgarr["image"]))
$image = $msgarr["image"];
}
// and now tweet it :-)
if(strlen($msg) and ($image != "")) {
@ -1336,9 +1014,14 @@ function twitter_expand_entities($a, $body, $item, $no_tags = false, $dontinclud
$type = $oembed_data->type;
if ($oembed_data->type == "video") {
$body = str_replace($url->url,
"[video]".$expanded_url."[/video]", $body);
$dontincludemedia = true;
//$body = str_replace($url->url,
// "[video]".$expanded_url."[/video]", $body);
//$dontincludemedia = true;
$type = $oembed_data->type;
$footerurl = $expanded_url;
$footerlink = "[url=".$expanded_url."]".$expanded_url."[/url]";
$body = str_replace($url->url, $footerlink, $body);
} elseif (($oembed_data->type == "photo") AND isset($oembed_data->url) AND !$dontincludemedia) {
$body = str_replace($url->url,
"[url=".$expanded_url."][img]".$oembed_data->url."[/img][/url]",

View file

@ -1,13 +1,15 @@
#wppost-enable-label, #wppost-username-label, #wppost-password-label,
#wppost-blog-label, #wppost-bydefault-label, #wppost-backlink-label {
#wppost-blog-label, #wppost-bydefault-label,
#wppost-backlink-label, #wppost-shortcheck-label {
float: left;
width: 200px;
margin-top: 10px;
}
#wppost-checkbox, #wppost-username, #wppost-password,
#wppost-blog, #wppost-bydefault, #wppost-backlink {
#wppost-blog, #wppost-bydefault,
#wppost-shortcheck, #wppost-backlink {
float: left;
margin-top: 10px;
}

View file

@ -62,9 +62,11 @@ function wppost_settings(&$a,&$s) {
$def_enabled = get_pconfig(local_user(),'wppost','post_by_default');
$back_enabled = get_pconfig(local_user(),'wppost','backlink');
$shortcheck_enabled = get_pconfig(local_user(),'wppost','shortcheck');
$def_checked = (($def_enabled) ? ' checked="checked" ' : '');
$back_checked = (($back_enabled) ? ' checked="checked" ' : '');
$shortcheck_checked = (($shortcheck_enabled) ? ' checked="checked" ' : '');
$wp_username = get_pconfig(local_user(), 'wppost', 'wp_username');
$wp_password = get_pconfig(local_user(), 'wppost', 'wp_password');
@ -108,7 +110,11 @@ function wppost_settings(&$a,&$s) {
$s .= '<div id="wppost-backlink-wrapper">';
$s .= '<label id="wppost-backlink-label" for="wppost-backlink">' . t('Provide a backlink to the Friendica post') . '</label>';
$s .= '<input id="wppost-backlink" type="checkbox" name="wp_backlink" value="1" ' . $back_checked . '/>';
$s .= '</div><div class="clear"></div>';
$s .= '<div id="wppost-shortcheck-wrapper">';
$s .= '<label id="wppost-shortcheck-label" for="wppost-shortcheck">' . t("Don't post messages that are too short") . '</label>';
$s .= '<input id="wppost-shortcheck" type="checkbox" name="wp_shortcheck" value="1" '.$shortcheck_checked.'/>';
$s .= '</div><div class="clear"></div>';
/* provide a submit button */
@ -128,6 +134,7 @@ function wppost_settings_post(&$a,&$b) {
set_pconfig(local_user(),'wppost','wp_password',trim($_POST['wp_password']));
set_pconfig(local_user(),'wppost','wp_blog',trim($_POST['wp_blog']));
set_pconfig(local_user(),'wppost','backlink',trim($_POST['wp_backlink']));
set_pconfig(local_user(),'wppost','shortcheck',trim($_POST['wp_shortcheck']));
}
@ -184,9 +191,32 @@ function wppost_send(&$a,&$b) {
require_once('include/bbcode.php');
require_once('include/html2plain.php');
require_once('include/plaintext.php');
$wptitle = trim($b['title']);
if (intval(get_pconfig($b['uid'],'wppost','shortcheck'))) {
// Checking, if its a post that is worth a blog post
$postentry = false;
$siteinfo = get_attached_data($b["body"]);
// Is it a link to an aricle, a video or a photo?
if (isset($siteinfo["type"]))
if (in_array($siteinfo["type"], array("link", "audio", "video", "photo")))
$postentry = true;
// Does it have a title?
if ($wptitle != "")
$postentry = true;
// Is it larger than 500 characters?
if (strlen($b['body']) > 500)
$postentry = true;
if (!$postentry)
return;
}
// If the title is empty then try to guess
if ($wptitle == '') {
// Take the description from the bookmark