]> git.mxchange.org Git - friendica.git/blob - src/Module/Settings/RemoveMe.php
Merge pull request #12164 from MrPetovan/bug/fatal-errors
[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 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                 $admin_mails = explode(',', $this->config->get('config', 'admin_email'));
89                 foreach ($admin_mails as $mail) {
90                         $admin = $this->database->selectFirst('user', ['uid', 'language', 'email', 'username'], ['email' => trim($mail)]);
91                         if (!$admin) {
92                                 continue;
93                         }
94
95                         $l10n = $this->l10n->withLang($admin['language']);
96
97                         $email = $this->emailer
98                                 ->newSystemMail()
99                                 ->withMessage(
100                                         $l10n->t('[Friendica System Notify]') . ' ' . $l10n->t('User deleted their account'),
101                                         $l10n->t('On your Friendica node an user deleted their account. Please ensure that their data is removed from the backups.'),
102                                         $l10n->t('The user id is %d', $this->session->getLocalUserId()))
103                                 ->forUser($admin)
104                                 ->withRecipient($admin['email'])
105                                 ->build();
106                         $this->emailer->send($email);
107                 }
108
109                 User::remove($userId);
110
111                 $this->session->clear();
112                 $this->cookie->clear();
113
114                 $this->systemMessages->addInfo($this->t('Your user account has been successfully removed. Bye bye!'));
115                 $this->baseUrl->redirect();
116         }
117
118         protected function content(array $request = []): string
119         {
120                 parent::content();
121
122                 if (!$this->session->getLocalUserId()) {
123                         $this->systemMessages->addNotice($this->t('Permission denied.'));
124                         $this->baseUrl->redirect();
125                 }
126
127                 $hash = Strings::getRandomHex();
128
129                 $this->session->set('remove_account_verify', $hash);
130
131                 $tpl = Renderer::getMarkupTemplate('settings/removeme.tpl');
132                 return Renderer::replaceMacros($tpl, [
133                         '$l10n' => [
134                                 'title' => DI::l10n()->t('Remove My Account'),
135                                 'desc'  => DI::l10n()->t('This will completely remove your account. Once this has been done it is not recoverable.'),
136                         ],
137                         '$password' => [$hash, $this->t('Please enter your password for verification:'), null, null, true],
138                 ]);
139         }
140 }