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