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