]> git.mxchange.org Git - friendica.git/blob - src/Module/Admin/Users.php
Merge pull request #8236 from annando/a11y-1
[friendica.git] / src / Module / Admin / Users.php
1 <?php
2
3 namespace Friendica\Module\Admin;
4
5 use Friendica\Content\Pager;
6 use Friendica\Core\Renderer;
7 use Friendica\Database\DBA;
8 use Friendica\DI;
9 use Friendica\Model\Register;
10 use Friendica\Model\User;
11 use Friendica\Module\BaseAdmin;
12 use Friendica\Util\Strings;
13 use Friendica\Util\Temporal;
14
15 class Users extends BaseAdmin
16 {
17         public static function post(array $parameters = [])
18         {
19                 parent::post($parameters);
20
21                 $pending     = $_POST['pending']           ?? [];
22                 $users       = $_POST['user']              ?? [];
23                 $nu_name     = $_POST['new_user_name']     ?? '';
24                 $nu_nickname = $_POST['new_user_nickname'] ?? '';
25                 $nu_email    = $_POST['new_user_email']    ?? '';
26                 $nu_language = DI::config()->get('system', 'language');
27
28                 parent::checkFormSecurityTokenRedirectOnError('/admin/users', 'admin_users');
29
30                 if ($nu_name !== '' && $nu_email !== '' && $nu_nickname !== '') {
31                         try {
32                                 $result = User::create([
33                                         'username' => $nu_name,
34                                         'email' => $nu_email,
35                                         'nickname' => $nu_nickname,
36                                         'verified' => 1,
37                                         'language' => $nu_language
38                                 ]);
39                         } catch (\Exception $ex) {
40                                 notice($ex->getMessage());
41                                 return;
42                         }
43
44                         $user = $result['user'];
45                         $preamble = Strings::deindent(DI::l10n()->t('
46                         Dear %1$s,
47                                 the administrator of %2$s has set up an account for you.'));
48                         $body = Strings::deindent(DI::l10n()->t('
49                         The login details are as follows:
50
51                         Site Location:  %1$s
52                         Login Name:             %2$s
53                         Password:               %3$s
54
55                         You may change your password from your account "Settings" page after logging
56                         in.
57
58                         Please take a few moments to review the other account settings on that page.
59
60                         You may also wish to add some basic information to your default profile
61                         (on the "Profiles" page) so that other people can easily find you.
62
63                         We recommend setting your full name, adding a profile photo,
64                         adding some profile "keywords" (very useful in making new friends) - and
65                         perhaps what country you live in; if you do not wish to be more specific
66                         than that.
67
68                         We fully respect your right to privacy, and none of these items are necessary.
69                         If you are new and do not know anybody here, they may help
70                         you to make some new and interesting friends.
71
72                         If you ever want to delete your account, you can do so at %1$s/removeme
73
74                         Thank you and welcome to %4$s.'));
75
76                         $preamble = sprintf($preamble, $user['username'], DI::config()->get('config', 'sitename'));
77                         $body = sprintf($body, DI::baseUrl()->get(), $user['nickname'], $result['password'], DI::config()->get('config', 'sitename'));
78
79                         $email = DI::emailer()
80                                 ->newSystemMail()
81                                 ->withMessage(DI::l10n()->t('Registration details for %s', DI::config()->get('config', 'sitename')), $preamble, $body)
82                                 ->forUser($user)
83                                 ->withRecipient($user['email'])
84                                 ->build();
85                         return DI::emailer()->send($email);
86                 }
87
88                 if (!empty($_POST['page_users_block'])) {
89                         // @TODO Move this to Model\User:block($users);
90                         DBA::update('user', ['blocked' => 1], ['uid' => $users]);
91                         notice(DI::l10n()->tt('%s user blocked', '%s users blocked', count($users)));
92                 }
93
94                 if (!empty($_POST['page_users_unblock'])) {
95                         // @TODO Move this to Model\User:unblock($users);
96                         DBA::update('user', ['blocked' => 0], ['uid' => $users]);
97                         notice(DI::l10n()->tt('%s user unblocked', '%s users unblocked', count($users)));
98                 }
99
100                 if (!empty($_POST['page_users_delete'])) {
101                         foreach ($users as $uid) {
102                                 if (local_user() != $uid) {
103                                         User::remove($uid);
104                                 } else {
105                                         notice(DI::l10n()->t('You can\'t remove yourself'));
106                                 }
107                         }
108
109                         notice(DI::l10n()->tt('%s user deleted', '%s users deleted', count($users)));
110                 }
111
112                 if (!empty($_POST['page_users_approve'])) {
113                         require_once 'mod/regmod.php';
114                         foreach ($pending as $hash) {
115                                 user_allow($hash);
116                         }
117                 }
118
119                 if (!empty($_POST['page_users_deny'])) {
120                         require_once 'mod/regmod.php';
121                         foreach ($pending as $hash) {
122                                 user_deny($hash);
123                         }
124                 }
125
126                 DI::baseUrl()->redirect('admin/users');
127         }
128
129         public static function content(array $parameters = [])
130         {
131                 parent::content($parameters);
132
133                 $a = DI::app();
134
135                 if ($a->argc > 3) {
136                         // @TODO: Replace with parameter from router
137                         $action = $a->argv[2];
138                         $uid = $a->argv[3];
139                         $user = User::getById($uid, ['username', 'blocked']);
140                         if (!DBA::isResult($user)) {
141                                 notice('User not found' . EOL);
142                                 DI::baseUrl()->redirect('admin/users');
143                                 return ''; // NOTREACHED
144                         }
145
146                         switch ($action) {
147                                 case 'delete':
148                                         if (local_user() != $uid) {
149                                                 parent::checkFormSecurityTokenRedirectOnError('/admin/users', 'admin_users', 't');
150                                                 // delete user
151                                                 User::remove($uid);
152
153                                                 notice(DI::l10n()->t('User "%s" deleted', $user['username']));
154                                         } else {
155                                                 notice(DI::l10n()->t('You can\'t remove yourself'));
156                                         }
157                                         break;
158                                 case 'block':
159                                         parent::checkFormSecurityTokenRedirectOnError('/admin/users', 'admin_users', 't');
160                                         // @TODO Move this to Model\User:block([$uid]);
161                                         DBA::update('user', ['blocked' => 1], ['uid' => $uid]);
162                                         notice(DI::l10n()->t('User "%s" blocked', $user['username']));
163                                         break;
164                                 case 'unblock':
165                                         parent::checkFormSecurityTokenRedirectOnError('/admin/users', 'admin_users', 't');
166                                         // @TODO Move this to Model\User:unblock([$uid]);
167                                         DBA::update('user', ['blocked' => 0], ['uid' => $uid]);
168                                         notice(DI::l10n()->t('User "%s" unblocked', $user['username']));
169                                         break;
170                         }
171
172                         DI::baseUrl()->redirect('admin/users');
173                 }
174
175                 /* get pending */
176                 $pending = Register::getPending();
177
178                 $pager = new Pager(DI::args()->getQueryString(), 100);
179
180                 // @TODO Move below block to Model\User::getUsers($start, $count, $order = 'contact.name', $order_direction = '+')
181                 $valid_orders = [
182                         'contact.name',
183                         'user.email',
184                         'user.register_date',
185                         'user.login_date',
186                         'lastitem_date',
187                         'user.page-flags'
188                 ];
189
190                 $order = 'contact.name';
191                 $order_direction = '+';
192                 if (!empty($_GET['o'])) {
193                         $new_order = $_GET['o'];
194                         if ($new_order[0] === '-') {
195                                 $order_direction = '-';
196                                 $new_order = substr($new_order, 1);
197                         }
198
199                         if (in_array($new_order, $valid_orders)) {
200                                 $order = $new_order;
201                         }
202                 }
203                 $sql_order = '`' . str_replace('.', '`.`', $order) . '`';
204                 $sql_order_direction = ($order_direction === '+') ? 'ASC' : 'DESC';
205
206                 $usersStmt = DBA::p("SELECT `user`.*, `contact`.`name`, `contact`.`url`, `contact`.`micro`, `user`.`account_expired`, `contact`.`last-item` AS `lastitem_date`
207                                 FROM `user`
208                                 INNER JOIN `contact` ON `contact`.`uid` = `user`.`uid` AND `contact`.`self`
209                                 WHERE `user`.`verified`
210                                 ORDER BY $sql_order $sql_order_direction LIMIT ?, ?", $pager->getStart(), $pager->getItemsPerPage()
211                 );
212                 $users = DBA::toArray($usersStmt);
213
214                 $adminlist = explode(',', str_replace(' ', '', DI::config()->get('config', 'admin_email')));
215                 $_setup_users = function ($e) use ($adminlist) {
216                         $page_types = [
217                                 User::PAGE_FLAGS_NORMAL    => DI::l10n()->t('Normal Account Page'),
218                                 User::PAGE_FLAGS_SOAPBOX   => DI::l10n()->t('Soapbox Page'),
219                                 User::PAGE_FLAGS_COMMUNITY => DI::l10n()->t('Public Forum'),
220                                 User::PAGE_FLAGS_FREELOVE  => DI::l10n()->t('Automatic Friend Page'),
221                                 User::PAGE_FLAGS_PRVGROUP  => DI::l10n()->t('Private Forum')
222                         ];
223                         $account_types = [
224                                 User::ACCOUNT_TYPE_PERSON       => DI::l10n()->t('Personal Page'),
225                                 User::ACCOUNT_TYPE_ORGANISATION => DI::l10n()->t('Organisation Page'),
226                                 User::ACCOUNT_TYPE_NEWS         => DI::l10n()->t('News Page'),
227                                 User::ACCOUNT_TYPE_COMMUNITY    => DI::l10n()->t('Community Forum'),
228                                 User::ACCOUNT_TYPE_RELAY        => DI::l10n()->t('Relay'),
229                         ];
230
231                         $e['page_flags_raw'] = $e['page-flags'];
232                         $e['page-flags'] = $page_types[$e['page-flags']];
233
234                         $e['account_type_raw'] = ($e['page_flags_raw'] == 0) ? $e['account-type'] : -1;
235                         $e['account-type'] = ($e['page_flags_raw'] == 0) ? $account_types[$e['account-type']] : '';
236
237                         $e['register_date'] = Temporal::getRelativeDate($e['register_date']);
238                         $e['login_date'] = Temporal::getRelativeDate($e['login_date']);
239                         $e['lastitem_date'] = Temporal::getRelativeDate($e['lastitem_date']);
240                         $e['is_admin'] = in_array($e['email'], $adminlist);
241                         $e['is_deletable'] = (intval($e['uid']) != local_user());
242                         $e['deleted'] = ($e['account_removed'] ? Temporal::getRelativeDate($e['account_expires_on']) : False);
243
244                         return $e;
245                 };
246
247                 $tmp_users = array_map($_setup_users, $users);
248
249                 // Get rid of dashes in key names, Smarty3 can't handle them
250                 // and extracting deleted users
251
252                 $deleted = [];
253                 $users = [];
254                 foreach ($tmp_users as $user) {
255                         foreach ($user as $k => $v) {
256                                 $newkey = str_replace('-', '_', $k);
257                                 $user[$newkey] = $v;
258                         }
259
260                         if ($user['deleted']) {
261                                 $deleted[] = $user;
262                         } else {
263                                 $users[] = $user;
264                         }
265                 }
266
267                 $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 item'), DI::l10n()->t('Type')], $valid_orders);
268
269                 $t = Renderer::getMarkupTemplate('admin/users.tpl');
270                 $o = Renderer::replaceMacros($t, [
271                         // strings //
272                         '$title' => DI::l10n()->t('Administration'),
273                         '$page' => DI::l10n()->t('Users'),
274                         '$submit' => DI::l10n()->t('Add User'),
275                         '$select_all' => DI::l10n()->t('select all'),
276                         '$h_pending' => DI::l10n()->t('User registrations waiting for confirm'),
277                         '$h_deleted' => DI::l10n()->t('User waiting for permanent deletion'),
278                         '$th_pending' => [DI::l10n()->t('Request date'), DI::l10n()->t('Name'), DI::l10n()->t('Email')],
279                         '$no_pending' => DI::l10n()->t('No registrations.'),
280                         '$pendingnotetext' => DI::l10n()->t('Note from the user'),
281                         '$approve' => DI::l10n()->t('Approve'),
282                         '$deny' => DI::l10n()->t('Deny'),
283                         '$delete' => DI::l10n()->t('Delete'),
284                         '$block' => DI::l10n()->t('Block'),
285                         '$blocked' => DI::l10n()->t('User blocked'),
286                         '$unblock' => DI::l10n()->t('Unblock'),
287                         '$siteadmin' => DI::l10n()->t('Site admin'),
288                         '$accountexpired' => DI::l10n()->t('Account expired'),
289
290                         '$h_users' => DI::l10n()->t('Users'),
291                         '$h_newuser' => DI::l10n()->t('New User'),
292                         '$th_deleted' => [DI::l10n()->t('Name'), DI::l10n()->t('Email'), DI::l10n()->t('Register date'), DI::l10n()->t('Last login'), DI::l10n()->t('Last item'), DI::l10n()->t('Permanent deletion')],
293                         '$th_users' => $th_users,
294                         '$order_users' => $order,
295                         '$order_direction_users' => $order_direction,
296
297                         '$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?'),
298                         '$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?'),
299
300                         '$form_security_token' => parent::getFormSecurityToken('admin_users'),
301
302                         // values //
303                         '$baseurl' => DI::baseUrl()->get(true),
304
305                         '$pending' => $pending,
306                         'deleted' => $deleted,
307                         '$users' => $users,
308                         '$newusername' => ['new_user_name', DI::l10n()->t('Name'), '', DI::l10n()->t('Name of the new user.')],
309                         '$newusernickname' => ['new_user_nickname', DI::l10n()->t('Nickname'), '', DI::l10n()->t('Nickname of the new user.')],
310                         '$newuseremail' => ['new_user_email', DI::l10n()->t('Email'), '', DI::l10n()->t('Email address of the new user.'), '', '', 'email'],
311                 ]);
312
313                 $o .= $pager->renderFull(DBA::count('user'));
314
315                 return $o;
316         }
317 }