* Status: Unsupported
*/
require 'addon/buffer/bufferapp.php';
use Friendica\App;
use Friendica\Content\Text\Plaintext;
use Friendica\Core\Hook;
use Friendica\Core\Logger;
use Friendica\Core\Protocol;
use Friendica\Core\Renderer;
use Friendica\Database\DBA;
use Friendica\DI;
use Friendica\Util\Proxy as ProxyUtils;
use Friendica\Util\Strings;
function buffer_install()
{
Hook::register('hook_fork', 'addon/buffer/buffer.php', 'buffer_hook_fork');
Hook::register('post_local', 'addon/buffer/buffer.php', 'buffer_post_local');
Hook::register('notifier_normal', 'addon/buffer/buffer.php', 'buffer_send');
Hook::register('jot_networks', 'addon/buffer/buffer.php', 'buffer_jot_nets');
Hook::register('connector_settings', 'addon/buffer/buffer.php', 'buffer_settings');
Hook::register('connector_settings_post', 'addon/buffer/buffer.php', 'buffer_settings_post');
}
function buffer_module()
{
}
function buffer_content(App $a)
{
if (! local_user()) {
notice(DI::l10n()->t('Permission denied.') . EOL);
return '';
}
require_once "mod/settings.php";
settings_init($a);
if (isset(DI::args()->getArgv()[1])) {
switch (DI::args()->getArgv()[1]) {
case "connect":
$o = buffer_connect($a);
break;
default:
$o = print_r(DI::args()->getArgv(), true);
break;
}
} else {
$o = buffer_connect($a);
}
return $o;
}
function buffer_addon_admin(App $a, &$o)
{
$t = Renderer::getMarkupTemplate("admin.tpl", "addon/buffer/");
$o = Renderer::replaceMacros($t, [
'$submit' => DI::l10n()->t('Save Settings'),
// name, label, value, help, [extra values]
'$client_id' => ['client_id', DI::l10n()->t('Client ID'), DI::config()->get('buffer', 'client_id'), ''],
'$client_secret' => ['client_secret', DI::l10n()->t('Client Secret'), DI::config()->get('buffer', 'client_secret'), ''],
]);
}
function buffer_addon_admin_post(App $a)
{
$client_id = (!empty($_POST['client_id']) ? Strings::escapeTags(trim($_POST['client_id'])) : '');
$client_secret = (!empty($_POST['client_secret']) ? Strings::escapeTags(trim($_POST['client_secret'])) : '');
DI::config()->set('buffer', 'client_id' , $client_id);
DI::config()->set('buffer', 'client_secret', $client_secret);
}
function buffer_connect(App $a)
{
if (isset($_REQUEST["error"])) {
$o = DI::l10n()->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 = DI::config()->get('buffer','client_id');
$client_secret = DI::config()->get('buffer','client_secret');
// The callback URL is the script that gets called after the user authenticates with buffer
$callback_url = DI::baseUrl()->get()."/buffer/connect";
$buffer = new BufferApp($client_id, $client_secret, $callback_url);
if (!$buffer->ok) {
$o = 'Connect to Buffer! ';
} else {
Logger::notice("buffer_connect: authenticated");
$o = DI::l10n()->t("You are now authenticated to buffer. ");
$o .= '' . DI::l10n()->t("return to the connector page") . ' ';
DI::pConfig()->set(local_user(), 'buffer','access_token', $buffer->access_token);
}
return $o;
}
function buffer_jot_nets(App $a, array &$jotnets_fields)
{
if (!local_user()) {
return;
}
if (DI::pConfig()->get(local_user(), 'buffer', 'post')) {
$jotnets_fields[] = [
'type' => 'checkbox',
'field' => [
'buffer_enable',
DI::l10n()->t('Post to Buffer'),
DI::pConfig()->get(local_user(), 'buffer', 'post_by_default')
]
];
}
}
function buffer_settings(App $a, &$s)
{
if (! local_user()) {
return;
}
/* Add our stylesheet to the page so we can make our settings look nice */
DI::page()['htmlhead'] .= ' ' . "\r\n";
/* Get the current state of our config variables */
$enabled = DI::pConfig()->get(local_user(),'buffer','post');
$checked = (($enabled) ? ' checked="checked" ' : '');
$css = (($enabled) ? '' : '-disabled');
$def_enabled = DI::pConfig()->get(local_user(),'buffer','post_by_default');
$def_checked = (($def_enabled) ? ' checked="checked" ' : '');
/* Add some HTML to the existing form */
$s .= '';
$s .= ''. DI::l10n()->t('Buffer Export').' ';
$s .= ' ';
$s .= '
';
$s .= '
';
$s .= ''. DI::l10n()->t('Buffer Export').' ';
$s .= ' ';
$client_id = DI::config()->get("buffer", "client_id");
$client_secret = DI::config()->get("buffer", "client_secret");
$access_token = DI::pConfig()->get(local_user(), "buffer", "access_token");
$s .= '
';
if ($access_token == "") {
$s .= '
';
} else {
$s .= '
';
$s .= '' . DI::l10n()->t('Enable Buffer Post Addon') . ' ';
$s .= ' ';
$s .= '
';
$s .= '
';
$s .= '' . DI::l10n()->t('Post to Buffer by default') . ' ';
$s .= ' ';
$s .= '
';
$s .= '
';
$s .= '' . DI::l10n()->t('Check to delete this preset') . ' ';
$s .= ' ';
$s .= '
';
// The callback URL is the script that gets called after the user authenticates with buffer
$callback_url = DI::baseUrl()->get() . '/buffer/connect';
$buffer = new BufferApp($client_id, $client_secret, $callback_url, $access_token);
$profiles = $buffer->go('/profiles');
if (is_array($profiles)) {
$s .= '
';
$s .= DI::l10n()->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(App $a, array &$b)
{
if (!empty($_POST['buffer-submit'])) {
if (!empty($_POST['buffer_delete'])) {
DI::pConfig()->set(local_user(), 'buffer', 'access_token' , '');
DI::pConfig()->set(local_user(), 'buffer', 'post' , false);
DI::pConfig()->set(local_user(), 'buffer', 'post_by_default', false);
} else {
DI::pConfig()->set(local_user(), 'buffer', 'post' , intval($_POST['buffer'] ?? false));
DI::pConfig()->set(local_user(), 'buffer', 'post_by_default', intval($_POST['buffer_bydefault'] ?? false));
}
}
}
function buffer_post_local(App $a, array &$b)
{
if (!local_user() || (local_user() != $b['uid'])) {
return;
}
$buffer_post = intval(DI::pConfig()->get(local_user(),'buffer','post'));
$buffer_enable = (($buffer_post && !empty($_REQUEST['buffer_enable'])) ? intval($_REQUEST['buffer_enable']) : 0);
if ($b['api_source'] && intval(DI::pConfig()->get(local_user(),'buffer','post_by_default'))) {
$buffer_enable = 1;
}
if (!$buffer_enable) {
return;
}
if (strlen($b['postopts'])) {
$b['postopts'] .= ',';
}
$b['postopts'] .= 'buffer';
}
function buffer_hook_fork(&$a, &$b)
{
if ($b['name'] != 'notifier_normal') {
return;
}
$post = $b['data'];
if ($post['deleted'] || $post['private'] || ($post['created'] !== $post['edited']) ||
!strstr($post['postopts'], 'buffer') || ($post['parent'] != $post['id'])) {
$b['execute'] = false;
return;
}
}
function buffer_send(App $a, array &$b)
{
if ($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited'])) {
return;
}
if (!strstr($b['postopts'],'buffer')) {
return;
}
if ($b['parent'] != $b['id']) {
return;
}
// Dont't post if the post doesn't belong to us.
// This is a check for forum postings
$self = DBA::selectFirst('contact', ['id'], ['uid' => $b['uid'], 'self' => true]);
if ($b['contact-id'] != $self['id']) {
return;
}
// if post comes from buffer don't send it back
//if($b['app'] == "Buffer")
// return;
$client_id = DI::config()->get("buffer", "client_id");
$client_secret = DI::config()->get("buffer", "client_secret");
$access_token = DI::pConfig()->get($b['uid'], "buffer","access_token");
$callback_url = "";
if ($access_token) {
$buffer = new BufferApp($client_id, $client_secret, $callback_url, $access_token);
$profiles = $buffer->go('/profiles');
if (is_array($profiles)) {
Logger::info("Will send these parameter ".print_r($b, true));
foreach ($profiles as $profile) {
if (!$profile->default)
continue;
$send = false;
switch ($profile->service) {
case 'facebook':
$send = ($b["extid"] != Protocol::FACEBOOK);
$limit = 0;
$includedlinks = false;
$htmlmode = 9;
break;
case 'twitter':
$send = ($b["extid"] != Protocol::TWITTER);
$limit = 280;
$includedlinks = true;
$htmlmode = 8;
break;
case 'linkedin':
$send = ($b["extid"] != Protocol::LINKEDIN);
$limit = 700;
$includedlinks = true;
$htmlmode = 2;
break;
}
if (!$send)
continue;
$item = $b;
$post = Plaintext::getPost($item, $limit, $includedlinks, $htmlmode);
Logger::info("buffer_send: converted message ".$b["id"]." result: ".print_r($post, true));
// The image proxy is used as a sanitizer. Buffer seems to be really picky about pictures
if (isset($post["image"])) {
$post["image"] = ProxyUtils::proxifyUrl($post["image"]);
}
if (isset($post["preview"])) {
$post["preview"] = ProxyUtils::proxifyUrl($post["preview"]);
}
// Seems like a bug to me
// Buffer doesn't add links to Twitter (but pictures)
if (($profile->service == "twitter") && isset($post["url"]) && ($post["type"] != "photo")) {
$post["text"] .= " " . $post["url"];
}
$message = [];
$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"]) && ($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);
Logger::info("buffer_send: data for message " . $b["id"] . ": " . print_r($message, true));
$ret = $buffer->go('/updates/create', $message);
Logger::info("buffer_send: send message " . $b["id"] . " result: " . print_r($ret, true));
}
}
}
}