Massive reworking of the export connectors using the plaintext function, improving of the tumblr export, option to prevent too short wordpress postings

This commit is contained in:
Michael Vogel 2014-05-29 12:42:25 +02:00
parent 8ce597cb4d
commit 021d6265cd
11 changed files with 191 additions and 880 deletions

View file

@ -5,6 +5,7 @@
* Description: Posts to app.net with the help of ifttt.com * Description: Posts to app.net with the help of ifttt.com
* Version: 0.1 * Version: 0.1
* Author: Michael Vogel <https://pirati.ca/profile/heluecht> * Author: Michael Vogel <https://pirati.ca/profile/heluecht>
* Status: Unsupported
*/ */
function appnetpost_install() { function appnetpost_install() {

View file

@ -1,8 +1,8 @@
<?php <?php
/** /**
* Name: Buffer Post Connector * Name: Buffer Post Connector
* Description: Post to Buffer * Description: Post to Buffer (Linkedin, App.net, Google+, Facebook, Twitter)
* Version: 0.1 alpha * Version: 0.2
* Author: Michael Vogel <http://pirati.ca/profile/heluecht> * Author: Michael Vogel <http://pirati.ca/profile/heluecht>
*/ */
require('addon/buffer/bufferapp.php'); require('addon/buffer/bufferapp.php');

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 App $a
* @param object $b * @param object $b
@ -685,11 +558,12 @@ function fbpost_post_hook(&$a,&$b) {
logger('fbpost_post_hook: original msg=' . $msg, LOGGER_DATA); logger('fbpost_post_hook: original msg=' . $msg, LOGGER_DATA);
if ($toplevel) { if ($toplevel) {
$msgarr = fbpost_createmsg($b); require_once("include/plaintext.php");
$msg = $msgarr["msg"]; $msgarr = plaintext($a, $b, 0, false);
$link = $msgarr["link"]; $msg = $msgarr["text"];
$link = $msgarr["url"];
$image = $msgarr["image"]; $image = $msgarr["image"];
$linkname = $msgarr["linkname"]; $linkname = $msgarr["title"];
// Fallback - if message is empty // Fallback - if message is empty
if(!strlen($msg)) if(!strlen($msg))

View file

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

View file

@ -257,7 +257,6 @@ function gpluspost_send(&$a,&$b) {
if($username && $password) { if($username && $password) {
require_once("addon/gpluspost/postToGooglePlus.php"); require_once("addon/gpluspost/postToGooglePlus.php");
require_once("include/plaintext.php"); require_once("include/plaintext.php");
require_once("include/network.php");
$item = $b; $item = $b;

View file

@ -57,24 +57,24 @@ function openstreetmap_location($a, &$item) {
$xml = parse_xml_string($s); $xml = parse_xml_string($s);
if($xml->geoname->lat && $xml->geoname->lng){ if($xml->geoname->lat && $xml->geoname->lng){
$item['coord'] = $xml->geoname->lat . ' ' . $xml->geoname->lng; $item['coord'] = $xml->geoname->lat.' '.$xml->geoname->lng;
} }
} }
} }
$location = (($item['location']) ? '<a target="map" title="' . $item['location'] . '" href="'.$tmsserver.'?q=' . urlencode($item['location']) . '">' . $item['location'] . '</a>' : ''); $location = (($item['location']) ? '<a target="map" title="'.$item['location'].'" href="'.$tmsserver.'?q='.urlencode($item['location']).'">'.$item['location'].'</a>' : '');
if($item['coord']) { if($item['coord']) {
$coords = explode(' ', $item['coord']); $coords = explode(' ', $item['coord']);
if(count($coords) > 1) { 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(strlen($coord)) {
if($location) if($location)
$location .= '<br /><span class="smalltext">(' . $coord . ')</span>'; $location .= ' <span class="smalltext">('.$coord.')</span>';
else else
$location = '<span class="smalltext">' . $coord . '</span>'; $location = '<span class="smalltext">'.$coord.'</span>';
} }
$item['html'] = $location; $item['html'] = $location;
return; return;

View file

@ -168,10 +168,8 @@ function statusnet_settings_post ($a,$post) {
del_pconfig(local_user(), 'statusnet', 'oauthtoken'); del_pconfig(local_user(), 'statusnet', 'oauthtoken');
del_pconfig(local_user(), 'statusnet', 'oauthsecret'); del_pconfig(local_user(), 'statusnet', 'oauthsecret');
del_pconfig(local_user(), 'statusnet', 'baseapi'); del_pconfig(local_user(), 'statusnet', 'baseapi');
del_pconfig(local_user(), 'statusnet', 'post_taglinks');
del_pconfig(local_user(), 'statusnet', 'lastid'); del_pconfig(local_user(), 'statusnet', 'lastid');
del_pconfig(local_user(), 'statusnet', 'mirror_posts'); 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', 'import');
del_pconfig(local_user(), 'statusnet', 'create_user'); del_pconfig(local_user(), 'statusnet', 'create_user');
del_pconfig(local_user(), 'statusnet', 'own_id'); 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 // 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',intval($_POST['statusnet-enable']));
set_pconfig(local_user(),'statusnet','post_by_default',intval($_POST['statusnet-default'])); 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', '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', 'import', intval($_POST['statusnet-import']));
set_pconfig(local_user(), 'statusnet', 'create_user', intval($_POST['statusnet-create_user'])); set_pconfig(local_user(), 'statusnet', 'create_user', intval($_POST['statusnet-create_user']));
info( t('StatusNet settings updated.') . EOL); info( t('StatusNet settings updated.') . EOL);
@ -268,20 +264,16 @@ function statusnet_settings(&$a,&$s) {
* 3) Checkbox for "Send public notices (respect size limitation) * 3) Checkbox for "Send public notices (respect size limitation)
*/ */
$api = get_pconfig(local_user(), 'statusnet', 'baseapi'); $api = get_pconfig(local_user(), 'statusnet', 'baseapi');
$ckey = get_pconfig(local_user(), 'statusnet', 'consumerkey' ); $ckey = get_pconfig(local_user(), 'statusnet', 'consumerkey');
$csecret = get_pconfig(local_user(), 'statusnet', 'consumersecret' ); $csecret = get_pconfig(local_user(), 'statusnet', 'consumersecret');
$otoken = get_pconfig(local_user(), 'statusnet', 'oauthtoken' ); $otoken = get_pconfig(local_user(), 'statusnet', 'oauthtoken');
$osecret = get_pconfig(local_user(), 'statusnet', 'oauthsecret' ); $osecret = get_pconfig(local_user(), 'statusnet', 'oauthsecret');
$enabled = get_pconfig(local_user(), 'statusnet', 'post'); $enabled = get_pconfig(local_user(), 'statusnet', 'post');
$checked = (($enabled) ? ' checked="checked" ' : ''); $checked = (($enabled) ? ' checked="checked" ' : '');
$defenabled = get_pconfig(local_user(),'statusnet','post_by_default'); $defenabled = get_pconfig(local_user(),'statusnet','post_by_default');
$defchecked = (($defenabled) ? ' checked="checked" ' : ''); $defchecked = (($defenabled) ? ' checked="checked" ' : '');
$linksenabled = get_pconfig(local_user(),'statusnet','post_taglinks');
$linkschecked = (($linksenabled) ? ' checked="checked" ' : '');
$mirrorenabled = get_pconfig(local_user(),'statusnet','mirror_posts'); $mirrorenabled = get_pconfig(local_user(),'statusnet','mirror_posts');
$mirrorchecked = (($mirrorenabled) ? ' checked="checked" ' : ''); $mirrorchecked = (($mirrorenabled) ? ' checked="checked" ' : '');
$shorteningenabled = get_pconfig(local_user(),'statusnet','intelligent_shortening');
$shorteningchecked = (($shorteningenabled) ? ' checked="checked" ' : '');
$importenabled = get_pconfig(local_user(),'statusnet','import'); $importenabled = get_pconfig(local_user(),'statusnet','import');
$importchecked = (($importenabled) ? ' checked="checked" ' : ''); $importchecked = (($importenabled) ? ' checked="checked" ' : '');
$create_userenabled = get_pconfig(local_user(),'statusnet','create_user'); $create_userenabled = get_pconfig(local_user(),'statusnet','create_user');
@ -289,9 +281,6 @@ function statusnet_settings(&$a,&$s) {
$css = (($enabled) ? '' : '-disabled'); $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 .= '<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 .= '<img class="connector'.$css.'" src="images/gnusocial.png" /><h3 class="connector">'. t('StatusNet Import/Export/Mirror').'</h3>';
$s .= '</span>'; $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 .= '<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 .= '<input id="statusnet-mirror" type="checkbox" name="statusnet-mirror" value="1" '. $mirrorchecked . '/>';
$s .= '<div class="clear"></div>'; $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 .= '</div>';
$s .= '<label id="statusnet-import-label" for="statusnet-import">'.t('Import the remote timeline').'</label>'; $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) { function statusnet_action($a, $uid, $pid, $action) {
$api = get_pconfig($uid, 'statusnet', 'baseapi'); $api = get_pconfig($uid, 'statusnet', 'baseapi');
$ckey = get_pconfig($uid, 'statusnet', 'consumerkey'); $ckey = get_pconfig($uid, 'statusnet', 'consumerkey');
@ -740,11 +533,6 @@ function statusnet_post_hook(&$a,&$b) {
$csecret = get_pconfig($b['uid'], 'statusnet', 'consumersecret'); $csecret = get_pconfig($b['uid'], 'statusnet', 'consumersecret');
$otoken = get_pconfig($b['uid'], 'statusnet', 'oauthtoken'); $otoken = get_pconfig($b['uid'], 'statusnet', 'oauthtoken');
$osecret = get_pconfig($b['uid'], 'statusnet', 'oauthsecret'); $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) { if($ckey && $csecret && $otoken && $osecret) {
@ -755,119 +543,34 @@ function statusnet_post_hook(&$a,&$b) {
require_once('include/bbcode.php'); require_once('include/bbcode.php');
$dent = new StatusNetOAuth($api,$ckey,$csecret,$otoken,$osecret); $dent = new StatusNetOAuth($api,$ckey,$csecret,$otoken,$osecret);
$max_char = $dent->get_maxlength(); // max. length for a dent $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 = ""; $tempfile = "";
$intelligent_shortening = get_config('statusnet','intelligent_shortening'); require_once("include/plaintext.php");
if (!$intelligent_shortening) { require_once("include/network.php");
if (! $b['title']=='') { $msgarr = plaintext($a, $b, $max_char, true);
$tmp = $b['title'].": \n".$b['body']; $msg = $msgarr["text"];
// $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);
// quotes not working - let's try this if (($msg == "") AND isset($msgarr["title"]))
$msg = html_entity_decode($msg); $msg = shortenmsg($msgarr["title"], $max_char - 50);
if (( strlen($msg) > $max_char) && $max_char > 0) { $image = "";
$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;
}
$msg = trim($msg); if (isset($msgarr["url"])) {
$postdata = array('status' => $msg); if ((strlen($msgarr["url"]) > 20) AND
} else { ((strlen($msg." \n".$msgarr["url"]) > $max_char)))
/* $msgarr = statusnet_shortenmsg($b, $max_char); $msg .= " \n".short_link($msgarr["url"]);
$msg = $msgarr["msg"]; else
$msg .= " \n".$msgarr["url"];
} elseif (isset($msgarr["image"]))
$image = $msgarr["image"]; $image = $msgarr["image"];
*/
require_once("include/plaintext.php");
require_once("include/network.php");
$msgarr = plaintext($a, $b, $max_char, true);
$msg = $msgarr["text"];
if (($msg == "") AND isset($msgarr["title"])) if ($image != "") {
$msg = shortenmsg($msgarr["title"], $max_char - 50); $img_str = fetch_url($image);
$tempfile = tempnam(get_config("system","temppath"), "cache");
$image = ""; file_put_contents($tempfile, $img_str);
$postdata = array("status" => $msg, "media[]" => $tempfile);
if (isset($msgarr["url"])) { } else
if ((strlen($msgarr["url"]) > 20) AND $postdata = array("status"=>$msg);
((strlen($msg." ".$msgarr["url"]) > $max_char)))
$msg .= " ".short_link($msgarr["url"]);
else
$msg .= " ".$msgarr["url"];
} elseif (isset($msgarr["image"]))
$image = $msgarr["image"];
if ($image != "") {
$img_str = fetch_url($image);
$tempfile = tempnam(get_config("system","temppath"), "cache");
file_put_contents($tempfile, $img_str);
$postdata = array("status" => $msg, "media[]" => $tempfile);
} else
$postdata = array("status"=>$msg);
}
// and now dent it :-) // and now dent it :-)
if(strlen($msg)) { if(strlen($msg)) {
@ -1712,6 +1415,7 @@ function statusnet_convertmsg($a, $body, $no_tags = false) {
$footer = ""; $footer = "";
$footerurl = ""; $footerurl = "";
$footerlink = "";
$type = ""; $type = "";
if ($links) { if ($links) {
@ -1720,14 +1424,18 @@ function statusnet_convertmsg($a, $body, $no_tags = false) {
$expanded_url = original_url($match[1]); $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 == "") if ($type == "")
$type = $oembed_data->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, $footerlink, $body);
$body = str_replace($search, "[video]".$expanded_url."[/video]", $body); } elseif (($oembed_data->type == "photo") AND isset($oembed_data->url) AND !$dontincludemedia)
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); $body = str_replace($search, "[url=".$expanded_url."][img]".$oembed_data->url."[/img][/url]", $body);
elseif ($oembed_data->type != "link") elseif ($oembed_data->type != "link")
$body = str_replace($search, "[url=".$expanded_url."]".$expanded_url."[/url]", $body); $body = str_replace($search, "[url=".$expanded_url."]".$expanded_url."[/url]", $body);
@ -1747,7 +1455,7 @@ function statusnet_convertmsg($a, $body, $no_tags = false) {
$footerurl = $expanded_url; $footerurl = $expanded_url;
$footerlink = "[url=".$expanded_url."]".$expanded_url."[/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 * Name: Tumblr Post Connector
* Description: Post to Tumblr * Description: Post to Tumblr
* Version: 1.0 * Version: 2.0
* Author: Mike Macgirvin <http://macgirvin.com/profile/mike> * Author: Mike Macgirvin <http://macgirvin.com/profile/mike>
* Author: Michael Vogel <https://pirati.ca/profile/heluecht>
*/ */
require_once('library/OAuth1.php'); require_once('library/OAuth1.php');
@ -316,54 +317,68 @@ function tumblr_send(&$a,&$b) {
if(count($tag_arr)) if(count($tag_arr))
$tags = implode(',',$tag_arr); $tags = implode(',',$tag_arr);
$link = "";
$video = false;
$title = trim($b['title']); $title = trim($b['title']);
require_once('include/plaintext.php');
// Checking for a bookmark $siteinfo = get_attached_data($b["body"]);
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')));
}
$params = array( $params = array(
'format' => 'html', 'state' => 'published',
'tags' => $tags,
'tweet' => 'off', 'tweet' => 'off',
'tags' => $tags); 'format' => 'html');
if (($link != '') and $video) { if (!isset($siteinfo["type"]))
$params['type'] = "video"; $siteinfo["type"] = "";
$params['embed'] = $link;
if ($title != '') if (($title == "") AND isset($siteinfo["title"]))
$params['caption'] = '<h1><a href="'.$link.'">'.$title. $title = $siteinfo["title"];
"</a></h1><p>".bbcode($body, false, false, 4)."</p>";
else 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); $params['caption'] = bbcode($body, false, false, 4);
} else if (($link != '') and !$video) {
$params['type'] = "link"; if (isset($siteinfo["url"]))
$params['title'] = $title; $params['link'] = $siteinfo["url"];
$params['url'] = $link;
$params['description'] = bbcode($b["body"], false, false, 4); $params['source'] = $siteinfo["image"];
} else { break;
$params['type'] = "text"; case "link":
$params['title'] = $title; $params['type'] = "link";
$params['body'] = bbcode($b['body'], false, false, 4); $params['title'] = $title;
$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_key = get_config('tumblr','consumer_key');
$consumer_secret = get_config('tumblr','consumer_secret'); $consumer_secret = get_config('tumblr','consumer_secret');
@ -371,9 +386,8 @@ function tumblr_send(&$a,&$b) {
// Make an API call with the TumblrOAuth instance. // Make an API call with the TumblrOAuth instance.
$x = $tum_oauth->post($tmbl_blog,$params); $x = $tum_oauth->post($tmbl_blog,$params);
$ret_code = $tum_oauth->http_code; $ret_code = $tum_oauth->http_code;
//print_r($params);
if($ret_code == 201) if($ret_code == 201)
logger('tumblr_send: success'); logger('tumblr_send: success');
elseif($ret_code == 403) 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', 'oauthsecret');
del_pconfig(local_user(), 'twitter', 'post'); del_pconfig(local_user(), 'twitter', 'post');
del_pconfig(local_user(), 'twitter', 'post_by_default'); 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', 'lastid');
del_pconfig(local_user(), 'twitter', 'mirror_posts'); 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', 'import');
del_pconfig(local_user(), 'twitter', 'create_user'); del_pconfig(local_user(), 'twitter', 'create_user');
del_pconfig(local_user(), 'twitter', 'own_id'); 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', 'oauthtoken', $token['oauth_token']);
set_pconfig(local_user(),'twitter', 'oauthsecret', $token['oauth_token_secret']); set_pconfig(local_user(),'twitter', 'oauthsecret', $token['oauth_token_secret']);
set_pconfig(local_user(),'twitter', 'post', 1); 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 // reload the Addon Settings page, if we don't do it see Bug #42
goaway($a->get_baseurl().'/settings/connectors'); goaway($a->get_baseurl().'/settings/connectors');
} else { } else {
@ -194,9 +191,7 @@ function twitter_settings_post ($a,$post) {
// to post a tweet for every new __public__ posting to the wall // 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',intval($_POST['twitter-enable']));
set_pconfig(local_user(),'twitter','post_by_default',intval($_POST['twitter-default'])); 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', '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', 'import', intval($_POST['twitter-import']));
set_pconfig(local_user(), 'twitter', 'create_user', intval($_POST['twitter-create_user'])); set_pconfig(local_user(), 'twitter', 'create_user', intval($_POST['twitter-create_user']));
info( t('Twitter settings updated.') . EOL); info( t('Twitter settings updated.') . EOL);
@ -219,12 +214,8 @@ function twitter_settings(&$a,&$s) {
$checked = (($enabled) ? ' checked="checked" ' : ''); $checked = (($enabled) ? ' checked="checked" ' : '');
$defenabled = get_pconfig(local_user(),'twitter','post_by_default'); $defenabled = get_pconfig(local_user(),'twitter','post_by_default');
$defchecked = (($defenabled) ? ' checked="checked" ' : ''); $defchecked = (($defenabled) ? ' checked="checked" ' : '');
$linksenabled = get_pconfig(local_user(),'twitter','post_taglinks');
$linkschecked = (($linksenabled) ? ' checked="checked" ' : '');
$mirrorenabled = get_pconfig(local_user(),'twitter','mirror_posts'); $mirrorenabled = get_pconfig(local_user(),'twitter','mirror_posts');
$mirrorchecked = (($mirrorenabled) ? ' checked="checked" ' : ''); $mirrorchecked = (($mirrorenabled) ? ' checked="checked" ' : '');
$shorteningenabled = get_pconfig(local_user(),'twitter','intelligent_shortening');
$shorteningchecked = (($shorteningenabled) ? ' checked="checked" ' : '');
$importenabled = get_pconfig(local_user(),'twitter','import'); $importenabled = get_pconfig(local_user(),'twitter','import');
$importchecked = (($importenabled) ? ' checked="checked" ' : ''); $importchecked = (($importenabled) ? ' checked="checked" ' : '');
$create_userenabled = get_pconfig(local_user(),'twitter','create_user'); $create_userenabled = get_pconfig(local_user(),'twitter','create_user');
@ -232,8 +223,6 @@ function twitter_settings(&$a,&$s) {
$css = (($enabled) ? '' : '-disabled'); $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 .= '<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 .= '<img class="connector'.$css.'" src="images/twitter.png" /><h3 class="connector">'. t('Twitter Import/Export/Mirror').'</h3>';
$s .= '</span>'; $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 .= '<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 .= '<input id="twitter-mirror" type="checkbox" name="twitter-mirror" value="1" '. $mirrorchecked . '/>';
$s .= '<div class="clear"></div>'; $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 .= '</div>';
$s .= '<label id="twitter-import-label" for="twitter-import">'.t('Import the remote timeline').'</label>'; $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) { function twitter_action($a, $uid, $pid, $action) {
$ckey = get_config('twitter', 'consumerkey'); $ckey = get_config('twitter', 'consumerkey');
@ -618,6 +369,8 @@ function twitter_post_hook(&$a,&$b) {
* Post to Twitter * Post to Twitter
*/ */
require_once("include/network.php");
if (!get_pconfig($b["uid"],'twitter','import')) { if (!get_pconfig($b["uid"],'twitter','import')) {
if($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited'])) if($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited']))
return; return;
@ -687,11 +440,6 @@ function twitter_post_hook(&$a,&$b) {
$csecret = get_config('twitter', 'consumersecret'); $csecret = get_config('twitter', 'consumersecret');
$otoken = get_pconfig($b['uid'], 'twitter', 'oauthtoken'); $otoken = get_pconfig($b['uid'], 'twitter', 'oauthtoken');
$osecret = get_pconfig($b['uid'], 'twitter', 'oauthsecret'); $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) { if($ckey && $csecret && $otoken && $osecret) {
logger('twitter: we have customer key and oauth stuff, going to send.', LOGGER_DEBUG); 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'); require_once('include/bbcode.php');
$tweet = new TwitterOAuth($ckey,$csecret,$otoken,$osecret); $tweet = new TwitterOAuth($ckey,$csecret,$otoken,$osecret);
// in theory max char is 140 but T. uses t.co to make links $max_char = 140;
// longer so we give them 10 characters extra require_once("include/plaintext.php");
if (!$intelligent_shortening) { $msgarr = plaintext($a, $b, $max_char, true);
$max_char = 130; // max. length for a tweet $msg = $msgarr["text"];
// 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);
// quotes not working - let's try this if (($msg == "") AND isset($msgarr["title"]))
$msg = html_entity_decode($msg); $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;
}
$msg = trim($msg); $image = "";
$image = "";
} else { if (isset($msgarr["url"]))
$msgarr = twitter_shortenmsg($b); $msg .= "\n".$msgarr["url"];
$msg = $msgarr["msg"]; elseif (isset($msgarr["image"]))
$image = $msgarr["image"]; $image = $msgarr["image"];
}
// and now tweet it :-) // and now tweet it :-)
if(strlen($msg) and ($image != "")) { if(strlen($msg) and ($image != "")) {
@ -1336,9 +1014,14 @@ function twitter_expand_entities($a, $body, $item, $no_tags = false, $dontinclud
$type = $oembed_data->type; $type = $oembed_data->type;
if ($oembed_data->type == "video") { if ($oembed_data->type == "video") {
$body = str_replace($url->url, //$body = str_replace($url->url,
"[video]".$expanded_url."[/video]", $body); // "[video]".$expanded_url."[/video]", $body);
$dontincludemedia = true; //$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) { } elseif (($oembed_data->type == "photo") AND isset($oembed_data->url) AND !$dontincludemedia) {
$body = str_replace($url->url, $body = str_replace($url->url,
"[url=".$expanded_url."][img]".$oembed_data->url."[/img][/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-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; float: left;
width: 200px; width: 200px;
margin-top: 10px; margin-top: 10px;
} }
#wppost-checkbox, #wppost-username, #wppost-password, #wppost-checkbox, #wppost-username, #wppost-password,
#wppost-blog, #wppost-bydefault, #wppost-backlink { #wppost-blog, #wppost-bydefault,
#wppost-shortcheck, #wppost-backlink {
float: left; float: left;
margin-top: 10px; margin-top: 10px;
} }

View file

@ -46,25 +46,27 @@ function wppost_jot_nets(&$a,&$b) {
function wppost_settings(&$a,&$s) { function wppost_settings(&$a,&$s) {
if(! local_user()) if(! local_user())
return; return;
/* Add our stylesheet to the page so we can make our settings look nice */ /* Add our stylesheet to the page so we can make our settings look nice */
$a->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . $a->get_baseurl() . '/addon/wppost/wppost.css' . '" media="all" />' . "\r\n"; $a->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . $a->get_baseurl() . '/addon/wppost/wppost.css' . '" media="all" />' . "\r\n";
/* Get the current state of our config variables */ /* Get the current state of our config variables */
$enabled = get_pconfig(local_user(),'wppost','post'); $enabled = get_pconfig(local_user(),'wppost','post');
$checked = (($enabled) ? ' checked="checked" ' : ''); $checked = (($enabled) ? ' checked="checked" ' : '');
$css = (($enabled) ? '' : '-disabled'); $css = (($enabled) ? '' : '-disabled');
$def_enabled = get_pconfig(local_user(),'wppost','post_by_default'); $def_enabled = get_pconfig(local_user(),'wppost','post_by_default');
$back_enabled = get_pconfig(local_user(),'wppost','backlink'); $back_enabled = get_pconfig(local_user(),'wppost','backlink');
$shortcheck_enabled = get_pconfig(local_user(),'wppost','shortcheck');
$def_checked = (($def_enabled) ? ' checked="checked" ' : ''); $def_checked = (($def_enabled) ? ' checked="checked" ' : '');
$back_checked = (($back_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_username = get_pconfig(local_user(), 'wppost', 'wp_username');
$wp_password = get_pconfig(local_user(), 'wppost', 'wp_password'); $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 .= '<div id="wppost-backlink-wrapper">';
$s .= '<label id="wppost-backlink-label" for="wppost-backlink">' . t('Provide a backlink to the Friendica post') . '</label>'; $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 .= '<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>'; $s .= '</div><div class="clear"></div>';
/* provide a submit button */ /* 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_password',trim($_POST['wp_password']));
set_pconfig(local_user(),'wppost','wp_blog',trim($_POST['wp_blog'])); 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','backlink',trim($_POST['wp_backlink']));
set_pconfig(local_user(),'wppost','shortcheck',trim($_POST['wp_shortcheck']));
} }
@ -166,14 +173,14 @@ function wppost_post_local(&$a,&$b) {
function wppost_send(&$a,&$b) { function wppost_send(&$a,&$b) {
if($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited'])) if($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited']))
return; return;
if(! strstr($b['postopts'],'wppost')) if(! strstr($b['postopts'],'wppost'))
return; return;
if($b['parent'] != $b['id']) if($b['parent'] != $b['id'])
return; return;
$wp_username = xmlify(get_pconfig($b['uid'],'wppost','wp_username')); $wp_username = xmlify(get_pconfig($b['uid'],'wppost','wp_username'));
@ -184,9 +191,32 @@ function wppost_send(&$a,&$b) {
require_once('include/bbcode.php'); require_once('include/bbcode.php');
require_once('include/html2plain.php'); require_once('include/html2plain.php');
require_once('include/plaintext.php');
$wptitle = trim($b['title']); $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 the title is empty then try to guess
if ($wptitle == '') { if ($wptitle == '') {
// Take the description from the bookmark // Take the description from the bookmark