]> git.mxchange.org Git - friendica.git/blob - src/Module/Settings/RemoveMe.php
Merge remote-tracking branch 'upstream/develop' into api
[friendica.git] / src / Module / Settings / RemoveMe.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\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 IManageConfigValues */
44         private $config;
45         /** @var Database */
46         private $database;
47         /** @var Emailer */
48         private $emailer;
49         /** @var SystemMessages */
50         private $systemMessages;
51         /** @var Cookie */
52         private $cookie;
53
54         public function __construct(Cookie $cookie, SystemMessages $systemMessages, Emailer $emailer, Database $database, 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 = [])
55         {
56                 parent::__construct($session, $page, $l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
57
58                 $this->config         = $config;
59                 $this->database       = $database;
60                 $this->emailer        = $emailer;
61                 $this->systemMessages = $systemMessages;
62                 $this->cookie         = $cookie;
63         }
64
65         protected function post(array $request = [])
66         {
67                 if (!$this->session->getLocalUserId()) {
68                         return;
69                 }
70
71                 if ($this->session->getSubManagedUserId()) {
72                         return;
73                 }
74
75                 $hash = $this->session->pop('remove_account_verify');
76                 if (empty($hash) || empty($request[$hash])) {
77                         return;
78                 }
79
80                 try {
81                         $userId = User::getIdFromPasswordAuthentication($this->session->getLocalUserId(), trim($request[$hash]));
82                 } catch (\Throwable $e) {
83                         $this->systemMessages->addNotice($e->getMessage());
84                         return;
85                 }
86
87                 // send notification to admins so that they can clean up the backups
88                 foreach (User::getAdminListForEmailing(['uid', 'language', 'email']) as $admin) {
89                         $l10n = $this->l10n->withLang($admin['language'] ?: 'en');
90
91                         $email = $this->emailer
92                                 ->newSystemMail()
93                                 ->withMessage(
94                                         $l10n->t('[Friendica System Notify]') . ' ' . $l10n->t('User deleted their account'),
95                                         $l10n->t('On your Friendica node an user deleted their account. Please ensure that their data is removed from the backups.'),
96                                         $l10n->t('The user id is %d', $this->session->getLocalUserId()))
97                                 ->forUser($admin)
98                                 ->withRecipient($admin['email'])
99                                 ->build();
100                         $this->emailer->send($email);
101                 }
102
103                 User::remove($userId);
104
105                 $this->session->clear();
106                 $this->cookie->clear();
107
108                 $this->systemMessages->addInfo($this->t('Your user account has been successfully removed. Bye bye!'));
109                 $this->baseUrl->redirect();
110         }
111
112         protected function content(array $request = []): string
113         {
114                 parent::content();
115
116                 if (!$this->session->getLocalUserId()) {
117                         $this->systemMessages->addNotice($this->t('Permission denied.'));
118                         $this->baseUrl->redirect();
119                 }
120
121                 $hash = Strings::getRandomHex();
122
123                 $this->session->set('remove_account_verify', $hash);
124
125                 $tpl = Renderer::getMarkupTemplate('settings/removeme.tpl');
126                 return Renderer::replaceMacros($tpl, [
127                         '$l10n' => [
128                                 'title' => DI::l10n()->t('Remove My Account'),
129                                 'desc'  => DI::l10n()->t('This will completely remove your account. Once this has been done it is not recoverable.'),
130                         ],
131                         '$password' => [$hash, $this->t('Please enter your password for verification:'), null, null, true],
132                 ]);
133         }
134 }