friendica-addons/statusnet/statusnet.php

493 lines
22 KiB
PHP
Raw Permalink Normal View History

2011-09-25 08:56:03 +00:00
<?php
2017-12-08 05:18:01 +00:00
2011-09-25 08:56:03 +00:00
/**
* Name: GNU Social Connector
* Description: Bidirectional (posting, relaying and reading) connector for GNU Social.
* Version: 1.0.5
2013-06-14 17:40:10 +00:00
* Author: Tobias Diekershoff <https://f.diekershoff.de/profile/tobias>
* Author: Michael Vogel <https://pirati.ca/profile/heluecht>
2011-09-25 08:56:03 +00:00
*
2013-05-31 11:27:31 +00:00
* Copyright (c) 2011-2013 Tobias Diekershoff, Michael Vogel
* All rights reserved.
2011-09-25 08:56:03 +00:00
*
2013-05-31 11:27:31 +00:00
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* * copyright notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the distribution.
* * Neither the name of the <organization> nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
2011-09-25 08:56:03 +00:00
*
2013-05-31 11:27:31 +00:00
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2011-09-25 08:56:03 +00:00
*
*/
define('STATUSNET_DEFAULT_POLL_INTERVAL', 5); // given in minutes
require_once __DIR__ . DIRECTORY_SEPARATOR . 'library' . DIRECTORY_SEPARATOR . 'statusnetoauth.php';
2019-10-24 22:10:19 +00:00
use CodebirdSN\CodebirdSN;
2017-12-08 05:18:01 +00:00
use Friendica\App;
use Friendica\Content\Text\Plaintext;
use Friendica\Core\Hook;
use Friendica\Core\Logger;
use Friendica\Core\Renderer;
2021-11-04 20:32:16 +00:00
use Friendica\Core\System;
use Friendica\Database\DBA;
use Friendica\DI;
2018-01-28 11:19:38 +00:00
use Friendica\Model\Item;
use Friendica\Model\Post;
2017-12-09 18:50:29 +00:00
use Friendica\Model\User;
2017-12-08 05:18:01 +00:00
function statusnet_install()
{
2011-09-25 08:56:03 +00:00
// we need some hooks, for the configuration and for sending tweets
Hook::register('connector_settings', 'addon/statusnet/statusnet.php', 'statusnet_settings');
Hook::register('connector_settings_post', 'addon/statusnet/statusnet.php', 'statusnet_settings_post');
Hook::register('notifier_normal', 'addon/statusnet/statusnet.php', 'statusnet_post_hook');
Hook::register('hook_fork', 'addon/statusnet/statusnet.php', 'statusnet_hook_fork');
Hook::register('post_local', 'addon/statusnet/statusnet.php', 'statusnet_post_local');
Hook::register('jot_networks', 'addon/statusnet/statusnet.php', 'statusnet_jot_nets');
2022-06-22 16:17:09 +00:00
Logger::notice('installed GNU Social');
2011-09-25 08:56:03 +00:00
}
function statusnet_jot_nets(array &$jotnets_fields)
2017-12-08 05:18:01 +00:00
{
2022-10-20 21:51:49 +00:00
if (!DI::userSession()->getLocalUserId()) {
2011-09-25 08:56:03 +00:00
return;
2017-12-08 05:18:01 +00:00
}
2011-09-25 08:56:03 +00:00
2022-10-20 21:51:49 +00:00
if (DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'statusnet', 'post')) {
$jotnets_fields[] = [
'type' => 'checkbox',
'field' => [
'statusnet_enable',
DI::l10n()->t('Post to GNU Social'),
2022-10-20 21:51:49 +00:00
DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'statusnet', 'post_by_default')
]
];
2011-09-25 08:56:03 +00:00
}
}
function statusnet_settings_post($post)
2017-12-08 05:18:01 +00:00
{
2022-10-20 21:51:49 +00:00
if (!DI::userSession()->getLocalUserId()) {
return;
2017-12-08 05:18:01 +00:00
}
// don't check GNU Social settings if GNU Social submit button is not clicked
if (empty($_POST['statusnet-submit']) && empty($_POST['statusnet-disconnect'])) {
return;
2017-12-08 05:18:01 +00:00
}
if (!empty($_POST['statusnet-disconnect'])) {
2017-12-08 05:18:01 +00:00
/* * *
* if the GNU Social-disconnect button is clicked, clear the GNU Social configuration
*/
2022-10-20 21:51:49 +00:00
DI::pConfig()->delete(DI::userSession()->getLocalUserId(), 'statusnet', 'consumerkey');
DI::pConfig()->delete(DI::userSession()->getLocalUserId(), 'statusnet', 'consumersecret');
DI::pConfig()->delete(DI::userSession()->getLocalUserId(), 'statusnet', 'post');
DI::pConfig()->delete(DI::userSession()->getLocalUserId(), 'statusnet', 'post_by_default');
DI::pConfig()->delete(DI::userSession()->getLocalUserId(), 'statusnet', 'oauthtoken');
DI::pConfig()->delete(DI::userSession()->getLocalUserId(), 'statusnet', 'oauthsecret');
DI::pConfig()->delete(DI::userSession()->getLocalUserId(), 'statusnet', 'baseapi');
2011-09-25 08:56:03 +00:00
} else {
2017-12-08 05:18:01 +00:00
if (isset($_POST['statusnet-preconf-apiurl'])) {
/* * *
* If the user used one of the preconfigured GNU Social server credentials
* use them. All the data are available in the global config.
* Check the API Url never the less and blame the admin if it's not working ^^
*/
$globalsn = DI::config()->get('statusnet', 'sites');
2017-12-08 05:18:01 +00:00
foreach ($globalsn as $asn) {
if ($asn['apiurl'] == $_POST['statusnet-preconf-apiurl']) {
$apibase = $asn['apiurl'];
$c = DI::httpClient()->fetch($apibase . 'statusnet/version.xml');
2017-12-08 05:18:01 +00:00
if (strlen($c) > 0) {
2022-10-20 21:51:49 +00:00
DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'statusnet', 'consumerkey', $asn['consumerkey']);
DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'statusnet', 'consumersecret', $asn['consumersecret']);
DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'statusnet', 'baseapi', $asn['apiurl']);
//DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'statusnet', 'application_name', $asn['applicationname'] );
2017-12-08 05:18:01 +00:00
} else {
2022-10-18 12:28:46 +00:00
DI::sysmsg()->addNotice(DI::l10n()->t('Please contact your site administrator.<br />The provided API URL is not valid.') . '<br />' . $asn['apiurl']);
2017-12-08 05:18:01 +00:00
}
}
2017-12-08 04:24:41 +00:00
}
} else {
2017-12-08 05:18:01 +00:00
if (isset($_POST['statusnet-consumersecret'])) {
// check if we can reach the API of the GNU Social server
// we'll check the API Version for that, if we don't get one we'll try to fix the path but will
// resign quickly after this one try to fix the path ;-)
$apibase = $_POST['statusnet-baseapi'];
$c = DI::httpClient()->fetch($apibase . 'statusnet/version.xml');
2017-12-08 05:18:01 +00:00
if (strlen($c) > 0) {
// ok the API path is correct, let's save the settings
2022-10-20 21:51:49 +00:00
DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'statusnet', 'consumerkey', $_POST['statusnet-consumerkey']);
DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'statusnet', 'consumersecret', $_POST['statusnet-consumersecret']);
DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'statusnet', 'baseapi', $apibase);
//DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'statusnet', 'application_name', $_POST['statusnet-applicationname'] );
2017-12-08 05:18:01 +00:00
} else {
// the API path is not correct, maybe missing trailing / ?
$apibase = $apibase . '/';
$c = DI::httpClient()->fetch($apibase . 'statusnet/version.xml');
2017-12-08 05:18:01 +00:00
if (strlen($c) > 0) {
// ok the API path is now correct, let's save the settings
2022-10-20 21:51:49 +00:00
DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'statusnet', 'consumerkey', $_POST['statusnet-consumerkey']);
DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'statusnet', 'consumersecret', $_POST['statusnet-consumersecret']);
DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'statusnet', 'baseapi', $apibase);
2017-12-08 05:18:01 +00:00
} else {
// still not the correct API base, let's do noting
2022-10-17 21:00:03 +00:00
DI::sysmsg()->addNotice(DI::l10n()->t('We could not contact the GNU Social API with the Path you entered.'));
2017-12-08 05:18:01 +00:00
}
}
} else {
2017-12-08 05:18:01 +00:00
if (isset($_POST['statusnet-pin'])) {
// if the user supplied us with a PIN from GNU Social, let the magic of OAuth happen
2022-10-20 21:51:49 +00:00
$api = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'statusnet', 'baseapi');
$ckey = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'statusnet', 'consumerkey');
$csecret = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'statusnet', 'consumersecret');
2017-12-08 05:18:01 +00:00
// the token and secret for which the PIN was generated were hidden in the settings
// form as token and token2, we need a new connection to GNU Social using these token
// and secret to request a Access Token with the PIN
$connection = new StatusNetOAuth($api, $ckey, $csecret, $_POST['statusnet-token'], $_POST['statusnet-token2']);
$token = $connection->getAccessToken($_POST['statusnet-pin']);
// ok, now that we have the Access Token, save them in the user config
2022-10-20 21:51:49 +00:00
DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'statusnet', 'oauthtoken', $token['oauth_token']);
DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'statusnet', 'oauthsecret', $token['oauth_token_secret']);
DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'statusnet', 'post', 1);
DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'statusnet', 'post_taglinks', 1);
2017-12-08 05:18:01 +00:00
// reload the Addon Settings page, if we don't do it see Bug #42
} else {
// if no PIN is supplied in the POST variables, the user has changed the setting
// to post a dent for every new __public__ posting to the wall
2022-10-20 21:51:49 +00:00
DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'statusnet', 'post', intval($_POST['statusnet-enable']));
DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'statusnet', 'post_by_default', intval($_POST['statusnet-default']));
2017-12-08 05:18:01 +00:00
}
}
}
2017-12-08 05:18:01 +00:00
}
2011-09-25 08:56:03 +00:00
}
2017-12-08 05:18:01 +00:00
function statusnet_settings(array &$data)
2017-12-08 05:18:01 +00:00
{
2022-10-20 21:51:49 +00:00
if (!DI::userSession()->getLocalUserId()) {
return;
2017-12-08 05:18:01 +00:00
}
2021-08-08 17:10:04 +00:00
DI::page()->registerStylesheet(__DIR__ . '/statusnet.css', 'all');
2021-08-08 17:10:04 +00:00
2017-12-08 05:18:01 +00:00
/* * *
2011-09-25 08:56:03 +00:00
* 1) Check that we have a base api url and a consumer key & secret
* 2) If no OAuthtoken & stuff is present, generate button to get some
* allow the user to cancel the connection process at this step
2011-09-25 08:56:03 +00:00
* 3) Checkbox for "Send public notices (respect size limitation)
*/
2022-12-21 22:20:34 +00:00
$baseapi = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'statusnet', 'baseapi');
$ckey = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'statusnet', 'consumerkey');
$csecret = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'statusnet', 'consumersecret');
$otoken = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'statusnet', 'oauthtoken');
$osecret = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'statusnet', 'oauthsecret');
$enabled = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'statusnet', 'post', false);
$def_enabled = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'statusnet', 'post_by_default', false);
// Radio button list to select existing application credentials
$sites = array_map(function ($site) {
return ['statusnet-preconf-apiurl', $site['sitename'], $site['apiurl']];
}, DI::config()->get('statusnet', 'sites', []));
$submit = ['statusnet-submit' => DI::l10n()->t('Save Settings')];
if ($ckey && $csecret) {
if ($otoken && $osecret) {
2017-12-08 05:18:01 +00:00
/* * *
2011-09-25 08:56:03 +00:00
* we have an OAuth key / secret pair for the user
* so let's give a chance to disable the postings to GNU Social
2011-09-25 08:56:03 +00:00
*/
$connection = new StatusNetOAuth($baseapi, $ckey, $csecret, $otoken, $osecret);
$account = $connection->get('account/verify_credentials');
if (!empty($account)) {
$connected_account = DI::l10n()->t('Currently connected to: <a href="%s" target="_statusnet">%s</a>', $account->statusnet_profile_url, $account->screen_name);
2018-08-09 04:16:56 +00:00
}
2022-10-20 21:51:49 +00:00
$user = User::getById(DI::userSession()->getLocalUserId());
2021-08-08 17:10:04 +00:00
if ($user['hidewall']) {
$privacy_warning = DI::l10n()->t('<strong>Note</strong>: Due your privacy settings (<em>Hide your profile details from unknown viewers?</em>) the link potentially included in public postings relayed to GNU Social will lead the visitor to a blank page informing the visitor that the access to your profile has been restricted.');
}
$submit['statusnet-disconnect'] = DI::l10n()->t('Clear OAuth configuration');
} else {
/* * *
* the user has not yet connected the account to GNU Social
* get a temporary OAuth key/secret pair and display a button with
* which the user can request a PIN to connect the account to a
* account at GNU Social
2017-12-08 05:18:01 +00:00
*/
$connection = new StatusNetOAuth($baseapi, $ckey, $csecret);
$request_token = $connection->getRequestToken('oob');
$authorize_url = $connection->getAuthorizeURL($request_token['oauth_token'], false);
$submit['statusnet-disconnect'] = DI::l10n()->t('Cancel GNU Social Connection');
2011-09-25 08:56:03 +00:00
}
}
$t = Renderer::getMarkupTemplate('connector_settings.tpl', 'addon/statusnet/');
$html = Renderer::replaceMacros($t, [
'$l10n' => [
'global_title' => DI::l10n()->t('Globally Available GNU Social OAuthKeys'),
'global_info' => DI::l10n()->t(DI::l10n()->t('There are preconfigured OAuth key pairs for some GNU Social servers available. If you are using one of them, please use these credentials. If not feel free to connect to any other GNU Social instance (see below).')),
'credentials_title' => DI::l10n()->t('Provide your own OAuth Credentials'),
'credentials_info' => DI::l10n()->t('No consumer key pair for GNU Social found. Register your Friendica Account as a desktop application on your GNU Social account, copy the consumer key pair here and enter the API base root.<br />Before you register your own OAuth key pair ask the administrator if there is already a key pair for this Friendica installation at your favorite GNU Social installation.'),
'oauth_info' => DI::l10n()->t('To connect to your GNU Social account click the button below to get a security code from GNU Social which you have to copy into the input box below and submit the form. Only your <strong>public</strong> posts will be posted to GNU Social.'),
'oauth_alt' => DI::l10n()->t('Log in with GNU Social'),
'oauth_cancel' => DI::l10n()->t('Cancel Connection Process'),
'oauth_api' => DI::l10n()->t('Current GNU Social API is: %s', $baseapi),
'connected_account' => $connected_account ?? '',
'privacy_warning' => $privacy_warning ?? '',
],
'$ckey' => $ckey,
'$csecret' => $csecret,
'$otoken' => $otoken,
'$osecret' => $osecret,
'$sites' => $sites,
'$authorize_url' => $authorize_url ?? '',
'$request_token' => $request_token ?? null,
'$account' => $account ?? null,
'$authenticate_url' => DI::baseUrl() . '/statusnet/connect',
'$consumerkey' => ['statusnet-consumerkey', DI::l10n()->t('OAuth Consumer Key'), '', '', false, ' size="35'],
'$consumersecret' => ['statusnet-consumersecret', DI::l10n()->t('OAuth Consumer Secret'), '', '', false, ' size="35'],
'$baseapi' => ['statusnet-baseapi', DI::l10n()->t('Base API Path (remember the trailing /)'), '', '', false, ' size="35'],
'$pin' => ['statusnet-pin', DI::l10n()->t('Copy the security code from GNU Social here')],
'$enable' => ['statusnet-enabled', DI::l10n()->t('Allow posting to GNU Social'), $enabled, DI::l10n()->t('If enabled all your <strong>public</strong> postings can be posted to the associated GNU Social account. You can choose to do so by default (here) or for every posting separately in the posting options when writing the entry.')],
'$default' => ['statusnet-default', DI::l10n()->t('Post to GNU Social by default'), $def_enabled],
]);
$data = [
'connector' => 'statusnet',
'title' => DI::l10n()->t('GNU Social Import/Export/Mirror'),
'image' => 'images/gnusocial.png',
'enabled' => $enabled,
'html' => $html,
'submit' => $submit,
];
2011-09-25 08:56:03 +00:00
}
function statusnet_hook_fork(array &$b)
{
if ($b['name'] != 'notifier_normal') {
return;
}
$post = $b['data'];
2022-12-21 22:29:38 +00:00
if ($post['deleted'] || ($post['created'] !== $post['edited']) || strpos($post['postopts'] ?? '', 'statusnet') === false || ($post['parent'] != $post['id']) || $post['private']) {
$b['execute'] = false;
return;
}
}
function statusnet_post_local(array &$b)
2017-12-08 05:18:01 +00:00
{
if ($b['edit']) {
2011-10-12 01:46:33 +00:00
return;
}
2011-10-12 01:46:33 +00:00
2022-10-20 21:51:49 +00:00
if (!DI::userSession()->getLocalUserId() || (DI::userSession()->getLocalUserId() != $b['uid'])) {
return;
}
2011-10-12 01:46:33 +00:00
2022-10-20 21:51:49 +00:00
$statusnet_post = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'statusnet', 'post');
$statusnet_enable = (($statusnet_post && !empty($_REQUEST['statusnet_enable'])) ? intval($_REQUEST['statusnet_enable']) : 0);
2011-10-12 01:46:33 +00:00
// if API is used, default to the chosen settings
2022-10-20 21:51:49 +00:00
if ($b['api_source'] && intval(DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'statusnet', 'post_by_default'))) {
$statusnet_enable = 1;
}
2011-10-12 01:46:33 +00:00
if (!$statusnet_enable) {
return;
}
2011-10-12 01:46:33 +00:00
if (strlen($b['postopts'])) {
$b['postopts'] .= ',';
}
$b['postopts'] .= 'statusnet';
2011-10-12 01:46:33 +00:00
}
function statusnet_post_hook(array &$b)
2017-12-08 05:18:01 +00:00
{
2011-09-25 08:56:03 +00:00
/**
* Post to GNU Social
2011-09-25 08:56:03 +00:00
*/
2022-12-21 22:20:34 +00:00
if ($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited'])) {
return;
}
2011-09-25 08:56:03 +00:00
2022-10-29 22:14:12 +00:00
$b['body'] = Post\Media::addAttachmentsToBody($b['uri-id'], DI::contentItem()->addSharedPost($b));
2022-06-22 16:17:09 +00:00
$api = DI::pConfig()->get($b['uid'], 'statusnet', 'baseapi');
$hostname = preg_replace("=https?://([\w\.]*)/.*=ism", "$1", $api);
2011-09-25 08:56:03 +00:00
2022-12-21 22:20:34 +00:00
if ($b['private'] || !strstr($b['postopts'], 'statusnet')) {
return;
2017-12-08 05:18:01 +00:00
}
2022-12-21 22:20:34 +00:00
// Dont't post if the post doesn't belong to us.
// This is a check for group postings
2022-12-21 22:20:34 +00:00
$self = DBA::selectFirst('contact', ['id'], ['uid' => $b['uid'], 'self' => true]);
if ($b['contact-id'] != $self['id']) {
return;
2017-12-08 05:18:01 +00:00
}
2021-10-03 17:35:20 +00:00
Logger::notice('GNU Socialpost invoked');
DI::pConfig()->load($b['uid'], 'statusnet');
$api = DI::pConfig()->get($b['uid'], 'statusnet', 'baseapi');
$ckey = DI::pConfig()->get($b['uid'], 'statusnet', 'consumerkey');
$csecret = DI::pConfig()->get($b['uid'], 'statusnet', 'consumersecret');
$otoken = DI::pConfig()->get($b['uid'], 'statusnet', 'oauthtoken');
$osecret = DI::pConfig()->get($b['uid'], 'statusnet', 'oauthsecret');
2011-10-12 01:46:33 +00:00
2017-12-08 05:18:01 +00:00
if ($ckey && $csecret && $otoken && $osecret) {
$dent = new StatusNetOAuth($api, $ckey, $csecret, $otoken, $osecret);
$max_char = $dent->get_maxlength(); // max. length for a dent
DI::pConfig()->set($b['uid'], 'statusnet', 'max_char', $max_char);
2022-06-22 16:17:09 +00:00
$tempfile = '';
2021-01-30 23:14:05 +00:00
$msgarr = Plaintext::getPost($b, $max_char, true, 7);
2022-06-22 16:17:09 +00:00
$msg = $msgarr['text'];
2022-06-22 16:17:09 +00:00
if (($msg == '') && isset($msgarr['title']))
$msg = Plaintext::shorten($msgarr['title'], $max_char - 50, $b['uid']);
2022-06-22 16:17:09 +00:00
$image = '';
2022-06-22 16:17:09 +00:00
if (isset($msgarr['url']) && ($msgarr['type'] != 'photo')) {
$msg .= " \n" . $msgarr['url'];
} elseif (isset($msgarr['image']) && ($msgarr['type'] != 'video')) {
$image = $msgarr['image'];
2017-12-08 05:18:01 +00:00
}
2022-06-22 16:17:09 +00:00
if ($image != '') {
$img_str = DI::httpClient()->fetch($image);
2022-06-22 16:17:09 +00:00
$tempfile = tempnam(System::getTempPath(), 'cache');
file_put_contents($tempfile, $img_str);
2022-06-22 16:17:09 +00:00
$postdata = ['status' => $msg, 'media[]' => $tempfile];
2017-12-08 05:18:01 +00:00
} else {
2022-06-22 16:17:09 +00:00
$postdata = ['status' => $msg];
2017-12-08 05:18:01 +00:00
}
// and now send it :-)
2017-12-08 05:18:01 +00:00
if (strlen($msg)) {
// New code that is able to post pictures
require_once __DIR__ . DIRECTORY_SEPARATOR . 'library' . DIRECTORY_SEPARATOR . 'codebirdsn.php';
$cb = CodebirdSN::getInstance();
$cb->setAPIEndpoint($api);
$cb->setConsumerKey($ckey, $csecret);
$cb->setToken($otoken, $osecret);
$result = $cb->statuses_update($postdata);
//$result = $dent->post('statuses/update', $postdata);
2021-10-03 17:35:20 +00:00
Logger::info('statusnet_post send, result: ' . print_r($result, true) .
"\nmessage: " . $msg . "\nOriginal post: " . print_r($b, true) . "\nPost Data: " . print_r($postdata, true));
if (!empty($result->source)) {
2022-06-22 16:17:09 +00:00
DI::pConfig()->set($b['uid'], 'statusnet', 'application_name', strip_tags($result->source));
2017-12-08 05:18:01 +00:00
}
if (!empty($result->error)) {
2021-10-03 17:35:20 +00:00
Logger::notice('Send to GNU Social failed: "' . $result->error . '"');
} elseif ($iscomment) {
2022-06-22 16:17:09 +00:00
Logger::notice('statusnet_post: Update extid ' . $result->id . ' for post id ' . $b['id']);
Item::update(['extid' => $hostname . '::' . $result->id, 'body' => $result->text], ['id' => $b['id']]);
}
}
2022-06-22 16:17:09 +00:00
if ($tempfile != '') {
unlink($tempfile);
2017-12-08 05:18:01 +00:00
}
2011-10-12 01:46:33 +00:00
}
2011-09-25 08:56:03 +00:00
}
function statusnet_addon_admin_post()
2017-12-08 05:18:01 +00:00
{
2018-01-15 13:15:33 +00:00
$sites = [];
2017-12-08 05:18:01 +00:00
foreach ($_POST['sitename'] as $id => $sitename) {
$sitename = trim($sitename);
$apiurl = trim($_POST['apiurl'][$id]);
if (!(substr($apiurl, -1) == '/')) {
$apiurl = $apiurl . '/';
}
$secret = trim($_POST['secret'][$id]);
$key = trim($_POST['key'][$id]);
//$applicationname = (!empty($_POST['applicationname']) ? Strings::escapeTags(trim($_POST['applicationname'][$id])):'');
2022-06-22 16:17:09 +00:00
if ($sitename != '' &&
$apiurl != '' &&
$secret != '' &&
$key != '' &&
empty($_POST['delete'][$id])) {
2017-12-08 05:18:01 +00:00
2018-01-15 13:15:33 +00:00
$sites[] = [
2017-12-08 05:18:01 +00:00
'sitename' => $sitename,
'apiurl' => $apiurl,
'consumersecret' => $secret,
'consumerkey' => $key,
//'applicationname' => $applicationname
2018-01-15 13:15:33 +00:00
];
2011-09-25 08:56:03 +00:00
}
}
$sites = DI::config()->set('statusnet', 'sites', $sites);
2011-09-25 08:56:03 +00:00
}
function statusnet_addon_admin(string &$o)
2017-12-08 05:18:01 +00:00
{
$sites = DI::config()->get('statusnet', 'sites');
2018-01-15 13:15:33 +00:00
$sitesform = [];
2017-12-08 05:18:01 +00:00
if (is_array($sites)) {
foreach ($sites as $id => $s) {
2018-01-15 13:15:33 +00:00
$sitesform[] = [
'sitename' => ["sitename[$id]", "Site name", $s['sitename'], ""],
'apiurl' => ["apiurl[$id]", "Api url", $s['apiurl'], DI::l10n()->t("Base API Path \x28remember the trailing /\x29")],
2018-01-15 13:15:33 +00:00
'secret' => ["secret[$id]", "Secret", $s['consumersecret'], ""],
'key' => ["key[$id]", "Key", $s['consumerkey'], ""],
//'applicationname' => Array("applicationname[$id]", "Application name", $s['applicationname'], ""),
2018-01-15 13:15:33 +00:00
'delete' => ["delete[$id]", "Delete", False, "Check to delete this preset"],
];
2011-09-25 08:56:03 +00:00
}
}
/* empty form to add new site */
2018-10-17 00:06:44 +00:00
$id = count($sitesform);
2018-01-15 13:15:33 +00:00
$sitesform[] = [
'sitename' => ["sitename[$id]", DI::l10n()->t("Site name"), "", ""],
'apiurl' => ["apiurl[$id]", "Api url", "", DI::l10n()->t("Base API Path \x28remember the trailing /\x29")],
'secret' => ["secret[$id]", DI::l10n()->t("Consumer Secret"), "", ""],
'key' => ["key[$id]", DI::l10n()->t("Consumer Key"), "", ""],
//'applicationname' => Array("applicationname[$id]", DI::l10n()->t("Application name"), "", ""),
2018-01-15 13:15:33 +00:00
];
2011-09-25 08:56:03 +00:00
2022-06-22 16:17:09 +00:00
$t = Renderer::getMarkupTemplate('admin.tpl', 'addon/statusnet/');
$o = Renderer::replaceMacros($t, [
'$submit' => DI::l10n()->t('Save Settings'),
2011-09-25 08:56:03 +00:00
'$sites' => $sitesform,
2018-01-15 13:15:33 +00:00
]);
2011-09-25 08:56:03 +00:00
}