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