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'));
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,
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)));
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)));
102 if (!empty($_POST['page_users_delete'])) {
103 foreach ($users as $uid) {
104 if (local_user() != $uid) {
107 notice(DI::l10n()->t('You can\'t remove yourself'));
111 notice(DI::l10n()->tt('%s user deleted', '%s users deleted', count($users)));
114 if (!empty($_POST['page_users_approve'])) {
115 require_once 'mod/regmod.php';
116 foreach ($pending as $hash) {
121 if (!empty($_POST['page_users_deny'])) {
122 require_once 'mod/regmod.php';
123 foreach ($pending as $hash) {
128 DI::baseUrl()->redirect('admin/users');
131 public static function content(array $parameters = [])
133 parent::content($parameters);
138 // @TODO: Replace with parameter from router
139 $action = $a->argv[2];
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
150 if (local_user() != $uid) {
151 parent::checkFormSecurityTokenRedirectOnError('/admin/users', 'admin_users', 't');
155 notice(DI::l10n()->t('User "%s" deleted', $user['username']));
157 notice(DI::l10n()->t('You can\'t remove yourself'));
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']));
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']));
174 DI::baseUrl()->redirect('admin/users');
178 $pending = Register::getPending();
180 $pager = new Pager(DI::args()->getQueryString(), 100);
182 // @TODO Move below block to Model\User::getUsers($start, $count, $order = 'contact.name', $order_direction = '+')
186 'user.register_date',
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);
201 if (in_array($new_order, $valid_orders)) {
205 $sql_order = '`' . str_replace('.', '`.`', $order) . '`';
206 $sql_order_direction = ($order_direction === '+') ? 'ASC' : 'DESC';
208 $usersStmt = DBA::p("SELECT `user`.*, `contact`.`name`, `contact`.`url`, `contact`.`micro`, `user`.`account_expired`, `contact`.`last-item` AS `lastitem_date`
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()
214 $users = DBA::toArray($usersStmt);
216 $adminlist = explode(',', str_replace(' ', '', DI::config()->get('config', 'admin_email')));
217 $_setup_users = function ($e) use ($adminlist) {
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')
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'),
233 $e['page_flags_raw'] = $e['page-flags'];
234 $e['page-flags'] = $page_types[$e['page-flags']];
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']] : '';
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);
249 $tmp_users = array_map($_setup_users, $users);
251 // Get rid of dashes in key names, Smarty3 can't handle them
252 // and extracting deleted users
256 foreach ($tmp_users as $user) {
257 foreach ($user as $k => $v) {
258 $newkey = str_replace('-', '_', $k);
262 if ($user['deleted']) {
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);
271 $t = Renderer::getMarkupTemplate('admin/users.tpl');
272 $o = Renderer::replaceMacros($t, [
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'),
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,
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?'),
302 '$form_security_token' => parent::getFormSecurityToken('admin_users'),
305 '$baseurl' => DI::baseUrl()->get(true),
307 '$pending' => $pending,
308 'deleted' => $deleted,
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'],
315 $o .= $pager->renderFull(DBA::count('user'));