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