From dcafad573ea4320262e90e6107cbbd36faada2e0 Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 3 Mar 2024 12:02:53 +0000 Subject: [PATCH] Pnut: Client Id/Secret can be set by admins --- pnut/README.md | 5 +- pnut/lang/C/messages.po | 74 +++++++++++++++++++++++ pnut/pnut.php | 86 ++++++++++++++++++--------- pnut/templates/admin.tpl | 3 + pnut/templates/connector_settings.tpl | 8 ++- 5 files changed, 145 insertions(+), 31 deletions(-) create mode 100644 pnut/lang/C/messages.po create mode 100644 pnut/templates/admin.tpl diff --git a/pnut/README.md b/pnut/README.md index d935efe7..6f6a9224 100644 --- a/pnut/README.md +++ b/pnut/README.md @@ -2,4 +2,7 @@ With this addon to friendica you can give your users the possibility to post their *public* messages to pnut.io. -No setup is needed for the admins to make it work for their users. +No setup is needed for the admins to make it work for their users, however it is possible for the admin to create a client, so that the users don't have to. + +To do so, go to https://pnut.io/dev and scroll down to "Create New Client". Enter a name of your choice and enter your Friendica host name as the website. Use https://(yourhost.name)/pnut/connect as a redirect url, replace "(yourhost.name)" with the host name of your system. +Limit the scope to "basic,files,follow,polls,presence,stream,update_profile,write_post" \ No newline at end of file diff --git a/pnut/lang/C/messages.po b/pnut/lang/C/messages.po new file mode 100644 index 00000000..89b065fd --- /dev/null +++ b/pnut/lang/C/messages.po @@ -0,0 +1,74 @@ +# ADDON pnut +# Copyright (C) +# This file is distributed under the same license as the Friendica pnut addon package. +# +# +#, fuzzy +msgid "" +msgstr "" +"Project-Id-Version: \n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2024-03-03 11:53+0000\n" +"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" +"Last-Translator: FULL NAME \n" +"Language-Team: LANGUAGE \n" +"Language: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" + +#: pnut.php:42 +msgid "Permission denied." +msgstr "" + +#: pnut.php:79 +msgid "You are now authenticated with pnut.io." +msgstr "" + +#: pnut.php:82 +msgid "Error fetching token. Please try again." +msgstr "" + +#: pnut.php:85 +msgid "return to the connector page" +msgstr "" + +#: pnut.php:103 +msgid "Save Settings" +msgstr "" + +#: pnut.php:104 pnut.php:150 +msgid "Client ID" +msgstr "" + +#: pnut.php:105 pnut.php:151 +msgid "Client Secret" +msgstr "" + +#: pnut.php:139 +msgid "Authenticate with pnut.io" +msgstr "" + +#: pnut.php:143 +msgid "Disconnect" +msgstr "" + +#: pnut.php:148 +msgid "Enable Pnut Post Addon" +msgstr "" + +#: pnut.php:149 +msgid "Post to Pnut by default" +msgstr "" + +#: pnut.php:152 +msgid "Access Token" +msgstr "" + +#: pnut.php:161 +msgid "Pnut Export" +msgstr "" + +#: pnut.php:203 +msgid "Post to Pnut" +msgstr "" diff --git a/pnut/pnut.php b/pnut/pnut.php index f278fb55..e84f208b 100644 --- a/pnut/pnut.php +++ b/pnut/pnut.php @@ -2,7 +2,7 @@ /** * Name: Pnut Connector * Description: Post to pnut.io - * Version: 0.1.1 + * Version: 0.1.2 * Author: Morgan McMillian * Status: In Development */ @@ -18,12 +18,10 @@ use Friendica\Core\Logger; use Friendica\Core\Renderer; use Friendica\Core\System; use Friendica\DI; -use Friendica\Model\Item; use Friendica\Model\Photo; -use Friendica\Object\Image; -use Friendica\Network\HTTPClient\Client\HttpClientAccept; -use Friendica\Network\HTTPClient\Client\HttpClientOptions; -use Friendica\Util\DateTimeFormat; +use phpnut\phpnutException; + +const PNUT_LIMIT = 256; function pnut_install() { @@ -63,19 +61,25 @@ function pnut_content() function pnut_connect() { - $client_id = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'pnut', 'client_id'); - $client_secret = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'pnut', 'client_secret'); + $client_id = DI::config()->get('pnut', 'client_id'); + $client_secret = DI::config()->get('pnut', 'client_secret'); + + if (empty($client_id) || empty($client_secret)) { + $client_id = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'pnut', 'client_id'); + $client_secret = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'pnut', 'client_secret'); + } + $callback_url = DI::baseUrl() . '/pnut/connect'; $nut = new phpnut\phpnut($client_id, $client_secret); try { $token = $nut->getAccessToken($callback_url); - Logger::debug('TOKEN', [$token]); + Logger::debug('Got Token', [$token]); $o = DI::l10n()->t('You are now authenticated with pnut.io.'); DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'pnut', 'access_token', $token); } catch (phpnutException $e) { - $o = DI::l10n()->t('Error fetching token. Please try again.'); + $o = DI::l10n()->t('Error fetching token. Please try again.', ['code' => $e->getCode(), 'message' => $e->getMessage()]); } $o .= '
' . DI::l10n()->t("return to the connector page").''; @@ -88,6 +92,26 @@ function pnut_load_config(ConfigFileManager $loader) DI::app()->getConfigCache()->load($loader->loadAddonConfig('pnut'), \Friendica\Core\Config\ValueObject\Cache::SOURCE_STATIC); } +function pnut_addon_admin(string &$o) +{ + $client_id = DI::config()->get('pnut', 'client_id'); + $client_secret = DI::config()->get('pnut', 'client_secret'); + + $t = Renderer::getMarkupTemplate('admin.tpl', 'addon/pnut/'); + + $o = Renderer::replaceMacros($t, [ + '$submit' => DI::l10n()->t('Save Settings'), + '$client_id' => ['pnut_client_id', DI::l10n()->t('Client ID'), $client_id], + '$client_secret' => ['pnut_client_secret', DI::l10n()->t('Client Secret'), $client_secret], + ]); +} + +function pnut_addon_admin_post() +{ + DI::config()->set('pnut', 'client_id', $_POST['pnut_client_id']); + DI::config()->set('pnut', 'client_secret', $_POST['pnut_client_secret']); +} + function pnut_settings(array &$data) { if (!DI::userSession()->getLocalUserId()) { @@ -99,12 +123,15 @@ function pnut_settings(array &$data) $enabled = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'pnut', 'post') ?? false; $def_enabled = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'pnut', 'post_by_default') ?? false; - $client_id = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'pnut', 'client_id'); - $client_secret = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'pnut', 'client_secret'); + $client_id = DI::config()->get('pnut', 'client_id'); + $client_secret = DI::config()->get('pnut', 'client_secret'); $token = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'pnut', 'access_token'); - - Logger::debug('CLIENT_ID', [$client_id]); - Logger::debug('CLIENT_SECRET', [$client_secret]); + + $user_client = empty($client_id) || empty($client_secret); + if ($user_client) { + $client_id = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'pnut', 'client_id'); + $client_secret = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'pnut', 'client_secret'); + } if (!empty($client_id) && !empty($client_secret) && empty($token)) { $nut = new phpnut\phpnut($client_id, $client_secret); @@ -118,19 +145,20 @@ function pnut_settings(array &$data) $t = Renderer::getMarkupTemplate('connector_settings.tpl', 'addon/pnut/'); $html = Renderer::replaceMacros($t, [ - '$enable' => ['pnut', DI::l10n()->t('Enable Pnut Post Addon'), $enabled], - '$bydefault' => ['pnut_bydefault', DI::l10n()->t('Post to Pnut by default'), $def_enabled], - '$client_id' => ['pnut_client_id', DI::l10n()->t('Client ID'), $client_id], - '$client_secret' => ['pnut_client_secret', DI::l10n()->t('Client Secret'), $client_secret], - '$access_token' => ['pnut_access_token', DI::l10n()->t('Access Token'), $token, '', '', 'readonly'], - '$authorize_url' => $authorize_url ?? '', + '$enable' => ['pnut', DI::l10n()->t('Enable Pnut Post Addon'), $enabled], + '$bydefault' => ['pnut_bydefault', DI::l10n()->t('Post to Pnut by default'), $def_enabled], + '$client_id' => ['pnut_client_id', DI::l10n()->t('Client ID'), $client_id], + '$client_secret' => ['pnut_client_secret', DI::l10n()->t('Client Secret'), $client_secret], + '$access_token' => ['pnut_access_token', DI::l10n()->t('Access Token'), $token, '', '', 'readonly'], + '$authorize_url' => $authorize_url ?? '', '$authorize_text' => $authorize_text ?? '', - '$disconn_btn' => $disconn_btn ?? '', + '$disconn_btn' => $disconn_btn ?? '', + 'user_client' => $user_client, ]); $data = [ 'connector' => 'pnut', - 'title' => DI::l10n()->t('Pnut Import/Export'), + 'title' => DI::l10n()->t('Pnut Export'), 'image' => 'addon/pnut/pnut.svg', 'enabled' => $enabled, 'html' => $html, @@ -152,8 +180,12 @@ function pnut_settings_post(array &$b) } else { DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'pnut', 'post', intval($_POST['pnut'])); DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'pnut', 'post_by_default', intval($_POST['pnut_bydefault'])); - DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'pnut', 'client_id', $_POST['pnut_client_id']); - DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'pnut', 'client_secret', $_POST['pnut_client_secret']); + if (!empty($_POST['pnut_client_id'])) { + DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'pnut', 'client_id', $_POST['pnut_client_id']); + } + if (!empty($_POST['pnut_client_secret'])) { + DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'pnut', 'client_secret', $_POST['pnut_client_secret']); + } } } @@ -243,7 +275,7 @@ function pnut_post_hook(array &$b) $token = DI::pConfig()->get($b['uid'], 'pnut', 'access_token'); $nut = new phpnut\phpnut($token); - $msgarr = Plaintext::getPost($b, 256, true, BBCode::EXTERNAL); + $msgarr = Plaintext::getPost($b, PNUT_LIMIT, true, BBCode::EXTERNAL); $text = $msgarr['text']; $raw = []; @@ -275,7 +307,7 @@ function pnut_post_hook(array &$b) $picturedata = Photo::getImageForPhoto($photo); Logger::debug('PNUT photo', $photo); - $picurefile = System::getTempPath() . DIRECTORY_SEPARATOR . $photo['filename']; + $picurefile = tempnam(System::getTempPath(), 'pnut'); file_put_contents($picurefile, $picturedata); Logger::debug('PNUT got file?', ['filename' => $picurefile]); $imagefile = $nut->createFile($picurefile, $fileraw); diff --git a/pnut/templates/admin.tpl b/pnut/templates/admin.tpl new file mode 100644 index 00000000..b4cc3651 --- /dev/null +++ b/pnut/templates/admin.tpl @@ -0,0 +1,3 @@ +{{include file="field_input.tpl" field=$client_id}} +{{include file="field_input.tpl" field=$client_secret}} +
diff --git a/pnut/templates/connector_settings.tpl b/pnut/templates/connector_settings.tpl index 7d1ad3f2..6662a5ae 100644 --- a/pnut/templates/connector_settings.tpl +++ b/pnut/templates/connector_settings.tpl @@ -1,9 +1,11 @@

{{$status}}

{{include file="field_checkbox.tpl" field=$enable}} {{include file="field_checkbox.tpl" field=$bydefault}} -{{include file="field_input.tpl" field=$client_id}} -{{include file="field_input.tpl" field=$client_secret}} -{{include file="field_input.tpl" field=$access_token}} +{{if $user_client}} + {{include file="field_input.tpl" field=$client_id}} + {{include file="field_input.tpl" field=$client_secret}} + {{include file="field_input.tpl" field=$access_token}} +{{/if}} {{if $authorize_url}} {{$authorize_text}} {{/if}}