]> git.mxchange.org Git - friendica.git/blob - src/Module/Settings/RemoveMe.php
Move settings/features to src/Module
[friendica.git] / src / Module / Settings / RemoveMe.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, 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\L10n;
27 use Friendica\Core\Renderer;
28 use Friendica\Core\Session\Capability\IHandleUserSessions;
29 use Friendica\Database\Database;
30 use Friendica\DI;
31 use Friendica\Model\User;
32 use Friendica\Model\User\Cookie;
33 use Friendica\Module\BaseSettings;
34 use Friendica\Module\Response;
35 use Friendica\Navigation\SystemMessages;
36 use Friendica\Util\Emailer;
37 use Friendica\Util\Profiler;
38 use Friendica\Util\Strings;
39 use Psr\Log\LoggerInterface;
40
41 class RemoveMe extends BaseSettings
42 {
43         /** @var IHandleUserSessions */
44         private $session;
45         /** @var IManageConfigValues */
46         private $config;
47         /** @var Database */
48         private $database;
49         /** @var Emailer */
50         private $emailer;
51         /** @var SystemMessages */
52         private $systemMessages;
53         /** @var Cookie */
54         private $cookie;
55
56         public function __construct(Cookie $cookie, SystemMessages $systemMessages, Emailer $emailer, Database $database, IManageConfigValues $config, IHandleUserSessions $session, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, array $server, array $parameters = [])
57         {
58                 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
59
60                 $this->session        = $session;
61                 $this->config         = $config;
62                 $this->database       = $database;
63                 $this->emailer        = $emailer;
64                 $this->systemMessages = $systemMessages;
65                 $this->cookie         = $cookie;
66         }
67
68         protected function post(array $request = [])
69         {
70                 if (!$this->session->getLocalUserId()) {
71                         return;
72                 }
73
74                 if ($this->session->getSubManagedUserId()) {
75                         return;
76                 }
77
78                 $hash = $this->session->pop('remove_account_verify');
79                 if (empty($hash) || empty($request[$hash])) {
80                         return;
81                 }
82
83                 try {
84                         $userId = User::getIdFromPasswordAuthentication($this->session->getLocalUserId(), trim($request[$hash]));
85                 } catch (\Throwable $e) {
86                         $this->systemMessages->addNotice($e->getMessage());
87                         return;
88                 }
89
90                 // send notification to admins so that they can clean up the backups
91                 $admin_mails = explode(',', $this->config->get('config', 'admin_email'));
92                 foreach ($admin_mails as $mail) {
93                         $admin = $this->database->selectFirst('user', ['uid', 'language', 'email', 'username'], ['email' => trim($mail)]);
94                         if (!$admin) {
95                                 continue;
96                         }
97
98                         $l10n = $this->l10n->withLang($admin['language']);
99
100                         $email = $this->emailer
101                                 ->newSystemMail()
102                                 ->withMessage(
103                                         $l10n->t('[Friendica System Notify]') . ' ' . $l10n->t('User deleted their account'),
104                                         $l10n->t('On your Friendica node an user deleted their account. Please ensure that their data is removed from the backups.'),
105                                         $l10n->t('The user id is %d', $this->session->getLocalUserId()))
106                                 ->forUser($admin)
107                                 ->withRecipient($admin['email'])
108                                 ->build();
109                         $this->emailer->send($email);
110                 }
111
112                 User::remove($userId);
113
114                 $this->session->clear();
115                 $this->cookie->clear();
116
117                 $this->systemMessages->addInfo($this->t('Your user account has been successfully removed. Bye bye!'));
118                 $this->baseUrl->redirect();
119         }
120
121         protected function content(array $request = []): string
122         {
123                 parent::content();
124
125                 if (!$this->session->getLocalUserId()) {
126                         $this->systemMessages->addNotice($this->t('Permission denied.'));
127                         $this->baseUrl->redirect();
128                 }
129
130                 $hash = Strings::getRandomHex();
131
132                 $this->session->set('remove_account_verify', $hash);
133
134                 $tpl = Renderer::getMarkupTemplate('settings/removeme.tpl');
135                 return Renderer::replaceMacros($tpl, [
136                         '$l10n' => [
137                                 'title' => DI::l10n()->t('Remove My Account'),
138                                 'desc'  => DI::l10n()->t('This will completely remove your account. Once this has been done it is not recoverable.'),
139                         ],
140                         '$password' => [$hash, $this->t('Please enter your password for verification:'), null, null, true],
141                 ]);
142         }
143 }