]> git.mxchange.org Git - friendica.git/blobdiff - mod/settings.php
Merge pull request #11014 from MrPetovan/task/10978-settings-keyboard-actionable
[friendica.git] / mod / settings.php
index ce556b8bf146c5d981b38897583713b87c302daf..632517f16554e7e86d29850c0af7db6928bacd68 100644 (file)
@@ -37,7 +37,6 @@ use Friendica\Model\User;
 use Friendica\Module\BaseSettings;
 use Friendica\Module\Security\Login;
 use Friendica\Protocol\Email;
-use Friendica\Util\Strings;
 use Friendica\Util\Temporal;
 use Friendica\Worker\Delivery;
 
@@ -48,7 +47,7 @@ function settings_init(App $a)
                return;
        }
 
-       BaseSettings::content();
+       BaseSettings::createAside();
 }
 
 function settings_post(App $a)
@@ -63,25 +62,26 @@ function settings_post(App $a)
        }
 
        if ((DI::args()->getArgc() > 1) && (DI::args()->getArgv()[1] == 'addon')) {
-               BaseModule::checkFormSecurityTokenRedirectOnError('/settings/addon', 'settings_addon');
+               BaseModule::checkFormSecurityTokenRedirectOnError(DI::args()->getQueryString(), 'settings_addon');
 
                Hook::callAll('addon_settings_post', $_POST);
+               DI::baseUrl()->redirect(DI::args()->getQueryString());
                return;
        }
 
        $user = User::getById($a->getLoggedInUserId());
 
        if ((DI::args()->getArgc() > 1) && (DI::args()->getArgv()[1] == 'connectors')) {
-               BaseModule::checkFormSecurityTokenRedirectOnError('/settings/connectors', 'settings_connectors');
+               BaseModule::checkFormSecurityTokenRedirectOnError(DI::args()->getQueryString(), 'settings_connectors');
 
                if (!empty($_POST['general-submit'])) {
                        DI::pConfig()->set(local_user(), 'system', 'accept_only_sharer', intval($_POST['accept_only_sharer']));
-                       DI::pConfig()->set(local_user(), 'system', 'disable_cw', intval($_POST['disable_cw']));
-                       DI::pConfig()->set(local_user(), 'system', 'no_intelligent_shortening', intval($_POST['no_intelligent_shortening']));
+                       DI::pConfig()->set(local_user(), 'system', 'disable_cw', !intval($_POST['enable_cw']));
+                       DI::pConfig()->set(local_user(), 'system', 'no_intelligent_shortening', !intval($_POST['enable_smart_shortening']));
                        DI::pConfig()->set(local_user(), 'system', 'simple_shortening', intval($_POST['simple_shortening']));
                        DI::pConfig()->set(local_user(), 'system', 'attach_link_title', intval($_POST['attach_link_title']));
                        DI::pConfig()->set(local_user(), 'ostatus', 'legacy_contact', $_POST['legacy_contact']);
-               } elseif (!empty($_POST['imap-submit'])) {
+               } elseif (!empty($_POST['mail-submit'])) {
                        $mail_server       =                 $_POST['mail_server']       ?? '';
                        $mail_port         =                 $_POST['mail_port']         ?? '';
                        $mail_ssl          = strtolower(trim($_POST['mail_ssl']          ?? ''));
@@ -92,16 +92,8 @@ function settings_post(App $a)
                        $mail_replyto      =                 $_POST['mail_replyto']      ?? '';
                        $mail_pubmail      =                 $_POST['mail_pubmail']      ?? '';
 
-                       if (
-                               !DI::config()->get('system', 'dfrn_only')
-                               && function_exists('imap_open')
-                               && !DI::config()->get('system', 'imap_disabled')
-                       ) {
-                               $failed = false;
-                               $r = q("SELECT * FROM `mailacct` WHERE `uid` = %d LIMIT 1",
-                                       intval(local_user())
-                               );
-                               if (!DBA::isResult($r)) {
+                       if (function_exists('imap_open') && !DI::config()->get('system', 'imap_disabled')) {
+                               if (!DBA::exists('mailacct', ['uid' => local_user()])) {
                                        DBA::insert('mailacct', ['uid' => local_user()]);
                                }
                                if (strlen($mail_pass)) {
@@ -109,34 +101,30 @@ function settings_post(App $a)
                                        openssl_public_encrypt($mail_pass, $pass, $user['pubkey']);
                                        DBA::update('mailacct', ['pass' => bin2hex($pass)], ['uid' => local_user()]);
                                }
-                               $r = q("UPDATE `mailacct` SET `server` = '%s', `port` = %d, `ssltype` = '%s', `user` = '%s',
-                                       `action` = %d, `movetofolder` = '%s',
-                                       `mailbox` = 'INBOX', `reply_to` = '%s', `pubmail` = %d WHERE `uid` = %d",
-                                       DBA::escape($mail_server),
-                                       intval($mail_port),
-                                       DBA::escape($mail_ssl),
-                                       DBA::escape($mail_user),
-                                       intval($mail_action),
-                                       DBA::escape($mail_movetofolder),
-                                       DBA::escape($mail_replyto),
-                                       intval($mail_pubmail),
-                                       intval(local_user())
-                               );
+
+                               $r = DBA::update('mailacct', [
+                                       'server'       => $mail_server,
+                                       'port'         => $mail_port,
+                                       'ssltype'      => $mail_ssl,
+                                       'user'         => $mail_user,
+                                       `action`       => $mail_action,
+                                       'movetofolder' => $mail_movetofolder,
+                                       'mailbox'      => 'INBOX',
+                                       'reply_to'     => $mail_replyto,
+                                       'pubmail'      => $mail_pubmail
+                               ], ['uid' => local_user()]);
+
                                Logger::notice('updating mailaccount', ['response' => $r]);
-                               $r = q("SELECT * FROM `mailacct` WHERE `uid` = %d LIMIT 1",
-                                       intval(local_user())
-                               );
-                               if (DBA::isResult($r)) {
-                                       $eacct = $r[0];
-                                       $mb = Email::constructMailboxName($eacct);
-
-                                       if (strlen($eacct['server'])) {
+                               $mailacct = DBA::selectFirst('mailacct', [], ['uid' => local_user()]);
+                               if (DBA::isResult($mailacct)) {
+                                       $mb = Email::constructMailboxName($mailacct);
+
+                                       if (strlen($mailacct['server'])) {
                                                $dcrpass = '';
-                                               openssl_private_decrypt(hex2bin($eacct['pass']), $dcrpass, $user['prvkey']);
+                                               openssl_private_decrypt(hex2bin($mailacct['pass']), $dcrpass, $user['prvkey']);
                                                $mbox = Email::connect($mb, $mail_user, $dcrpass);
                                                unset($dcrpass);
                                                if (!$mbox) {
-                                                       $failed = true;
                                                        notice(DI::l10n()->t('Failed to connect with email account using the settings provided.'));
                                                }
                                        }
@@ -145,6 +133,7 @@ function settings_post(App $a)
                }
 
                Hook::callAll('connector_settings_post', $_POST);
+               DI::baseUrl()->redirect(DI::args()->getQueryString());
                return;
        }
 
@@ -228,14 +217,14 @@ function settings_post(App $a)
                }
        }
 
-       $username         = (!empty($_POST['username'])   ? Strings::escapeTags(trim($_POST['username']))     : '');
-       $email            = (!empty($_POST['email'])      ? Strings::escapeTags(trim($_POST['email']))        : '');
-       $timezone         = (!empty($_POST['timezone'])   ? Strings::escapeTags(trim($_POST['timezone']))     : '');
-       $language         = (!empty($_POST['language'])   ? Strings::escapeTags(trim($_POST['language']))     : '');
+       $username         = (!empty($_POST['username'])        ? trim($_POST['username'])          : '');
+       $email            = (!empty($_POST['email'])           ? trim($_POST['email'])             : '');
+       $timezone         = (!empty($_POST['timezone'])        ? trim($_POST['timezone'])          : '');
+       $language         = (!empty($_POST['language'])        ? trim($_POST['language'])          : '');
 
-       $defloc           = (!empty($_POST['defloc'])     ? Strings::escapeTags(trim($_POST['defloc']))       : '');
-       $maxreq           = (!empty($_POST['maxreq'])     ? intval($_POST['maxreq'])             : 0);
-       $expire           = (!empty($_POST['expire'])     ? intval($_POST['expire'])             : 0);
+       $defloc           = (!empty($_POST['defloc'])          ? trim($_POST['defloc'])            : '');
+       $maxreq           = (!empty($_POST['maxreq'])          ? intval($_POST['maxreq'])          : 0);
+       $expire           = (!empty($_POST['expire'])          ? intval($_POST['expire'])          : 0);
        $def_gid          = (!empty($_POST['group-selection']) ? intval($_POST['group-selection']) : 0);
 
 
@@ -262,6 +251,9 @@ function settings_post(App $a)
        $unlisted         = (($_POST['unlisted'] == 1) ? 1: 0);
        $accessiblephotos = (($_POST['accessible-photos'] == 1) ? 1: 0);
 
+       $notify_like      = (($_POST['notify_like'] == 1) ? 1 : 0);
+       $notify_announce  = (($_POST['notify_announce'] == 1) ? 1 : 0);
+
        $email_textonly   = (($_POST['email_textonly'] == 1) ? 1 : 0);
        $detailed_notif   = (($_POST['detailed_notif'] == 1) ? 1 : 0);
 
@@ -318,7 +310,9 @@ function settings_post(App $a)
 
        if ($email != $user['email']) {
                //  check for the correct password
-               if (!User::authenticate(intval(local_user()), $_POST['mpassword'])) {
+               try {
+                       User::getIdFromPasswordAuthentication(local_user(), $_POST['mpassword']);
+               } catch (Exception $ex) {
                        $err .= DI::l10n()->t('Wrong Password.');
                        $email = $user['email'];
                }
@@ -342,7 +336,7 @@ function settings_post(App $a)
        }
 
        if (($timezone != $user['timezone']) && strlen($timezone)) {
-               date_default_timezone_set($timezone);
+               $a->setTimeZone($timezone);
        }
 
        $aclFormatter = DI::aclFormatter();
@@ -358,6 +352,9 @@ function settings_post(App $a)
        DI::pConfig()->set(local_user(), 'expire', 'photos', $expire_photos);
        DI::pConfig()->set(local_user(), 'expire', 'network_only', $expire_network_only);
 
+       DI::pConfig()->set(local_user(), 'system', 'notify_like', $notify_like);
+       DI::pConfig()->set(local_user(), 'system', 'notify_announce', $notify_announce);
+
        DI::pConfig()->set(local_user(), 'system', 'email_textonly', $email_textonly);
        DI::pConfig()->set(local_user(), 'system', 'detailed_notif', $detailed_notif);
        DI::pConfig()->set(local_user(), 'system', 'notify_ignored', $notify_ignored);
@@ -413,7 +410,7 @@ function settings_content(App $a)
 
        if (!empty($_SESSION['submanage'])) {
                notice(DI::l10n()->t('Permission denied.'));
-               return;
+               return '';
        }
 
        if ((DI::args()->getArgc() > 1) && (DI::args()->getArgv()[1] === 'oauth')) {
@@ -422,7 +419,7 @@ function settings_content(App $a)
 
                        DBA::delete('application-token', ['application-id' => DI::args()->getArgv()[3], 'uid' => local_user()]);
                        DI::baseUrl()->redirect('settings/oauth/', true);
-                       return;
+                       return '';
                }
 
                $applications = DBA::selectToArray('application-view', ['id', 'uid', 'name', 'website', 'scopes', 'created_at'], ['uid' => local_user()]);
@@ -443,11 +440,27 @@ function settings_content(App $a)
 
        if ((DI::args()->getArgc() > 1) && (DI::args()->getArgv()[1] === 'addon')) {
                $addon_settings_forms = [];
-
                foreach (DI::dba()->selectToArray('hook', ['file', 'function'], ['hook' => 'addon_settings']) as $hook) {
-                       $data = '';
+                       $data = [];
                        Hook::callSingle(DI::app(), 'addon_settings', [$hook['file'], $hook['function']], $data);
-                       $addon_settings_forms[] = $data;
+
+                       if (!empty($data['href'])) {
+                               $tpl = Renderer::getMarkupTemplate('settings/addon/link.tpl');
+                               $addon_settings_forms[] = Renderer::replaceMacros($tpl, [
+                                       '$addon' => $data['addon'],
+                                       '$title' => $data['title'],
+                                       '$href'  => $data['href'],
+                               ]);
+                       } elseif(!empty($data['addon'])) {
+                               $tpl = Renderer::getMarkupTemplate('settings/addon/panel.tpl');
+                               $addon_settings_forms[$data['addon']] = Renderer::replaceMacros($tpl, [
+                                       '$addon'  => $data['addon'],
+                                       '$title'  => $data['title'],
+                                       '$open'   => (DI::args()->getArgv()[2] ?? '') === $data['addon'],
+                                       '$html'   => $data['html'] ?? '',
+                                       '$submit' => $data['submit'] ?? DI::l10n()->t('Save Settings'),
+                               ]);
+                       }
                }
 
                $tpl = Renderer::getMarkupTemplate('settings/addons.tpl');
@@ -484,8 +497,8 @@ function settings_content(App $a)
 
        if ((DI::args()->getArgc() > 1) && (DI::args()->getArgv()[1] === 'connectors')) {
                $accept_only_sharer        = intval(DI::pConfig()->get(local_user(), 'system', 'accept_only_sharer'));
-               $disable_cw                = intval(DI::pConfig()->get(local_user(), 'system', 'disable_cw'));
-               $no_intelligent_shortening = intval(DI::pConfig()->get(local_user(), 'system', 'no_intelligent_shortening'));
+               $enable_cw                 = !intval(DI::pConfig()->get(local_user(), 'system', 'disable_cw'));
+               $enable_smart_shortening   = !intval(DI::pConfig()->get(local_user(), 'system', 'no_intelligent_shortening'));
                $simple_shortening         = intval(DI::pConfig()->get(local_user(), 'system', 'simple_shortening'));
                $attach_link_title         = intval(DI::pConfig()->get(local_user(), 'system', 'attach_link_title'));
                $legacy_contact            = DI::pConfig()->get(local_user(), 'ostatus', 'legacy_contact');
@@ -495,10 +508,24 @@ function settings_content(App $a)
                        DI::page()['htmlhead'] = '<meta http-equiv="refresh" content="0; URL=' . DI::baseUrl().'/ostatus_subscribe?url=' . urlencode($legacy_contact) . '">';
                }
 
-               $settings_connectors = '';
-               Hook::callAll('connector_settings', $settings_connectors);
+               $connector_settings_forms = [];
+               foreach (DI::dba()->selectToArray('hook', ['file', 'function'], ['hook' => 'connector_settings']) as $hook) {
+                       $data = [];
+                       Hook::callSingle(DI::app(), 'connector_settings', [$hook['file'], $hook['function']], $data);
+
+                       $tpl = Renderer::getMarkupTemplate('settings/addon/connector.tpl');
+                       $connector_settings_forms[$data['connector']] = Renderer::replaceMacros($tpl, [
+                               '$connector' => $data['connector'],
+                               '$title'     => $data['title'],
+                               '$image'     => $data['image'] ?? '',
+                               '$enabled'   => $data['enabled'] ?? true,
+                               '$open'      => (DI::args()->getArgv()[2] ?? '') === $data['connector'],
+                               '$html'      => $data['html'] ?? '',
+                               '$submit'    => $data['submit'] ?? DI::l10n()->t('Save Settings'),
+                       ]);
+               }
 
-               if (is_site_admin()) {
+               if ($a->isSiteAdmin()) {
                        $diasp_enabled = DI::l10n()->t('Built-in support for %s connectivity is %s', DI::l10n()->t('Diaspora (Socialhome, Hubzilla)'), ((DI::config()->get('system', 'diaspora_enabled')) ? DI::l10n()->t('enabled') : DI::l10n()->t('disabled')));
                        $ostat_enabled = DI::l10n()->t('Built-in support for %s connectivity is %s', DI::l10n()->t('OStatus (GNU Social)'), ((DI::config()->get('system', 'ostatus_disabled')) ? DI::l10n()->t('disabled') : DI::l10n()->t('enabled')));
                } else {
@@ -507,26 +534,21 @@ function settings_content(App $a)
                }
 
                $mail_disabled = ((function_exists('imap_open') && (!DI::config()->get('system', 'imap_disabled'))) ? 0 : 1);
-               if (DI::config()->get('system', 'dfrn_only')) {
-                       $mail_disabled = 1;
-               }
                if (!$mail_disabled) {
-                       $r = q("SELECT * FROM `mailacct` WHERE `uid` = %d LIMIT 1",
-                               local_user()
-                       );
+                       $mailacct = DBA::selectFirst('mailacct', [], ['uid' => local_user()]);
                } else {
-                       $r = null;
+                       $mailacct = null;
                }
 
-               $mail_server       = ((DBA::isResult($r)) ? $r[0]['server'] : '');
-               $mail_port         = ((DBA::isResult($r) && intval($r[0]['port'])) ? intval($r[0]['port']) : '');
-               $mail_ssl          = ((DBA::isResult($r)) ? $r[0]['ssltype'] : '');
-               $mail_user         = ((DBA::isResult($r)) ? $r[0]['user'] : '');
-               $mail_replyto      = ((DBA::isResult($r)) ? $r[0]['reply_to'] : '');
-               $mail_pubmail      = ((DBA::isResult($r)) ? $r[0]['pubmail'] : 0);
-               $mail_action       = ((DBA::isResult($r)) ? $r[0]['action'] : 0);
-               $mail_movetofolder = ((DBA::isResult($r)) ? $r[0]['movetofolder'] : '');
-               $mail_chk          = ((DBA::isResult($r)) ? $r[0]['last_check'] : DBA::NULL_DATETIME);
+               $mail_server       = $mailacct['server'] ?? '';
+               $mail_port         = (!empty($mailacct['port']) && is_numeric($mailacct['port'])) ? (int)$mailacct['port'] : '';
+               $mail_ssl          = $mailacct['ssltype'] ?? '';
+               $mail_user         = $mailacct['user'] ?? '';
+               $mail_replyto      = $mailacct['reply_to'] ?? '';
+               $mail_pubmail      = $mailacct['pubmail'] ?? 0;
+               $mail_action       = $mailacct['action'] ?? 0;
+               $mail_movetofolder = $mailacct['movetofolder'] ?? '';
+               $mail_chk          = $mailacct['last_check'] ?? DBA::NULL_DATETIME;
 
 
                $tpl = Renderer::getMarkupTemplate('settings/connectors.tpl');
@@ -549,8 +571,8 @@ function settings_content(App $a)
 
                        '$general_settings' => DI::l10n()->t('General Social Media Settings'),
                        '$accept_only_sharer' => ['accept_only_sharer', DI::l10n()->t('Accept only top level posts by contacts you follow'), $accept_only_sharer, DI::l10n()->t('The system does an auto completion of threads when a comment arrives. This has got the side effect that you can receive posts that had been started by a non-follower but had been commented by someone you follow. This setting deactivates this behaviour. When activated, you strictly only will receive posts from people you really do follow.')],
-                       '$disable_cw' => ['disable_cw', DI::l10n()->t('Disable Content Warning'), $disable_cw, DI::l10n()->t('Users on networks like Mastodon or Pleroma are able to set a content warning field which collapse their post by default. This disables the automatic collapsing and sets the content warning as the post title. Doesn\'t affect any other content filtering you eventually set up.')],
-                       '$no_intelligent_shortening' => ['no_intelligent_shortening', DI::l10n()->t('Disable intelligent shortening'), $no_intelligent_shortening, DI::l10n()->t('Normally the system tries to find the best link to add to shortened posts. If this option is enabled then every shortened post will always point to the original friendica post.')],
+                       '$enable_cw' => ['enable_cw', DI::l10n()->t('Enable Content Warning'), $enable_cw, DI::l10n()->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.')],
+                       '$enable_smart_shortening' => ['enable_smart_shortening', DI::l10n()->t('Enable intelligent shortening'), $enable_smart_shortening, DI::l10n()->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.')],
                        '$simple_shortening' => ['simple_shortening', DI::l10n()->t('Enable simple text shortening'), $simple_shortening, DI::l10n()->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.')],
                        '$attach_link_title' => ['attach_link_title', DI::l10n()->t('Attach the link title'), $attach_link_title, DI::l10n()->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.')],
                        '$legacy_contact' => ['legacy_contact', DI::l10n()->t('Your legacy ActivityPub/GNU Social account'), $legacy_contact, DI::l10n()->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.")],
@@ -558,11 +580,11 @@ function settings_content(App $a)
                        '$repair_ostatus_url' => DI::baseUrl() . '/repair_ostatus',
                        '$repair_ostatus_text' => DI::l10n()->t('Repair OStatus subscriptions'),
 
-                       '$settings_connectors' => $settings_connectors,
+                       '$connector_settings_forms' => $connector_settings_forms,
 
-                       '$h_imap' => DI::l10n()->t('Email/Mailbox Setup'),
-                       '$imap_desc' => DI::l10n()->t("If you wish to communicate with email contacts using this service \x28optional\x29, please specify how to connect to your mailbox."),
-                       '$imap_lastcheck' => ['imap_lastcheck', DI::l10n()->t('Last successful email check:'), $mail_chk, ''],
+                       '$h_mail' => DI::l10n()->t('Email/Mailbox Setup'),
+                       '$mail_desc' => DI::l10n()->t("If you wish to communicate with email contacts using this service \x28optional\x29, please specify how to connect to your mailbox."),
+                       '$mail_lastcheck' => ['mail_lastcheck', DI::l10n()->t('Last successful email check:'), $mail_chk, ''],
                        '$mail_disabled' => $mail_disabled_message,
                        '$mail_server'  => ['mail_server',      DI::l10n()->t('IMAP server name:'), $mail_server, ''],
                        '$mail_port'    => ['mail_port',        DI::l10n()->t('IMAP port:'), $mail_port, ''],
@@ -587,7 +609,7 @@ function settings_content(App $a)
        $profile = DBA::selectFirst('profile', [], ['uid' => local_user()]);
        if (!DBA::isResult($profile)) {
                notice(DI::l10n()->t('Unable to find your profile. Please contact your admin.'));
-               return;
+               return '';
        }
 
        $user = User::getById($a->getLoggedInUserId());
@@ -612,7 +634,7 @@ function settings_content(App $a)
        $expire_network_only = DI::pConfig()->get(local_user(), 'expire', 'network_only', false);
 
        if (!strlen($user['timezone'])) {
-               $timezone = date_default_timezone_get();
+               $timezone = $a->getTimeZone();
        }
 
        // Set the account type to "Community" when the page is a community page but the account type doesn't fit
@@ -771,6 +793,10 @@ function settings_content(App $a)
                '$notify7'  => ['notify7', DI::l10n()->t('You are tagged in a post'), ($notify & Notification\Type::TAG_SELF), Notification\Type::TAG_SELF, ''],
                '$notify8'  => ['notify8', DI::l10n()->t('You are poked/prodded/etc. in a post'), ($notify & Notification\Type::POKE), Notification\Type::POKE, ''],
 
+               '$lbl_notify'      => DI::l10n()->t('Create a desktop notification when:'),
+               '$notify_like'     => ['notify_like', DI::l10n()->t('Someone liked your content'), DI::pConfig()->get(local_user(), 'system', 'notify_like'), ''],
+               '$notify_announce' => ['notify_announce', DI::l10n()->t('Someone shared your content'), DI::pConfig()->get(local_user(), 'system', 'notify_announce'), ''],
+
                '$desktop_notifications' => ['desktop_notifications', DI::l10n()->t('Activate desktop notifications') , false, DI::l10n()->t('Show desktop popup on new notifications')],
 
                '$email_textonly' => ['email_textonly', DI::l10n()->t('Text-only notification emails'),