]> git.mxchange.org Git - friendica.git/blob - src/Module/Settings/Connectors.php
Fix Mastodon API fav/unfav temporarily by checking if blog in post func
[friendica.git] / src / Module / Settings / Connectors.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Module\Settings;
23
24 use Friendica\App;
25 use Friendica\Core\Config\Capability\IManageConfigValues;
26 use Friendica\Core\Hook;
27 use Friendica\Core\L10n;
28 use Friendica\Core\PConfig\Capability\IManagePersonalConfigValues;
29 use Friendica\Core\Renderer;
30 use Friendica\Core\Session\Capability\IHandleUserSessions;
31 use Friendica\Database\Database;
32 use Friendica\Database\DBA;
33 use Friendica\Model\Item;
34 use Friendica\Model\User;
35 use Friendica\Module\BaseSettings;
36 use Friendica\Module\Response;
37 use Friendica\Navigation\SystemMessages;
38 use Friendica\Protocol\Email;
39 use Friendica\Util\Profiler;
40 use Psr\Log\LoggerInterface;
41
42 class Connectors extends BaseSettings
43 {
44         /** @var IManageConfigValues */
45         private $config;
46         /** @var IManagePersonalConfigValues */
47         private $pconfig;
48         /** @var Database */
49         private $database;
50         /** @var SystemMessages */
51         private $systemMessages;
52
53         public function __construct(SystemMessages $systemMessages, Database $database, IManagePersonalConfigValues $pconfig, IManageConfigValues $config, IHandleUserSessions $session, App\Page $page, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, array $server, array $parameters = [])
54         {
55                 parent::__construct($session, $page, $l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
56
57                 $this->config         = $config;
58                 $this->pconfig        = $pconfig;
59                 $this->database       = $database;
60                 $this->systemMessages = $systemMessages;
61         }
62
63         protected function post(array $request = [])
64         {
65                 BaseSettings::checkFormSecurityTokenRedirectOnError($this->args->getQueryString(), 'settings_connectors');
66
67                 $user = User::getById($this->session->getLocalUserId());
68
69                 if (!empty($request['general-submit'])) {
70                         $this->pconfig->set($this->session->getLocalUserId(), 'system', 'accept_only_sharer', intval($request['accept_only_sharer']));
71                         $this->pconfig->set($this->session->getLocalUserId(), 'system', 'disable_cw', !intval($request['enable_cw']));
72                         $this->pconfig->set($this->session->getLocalUserId(), 'system', 'no_intelligent_shortening', !intval($request['enable_smart_shortening']));
73                         $this->pconfig->set($this->session->getLocalUserId(), 'system', 'simple_shortening', intval($request['simple_shortening']));
74                         $this->pconfig->set($this->session->getLocalUserId(), 'system', 'attach_link_title', intval($request['attach_link_title']));
75                         $this->pconfig->set($this->session->getLocalUserId(), 'system', 'api_spoiler_title', intval($request['api_spoiler_title']));
76                         $this->pconfig->set($this->session->getLocalUserId(), 'system', 'api_auto_attach', intval($request['api_auto_attach']));
77                         $this->pconfig->set($this->session->getLocalUserId(), 'ostatus', 'legacy_contact', $request['legacy_contact']);
78                 } elseif (!empty($request['mail-submit']) && function_exists('imap_open') && !$this->config->get('system', 'imap_disabled')) {
79                         $mail_server       =                 $request['mail_server'] ?? '';
80                         $mail_port         =                 $request['mail_port'] ?? '';
81                         $mail_ssl          = strtolower(trim($request['mail_ssl'] ?? ''));
82                         $mail_user         =                 $request['mail_user'] ?? '';
83                         $mail_pass         =            trim($request['mail_pass'] ?? '');
84                         $mail_action       =            trim($request['mail_action'] ?? '');
85                         $mail_movetofolder =            trim($request['mail_movetofolder'] ?? '');
86                         $mail_replyto      =                 $request['mail_replyto'] ?? '';
87                         $mail_pubmail      =                 $request['mail_pubmail'] ?? '';
88
89                         if (!$this->database->exists('mailacct', ['uid' => $this->session->getLocalUserId()])) {
90                                 $this->database->insert('mailacct', ['uid' => $this->session->getLocalUserId()]);
91                         }
92
93                         if (strlen($mail_pass)) {
94                                 $pass = '';
95                                 openssl_public_encrypt($mail_pass, $pass, $user['pubkey']);
96                                 $this->database->update('mailacct', ['pass' => bin2hex($pass)], ['uid' => $this->session->getLocalUserId()]);
97                         }
98
99                         $r = $this->database->update('mailacct', [
100                                 'server'       => $mail_server,
101                                 'port'         => $mail_port,
102                                 'ssltype'      => $mail_ssl,
103                                 'user'         => $mail_user,
104                                 'action'       => $mail_action,
105                                 'movetofolder' => $mail_movetofolder,
106                                 'mailbox'      => 'INBOX',
107                                 'reply_to'     => $mail_replyto,
108                                 'pubmail'      => $mail_pubmail
109                         ], ['uid' => $this->session->getLocalUserId()]);
110
111                         $this->logger->debug('updating mailaccount', ['response' => $r]);
112                         $mailacct = $this->database->selectFirst('mailacct', [], ['uid' => $this->session->getLocalUserId()]);
113                         if ($this->database->isResult($mailacct)) {
114                                 if (strlen($mailacct['server'])) {
115                                         $dcrpass = '';
116                                         openssl_private_decrypt(hex2bin($mailacct['pass']), $dcrpass, $user['prvkey']);
117                                         $mbox = Email::connect(Email::constructMailboxName($mailacct), $mail_user, $dcrpass);
118                                         unset($dcrpass);
119                                         if (!$mbox) {
120                                                 $this->systemMessages->addNotice($this->t('Failed to connect with email account using the settings provided.'));
121                                         }
122                                 }
123                         }
124                 }
125
126                 Hook::callAll('connector_settings_post', $request);
127                 $this->baseUrl->redirect($this->args->getQueryString());
128         }
129
130         protected function content(array $request = []): string
131         {
132                 parent::content($request);
133
134                 $accept_only_sharer      =  intval($this->pconfig->get($this->session->getLocalUserId(), 'system', 'accept_only_sharer'));
135                 $enable_cw               = !intval($this->pconfig->get($this->session->getLocalUserId(), 'system', 'disable_cw'));
136                 $enable_smart_shortening = !intval($this->pconfig->get($this->session->getLocalUserId(), 'system', 'no_intelligent_shortening'));
137                 $simple_shortening       =  intval($this->pconfig->get($this->session->getLocalUserId(), 'system', 'simple_shortening'));
138                 $attach_link_title       =  intval($this->pconfig->get($this->session->getLocalUserId(), 'system', 'attach_link_title'));
139                 $api_spoiler_title       =  intval($this->pconfig->get($this->session->getLocalUserId(), 'system', 'api_spoiler_title', true));
140                 $api_auto_attach         =  intval($this->pconfig->get($this->session->getLocalUserId(), 'system', 'api_auto_attach', false));
141                 $legacy_contact          =         $this->pconfig->get($this->session->getLocalUserId(), 'ostatus', 'legacy_contact');
142
143                 if (!empty($legacy_contact)) {
144                         $this->baseUrl->redirect('ostatus/subscribe?url=' . urlencode($legacy_contact));
145                 }
146
147                 $connector_settings_forms = [];
148                 foreach ($this->database->selectToArray('hook', ['file', 'function'], ['hook' => 'connector_settings']) as $hook) {
149                         $data = [];
150                         Hook::callSingle('connector_settings', [$hook['file'], $hook['function']], $data);
151
152                         $tpl                                          = Renderer::getMarkupTemplate('settings/addons/connector.tpl');
153                         $connector_settings_forms[$data['connector']] = Renderer::replaceMacros($tpl, [
154                                 '$connector' => $data['connector'],
155                                 '$title'     => $data['title'],
156                                 '$image'     => $data['image'] ?? '',
157                                 '$enabled'   => $data['enabled'] ?? true,
158                                 '$open'      => ($this->parameters['connector'] ?? '') === $data['connector'],
159                                 '$html'      => $data['html'] ?? '',
160                                 '$submit'    => $data['submit'] ?? $this->t('Save Settings'),
161                         ]);
162                 }
163
164                 if ($this->session->isSiteAdmin()) {
165                         $diasp_enabled = $this->config->get('system', 'diaspora_enabled') ?
166                                 $this->t('Built-in support for %s connectivity is enabled', $this->t('Diaspora (Socialhome, Hubzilla)')) :
167                                 $this->t('Built-in support for %s connectivity is disabled', $this->t('Diaspora (Socialhome, Hubzilla)'));
168                         $ostat_enabled = $this->config->get('system', 'ostatus_disabled') ?
169                                 $this->t('Built-in support for %s connectivity is disabled', $this->t('OStatus (GNU Social)')) :
170                                 $this->t('Built-in support for %s connectivity is enabled', $this->t('OStatus (GNU Social)'));
171                 } else {
172                         $diasp_enabled = '';
173                         $ostat_enabled = '';
174                 }
175
176                 $mail_enabled = function_exists('imap_open') && !$this->config->get('system', 'imap_disabled');
177                 if ($mail_enabled) {
178                         $mail_account  = $this->database->selectFirst('mailacct', [], ['uid' => $this->session->getLocalUserId()]);
179                         $mail_disabled = '';
180                 } else {
181                         $mail_account  = null;
182                         $mail_disabled = $this->t('Email access is disabled on this site.');
183                 }
184
185                 $mail_server       = $mail_account['server']       ?? '';
186                 $mail_port         = (!empty($mail_account['port']) && is_numeric($mail_account['port'])) ? (int)$mail_account['port'] : '';
187                 $mail_ssl          = $mail_account['ssltype']      ?? '';
188                 $mail_user         = $mail_account['user']         ?? '';
189                 $mail_replyto      = $mail_account['reply_to']     ?? '';
190                 $mail_pubmail      = $mail_account['pubmail']      ?? 0;
191                 $mail_action       = $mail_account['action']       ?? 0;
192                 $mail_movetofolder = $mail_account['movetofolder'] ?? '';
193                 $mail_chk          = $mail_account['last_check']   ?? DBA::NULL_DATETIME;
194
195                 $ssl_options = ['TLS' => 'TLS', 'SSL' => 'SSL'];
196                 if ($this->config->get('system', 'insecure_imap')) {
197                         $ssl_options['notls'] = $this->t('None');
198                 }
199
200                 $tpl = Renderer::getMarkupTemplate('settings/connectors.tpl');
201                 $o   = Renderer::replaceMacros($tpl, [
202                         '$form_security_token' => BaseSettings::getFormSecurityToken("settings_connectors"),
203
204                         '$title' => $this->t('Social Networks'),
205
206                         '$diasp_enabled' => $diasp_enabled,
207                         '$ostat_enabled' => $ostat_enabled,
208
209                         '$general_settings'   => $this->t('General Social Media Settings'),
210                         '$accept_only_sharer' => [
211                                 'accept_only_sharer',
212                                 $this->t('Followed content scope'),
213                                 $accept_only_sharer,
214                                 $this->t('By default, conversations in which your follows participated but didn\'t start will be shown in your timeline. You can turn this behavior off, or expand it to the conversations in which your follows liked a post.'),
215                                 [
216                                         Item::COMPLETION_NONE    => $this->t('Only conversations my follows started'),
217                                         Item::COMPLETION_COMMENT => $this->t('Conversations my follows started or commented on (default)'),
218                                         Item::COMPLETION_LIKE    => $this->t('Any conversation my follows interacted with, including likes'),
219                                 ]
220                         ],
221                         '$enable_cw'               => ['enable_cw', $this->t('Enable Content Warning'), $enable_cw, $this->t('Users on networks like Mastodon or Pleroma are able to set a content warning field which collapse their post by default. This enables the automatic collapsing instead of setting the content warning as the post title. Doesn\'t affect any other content filtering you eventually set up.')],
222                         '$enable_smart_shortening' => ['enable_smart_shortening', $this->t('Enable intelligent shortening'), $enable_smart_shortening, $this->t('Normally the system tries to find the best link to add to shortened posts. If disabled, every shortened post will always point to the original friendica post.')],
223                         '$simple_shortening'       => ['simple_shortening', $this->t('Enable simple text shortening'), $simple_shortening, $this->t('Normally the system shortens posts at the next line feed. If this option is enabled then the system will shorten the text at the maximum character limit.')],
224                         '$attach_link_title'       => ['attach_link_title', $this->t('Attach the link title'), $attach_link_title, $this->t('When activated, the title of the attached link will be added as a title on posts to Diaspora. This is mostly helpful with "remote-self" contacts that share feed content.')],
225                         '$api_spoiler_title'       => ['api_spoiler_title', $this->t('API: Use spoiler field as title'), $api_spoiler_title, $this->t('When activated, the "spoiler_text" field in the API will be used for the title on standalone posts. When deactivated it will be used for spoiler text. For comments it will always be used for spoiler text.')],
226                         '$api_auto_attach'         => ['api_auto_attach', $this->t('API: Automatically links at the end of the post as attached posts'), $api_auto_attach, $this->t('When activated, added links at the end of the post react the same way as added links in the web interface.')],
227                         '$legacy_contact'          => ['legacy_contact', $this->t('Your legacy ActivityPub/GNU Social account'), $legacy_contact, $this->t('If you enter your old account name from an ActivityPub based system or your GNU Social/Statusnet account name here (in the format user@domain.tld), your contacts will be added automatically. The field will be emptied when done.')],
228                         '$repair_ostatus_url'  => 'ostatus/repair',
229                         '$repair_ostatus_text' => $this->t('Repair OStatus subscriptions'),
230
231                         '$connector_settings_forms' => $connector_settings_forms,
232
233                         '$h_mail'            => $this->t('Email/Mailbox Setup'),
234                         '$mail_desc'         => $this->t("If you wish to communicate with email contacts using this service \x28optional\x29, please specify how to connect to your mailbox."),
235                         '$mail_lastcheck'    => ['mail_lastcheck', $this->t('Last successful email check:'), $mail_chk, ''],
236                         '$mail_disabled'     => $mail_disabled,
237                         '$mail_server'       => ['mail_server', $this->t('IMAP server name:'), $mail_server, ''],
238                         '$mail_port'         => ['mail_port', $this->t('IMAP port:'), $mail_port, ''],
239                         '$mail_ssl'          => ['mail_ssl', $this->t('Security:'), strtoupper($mail_ssl), '', $ssl_options],
240                         '$mail_user'         => ['mail_user', $this->t('Email login name:'), $mail_user, ''],
241                         '$mail_pass'         => ['mail_pass', $this->t('Email password:'), '', ''],
242                         '$mail_replyto'      => ['mail_replyto', $this->t('Reply-to address:'), $mail_replyto, 'Optional'],
243                         '$mail_pubmail'      => ['mail_pubmail', $this->t('Send public posts to all email contacts:'), $mail_pubmail, ''],
244                         '$mail_action'       => ['mail_action', $this->t('Action after import:'), $mail_action, '', [0 => $this->t('None'), 1 => $this->t('Delete'), 2 => $this->t('Mark as seen'), 3 => $this->t('Move to folder')]],
245                         '$mail_movetofolder' => ['mail_movetofolder', $this->t('Move to folder:'), $mail_movetofolder, ''],
246                         '$submit'            => $this->t('Save Settings'),
247                 ]);
248
249                 return $o;
250         }
251 }