3 * @file src/Model/User.php
4 * @brief This file includes the User class with user related database functions
6 namespace Friendica\Model;
8 use DivineOmega\PasswordExposed\PasswordStatus;
10 use Friendica\Core\Addon;
11 use Friendica\Core\Config;
12 use Friendica\Core\L10n;
13 use Friendica\Core\PConfig;
14 use Friendica\Core\Protocol;
15 use Friendica\Core\System;
16 use Friendica\Core\Worker;
17 use Friendica\Database\DBA;
18 use Friendica\Object\Image;
19 use Friendica\Util\Crypto;
20 use Friendica\Util\DateTimeFormat;
21 use Friendica\Util\Network;
23 use function password_exposed;
25 require_once 'boot.php';
26 require_once 'include/dba.php';
27 require_once 'include/enotify.php';
28 require_once 'include/text.php';
30 * @brief This class handles User related functions
35 * @brief Get owner data by user id
38 * @return boolean|array
40 public static function getOwnerDataById($uid) {
41 $r = DBA::fetchFirst("SELECT
43 `user`.`prvkey` AS `uprvkey`,
49 `user`.`account-type`,
53 ON `user`.`uid` = `contact`.`uid`
54 WHERE `contact`.`uid` = ?
59 if (!DBA::isResult($r)) {
66 * @brief Get owner data by nick name
69 * @return boolean|array
71 public static function getOwnerDataByNick($nick)
73 $user = DBA::selectFirst('user', ['uid'], ['nickname' => $nick]);
75 if (!DBA::isResult($user)) {
79 return self::getOwnerDataById($user['uid']);
83 * @brief Returns the default group for a given user and network
85 * @param int $uid User id
86 * @param string $network network name
88 * @return int group id
90 public static function getDefaultGroup($uid, $network = '')
94 if ($network == Protocol::OSTATUS) {
95 $default_group = PConfig::get($uid, "ostatus", "default_group");
98 if ($default_group != 0) {
99 return $default_group;
102 $user = DBA::selectFirst('user', ['def_gid'], ['uid' => $uid]);
104 if (DBA::isResult($user)) {
105 $default_group = $user["def_gid"];
108 return $default_group;
113 * Authenticate a user with a clear text password
115 * @brief Authenticate a user with a clear text password
116 * @param mixed $user_info
117 * @param string $password
118 * @return int|boolean
119 * @deprecated since version 3.6
120 * @see User::getIdFromPasswordAuthentication()
122 public static function authenticate($user_info, $password)
125 return self::getIdFromPasswordAuthentication($user_info, $password);
126 } catch (Exception $ex) {
132 * Returns the user id associated with a successful password authentication
134 * @brief Authenticate a user with a clear text password
135 * @param mixed $user_info
136 * @param string $password
137 * @return int User Id if authentication is successful
140 public static function getIdFromPasswordAuthentication($user_info, $password)
142 $user = self::getAuthenticationInfo($user_info);
144 if (strpos($user['password'], '$') === false) {
145 //Legacy hash that has not been replaced by a new hash yet
146 if (self::hashPasswordLegacy($password) === $user['password']) {
147 self::updatePassword($user['uid'], $password);
151 } elseif (!empty($user['legacy_password'])) {
152 //Legacy hash that has been double-hashed and not replaced by a new hash yet
153 //Warning: `legacy_password` is not necessary in sync with the content of `password`
154 if (password_verify(self::hashPasswordLegacy($password), $user['password'])) {
155 self::updatePassword($user['uid'], $password);
159 } elseif (password_verify($password, $user['password'])) {
161 if (password_needs_rehash($user['password'], PASSWORD_DEFAULT)) {
162 self::updatePassword($user['uid'], $password);
168 throw new Exception(L10n::t('Login failed'));
172 * Returns authentication info from various parameters types
174 * User info can be any of the following:
177 * - User email or username or nickname
178 * - User array with at least the uid and the hashed password
180 * @param mixed $user_info
184 private static function getAuthenticationInfo($user_info)
188 if (is_object($user_info) || is_array($user_info)) {
189 if (is_object($user_info)) {
190 $user = (array) $user_info;
195 if (!isset($user['uid'])
196 || !isset($user['password'])
197 || !isset($user['legacy_password'])
199 throw new Exception(L10n::t('Not enough information to authenticate'));
201 } elseif (is_int($user_info) || is_string($user_info)) {
202 if (is_int($user_info)) {
203 $user = DBA::selectFirst('user', ['uid', 'password', 'legacy_password'],
207 'account_expired' => 0,
208 'account_removed' => 0,
213 $fields = ['uid', 'password', 'legacy_password'];
214 $condition = ["(`email` = ? OR `username` = ? OR `nickname` = ?)
215 AND NOT `blocked` AND NOT `account_expired` AND NOT `account_removed` AND `verified`",
216 $user_info, $user_info, $user_info];
217 $user = DBA::selectFirst('user', $fields, $condition);
220 if (!DBA::isResult($user)) {
221 throw new Exception(L10n::t('User not found'));
229 * Generates a human-readable random password
233 public static function generateNewPassword()
235 return autoname(6) . mt_rand(100, 9999);
239 * Checks if the provided plaintext password has been exposed or not
241 * @param string $password
244 public static function isPasswordExposed($password)
246 return password_exposed($password) === PasswordStatus::EXPOSED;
250 * Legacy hashing function, kept for password migration purposes
252 * @param string $password
255 private static function hashPasswordLegacy($password)
257 return hash('whirlpool', $password);
261 * Global user password hashing function
263 * @param string $password
266 public static function hashPassword($password)
268 if (!trim($password)) {
269 throw new Exception(L10n::t('Password can\'t be empty'));
272 return password_hash($password, PASSWORD_DEFAULT);
276 * Updates a user row with a new plaintext password
279 * @param string $password
282 public static function updatePassword($uid, $password)
284 return self::updatePasswordHashed($uid, self::hashPassword($password));
288 * Updates a user row with a new hashed password.
289 * Empties the password reset token field just in case.
292 * @param string $pasword_hashed
295 private static function updatePasswordHashed($uid, $pasword_hashed)
298 'password' => $pasword_hashed,
300 'pwdreset_time' => null,
301 'legacy_password' => false
303 return DBA::update('user', $fields, ['uid' => $uid]);
307 * @brief Checks if a nickname is in the list of the forbidden nicknames
309 * Check if a nickname is forbidden from registration on the node by the
310 * admin. Forbidden nicknames (e.g. role namess) can be configured in the
313 * @param string $nickname The nickname that should be checked
314 * @return boolean True is the nickname is blocked on the node
316 public static function isNicknameBlocked($nickname)
318 $forbidden_nicknames = Config::get('system', 'forbidden_nicknames', '');
320 // if the config variable is empty return false
321 if (empty($forbidden_nicknames)) {
325 // check if the nickname is in the list of blocked nicknames
326 $forbidden = explode(',', $forbidden_nicknames);
327 $forbidden = array_map('trim', $forbidden);
328 if (in_array(strtolower($nickname), $forbidden)) {
337 * @brief Catch-all user creation function
339 * Creates a user from the provided data array, either form fields or OpenID.
340 * Required: { username, nickname, email } or { openid_url }
342 * Performs the following:
343 * - Sends to the OpenId auth URL (if relevant)
344 * - Creates new key pairs for crypto
345 * - Create self-contact
346 * - Create profile image
352 public static function create(array $data)
355 $return = ['user' => null, 'password' => ''];
357 $using_invites = Config::get('system', 'invitation_only');
358 $num_invites = Config::get('system', 'number_invites');
360 $invite_id = !empty($data['invite_id']) ? notags(trim($data['invite_id'])) : '';
361 $username = !empty($data['username']) ? notags(trim($data['username'])) : '';
362 $nickname = !empty($data['nickname']) ? notags(trim($data['nickname'])) : '';
363 $email = !empty($data['email']) ? notags(trim($data['email'])) : '';
364 $openid_url = !empty($data['openid_url']) ? notags(trim($data['openid_url'])) : '';
365 $photo = !empty($data['photo']) ? notags(trim($data['photo'])) : '';
366 $password = !empty($data['password']) ? trim($data['password']) : '';
367 $password1 = !empty($data['password1']) ? trim($data['password1']) : '';
368 $confirm = !empty($data['confirm']) ? trim($data['confirm']) : '';
369 $blocked = !empty($data['blocked']) ? intval($data['blocked']) : 0;
370 $verified = !empty($data['verified']) ? intval($data['verified']) : 0;
371 $language = !empty($data['language']) ? notags(trim($data['language'])) : 'en';
373 $publish = !empty($data['profile_publish_reg']) && intval($data['profile_publish_reg']) ? 1 : 0;
374 $netpublish = strlen(Config::get('system', 'directory')) ? $publish : 0;
376 if ($password1 != $confirm) {
377 throw new Exception(L10n::t('Passwords do not match. Password unchanged.'));
378 } elseif ($password1 != '') {
379 $password = $password1;
382 if ($using_invites) {
384 throw new Exception(L10n::t('An invitation is required.'));
387 if (!DBA::exists('register', ['hash' => $invite_id])) {
388 throw new Exception(L10n::t('Invitation could not be verified.'));
392 if (empty($username) || empty($email) || empty($nickname)) {
394 if (!Network::isUrlValid($openid_url)) {
395 throw new Exception(L10n::t('Invalid OpenID url'));
397 $_SESSION['register'] = 1;
398 $_SESSION['openid'] = $openid_url;
400 $openid = new LightOpenID($a->get_hostname());
401 $openid->identity = $openid_url;
402 $openid->returnUrl = System::baseUrl() . '/openid';
403 $openid->required = ['namePerson/friendly', 'contact/email', 'namePerson'];
404 $openid->optional = ['namePerson/first', 'media/image/aspect11', 'media/image/default'];
406 $authurl = $openid->authUrl();
407 } catch (Exception $e) {
408 throw new Exception(L10n::t('We encountered a problem while logging in with the OpenID you provided. Please check the correct spelling of the ID.') . EOL . EOL . L10n::t('The error message was:') . $e->getMessage(), 0, $e);
414 throw new Exception(L10n::t('Please enter the required information.'));
417 if (!Network::isUrlValid($openid_url)) {
423 // collapse multiple spaces in name
424 $username = preg_replace('/ +/', ' ', $username);
426 if (mb_strlen($username) > 48) {
427 throw new Exception(L10n::t('Please use a shorter name.'));
429 if (mb_strlen($username) < 3) {
430 throw new Exception(L10n::t('Name too short.'));
433 // So now we are just looking for a space in the full name.
434 $loose_reg = Config::get('system', 'no_regfullname');
436 $username = mb_convert_case($username, MB_CASE_TITLE, 'UTF-8');
437 if (!strpos($username, ' ')) {
438 throw new Exception(L10n::t("That doesn't appear to be your full \x28First Last\x29 name."));
442 if (!Network::isEmailDomainAllowed($email)) {
443 throw new Exception(L10n::t('Your email domain is not among those allowed on this site.'));
446 if (!valid_email($email) || !Network::isEmailDomainValid($email)) {
447 throw new Exception(L10n::t('Not a valid email address.'));
449 if (self::isNicknameBlocked($nickname)) {
450 throw new Exception(L10n::t('The nickname was blocked from registration by the nodes admin.'));
453 if (Config::get('system', 'block_extended_register', false) && DBA::exists('user', ['email' => $email])) {
454 throw new Exception(L10n::t('Cannot use that email.'));
457 // Disallow somebody creating an account using openid that uses the admin email address,
458 // since openid bypasses email verification. We'll allow it if there is not yet an admin account.
459 if (Config::get('config', 'admin_email') && strlen($openid_url)) {
460 $adminlist = explode(',', str_replace(' ', '', strtolower(Config::get('config', 'admin_email'))));
461 if (in_array(strtolower($email), $adminlist)) {
462 throw new Exception(L10n::t('Cannot use that email.'));
466 $nickname = $data['nickname'] = strtolower($nickname);
468 if (!preg_match('/^[a-z0-9][a-z0-9\_]*$/', $nickname)) {
469 throw new Exception(L10n::t('Your nickname can only contain a-z, 0-9 and _.'));
472 // Check existing and deleted accounts for this nickname.
473 if (DBA::exists('user', ['nickname' => $nickname])
474 || DBA::exists('userd', ['username' => $nickname])
476 throw new Exception(L10n::t('Nickname is already registered. Please choose another.'));
479 $new_password = strlen($password) ? $password : User::generateNewPassword();
480 $new_password_encoded = self::hashPassword($new_password);
482 $return['password'] = $new_password;
484 $keys = Crypto::newKeypair(4096);
485 if ($keys === false) {
486 throw new Exception(L10n::t('SERIOUS ERROR: Generation of security keys failed.'));
489 $prvkey = $keys['prvkey'];
490 $pubkey = $keys['pubkey'];
492 // Create another keypair for signing/verifying salmon protocol messages.
493 $sres = Crypto::newKeypair(512);
494 $sprvkey = $sres['prvkey'];
495 $spubkey = $sres['pubkey'];
497 $insert_result = DBA::insert('user', [
498 'guid' => System::createGUID(32),
499 'username' => $username,
500 'password' => $new_password_encoded,
502 'openid' => $openid_url,
503 'nickname' => $nickname,
506 'spubkey' => $spubkey,
507 'sprvkey' => $sprvkey,
508 'verified' => $verified,
509 'blocked' => $blocked,
510 'language' => $language,
512 'register_date' => DateTimeFormat::utcNow(),
513 'default-location' => ''
516 if ($insert_result) {
517 $uid = DBA::lastInsertId();
518 $user = DBA::selectFirst('user', [], ['uid' => $uid]);
520 throw new Exception(L10n::t('An error occurred during registration. Please try again.'));
524 throw new Exception(L10n::t('An error occurred during registration. Please try again.'));
527 // if somebody clicked submit twice very quickly, they could end up with two accounts
528 // due to race condition. Remove this one.
529 $user_count = DBA::count('user', ['nickname' => $nickname]);
530 if ($user_count > 1) {
531 DBA::delete('user', ['uid' => $uid]);
533 throw new Exception(L10n::t('Nickname is already registered. Please choose another.'));
536 $insert_result = DBA::insert('profile', [
539 'photo' => System::baseUrl() . "/photo/profile/{$uid}.jpg",
540 'thumb' => System::baseUrl() . "/photo/avatar/{$uid}.jpg",
541 'publish' => $publish,
543 'net-publish' => $netpublish,
544 'profile-name' => L10n::t('default')
546 if (!$insert_result) {
547 DBA::delete('user', ['uid' => $uid]);
549 throw new Exception(L10n::t('An error occurred creating your default profile. Please try again.'));
552 // Create the self contact
553 if (!Contact::createSelfFromUserId($uid)) {
554 DBA::delete('user', ['uid' => $uid]);
556 throw new Exception(L10n::t('An error occurred creating your self contact. Please try again.'));
559 // Create a group with no members. This allows somebody to use it
560 // right away as a default group for new contacts.
561 $def_gid = Group::create($uid, L10n::t('Friends'));
563 DBA::delete('user', ['uid' => $uid]);
565 throw new Exception(L10n::t('An error occurred creating your default contact group. Please try again.'));
568 $fields = ['def_gid' => $def_gid];
569 if (Config::get('system', 'newuser_private') && $def_gid) {
570 $fields['allow_gid'] = '<' . $def_gid . '>';
573 DBA::update('user', $fields, ['uid' => $uid]);
575 // if we have no OpenID photo try to look up an avatar
576 if (!strlen($photo)) {
577 $photo = Network::lookupAvatarByEmail($email);
580 // unless there is no avatar-addon loaded
581 if (strlen($photo)) {
582 $photo_failure = false;
584 $filename = basename($photo);
585 $img_str = Network::fetchUrl($photo, true);
586 // guess mimetype from headers or filename
587 $type = Image::guessType($photo, true);
589 $Image = new Image($img_str, $type);
590 if ($Image->isValid()) {
591 $Image->scaleToSquare(175);
593 $hash = Photo::newResource();
595 $r = Photo::store($Image, $uid, 0, $hash, $filename, L10n::t('Profile Photos'), 4);
598 $photo_failure = true;
601 $Image->scaleDown(80);
603 $r = Photo::store($Image, $uid, 0, $hash, $filename, L10n::t('Profile Photos'), 5);
606 $photo_failure = true;
609 $Image->scaleDown(48);
611 $r = Photo::store($Image, $uid, 0, $hash, $filename, L10n::t('Profile Photos'), 6);
614 $photo_failure = true;
617 if (!$photo_failure) {
618 DBA::update('photo', ['profile' => 1], ['resource-id' => $hash]);
623 Addon::callHooks('register_account', $uid);
625 $return['user'] = $user;
630 * @brief Sends pending registration confiĆmation email
632 * @param string $email
633 * @param string $sitename
634 * @param string $username
635 * @return NULL|boolean from notification() and email() inherited
637 public static function sendRegisterPendingEmail($email, $sitename, $username)
639 $body = deindent(L10n::t('
641 Thank you for registering at %2$s. Your account is pending for approval by the administrator.
644 $body = sprintf($body, $username, $sitename);
646 return notification([
647 'type' => SYSTEM_EMAIL,
648 'to_email' => $email,
649 'subject'=> L10n::t('Registration at %s', $sitename),
654 * @brief Sends registration confirmation
656 * It's here as a function because the mail is sent from different parts
658 * @param string $email
659 * @param string $sitename
660 * @param string $siteurl
661 * @param string $username
662 * @param string $password
663 * @return NULL|boolean from notification() and email() inherited
665 public static function sendRegisterOpenEmail($email, $sitename, $siteurl, $username, $password, $user)
667 $preamble = deindent(L10n::t('
669 Thank you for registering at %2$s. Your account has been created.
671 $body = deindent(L10n::t('
672 The login details are as follows:
678 You may change your password from your account "Settings" page after logging
681 Please take a few moments to review the other account settings on that page.
683 You may also wish to add some basic information to your default profile
684 ' . "\x28" . 'on the "Profiles" page' . "\x29" . ' so that other people can easily find you.
686 We recommend setting your full name, adding a profile photo,
687 adding some profile "keywords" ' . "\x28" . 'very useful in making new friends' . "\x29" . ' - and
688 perhaps what country you live in; if you do not wish to be more specific
691 We fully respect your right to privacy, and none of these items are necessary.
692 If you are new and do not know anybody here, they may help
693 you to make some new and interesting friends.
695 If you ever want to delete your account, you can do so at %3$s/removeme
697 Thank you and welcome to %2$s.'));
699 $preamble = sprintf($preamble, $username, $sitename);
700 $body = sprintf($body, $email, $sitename, $siteurl, $username, $password);
702 return notification([
703 'uid' => $user['uid'],
704 'language' => $user['language'],
705 'type' => SYSTEM_EMAIL,
706 'to_email' => $email,
707 'subject'=> L10n::t('Registration details for %s', $sitename),
708 'preamble'=> $preamble,
713 * @param object $uid user to remove
716 public static function remove($uid)
722 logger('Removing user: ' . $uid);
724 $user = DBA::selectFirst('user', [], ['uid' => $uid]);
726 Addon::callHooks('remove_user', $user);
728 // save username (actually the nickname as it is guaranteed
729 // unique), so it cannot be re-registered in the future.
730 DBA::insert('userd', ['username' => $user['nickname']]);
732 // The user and related data will be deleted in "cron_expire_and_remove_users" (cronjobs.php)
733 DBA::update('user', ['account_removed' => true, 'account_expires_on' => DateTimeFormat::utc(DateTimeFormat::utcNow() . " + 7 day")], ['uid' => $uid]);
734 Worker::add(PRIORITY_HIGH, "Notifier", "removeme", $uid);
736 // Send an update to the directory
737 $self = DBA::selectFirst('contact', ['url'], ['uid' => $uid, 'self' => true]);
738 Worker::add(PRIORITY_LOW, "Directory", $self['url']);
740 // Remove the user relevant data
741 Worker::add(PRIORITY_LOW, "RemoveUser", $uid);
743 if ($uid == local_user()) {
744 unset($_SESSION['authenticated']);
745 unset($_SESSION['uid']);
746 goaway(System::baseUrl());