X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=buffer%2Fbuffer.php;h=9bbdf21e529850b09f0524494abd1eac4c6d1eb6;hb=e34b227e614c9299326ab3ddabac3befe30da7fe;hp=c0a06d06892b2ba45ae07eacf54c76fe571eb931;hpb=7032e088f7d314c03f02bf6f3dd927dc5e66cfb9;p=friendica-addons.git
diff --git a/buffer/buffer.php b/buffer/buffer.php
index c0a06d06..9bbdf21e 100644
--- a/buffer/buffer.php
+++ b/buffer/buffer.php
@@ -1,38 +1,32 @@
+ * Status: Unsupported
*/
require 'addon/buffer/bufferapp.php';
use Friendica\App;
-use Friendica\Content\Text\Plaintext;
-use Friendica\Core\Addon;
-use Friendica\Core\Config;
-use Friendica\Core\L10n;
-use Friendica\Core\PConfig;
+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\Model\ItemContent;
-use Friendica\Module\Proxy;
+use Friendica\Util\Proxy as ProxyUtils;
+use Friendica\Util\Strings;
function buffer_install()
{
- Addon::registerHook('post_local', 'addon/buffer/buffer.php', 'buffer_post_local');
- Addon::registerHook('notifier_normal', 'addon/buffer/buffer.php', 'buffer_send');
- Addon::registerHook('jot_networks', 'addon/buffer/buffer.php', 'buffer_jot_nets');
- Addon::registerHook('connector_settings', 'addon/buffer/buffer.php', 'buffer_settings');
- Addon::registerHook('connector_settings_post', 'addon/buffer/buffer.php', 'buffer_settings_post');
-}
-
-function buffer_uninstall()
-{
- Addon::unregisterHook('post_local', 'addon/buffer/buffer.php', 'buffer_post_local');
- Addon::unregisterHook('notifier_normal', 'addon/buffer/buffer.php', 'buffer_send');
- Addon::unregisterHook('jot_networks', 'addon/buffer/buffer.php', 'buffer_jot_nets');
- Addon::unregisterHook('connector_settings', 'addon/buffer/buffer.php', 'buffer_settings');
- Addon::unregisterHook('connector_settings_post', 'addon/buffer/buffer.php', 'buffer_settings_post');
+ 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()
@@ -42,7 +36,7 @@ function buffer_module()
function buffer_content(App $a)
{
if (! local_user()) {
- notice(L10n::t('Permission denied.') . EOL);
+ notice(DI::l10n()->t('Permission denied.') . EOL);
return '';
}
@@ -68,31 +62,29 @@ function buffer_content(App $a)
function buffer_addon_admin(App $a, &$o)
{
- $t = get_markup_template("admin.tpl", "addon/buffer/");
+ $t = Renderer::getMarkupTemplate("admin.tpl", "addon/buffer/");
- $o = replace_macros($t, [
- '$submit' => L10n::t('Save Settings'),
+ $o = Renderer::replaceMacros($t, [
+ '$submit' => DI::l10n()->t('Save Settings'),
// name, label, value, help, [extra values]
- '$client_id' => ['client_id', L10n::t('Client ID'), Config::get('buffer', 'client_id'), ''],
- '$client_secret' => ['client_secret', L10n::t('Client Secret'), Config::get('buffer', 'client_secret'), ''],
+ '$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'])) ? notags(trim($_POST['client_id'])) : '');
- $client_secret = ((!empty($_POST['client_secret'])) ? notags(trim($_POST['client_secret'])) : '');
-
- Config::set('buffer', 'client_id' , $client_id);
- Config::set('buffer', 'client_secret', $client_secret);
+ $client_id = (!empty($_POST['client_id']) ? Strings::escapeTags(trim($_POST['client_id'])) : '');
+ $client_secret = (!empty($_POST['client_secret']) ? Strings::escapeTags(trim($_POST['client_secret'])) : '');
- info(L10n::t('Settings updated.'). EOL);
+ 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 = L10n::t('Error when registering buffer connection:')." ".$_REQUEST["error"];
+ $o = DI::l10n()->t('Error when registering buffer connection:')." ".$_REQUEST["error"];
return $o;
}
@@ -100,39 +92,41 @@ function buffer_connect(App $a)
session_start();
// Define the needed keys
- $client_id = Config::get('buffer','client_id');
- $client_secret = Config::get('buffer','client_secret');
+ $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 = $a->get_baseurl()."/buffer/connect";
+ $callback_url = DI::baseUrl()->get()."/buffer/connect";
$buffer = new BufferApp($client_id, $client_secret, $callback_url);
if (!$buffer->ok) {
- $o .= 'Connect to Buffer! ';
+ $o = 'Connect to Buffer! ';
} else {
- logger("buffer_connect: authenticated");
- $o .= L10n::t("You are now authenticated to buffer. ");
- $o .= '' . L10n::t("return to the connector page") . ' ';
- PConfig::set(local_user(), 'buffer','access_token', $buffer->access_token);
+ Logger::log("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, &$b)
+function buffer_jot_nets(App $a, array &$jotnets_fields)
{
if (!local_user()) {
return;
}
- $buffer_post = PConfig::get(local_user(), 'buffer', 'post');
-
- if (intval($buffer_post) == 1) {
- $buffer_defpost = PConfig::get(local_user(), 'buffer', 'post_by_default');
- $selected = ((intval($buffer_defpost) == 1) ? ' checked="checked" ' : '');
- $b .= '
'
- . L10n::t('Post to Buffer') . '
';
+ 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')
+ ]
+ ];
}
}
@@ -144,59 +138,62 @@ function buffer_settings(App $a, &$s)
/* Add our stylesheet to the page so we can make our settings look nice */
- $a->page['htmlhead'] .= ' ' . "\r\n";
+ DI::page()['htmlhead'] .= ' ' . "\r\n";
/* Get the current state of our config variables */
- $enabled = PConfig::get(local_user(),'buffer','post');
+ $enabled = DI::pConfig()->get(local_user(),'buffer','post');
$checked = (($enabled) ? ' checked="checked" ' : '');
$css = (($enabled) ? '' : '-disabled');
- $def_enabled = PConfig::get(local_user(),'buffer','post_by_default');
+ $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 .= ''. L10n::t('Buffer Export').' ';
+ $s .= ''. DI::l10n()->t('Buffer Export').' ';
$s .= ' ';
$s .= '';
$s .= '
';
- $s .= ''. L10n::t('Buffer Export').' ';
+ $s .= ''. DI::l10n()->t('Buffer Export').' ';
$s .= ' ';
- $client_id = Config::get("buffer", "client_id");
- $client_secret = Config::get("buffer", "client_secret");
- $access_token = PConfig::get(local_user(), "buffer", "access_token");
+ $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 .= '' . L10n::t('Enable Buffer Post Addon') . ' ';
+ $s .= '' . DI::l10n()->t('Enable Buffer Post Addon') . ' ';
$s .= ' ';
$s .= '
';
$s .= '
';
- $s .= '' . L10n::t('Post to Buffer by default') . ' ';
+ $s .= '' . DI::l10n()->t('Post to Buffer by default') . ' ';
$s .= ' ';
$s .= '
';
$s .= '
';
- $s .= '' . L10n::t('Check to delete this preset') . ' ';
+ $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 .= L10n::t("Posts are going to all accounts that are enabled by default:");
+ $s .= DI::l10n()->t("Posts are going to all accounts that are enabled by default:");
$s .= "
';
+ $s .= '
';
}
@@ -224,12 +221,12 @@ function buffer_settings_post(App $a, array &$b)
{
if (!empty($_POST['buffer-submit'])) {
if (!empty($_POST['buffer_delete'])) {
- PConfig::set(local_user(), 'buffer', 'access_token' , '');
- PConfig::set(local_user(), 'buffer', 'post' , false);
- PConfig::set(local_user(), 'buffer', 'post_by_default', false);
+ 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 {
- PConfig::set(local_user(), 'buffer', 'post' , intval($_POST['buffer']));
- PConfig::set(local_user(), 'buffer', 'post_by_default', intval($_POST['buffer_bydefault']));
+ 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));
}
}
}
@@ -240,11 +237,11 @@ function buffer_post_local(App $a, array &$b)
return;
}
- $buffer_post = intval(PConfig::get(local_user(),'buffer','post'));
+ $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(PConfig::get(local_user(),'buffer','post_by_default'))) {
+ if ($b['api_source'] && intval(DI::pConfig()->get(local_user(),'buffer','post_by_default'))) {
$buffer_enable = 1;
}
@@ -259,6 +256,21 @@ function buffer_post_local(App $a, array &$b)
$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'])) {
@@ -284,9 +296,9 @@ function buffer_send(App $a, array &$b)
//if($b['app'] == "Buffer")
// return;
- $client_id = Config::get("buffer", "client_id");
- $client_secret = Config::get("buffer", "client_secret");
- $access_token = PConfig::get($b['uid'], "buffer","access_token");
+ $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) {
@@ -294,7 +306,7 @@ function buffer_send(App $a, array &$b)
$profiles = $buffer->go('/profiles');
if (is_array($profiles)) {
- logger("Will send these parameter ".print_r($b, true), LOGGER_DEBUG);
+ Logger::log("Will send these parameter ".print_r($b, true), Logger::DEBUG);
foreach ($profiles as $profile) {
if (!$profile->default)
@@ -303,42 +315,23 @@ function buffer_send(App $a, array &$b)
$send = false;
switch ($profile->service) {
- case 'appdotnet':
- $send = ($b["extid"] != NETWORK_APPNET);
- $limit = 256;
- $markup = false;
- $includedlinks = true;
- $htmlmode = 6;
- break;
-
case 'facebook':
- $send = ($b["extid"] != NETWORK_FACEBOOK);
- $limit = 0;
- $markup = false;
- $includedlinks = false;
- $htmlmode = 9;
- break;
-
- case 'google':
- $send = ($b["extid"] != NETWORK_GPLUS);
+ $send = ($b["extid"] != Protocol::FACEBOOK);
$limit = 0;
- $markup = true;
$includedlinks = false;
$htmlmode = 9;
break;
case 'twitter':
- $send = ($b["extid"] != NETWORK_TWITTER);
+ $send = ($b["extid"] != Protocol::TWITTER);
$limit = 280;
- $markup = false;
$includedlinks = true;
$htmlmode = 8;
break;
case 'linkedin':
- $send = ($b["extid"] != NETWORK_LINKEDIN);
+ $send = ($b["extid"] != Protocol::LINKEDIN);
$limit = 700;
- $markup = false;
$includedlinks = true;
$htmlmode = 2;
break;
@@ -349,41 +342,22 @@ function buffer_send(App $a, array &$b)
$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 = ItemContent::getPlaintextPost($item, $limit, $includedlinks, $htmlmode);
- logger("buffer_send: converted message ".$b["id"]." result: ".print_r($post, true), LOGGER_DEBUG);
+ Logger::log("buffer_send: converted message ".$b["id"]." result: ".print_r($post, true), Logger::DEBUG);
// The image proxy is used as a sanitizer. Buffer seems to be really picky about pictures
if (isset($post["image"])) {
- $post["image"] = Proxy::proxifyUrl($post["image"]);
+ $post["image"] = ProxyUtils::proxifyUrl($post["image"]);
}
if (isset($post["preview"])) {
- $post["preview"] = Proxy::proxifyUrl($post["preview"]);
+ $post["preview"] = ProxyUtils::proxifyUrl($post["preview"]);
}
// Seems like a bug to me
- // Buffer doesn't add links to Twitter and App.net (but pictures)
+ // Buffer doesn't add links to Twitter (but pictures)
if (($profile->service == "twitter") && isset($post["url"]) && ($post["type"] != "photo")) {
$post["text"] .= " " . $post["url"];
- } elseif (($profile->service == "appdotnet") && isset($post["url"]) && isset($post["title"]) && ($post["type"] != "photo")) {
- $post["title"] = Plaintext::shorten($post["title"], 90);
- $post["text"] = Plaintext::shorten($post["text"], $limit - (24 + strlen($post["title"])));
- $post["text"] .= "\n[" . $post["title"] . "](" . $post["url"] . ")";
- } elseif (($profile->service == "appdotnet") && isset($post["url"]) && ($post["type"] != "photo")) {
- $post["text"] .= " " . $post["url"];
- } elseif ($profile->service == "google") {
- $post["text"] .= html_entity_decode(" ", ENT_QUOTES, 'UTF-8'); // Send a special blank to identify the post through the "fromgplus" addon
}
$message = [];
@@ -417,9 +391,9 @@ function buffer_send(App $a, array &$b)
}
//print_r($message);
- logger("buffer_send: data for message " . $b["id"] . ": " . print_r($message, true), LOGGER_DEBUG);
+ Logger::log("buffer_send: data for message " . $b["id"] . ": " . print_r($message, true), Logger::DEBUG);
$ret = $buffer->go('/updates/create', $message);
- logger("buffer_send: send message " . $b["id"] . " result: " . print_r($ret, true), LOGGER_DEBUG);
+ Logger::log("buffer_send: send message " . $b["id"] . " result: " . print_r($ret, true), Logger::DEBUG);
}
}
}