3 * @copyright Copyright (C) 2010-2024, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Module\Settings;
25 use Friendica\Content\Widget;
26 use Friendica\Core\L10n;
27 use Friendica\Core\Renderer;
28 use Friendica\Core\Session\Capability\IHandleUserSessions;
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;
40 class RemoveMe extends BaseSettings
44 /** @var SystemMessages */
45 private $systemMessages;
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 = [])
51 parent::__construct($session, $page, $l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
53 $this->emailer = $emailer;
54 $this->systemMessages = $systemMessages;
55 $this->cookie = $cookie;
58 protected function post(array $request = [])
60 if (!$this->session->getLocalUserId()) {
64 if ($this->session->getSubManagedUserId()) {
68 $hash = $this->session->pop('remove_account_verify');
69 if (empty($hash) || empty($request[$hash])) {
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.'));
78 } catch (\Throwable $e) {
79 $this->systemMessages->addNotice($e->getMessage());
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');
87 $email = $this->emailer
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()))
94 ->withRecipient($admin['email'])
96 $this->emailer->send($email);
100 User::remove($userId);
102 $this->session->clear();
103 $this->cookie->clear();
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());
114 protected function content(array $request = []): string
118 if (!$this->session->getLocalUserId()) {
119 $this->systemMessages->addNotice($this->t('Permission denied.'));
120 $this->baseUrl->redirect();
123 $hash = Strings::getRandomHex();
125 $this->session->set('remove_account_verify', $hash);
127 $tpl = Renderer::getMarkupTemplate('settings/removeme.tpl');
128 return Renderer::replaceMacros($tpl, [
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.'),
134 '$hovercard' => Widget\Hovercard::getHTML(User::getOwnerDataById($this->session->getLocalUserId())),
136 '$password' => [$hash, $this->t('Please enter your password for verification:'), null, null, true],