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