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