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