*/
require('addon/buffer/bufferapp.php');
function buffer_install() {
register_hook('post_local', 'addon/buffer/buffer.php', 'buffer_post_local');
register_hook('notifier_normal', 'addon/buffer/buffer.php', 'buffer_send');
register_hook('jot_networks', 'addon/buffer/buffer.php', 'buffer_jot_nets');
register_hook('connector_settings', 'addon/buffer/buffer.php', 'buffer_settings');
register_hook('connector_settings_post', 'addon/buffer/buffer.php', 'buffer_settings_post');
}
function buffer_uninstall() {
unregister_hook('post_local', 'addon/buffer/buffer.php', 'buffer_post_local');
unregister_hook('notifier_normal', 'addon/buffer/buffer.php', 'buffer_send');
unregister_hook('jot_networks', 'addon/buffer/buffer.php', 'buffer_jot_nets');
unregister_hook('connector_settings', 'addon/buffer/buffer.php', 'buffer_settings');
unregister_hook('connector_settings_post', 'addon/buffer/buffer.php', 'buffer_settings_post');
}
function buffer_module() {}
function buffer_content(&$a) {
if(! local_user()) {
notice( t('Permission denied.') . EOL);
return '';
}
require_once("mod/settings.php");
settings_init($a);
if (isset($a->argv[1]))
switch ($a->argv[1]) {
case "connect":
$o = buffer_connect($a);
break;
default:
$o = print_r($a->argv, true);
break;
}
else
$o = buffer_connect($a);
return $o;
}
function buffer_plugin_admin(&$a, &$o){
$t = get_markup_template( "admin.tpl", "addon/buffer/" );
$o = replace_macros($t, array(
'$submit' => t('Save Settings'),
// name, label, value, help, [extra values]
'$client_id' => array('client_id', t('Client ID'), get_config('buffer', 'client_id' ), ''),
'$client_secret' => array('client_secret', t('Client Secret'), get_config('buffer', 'client_secret' ), ''),
));
}
function buffer_connect(&$a) {
if (isset($_REQUEST["error"])) {
$o = t('Error when registering buffer connection:')." ".$_REQUEST["error"];
return $o;
}
// Start a session. This is necessary to hold on to a few keys the callback script will also need
session_start();
// Define the needed keys
$client_id = get_config('buffer','client_id');
$client_secret = get_config('buffer','client_secret');
// The callback URL is the script that gets called after the user authenticates with buffer
$callback_url = $a->get_baseurl()."/buffer/connect";
$buffer = new BufferApp($client_id, $client_secret, $callback_url);
if (!$buffer->ok) {
$o .= 'Connect to Buffer!';
} else {
logger("buffer_connect: authenticated");
$o .= t("You are now authenticated to buffer. ");
$o .= '
'.t("return to the connector page").'';
set_pconfig(local_user(), 'buffer','access_token', $buffer->access_token);
}
return($o);
}
function buffer_jot_nets(&$a,&$b) {
if(! local_user())
return;
$buffer_post = get_pconfig(local_user(),'buffer','post');
if(intval($buffer_post) == 1) {
$buffer_defpost = get_pconfig(local_user(),'buffer','post_by_default');
$selected = ((intval($buffer_defpost) == 1) ? ' checked="checked" ' : '');
$b .= '
'
. t('Post to Buffer') . '
';
}
}
function buffer_settings(&$a,&$s) {
if(! local_user())
return;
/* Add our stylesheet to the page so we can make our settings look nice */
$a->page['htmlhead'] .= '' . "\r\n";
/* Get the current state of our config variables */
$enabled = get_pconfig(local_user(),'buffer','post');
$checked = (($enabled) ? ' checked="checked" ' : '');
$css = (($enabled) ? '' : '-disabled');
$def_enabled = get_pconfig(local_user(),'buffer','post_by_default');
$def_checked = (($def_enabled) ? ' checked="checked" ' : '');
/* Add some HTML to the existing form */
$s .= '';
$s .= '![](images/buffer.png)
'. t('Buffer Export').'
';
$s .= '';
$s .= '';
$s .= '
';
$s .= '![](images/buffer.png)
'. t('Buffer Export').'
';
$s .= '';
$client_id = get_config("buffer", "client_id");
$client_secret = get_config("buffer", "client_secret");
$access_token = get_pconfig(local_user(), "buffer", "access_token");
$s .= '
';
if ($access_token == "") {
$s .= '
';
} else {
$s .= '
';
$s .= '';
$s .= '';
$s .= '
';
$s .= '
';
$s .= '';
$s .= '';
$s .= '
';
$s .= '
';
$s .= '';
$s .= '';
$s .= '
';
$buffer = new BufferApp($client_id, $client_secret, $callback_url, $access_token);
$profiles = $buffer->go('/profiles');
if (is_array($profiles)) {
$s .= '
';
$s .= t("Posts are going to all accounts that are enabled by default:");
$s .= "
";
foreach ($profiles as $profile) {
if (!$profile->default)
continue;
$s .= "- ";
//$s .= "
";
$s .= " ".$profile->formatted_username." (".$profile->formatted_service.")";
$s .= " ";
}
$s .= "
";
$s .= '
';
}
}
$s .= '
';
/* provide a submit button */
$s .= '
';
}
function buffer_settings_post(&$a,&$b) {
if(x($_POST,'buffer-submit')) {
if(x($_POST,'buffer_delete')) {
set_pconfig(local_user(),'buffer','access_token','');
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");
}
}
}
function buffer_post_local(&$a,&$b) {
if((! local_user()) || (local_user() != $b['uid']))
return;
$buffer_post = intval(get_pconfig(local_user(),'buffer','post'));
$buffer_enable = (($buffer_post && x($_REQUEST,'buffer_enable')) ? intval($_REQUEST['buffer_enable']) : 0);
if($_REQUEST['api_source'] && intval(get_pconfig(local_user(),'buffer','post_by_default')))
$buffer_enable = 1;
if(! $buffer_enable)
return;
if(strlen($b['postopts']))
$b['postopts'] .= ',';
$b['postopts'] .= 'buffer';
}
function buffer_send(&$a,&$b) {
if($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited']))
return;
logger("buffer_send: parameter ".print_r($b, true), LOGGER_DATA);
if(! strstr($b['postopts'],'buffer'))
return;
if($b['parent'] != $b['id'])
return;
// if post comes from buffer don't send it back
if($b['app'] == "Buffer")
return;
$client_id = get_config("buffer", "client_id");
$client_secret = get_config("buffer", "client_secret");
$access_token = get_pconfig($b['uid'], "buffer","access_token");
if($access_token) {
$buffer = new BufferApp($client_id, $client_secret, $callback_url, $access_token);
$result = q("SELECT `installed` FROM `addon` WHERE `name` = 'privacy_image_cache' AND `installed`");
$image_cache = (count($result) > 0);
require_once("include/plaintext.php");
require_once("include/network.php");
$profiles = $buffer->go('/profiles');
if (is_array($profiles)) {
foreach ($profiles as $profile) {
if (!$profile->default)
continue;
switch ($profile->service) {
case 'appdotnet':
$limit = 256;
$markup = false;
$includedlinks = true;
break;
case 'facebook':
$limit = 0;
$markup = false;
$includedlinks = false;
break;
case 'google':
$limit = 0;
$markup = true;
$includedlinks = false;
break;
case 'twitter':
$limit = 140;
$markup = false;
$includedlinks = true;
break;
case 'linkedin':
$limit = 700;
$markup = false;
$includedlinks = true;
break;
}
$item = $b;
// Markup for Google+
if ($markup) {
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"]);
}
$post = plaintext($a, $item, $limit, $includedlinks);
logger("buffer_send: converted message ".$b["id"]." result: ".print_r($post, true), LOGGER_DEBUG);
// The image cache is used as a sanitizer. Buffer seems to be really picky about pictures
if ($image_cache) {
require_once("addon/privacy_image_cache/privacy_image_cache.php");
if (isset($post["image"]))
$post["image"] = $a->get_baseurl() . "/privacy_image_cache/".privacy_image_cache_cachename($post["image"]);
if (isset($post["preview"]))
$post["preview"] = $a->get_baseurl() . "/privacy_image_cache/".privacy_image_cache_cachename($post["preview"]);
}
//if ($profile->service == "twitter") {
if ($includedlinks) {
if (isset($post["url"]))
$post["url"] = short_link($post["url"]);
if (isset($post["image"]))
$post["image"] = short_link($post["image"]);
if (isset($post["preview"]))
$post["preview"] = short_link($post["preview"]);
}
// Seems like a bug to me
// Buffer doesn't add links to Twitter and App.net (but pictures)
//if ($includedlinks AND isset($post["url"]))
if (($profile->service == "twitter") AND isset($post["url"]))
$post["text"] .= " ".$post["url"];
elseif (($profile->service == "appdotnet") AND isset($post["url"]) AND isset($post["title"])) {
$maxdesclength = $limit - (strlen($post["text"]) + 1);
if (strlen($post["title"]) > $maxdesclength)
$posttitle = trim(substr($post["title"], 0, $maxdesclength - 3))."...";
else
$posttitle = trim($post["title"]);
$post["text"] .= "\n[".$posttitle."](".$post["url"].")";
} elseif (($profile->service == "appdotnet") AND isset($post["url"]))
$post["text"] .= " ".$post["url"];
$message = array();
$message["text"] = $post["text"];
$message["profile_ids[]"] = $profile->id;
$message["shorten"] = false;
$message["now"] = true;
if (isset($post["title"]))
$message["media[title]"] = $post["title"];
if (isset($post["description"]))
$message["media[description]"] = $post["description"];
if (isset($post["url"]) AND ($post["type"] != "photo"))
$message["media[link]"] = $post["url"];
if (isset($post["image"])) {
$message["media[picture]"] = $post["image"];
if ($post["type"] == "photo")
$message["media[thumbnail]"] = $post["image"];
}
if (isset($post["preview"]))
$message["media[thumbnail]"] = $post["preview"];
//print_r($message);
$ret = $buffer->go('/updates/create', $message);
logger("buffer_send: send message ".$b["id"]." result: ".print_r($ret, true), LOGGER_DEBUG);
}
}
}
}