3 * @copyright Copyright (C) 2010-2022, 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\Admin\Users;
24 use Friendica\Content\Pager;
25 use Friendica\Core\Renderer;
26 use Friendica\Core\Session;
27 use Friendica\Database\DBA;
29 use Friendica\Model\User;
30 use Friendica\Module\Admin\BaseUsers;
32 class Index extends BaseUsers
34 protected function post(array $request = [])
36 self::checkAdminAccess();
38 self::checkFormSecurityTokenRedirectOnError('admin/users', 'admin_users');
40 $users = $_POST['user'] ?? [];
42 if (!empty($_POST['page_users_block'])) {
43 foreach ($users as $uid) {
46 DI::sysmsg()->addInfo(DI::l10n()->tt('%s user blocked', '%s users blocked', count($users)));
49 if (!empty($_POST['page_users_unblock'])) {
50 foreach ($users as $uid) {
51 User::block($uid, false);
53 DI::sysmsg()->addInfo(DI::l10n()->tt('%s user unblocked', '%s users unblocked', count($users)));
56 if (!empty($_POST['page_users_delete'])) {
57 foreach ($users as $uid) {
58 if (Session::getLocalUser() != $uid) {
61 DI::sysmsg()->addNotice(DI::l10n()->t('You can\'t remove yourself'));
65 DI::sysmsg()->addInfo(DI::l10n()->tt('%s user deleted', '%s users deleted', count($users)));
68 DI::baseUrl()->redirect(DI::args()->getQueryString());
71 protected function content(array $request = []): string
75 $action = $this->parameters['action'] ?? '';
76 $uid = $this->parameters['uid'] ?? 0;
79 $user = User::getById($uid, ['username', 'blocked']);
80 if (!DBA::isResult($user)) {
81 DI::sysmsg()->addNotice(DI::l10n()->t('User not found'));
82 DI::baseUrl()->redirect('admin/users');
83 return ''; // NOTREACHED
89 if (Session::getLocalUser() != $uid) {
90 self::checkFormSecurityTokenRedirectOnError(DI::baseUrl()->get(true), 'admin_users', 't');
94 DI::sysmsg()->addNotice(DI::l10n()->t('User "%s" deleted', $user['username']));
96 DI::sysmsg()->addNotice(DI::l10n()->t('You can\'t remove yourself'));
99 DI::baseUrl()->redirect('admin/users');
102 self::checkFormSecurityTokenRedirectOnError('admin/users', 'admin_users', 't');
104 DI::sysmsg()->addNotice(DI::l10n()->t('User "%s" blocked', $user['username']));
105 DI::baseUrl()->redirect('admin/users');
108 self::checkFormSecurityTokenRedirectOnError('admin/users', 'admin_users', 't');
109 User::block($uid, false);
110 DI::sysmsg()->addNotice(DI::l10n()->t('User "%s" unblocked', $user['username']));
111 DI::baseUrl()->redirect('admin/users');
114 $pager = new Pager(DI::l10n(), DI::args()->getQueryString(), 100);
126 $order_direction = '+';
127 if (!empty($_GET['o'])) {
128 $new_order = $_GET['o'];
129 if ($new_order[0] === '-') {
130 $order_direction = '-';
131 $new_order = substr($new_order, 1);
134 if (in_array($new_order, $valid_orders)) {
139 $users = User::getList($pager->getStart(), $pager->getItemsPerPage(), 'all', $order, ($order_direction == '-'));
141 $users = array_map(self::setupUserCallback(), $users);
143 $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);
145 $count = DBA::count('user', ["`uid` != ?", 0]);
147 $t = Renderer::getMarkupTemplate('admin/users/index.tpl');
148 return self::getTabsHTML('all') . Renderer::replaceMacros($t, [
150 '$title' => DI::l10n()->t('Administration'),
151 '$page' => DI::l10n()->t('Users'),
152 '$select_all' => DI::l10n()->t('select all'),
153 '$h_deleted' => DI::l10n()->t('User waiting for permanent deletion'),
154 '$delete' => DI::l10n()->t('Delete'),
155 '$block' => DI::l10n()->t('Block'),
156 '$blocked' => DI::l10n()->t('User blocked'),
157 '$unblock' => DI::l10n()->t('Unblock'),
158 '$siteadmin' => DI::l10n()->t('Site admin'),
159 '$accountexpired' => DI::l10n()->t('Account expired'),
161 '$h_users' => DI::l10n()->t('Users'),
162 '$h_newuser' => DI::l10n()->t('Create a new user'),
163 '$th_deleted' => [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('Permanent deletion')],
164 '$th_users' => $th_users,
165 '$order_users' => $order,
166 '$order_direction_users' => $order_direction,
168 '$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?'),
169 '$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?'),
171 '$form_security_token' => self::getFormSecurityToken('admin_users'),
174 '$baseurl' => DI::baseUrl()->get(true),
175 '$query_string' => DI::args()->getQueryString(),
179 '$pager' => $pager->renderFull($count),