3 namespace Friendica\Module\Admin;
5 use Friendica\Content\Pager;
6 use Friendica\Core\Renderer;
7 use Friendica\Database\DBA;
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;
15 class Users extends BaseAdmin
17 public static function post(array $parameters = [])
19 parent::post($parameters);
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');
28 parent::checkFormSecurityTokenRedirectOnError('/admin/users', 'admin_users');
30 if ($nu_name !== '' && $nu_email !== '' && $nu_nickname !== '') {
32 $result = User::create([
33 'username' => $nu_name,
35 'nickname' => $nu_nickname,
37 'language' => $nu_language
39 } catch (\Exception $ex) {
40 notice($ex->getMessage());
44 $user = $result['user'];
45 $preamble = Strings::deindent(DI::l10n()->t('
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:
55 You may change your password from your account "Settings" page after logging
58 Please take a few moments to review the other account settings on that page.
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.
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
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.
72 If you ever want to delete your account, you can do so at %1$s/removeme
74 Thank you and welcome to %4$s.'));
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'));
79 $email = DI::emailer()
81 ->withMessage(DI::l10n()->t('Registration details for %s', DI::config()->get('config', 'sitename')), $preamble, $body)
83 ->withRecipient($user['email'])
85 return DI::emailer()->send($email);
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)));
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)));
100 if (!empty($_POST['page_users_delete'])) {
101 foreach ($users as $uid) {
102 if (local_user() != $uid) {
105 notice(DI::l10n()->t('You can\'t remove yourself'));
109 notice(DI::l10n()->tt('%s user deleted', '%s users deleted', count($users)));
112 if (!empty($_POST['page_users_approve'])) {
113 require_once 'mod/regmod.php';
114 foreach ($pending as $hash) {
119 if (!empty($_POST['page_users_deny'])) {
120 require_once 'mod/regmod.php';
121 foreach ($pending as $hash) {
126 DI::baseUrl()->redirect('admin/users');
129 public static function content(array $parameters = [])
131 parent::content($parameters);
136 // @TODO: Replace with parameter from router
137 $action = $a->argv[2];
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
148 if (local_user() != $uid) {
149 parent::checkFormSecurityTokenRedirectOnError('/admin/users', 'admin_users', 't');
153 notice(DI::l10n()->t('User "%s" deleted', $user['username']));
155 notice(DI::l10n()->t('You can\'t remove yourself'));
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']));
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']));
172 DI::baseUrl()->redirect('admin/users');
176 $pending = Register::getPending();
178 $pager = new Pager(DI::args()->getQueryString(), 100);
180 // @TODO Move below block to Model\User::getUsers($start, $count, $order = 'contact.name', $order_direction = '+')
184 'user.register_date',
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);
199 if (in_array($new_order, $valid_orders)) {
203 $sql_order = '`' . str_replace('.', '`.`', $order) . '`';
204 $sql_order_direction = ($order_direction === '+') ? 'ASC' : 'DESC';
206 $usersStmt = DBA::p("SELECT `user`.*, `contact`.`name`, `contact`.`url`, `contact`.`micro`, `user`.`account_expired`, `contact`.`last-item` AS `lastitem_date`
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()
212 $users = DBA::toArray($usersStmt);
214 $adminlist = explode(',', str_replace(' ', '', DI::config()->get('config', 'admin_email')));
215 $_setup_users = function ($e) use ($adminlist) {
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')
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'),
231 $e['page_flags_raw'] = $e['page-flags'];
232 $e['page-flags'] = $page_types[$e['page-flags']];
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']] : '';
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);
247 $tmp_users = array_map($_setup_users, $users);
249 // Get rid of dashes in key names, Smarty3 can't handle them
250 // and extracting deleted users
254 foreach ($tmp_users as $user) {
255 foreach ($user as $k => $v) {
256 $newkey = str_replace('-', '_', $k);
260 if ($user['deleted']) {
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);
269 $t = Renderer::getMarkupTemplate('admin/users.tpl');
270 $o = Renderer::replaceMacros($t, [
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'),
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,
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?'),
300 '$form_security_token' => parent::getFormSecurityToken('admin_users'),
303 '$baseurl' => DI::baseUrl()->get(true),
305 '$pending' => $pending,
306 'deleted' => $deleted,
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'],
313 $o .= $pager->renderFull(DBA::count('user'));