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