4 * @file src/Model/User.php
5 * @brief This file includes the User class with user related database functions
8 namespace Friendica\Model;
10 use Friendica\Core\Addon;
11 use Friendica\Core\Config;
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;
24 require_once 'boot.php';
25 require_once 'include/dba.php';
26 require_once 'include/enotify.php';
27 require_once 'include/network.php';
28 require_once 'library/openid.php';
29 require_once 'include/pgettext.php';
30 require_once 'include/text.php';
32 * @brief This class handles User related functions
37 * @brief Get owner data by user id
40 * @return boolean|array
42 public static function getOwnerDataById($uid) {
43 $r = dba::fetch_first("SELECT
45 `user`.`prvkey` AS `uprvkey`,
51 `user`.`account-type`,
55 ON `user`.`uid` = `contact`.`uid`
56 WHERE `contact`.`uid` = ?
61 if (!DBM::is_result($r)) {
68 * @brief Returns the default group for a given user and network
70 * @param int $uid User id
71 * @param string $network network name
73 * @return int group id
75 public static function getDefaultGroup($uid, $network = '')
79 if ($network == NETWORK_OSTATUS) {
80 $default_group = PConfig::get($uid, "ostatus", "default_group");
83 if ($default_group != 0) {
84 return $default_group;
87 $user = dba::selectFirst('user', ['def_gid'], ['uid' => $uid]);
89 if (DBM::is_result($user)) {
90 $default_group = $user["def_gid"];
93 return $default_group;
98 * @brief Authenticate a user with a clear text password
100 * User info can be any of the following:
103 * - User email or username or nickname
104 * - User array with at least the uid and the hashed password
106 * @param mixed $user_info
107 * @param string $password
110 public static function authenticate($user_info, $password)
112 if (is_object($user_info)) {
113 $user = (array) $user_info;
114 } elseif (is_int($user_info)) {
115 $user = dba::selectFirst('user', ['uid', 'password'],
119 'account_expired' => 0,
120 'account_removed' => 0,
124 } elseif (is_string($user_info)) {
125 $user = dba::fetch_first('SELECT `uid`, `password`
127 WHERE (`email` = ? OR `username` = ? OR `nickname` = ?)
129 AND `account_expired` = 0
130 AND `account_removed` = 0
141 if (!DBM::is_result($user) || !isset($user['uid']) || !isset($user['password'])) {
145 $password_hashed = self::hashPassword($password);
147 if ($password_hashed !== $user['password']) {
155 * Generates a human-readable random password
159 public static function generateNewPassword()
161 return autoname(6) . mt_rand(100, 9999);
165 * Global user password hashing function
167 * @param string $password
170 private static function hashPassword($password)
172 return hash('whirlpool', $password);
176 * Updates a user row with a new plaintext password
179 * @param string $password
182 public static function updatePassword($uid, $password)
184 return self::updatePasswordHashed($uid, self::hashPassword($password));
188 * Updates a user row with a new hashed password.
189 * Empties the password reset token field just in case.
192 * @param string $pasword_hashed
195 private static function updatePasswordHashed($uid, $pasword_hashed)
197 return dba::update('user', ['password' => $pasword_hashed, 'pwdreset' => ''], ['uid' => $uid]);
201 * @brief Catch-all user creation function
203 * Creates a user from the provided data array, either form fields or OpenID.
204 * Required: { username, nickname, email } or { openid_url }
206 * Performs the following:
207 * - Sends to the OpenId auth URL (if relevant)
208 * - Creates new key pairs for crypto
209 * - Create self-contact
210 * - Create profile image
216 public static function create(array $data)
219 $return = ['user' => null, 'password' => ''];
221 $using_invites = Config::get('system', 'invitation_only');
222 $num_invites = Config::get('system', 'number_invites');
224 $invite_id = x($data, 'invite_id') ? notags(trim($data['invite_id'])) : '';
225 $username = x($data, 'username') ? notags(trim($data['username'])) : '';
226 $nickname = x($data, 'nickname') ? notags(trim($data['nickname'])) : '';
227 $email = x($data, 'email') ? notags(trim($data['email'])) : '';
228 $openid_url = x($data, 'openid_url') ? notags(trim($data['openid_url'])) : '';
229 $photo = x($data, 'photo') ? notags(trim($data['photo'])) : '';
230 $password = x($data, 'password') ? trim($data['password']) : '';
231 $password1 = x($data, 'password1') ? trim($data['password1']) : '';
232 $confirm = x($data, 'confirm') ? trim($data['confirm']) : '';
233 $blocked = x($data, 'blocked') ? intval($data['blocked']) : 0;
234 $verified = x($data, 'verified') ? intval($data['verified']) : 0;
236 $publish = x($data, 'profile_publish_reg') && intval($data['profile_publish_reg']) ? 1 : 0;
237 $netpublish = strlen(Config::get('system', 'directory')) ? $publish : 0;
239 if ($password1 != $confirm) {
240 throw new Exception(t('Passwords do not match. Password unchanged.'));
241 } elseif ($password1 != '') {
242 $password = $password1;
245 if ($using_invites) {
247 throw new Exception(t('An invitation is required.'));
250 if (!dba::exists('register', ['hash' => $invite_id])) {
251 throw new Exception(t('Invitation could not be verified.'));
255 if (!x($username) || !x($email) || !x($nickname)) {
257 if (!validate_url($openid_url)) {
258 throw new Exception(t('Invalid OpenID url'));
260 $_SESSION['register'] = 1;
261 $_SESSION['openid'] = $openid_url;
263 $openid = new \LightOpenID;
264 $openid->identity = $openid_url;
265 $openid->returnUrl = System::baseUrl() . '/openid';
266 $openid->required = ['namePerson/friendly', 'contact/email', 'namePerson'];
267 $openid->optional = ['namePerson/first', 'media/image/aspect11', 'media/image/default'];
269 $authurl = $openid->authUrl();
270 } catch (Exception $e) {
271 throw new Exception(t('We encountered a problem while logging in with the OpenID you provided. Please check the correct spelling of the ID.') . EOL . EOL . t('The error message was:') . $e->getMessage(), 0, $e);
277 throw new Exception(t('Please enter the required information.'));
280 if (!validate_url($openid_url)) {
286 // collapse multiple spaces in name
287 $username = preg_replace('/ +/', ' ', $username);
289 if (mb_strlen($username) > 48) {
290 throw new Exception(t('Please use a shorter name.'));
292 if (mb_strlen($username) < 3) {
293 throw new Exception(t('Name too short.'));
296 // So now we are just looking for a space in the full name.
297 $loose_reg = Config::get('system', 'no_regfullname');
299 $username = mb_convert_case($username, MB_CASE_TITLE, 'UTF-8');
300 if (!strpos($username, ' ')) {
301 throw new Exception(t("That doesn't appear to be your full \x28First Last\x29 name."));
305 if (!allowed_email($email)) {
306 throw new Exception(t('Your email domain is not among those allowed on this site.'));
309 if (!valid_email($email) || !validate_email($email)) {
310 throw new Exception(t('Not a valid email address.'));
313 if (dba::exists('user', ['email' => $email])) {
314 throw new Exception(t('Cannot use that email.'));
317 // Disallow somebody creating an account using openid that uses the admin email address,
318 // since openid bypasses email verification. We'll allow it if there is not yet an admin account.
319 if (x($a->config, 'admin_email') && strlen($openid_url)) {
320 $adminlist = explode(',', str_replace(' ', '', strtolower($a->config['admin_email'])));
321 if (in_array(strtolower($email), $adminlist)) {
322 throw new Exception(t('Cannot use that email.'));
326 $nickname = $data['nickname'] = strtolower($nickname);
328 if (!preg_match('/^[a-z0-9][a-z0-9\_]*$/', $nickname)) {
329 throw new Exception(t('Your "nickname" can only contain "a-z", "0-9" and "_".'));
332 // Check existing and deleted accounts for this nickname.
333 if (dba::exists('user', ['nickname' => $nickname])
334 || dba::exists('userd', ['username' => $nickname])
336 throw new Exception(t('Nickname is already registered. Please choose another.'));
339 $new_password = strlen($password) ? $password : User::generateNewPassword();
340 $new_password_encoded = self::hashPassword($new_password);
342 $return['password'] = $new_password;
344 $keys = Crypto::newKeypair(4096);
345 if ($keys === false) {
346 throw new Exception(t('SERIOUS ERROR: Generation of security keys failed.'));
349 $prvkey = $keys['prvkey'];
350 $pubkey = $keys['pubkey'];
352 // Create another keypair for signing/verifying salmon protocol messages.
353 $sres = Crypto::newKeypair(512);
354 $sprvkey = $sres['prvkey'];
355 $spubkey = $sres['pubkey'];
357 $insert_result = dba::insert('user', [
358 'guid' => generate_user_guid(),
359 'username' => $username,
360 'password' => $new_password_encoded,
362 'openid' => $openid_url,
363 'nickname' => $nickname,
366 'spubkey' => $spubkey,
367 'sprvkey' => $sprvkey,
368 'verified' => $verified,
369 'blocked' => $blocked,
371 'register_date' => datetime_convert(),
372 'default-location' => ''
375 if ($insert_result) {
376 $uid = dba::lastInsertId();
377 $user = dba::selectFirst('user', [], ['uid' => $uid]);
379 throw new Exception(t('An error occurred during registration. Please try again.'));
383 throw new Exception(t('An error occurred during registration. Please try again.'));
386 // if somebody clicked submit twice very quickly, they could end up with two accounts
387 // due to race condition. Remove this one.
388 $user_count = dba::count('user', ['nickname' => $nickname]);
389 if ($user_count > 1) {
390 dba::delete('user', ['uid' => $uid]);
392 throw new Exception(t('Nickname is already registered. Please choose another.'));
395 $insert_result = dba::insert('profile', [
398 'photo' => System::baseUrl() . "/photo/profile/{$uid}.jpg",
399 'thumb' => System::baseUrl() . "/photo/avatar/{$uid}.jpg",
400 'publish' => $publish,
402 'net-publish' => $netpublish,
403 'profile-name' => t('default')
405 if (!$insert_result) {
406 dba::delete('user', ['uid' => $uid]);
408 throw new Exception(t('An error occurred creating your default profile. Please try again.'));
411 // Create the self contact
412 if (!Contact::createSelfFromUserId($uid)) {
413 dba::delete('user', ['uid' => $uid]);
415 throw new Exception(t('An error occurred creating your self contact. Please try again.'));
418 // Create a group with no members. This allows somebody to use it
419 // right away as a default group for new contacts.
420 $def_gid = Group::create($uid, t('Friends'));
422 dba::delete('user', ['uid' => $uid]);
424 throw new Exception(t('An error occurred creating your default contact group. Please try again.'));
427 $fields = ['def_gid' => $def_gid];
428 if (Config::get('system', 'newuser_private') && $def_gid) {
429 $fields['allow_gid'] = '<' . $def_gid . '>';
432 dba::update('user', $fields, ['uid' => $uid]);
434 // if we have no OpenID photo try to look up an avatar
435 if (!strlen($photo)) {
436 $photo = avatar_img($email);
439 // unless there is no avatar-addon loaded
440 if (strlen($photo)) {
441 $photo_failure = false;
443 $filename = basename($photo);
444 $img_str = fetch_url($photo, true);
445 // guess mimetype from headers or filename
446 $type = Image::guessType($photo, true);
448 $Image = new Image($img_str, $type);
449 if ($Image->isValid()) {
450 $Image->scaleToSquare(175);
452 $hash = photo_new_resource();
454 $r = Photo::store($Image, $uid, 0, $hash, $filename, t('Profile Photos'), 4);
457 $photo_failure = true;
460 $Image->scaleDown(80);
462 $r = Photo::store($Image, $uid, 0, $hash, $filename, t('Profile Photos'), 5);
465 $photo_failure = true;
468 $Image->scaleDown(48);
470 $r = Photo::store($Image, $uid, 0, $hash, $filename, t('Profile Photos'), 6);
473 $photo_failure = true;
476 if (!$photo_failure) {
477 dba::update('photo', ['profile' => 1], ['resource-id' => $hash]);
482 Addon::callHooks('register_account', $uid);
484 $return['user'] = $user;
489 * @brief Sends pending registration confiĆmation email
491 * @param string $email
492 * @param string $sitename
493 * @param string $username
494 * @return NULL|boolean from notification() and email() inherited
496 public static function sendRegisterPendingEmail($email, $sitename, $username)
500 Thank you for registering at %2$s. Your account is pending for approval by the administrator.
503 $body = sprintf($body, $username, $sitename);
505 return notification([
506 'type' => SYSTEM_EMAIL,
507 'to_email' => $email,
508 'subject'=> sprintf( t('Registration at %s'), $sitename),
513 * @brief Sends registration confirmation
515 * It's here as a function because the mail is sent from different parts
517 * @param string $email
518 * @param string $sitename
519 * @param string $siteurl
520 * @param string $username
521 * @param string $password
522 * @return NULL|boolean from notification() and email() inherited
524 public static function sendRegisterOpenEmail($email, $sitename, $siteurl, $username, $password)
526 $preamble = deindent(t('
528 Thank you for registering at %2$s. Your account has been created.
531 The login details are as follows:
536 You may change your password from your account "Settings" page after logging
539 Please take a few moments to review the other account settings on that page.
541 You may also wish to add some basic information to your default profile
542 (on the "Profiles" page) so that other people can easily find you.
544 We recommend setting your full name, adding a profile photo,
545 adding some profile "keywords" (very useful in making new friends) - and
546 perhaps what country you live in; if you do not wish to be more specific
549 We fully respect your right to privacy, and none of these items are necessary.
550 If you are new and do not know anybody here, they may help
551 you to make some new and interesting friends.
554 Thank you and welcome to %2$s.'));
556 $preamble = sprintf($preamble, $username, $sitename);
557 $body = sprintf($body, $email, $sitename, $siteurl, $username, $password);
559 return notification([
560 'type' => SYSTEM_EMAIL,
561 'to_email' => $email,
562 'subject'=> sprintf( t('Registration details for %s'), $sitename),
563 'preamble'=> $preamble,
568 * @param object $uid user to remove
571 public static function remove($uid)
577 logger('Removing user: ' . $uid);
579 $user = dba::selectFirst('user', [], ['uid' => $uid]);
581 Addon::callHooks('remove_user', $user);
583 // save username (actually the nickname as it is guaranteed
584 // unique), so it cannot be re-registered in the future.
585 dba::insert('userd', ['username' => $user['nickname']]);
587 // The user and related data will be deleted in "cron_expire_and_remove_users" (cronjobs.php)
588 dba::update('user', ['account_removed' => true, 'account_expires_on' => datetime_convert()], ['uid' => $uid]);
589 Worker::add(PRIORITY_HIGH, "Notifier", "removeme", $uid);
591 // Send an update to the directory
592 Worker::add(PRIORITY_LOW, "Directory", $user['url']);
594 if ($uid == local_user()) {
595 unset($_SESSION['authenticated']);
596 unset($_SESSION['uid']);
597 goaway(System::baseUrl());