3 namespace Friendica\Module\Admin;
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;
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;
17 class Users extends BaseAdminModule
19 public static function post(array $parameters = [])
21 parent::post($parameters);
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');
30 parent::checkFormSecurityTokenRedirectOnError('/admin/users', 'admin_users');
32 if ($nu_name !== '' && $nu_email !== '' && $nu_nickname !== '') {
34 $result = User::create([
35 'username' => $nu_name,
37 'nickname' => $nu_nickname,
39 'language' => $nu_language
41 } catch (\Exception $ex) {
42 notice($ex->getMessage());
46 $user = $result['user'];
47 $preamble = Strings::deindent(L10n::t('
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:
57 You may change your password from your account "Settings" page after logging
60 Please take a few moments to review the other account settings on that page.
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.
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
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.
74 If you ever want to delete your account, you can do so at %1$s/removeme
76 Thank you and welcome to %4$s.'));
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'));
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,
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)));
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)));
104 if (!empty($_POST['page_users_delete'])) {
105 foreach ($users as $uid) {
106 if (local_user() != $uid) {
109 notice(L10n::t('You can\'t remove yourself'));
113 notice(L10n::tt('%s user deleted', '%s users deleted', count($users)));
116 if (!empty($_POST['page_users_approve'])) {
117 require_once 'mod/regmod.php';
118 foreach ($pending as $hash) {
123 if (!empty($_POST['page_users_deny'])) {
124 require_once 'mod/regmod.php';
125 foreach ($pending as $hash) {
130 DI::baseUrl()->redirect('admin/users');
133 public static function content(array $parameters = [])
135 parent::content($parameters);
140 // @TODO: Replace with parameter from router
141 $action = $a->argv[2];
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
152 if (local_user() != $uid) {
153 parent::checkFormSecurityTokenRedirectOnError('/admin/users', 'admin_users', 't');
157 notice(L10n::t('User "%s" deleted', $user['username']));
159 notice(L10n::t('You can\'t remove yourself'));
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']));
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']));
176 DI::baseUrl()->redirect('admin/users');
180 $pending = Register::getPending();
182 $pager = new Pager(DI::args()->getQueryString(), 100);
184 // @TODO Move below block to Model\User::getUsers($start, $count, $order = 'contact.name', $order_direction = '+')
188 'user.register_date',
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);
203 if (in_array($new_order, $valid_orders)) {
207 $sql_order = '`' . str_replace('.', '`.`', $order) . '`';
208 $sql_order_direction = ($order_direction === '+') ? 'ASC' : 'DESC';
210 $usersStmt = DBA::p("SELECT `user`.*, `contact`.`name`, `contact`.`url`, `contact`.`micro`, `user`.`account_expired`, `contact`.`last-item` AS `lastitem_date`
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()
216 $users = DBA::toArray($usersStmt);
218 $adminlist = explode(',', str_replace(' ', '', Config::get('config', 'admin_email')));
219 $_setup_users = function ($e) use ($adminlist) {
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')
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'),
235 $e['page_flags_raw'] = $e['page-flags'];
236 $e['page-flags'] = $page_types[$e['page-flags']];
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']] : '';
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);
251 $tmp_users = array_map($_setup_users, $users);
253 // Get rid of dashes in key names, Smarty3 can't handle them
254 // and extracting deleted users
258 foreach ($tmp_users as $user) {
259 foreach ($user as $k => $v) {
260 $newkey = str_replace('-', '_', $k);
264 if ($user['deleted']) {
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);
273 $t = Renderer::getMarkupTemplate('admin/users.tpl');
274 $o = Renderer::replaceMacros($t, [
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'),
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,
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?'),
304 '$form_security_token' => parent::getFormSecurityToken('admin_users'),
307 '$baseurl' => DI::baseUrl()->get(true),
309 '$pending' => $pending,
310 'deleted' => $deleted,
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'],
317 $o .= $pager->renderFull(DBA::count('user'));