3 * @copyright Copyright (C) 2010-2023, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Model;
24 use DivineOmega\DOFileCachePSR6\CacheItemPool;
25 use DivineOmega\PasswordExposed;
28 use Friendica\Content\Pager;
29 use Friendica\Core\Hook;
30 use Friendica\Core\L10n;
31 use Friendica\Core\Logger;
32 use Friendica\Core\Protocol;
33 use Friendica\Core\Search;
34 use Friendica\Core\System;
35 use Friendica\Core\Worker;
36 use Friendica\Database\DBA;
39 use Friendica\Network\HTTPClient\Client\HttpClientAccept;
40 use Friendica\Security\TwoFactor\Model\AppSpecificPassword;
41 use Friendica\Network\HTTPException;
42 use Friendica\Object\Image;
43 use Friendica\Protocol\Delivery;
44 use Friendica\Util\Crypto;
45 use Friendica\Util\DateTimeFormat;
46 use Friendica\Util\Images;
47 use Friendica\Util\Network;
48 use Friendica\Util\Proxy;
49 use Friendica\Util\Strings;
54 * This class handles User related functions
61 * PAGE_FLAGS_NORMAL is a typical personal profile account
62 * PAGE_FLAGS_SOAPBOX automatically approves all friend requests as Contact::SHARING, (readonly)
63 * PAGE_FLAGS_COMMUNITY automatically approves all friend requests as Contact::SHARING, but with
64 * write access to wall and comments (no email and not included in page owner's ACL lists)
65 * PAGE_FLAGS_FREELOVE automatically approves all friend requests as full friends (Contact::FRIEND).
69 const PAGE_FLAGS_NORMAL = 0;
70 const PAGE_FLAGS_SOAPBOX = 1;
71 const PAGE_FLAGS_COMMUNITY = 2;
72 const PAGE_FLAGS_FREELOVE = 3;
73 const PAGE_FLAGS_BLOG = 4;
74 const PAGE_FLAGS_PRVGROUP = 5;
82 * ACCOUNT_TYPE_PERSON - the account belongs to a person
83 * Associated page types: PAGE_FLAGS_NORMAL, PAGE_FLAGS_SOAPBOX, PAGE_FLAGS_FREELOVE
85 * ACCOUNT_TYPE_ORGANISATION - the account belongs to an organisation
86 * Associated page type: PAGE_FLAGS_SOAPBOX
88 * ACCOUNT_TYPE_NEWS - the account is a news reflector
89 * Associated page type: PAGE_FLAGS_SOAPBOX
91 * ACCOUNT_TYPE_COMMUNITY - the account is community group
92 * Associated page types: PAGE_COMMUNITY, PAGE_FLAGS_PRVGROUP
94 * ACCOUNT_TYPE_RELAY - the account is a relay
95 * This will only be assigned to contacts, not to user accounts
98 const ACCOUNT_TYPE_PERSON = 0;
99 const ACCOUNT_TYPE_ORGANISATION = 1;
100 const ACCOUNT_TYPE_NEWS = 2;
101 const ACCOUNT_TYPE_COMMUNITY = 3;
102 const ACCOUNT_TYPE_RELAY = 4;
103 const ACCOUNT_TYPE_DELETED = 127;
108 private static $owner;
111 * Returns the numeric account type by their string
113 * @param string $accounttype as string constant
114 * @return int|null Numeric account type - or null when not set
116 public static function getAccountTypeByString(string $accounttype)
118 switch ($accounttype) {
120 return User::ACCOUNT_TYPE_PERSON;
123 return User::ACCOUNT_TYPE_ORGANISATION;
126 return User::ACCOUNT_TYPE_NEWS;
129 return User::ACCOUNT_TYPE_COMMUNITY;
136 * Get the Uri-Id of the system account
140 public static function getSystemUriId(): int
142 $system = self::getSystemAccount();
143 return $system['uri-id'] ?? 0;
147 * Fetch the system account
149 * @return array system account
151 public static function getSystemAccount(): array
153 $system = Contact::selectFirst([], ['self' => true, 'uid' => 0]);
154 if (!DBA::isResult($system)) {
155 self::createSystemAccount();
156 $system = Contact::selectFirst([], ['self' => true, 'uid' => 0]);
157 if (!DBA::isResult($system)) {
162 $system['sprvkey'] = $system['uprvkey'] = $system['prvkey'];
163 $system['spubkey'] = $system['upubkey'] = $system['pubkey'];
164 $system['nickname'] = $system['nick'];
165 $system['page-flags'] = User::PAGE_FLAGS_SOAPBOX;
166 $system['account-type'] = $system['contact-type'];
167 $system['guid'] = '';
168 $system['picdate'] = '';
169 $system['theme'] = '';
170 $system['publish'] = false;
171 $system['net-publish'] = false;
172 $system['hide-friends'] = true;
173 $system['hidewall'] = true;
174 $system['prv_keywords'] = '';
175 $system['pub_keywords'] = '';
176 $system['address'] = '';
177 $system['locality'] = '';
178 $system['region'] = '';
179 $system['postal-code'] = '';
180 $system['country-name'] = '';
181 $system['homepage'] = (string)DI::baseUrl();
182 $system['dob'] = '0000-00-00';
184 // Ensure that the user contains data
185 $user = DBA::selectFirst('user', ['prvkey', 'guid'], ['uid' => 0]);
186 if (empty($user['prvkey']) || empty($user['guid'])) {
188 'username' => $system['name'],
189 'nickname' => $system['nick'],
190 'register_date' => $system['created'],
191 'pubkey' => $system['pubkey'],
192 'prvkey' => $system['prvkey'],
193 'spubkey' => $system['spubkey'],
194 'sprvkey' => $system['sprvkey'],
195 'guid' => System::createUUID(),
197 'page-flags' => User::PAGE_FLAGS_SOAPBOX,
198 'account-type' => User::ACCOUNT_TYPE_RELAY,
201 DBA::update('user', $fields, ['uid' => 0]);
203 $system['guid'] = $fields['guid'];
205 $system['guid'] = $user['guid'];
212 * Create the system account
216 private static function createSystemAccount()
218 $system_actor_name = self::getActorName();
219 if (empty($system_actor_name)) {
223 $keys = Crypto::newKeypair(4096);
224 if ($keys === false) {
225 throw new Exception(DI::l10n()->t('SERIOUS ERROR: Generation of security keys failed.'));
230 'created' => DateTimeFormat::utcNow(),
232 'network' => Protocol::ACTIVITYPUB,
233 'name' => 'System Account',
234 'addr' => $system_actor_name . '@' . DI::baseUrl()->getHost(),
235 'nick' => $system_actor_name,
236 'url' => DI::baseUrl() . '/friendica',
237 'pubkey' => $keys['pubkey'],
238 'prvkey' => $keys['prvkey'],
241 'contact-type' => Contact::TYPE_RELAY, // In AP this is translated to 'Application'
242 'name-date' => DateTimeFormat::utcNow(),
243 'uri-date' => DateTimeFormat::utcNow(),
244 'avatar-date' => DateTimeFormat::utcNow(),
246 'baseurl' => DI::baseUrl(),
249 $system['avatar'] = $system['photo'] = Contact::getDefaultAvatar($system, Proxy::SIZE_SMALL);
250 $system['thumb'] = Contact::getDefaultAvatar($system, Proxy::SIZE_THUMB);
251 $system['micro'] = Contact::getDefaultAvatar($system, Proxy::SIZE_MICRO);
252 $system['nurl'] = Strings::normaliseLink($system['url']);
253 $system['gsid'] = GServer::getID($system['baseurl']);
255 Contact::insert($system);
259 * Detect a usable actor name
261 * @return string actor account name
263 public static function getActorName(): string
265 $system_actor_name = DI::config()->get('system', 'actor_name');
266 if (!empty($system_actor_name)) {
267 $self = Contact::selectFirst(['nick'], ['uid' => 0, 'self' => true]);
268 if (!empty($self['nick'])) {
269 if ($self['nick'] != $system_actor_name) {
270 // Reset the actor name to the already used name
271 DI::config()->set('system', 'actor_name', $self['nick']);
272 $system_actor_name = $self['nick'];
275 return $system_actor_name;
278 // List of possible actor names
279 $possible_accounts = ['friendica', 'actor', 'system', 'internal'];
280 foreach ($possible_accounts as $name) {
281 if (!DBA::exists('user', ['nickname' => $name, 'account_removed' => false, 'account_expired' => false]) &&
282 !DBA::exists('userd', ['username' => $name])) {
283 DI::config()->set('system', 'actor_name', $name);
291 * Returns true if a user record exists with the provided id
298 public static function exists(int $uid): bool
300 return DBA::exists('user', ['uid' => $uid]);
304 * @param integer $uid
305 * @param array $fields
306 * @return array|boolean User record if it exists, false otherwise
309 public static function getById(int $uid, array $fields = [])
311 return !empty($uid) ? DBA::selectFirst('user', $fields, ['uid' => $uid]) : [];
315 * Returns a user record based on it's GUID
317 * @param string $guid The guid of the user
318 * @param array $fields The fields to retrieve
319 * @param bool $active True, if only active records are searched
321 * @return array|boolean User record if it exists, false otherwise
324 public static function getByGuid(string $guid, array $fields = [], bool $active = true)
327 $cond = ['guid' => $guid, 'account_expired' => false, 'account_removed' => false];
329 $cond = ['guid' => $guid];
332 return DBA::selectFirst('user', $fields, $cond);
336 * @param string $nickname
337 * @param array $fields
338 * @return array|boolean User record if it exists, false otherwise
341 public static function getByNickname(string $nickname, array $fields = [])
343 return DBA::selectFirst('user', $fields, ['nickname' => $nickname]);
347 * Returns the user id of a given profile URL
351 * @return integer user id
354 public static function getIdForURL(string $url): int
356 // Avoid database queries when the local node hostname isn't even part of the url.
357 if (!Contact::isLocal($url)) {
361 $self = Contact::selectFirst(['uid'], ['self' => true, 'nurl' => Strings::normaliseLink($url)]);
362 if (!empty($self['uid'])) {
366 $self = Contact::selectFirst(['uid'], ['self' => true, 'addr' => $url]);
367 if (!empty($self['uid'])) {
371 $self = Contact::selectFirst(['uid'], ['self' => true, 'alias' => [$url, Strings::normaliseLink($url)]]);
372 if (!empty($self['uid'])) {
380 * Get a user based on its email
382 * @param string $email
383 * @param array $fields
384 * @return array|boolean User record if it exists, false otherwise
387 public static function getByEmail(string $email, array $fields = [])
389 return DBA::selectFirst('user', $fields, ['email' => $email]);
393 * Fetch the user array of the administrator. The first one if there are several.
395 * @param array $fields
399 public static function getFirstAdmin(array $fields = []) : array
401 if (!empty(DI::config()->get('config', 'admin_nickname'))) {
402 return self::getByNickname(DI::config()->get('config', 'admin_nickname'), $fields);
405 return self::getAdminList()[0] ?? [];
409 * Get owner data by user id
412 * @param boolean $repairMissing Repair the owner data if it's missing
413 * @return boolean|array
416 public static function getOwnerDataById(int $uid, bool $repairMissing = true)
419 return self::getSystemAccount();
422 if (!empty(self::$owner[$uid])) {
423 return self::$owner[$uid];
426 $owner = DBA::selectFirst('owner-view', [], ['uid' => $uid]);
427 if (!DBA::isResult($owner)) {
428 if (!self::exists($uid) || !$repairMissing) {
431 if (!DBA::exists('profile', ['uid' => $uid])) {
432 DBA::insert('profile', ['uid' => $uid]);
434 if (!DBA::exists('contact', ['uid' => $uid, 'self' => true])) {
435 Contact::createSelfFromUserId($uid);
437 $owner = self::getOwnerDataById($uid, false);
440 if (empty($owner['nickname'])) {
444 if (!$repairMissing || $owner['account_expired']) {
448 // Check if the returned data is valid, otherwise fix it. See issue #6122
450 // Check for correct url and normalised nurl
451 $url = DI::baseUrl() . '/profile/' . $owner['nickname'];
452 $repair = empty($owner['network']) || ($owner['url'] != $url) || ($owner['nurl'] != Strings::normaliseLink($owner['url']));
455 // Check if "addr" is present and correct
456 $addr = $owner['nickname'] . '@' . substr(DI::baseUrl(), strpos(DI::baseUrl(), '://') + 3);
457 $repair = ($addr != $owner['addr']) || empty($owner['prvkey']) || empty($owner['pubkey']);
461 // Check if the avatar field is filled and the photo directs to the correct path
462 $avatar = Photo::selectFirst(['resource-id'], ['uid' => $uid, 'profile' => true]);
463 if (DBA::isResult($avatar)) {
464 $repair = empty($owner['avatar']) || !strpos($owner['photo'], $avatar['resource-id']);
469 Contact::updateSelfFromUserID($uid);
470 // Return the corrected data and avoid a loop
471 $owner = self::getOwnerDataById($uid, false);
474 self::$owner[$uid] = $owner;
479 * Get owner data by nick name
482 * @return boolean|array
485 public static function getOwnerDataByNick(string $nick)
487 $user = DBA::selectFirst('user', ['uid'], ['nickname' => $nick]);
489 if (!DBA::isResult($user)) {
493 return self::getOwnerDataById($user['uid']);
497 * Returns the default circle for a given user
499 * @param int $uid User id
501 * @return int circle id
504 public static function getDefaultCircle(int $uid): int
506 $user = DBA::selectFirst('user', ['def_gid'], ['uid' => $uid]);
507 if (DBA::isResult($user)) {
508 $default_circle = $user['def_gid'];
513 return $default_circle;
517 * Returns the default circle for groups for a given user
519 * @param int $uid User id
521 * @return int circle id
524 public static function getDefaultGroupCircle(int $uid): int
526 $default_circle = DI::pConfig()->get($uid, 'system', 'default-group-gid');
527 if (empty($default_circle)) {
528 $default_circle = self::getDefaultCircle($uid);
531 return $default_circle;
535 * Authenticate a user with a clear text password
537 * Returns the user id associated with a successful password authentication
539 * @param mixed $user_info
540 * @param string $password
541 * @param bool $third_party
542 * @return int User Id if authentication is successful
543 * @throws HTTPException\ForbiddenException
544 * @throws HTTPException\NotFoundException
546 public static function getIdFromPasswordAuthentication($user_info, string $password, bool $third_party = false): int
548 // Addons registered with the "authenticate" hook may create the user on the
549 // fly. `getAuthenticationInfo` will fail if the user doesn't exist yet. If
550 // the user doesn't exist, we should give the addons a chance to create the
551 // user in our database, if applicable, before re-throwing the exception if
554 $user = self::getAuthenticationInfo($user_info);
555 } catch (Exception $e) {
556 $username = (is_string($user_info) ? $user_info : $user_info['nickname'] ?? '');
558 // Addons can create users, and since this 'catch' branch should only
559 // execute if getAuthenticationInfo can't find an existing user, that's
560 // exactly what will happen here. Creating a numeric username would create
561 // ambiguity with user IDs, possibly opening up an attack vector.
562 // So let's be very careful about that.
563 if (empty($username) || is_numeric($username)) {
567 return self::getIdFromAuthenticateHooks($username, $password);
570 if ($third_party && DI::pConfig()->get($user['uid'], '2fa', 'verified')) {
571 // Third-party apps can't verify two-factor authentication, we use app-specific passwords instead
572 if (AppSpecificPassword::authenticateUser($user['uid'], $password)) {
575 } elseif (strpos($user['password'], '$') === false) {
576 //Legacy hash that has not been replaced by a new hash yet
577 if (self::hashPasswordLegacy($password) === $user['password']) {
578 self::updatePasswordHashed($user['uid'], self::hashPassword($password));
582 } elseif (!empty($user['legacy_password'])) {
583 //Legacy hash that has been double-hashed and not replaced by a new hash yet
584 //Warning: `legacy_password` is not necessary in sync with the content of `password`
585 if (password_verify(self::hashPasswordLegacy($password), $user['password'])) {
586 self::updatePasswordHashed($user['uid'], self::hashPassword($password));
590 } elseif (password_verify($password, $user['password'])) {
592 if (password_needs_rehash($user['password'], PASSWORD_DEFAULT)) {
593 self::updatePasswordHashed($user['uid'], self::hashPassword($password));
598 return self::getIdFromAuthenticateHooks($user['nickname'], $password); // throws
601 throw new HTTPException\ForbiddenException(DI::l10n()->t('Login failed'));
605 * Try to obtain a user ID via "authenticate" hook addons
607 * Returns the user id associated with a successful password authentication
609 * @param string $username
610 * @param string $password
611 * @return int User Id if authentication is successful
612 * @throws HTTPException\ForbiddenException
614 public static function getIdFromAuthenticateHooks(string $username, string $password): int
617 'username' => $username,
618 'password' => $password,
619 'authenticated' => 0,
620 'user_record' => null
624 * An addon indicates successful login by setting 'authenticated' to non-zero value and returning a user record
625 * Addons should never set 'authenticated' except to indicate success - as hooks may be chained
626 * and later addons should not interfere with an earlier one that succeeded.
628 Hook::callAll('authenticate', $addon_auth);
630 if ($addon_auth['authenticated'] && $addon_auth['user_record']) {
631 return $addon_auth['user_record']['uid'];
634 throw new HTTPException\ForbiddenException(DI::l10n()->t('Login failed'));
638 * Returns authentication info from various parameters types
640 * User info can be any of the following:
643 * - User email or username or nickname
644 * - User array with at least the uid and the hashed password
646 * @param mixed $user_info
647 * @return array|null Null if not found/determined
648 * @throws HTTPException\NotFoundException
650 public static function getAuthenticationInfo($user_info)
654 if (is_object($user_info) || is_array($user_info)) {
655 if (is_object($user_info)) {
656 $user = (array) $user_info;
663 || !isset($user['password'])
664 || !isset($user['legacy_password'])
666 throw new Exception(DI::l10n()->t('Not enough information to authenticate'));
668 } elseif (is_int($user_info) || is_string($user_info)) {
669 if (is_int($user_info)) {
670 $user = DBA::selectFirst(
672 ['uid', 'nickname', 'password', 'legacy_password'],
676 'account_expired' => 0,
677 'account_removed' => 0,
682 $fields = ['uid', 'nickname', 'password', 'legacy_password'];
684 "(`email` = ? OR `username` = ? OR `nickname` = ?)
685 AND NOT `blocked` AND NOT `account_expired` AND NOT `account_removed` AND `verified`",
686 $user_info, $user_info, $user_info
688 $user = DBA::selectFirst('user', $fields, $condition);
691 if (!DBA::isResult($user)) {
692 throw new HTTPException\NotFoundException(DI::l10n()->t('User not found'));
700 * Update the day of the last activity of the given user
702 * @param integer $uid
705 public static function updateLastActivity(int $uid)
711 $user = User::getById($uid, ['last-activity']);
716 $current_day = DateTimeFormat::utcNow('Y-m-d');
718 if ($user['last-activity'] != $current_day) {
719 User::update(['last-activity' => $current_day], $uid);
720 // Set the last activity for all identities of the user
721 DBA::update('user', ['last-activity' => $current_day], ['parent-uid' => $uid, 'account_removed' => false]);
726 * Generates a human-readable random password
731 public static function generateNewPassword(): string
733 return ucfirst(Strings::getRandomName(8)) . random_int(1000, 9999);
737 * Checks if the provided plaintext password has been exposed or not
739 * @param string $password
743 public static function isPasswordExposed(string $password): bool
745 $cache = new CacheItemPool();
746 $cache->changeConfig([
747 'cacheDirectory' => System::getTempPath() . '/password-exposed-cache/',
751 $passwordExposedChecker = new PasswordExposed\PasswordExposedChecker(null, $cache);
753 return $passwordExposedChecker->passwordExposed($password) === PasswordExposed\PasswordStatus::EXPOSED;
754 } catch (Exception $e) {
755 Logger::error('Password Exposed Exception: ' . $e->getMessage(), [
756 'code' => $e->getCode(),
757 'file' => $e->getFile(),
758 'line' => $e->getLine(),
759 'trace' => $e->getTraceAsString()
767 * Legacy hashing function, kept for password migration purposes
769 * @param string $password
772 private static function hashPasswordLegacy(string $password): string
774 return hash('whirlpool', $password);
778 * Global user password hashing function
780 * @param string $password
784 public static function hashPassword(string $password): string
786 if (!trim($password)) {
787 throw new Exception(DI::l10n()->t('Password can\'t be empty'));
790 return password_hash($password, PASSWORD_DEFAULT);
794 * Allowed characters are a-z, A-Z, 0-9 and special characters except white spaces and accentuated letters.
796 * Password length is limited to 72 characters if the current default password hashing algorithm is Blowfish.
797 * From the manual: "Using the PASSWORD_BCRYPT as the algorithm, will result in the password parameter being
798 * truncated to a maximum length of 72 bytes."
800 * @see https://www.php.net/manual/en/function.password-hash.php#refsect1-function.password-hash-parameters
802 * @param string|null $delimiter Whether the regular expression is meant to be wrapper in delimiter characters
805 public static function getPasswordRegExp(string $delimiter = null): string
807 $allowed_characters = ':!"#$%&\'()*+,-./;<=>?@[\]^_`{|}~';
810 $allowed_characters = preg_quote($allowed_characters, $delimiter);
813 return '^[a-zA-Z0-9' . $allowed_characters . ']' . (PASSWORD_DEFAULT === PASSWORD_BCRYPT ? '{1,72}' : '+') . '$';
817 * Updates a user row with a new plaintext password
820 * @param string $password
824 public static function updatePassword(int $uid, string $password): bool
826 $password = trim($password);
828 if (empty($password)) {
829 throw new Exception(DI::l10n()->t('Empty passwords are not allowed.'));
832 if (!DI::config()->get('system', 'disable_password_exposed', false) && self::isPasswordExposed($password)) {
833 throw new Exception(DI::l10n()->t('The new password has been exposed in a public data dump, please choose another.'));
836 if (PASSWORD_DEFAULT === PASSWORD_BCRYPT && strlen($password) > 72) {
837 throw new Exception(DI::l10n()->t('The password length is limited to 72 characters.'));
840 if (!preg_match('/' . self::getPasswordRegExp('/') . '/', $password)) {
841 throw new Exception(DI::l10n()->t("The password can't contain white spaces nor accentuated letters"));
844 return self::updatePasswordHashed($uid, self::hashPassword($password));
848 * Updates a user row with a new hashed password.
849 * Empties the password reset token field just in case.
852 * @param string $password_hashed
856 private static function updatePasswordHashed(int $uid, string $password_hashed): bool
859 'password' => $password_hashed,
861 'pwdreset_time' => null,
862 'legacy_password' => false
864 return DBA::update('user', $fields, ['uid' => $uid]);
868 * Returns if the given uid is valid and in the admin list
875 public static function isSiteAdmin(int $uid): bool
877 return DBA::exists('user', [
879 'email' => self::getAdminEmailList()
884 * Checks if a nickname is in the list of the forbidden nicknames
886 * Check if a nickname is forbidden from registration on the node by the
887 * admin. Forbidden nicknames (e.g. role names) can be configured in the
890 * @param string $nickname The nickname that should be checked
891 * @return boolean True is the nickname is blocked on the node
893 public static function isNicknameBlocked(string $nickname): bool
895 $forbidden_nicknames = DI::config()->get('system', 'forbidden_nicknames', '');
896 if (!empty($forbidden_nicknames)) {
897 $forbidden = explode(',', $forbidden_nicknames);
898 $forbidden = array_map('trim', $forbidden);
903 // Add the name of the internal actor to the "forbidden" list
904 $actor_name = self::getActorName();
905 if (!empty($actor_name)) {
906 $forbidden[] = $actor_name;
909 if (empty($forbidden)) {
913 // check if the nickname is in the list of blocked nicknames
914 if (in_array(strtolower($nickname), $forbidden)) {
923 * Get avatar link for given user
926 * @param string $size One of the Proxy::SIZE_* constants
927 * @return string avatar link
930 public static function getAvatarUrl(array $user, string $size = ''): string
932 if (empty($user['nickname'])) {
933 DI::logger()->warning('Missing user nickname key', ['trace' => System::callstack(20)]);
936 $url = DI::baseUrl() . '/photo/';
939 case Proxy::SIZE_MICRO:
943 case Proxy::SIZE_THUMB:
956 $photo = Photo::selectFirst(['type', 'created', 'edited', 'updated'], ["scale" => $scale, 'uid' => $user['uid'], 'profile' => true]);
957 if (!empty($photo)) {
958 $updated = max($photo['created'], $photo['edited'], $photo['updated']);
959 $mimetype = $photo['type'];
962 return $url . $user['nickname'] . Images::getExtensionByMimeType($mimetype) . ($updated ? '?ts=' . strtotime($updated) : '');
966 * Get banner link for given user
969 * @return string banner link
972 public static function getBannerUrl(array $user): string
974 if (empty($user['nickname'])) {
975 DI::logger()->warning('Missing user nickname key', ['trace' => System::callstack(20)]);
978 $url = DI::baseUrl() . '/photo/banner/';
983 $photo = Photo::selectFirst(['type', 'created', 'edited', 'updated'], ["scale" => 3, 'uid' => $user['uid'], 'photo-type' => Photo::USER_BANNER]);
984 if (!empty($photo)) {
985 $updated = max($photo['created'], $photo['edited'], $photo['updated']);
986 $mimetype = $photo['type'];
988 // Only for the RC phase: Don't return an image link for the default picture
992 return $url . $user['nickname'] . Images::getExtensionByMimeType($mimetype) . ($updated ? '?ts=' . strtotime($updated) : '');
996 * Catch-all user creation function
998 * Creates a user from the provided data array, either form fields or OpenID.
999 * Required: { username, nickname, email } or { openid_url }
1001 * Performs the following:
1002 * - Sends to the OpenId auth URL (if relevant)
1003 * - Creates new key pairs for crypto
1004 * - Create self-contact
1005 * - Create profile image
1007 * @param array $data
1009 * @throws ErrorException
1010 * @throws HTTPException\InternalServerErrorException
1011 * @throws ImagickException
1014 public static function create(array $data): array
1016 $return = ['user' => null, 'password' => ''];
1018 $using_invites = DI::config()->get('system', 'invitation_only');
1020 $invite_id = !empty($data['invite_id']) ? trim($data['invite_id']) : '';
1021 $username = !empty($data['username']) ? trim($data['username']) : '';
1022 $nickname = !empty($data['nickname']) ? trim($data['nickname']) : '';
1023 $email = !empty($data['email']) ? trim($data['email']) : '';
1024 $openid_url = !empty($data['openid_url']) ? trim($data['openid_url']) : '';
1025 $photo = !empty($data['photo']) ? trim($data['photo']) : '';
1026 $password = !empty($data['password']) ? trim($data['password']) : '';
1027 $password1 = !empty($data['password1']) ? trim($data['password1']) : '';
1028 $confirm = !empty($data['confirm']) ? trim($data['confirm']) : '';
1029 $blocked = !empty($data['blocked']);
1030 $verified = !empty($data['verified']);
1031 $language = !empty($data['language']) ? trim($data['language']) : 'en';
1033 $netpublish = $publish = !empty($data['profile_publish_reg']);
1035 if ($password1 != $confirm) {
1036 throw new Exception(DI::l10n()->t('Passwords do not match. Password unchanged.'));
1037 } elseif ($password1 != '') {
1038 $password = $password1;
1041 if ($using_invites) {
1043 throw new Exception(DI::l10n()->t('An invitation is required.'));
1046 if (!Register::existsByHash($invite_id)) {
1047 throw new Exception(DI::l10n()->t('Invitation could not be verified.'));
1051 /// @todo Check if this part is really needed. We should have fetched all this data in advance
1052 if (empty($username) || empty($email) || empty($nickname)) {
1054 if (!Network::isUrlValid($openid_url)) {
1055 throw new Exception(DI::l10n()->t('Invalid OpenID url'));
1057 $_SESSION['register'] = 1;
1058 $_SESSION['openid'] = $openid_url;
1060 $openid = new LightOpenID(DI::baseUrl()->getHost());
1061 $openid->identity = $openid_url;
1062 $openid->returnUrl = DI::baseUrl() . '/openid';
1063 $openid->required = ['namePerson/friendly', 'contact/email', 'namePerson'];
1064 $openid->optional = ['namePerson/first', 'media/image/aspect11', 'media/image/default'];
1066 $authurl = $openid->authUrl();
1067 } catch (Exception $e) {
1068 throw new Exception(DI::l10n()->t('We encountered a problem while logging in with the OpenID you provided. Please check the correct spelling of the ID.') . '<br />' . DI::l10n()->t('The error message was:') . $e->getMessage(), 0, $e);
1070 System::externalRedirect($authurl);
1074 throw new Exception(DI::l10n()->t('Please enter the required information.'));
1077 if (!Network::isUrlValid($openid_url)) {
1081 // collapse multiple spaces in name
1082 $username = preg_replace('/ +/', ' ', $username);
1084 $username_min_length = max(1, min(64, intval(DI::config()->get('system', 'username_min_length', 3))));
1085 $username_max_length = max(1, min(64, intval(DI::config()->get('system', 'username_max_length', 48))));
1087 if ($username_min_length > $username_max_length) {
1088 Logger::error(DI::l10n()->t('system.username_min_length (%s) and system.username_max_length (%s) are excluding each other, swapping values.', $username_min_length, $username_max_length));
1089 $tmp = $username_min_length;
1090 $username_min_length = $username_max_length;
1091 $username_max_length = $tmp;
1094 if (mb_strlen($username) < $username_min_length) {
1095 throw new Exception(DI::l10n()->tt('Username should be at least %s character.', 'Username should be at least %s characters.', $username_min_length));
1098 if (mb_strlen($username) > $username_max_length) {
1099 throw new Exception(DI::l10n()->tt('Username should be at most %s character.', 'Username should be at most %s characters.', $username_max_length));
1102 // So now we are just looking for a space in the full name.
1103 $loose_reg = DI::config()->get('system', 'no_regfullname');
1105 $username = mb_convert_case($username, MB_CASE_TITLE, 'UTF-8');
1106 if (strpos($username, ' ') === false) {
1107 throw new Exception(DI::l10n()->t("That doesn't appear to be your full (First Last) name."));
1111 if (!Network::isEmailDomainAllowed($email)) {
1112 throw new Exception(DI::l10n()->t('Your email domain is not among those allowed on this site.'));
1115 if (!filter_var($email, FILTER_VALIDATE_EMAIL) || !Network::isEmailDomainValid($email)) {
1116 throw new Exception(DI::l10n()->t('Not a valid email address.'));
1118 if (self::isNicknameBlocked($nickname)) {
1119 throw new Exception(DI::l10n()->t('The nickname was blocked from registration by the nodes admin.'));
1122 if (DI::config()->get('system', 'block_extended_register', false) && DBA::exists('user', ['email' => $email])) {
1123 throw new Exception(DI::l10n()->t('Cannot use that email.'));
1126 // Disallow somebody creating an account using openid that uses the admin email address,
1127 // since openid bypasses email verification. We'll allow it if there is not yet an admin account.
1128 if (strlen($openid_url) && in_array(strtolower($email), self::getAdminEmailList())) {
1129 throw new Exception(DI::l10n()->t('Cannot use that email.'));
1132 $nickname = $data['nickname'] = strtolower($nickname);
1134 if (!preg_match('/^[a-z0-9][a-z0-9_]*$/', $nickname)) {
1135 throw new Exception(DI::l10n()->t('Your nickname can only contain a-z, 0-9 and _.'));
1138 // Check existing and deleted accounts for this nickname.
1140 DBA::exists('user', ['nickname' => $nickname])
1141 || DBA::exists('userd', ['username' => $nickname])
1143 throw new Exception(DI::l10n()->t('Nickname is already registered. Please choose another.'));
1146 $new_password = strlen($password) ? $password : User::generateNewPassword();
1147 $new_password_encoded = self::hashPassword($new_password);
1149 $return['password'] = $new_password;
1151 $keys = Crypto::newKeypair(4096);
1152 if ($keys === false) {
1153 throw new Exception(DI::l10n()->t('SERIOUS ERROR: Generation of security keys failed.'));
1156 $prvkey = $keys['prvkey'];
1157 $pubkey = $keys['pubkey'];
1159 // Create another keypair for signing/verifying salmon protocol messages.
1160 $sres = Crypto::newKeypair(512);
1161 $sprvkey = $sres['prvkey'];
1162 $spubkey = $sres['pubkey'];
1164 $insert_result = DBA::insert('user', [
1165 'guid' => System::createUUID(),
1166 'username' => $username,
1167 'password' => $new_password_encoded,
1169 'openid' => $openid_url,
1170 'nickname' => $nickname,
1171 'pubkey' => $pubkey,
1172 'prvkey' => $prvkey,
1173 'spubkey' => $spubkey,
1174 'sprvkey' => $sprvkey,
1175 'verified' => $verified,
1176 'blocked' => $blocked,
1177 'language' => $language,
1178 'timezone' => 'UTC',
1179 'register_date' => DateTimeFormat::utcNow(),
1180 'default-location' => ''
1183 if ($insert_result) {
1184 $uid = DBA::lastInsertId();
1185 $user = DBA::selectFirst('user', [], ['uid' => $uid]);
1187 throw new Exception(DI::l10n()->t('An error occurred during registration. Please try again.'));
1191 throw new Exception(DI::l10n()->t('An error occurred during registration. Please try again.'));
1194 // if somebody clicked submit twice very quickly, they could end up with two accounts
1195 // due to race condition. Remove this one.
1196 $user_count = DBA::count('user', ['nickname' => $nickname]);
1197 if ($user_count > 1) {
1198 DBA::delete('user', ['uid' => $uid]);
1200 throw new Exception(DI::l10n()->t('Nickname is already registered. Please choose another.'));
1203 $insert_result = DBA::insert('profile', [
1205 'name' => $username,
1206 'photo' => self::getAvatarUrl($user),
1207 'thumb' => self::getAvatarUrl($user, Proxy::SIZE_THUMB),
1208 'publish' => $publish,
1209 'net-publish' => $netpublish,
1211 if (!$insert_result) {
1212 DBA::delete('user', ['uid' => $uid]);
1214 throw new Exception(DI::l10n()->t('An error occurred creating your default profile. Please try again.'));
1217 // Create the self contact
1218 if (!Contact::createSelfFromUserId($uid)) {
1219 DBA::delete('user', ['uid' => $uid]);
1221 throw new Exception(DI::l10n()->t('An error occurred creating your self contact. Please try again.'));
1224 // Create a circle with no members. This allows somebody to use it
1225 // right away as a default circle for new contacts.
1226 $def_gid = Circle::create($uid, DI::l10n()->t('Friends'));
1228 DBA::delete('user', ['uid' => $uid]);
1230 throw new Exception(DI::l10n()->t('An error occurred creating your default contact circle. Please try again.'));
1233 $fields = ['def_gid' => $def_gid];
1234 if (DI::config()->get('system', 'newuser_private') && $def_gid) {
1235 $fields['allow_gid'] = '<' . $def_gid . '>';
1238 DBA::update('user', $fields, ['uid' => $uid]);
1240 $def_gid_groups = Circle::create($uid, DI::l10n()->t('Groups'));
1241 if ($def_gid_groups) {
1242 DI::pConfig()->set($uid, 'system', 'default-group-gid', $def_gid_groups);
1245 // if we have no OpenID photo try to look up an avatar
1246 if (!strlen($photo)) {
1247 $photo = Network::lookupAvatarByEmail($email);
1250 // unless there is no avatar-addon loaded
1251 if (strlen($photo)) {
1252 $photo_failure = false;
1254 $filename = basename($photo);
1255 $curlResult = DI::httpClient()->get($photo, HttpClientAccept::IMAGE);
1256 if ($curlResult->isSuccess()) {
1257 Logger::debug('Got picture', ['Content-Type' => $curlResult->getHeader('Content-Type'), 'url' => $photo]);
1258 $img_str = $curlResult->getBody();
1259 $type = $curlResult->getContentType();
1265 $type = Images::getMimeTypeByData($img_str, $photo, $type);
1267 $image = new Image($img_str, $type);
1268 if ($image->isValid()) {
1269 $image->scaleToSquare(300);
1271 $resource_id = Photo::newResource();
1273 // Not using Photo::PROFILE_PHOTOS here, so that it is discovered as translatable string
1274 $profile_album = DI::l10n()->t('Profile Photos');
1276 $r = Photo::store($image, $uid, 0, $resource_id, $filename, $profile_album, 4);
1279 $photo_failure = true;
1282 $image->scaleDown(80);
1284 $r = Photo::store($image, $uid, 0, $resource_id, $filename, $profile_album, 5);
1287 $photo_failure = true;
1290 $image->scaleDown(48);
1292 $r = Photo::store($image, $uid, 0, $resource_id, $filename, $profile_album, 6);
1295 $photo_failure = true;
1298 if (!$photo_failure) {
1299 Photo::update(['profile' => true, 'photo-type' => Photo::USER_AVATAR], ['resource-id' => $resource_id]);
1303 Contact::updateSelfFromUserID($uid, true);
1306 Hook::callAll('register_account', $uid);
1308 self::setRegisterMethodByUserCount();
1310 $return['user'] = $user;
1315 * Update a user entry and distribute the changes if needed
1317 * @param array $fields
1318 * @param integer $uid
1321 public static function update(array $fields, int $uid): bool
1323 $old_owner = self::getOwnerDataById($uid);
1324 if (empty($old_owner)) {
1328 if (!DBA::update('user', $fields, ['uid' => $uid])) {
1332 $update = Contact::updateSelfFromUserID($uid);
1334 $owner = self::getOwnerDataById($uid);
1335 if (empty($owner)) {
1339 if ($old_owner['name'] != $owner['name']) {
1340 Profile::update(['name' => $owner['name']], $uid);
1344 Profile::publishUpdate($uid);
1351 * Sets block state for a given user
1353 * @param int $uid The user id
1354 * @param bool $block Block state (default is true)
1356 * @return bool True, if successfully blocked
1360 public static function block(int $uid, bool $block = true): bool
1362 return DBA::update('user', ['blocked' => $block], ['uid' => $uid]);
1366 * Allows a registration based on a hash
1368 * @param string $hash
1370 * @return bool True, if the allow was successful
1372 * @throws HTTPException\InternalServerErrorException
1375 public static function allow(string $hash): bool
1377 $register = Register::getByHash($hash);
1378 if (!DBA::isResult($register)) {
1382 $user = User::getById($register['uid']);
1383 if (!DBA::isResult($user)) {
1387 Register::deleteByHash($hash);
1389 DBA::update('user', ['blocked' => false, 'verified' => true], ['uid' => $register['uid']]);
1391 $profile = DBA::selectFirst('profile', ['net-publish'], ['uid' => $register['uid']]);
1393 if (DBA::isResult($profile) && $profile['net-publish'] && Search::getGlobalDirectory()) {
1394 $url = DI::baseUrl() . '/profile/' . $user['nickname'];
1395 Worker::add(Worker::PRIORITY_LOW, "Directory", $url);
1398 $l10n = DI::l10n()->withLang($register['language']);
1400 return User::sendRegisterOpenEmail(
1403 DI::config()->get('config', 'sitename'),
1405 ($register['password'] ?? '') ?: 'Sent in a previous email'
1410 * Denys a pending registration
1412 * @param string $hash The hash of the pending user
1414 * This does not have to go through user_remove() and save the nickname
1415 * permanently against re-registration, as the person was not yet
1416 * allowed to have friends on this system
1418 * @return bool True, if the deny was successful
1421 public static function deny(string $hash): bool
1423 $register = Register::getByHash($hash);
1424 if (!DBA::isResult($register)) {
1428 $user = User::getById($register['uid']);
1429 if (!DBA::isResult($user)) {
1433 // Delete the avatar
1434 Photo::delete(['uid' => $register['uid']]);
1436 return DBA::delete('user', ['uid' => $register['uid']]) &&
1437 Register::deleteByHash($register['hash']);
1441 * Creates a new user based on a minimal set and sends an email to this user
1443 * @param string $name The user's name
1444 * @param string $email The user's email address
1445 * @param string $nick The user's nick name
1446 * @param string $lang The user's language (default is english)
1447 * @return bool True, if the user was created successfully
1448 * @throws HTTPException\InternalServerErrorException
1449 * @throws ErrorException
1450 * @throws ImagickException
1452 public static function createMinimal(string $name, string $email, string $nick, string $lang = L10n::DEFAULT): bool
1457 throw new HTTPException\InternalServerErrorException('Invalid arguments.');
1460 $result = self::create([
1461 'username' => $name,
1463 'nickname' => $nick,
1468 $user = $result['user'];
1469 $preamble = Strings::deindent(DI::l10n()->t('
1471 the administrator of %2$s has set up an account for you.'));
1472 $body = Strings::deindent(DI::l10n()->t('
1473 The login details are as follows:
1479 You may change your password from your account "Settings" page after logging
1482 Please take a few moments to review the other account settings on that page.
1484 You may also wish to add some basic information to your default profile
1485 (on the "Profiles" page) so that other people can easily find you.
1487 We recommend setting your full name, adding a profile photo,
1488 adding some profile "keywords" (very useful in making new friends) - and
1489 perhaps what country you live in; if you do not wish to be more specific
1492 We fully respect your right to privacy, and none of these items are necessary.
1493 If you are new and do not know anybody here, they may help
1494 you to make some new and interesting friends.
1496 If you ever want to delete your account, you can do so at %1$s/settings/removeme
1498 Thank you and welcome to %4$s.'));
1500 $preamble = sprintf($preamble, $user['username'], DI::config()->get('config', 'sitename'));
1501 $body = sprintf($body, DI::baseUrl(), $user['nickname'], $result['password'], DI::config()->get('config', 'sitename'));
1503 $email = DI::emailer()
1505 ->withMessage(DI::l10n()->t('Registration details for %s', DI::config()->get('config', 'sitename')), $preamble, $body)
1507 ->withRecipient($user['email'])
1509 return DI::emailer()->send($email);
1513 * Sends pending registration confirmation email
1515 * @param array $user User record array
1516 * @param string $sitename
1517 * @param string $siteurl
1518 * @param string $password Plaintext password
1519 * @return NULL|boolean from notification() and email() inherited
1520 * @throws HTTPException\InternalServerErrorException
1522 public static function sendRegisterPendingEmail(array $user, string $sitename, string $siteurl, string $password)
1524 $body = Strings::deindent(DI::l10n()->t(
1527 Thank you for registering at %2$s. Your account is pending for approval by the administrator.
1529 Your login details are as follows:
1542 $email = DI::emailer()
1544 ->withMessage(DI::l10n()->t('Registration at %s', $sitename), $body)
1546 ->withRecipient($user['email'])
1548 return DI::emailer()->send($email);
1552 * Sends registration confirmation
1554 * It's here as a function because the mail is sent from different parts
1556 * @param L10n $l10n The used language
1557 * @param array $user User record array
1558 * @param string $sitename
1559 * @param string $siteurl
1560 * @param string $password Plaintext password
1562 * @return NULL|boolean from notification() and email() inherited
1563 * @throws HTTPException\InternalServerErrorException
1565 public static function sendRegisterOpenEmail(L10n $l10n, array $user, string $sitename, string $siteurl, string $password)
1567 $preamble = Strings::deindent($l10n->t(
1570 Thank you for registering at %2$s. Your account has been created.
1575 $body = Strings::deindent($l10n->t(
1577 The login details are as follows:
1583 You may change your password from your account "Settings" page after logging
1586 Please take a few moments to review the other account settings on that page.
1588 You may also wish to add some basic information to your default profile
1589 ' . "\x28" . 'on the "Profiles" page' . "\x29" . ' so that other people can easily find you.
1591 We recommend setting your full name, adding a profile photo,
1592 adding some profile "keywords" ' . "\x28" . 'very useful in making new friends' . "\x29" . ' - and
1593 perhaps what country you live in; if you do not wish to be more specific
1596 We fully respect your right to privacy, and none of these items are necessary.
1597 If you are new and do not know anybody here, they may help
1598 you to make some new and interesting friends.
1600 If you ever want to delete your account, you can do so at %3$s/settings/removeme
1602 Thank you and welcome to %2$s.',
1610 $email = DI::emailer()
1612 ->withMessage(DI::l10n()->t('Registration details for %s', $sitename), $preamble, $body)
1614 ->withRecipient($user['email'])
1616 return DI::emailer()->send($email);
1620 * @param int $uid user to remove
1622 * @throws HTTPException\InternalServerErrorException
1624 public static function remove(int $uid): bool
1630 Logger::notice('Removing user', ['user' => $uid]);
1632 $user = DBA::selectFirst('user', [], ['uid' => $uid]);
1634 Hook::callAll('remove_user', $user);
1636 // save username (actually the nickname as it is guaranteed
1637 // unique), so it cannot be re-registered in the future.
1638 DBA::insert('userd', ['username' => $user['nickname']]);
1640 // Remove all personal settings, especially connector settings
1641 DBA::delete('pconfig', ['uid' => $uid]);
1643 // The user and related data will be deleted in Friendica\Worker\ExpireAndRemoveUsers
1644 DBA::update('user', ['account_removed' => true, 'account_expires_on' => DateTimeFormat::utc('now + 7 day')], ['uid' => $uid]);
1645 Worker::add(Worker::PRIORITY_HIGH, 'Notifier', Delivery::REMOVAL, $uid);
1647 // Send an update to the directory
1648 $self = DBA::selectFirst('contact', ['url'], ['uid' => $uid, 'self' => true]);
1649 Worker::add(Worker::PRIORITY_LOW, 'Directory', $self['url']);
1651 // Remove the user relevant data
1652 Worker::add(Worker::PRIORITY_NEGLIGIBLE, 'RemoveUser', $uid);
1654 self::setRegisterMethodByUserCount();
1659 * Return all identities to a user
1661 * @param int $uid The user id
1662 * @return array All identities for this user
1664 * Example for a return:
1668 * 'username' => 'maxmuster',
1669 * 'nickname' => 'Max Mustermann'
1673 * 'username' => 'johndoe',
1674 * 'nickname' => 'John Doe'
1679 public static function identities(int $uid): array
1687 $user = DBA::selectFirst('user', ['uid', 'nickname', 'username', 'parent-uid'], ['uid' => $uid]);
1688 if (!DBA::isResult($user)) {
1692 if (!$user['parent-uid']) {
1693 // First add our own entry
1695 'uid' => $user['uid'],
1696 'username' => $user['username'],
1697 'nickname' => $user['nickname']
1700 // Then add all the children
1703 ['uid', 'username', 'nickname'],
1704 ['parent-uid' => $user['uid'], 'account_removed' => false]
1706 if (DBA::isResult($r)) {
1707 $identities = array_merge($identities, DBA::toArray($r));
1710 // First entry is our parent
1713 ['uid', 'username', 'nickname'],
1714 ['uid' => $user['parent-uid'], 'account_removed' => false]
1716 if (DBA::isResult($r)) {
1717 $identities = DBA::toArray($r);
1720 // Then add all siblings
1723 ['uid', 'username', 'nickname'],
1724 ['parent-uid' => $user['parent-uid'], 'account_removed' => false]
1726 if (DBA::isResult($r)) {
1727 $identities = array_merge($identities, DBA::toArray($r));
1732 "SELECT `user`.`uid`, `user`.`username`, `user`.`nickname`
1734 INNER JOIN `user` ON `manage`.`mid` = `user`.`uid`
1735 WHERE `user`.`account_removed` = 0 AND `manage`.`uid` = ?",
1738 if (DBA::isResult($r)) {
1739 $identities = array_merge($identities, DBA::toArray($r));
1746 * Check if the given user id has delegations or is delegated
1751 public static function hasIdentities(int $uid): bool
1757 $user = DBA::selectFirst('user', ['parent-uid'], ['uid' => $uid, 'account_removed' => false]);
1758 if (!DBA::isResult($user)) {
1762 if ($user['parent-uid']) {
1766 if (DBA::exists('user', ['parent-uid' => $uid, 'account_removed' => false])) {
1770 if (DBA::exists('manage', ['uid' => $uid])) {
1778 * Returns statistical information about the current users of this node
1784 public static function getStatistics(): array
1788 'active_users_halfyear' => 0,
1789 'active_users_monthly' => 0,
1790 'active_users_weekly' => 0,
1793 $userStmt = DBA::select('owner-view', ['uid', 'last-activity', 'last-item'],
1794 ["`verified` AND `last-activity` > ? AND NOT `blocked`
1795 AND NOT `account_removed` AND NOT `account_expired`",
1796 DBA::NULL_DATETIME]);
1797 if (!DBA::isResult($userStmt)) {
1801 $halfyear = time() - (180 * 24 * 60 * 60);
1802 $month = time() - (30 * 24 * 60 * 60);
1803 $week = time() - (7 * 24 * 60 * 60);
1805 while ($user = DBA::fetch($userStmt)) {
1806 $statistics['total_users']++;
1808 if ((strtotime($user['last-activity']) > $halfyear) || (strtotime($user['last-item']) > $halfyear)
1810 $statistics['active_users_halfyear']++;
1813 if ((strtotime($user['last-activity']) > $month) || (strtotime($user['last-item']) > $month)
1815 $statistics['active_users_monthly']++;
1818 if ((strtotime($user['last-activity']) > $week) || (strtotime($user['last-item']) > $week)
1820 $statistics['active_users_weekly']++;
1823 DBA::close($userStmt);
1829 * Get all users of the current node
1831 * @param int $start Start count (Default is 0)
1832 * @param int $count Count of the items per page (Default is @see Pager::ITEMS_PER_PAGE)
1833 * @param string $type The type of users, which should get (all, blocked, removed)
1834 * @param string $order Order of the user list (Default is 'contact.name')
1835 * @param bool $descending Order direction (Default is ascending)
1836 * @return array|bool The list of the users
1839 public static function getList(int $start = 0, int $count = Pager::ITEMS_PER_PAGE, string $type = 'all', string $order = 'name', bool $descending = false)
1841 $param = ['limit' => [$start, $count], 'order' => [$order => $descending]];
1845 $condition['account_removed'] = false;
1846 $condition['blocked'] = false;
1850 $condition['account_removed'] = false;
1851 $condition['blocked'] = true;
1852 $condition['verified'] = true;
1856 $condition['account_removed'] = true;
1860 return DBA::selectToArray('owner-view', [], $condition, $param);
1864 * Returns a list of lowercase admin email addresses from the comma-separated list in the config
1868 public static function getAdminEmailList(): array
1870 $adminEmails = strtolower(str_replace(' ', '', DI::config()->get('config', 'admin_email')));
1871 if (!$adminEmails) {
1875 return explode(',', $adminEmails);
1879 * Returns the complete list of admin user accounts
1881 * @param array $fields
1885 public static function getAdminList(array $fields = []): array
1888 'email' => self::getAdminEmailList(),
1889 'parent-uid' => null,
1892 'account_removed' => false,
1893 'account_expired' => false,
1896 return DBA::selectToArray('user', $fields, $condition, ['order' => ['uid']]);
1900 * Return a list of admin user accounts where each unique email address appears only once.
1902 * This method is meant for admin notifications that do not need to be sent multiple times to the same email address.
1904 * @param array $fields
1908 public static function getAdminListForEmailing(array $fields = []): array
1910 return array_filter(self::getAdminList($fields), function ($user) {
1911 static $emails = [];
1913 if (in_array($user['email'], $emails)) {
1917 $emails[] = $user['email'];
1923 public static function setRegisterMethodByUserCount()
1925 $max_registered_users = DI::config()->get('config', 'max_registered_users');
1926 if ($max_registered_users <= 0) {
1930 $register_policy = DI::config()->get('config', 'register_policy');
1931 if (!in_array($register_policy, [Module\Register::OPEN, Module\Register::CLOSED])) {
1932 Logger::debug('Unsupported register policy.', ['policy' => $register_policy]);
1936 $users = DBA::count('user', ['blocked' => false, 'account_removed' => false, 'account_expired' => false]);
1937 if (($users >= $max_registered_users) && ($register_policy == Module\Register::OPEN)) {
1938 DI::config()->set('config', 'register_policy', Module\Register::CLOSED);
1939 Logger::notice('Max users reached, registration is closed.', ['users' => $users, 'max' => $max_registered_users]);
1940 } elseif (($users < $max_registered_users) && ($register_policy == Module\Register::CLOSED)) {
1941 DI::config()->set('config', 'register_policy', Module\Register::OPEN);
1942 Logger::notice('Below maximum users, registration is opened.', ['users' => $users, 'max' => $max_registered_users]);
1944 Logger::debug('Unchanged register policy', ['policy' => $register_policy, 'users' => $users, 'max' => $max_registered_users]);