]> git.mxchange.org Git - friendica.git/blob - src/Module/Settings/Connectors.php
Merge branch 'develop' into mastodon-instance-v2-implementation
[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(), 'ostatus', 'legacy_contact', $request['legacy_contact']);
77                 } elseif (!empty($request['mail-submit']) && function_exists('imap_open') && !$this->config->get('system', 'imap_disabled')) {
78                         $mail_server       =                 $request['mail_server'] ?? '';
79                         $mail_port         =                 $request['mail_port'] ?? '';
80                         $mail_ssl          = strtolower(trim($request['mail_ssl'] ?? ''));
81                         $mail_user         =                 $request['mail_user'] ?? '';
82                         $mail_pass         =            trim($request['mail_pass'] ?? '');
83                         $mail_action       =            trim($request['mail_action'] ?? '');
84                         $mail_movetofolder =            trim($request['mail_movetofolder'] ?? '');
85                         $mail_replyto      =                 $request['mail_replyto'] ?? '';
86                         $mail_pubmail      =                 $request['mail_pubmail'] ?? '';
87
88                         if (!$this->database->exists('mailacct', ['uid' => $this->session->getLocalUserId()])) {
89                                 $this->database->insert('mailacct', ['uid' => $this->session->getLocalUserId()]);
90                         }
91
92                         if (strlen($mail_pass)) {
93                                 $pass = '';
94                                 openssl_public_encrypt($mail_pass, $pass, $user['pubkey']);
95                                 $this->database->update('mailacct', ['pass' => bin2hex($pass)], ['uid' => $this->session->getLocalUserId()]);
96                         }
97
98                         $r = $this->database->update('mailacct', [
99                                 'server'       => $mail_server,
100                                 'port'         => $mail_port,
101                                 'ssltype'      => $mail_ssl,
102                                 'user'         => $mail_user,
103                                 'action'       => $mail_action,
104                                 'movetofolder' => $mail_movetofolder,
105                                 'mailbox'      => 'INBOX',
106                                 'reply_to'     => $mail_replyto,
107                                 'pubmail'      => $mail_pubmail
108                         ], ['uid' => $this->session->getLocalUserId()]);
109
110                         $this->logger->debug('updating mailaccount', ['response' => $r]);
111                         $mailacct = $this->database->selectFirst('mailacct', [], ['uid' => $this->session->getLocalUserId()]);
112                         if ($this->database->isResult($mailacct)) {
113                                 if (strlen($mailacct['server'])) {
114                                         $dcrpass = '';
115                                         openssl_private_decrypt(hex2bin($mailacct['pass']), $dcrpass, $user['prvkey']);
116                                         $mbox = Email::connect(Email::constructMailboxName($mailacct), $mail_user, $dcrpass);
117                                         unset($dcrpass);
118                                         if (!$mbox) {
119                                                 $this->systemMessages->addNotice($this->t('Failed to connect with email account using the settings provided.'));
120                                         }
121                                 }
122                         }
123                 }
124
125                 Hook::callAll('connector_settings_post', $request);
126                 $this->baseUrl->redirect($this->args->getQueryString());
127         }
128
129         protected function content(array $request = []): string
130         {
131                 parent::content($request);
132
133                 $accept_only_sharer      =  intval($this->pconfig->get($this->session->getLocalUserId(), 'system', 'accept_only_sharer'));
134                 $enable_cw               = !intval($this->pconfig->get($this->session->getLocalUserId(), 'system', 'disable_cw'));
135                 $enable_smart_shortening = !intval($this->pconfig->get($this->session->getLocalUserId(), 'system', 'no_intelligent_shortening'));
136                 $simple_shortening       =  intval($this->pconfig->get($this->session->getLocalUserId(), 'system', 'simple_shortening'));
137                 $attach_link_title       =  intval($this->pconfig->get($this->session->getLocalUserId(), 'system', 'attach_link_title'));
138                 $api_spoiler_title       =  intval($this->pconfig->get($this->session->getLocalUserId(), 'system', 'api_spoiler_title', true));
139                 $legacy_contact          =         $this->pconfig->get($this->session->getLocalUserId(), 'ostatus', 'legacy_contact');
140
141                 if (!empty($legacy_contact)) {
142                         $this->baseUrl->redirect('ostatus/subscribe?url=' . urlencode($legacy_contact));
143                 }
144
145                 $connector_settings_forms = [];
146                 foreach ($this->database->selectToArray('hook', ['file', 'function'], ['hook' => 'connector_settings']) as $hook) {
147                         $data = [];
148                         Hook::callSingle('connector_settings', [$hook['file'], $hook['function']], $data);
149
150                         $tpl                                          = Renderer::getMarkupTemplate('settings/addons/connector.tpl');
151                         $connector_settings_forms[$data['connector']] = Renderer::replaceMacros($tpl, [
152                                 '$connector' => $data['connector'],
153                                 '$title'     => $data['title'],
154                                 '$image'     => $data['image'] ?? '',
155                                 '$enabled'   => $data['enabled'] ?? true,
156                                 '$open'      => ($this->parameters['connector'] ?? '') === $data['connector'],
157                                 '$html'      => $data['html'] ?? '',
158                                 '$submit'    => $data['submit'] ?? $this->t('Save Settings'),
159                         ]);
160                 }
161
162                 if ($this->session->isSiteAdmin()) {
163                         $diasp_enabled = $this->config->get('system', 'diaspora_enabled') ?
164                                 $this->t('Built-in support for %s connectivity is enabled', $this->t('Diaspora (Socialhome, Hubzilla)')) :
165                                 $this->t('Built-in support for %s connectivity is disabled', $this->t('Diaspora (Socialhome, Hubzilla)'));
166                         $ostat_enabled = $this->config->get('system', 'ostatus_disabled') ?
167                                 $this->t('Built-in support for %s connectivity is disabled', $this->t('OStatus (GNU Social)')) :
168                                 $this->t('Built-in support for %s connectivity is enabled', $this->t('OStatus (GNU Social)'));
169                 } else {
170                         $diasp_enabled = '';
171                         $ostat_enabled = '';
172                 }
173
174                 $mail_enabled = function_exists('imap_open') && !$this->config->get('system', 'imap_disabled');
175                 if ($mail_enabled) {
176                         $mail_account  = $this->database->selectFirst('mailacct', [], ['uid' => $this->session->getLocalUserId()]);
177                         $mail_disabled = '';
178                 } else {
179                         $mail_account  = null;
180                         $mail_disabled = $this->t('Email access is disabled on this site.');
181                 }
182
183                 $mail_server       = $mail_account['server']       ?? '';
184                 $mail_port         = (!empty($mail_account['port']) && is_numeric($mail_account['port'])) ? (int)$mail_account['port'] : '';
185                 $mail_ssl          = $mail_account['ssltype']      ?? '';
186                 $mail_user         = $mail_account['user']         ?? '';
187                 $mail_replyto      = $mail_account['reply_to']     ?? '';
188                 $mail_pubmail      = $mail_account['pubmail']      ?? 0;
189                 $mail_action       = $mail_account['action']       ?? 0;
190                 $mail_movetofolder = $mail_account['movetofolder'] ?? '';
191                 $mail_chk          = $mail_account['last_check']   ?? DBA::NULL_DATETIME;
192
193                 $ssl_options = ['TLS' => 'TLS', 'SSL' => 'SSL'];
194                 if ($this->config->get('system', 'insecure_imap')) {
195                         $ssl_options['notls'] = $this->t('None');
196                 }
197
198                 $tpl = Renderer::getMarkupTemplate('settings/connectors.tpl');
199                 $o   = Renderer::replaceMacros($tpl, [
200                         '$form_security_token' => BaseSettings::getFormSecurityToken("settings_connectors"),
201
202                         '$title' => $this->t('Social Networks'),
203
204                         '$diasp_enabled' => $diasp_enabled,
205                         '$ostat_enabled' => $ostat_enabled,
206
207                         '$general_settings'   => $this->t('General Social Media Settings'),
208                         '$accept_only_sharer' => [
209                                 'accept_only_sharer',
210                                 $this->t('Followed content scope'),
211                                 $accept_only_sharer,
212                                 $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.'),
213                                 [
214                                         Item::COMPLETION_NONE    => $this->t('Only conversations my follows started'),
215                                         Item::COMPLETION_COMMENT => $this->t('Conversations my follows started or commented on (default)'),
216                                         Item::COMPLETION_LIKE    => $this->t('Any conversation my follows interacted with, including likes'),
217                                 ]
218                         ],
219                         '$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.')],
220                         '$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.')],
221                         '$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.')],
222                         '$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.')],
223                         '$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.')],
224                         '$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.')],
225                         '$repair_ostatus_url'  => 'ostatus/repair',
226                         '$repair_ostatus_text' => $this->t('Repair OStatus subscriptions'),
227
228                         '$connector_settings_forms' => $connector_settings_forms,
229
230                         '$h_mail'            => $this->t('Email/Mailbox Setup'),
231                         '$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."),
232                         '$mail_lastcheck'    => ['mail_lastcheck', $this->t('Last successful email check:'), $mail_chk, ''],
233                         '$mail_disabled'     => $mail_disabled,
234                         '$mail_server'       => ['mail_server', $this->t('IMAP server name:'), $mail_server, ''],
235                         '$mail_port'         => ['mail_port', $this->t('IMAP port:'), $mail_port, ''],
236                         '$mail_ssl'          => ['mail_ssl', $this->t('Security:'), strtoupper($mail_ssl), '', $ssl_options],
237                         '$mail_user'         => ['mail_user', $this->t('Email login name:'), $mail_user, ''],
238                         '$mail_pass'         => ['mail_pass', $this->t('Email password:'), '', ''],
239                         '$mail_replyto'      => ['mail_replyto', $this->t('Reply-to address:'), $mail_replyto, 'Optional'],
240                         '$mail_pubmail'      => ['mail_pubmail', $this->t('Send public posts to all email contacts:'), $mail_pubmail, ''],
241                         '$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')]],
242                         '$mail_movetofolder' => ['mail_movetofolder', $this->t('Move to folder:'), $mail_movetofolder, ''],
243                         '$submit'            => $this->t('Save Settings'),
244                 ]);
245
246                 return $o;
247         }
248 }