]> git.mxchange.org Git - friendica.git/blob - src/Module/Admin/Users/Index.php
Merge pull request #10683 from annando/proxify
[friendica.git] / src / Module / Admin / Users / Index.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, 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 Index extends BaseUsers
32 {
33         public static function post(array $parameters = [])
34         {
35                 self::checkAdminAccess();
36
37                 self::checkFormSecurityTokenRedirectOnError('admin/users', 'admin_users');
38
39                 $users = $_POST['user'] ?? [];
40
41                 if (!empty($_POST['page_users_block'])) {
42                         foreach ($users as $uid) {
43                                 User::block($uid);
44                         }
45                         info(DI::l10n()->tt('%s user blocked', '%s users blocked', count($users)));
46                 }
47
48                 if (!empty($_POST['page_users_unblock'])) {
49                         foreach ($users as $uid) {
50                                 User::block($uid, false);
51                         }
52                         info(DI::l10n()->tt('%s user unblocked', '%s users unblocked', count($users)));
53                 }
54
55                 if (!empty($_POST['page_users_delete'])) {
56                         foreach ($users as $uid) {
57                                 if (local_user() != $uid) {
58                                         User::remove($uid);
59                                 } else {
60                                         notice(DI::l10n()->t('You can\'t remove yourself'));
61                                 }
62                         }
63
64                         info(DI::l10n()->tt('%s user deleted', '%s users deleted', count($users)));
65                 }
66
67                 DI::baseUrl()->redirect(DI::args()->getQueryString());
68         }
69
70         public static function content(array $parameters = [])
71         {
72                 parent::content($parameters);
73
74                 $action = $parameters['action'] ?? '';
75                 $uid = $parameters['uid'] ?? 0;
76
77                 if ($uid) {
78                         $user = User::getById($uid, ['username', 'blocked']);
79                         if (!DBA::isResult($user)) {
80                                 notice(DI::l10n()->t('User not found'));
81                                 DI::baseUrl()->redirect('admin/users');
82                                 return ''; // NOTREACHED
83                         }
84                 }
85
86                 switch ($action) {
87                         case 'delete':
88                                 if (local_user() != $uid) {
89                                         self::checkFormSecurityTokenRedirectOnError(DI::baseUrl()->get(true), 'admin_users', 't');
90                                         // delete user
91                                         User::remove($uid);
92
93                                         notice(DI::l10n()->t('User "%s" deleted', $user['username']));
94                                 } else {
95                                         notice(DI::l10n()->t('You can\'t remove yourself'));
96                                 }
97
98                                 DI::baseUrl()->redirect('admin/users');
99                                 break;
100                         case 'block':
101                                 self::checkFormSecurityTokenRedirectOnError('admin/users', 'admin_users', 't');
102                                 User::block($uid);
103                                 notice(DI::l10n()->t('User "%s" blocked', $user['username']));
104                                 DI::baseUrl()->redirect('admin/users');
105                                 break;
106                         case 'unblock':
107                                 self::checkFormSecurityTokenRedirectOnError('admin/users', 'admin_users', 't');
108                                 User::block($uid, false);
109                                 notice(DI::l10n()->t('User "%s" unblocked', $user['username']));
110                                 DI::baseUrl()->redirect('admin/users');
111                                 break;
112                 }
113                 $pager = new Pager(DI::l10n(), DI::args()->getQueryString(), 100);
114
115                 $valid_orders = [
116                         'name',
117                         'email',
118                         'register_date',
119                         'login_date',
120                         'last-item',
121                         'page-flags'
122                 ];
123
124                 $order = 'name';
125                 $order_direction = '+';
126                 if (!empty($_GET['o'])) {
127                         $new_order = $_GET['o'];
128                         if ($new_order[0] === '-') {
129                                 $order_direction = '-';
130                                 $new_order = substr($new_order, 1);
131                         }
132
133                         if (in_array($new_order, $valid_orders)) {
134                                 $order = $new_order;
135                         }
136                 }
137
138                 $users = User::getList($pager->getStart(), $pager->getItemsPerPage(), 'all', $order, ($order_direction == '-'));
139
140                 $users = array_map(self::setupUserCallback(), $users);
141
142                 $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);
143
144                 $count = DBA::count('user');
145
146                 $t = Renderer::getMarkupTemplate('admin/users/index.tpl');
147                 return self::getTabsHTML('all') .       Renderer::replaceMacros($t, [
148                         // strings //
149                         '$title' => DI::l10n()->t('Administration'),
150                         '$page' => DI::l10n()->t('Users'),
151                         '$select_all' => DI::l10n()->t('select all'),
152                         '$h_deleted' => DI::l10n()->t('User waiting for permanent deletion'),
153                         '$delete' => DI::l10n()->t('Delete'),
154                         '$block' => DI::l10n()->t('Block'),
155                         '$blocked' => DI::l10n()->t('User blocked'),
156                         '$unblock' => DI::l10n()->t('Unblock'),
157                         '$siteadmin' => DI::l10n()->t('Site admin'),
158                         '$accountexpired' => DI::l10n()->t('Account expired'),
159
160                         '$h_users' => DI::l10n()->t('Users'),
161                         '$h_newuser' => DI::l10n()->t('Create a new user'),
162                         '$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')],
163                         '$th_users' => $th_users,
164                         '$order_users' => $order,
165                         '$order_direction_users' => $order_direction,
166
167                         '$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?'),
168                         '$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?'),
169
170                         '$form_security_token' => self::getFormSecurityToken('admin_users'),
171
172                         // values //
173                         '$baseurl' => DI::baseUrl()->get(true),
174                         '$query_string' => DI::args()->getQueryString(),
175
176                         '$users' => $users,
177                         '$count' => $count,
178                         '$pager' => $pager->renderFull($count),
179                 ]);
180         }
181 }