]> git.mxchange.org Git - friendica.git/blob - src/Module/Moderation/Users/Index.php
Merge pull request #12169 from MrPetovan/bug/7574-notifications-deleted-users
[friendica.git] / src / Module / Moderation / Users / Index.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\Moderation\Users;
23
24 use Friendica\Content\Pager;
25 use Friendica\Core\Renderer;
26 use Friendica\Model\User;
27 use Friendica\Module\Moderation\BaseUsers;
28
29 class Index extends BaseUsers
30 {
31         protected function post(array $request = [])
32         {
33                 $this->checkModerationAccess();
34
35                 self::checkFormSecurityTokenRedirectOnError('moderation/users', 'moderation_users');
36
37                 $users = $request['user'] ?? [];
38
39                 if (!empty($request['page_users_block'])) {
40                         foreach ($users as $uid) {
41                                 User::block($uid);
42                         }
43                         $this->systemMessages->addInfo($this->tt('%s user blocked', '%s users blocked', count($users)));
44                 }
45
46                 if (!empty($request['page_users_unblock'])) {
47                         foreach ($users as $uid) {
48                                 User::block($uid, false);
49                         }
50                         $this->systemMessages->addInfo($this->tt('%s user unblocked', '%s users unblocked', count($users)));
51                 }
52
53                 if (!empty($request['page_users_delete'])) {
54                         foreach ($users as $uid) {
55                                 if ($this->session->getLocalUserId() != $uid) {
56                                         User::remove($uid);
57                                 } else {
58                                         $this->systemMessages->addNotice($this->t('You can\'t remove yourself'));
59                                 }
60                         }
61
62                         $this->systemMessages->addInfo($this->tt('%s user deleted', '%s users deleted', count($users)));
63                 }
64
65                 $this->baseUrl->redirect($this->args->getQueryString());
66         }
67
68         protected function content(array $request = []): string
69         {
70                 parent::content();
71
72                 $action = $this->parameters['action'] ?? '';
73                 $uid    = $this->parameters['uid']    ?? 0;
74
75                 if ($uid) {
76                         $user = User::getById($uid, ['username', 'blocked']);
77                         if (!$user) {
78                                 $this->systemMessages->addNotice($this->t('User not found'));
79                                 $this->baseUrl->redirect('moderation/users');
80                         }
81                 }
82
83                 switch ($action) {
84                         case 'delete':
85                                 if ($this->session->getLocalUserId() != $uid) {
86                                         self::checkFormSecurityTokenRedirectOnError($this->baseUrl->get(true), 'moderation_users', 't');
87                                         // delete user
88                                         User::remove($uid);
89
90                                         $this->systemMessages->addNotice($this->t('User "%s" deleted', $user['username']));
91                                 } else {
92                                         $this->systemMessages->addNotice($this->t('You can\'t remove yourself'));
93                                 }
94
95                                 $this->baseUrl->redirect('moderation/users');
96                                 break;
97                         case 'block':
98                                 self::checkFormSecurityTokenRedirectOnError('moderation/users', 'moderation_users', 't');
99                                 User::block($uid);
100                                 $this->systemMessages->addNotice($this->t('User "%s" blocked', $user['username']));
101                                 $this->baseUrl->redirect('moderation/users');
102                                 break;
103                         case 'unblock':
104                                 self::checkFormSecurityTokenRedirectOnError('moderation/users', 'moderation_users', 't');
105                                 User::block($uid, false);
106                                 $this->systemMessages->addNotice($this->t('User "%s" unblocked', $user['username']));
107                                 $this->baseUrl->redirect('moderation/users');
108                                 break;
109                 }
110
111                 $pager = new Pager($this->l10n, $this->args->getQueryString(), 100);
112
113                 $valid_orders = [
114                         'name',
115                         'email',
116                         'register_date',
117                         'login_date',
118                         'last-item',
119                         'page-flags',
120                 ];
121
122                 $order           = 'name';
123                 $order_direction = '+';
124                 if (!empty($request['o'])) {
125                         $new_order = $request['o'];
126                         if ($new_order[0] === '-') {
127                                 $order_direction = '-';
128                                 $new_order       = substr($new_order, 1);
129                         }
130
131                         if (in_array($new_order, $valid_orders)) {
132                                 $order = $new_order;
133                         }
134                 }
135
136                 $users = User::getList($pager->getStart(), $pager->getItemsPerPage(), 'all', $order, ($order_direction == '-'));
137
138                 $users = array_map($this->setupUserCallback(), $users);
139
140                 $th_users = array_map(null, [$this->t('Name'), $this->t('Email'), $this->t('Register date'), $this->t('Last login'), $this->t('Last public item'), $this->t('Type')], $valid_orders);
141
142                 $count = $this->database->count('user', ["`uid` != ?", 0]);
143
144                 $t = Renderer::getMarkupTemplate('moderation/users/index.tpl');
145                 return self::getTabsHTML('all') . Renderer::replaceMacros($t, [
146                         // strings //
147                         '$title'          => $this->t('Moderation'),
148                         '$page'           => $this->t('Users'),
149                         '$select_all'     => $this->t('select all'),
150                         '$h_deleted'      => $this->t('User waiting for permanent deletion'),
151                         '$delete'         => $this->t('Delete'),
152                         '$block'          => $this->t('Block'),
153                         '$blocked'        => $this->t('User blocked'),
154                         '$unblock'        => $this->t('Unblock'),
155                         '$siteadmin'      => $this->t('Site admin'),
156                         '$accountexpired' => $this->t('Account expired'),
157
158                         '$h_users'               => $this->t('Users'),
159                         '$h_newuser'             => $this->t('Create a new user'),
160                         '$th_deleted'            => [$this->t('Name'), $this->t('Email'), $this->t('Register date'), $this->t('Last login'), $this->t('Last public item'), $this->t('Permanent deletion')],
161                         '$th_users'              => $th_users,
162                         '$order_users'           => $order,
163                         '$order_direction_users' => $order_direction,
164
165                         '$confirm_delete_multi' => $this->t('Selected users will be deleted!\n\nEverything these users had posted on this site will be permanently deleted!\n\nAre you sure?'),
166                         '$confirm_delete'       => $this->t('The user {0} will be deleted!\n\nEverything this user has posted on this site will be permanently deleted!\n\nAre you sure?'),
167
168                         '$form_security_token' => self::getFormSecurityToken('moderation_users'),
169
170                         // values //
171                         '$baseurl'      => $this->baseUrl->get(true),
172                         '$query_string' => $this->args->getQueryString(),
173
174                         '$users' => $users,
175                         '$count' => $count,
176                         '$pager' => $pager->renderFull($count),
177                 ]);
178         }
179 }