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;
9 use Friendica\Core\Addon;
10 use Friendica\Core\Config;
11 use Friendica\Core\L10n;
12 use Friendica\Core\PConfig;
13 use Friendica\Core\System;
14 use Friendica\Core\Worker;
15 use Friendica\Database\DBM;
16 use Friendica\Model\Contact;
17 use Friendica\Model\Group;
18 use Friendica\Model\Photo;
19 use Friendica\Object\Image;
20 use Friendica\Util\Crypto;
21 use Friendica\Util\DateTimeFormat;
22 use Friendica\Util\Network;
26 use function password_exposed;
28 require_once 'boot.php';
29 require_once 'include/dba.php';
30 require_once 'include/enotify.php';
31 require_once 'include/text.php';
33 * @brief This class handles User related functions
38 * @brief Get owner data by user id
41 * @return boolean|array
43 public static function getOwnerDataById($uid) {
44 $r = dba::fetch_first("SELECT
46 `user`.`prvkey` AS `uprvkey`,
52 `user`.`account-type`,
56 ON `user`.`uid` = `contact`.`uid`
57 WHERE `contact`.`uid` = ?
62 if (!DBM::is_result($r)) {
69 * @brief Get owner data by nick name
72 * @return boolean|array
74 public static function getOwnerDataByNick($nick)
76 $user = dba::selectFirst('user', ['uid'], ['nickname' => $nick]);
77 if (!DBM::is_result($user)) {
80 return self::getOwnerDataById($user['uid']);
84 * @brief Returns the default group for a given user and network
86 * @param int $uid User id
87 * @param string $network network name
89 * @return int group id
91 public static function getDefaultGroup($uid, $network = '')
95 if ($network == NETWORK_OSTATUS) {
96 $default_group = PConfig::get($uid, "ostatus", "default_group");
99 if ($default_group != 0) {
100 return $default_group;
103 $user = dba::selectFirst('user', ['def_gid'], ['uid' => $uid]);
105 if (DBM::is_result($user)) {
106 $default_group = $user["def_gid"];
109 return $default_group;
114 * Authenticate a user with a clear text password
116 * @brief Authenticate a user with a clear text password
117 * @param mixed $user_info
118 * @param string $password
119 * @return int|boolean
120 * @deprecated since version 3.6
121 * @see User::getIdFromPasswordAuthentication()
123 public static function authenticate($user_info, $password)
126 return self::getIdFromPasswordAuthentication($user_info, $password);
127 } catch (Exception $ex) {
133 * Returns the user id associated with a successful password authentication
135 * @brief Authenticate a user with a clear text password
136 * @param mixed $user_info
137 * @param string $password
138 * @return int User Id if authentication is successful
141 public static function getIdFromPasswordAuthentication($user_info, $password)
143 $user = self::getAuthenticationInfo($user_info);
145 if (strpos($user['password'], '$') === false) {
146 //Legacy hash that has not been replaced by a new hash yet
147 if (self::hashPasswordLegacy($password) === $user['password']) {
148 self::updatePassword($user['uid'], $password);
152 } elseif (!empty($user['legacy_password'])) {
153 //Legacy hash that has been double-hashed and not replaced by a new hash yet
154 //Warning: `legacy_password` is not necessary in sync with the content of `password`
155 if (password_verify(self::hashPasswordLegacy($password), $user['password'])) {
156 self::updatePassword($user['uid'], $password);
160 } elseif (password_verify($password, $user['password'])) {
162 if (password_needs_rehash($user['password'], PASSWORD_DEFAULT)) {
163 self::updatePassword($user['uid'], $password);
169 throw new Exception(L10n::t('Login failed'));
173 * Returns authentication info from various parameters types
175 * User info can be any of the following:
178 * - User email or username or nickname
179 * - User array with at least the uid and the hashed password
181 * @param mixed $user_info
185 private static function getAuthenticationInfo($user_info)
189 if (is_object($user_info) || is_array($user_info)) {
190 if (is_object($user_info)) {
191 $user = (array) $user_info;
196 if (!isset($user['uid'])
197 || !isset($user['password'])
198 || !isset($user['legacy_password'])
200 throw new Exception(L10n::t('Not enough information to authenticate'));
202 } elseif (is_int($user_info) || is_string($user_info)) {
203 if (is_int($user_info)) {
204 $user = dba::selectFirst('user', ['uid', 'password', 'legacy_password'],
208 'account_expired' => 0,
209 'account_removed' => 0,
214 $fields = ['uid', 'password', 'legacy_password'];
215 $condition = ["(`email` = ? OR `username` = ? OR `nickname` = ?)
216 AND NOT `blocked` AND NOT `account_expired` AND NOT `account_removed` AND `verified`",
217 $user_info, $user_info, $user_info];
218 $user = dba::selectFirst('user', $fields, $condition);
221 if (!DBM::is_result($user)) {
222 throw new Exception(L10n::t('User not found'));
230 * Generates a human-readable random password
234 public static function generateNewPassword()
236 return autoname(6) . mt_rand(100, 9999);
240 * Checks if the provided plaintext password has been exposed or not
242 * @param string $password
245 public static function isPasswordExposed($password)
247 return password_exposed($password) === PasswordStatus::EXPOSED;
251 * Legacy hashing function, kept for password migration purposes
253 * @param string $password
256 private static function hashPasswordLegacy($password)
258 return hash('whirlpool', $password);
262 * Global user password hashing function
264 * @param string $password
267 public static function hashPassword($password)
269 if (!trim($password)) {
270 throw new Exception(L10n::t('Password can\'t be empty'));
273 return password_hash($password, PASSWORD_DEFAULT);
277 * Updates a user row with a new plaintext password
280 * @param string $password
283 public static function updatePassword($uid, $password)
285 return self::updatePasswordHashed($uid, self::hashPassword($password));
289 * Updates a user row with a new hashed password.
290 * Empties the password reset token field just in case.
293 * @param string $pasword_hashed
296 private static function updatePasswordHashed($uid, $pasword_hashed)
299 'password' => $pasword_hashed,
301 'pwdreset_time' => null,
302 'legacy_password' => false
304 return dba::update('user', $fields, ['uid' => $uid]);
308 * @brief Catch-all user creation function
310 * Creates a user from the provided data array, either form fields or OpenID.
311 * Required: { username, nickname, email } or { openid_url }
313 * Performs the following:
314 * - Sends to the OpenId auth URL (if relevant)
315 * - Creates new key pairs for crypto
316 * - Create self-contact
317 * - Create profile image
323 public static function create(array $data)
326 $return = ['user' => null, 'password' => ''];
328 $using_invites = Config::get('system', 'invitation_only');
329 $num_invites = Config::get('system', 'number_invites');
331 $invite_id = x($data, 'invite_id') ? notags(trim($data['invite_id'])) : '';
332 $username = x($data, 'username') ? notags(trim($data['username'])) : '';
333 $nickname = x($data, 'nickname') ? notags(trim($data['nickname'])) : '';
334 $email = x($data, 'email') ? notags(trim($data['email'])) : '';
335 $openid_url = x($data, 'openid_url') ? notags(trim($data['openid_url'])) : '';
336 $photo = x($data, 'photo') ? notags(trim($data['photo'])) : '';
337 $password = x($data, 'password') ? trim($data['password']) : '';
338 $password1 = x($data, 'password1') ? trim($data['password1']) : '';
339 $confirm = x($data, 'confirm') ? trim($data['confirm']) : '';
340 $blocked = x($data, 'blocked') ? intval($data['blocked']) : 0;
341 $verified = x($data, 'verified') ? intval($data['verified']) : 0;
342 $language = x($data, 'language') ? notags(trim($data['language'])) : 'en';
344 $publish = x($data, 'profile_publish_reg') && intval($data['profile_publish_reg']) ? 1 : 0;
345 $netpublish = strlen(Config::get('system', 'directory')) ? $publish : 0;
347 if ($password1 != $confirm) {
348 throw new Exception(L10n::t('Passwords do not match. Password unchanged.'));
349 } elseif ($password1 != '') {
350 $password = $password1;
353 if ($using_invites) {
355 throw new Exception(L10n::t('An invitation is required.'));
358 if (!dba::exists('register', ['hash' => $invite_id])) {
359 throw new Exception(L10n::t('Invitation could not be verified.'));
363 if (!x($username) || !x($email) || !x($nickname)) {
365 if (!Network::isUrlValid($openid_url)) {
366 throw new Exception(L10n::t('Invalid OpenID url'));
368 $_SESSION['register'] = 1;
369 $_SESSION['openid'] = $openid_url;
371 $openid = new LightOpenID($a->get_hostname());
372 $openid->identity = $openid_url;
373 $openid->returnUrl = System::baseUrl() . '/openid';
374 $openid->required = ['namePerson/friendly', 'contact/email', 'namePerson'];
375 $openid->optional = ['namePerson/first', 'media/image/aspect11', 'media/image/default'];
377 $authurl = $openid->authUrl();
378 } catch (Exception $e) {
379 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);
385 throw new Exception(L10n::t('Please enter the required information.'));
388 if (!Network::isUrlValid($openid_url)) {
394 // collapse multiple spaces in name
395 $username = preg_replace('/ +/', ' ', $username);
397 if (mb_strlen($username) > 48) {
398 throw new Exception(L10n::t('Please use a shorter name.'));
400 if (mb_strlen($username) < 3) {
401 throw new Exception(L10n::t('Name too short.'));
404 // So now we are just looking for a space in the full name.
405 $loose_reg = Config::get('system', 'no_regfullname');
407 $username = mb_convert_case($username, MB_CASE_TITLE, 'UTF-8');
408 if (!strpos($username, ' ')) {
409 throw new Exception(L10n::t("That doesn't appear to be your full \x28First Last\x29 name."));
413 if (!Network::isEmailDomainAllowed($email)) {
414 throw new Exception(L10n::t('Your email domain is not among those allowed on this site.'));
417 if (!valid_email($email) || !Network::isEmailDomainValid($email)) {
418 throw new Exception(L10n::t('Not a valid email address.'));
421 if (Config::get('system', 'block_extended_register', false) && dba::exists('user', ['email' => $email])) {
422 throw new Exception(L10n::t('Cannot use that email.'));
425 // Disallow somebody creating an account using openid that uses the admin email address,
426 // since openid bypasses email verification. We'll allow it if there is not yet an admin account.
427 if (x($a->config, 'admin_email') && strlen($openid_url)) {
428 $adminlist = explode(',', str_replace(' ', '', strtolower($a->config['admin_email'])));
429 if (in_array(strtolower($email), $adminlist)) {
430 throw new Exception(L10n::t('Cannot use that email.'));
434 $nickname = $data['nickname'] = strtolower($nickname);
436 if (!preg_match('/^[a-z0-9][a-z0-9\_]*$/', $nickname)) {
437 throw new Exception(L10n::t('Your nickname can only contain a-z, 0-9 and _.'));
440 // Check existing and deleted accounts for this nickname.
441 if (dba::exists('user', ['nickname' => $nickname])
442 || dba::exists('userd', ['username' => $nickname])
444 throw new Exception(L10n::t('Nickname is already registered. Please choose another.'));
447 $new_password = strlen($password) ? $password : User::generateNewPassword();
448 $new_password_encoded = self::hashPassword($new_password);
450 $return['password'] = $new_password;
452 $keys = Crypto::newKeypair(4096);
453 if ($keys === false) {
454 throw new Exception(L10n::t('SERIOUS ERROR: Generation of security keys failed.'));
457 $prvkey = $keys['prvkey'];
458 $pubkey = $keys['pubkey'];
460 // Create another keypair for signing/verifying salmon protocol messages.
461 $sres = Crypto::newKeypair(512);
462 $sprvkey = $sres['prvkey'];
463 $spubkey = $sres['pubkey'];
465 $insert_result = dba::insert('user', [
466 'guid' => generate_user_guid(),
467 'username' => $username,
468 'password' => $new_password_encoded,
470 'openid' => $openid_url,
471 'nickname' => $nickname,
474 'spubkey' => $spubkey,
475 'sprvkey' => $sprvkey,
476 'verified' => $verified,
477 'blocked' => $blocked,
478 'language' => $language,
480 'register_date' => DateTimeFormat::utcNow(),
481 'default-location' => ''
484 if ($insert_result) {
485 $uid = dba::lastInsertId();
486 $user = dba::selectFirst('user', [], ['uid' => $uid]);
488 throw new Exception(L10n::t('An error occurred during registration. Please try again.'));
492 throw new Exception(L10n::t('An error occurred during registration. Please try again.'));
495 // if somebody clicked submit twice very quickly, they could end up with two accounts
496 // due to race condition. Remove this one.
497 $user_count = dba::count('user', ['nickname' => $nickname]);
498 if ($user_count > 1) {
499 dba::delete('user', ['uid' => $uid]);
501 throw new Exception(L10n::t('Nickname is already registered. Please choose another.'));
504 $insert_result = dba::insert('profile', [
507 'photo' => System::baseUrl() . "/photo/profile/{$uid}.jpg",
508 'thumb' => System::baseUrl() . "/photo/avatar/{$uid}.jpg",
509 'publish' => $publish,
511 'net-publish' => $netpublish,
512 'profile-name' => L10n::t('default')
514 if (!$insert_result) {
515 dba::delete('user', ['uid' => $uid]);
517 throw new Exception(L10n::t('An error occurred creating your default profile. Please try again.'));
520 // Create the self contact
521 if (!Contact::createSelfFromUserId($uid)) {
522 dba::delete('user', ['uid' => $uid]);
524 throw new Exception(L10n::t('An error occurred creating your self contact. Please try again.'));
527 // Create a group with no members. This allows somebody to use it
528 // right away as a default group for new contacts.
529 $def_gid = Group::create($uid, L10n::t('Friends'));
531 dba::delete('user', ['uid' => $uid]);
533 throw new Exception(L10n::t('An error occurred creating your default contact group. Please try again.'));
536 $fields = ['def_gid' => $def_gid];
537 if (Config::get('system', 'newuser_private') && $def_gid) {
538 $fields['allow_gid'] = '<' . $def_gid . '>';
541 dba::update('user', $fields, ['uid' => $uid]);
543 // if we have no OpenID photo try to look up an avatar
544 if (!strlen($photo)) {
545 $photo = Network::lookupAvatarByEmail($email);
548 // unless there is no avatar-addon loaded
549 if (strlen($photo)) {
550 $photo_failure = false;
552 $filename = basename($photo);
553 $img_str = Network::fetchUrl($photo, true);
554 // guess mimetype from headers or filename
555 $type = Image::guessType($photo, true);
557 $Image = new Image($img_str, $type);
558 if ($Image->isValid()) {
559 $Image->scaleToSquare(175);
561 $hash = Photo::newResource();
563 $r = Photo::store($Image, $uid, 0, $hash, $filename, L10n::t('Profile Photos'), 4);
566 $photo_failure = true;
569 $Image->scaleDown(80);
571 $r = Photo::store($Image, $uid, 0, $hash, $filename, L10n::t('Profile Photos'), 5);
574 $photo_failure = true;
577 $Image->scaleDown(48);
579 $r = Photo::store($Image, $uid, 0, $hash, $filename, L10n::t('Profile Photos'), 6);
582 $photo_failure = true;
585 if (!$photo_failure) {
586 dba::update('photo', ['profile' => 1], ['resource-id' => $hash]);
591 Addon::callHooks('register_account', $uid);
593 $return['user'] = $user;
598 * @brief Sends pending registration confiĆmation email
600 * @param string $email
601 * @param string $sitename
602 * @param string $username
603 * @return NULL|boolean from notification() and email() inherited
605 public static function sendRegisterPendingEmail($email, $sitename, $username)
607 $body = deindent(L10n::t('
609 Thank you for registering at %2$s. Your account is pending for approval by the administrator.
612 $body = sprintf($body, $username, $sitename);
614 return notification([
615 'type' => SYSTEM_EMAIL,
616 'to_email' => $email,
617 'subject'=> L10n::t('Registration at %s', $sitename),
622 * @brief Sends registration confirmation
624 * It's here as a function because the mail is sent from different parts
626 * @param string $email
627 * @param string $sitename
628 * @param string $siteurl
629 * @param string $username
630 * @param string $password
631 * @return NULL|boolean from notification() and email() inherited
633 public static function sendRegisterOpenEmail($email, $sitename, $siteurl, $username, $password)
635 $preamble = deindent(L10n::t('
637 Thank you for registering at %2$s. Your account has been created.
639 $body = deindent(L10n::t('
640 The login details are as follows:
646 You may change your password from your account "Settings" page after logging
649 Please take a few moments to review the other account settings on that page.
651 You may also wish to add some basic information to your default profile
652 ' . "\x28" . 'on the "Profiles" page' . "\x29" . ' so that other people can easily find you.
654 We recommend setting your full name, adding a profile photo,
655 adding some profile "keywords" ' . "\x28" . 'very useful in making new friends' . "\x29" . ' - and
656 perhaps what country you live in; if you do not wish to be more specific
659 We fully respect your right to privacy, and none of these items are necessary.
660 If you are new and do not know anybody here, they may help
661 you to make some new and interesting friends.
663 If you ever want to delete your account, you can do so at %3$s/removeme
665 Thank you and welcome to %2$s.'));
667 $preamble = sprintf($preamble, $username, $sitename);
668 $body = sprintf($body, $email, $sitename, $siteurl, $username, $password);
670 return notification([
671 'type' => SYSTEM_EMAIL,
672 'to_email' => $email,
673 'subject'=> L10n::t('Registration details for %s', $sitename),
674 'preamble'=> $preamble,
679 * @param object $uid user to remove
682 public static function remove($uid)
688 logger('Removing user: ' . $uid);
690 $user = dba::selectFirst('user', [], ['uid' => $uid]);
692 Addon::callHooks('remove_user', $user);
694 // save username (actually the nickname as it is guaranteed
695 // unique), so it cannot be re-registered in the future.
696 dba::insert('userd', ['username' => $user['nickname']]);
698 // The user and related data will be deleted in "cron_expire_and_remove_users" (cronjobs.php)
699 dba::update('user', ['account_removed' => true, 'account_expires_on' => DateTimeFormat::utcNow()], ['uid' => $uid]);
700 Worker::add(PRIORITY_HIGH, "Notifier", "removeme", $uid);
702 // Send an update to the directory
703 Worker::add(PRIORITY_LOW, "Directory", $user['url']);
705 if ($uid == local_user()) {
706 unset($_SESSION['authenticated']);
707 unset($_SESSION['uid']);
708 goaway(System::baseUrl());