]> git.mxchange.org Git - friendica.git/blob - src/Module/Admin/Users.php
Move "User::allow()" to own method and update usages
[friendica.git] / src / Module / Admin / Users.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
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;
23
24 use Friendica\Content\Pager;
25 use Friendica\Core\Renderer;
26 use Friendica\Database\DBA;
27 use Friendica\DI;
28 use Friendica\Model\Register;
29 use Friendica\Model\User;
30 use Friendica\Module\BaseAdmin;
31 use Friendica\Util\Temporal;
32
33 class Users extends BaseAdmin
34 {
35         public static function post(array $parameters = [])
36         {
37                 parent::post($parameters);
38
39                 $pending     = $_POST['pending']           ?? [];
40                 $users       = $_POST['user']              ?? [];
41                 $nu_name     = $_POST['new_user_name']     ?? '';
42                 $nu_nickname = $_POST['new_user_nickname'] ?? '';
43                 $nu_email    = $_POST['new_user_email']    ?? '';
44                 $nu_language = DI::config()->get('system', 'language');
45
46                 parent::checkFormSecurityTokenRedirectOnError('/admin/users', 'admin_users');
47
48                 if ($nu_name !== '' && $nu_email !== '' && $nu_nickname !== '') {
49                         try {
50                                 DI::userService()->createMinimal($nu_name, $nu_email, $nu_nickname, $nu_language);
51                         } catch (\Exception $ex) {
52                                 notice($ex->getMessage());
53                                 return;
54                         }
55                 }
56
57                 if (!empty($_POST['page_users_block'])) {
58                         if (User::block($users)) {
59                                 notice(DI::l10n()->tt('%s user blocked', '%s users blocked', count($users)));
60                         }
61                 }
62
63                 if (!empty($_POST['page_users_unblock'])) {
64                         if (User::block($users, false)) {
65                                 notice(DI::l10n()->tt('%s user unblocked', '%s users unblocked', count($users)));
66                         }
67                 }
68
69                 if (!empty($_POST['page_users_delete'])) {
70                         foreach ($users as $uid) {
71                                 if (local_user() != $uid) {
72                                         User::remove($uid);
73                                 } else {
74                                         notice(DI::l10n()->t('You can\'t remove yourself'));
75                                 }
76                         }
77
78                         notice(DI::l10n()->tt('%s user deleted', '%s users deleted', count($users)));
79                 }
80
81                 if (!empty($_POST['page_users_approve'])) {
82                         require_once 'mod/regmod.php';
83                         foreach ($pending as $hash) {
84                                 User::allow($hash);
85                         }
86                 }
87
88                 if (!empty($_POST['page_users_deny'])) {
89                         require_once 'mod/regmod.php';
90                         foreach ($pending as $hash) {
91                                 user_deny($hash);
92                         }
93                 }
94
95                 DI::baseUrl()->redirect('admin/users');
96         }
97
98         public static function content(array $parameters = [])
99         {
100                 parent::content($parameters);
101
102                 $a = DI::app();
103
104                 if ($a->argc > 3) {
105                         // @TODO: Replace with parameter from router
106                         $action = $a->argv[2];
107                         $uid = $a->argv[3];
108                         $user = User::getById($uid, ['username', 'blocked']);
109                         if (!DBA::isResult($user)) {
110                                 notice('User not found' . EOL);
111                                 DI::baseUrl()->redirect('admin/users');
112                                 return ''; // NOTREACHED
113                         }
114
115                         switch ($action) {
116                                 case 'delete':
117                                         if (local_user() != $uid) {
118                                                 parent::checkFormSecurityTokenRedirectOnError('/admin/users', 'admin_users', 't');
119                                                 // delete user
120                                                 User::remove($uid);
121
122                                                 notice(DI::l10n()->t('User "%s" deleted', $user['username']));
123                                         } else {
124                                                 notice(DI::l10n()->t('You can\'t remove yourself'));
125                                         }
126                                         break;
127                                 case 'block':
128                                         parent::checkFormSecurityTokenRedirectOnError('/admin/users', 'admin_users', 't');
129                                         // @TODO Move this to Model\User:block([$uid]);
130                                         DBA::update('user', ['blocked' => 1], ['uid' => $uid]);
131                                         notice(DI::l10n()->t('User "%s" blocked', $user['username']));
132                                         break;
133                                 case 'unblock':
134                                         parent::checkFormSecurityTokenRedirectOnError('/admin/users', 'admin_users', 't');
135                                         // @TODO Move this to Model\User:unblock([$uid]);
136                                         DBA::update('user', ['blocked' => 0], ['uid' => $uid]);
137                                         notice(DI::l10n()->t('User "%s" unblocked', $user['username']));
138                                         break;
139                         }
140
141                         DI::baseUrl()->redirect('admin/users');
142                 }
143
144                 /* get pending */
145                 $pending = Register::getPending();
146
147                 $pager = new Pager(DI::l10n(), DI::args()->getQueryString(), 100);
148
149                 // @TODO Move below block to Model\User::getUsers($start, $count, $order = 'contact.name', $order_direction = '+')
150                 $valid_orders = [
151                         'contact.name',
152                         'user.email',
153                         'user.register_date',
154                         'user.login_date',
155                         'lastitem_date',
156                         'user.page-flags'
157                 ];
158
159                 $order = 'contact.name';
160                 $order_direction = '+';
161                 if (!empty($_GET['o'])) {
162                         $new_order = $_GET['o'];
163                         if ($new_order[0] === '-') {
164                                 $order_direction = '-';
165                                 $new_order = substr($new_order, 1);
166                         }
167
168                         if (in_array($new_order, $valid_orders)) {
169                                 $order = $new_order;
170                         }
171                 }
172                 $sql_order = '`' . str_replace('.', '`.`', $order) . '`';
173                 $sql_order_direction = ($order_direction === '+') ? 'ASC' : 'DESC';
174
175                 $usersStmt = DBA::p("SELECT `user`.*, `contact`.`name`, `contact`.`url`, `contact`.`micro`, `user`.`account_expired`, `contact`.`last-item` AS `lastitem_date`
176                                 FROM `user`
177                                 INNER JOIN `contact` ON `contact`.`uid` = `user`.`uid` AND `contact`.`self`
178                                 WHERE `user`.`verified`
179                                 ORDER BY $sql_order $sql_order_direction LIMIT ?, ?", $pager->getStart(), $pager->getItemsPerPage()
180                 );
181                 $users = DBA::toArray($usersStmt);
182
183                 $adminlist = explode(',', str_replace(' ', '', DI::config()->get('config', 'admin_email')));
184                 $_setup_users = function ($e) use ($adminlist) {
185                         $page_types = [
186                                 User::PAGE_FLAGS_NORMAL    => DI::l10n()->t('Normal Account Page'),
187                                 User::PAGE_FLAGS_SOAPBOX   => DI::l10n()->t('Soapbox Page'),
188                                 User::PAGE_FLAGS_COMMUNITY => DI::l10n()->t('Public Forum'),
189                                 User::PAGE_FLAGS_FREELOVE  => DI::l10n()->t('Automatic Friend Page'),
190                                 User::PAGE_FLAGS_PRVGROUP  => DI::l10n()->t('Private Forum')
191                         ];
192                         $account_types = [
193                                 User::ACCOUNT_TYPE_PERSON       => DI::l10n()->t('Personal Page'),
194                                 User::ACCOUNT_TYPE_ORGANISATION => DI::l10n()->t('Organisation Page'),
195                                 User::ACCOUNT_TYPE_NEWS         => DI::l10n()->t('News Page'),
196                                 User::ACCOUNT_TYPE_COMMUNITY    => DI::l10n()->t('Community Forum'),
197                                 User::ACCOUNT_TYPE_RELAY        => DI::l10n()->t('Relay'),
198                         ];
199
200                         $e['page_flags_raw'] = $e['page-flags'];
201                         $e['page-flags'] = $page_types[$e['page-flags']];
202
203                         $e['account_type_raw'] = ($e['page_flags_raw'] == 0) ? $e['account-type'] : -1;
204                         $e['account-type'] = ($e['page_flags_raw'] == 0) ? $account_types[$e['account-type']] : '';
205
206                         $e['register_date'] = Temporal::getRelativeDate($e['register_date']);
207                         $e['login_date'] = Temporal::getRelativeDate($e['login_date']);
208                         $e['lastitem_date'] = Temporal::getRelativeDate($e['lastitem_date']);
209                         $e['is_admin'] = in_array($e['email'], $adminlist);
210                         $e['is_deletable'] = (intval($e['uid']) != local_user());
211                         $e['deleted'] = ($e['account_removed'] ? Temporal::getRelativeDate($e['account_expires_on']) : False);
212
213                         return $e;
214                 };
215
216                 $tmp_users = array_map($_setup_users, $users);
217
218                 // Get rid of dashes in key names, Smarty3 can't handle them
219                 // and extracting deleted users
220
221                 $deleted = [];
222                 $users = [];
223                 foreach ($tmp_users as $user) {
224                         foreach ($user as $k => $v) {
225                                 $newkey = str_replace('-', '_', $k);
226                                 $user[$newkey] = $v;
227                         }
228
229                         if ($user['deleted']) {
230                                 $deleted[] = $user;
231                         } else {
232                                 $users[] = $user;
233                         }
234                 }
235
236                 $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);
237
238                 $t = Renderer::getMarkupTemplate('admin/users.tpl');
239                 $o = Renderer::replaceMacros($t, [
240                         // strings //
241                         '$title' => DI::l10n()->t('Administration'),
242                         '$page' => DI::l10n()->t('Users'),
243                         '$submit' => DI::l10n()->t('Add User'),
244                         '$select_all' => DI::l10n()->t('select all'),
245                         '$h_pending' => DI::l10n()->t('User registrations waiting for confirm'),
246                         '$h_deleted' => DI::l10n()->t('User waiting for permanent deletion'),
247                         '$th_pending' => [DI::l10n()->t('Request date'), DI::l10n()->t('Name'), DI::l10n()->t('Email')],
248                         '$no_pending' => DI::l10n()->t('No registrations.'),
249                         '$pendingnotetext' => DI::l10n()->t('Note from the user'),
250                         '$approve' => DI::l10n()->t('Approve'),
251                         '$deny' => DI::l10n()->t('Deny'),
252                         '$delete' => DI::l10n()->t('Delete'),
253                         '$block' => DI::l10n()->t('Block'),
254                         '$blocked' => DI::l10n()->t('User blocked'),
255                         '$unblock' => DI::l10n()->t('Unblock'),
256                         '$siteadmin' => DI::l10n()->t('Site admin'),
257                         '$accountexpired' => DI::l10n()->t('Account expired'),
258
259                         '$h_users' => DI::l10n()->t('Users'),
260                         '$h_newuser' => DI::l10n()->t('New User'),
261                         '$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')],
262                         '$th_users' => $th_users,
263                         '$order_users' => $order,
264                         '$order_direction_users' => $order_direction,
265
266                         '$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?'),
267                         '$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?'),
268
269                         '$form_security_token' => parent::getFormSecurityToken('admin_users'),
270
271                         // values //
272                         '$baseurl' => DI::baseUrl()->get(true),
273
274                         '$pending' => $pending,
275                         'deleted' => $deleted,
276                         '$users' => $users,
277                         '$newusername' => ['new_user_name', DI::l10n()->t('Name'), '', DI::l10n()->t('Name of the new user.')],
278                         '$newusernickname' => ['new_user_nickname', DI::l10n()->t('Nickname'), '', DI::l10n()->t('Nickname of the new user.')],
279                         '$newuseremail' => ['new_user_email', DI::l10n()->t('Email'), '', DI::l10n()->t('Email address of the new user.'), '', '', 'email'],
280                 ]);
281
282                 $o .= $pager->renderFull(DBA::count('user'));
283
284                 return $o;
285         }
286 }