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\Config;
11 use Friendica\Core\PConfig;
12 use Friendica\Core\System;
13 use Friendica\Core\Worker;
14 use Friendica\Database\DBM;
15 use Friendica\Model\Contact;
16 use Friendica\Model\Group;
17 use Friendica\Model\Photo;
18 use Friendica\Object\Image;
21 require_once 'boot.php';
22 require_once 'include/crypto.php';
23 require_once 'include/enotify.php';
24 require_once 'include/group.php';
25 require_once 'include/network.php';
26 require_once 'library/openid.php';
27 require_once 'include/pgettext.php';
28 require_once 'include/plugin.php';
29 require_once 'include/text.php';
31 * @brief This class handles User related functions
36 * @brief Returns the default group for a given user and network
38 * @param int $uid User id
39 * @param string $network network name
41 * @return int group id
43 public static function getDefaultGroup($uid, $network = '')
47 if ($network == NETWORK_OSTATUS) {
48 $default_group = PConfig::get($uid, "ostatus", "default_group");
51 if ($default_group != 0) {
52 return $default_group;
55 $user = dba::select('user', ['def_gid'], ['uid' => $uid], ['limit' => 1]);
57 if (DBM::is_result($user)) {
58 $default_group = $user["def_gid"];
61 return $default_group;
66 * @brief Authenticate a user with a clear text password
68 * User info can be any of the following:
71 * - User email or username or nickname
72 * - User array with at least the uid and the hashed password
74 * @param mixed $user_info
75 * @param string $password
78 public static function authenticate($user_info, $password)
80 if (is_object($user_info)) {
81 $user = (array) $user_info;
82 } elseif (is_int($user_info)) {
83 $user = dba::select('user',
88 'account_expired' => 0,
89 'account_removed' => 0,
94 } elseif (is_string($user_info)) {
95 $user = dba::fetch_first('SELECT `uid`, `password`
97 WHERE (`email` = ? OR `username` = ? OR `nickname` = ?)
99 AND `account_expired` = 0
100 AND `account_removed` = 0
111 if (!DBM::is_result($user) || !isset($user['uid']) || !isset($user['password'])) {
115 $password_hashed = hash('whirlpool', $password);
117 if ($password_hashed !== $user['password']) {
125 * @brief Catch-all user creation function
127 * Creates a user from the provided data array, either form fields or OpenID.
128 * Required: { username, nickname, email } or { openid_url }
130 * Performs the following:
131 * - Sends to the OpenId auth URL (if relevant)
132 * - Creates new key pairs for crypto
133 * - Create self-contact
134 * - Create profile image
139 public static function create(array $data)
142 $result = array('success' => false, 'user' => null, 'password' => '', 'message' => '');
144 $using_invites = Config::get('system', 'invitation_only');
145 $num_invites = Config::get('system', 'number_invites');
147 $invite_id = x($data, 'invite_id') ? notags(trim($data['invite_id'])) : '';
148 $username = x($data, 'username') ? notags(trim($data['username'])) : '';
149 $nickname = x($data, 'nickname') ? notags(trim($data['nickname'])) : '';
150 $email = x($data, 'email') ? notags(trim($data['email'])) : '';
151 $openid_url = x($data, 'openid_url') ? notags(trim($data['openid_url'])) : '';
152 $photo = x($data, 'photo') ? notags(trim($data['photo'])) : '';
153 $password = x($data, 'password') ? trim($data['password']) : '';
154 $password1 = x($data, 'password1') ? trim($data['password1']) : '';
155 $confirm = x($data, 'confirm') ? trim($data['confirm']) : '';
156 $blocked = x($data, 'blocked') ? intval($data['blocked']) : 0;
157 $verified = x($data, 'verified') ? intval($data['verified']) : 0;
159 $publish = x($data, 'profile_publish_reg') && intval($data['profile_publish_reg']) ? 1 : 0;
160 $netpublish = strlen(Config::get('system', 'directory')) ? $publish : 0;
162 if ($password1 != $confirm) {
163 $result['message'] .= t('Passwords do not match. Password unchanged.') . EOL;
165 } elseif ($password1 != "") {
166 $password = $password1;
169 $tmp_str = $openid_url;
171 if ($using_invites) {
173 $result['message'] .= t('An invitation is required.') . EOL;
176 $r = q("SELECT * FROM `register` WHERE `hash` = '%s' LIMIT 1", dbesc($invite_id));
178 $result['message'] .= t('Invitation could not be verified.') . EOL;
183 if (!x($username) || !x($email) || !x($nickname)) {
185 if (!validate_url($tmp_str)) {
186 $result['message'] .= t('Invalid OpenID url') . EOL;
189 $_SESSION['register'] = 1;
190 $_SESSION['openid'] = $openid_url;
192 $openid = new LightOpenID;
193 $openid->identity = $openid_url;
194 $openid->returnUrl = System::baseUrl() . '/openid';
195 $openid->required = array('namePerson/friendly', 'contact/email', 'namePerson');
196 $openid->optional = array('namePerson/first', 'media/image/aspect11', 'media/image/default');
198 $authurl = $openid->authUrl();
199 } catch (Exception $e) {
200 $result['message'] .= 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() . EOL;
207 notice(t('Please enter the required information.') . EOL);
211 if (!validate_url($tmp_str)) {
217 // collapse multiple spaces in name
218 $username = preg_replace('/ +/', ' ', $username);
220 if (mb_strlen($username) > 48) {
221 $result['message'] .= t('Please use a shorter name.') . EOL;
223 if (mb_strlen($username) < 3) {
224 $result['message'] .= t('Name too short.') . EOL;
227 // So now we are just looking for a space in the full name.
229 $loose_reg = Config::get('system', 'no_regfullname');
231 $username = mb_convert_case($username, MB_CASE_TITLE, 'UTF-8');
232 if (!strpos($username, ' ')) {
233 $result['message'] .= t("That doesn't appear to be your full \x28First Last\x29 name.") . EOL;
238 if (!allowed_email($email)) {
239 $result['message'] .= t('Your email domain is not among those allowed on this site.') . EOL;
242 if (!valid_email($email) || !validate_email($email)) {
243 $result['message'] .= t('Not a valid email address.') . EOL;
246 // Disallow somebody creating an account using openid that uses the admin email address,
247 // since openid bypasses email verification. We'll allow it if there is not yet an admin account.
249 $adminlist = explode(",", str_replace(" ", "", strtolower($a->config['admin_email'])));
251 //if((x($a->config,'admin_email')) && (strcasecmp($email,$a->config['admin_email']) == 0) && strlen($openid_url)) {
252 if (x($a->config, 'admin_email') && in_array(strtolower($email), $adminlist) && strlen($openid_url)) {
253 $r = q("SELECT * FROM `user` WHERE `email` = '%s' LIMIT 1",
256 if (DBM::is_result($r)) {
257 $result['message'] .= t('Cannot use that email.') . EOL;
261 $nickname = $data['nickname'] = strtolower($nickname);
263 if (!preg_match("/^[a-z0-9][a-z0-9\_]*$/", $nickname)) {
264 $result['message'] .= t('Your "nickname" can only contain "a-z", "0-9" and "_".') . EOL;
267 $r = q("SELECT `uid` FROM `user`
268 WHERE `nickname` = '%s' LIMIT 1",
271 if (DBM::is_result($r)) {
272 $result['message'] .= t('Nickname is already registered. Please choose another.') . EOL;
275 // Check deleted accounts that had this nickname. Doesn't matter to us,
276 // but could be a security issue for federated platforms.
278 $r = q("SELECT * FROM `userd`
279 WHERE `username` = '%s' LIMIT 1",
282 if (DBM::is_result($r)) {
283 $result['message'] .= t('Nickname was once registered here and may not be re-used. Please choose another.') . EOL;
286 if (strlen($result['message'])) {
290 $new_password = strlen($password) ? $password : autoname(6) . mt_rand(100, 9999);
291 $new_password_encoded = hash('whirlpool', $new_password);
293 $result['password'] = $new_password;
295 $keys = new_keypair(4096);
297 if ($keys === false) {
298 $result['message'] .= t('SERIOUS ERROR: Generation of security keys failed.') . EOL;
302 $prvkey = $keys['prvkey'];
303 $pubkey = $keys['pubkey'];
305 // Create another keypair for signing/verifying salmon protocol messages.
306 $sres = new_keypair(512);
307 $sprvkey = $sres['prvkey'];
308 $spubkey = $sres['pubkey'];
310 $r = q("INSERT INTO `user` (`guid`, `username`, `password`, `email`, `openid`, `nickname`,
311 `pubkey`, `prvkey`, `spubkey`, `sprvkey`, `register_date`, `verified`, `blocked`, `timezone`, `default-location`)
312 VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, 'UTC', '')",
313 dbesc(generate_user_guid()),
315 dbesc($new_password_encoded),
323 dbesc(datetime_convert()),
329 $r = q("SELECT * FROM `user`
330 WHERE `username` = '%s' AND `password` = '%s' LIMIT 1",
332 dbesc($new_password_encoded)
334 if (DBM::is_result($r)) {
336 $newuid = intval($r[0]['uid']);
339 $result['message'] .= t('An error occurred during registration. Please try again.') . EOL;
344 * if somebody clicked submit twice very quickly, they could end up with two accounts
345 * due to race condition. Remove this one.
347 $r = q("SELECT `uid` FROM `user`
348 WHERE `nickname` = '%s' ",
351 if (DBM::is_result($r) && count($r) > 1 && $newuid) {
352 $result['message'] .= t('Nickname is already registered. Please choose another.') . EOL;
353 dba::delete('user', array('uid' => $newuid));
357 if (x($newuid) !== false) {
358 $r = q("INSERT INTO `profile` ( `uid`, `profile-name`, `is-default`, `name`, `photo`, `thumb`, `publish`, `net-publish` )
359 VALUES ( %d, '%s', %d, '%s', '%s', '%s', %d, %d ) ",
364 dbesc(System::baseUrl() . "/photo/profile/{$newuid}.jpg"),
365 dbesc(System::baseUrl() . "/photo/avatar/{$newuid}.jpg"),
370 $result['message'] .= t('An error occurred creating your default profile. Please try again.') . EOL;
371 // Start fresh next time.
372 dba::delete('user', array('uid' => $newuid));
376 // Create the self contact
377 Contact::createSelfFromUserId($newuid);
379 // Create a group with no members. This allows somebody to use it
380 // right away as a default group for new contacts.
382 group_add($newuid, t('Friends'));
384 $r = q("SELECT `id` FROM `group` WHERE `uid` = %d AND `name` = '%s'",
388 if (DBM::is_result($r)) {
389 $def_gid = $r[0]['id'];
391 q("UPDATE `user` SET `def_gid` = %d WHERE `uid` = %d",
397 if (Config::get('system', 'newuser_private') && $def_gid) {
398 q("UPDATE `user` SET `allow_gid` = '%s' WHERE `uid` = %d",
399 dbesc("<" . $def_gid . ">"),
405 // if we have no OpenID photo try to look up an avatar
406 if (!strlen($photo)) {
407 $photo = avatar_img($email);
410 // unless there is no avatar-plugin loaded
411 if (strlen($photo)) {
412 $photo_failure = false;
414 $filename = basename($photo);
415 $img_str = fetch_url($photo, true);
416 // guess mimetype from headers or filename
417 $type = Image::guessType($photo, true);
420 $Image = new Image($img_str, $type);
421 if ($Image->isValid()) {
422 $Image->scaleToSquare(175);
424 $hash = photo_new_resource();
426 $r = Photo::store($Image, $newuid, 0, $hash, $filename, t('Profile Photos'), 4);
429 $photo_failure = true;
432 $Image->scaleDown(80);
434 $r = Photo::store($Image, $newuid, 0, $hash, $filename, t('Profile Photos'), 5);
437 $photo_failure = true;
440 $Image->scaleDown(48);
442 $r = Photo::store($Image, $newuid, 0, $hash, $filename, t('Profile Photos'), 6);
445 $photo_failure = true;
448 if (!$photo_failure) {
449 q("UPDATE `photo` SET `profile` = 1 WHERE `resource-id` = '%s' ",
456 call_hooks('register_account', $newuid);
458 $result['success'] = true;
459 $result['user'] = $u;
464 * @brief Sends pending registration confiĆmation email
466 * @param string $email
467 * @param string $sitename
468 * @param string $username
469 * @return NULL|boolean from notification() and email() inherited
471 public static function sendRegisterPendingEmail($email, $sitename, $username)
475 Thank you for registering at %2$s. Your account is pending for approval by the administrator.
478 $body = sprintf($body, $username, $sitename);
480 return notification(array(
481 'type' => SYSTEM_EMAIL,
482 'to_email' => $email,
483 'subject'=> sprintf( t('Registration at %s'), $sitename),
488 * @brief Sends registration confirmation
490 * It's here as a function because the mail is sent from different parts
492 * @param string $email
493 * @param string $sitename
494 * @param string $siteurl
495 * @param string $username
496 * @param string $password
497 * @return NULL|boolean from notification() and email() inherited
499 public static function sendRegisterOpenEmail($email, $sitename, $siteurl, $username, $password)
501 $preamble = deindent(t('
503 Thank you for registering at %2$s. Your account has been created.
506 The login details are as follows:
511 You may change your password from your account "Settings" page after logging
514 Please take a few moments to review the other account settings on that page.
516 You may also wish to add some basic information to your default profile
517 (on the "Profiles" page) so that other people can easily find you.
519 We recommend setting your full name, adding a profile photo,
520 adding some profile "keywords" (very useful in making new friends) - and
521 perhaps what country you live in; if you do not wish to be more specific
524 We fully respect your right to privacy, and none of these items are necessary.
525 If you are new and do not know anybody here, they may help
526 you to make some new and interesting friends.
529 Thank you and welcome to %2$s.'));
531 $preamble = sprintf($preamble, $username, $sitename);
532 $body = sprintf($body, $email, $sitename, $siteurl, $username, $password);
534 return notification(array(
535 'type' => SYSTEM_EMAIL,
536 'to_email' => $email,
537 'subject'=> sprintf( t('Registration details for %s'), $sitename),
538 'preamble'=> $preamble,
543 * @param object $uid user to remove
546 public static function remove($uid)
552 logger('Removing user: ' . $uid);
554 $user = dba::select('user', [], ['uid' => $uid], ['limit' => 1]);
556 call_hooks('remove_user', $user);
558 // save username (actually the nickname as it is guaranteed
559 // unique), so it cannot be re-registered in the future.
560 dba::insert('userd', ['username' => $user['nickname']]);
562 // The user and related data will be deleted in "cron_expire_and_remove_users" (cronjobs.php)
563 dba::update('user', ['account_removed' => true, 'account_expires_on' => datetime_convert()], ['uid' => $uid]);
564 Worker::add(PRIORITY_HIGH, "Notifier", "removeme", $uid);
566 // Send an update to the directory
567 Worker::add(PRIORITY_LOW, "Directory", $user['url']);
569 if ($uid == local_user()) {
570 unset($_SESSION['authenticated']);
571 unset($_SESSION['uid']);
572 goaway(System::baseUrl());