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 Returns the default group for a given user and network
71 * @param int $uid User id
72 * @param string $network network name
74 * @return int group id
76 public static function getDefaultGroup($uid, $network = '')
80 if ($network == NETWORK_OSTATUS) {
81 $default_group = PConfig::get($uid, "ostatus", "default_group");
84 if ($default_group != 0) {
85 return $default_group;
88 $user = dba::selectFirst('user', ['def_gid'], ['uid' => $uid]);
90 if (DBM::is_result($user)) {
91 $default_group = $user["def_gid"];
94 return $default_group;
99 * Authenticate a user with a clear text password
101 * @brief Authenticate a user with a clear text password
102 * @param mixed $user_info
103 * @param string $password
104 * @return int|boolean
105 * @deprecated since version 3.6
106 * @see User::getIdFromPasswordAuthentication()
108 public static function authenticate($user_info, $password)
111 return self::getIdFromPasswordAuthentication($user_info, $password);
112 } catch (Exception $ex) {
118 * Returns the user id associated with a successful password authentication
120 * @brief Authenticate a user with a clear text password
121 * @param mixed $user_info
122 * @param string $password
123 * @return int User Id if authentication is successful
126 public static function getIdFromPasswordAuthentication($user_info, $password)
128 $user = self::getAuthenticationInfo($user_info);
130 if ($user['legacy_password']) {
131 if (password_verify(self::hashPasswordLegacy($password), $user['password'])) {
132 self::updatePassword($user['uid'], $password);
136 } elseif (password_verify($password, $user['password'])) {
137 if (password_needs_rehash($user['password'], PASSWORD_DEFAULT)) {
138 self::updatePassword($user['uid'], $password);
144 throw new Exception(L10n::t('Login failed'));
148 * Returns authentication info from various parameters types
150 * User info can be any of the following:
153 * - User email or username or nickname
154 * - User array with at least the uid and the hashed password
156 * @param mixed $user_info
160 private static function getAuthenticationInfo($user_info)
164 if (is_object($user_info) || is_array($user_info)) {
165 if (is_object($user_info)) {
166 $user = (array) $user_info;
171 if (!isset($user['uid'])
172 || !isset($user['password'])
173 || !isset($user['legacy_password'])
175 throw new Exception(L10n::t('Not enough information to authenticate'));
177 } elseif (is_int($user_info) || is_string($user_info)) {
178 if (is_int($user_info)) {
179 $user = dba::selectFirst('user', ['uid', 'password', 'legacy_password'],
183 'account_expired' => 0,
184 'account_removed' => 0,
189 $user = dba::fetch_first('SELECT `uid`, `password`, `legacy_password`
191 WHERE (`email` = ? OR `username` = ? OR `nickname` = ?)
193 AND `account_expired` = 0
194 AND `account_removed` = 0
203 if (!DBM::is_result($user)) {
204 throw new Exception(L10n::t('User not found'));
212 * Generates a human-readable random password
216 public static function generateNewPassword()
218 return autoname(6) . mt_rand(100, 9999);
222 * Checks if the provided plaintext password has been exposed or not
224 * @param string $password
227 public static function isPasswordExposed($password)
229 return password_exposed($password) === PasswordStatus::EXPOSED;
233 * Legacy hashing function, kept for password migration purposes
235 * @param string $password
238 private static function hashPasswordLegacy($password)
240 return hash('whirlpool', $password);
244 * Global user password hashing function
246 * @param string $password
249 public static function hashPassword($password)
251 return password_hash($password, PASSWORD_DEFAULT);
255 * Updates a user row with a new plaintext password
258 * @param string $password
261 public static function updatePassword($uid, $password)
263 return self::updatePasswordHashed($uid, self::hashPassword($password));
267 * Updates a user row with a new hashed password.
268 * Empties the password reset token field just in case.
271 * @param string $pasword_hashed
274 private static function updatePasswordHashed($uid, $pasword_hashed)
277 'password' => $pasword_hashed,
279 'pwdreset_time' => null,
280 'legacy_password' => false
282 return dba::update('user', $fields, ['uid' => $uid]);
286 * @brief Catch-all user creation function
288 * Creates a user from the provided data array, either form fields or OpenID.
289 * Required: { username, nickname, email } or { openid_url }
291 * Performs the following:
292 * - Sends to the OpenId auth URL (if relevant)
293 * - Creates new key pairs for crypto
294 * - Create self-contact
295 * - Create profile image
301 public static function create(array $data)
304 $return = ['user' => null, 'password' => ''];
306 $using_invites = Config::get('system', 'invitation_only');
307 $num_invites = Config::get('system', 'number_invites');
309 $invite_id = x($data, 'invite_id') ? notags(trim($data['invite_id'])) : '';
310 $username = x($data, 'username') ? notags(trim($data['username'])) : '';
311 $nickname = x($data, 'nickname') ? notags(trim($data['nickname'])) : '';
312 $email = x($data, 'email') ? notags(trim($data['email'])) : '';
313 $openid_url = x($data, 'openid_url') ? notags(trim($data['openid_url'])) : '';
314 $photo = x($data, 'photo') ? notags(trim($data['photo'])) : '';
315 $password = x($data, 'password') ? trim($data['password']) : '';
316 $password1 = x($data, 'password1') ? trim($data['password1']) : '';
317 $confirm = x($data, 'confirm') ? trim($data['confirm']) : '';
318 $blocked = x($data, 'blocked') ? intval($data['blocked']) : 0;
319 $verified = x($data, 'verified') ? intval($data['verified']) : 0;
321 $publish = x($data, 'profile_publish_reg') && intval($data['profile_publish_reg']) ? 1 : 0;
322 $netpublish = strlen(Config::get('system', 'directory')) ? $publish : 0;
324 if ($password1 != $confirm) {
325 throw new Exception(L10n::t('Passwords do not match. Password unchanged.'));
326 } elseif ($password1 != '') {
327 $password = $password1;
330 if ($using_invites) {
332 throw new Exception(L10n::t('An invitation is required.'));
335 if (!dba::exists('register', ['hash' => $invite_id])) {
336 throw new Exception(L10n::t('Invitation could not be verified.'));
340 if (!x($username) || !x($email) || !x($nickname)) {
342 if (!Network::isUrlValid($openid_url)) {
343 throw new Exception(L10n::t('Invalid OpenID url'));
345 $_SESSION['register'] = 1;
346 $_SESSION['openid'] = $openid_url;
348 $openid = new LightOpenID;
349 $openid->identity = $openid_url;
350 $openid->returnUrl = System::baseUrl() . '/openid';
351 $openid->required = ['namePerson/friendly', 'contact/email', 'namePerson'];
352 $openid->optional = ['namePerson/first', 'media/image/aspect11', 'media/image/default'];
354 $authurl = $openid->authUrl();
355 } catch (Exception $e) {
356 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);
362 throw new Exception(L10n::t('Please enter the required information.'));
365 if (!Network::isUrlValid($openid_url)) {
371 // collapse multiple spaces in name
372 $username = preg_replace('/ +/', ' ', $username);
374 if (mb_strlen($username) > 48) {
375 throw new Exception(L10n::t('Please use a shorter name.'));
377 if (mb_strlen($username) < 3) {
378 throw new Exception(L10n::t('Name too short.'));
381 // So now we are just looking for a space in the full name.
382 $loose_reg = Config::get('system', 'no_regfullname');
384 $username = mb_convert_case($username, MB_CASE_TITLE, 'UTF-8');
385 if (!strpos($username, ' ')) {
386 throw new Exception(L10n::t("That doesn't appear to be your full \x28First Last\x29 name."));
390 if (!Network::isEmailDomainAllowed($email)) {
391 throw new Exception(L10n::t('Your email domain is not among those allowed on this site.'));
394 if (!valid_email($email) || !Network::isEmailDomainValid($email)) {
395 throw new Exception(L10n::t('Not a valid email address.'));
398 if (dba::exists('user', ['email' => $email])) {
399 throw new Exception(L10n::t('Cannot use that email.'));
402 // Disallow somebody creating an account using openid that uses the admin email address,
403 // since openid bypasses email verification. We'll allow it if there is not yet an admin account.
404 if (x($a->config, 'admin_email') && strlen($openid_url)) {
405 $adminlist = explode(',', str_replace(' ', '', strtolower($a->config['admin_email'])));
406 if (in_array(strtolower($email), $adminlist)) {
407 throw new Exception(L10n::t('Cannot use that email.'));
411 $nickname = $data['nickname'] = strtolower($nickname);
413 if (!preg_match('/^[a-z0-9][a-z0-9\_]*$/', $nickname)) {
414 throw new Exception(L10n::t('Your nickname can only contain a-z, 0-9 and _.'));
417 // Check existing and deleted accounts for this nickname.
418 if (dba::exists('user', ['nickname' => $nickname])
419 || dba::exists('userd', ['username' => $nickname])
421 throw new Exception(L10n::t('Nickname is already registered. Please choose another.'));
424 $new_password = strlen($password) ? $password : User::generateNewPassword();
425 $new_password_encoded = self::hashPassword($new_password);
427 $return['password'] = $new_password;
429 $keys = Crypto::newKeypair(4096);
430 if ($keys === false) {
431 throw new Exception(L10n::t('SERIOUS ERROR: Generation of security keys failed.'));
434 $prvkey = $keys['prvkey'];
435 $pubkey = $keys['pubkey'];
437 // Create another keypair for signing/verifying salmon protocol messages.
438 $sres = Crypto::newKeypair(512);
439 $sprvkey = $sres['prvkey'];
440 $spubkey = $sres['pubkey'];
442 $insert_result = dba::insert('user', [
443 'guid' => generate_user_guid(),
444 'username' => $username,
445 'password' => $new_password_encoded,
447 'openid' => $openid_url,
448 'nickname' => $nickname,
451 'spubkey' => $spubkey,
452 'sprvkey' => $sprvkey,
453 'verified' => $verified,
454 'blocked' => $blocked,
456 'register_date' => DateTimeFormat::utcNow(),
457 'default-location' => ''
460 if ($insert_result) {
461 $uid = dba::lastInsertId();
462 $user = dba::selectFirst('user', [], ['uid' => $uid]);
464 throw new Exception(L10n::t('An error occurred during registration. Please try again.'));
468 throw new Exception(L10n::t('An error occurred during registration. Please try again.'));
471 // if somebody clicked submit twice very quickly, they could end up with two accounts
472 // due to race condition. Remove this one.
473 $user_count = dba::count('user', ['nickname' => $nickname]);
474 if ($user_count > 1) {
475 dba::delete('user', ['uid' => $uid]);
477 throw new Exception(L10n::t('Nickname is already registered. Please choose another.'));
480 $insert_result = dba::insert('profile', [
483 'photo' => System::baseUrl() . "/photo/profile/{$uid}.jpg",
484 'thumb' => System::baseUrl() . "/photo/avatar/{$uid}.jpg",
485 'publish' => $publish,
487 'net-publish' => $netpublish,
488 'profile-name' => L10n::t('default')
490 if (!$insert_result) {
491 dba::delete('user', ['uid' => $uid]);
493 throw new Exception(L10n::t('An error occurred creating your default profile. Please try again.'));
496 // Create the self contact
497 if (!Contact::createSelfFromUserId($uid)) {
498 dba::delete('user', ['uid' => $uid]);
500 throw new Exception(L10n::t('An error occurred creating your self contact. Please try again.'));
503 // Create a group with no members. This allows somebody to use it
504 // right away as a default group for new contacts.
505 $def_gid = Group::create($uid, L10n::t('Friends'));
507 dba::delete('user', ['uid' => $uid]);
509 throw new Exception(L10n::t('An error occurred creating your default contact group. Please try again.'));
512 $fields = ['def_gid' => $def_gid];
513 if (Config::get('system', 'newuser_private') && $def_gid) {
514 $fields['allow_gid'] = '<' . $def_gid . '>';
517 dba::update('user', $fields, ['uid' => $uid]);
519 // if we have no OpenID photo try to look up an avatar
520 if (!strlen($photo)) {
521 $photo = Network::lookupAvatarByEmail($email);
524 // unless there is no avatar-addon loaded
525 if (strlen($photo)) {
526 $photo_failure = false;
528 $filename = basename($photo);
529 $img_str = Network::fetchUrl($photo, true);
530 // guess mimetype from headers or filename
531 $type = Image::guessType($photo, true);
533 $Image = new Image($img_str, $type);
534 if ($Image->isValid()) {
535 $Image->scaleToSquare(175);
537 $hash = Photo::newResource();
539 $r = Photo::store($Image, $uid, 0, $hash, $filename, L10n::t('Profile Photos'), 4);
542 $photo_failure = true;
545 $Image->scaleDown(80);
547 $r = Photo::store($Image, $uid, 0, $hash, $filename, L10n::t('Profile Photos'), 5);
550 $photo_failure = true;
553 $Image->scaleDown(48);
555 $r = Photo::store($Image, $uid, 0, $hash, $filename, L10n::t('Profile Photos'), 6);
558 $photo_failure = true;
561 if (!$photo_failure) {
562 dba::update('photo', ['profile' => 1], ['resource-id' => $hash]);
567 Addon::callHooks('register_account', $uid);
569 $return['user'] = $user;
574 * @brief Sends pending registration confiĆmation email
576 * @param string $email
577 * @param string $sitename
578 * @param string $username
579 * @return NULL|boolean from notification() and email() inherited
581 public static function sendRegisterPendingEmail($email, $sitename, $username)
583 $body = deindent(L10n::t('
585 Thank you for registering at %2$s. Your account is pending for approval by the administrator.
588 $body = sprintf($body, $username, $sitename);
590 return notification([
591 'type' => SYSTEM_EMAIL,
592 'to_email' => $email,
593 'subject'=> L10n::t('Registration at %s', $sitename),
598 * @brief Sends registration confirmation
600 * It's here as a function because the mail is sent from different parts
602 * @param string $email
603 * @param string $sitename
604 * @param string $siteurl
605 * @param string $username
606 * @param string $password
607 * @return NULL|boolean from notification() and email() inherited
609 public static function sendRegisterOpenEmail($email, $sitename, $siteurl, $username, $password)
611 $preamble = deindent(L10n::t('
613 Thank you for registering at %2$s. Your account has been created.
615 $body = deindent(L10n::t('
616 The login details are as follows:
621 You may change your password from your account Settings page after logging
624 Please take a few moments to review the other account settings on that page.
626 You may also wish to add some basic information to your default profile
627 ' . "\x28" . 'on the "Profiles" page' . "\x29" . ' so that other people can easily find you.
629 We recommend setting your full name, adding a profile photo,
630 adding some profile keywords ' . "\x28" . 'very useful in making new friends' . "\x29" . ' - and
631 perhaps what country you live in; if you do not wish to be more specific
634 We fully respect your right to privacy, and none of these items are necessary.
635 If you are new and do not know anybody here, they may help
636 you to make some new and interesting friends.
639 Thank you and welcome to %2$s.'));
641 $preamble = sprintf($preamble, $username, $sitename);
642 $body = sprintf($body, $email, $sitename, $siteurl, $username, $password);
644 return notification([
645 'type' => SYSTEM_EMAIL,
646 'to_email' => $email,
647 'subject'=> L10n::t('Registration details for %s', $sitename),
648 'preamble'=> $preamble,
653 * @param object $uid user to remove
656 public static function remove($uid)
662 logger('Removing user: ' . $uid);
664 $user = dba::selectFirst('user', [], ['uid' => $uid]);
666 Addon::callHooks('remove_user', $user);
668 // save username (actually the nickname as it is guaranteed
669 // unique), so it cannot be re-registered in the future.
670 dba::insert('userd', ['username' => $user['nickname']]);
672 // The user and related data will be deleted in "cron_expire_and_remove_users" (cronjobs.php)
673 dba::update('user', ['account_removed' => true, 'account_expires_on' => DateTimeFormat::utcNow()], ['uid' => $uid]);
674 Worker::add(PRIORITY_HIGH, "Notifier", "removeme", $uid);
676 // Send an update to the directory
677 Worker::add(PRIORITY_LOW, "Directory", $user['url']);
679 if ($uid == local_user()) {
680 unset($_SESSION['authenticated']);
681 unset($_SESSION['uid']);
682 goaway(System::baseUrl());