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