3 * @copyright Copyright (C) 2020, Friendica
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\PasswordExposed;
26 use Friendica\Content\Pager;
27 use Friendica\Core\Hook;
28 use Friendica\Core\L10n;
29 use Friendica\Core\Logger;
30 use Friendica\Core\Protocol;
31 use Friendica\Core\System;
32 use Friendica\Core\Worker;
33 use Friendica\Database\DBA;
35 use Friendica\Model\TwoFactor\AppSpecificPassword;
36 use Friendica\Network\HTTPException\InternalServerErrorException;
37 use Friendica\Network\HTTPRequest;
38 use Friendica\Object\Image;
39 use Friendica\Util\Crypto;
40 use Friendica\Util\DateTimeFormat;
41 use Friendica\Util\Images;
42 use Friendica\Util\Network;
43 use Friendica\Util\Strings;
44 use Friendica\Worker\Delivery;
48 * This class handles User related functions
55 * PAGE_FLAGS_NORMAL is a typical personal profile account
56 * PAGE_FLAGS_SOAPBOX automatically approves all friend requests as Contact::SHARING, (readonly)
57 * PAGE_FLAGS_COMMUNITY automatically approves all friend requests as Contact::SHARING, but with
58 * write access to wall and comments (no email and not included in page owner's ACL lists)
59 * PAGE_FLAGS_FREELOVE automatically approves all friend requests as full friends (Contact::FRIEND).
63 const PAGE_FLAGS_NORMAL = 0;
64 const PAGE_FLAGS_SOAPBOX = 1;
65 const PAGE_FLAGS_COMMUNITY = 2;
66 const PAGE_FLAGS_FREELOVE = 3;
67 const PAGE_FLAGS_BLOG = 4;
68 const PAGE_FLAGS_PRVGROUP = 5;
76 * ACCOUNT_TYPE_PERSON - the account belongs to a person
77 * Associated page types: PAGE_FLAGS_NORMAL, PAGE_FLAGS_SOAPBOX, PAGE_FLAGS_FREELOVE
79 * ACCOUNT_TYPE_ORGANISATION - the account belongs to an organisation
80 * Associated page type: PAGE_FLAGS_SOAPBOX
82 * ACCOUNT_TYPE_NEWS - the account is a news reflector
83 * Associated page type: PAGE_FLAGS_SOAPBOX
85 * ACCOUNT_TYPE_COMMUNITY - the account is community forum
86 * Associated page types: PAGE_COMMUNITY, PAGE_FLAGS_PRVGROUP
88 * ACCOUNT_TYPE_RELAY - the account is a relay
89 * This will only be assigned to contacts, not to user accounts
92 const ACCOUNT_TYPE_PERSON = 0;
93 const ACCOUNT_TYPE_ORGANISATION = 1;
94 const ACCOUNT_TYPE_NEWS = 2;
95 const ACCOUNT_TYPE_COMMUNITY = 3;
96 const ACCOUNT_TYPE_RELAY = 4;
102 * Returns true if a user record exists with the provided id
104 * @param integer $uid
108 public static function exists($uid)
110 return DBA::exists('user', ['uid' => $uid]);
114 * @param integer $uid
115 * @param array $fields
116 * @return array|boolean User record if it exists, false otherwise
119 public static function getById($uid, array $fields = [])
121 return DBA::selectFirst('user', $fields, ['uid' => $uid]);
125 * Returns a user record based on it's GUID
127 * @param string $guid The guid of the user
128 * @param array $fields The fields to retrieve
129 * @param bool $active True, if only active records are searched
131 * @return array|boolean User record if it exists, false otherwise
134 public static function getByGuid(string $guid, array $fields = [], bool $active = true)
137 $cond = ['guid' => $guid, 'account_expired' => false, 'account_removed' => false];
139 $cond = ['guid' => $guid];
142 return DBA::selectFirst('user', $fields, $cond);
146 * @param string $nickname
147 * @param array $fields
148 * @return array|boolean User record if it exists, false otherwise
151 public static function getByNickname($nickname, array $fields = [])
153 return DBA::selectFirst('user', $fields, ['nickname' => $nickname]);
157 * Returns the user id of a given profile URL
161 * @return integer user id
164 public static function getIdForURL($url)
166 $self = DBA::selectFirst('contact', ['uid'], ['nurl' => Strings::normaliseLink($url), 'self' => true]);
167 if (!DBA::isResult($self)) {
175 * Get a user based on its email
177 * @param string $email
178 * @param array $fields
180 * @return array|boolean User record if it exists, false otherwise
184 public static function getByEmail($email, array $fields = [])
186 return DBA::selectFirst('user', $fields, ['email' => $email]);
190 * Get owner data by user id
193 * @param boolean $check_valid Test if data is invalid and correct it
194 * @return boolean|array
197 public static function getOwnerDataById($uid, $check_valid = true)
199 $owner = DBA::selectFirst('owner-view', [], ['uid' => $uid]);
200 if (!DBA::isResult($owner)) {
201 if (!DBA::exists('user', ['uid' => $uid]) || !$check_valid) {
204 Contact::createSelfFromUserId($uid);
205 $owner = self::getOwnerDataById($uid, false);
208 if (empty($owner['nickname'])) {
216 // Check if the returned data is valid, otherwise fix it. See issue #6122
218 // Check for correct url and normalised nurl
219 $url = DI::baseUrl() . '/profile/' . $owner['nickname'];
220 $repair = ($owner['url'] != $url) || ($owner['nurl'] != Strings::normaliseLink($owner['url']));
223 // Check if "addr" is present and correct
224 $addr = $owner['nickname'] . '@' . substr(DI::baseUrl(), strpos(DI::baseUrl(), '://') + 3);
225 $repair = ($addr != $owner['addr']);
229 // Check if the avatar field is filled and the photo directs to the correct path
230 $avatar = Photo::selectFirst(['resource-id'], ['uid' => $uid, 'profile' => true]);
231 if (DBA::isResult($avatar)) {
232 $repair = empty($owner['avatar']) || !strpos($owner['photo'], $avatar['resource-id']);
237 Contact::updateSelfFromUserID($uid);
238 // Return the corrected data and avoid a loop
239 $owner = self::getOwnerDataById($uid, false);
246 * Get owner data by nick name
249 * @return boolean|array
252 public static function getOwnerDataByNick($nick)
254 $user = DBA::selectFirst('user', ['uid'], ['nickname' => $nick]);
256 if (!DBA::isResult($user)) {
260 return self::getOwnerDataById($user['uid']);
264 * Returns the default group for a given user and network
266 * @param int $uid User id
267 * @param string $network network name
269 * @return int group id
270 * @throws InternalServerErrorException
272 public static function getDefaultGroup($uid, $network = '')
276 if ($network == Protocol::OSTATUS) {
277 $default_group = DI::pConfig()->get($uid, "ostatus", "default_group");
280 if ($default_group != 0) {
281 return $default_group;
284 $user = DBA::selectFirst('user', ['def_gid'], ['uid' => $uid]);
286 if (DBA::isResult($user)) {
287 $default_group = $user["def_gid"];
290 return $default_group;
295 * Authenticate a user with a clear text password
297 * @param mixed $user_info
298 * @param string $password
299 * @param bool $third_party
300 * @return int|boolean
301 * @deprecated since version 3.6
302 * @see User::getIdFromPasswordAuthentication()
304 public static function authenticate($user_info, $password, $third_party = false)
307 return self::getIdFromPasswordAuthentication($user_info, $password, $third_party);
308 } catch (Exception $ex) {
314 * Authenticate a user with a clear text password
316 * Returns the user id associated with a successful password authentication
318 * @param mixed $user_info
319 * @param string $password
320 * @param bool $third_party
321 * @return int User Id if authentication is successful
324 public static function getIdFromPasswordAuthentication($user_info, $password, $third_party = false)
326 $user = self::getAuthenticationInfo($user_info);
328 if ($third_party && DI::pConfig()->get($user['uid'], '2fa', 'verified')) {
329 // Third-party apps can't verify two-factor authentication, we use app-specific passwords instead
330 if (AppSpecificPassword::authenticateUser($user['uid'], $password)) {
333 } elseif (strpos($user['password'], '$') === false) {
334 //Legacy hash that has not been replaced by a new hash yet
335 if (self::hashPasswordLegacy($password) === $user['password']) {
336 self::updatePasswordHashed($user['uid'], self::hashPassword($password));
340 } elseif (!empty($user['legacy_password'])) {
341 //Legacy hash that has been double-hashed and not replaced by a new hash yet
342 //Warning: `legacy_password` is not necessary in sync with the content of `password`
343 if (password_verify(self::hashPasswordLegacy($password), $user['password'])) {
344 self::updatePasswordHashed($user['uid'], self::hashPassword($password));
348 } elseif (password_verify($password, $user['password'])) {
350 if (password_needs_rehash($user['password'], PASSWORD_DEFAULT)) {
351 self::updatePasswordHashed($user['uid'], self::hashPassword($password));
357 throw new Exception(DI::l10n()->t('Login failed'));
361 * Returns authentication info from various parameters types
363 * User info can be any of the following:
366 * - User email or username or nickname
367 * - User array with at least the uid and the hashed password
369 * @param mixed $user_info
373 private static function getAuthenticationInfo($user_info)
377 if (is_object($user_info) || is_array($user_info)) {
378 if (is_object($user_info)) {
379 $user = (array) $user_info;
386 || !isset($user['password'])
387 || !isset($user['legacy_password'])
389 throw new Exception(DI::l10n()->t('Not enough information to authenticate'));
391 } elseif (is_int($user_info) || is_string($user_info)) {
392 if (is_int($user_info)) {
393 $user = DBA::selectFirst(
395 ['uid', 'password', 'legacy_password'],
399 'account_expired' => 0,
400 'account_removed' => 0,
405 $fields = ['uid', 'password', 'legacy_password'];
407 "(`email` = ? OR `username` = ? OR `nickname` = ?)
408 AND NOT `blocked` AND NOT `account_expired` AND NOT `account_removed` AND `verified`",
409 $user_info, $user_info, $user_info
411 $user = DBA::selectFirst('user', $fields, $condition);
414 if (!DBA::isResult($user)) {
415 throw new Exception(DI::l10n()->t('User not found'));
423 * Generates a human-readable random password
427 public static function generateNewPassword()
429 return ucfirst(Strings::getRandomName(8)) . random_int(1000, 9999);
433 * Checks if the provided plaintext password has been exposed or not
435 * @param string $password
439 public static function isPasswordExposed($password)
441 $cache = new \DivineOmega\DOFileCachePSR6\CacheItemPool();
442 $cache->changeConfig([
443 'cacheDirectory' => get_temppath() . '/password-exposed-cache/',
447 $passwordExposedChecker = new PasswordExposed\PasswordExposedChecker(null, $cache);
449 return $passwordExposedChecker->passwordExposed($password) === PasswordExposed\PasswordStatus::EXPOSED;
450 } catch (\Exception $e) {
451 Logger::error('Password Exposed Exception: ' . $e->getMessage(), [
452 'code' => $e->getCode(),
453 'file' => $e->getFile(),
454 'line' => $e->getLine(),
455 'trace' => $e->getTraceAsString()
463 * Legacy hashing function, kept for password migration purposes
465 * @param string $password
468 private static function hashPasswordLegacy($password)
470 return hash('whirlpool', $password);
474 * Global user password hashing function
476 * @param string $password
480 public static function hashPassword($password)
482 if (!trim($password)) {
483 throw new Exception(DI::l10n()->t('Password can\'t be empty'));
486 return password_hash($password, PASSWORD_DEFAULT);
490 * Updates a user row with a new plaintext password
493 * @param string $password
497 public static function updatePassword($uid, $password)
499 $password = trim($password);
501 if (empty($password)) {
502 throw new Exception(DI::l10n()->t('Empty passwords are not allowed.'));
505 if (!DI::config()->get('system', 'disable_password_exposed', false) && self::isPasswordExposed($password)) {
506 throw new Exception(DI::l10n()->t('The new password has been exposed in a public data dump, please choose another.'));
509 $allowed_characters = '!"#$%&\'()*+,-./;<=>?@[\]^_`{|}~';
511 if (!preg_match('/^[a-z0-9' . preg_quote($allowed_characters, '/') . ']+$/i', $password)) {
512 throw new Exception(DI::l10n()->t('The password can\'t contain accentuated letters, white spaces or colons (:)'));
515 return self::updatePasswordHashed($uid, self::hashPassword($password));
519 * Updates a user row with a new hashed password.
520 * Empties the password reset token field just in case.
523 * @param string $pasword_hashed
527 private static function updatePasswordHashed($uid, $pasword_hashed)
530 'password' => $pasword_hashed,
532 'pwdreset_time' => null,
533 'legacy_password' => false
535 return DBA::update('user', $fields, ['uid' => $uid]);
539 * Checks if a nickname is in the list of the forbidden nicknames
541 * Check if a nickname is forbidden from registration on the node by the
542 * admin. Forbidden nicknames (e.g. role namess) can be configured in the
545 * @param string $nickname The nickname that should be checked
546 * @return boolean True is the nickname is blocked on the node
547 * @throws InternalServerErrorException
549 public static function isNicknameBlocked($nickname)
551 $forbidden_nicknames = DI::config()->get('system', 'forbidden_nicknames', '');
553 // if the config variable is empty return false
554 if (empty($forbidden_nicknames)) {
558 // check if the nickname is in the list of blocked nicknames
559 $forbidden = explode(',', $forbidden_nicknames);
560 $forbidden = array_map('trim', $forbidden);
561 if (in_array(strtolower($nickname), $forbidden)) {
570 * Catch-all user creation function
572 * Creates a user from the provided data array, either form fields or OpenID.
573 * Required: { username, nickname, email } or { openid_url }
575 * Performs the following:
576 * - Sends to the OpenId auth URL (if relevant)
577 * - Creates new key pairs for crypto
578 * - Create self-contact
579 * - Create profile image
583 * @throws \ErrorException
584 * @throws InternalServerErrorException
585 * @throws \ImagickException
588 public static function create(array $data)
590 $return = ['user' => null, 'password' => ''];
592 $using_invites = DI::config()->get('system', 'invitation_only');
594 $invite_id = !empty($data['invite_id']) ? Strings::escapeTags(trim($data['invite_id'])) : '';
595 $username = !empty($data['username']) ? Strings::escapeTags(trim($data['username'])) : '';
596 $nickname = !empty($data['nickname']) ? Strings::escapeTags(trim($data['nickname'])) : '';
597 $email = !empty($data['email']) ? Strings::escapeTags(trim($data['email'])) : '';
598 $openid_url = !empty($data['openid_url']) ? Strings::escapeTags(trim($data['openid_url'])) : '';
599 $photo = !empty($data['photo']) ? Strings::escapeTags(trim($data['photo'])) : '';
600 $password = !empty($data['password']) ? trim($data['password']) : '';
601 $password1 = !empty($data['password1']) ? trim($data['password1']) : '';
602 $confirm = !empty($data['confirm']) ? trim($data['confirm']) : '';
603 $blocked = !empty($data['blocked']);
604 $verified = !empty($data['verified']);
605 $language = !empty($data['language']) ? Strings::escapeTags(trim($data['language'])) : 'en';
607 $netpublish = $publish = !empty($data['profile_publish_reg']);
609 if ($password1 != $confirm) {
610 throw new Exception(DI::l10n()->t('Passwords do not match. Password unchanged.'));
611 } elseif ($password1 != '') {
612 $password = $password1;
615 if ($using_invites) {
617 throw new Exception(DI::l10n()->t('An invitation is required.'));
620 if (!Register::existsByHash($invite_id)) {
621 throw new Exception(DI::l10n()->t('Invitation could not be verified.'));
625 /// @todo Check if this part is really needed. We should have fetched all this data in advance
626 if (empty($username) || empty($email) || empty($nickname)) {
628 if (!Network::isUrlValid($openid_url)) {
629 throw new Exception(DI::l10n()->t('Invalid OpenID url'));
631 $_SESSION['register'] = 1;
632 $_SESSION['openid'] = $openid_url;
634 $openid = new LightOpenID(DI::baseUrl()->getHostname());
635 $openid->identity = $openid_url;
636 $openid->returnUrl = DI::baseUrl() . '/openid';
637 $openid->required = ['namePerson/friendly', 'contact/email', 'namePerson'];
638 $openid->optional = ['namePerson/first', 'media/image/aspect11', 'media/image/default'];
640 $authurl = $openid->authUrl();
641 } catch (Exception $e) {
642 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.') . EOL . EOL . DI::l10n()->t('The error message was:') . $e->getMessage(), 0, $e);
644 System::externalRedirect($authurl);
648 throw new Exception(DI::l10n()->t('Please enter the required information.'));
651 if (!Network::isUrlValid($openid_url)) {
655 // collapse multiple spaces in name
656 $username = preg_replace('/ +/', ' ', $username);
658 $username_min_length = max(1, min(64, intval(DI::config()->get('system', 'username_min_length', 3))));
659 $username_max_length = max(1, min(64, intval(DI::config()->get('system', 'username_max_length', 48))));
661 if ($username_min_length > $username_max_length) {
662 Logger::log(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), Logger::WARNING);
663 $tmp = $username_min_length;
664 $username_min_length = $username_max_length;
665 $username_max_length = $tmp;
668 if (mb_strlen($username) < $username_min_length) {
669 throw new Exception(DI::l10n()->tt('Username should be at least %s character.', 'Username should be at least %s characters.', $username_min_length));
672 if (mb_strlen($username) > $username_max_length) {
673 throw new Exception(DI::l10n()->tt('Username should be at most %s character.', 'Username should be at most %s characters.', $username_max_length));
676 // So now we are just looking for a space in the full name.
677 $loose_reg = DI::config()->get('system', 'no_regfullname');
679 $username = mb_convert_case($username, MB_CASE_TITLE, 'UTF-8');
680 if (strpos($username, ' ') === false) {
681 throw new Exception(DI::l10n()->t("That doesn't appear to be your full (First Last) name."));
685 if (!Network::isEmailDomainAllowed($email)) {
686 throw new Exception(DI::l10n()->t('Your email domain is not among those allowed on this site.'));
689 if (!filter_var($email, FILTER_VALIDATE_EMAIL) || !Network::isEmailDomainValid($email)) {
690 throw new Exception(DI::l10n()->t('Not a valid email address.'));
692 if (self::isNicknameBlocked($nickname)) {
693 throw new Exception(DI::l10n()->t('The nickname was blocked from registration by the nodes admin.'));
696 if (DI::config()->get('system', 'block_extended_register', false) && DBA::exists('user', ['email' => $email])) {
697 throw new Exception(DI::l10n()->t('Cannot use that email.'));
700 // Disallow somebody creating an account using openid that uses the admin email address,
701 // since openid bypasses email verification. We'll allow it if there is not yet an admin account.
702 if (DI::config()->get('config', 'admin_email') && strlen($openid_url)) {
703 $adminlist = explode(',', str_replace(' ', '', strtolower(DI::config()->get('config', 'admin_email'))));
704 if (in_array(strtolower($email), $adminlist)) {
705 throw new Exception(DI::l10n()->t('Cannot use that email.'));
709 $nickname = $data['nickname'] = strtolower($nickname);
711 if (!preg_match('/^[a-z0-9][a-z0-9\_]*$/', $nickname)) {
712 throw new Exception(DI::l10n()->t('Your nickname can only contain a-z, 0-9 and _.'));
715 // Check existing and deleted accounts for this nickname.
717 DBA::exists('user', ['nickname' => $nickname])
718 || DBA::exists('userd', ['username' => $nickname])
720 throw new Exception(DI::l10n()->t('Nickname is already registered. Please choose another.'));
723 $new_password = strlen($password) ? $password : User::generateNewPassword();
724 $new_password_encoded = self::hashPassword($new_password);
726 $return['password'] = $new_password;
728 $keys = Crypto::newKeypair(4096);
729 if ($keys === false) {
730 throw new Exception(DI::l10n()->t('SERIOUS ERROR: Generation of security keys failed.'));
733 $prvkey = $keys['prvkey'];
734 $pubkey = $keys['pubkey'];
736 // Create another keypair for signing/verifying salmon protocol messages.
737 $sres = Crypto::newKeypair(512);
738 $sprvkey = $sres['prvkey'];
739 $spubkey = $sres['pubkey'];
741 $insert_result = DBA::insert('user', [
742 'guid' => System::createUUID(),
743 'username' => $username,
744 'password' => $new_password_encoded,
746 'openid' => $openid_url,
747 'nickname' => $nickname,
750 'spubkey' => $spubkey,
751 'sprvkey' => $sprvkey,
752 'verified' => $verified,
753 'blocked' => $blocked,
754 'language' => $language,
756 'register_date' => DateTimeFormat::utcNow(),
757 'default-location' => ''
760 if ($insert_result) {
761 $uid = DBA::lastInsertId();
762 $user = DBA::selectFirst('user', [], ['uid' => $uid]);
764 throw new Exception(DI::l10n()->t('An error occurred during registration. Please try again.'));
768 throw new Exception(DI::l10n()->t('An error occurred during registration. Please try again.'));
771 // if somebody clicked submit twice very quickly, they could end up with two accounts
772 // due to race condition. Remove this one.
773 $user_count = DBA::count('user', ['nickname' => $nickname]);
774 if ($user_count > 1) {
775 DBA::delete('user', ['uid' => $uid]);
777 throw new Exception(DI::l10n()->t('Nickname is already registered. Please choose another.'));
780 $insert_result = DBA::insert('profile', [
783 'photo' => DI::baseUrl() . "/photo/profile/{$uid}.jpg",
784 'thumb' => DI::baseUrl() . "/photo/avatar/{$uid}.jpg",
785 'publish' => $publish,
786 'net-publish' => $netpublish,
788 if (!$insert_result) {
789 DBA::delete('user', ['uid' => $uid]);
791 throw new Exception(DI::l10n()->t('An error occurred creating your default profile. Please try again.'));
794 // Create the self contact
795 if (!Contact::createSelfFromUserId($uid)) {
796 DBA::delete('user', ['uid' => $uid]);
798 throw new Exception(DI::l10n()->t('An error occurred creating your self contact. Please try again.'));
801 // Create a group with no members. This allows somebody to use it
802 // right away as a default group for new contacts.
803 $def_gid = Group::create($uid, DI::l10n()->t('Friends'));
805 DBA::delete('user', ['uid' => $uid]);
807 throw new Exception(DI::l10n()->t('An error occurred creating your default contact group. Please try again.'));
810 $fields = ['def_gid' => $def_gid];
811 if (DI::config()->get('system', 'newuser_private') && $def_gid) {
812 $fields['allow_gid'] = '<' . $def_gid . '>';
815 DBA::update('user', $fields, ['uid' => $uid]);
817 // if we have no OpenID photo try to look up an avatar
818 if (!strlen($photo)) {
819 $photo = Network::lookupAvatarByEmail($email);
822 // unless there is no avatar-addon loaded
823 if (strlen($photo)) {
824 $photo_failure = false;
826 $filename = basename($photo);
827 $curlResult = HTTPRequest::curl($photo, true);
828 if ($curlResult->isSuccess()) {
829 $img_str = $curlResult->getBody();
830 $type = $curlResult->getContentType();
836 $type = Images::getMimeTypeByData($img_str, $photo, $type);
838 $Image = new Image($img_str, $type);
839 if ($Image->isValid()) {
840 $Image->scaleToSquare(300);
842 $resource_id = Photo::newResource();
844 $r = Photo::store($Image, $uid, 0, $resource_id, $filename, DI::l10n()->t('Profile Photos'), 4);
847 $photo_failure = true;
850 $Image->scaleDown(80);
852 $r = Photo::store($Image, $uid, 0, $resource_id, $filename, DI::l10n()->t('Profile Photos'), 5);
855 $photo_failure = true;
858 $Image->scaleDown(48);
860 $r = Photo::store($Image, $uid, 0, $resource_id, $filename, DI::l10n()->t('Profile Photos'), 6);
863 $photo_failure = true;
866 if (!$photo_failure) {
867 Photo::update(['profile' => 1], ['resource-id' => $resource_id]);
872 Hook::callAll('register_account', $uid);
874 $return['user'] = $user;
879 * Sets block state for a given user
881 * @param int $uid The user id
882 * @param bool $block Block state (default is true)
884 * @return bool True, if successfully blocked
888 public static function block(int $uid, bool $block = true)
890 return DBA::update('user', ['blocked' => $block], ['uid' => $uid]);
894 * Allows a registration based on a hash
896 * @param string $hash
898 * @return bool True, if the allow was successful
900 * @throws InternalServerErrorException
903 public static function allow(string $hash)
905 $register = Register::getByHash($hash);
906 if (!DBA::isResult($register)) {
910 $user = User::getById($register['uid']);
911 if (!DBA::isResult($user)) {
915 Register::deleteByHash($hash);
917 DBA::update('user', ['blocked' => false, 'verified' => true], ['uid' => $register['uid']]);
919 $profile = DBA::selectFirst('profile', ['net-publish'], ['uid' => $register['uid']]);
921 if (DBA::isResult($profile) && $profile['net-publish'] && DI::config()->get('system', 'directory')) {
922 $url = DI::baseUrl() . '/profile/' . $user['nickname'];
923 Worker::add(PRIORITY_LOW, "Directory", $url);
926 $l10n = DI::l10n()->withLang($register['language']);
928 return User::sendRegisterOpenEmail(
931 DI::config()->get('config', 'sitename'),
932 DI::baseUrl()->get(),
933 ($register['password'] ?? '') ?: 'Sent in a previous email'
938 * Denys a pending registration
940 * @param string $hash The hash of the pending user
942 * This does not have to go through user_remove() and save the nickname
943 * permanently against re-registration, as the person was not yet
944 * allowed to have friends on this system
946 * @return bool True, if the deny was successfull
949 public static function deny(string $hash)
951 $register = Register::getByHash($hash);
952 if (!DBA::isResult($register)) {
956 $user = User::getById($register['uid']);
957 if (!DBA::isResult($user)) {
961 return DBA::delete('user', ['uid' => $register['uid']]) &&
962 Register::deleteByHash($register['hash']);
966 * Creates a new user based on a minimal set and sends an email to this user
968 * @param string $name The user's name
969 * @param string $email The user's email address
970 * @param string $nick The user's nick name
971 * @param string $lang The user's language (default is english)
973 * @return bool True, if the user was created successfully
974 * @throws InternalServerErrorException
975 * @throws \ErrorException
976 * @throws \ImagickException
978 public static function createMinimal(string $name, string $email, string $nick, string $lang = L10n::DEFAULT)
983 throw new InternalServerErrorException('Invalid arguments.');
986 $result = self::create([
994 $user = $result['user'];
995 $preamble = Strings::deindent(DI::l10n()->t('
997 the administrator of %2$s has set up an account for you.'));
998 $body = Strings::deindent(DI::l10n()->t('
999 The login details are as follows:
1005 You may change your password from your account "Settings" page after logging
1008 Please take a few moments to review the other account settings on that page.
1010 You may also wish to add some basic information to your default profile
1011 (on the "Profiles" page) so that other people can easily find you.
1013 We recommend setting your full name, adding a profile photo,
1014 adding some profile "keywords" (very useful in making new friends) - and
1015 perhaps what country you live in; if you do not wish to be more specific
1018 We fully respect your right to privacy, and none of these items are necessary.
1019 If you are new and do not know anybody here, they may help
1020 you to make some new and interesting friends.
1022 If you ever want to delete your account, you can do so at %1$s/removeme
1024 Thank you and welcome to %4$s.'));
1026 $preamble = sprintf($preamble, $user['username'], DI::config()->get('config', 'sitename'));
1027 $body = sprintf($body, DI::baseUrl()->get(), $user['nickname'], $result['password'], DI::config()->get('config', 'sitename'));
1029 $email = DI::emailer()
1031 ->withMessage(DI::l10n()->t('Registration details for %s', DI::config()->get('config', 'sitename')), $preamble, $body)
1033 ->withRecipient($user['email'])
1035 return DI::emailer()->send($email);
1039 * Sends pending registration confirmation email
1041 * @param array $user User record array
1042 * @param string $sitename
1043 * @param string $siteurl
1044 * @param string $password Plaintext password
1045 * @return NULL|boolean from notification() and email() inherited
1046 * @throws InternalServerErrorException
1048 public static function sendRegisterPendingEmail($user, $sitename, $siteurl, $password)
1050 $body = Strings::deindent(DI::l10n()->t(
1053 Thank you for registering at %2$s. Your account is pending for approval by the administrator.
1055 Your login details are as follows:
1068 $email = DI::emailer()
1070 ->withMessage(DI::l10n()->t('Registration at %s', $sitename), $body)
1072 ->withRecipient($user['email'])
1074 return DI::emailer()->send($email);
1078 * Sends registration confirmation
1080 * It's here as a function because the mail is sent from different parts
1082 * @param \Friendica\Core\L10n $l10n The used language
1083 * @param array $user User record array
1084 * @param string $sitename
1085 * @param string $siteurl
1086 * @param string $password Plaintext password
1088 * @return NULL|boolean from notification() and email() inherited
1089 * @throws InternalServerErrorException
1091 public static function sendRegisterOpenEmail(\Friendica\Core\L10n $l10n, $user, $sitename, $siteurl, $password)
1093 $preamble = Strings::deindent($l10n->t(
1096 Thank you for registering at %2$s. Your account has been created.
1101 $body = Strings::deindent($l10n->t(
1103 The login details are as follows:
1109 You may change your password from your account "Settings" page after logging
1112 Please take a few moments to review the other account settings on that page.
1114 You may also wish to add some basic information to your default profile
1115 ' . "\x28" . 'on the "Profiles" page' . "\x29" . ' so that other people can easily find you.
1117 We recommend setting your full name, adding a profile photo,
1118 adding some profile "keywords" ' . "\x28" . 'very useful in making new friends' . "\x29" . ' - and
1119 perhaps what country you live in; if you do not wish to be more specific
1122 We fully respect your right to privacy, and none of these items are necessary.
1123 If you are new and do not know anybody here, they may help
1124 you to make some new and interesting friends.
1126 If you ever want to delete your account, you can do so at %3$s/removeme
1128 Thank you and welcome to %2$s.',
1136 $email = DI::emailer()
1138 ->withMessage(DI::l10n()->t('Registration details for %s', $sitename), $preamble, $body)
1140 ->withRecipient($user['email'])
1142 return DI::emailer()->send($email);
1146 * @param int $uid user to remove
1148 * @throws InternalServerErrorException
1150 public static function remove(int $uid)
1156 Logger::log('Removing user: ' . $uid);
1158 $user = DBA::selectFirst('user', [], ['uid' => $uid]);
1160 Hook::callAll('remove_user', $user);
1162 // save username (actually the nickname as it is guaranteed
1163 // unique), so it cannot be re-registered in the future.
1164 DBA::insert('userd', ['username' => $user['nickname']]);
1166 // The user and related data will be deleted in Friendica\Worker\CronJobs::expireAndRemoveUsers()
1167 DBA::update('user', ['account_removed' => true, 'account_expires_on' => DateTimeFormat::utc('now + 7 day')], ['uid' => $uid]);
1168 Worker::add(PRIORITY_HIGH, 'Notifier', Delivery::REMOVAL, $uid);
1170 // Send an update to the directory
1171 $self = DBA::selectFirst('contact', ['url'], ['uid' => $uid, 'self' => true]);
1172 Worker::add(PRIORITY_LOW, 'Directory', $self['url']);
1174 // Remove the user relevant data
1175 Worker::add(PRIORITY_NEGLIGIBLE, 'RemoveUser', $uid);
1181 * Return all identities to a user
1183 * @param int $uid The user id
1184 * @return array All identities for this user
1186 * Example for a return:
1190 * 'username' => 'maxmuster',
1191 * 'nickname' => 'Max Mustermann'
1195 * 'username' => 'johndoe',
1196 * 'nickname' => 'John Doe'
1201 public static function identities($uid)
1205 $user = DBA::selectFirst('user', ['uid', 'nickname', 'username', 'parent-uid'], ['uid' => $uid]);
1206 if (!DBA::isResult($user)) {
1210 if ($user['parent-uid'] == 0) {
1211 // First add our own entry
1213 'uid' => $user['uid'],
1214 'username' => $user['username'],
1215 'nickname' => $user['nickname']
1218 // Then add all the children
1221 ['uid', 'username', 'nickname'],
1222 ['parent-uid' => $user['uid'], 'account_removed' => false]
1224 if (DBA::isResult($r)) {
1225 $identities = array_merge($identities, DBA::toArray($r));
1228 // First entry is our parent
1231 ['uid', 'username', 'nickname'],
1232 ['uid' => $user['parent-uid'], 'account_removed' => false]
1234 if (DBA::isResult($r)) {
1235 $identities = DBA::toArray($r);
1238 // Then add all siblings
1241 ['uid', 'username', 'nickname'],
1242 ['parent-uid' => $user['parent-uid'], 'account_removed' => false]
1244 if (DBA::isResult($r)) {
1245 $identities = array_merge($identities, DBA::toArray($r));
1250 "SELECT `user`.`uid`, `user`.`username`, `user`.`nickname`
1252 INNER JOIN `user` ON `manage`.`mid` = `user`.`uid`
1253 WHERE `user`.`account_removed` = 0 AND `manage`.`uid` = ?",
1256 if (DBA::isResult($r)) {
1257 $identities = array_merge($identities, DBA::toArray($r));
1264 * Returns statistical information about the current users of this node
1270 public static function getStatistics()
1274 'active_users_halfyear' => 0,
1275 'active_users_monthly' => 0,
1276 'active_users_weekly' => 0,
1279 $userStmt = DBA::select('owner-view', ['uid', 'login_date', 'last-item'],
1280 ["`verified` AND `login_date` > ? AND NOT `blocked`
1281 AND NOT `account_removed` AND NOT `account_expired`",
1282 DBA::NULL_DATETIME]);
1283 if (!DBA::isResult($userStmt)) {
1287 $halfyear = time() - (180 * 24 * 60 * 60);
1288 $month = time() - (30 * 24 * 60 * 60);
1289 $week = time() - (7 * 24 * 60 * 60);
1291 while ($user = DBA::fetch($userStmt)) {
1292 $statistics['total_users']++;
1294 if ((strtotime($user['login_date']) > $halfyear) || (strtotime($user['last-item']) > $halfyear)
1296 $statistics['active_users_halfyear']++;
1299 if ((strtotime($user['login_date']) > $month) || (strtotime($user['last-item']) > $month)
1301 $statistics['active_users_monthly']++;
1304 if ((strtotime($user['login_date']) > $week) || (strtotime($user['last-item']) > $week)
1306 $statistics['active_users_weekly']++;
1309 DBA::close($userStmt);
1315 * Get all users of the current node
1317 * @param int $start Start count (Default is 0)
1318 * @param int $count Count of the items per page (Default is @see Pager::ITEMS_PER_PAGE)
1319 * @param string $type The type of users, which should get (all, bocked, removed)
1320 * @param string $order Order of the user list (Default is 'contact.name')
1321 * @param bool $descending Order direction (Default is ascending)
1323 * @return array The list of the users
1326 public static function getList($start = 0, $count = Pager::ITEMS_PER_PAGE, $type = 'all', $order = 'name', bool $descending = false)
1328 $param = ['limit' => [$start, $count], 'order' => [$order => $descending]];
1332 $condition['account_removed'] = false;
1333 $condition['blocked'] = false;
1336 $condition['blocked'] = true;
1339 $condition['account_removed'] = true;
1343 return DBA::selectToArray('owner-view', [], $condition, $param);