3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2008, 2009, StatusNet, Inc.
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 if (!defined('GNUSOCIAL')) { exit(1); }
23 * Table Definition for user
26 class User extends Managed_DataObject
28 const SUBSCRIBE_POLICY_OPEN = 0;
29 const SUBSCRIBE_POLICY_MODERATE = 1;
32 /* the code below is auto generated do not remove the above tag */
34 public $__table = 'user'; // table name
35 public $id; // int(4) primary_key not_null
36 public $nickname; // varchar(64) unique_key
37 public $password; // varchar(191) not 255 because utf8mb4 takes more space
38 public $email; // varchar(191) unique_key not 255 because utf8mb4 takes more space
39 public $incomingemail; // varchar(191) unique_key not 255 because utf8mb4 takes more space
40 public $emailnotifysub; // tinyint(1) default_1
41 public $emailnotifyfav; // tinyint(1) default_1
42 public $emailnotifynudge; // tinyint(1) default_1
43 public $emailnotifymsg; // tinyint(1) default_1
44 public $emailnotifyattn; // tinyint(1) default_1
45 public $emailmicroid; // tinyint(1) default_1
46 public $language; // varchar(50)
47 public $timezone; // varchar(50)
48 public $emailpost; // tinyint(1) default_1
49 public $sms; // varchar(64) unique_key
50 public $carrier; // int(4)
51 public $smsnotify; // tinyint(1)
52 public $smsreplies; // tinyint(1)
53 public $smsemail; // varchar(191) not 255 because utf8mb4 takes more space
54 public $uri; // varchar(191) unique_key not 255 because utf8mb4 takes more space
55 public $autosubscribe; // tinyint(1)
56 public $subscribe_policy; // tinyint(1)
57 public $urlshorteningservice; // varchar(50) default_ur1.ca
58 public $private_stream; // tinyint(1) default_0
59 public $created; // datetime() not_null
60 public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
62 /* the code above is auto generated do not remove the tag below */
65 public static function schemaDef()
68 'description' => 'local users',
70 'id' => array('type' => 'int', 'not null' => true, 'description' => 'foreign key to profile table'),
71 'nickname' => array('type' => 'varchar', 'length' => 64, 'description' => 'nickname or username, duped in profile'),
72 'password' => array('type' => 'varchar', 'length' => 191, 'description' => 'salted password, can be null for OpenID users'),
73 'email' => array('type' => 'varchar', 'length' => 191, 'description' => 'email address for password recovery etc.'),
74 'incomingemail' => array('type' => 'varchar', 'length' => 191, 'description' => 'email address for post-by-email'),
75 'emailnotifysub' => array('type' => 'int', 'size' => 'tiny', 'default' => 1, 'description' => 'Notify by email of subscriptions'),
76 'emailnotifyfav' => array('type' => 'int', 'size' => 'tiny', 'default' => null, 'description' => 'Notify by email of favorites'),
77 'emailnotifynudge' => array('type' => 'int', 'size' => 'tiny', 'default' => 1, 'description' => 'Notify by email of nudges'),
78 'emailnotifymsg' => array('type' => 'int', 'size' => 'tiny', 'default' => 1, 'description' => 'Notify by email of direct messages'),
79 'emailnotifyattn' => array('type' => 'int', 'size' => 'tiny', 'default' => 1, 'description' => 'Notify by email of @-replies'),
80 'emailmicroid' => array('type' => 'int', 'size' => 'tiny', 'default' => 1, 'description' => 'whether to publish email microid'),
81 'language' => array('type' => 'varchar', 'length' => 50, 'description' => 'preferred language'),
82 'timezone' => array('type' => 'varchar', 'length' => 50, 'description' => 'timezone'),
83 'emailpost' => array('type' => 'int', 'size' => 'tiny', 'default' => 1, 'description' => 'Post by email'),
84 'sms' => array('type' => 'varchar', 'length' => 64, 'description' => 'sms phone number'),
85 'carrier' => array('type' => 'int', 'description' => 'foreign key to sms_carrier'),
86 'smsnotify' => array('type' => 'int', 'size' => 'tiny', 'default' => 0, 'description' => 'whether to send notices to SMS'),
87 'smsreplies' => array('type' => 'int', 'size' => 'tiny', 'default' => 0, 'description' => 'whether to send notices to SMS on replies'),
88 'smsemail' => array('type' => 'varchar', 'length' => 191, 'description' => 'built from sms and carrier'),
89 'uri' => array('type' => 'varchar', 'length' => 191, 'description' => 'universally unique identifier, usually a tag URI'),
90 'autosubscribe' => array('type' => 'int', 'size' => 'tiny', 'default' => 0, 'description' => 'automatically subscribe to users who subscribe to us'),
91 'subscribe_policy' => array('type' => 'int', 'size' => 'tiny', 'default' => 0, 'description' => '0 = anybody can subscribe; 1 = require approval'),
92 'urlshorteningservice' => array('type' => 'varchar', 'length' => 50, 'default' => 'internal', 'description' => 'service to use for auto-shortening URLs'),
93 'private_stream' => array('type' => 'int', 'size' => 'tiny', 'default' => 0, 'description' => 'whether to limit all notices to followers only'),
95 'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'),
96 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
98 'primary key' => array('id'),
99 'unique keys' => array(
100 'user_nickname_key' => array('nickname'),
101 'user_email_key' => array('email'),
102 'user_incomingemail_key' => array('incomingemail'),
103 'user_sms_key' => array('sms'),
104 'user_uri_key' => array('uri'),
106 'foreign keys' => array(
107 'user_id_fkey' => array('profile', array('id' => 'id')),
108 'user_carrier_fkey' => array('sms_carrier', array('carrier' => 'id')),
111 'user_smsemail_idx' => array('smsemail'),
116 protected $_profile = array();
121 * @throws UserNoProfileException if user has no profile
123 public function getProfile()
125 if (!isset($this->_profile[$this->id])) {
126 $profile = Profile::getKV('id', $this->id);
127 if (!$profile instanceof Profile) {
128 throw new UserNoProfileException($this);
130 $this->_profile[$this->id] = $profile;
132 return $this->_profile[$this->id];
135 public function sameAs(Profile $other)
137 return $this->getProfile()->sameAs($other);
140 public function getUri()
145 public function getNickname()
147 return $this->getProfile()->getNickname();
150 static function getByNickname($nickname)
152 $user = User::getKV('nickname', $nickname);
153 if (!$user instanceof User) {
154 throw new NoSuchUserException(array('nickname' => $nickname));
160 function isSubscribed(Profile $other)
162 return $this->getProfile()->isSubscribed($other);
165 function hasPendingSubscription(Profile $other)
167 return $this->getProfile()->hasPendingSubscription($other);
171 * Get the most recent notice posted by this user, if any.
173 * @return mixed Notice or null
175 function getCurrentNotice()
177 return $this->getProfile()->getCurrentNotice();
180 function getCarrier()
182 return Sms_carrier::getKV('id', $this->carrier);
185 function hasBlocked($other)
187 return $this->getProfile()->hasBlocked($other);
191 * Register a new user account and profile and set up default subscriptions.
192 * If a new-user welcome message is configured, this will be sent.
194 * @param array $fields associative array of optional properties
197 * bool 'email_confirmed' pass true to mark email as pre-confirmed
200 * string 'location' informal string description of geolocation
201 * float 'lat' decimal latitude for geolocation
202 * float 'lon' decimal longitude for geolocation
203 * int 'location_id' geoname identifier
204 * int 'location_ns' geoname namespace to interpret location_id
205 * string 'nickname' REQUIRED
206 * string 'password' (may be missing for eg OpenID registrations)
207 * string 'code' invite code
208 * ?string 'uri' permalink to notice; defaults to local notice URL
209 * @return User object
210 * @throws Exception on failure
212 static function register(array $fields) {
214 // MAGICALLY put fields into current scope
218 $profile = new Profile();
220 if (!empty($email)) {
221 $email = common_canonical_email($email);
224 // Normalize _and_ check whether it is in use. Throw NicknameException on failure.
225 $profile->nickname = Nickname::normalize($nickname, true);
227 $profile->profileurl = common_profile_url($profile->nickname);
229 if (!empty($fullname)) {
230 $profile->fullname = $fullname;
232 if (!empty($homepage)) {
233 $profile->homepage = $homepage;
236 $profile->bio = $bio;
238 if (!empty($location)) {
239 $profile->location = $location;
241 $loc = Location::fromName($location);
244 $profile->lat = $loc->lat;
245 $profile->lon = $loc->lon;
246 $profile->location_id = $loc->location_id;
247 $profile->location_ns = $loc->location_ns;
251 $profile->created = common_sql_now();
255 $user->nickname = $profile->nickname;
259 // Users who respond to invite email have proven their ownership of that address
262 $invite = Invitation::getKV($code);
263 if ($invite instanceof Invitation && $invite->address && $invite->address_type == 'email' && $invite->address == $email) {
264 $user->email = $invite->address;
268 if(isset($email_confirmed) && $email_confirmed) {
269 $user->email = $email;
272 // Set default-on options here, otherwise they'll be disabled
273 // initially for sites using caching, since the initial encache
274 // doesn't know about the defaults in the database.
275 $user->emailnotifysub = 1;
276 $user->emailnotifynudge = 1;
277 $user->emailnotifymsg = 1;
278 $user->emailnotifyattn = 1;
279 $user->emailmicroid = 1;
280 $user->emailpost = 1;
281 $user->jabbermicroid = 1;
283 $user->created = common_sql_now();
285 if (Event::handle('StartUserRegister', array($profile))) {
287 $profile->query('BEGIN');
289 $id = $profile->insert();
291 common_log_db_error($profile, 'INSERT', __FILE__);
292 $profile->query('ROLLBACK');
293 // TRANS: Profile data could not be inserted for some reason.
294 throw new ServerException(_m('Could not insert profile data for new user.'));
302 $user->uri = common_user_uri($user);
305 if (!empty($password)) { // may not have a password for OpenID users
306 $user->password = common_munge_password($password, $id);
309 $result = $user->insert();
311 if ($result === false) {
312 common_log_db_error($user, 'INSERT', __FILE__);
313 $profile->query('ROLLBACK');
314 // TRANS: User data could not be inserted for some reason.
315 throw new ServerException(_m('Could not insert user data for new user.'));
318 // Everyone is subscribed to themself
320 $subscription = new Subscription();
321 $subscription->subscriber = $user->id;
322 $subscription->subscribed = $user->id;
323 $subscription->created = $user->created;
325 $result = $subscription->insert();
328 common_log_db_error($subscription, 'INSERT', __FILE__);
329 $profile->query('ROLLBACK');
330 // TRANS: Subscription data could not be inserted for some reason.
331 throw new ServerException(_m('Could not insert subscription data for new user.'));
334 // Mark that this invite was converted
336 if (!empty($invite)) {
337 $invite->convert($user);
340 if (!empty($email) && !$user->email) {
342 $confirm = new Confirm_address();
343 $confirm->code = common_confirmation_code(128);
344 $confirm->user_id = $user->id;
345 $confirm->address = $email;
346 $confirm->address_type = 'email';
348 $result = $confirm->insert();
351 common_log_db_error($confirm, 'INSERT', __FILE__);
352 $profile->query('ROLLBACK');
353 // TRANS: Email confirmation data could not be inserted for some reason.
354 throw new ServerException(_m('Could not insert email confirmation data for new user.'));
358 if (!empty($code) && $user->email) {
359 $user->emailChanged();
362 // Default system subscription
364 $defnick = common_config('newuser', 'default');
366 if (!empty($defnick)) {
367 $defuser = User::getKV('nickname', $defnick);
368 if (empty($defuser)) {
369 common_log(LOG_WARNING, sprintf("Default user %s does not exist.", $defnick),
372 Subscription::ensureStart($profile, $defuser->getProfile());
376 $profile->query('COMMIT');
378 if (!empty($email) && !$user->email) {
379 mail_confirm_address($user, $confirm->code, $profile->nickname, $email);
384 $welcome = common_config('newuser', 'welcome');
386 if (!empty($welcome)) {
387 $welcomeuser = User::getKV('nickname', $welcome);
388 if (empty($welcomeuser)) {
389 common_log(LOG_WARNING, sprintf("Welcome user %s does not exist.", $defnick),
392 $notice = Notice::saveNew($welcomeuser->id,
393 // TRANS: Notice given on user registration.
394 // TRANS: %1$s is the sitename, $2$s is the registering user's nickname.
395 sprintf(_('Welcome to %1$s, @%2$s!'),
396 common_config('site', 'name'),
402 Event::handle('EndUserRegister', array($profile));
405 if (!$user instanceof User) {
406 throw new ServerException('User could not be registered. Probably an event hook that failed.');
412 // Things we do when the email changes
413 function emailChanged()
416 $invites = new Invitation();
417 $invites->address = $this->email;
418 $invites->address_type = 'email';
420 if ($invites->find()) {
421 while ($invites->fetch()) {
423 $other = Profile::getKV('id', $invites->user_id);
424 if (!($other instanceof Profile)) { // remove when getKV throws exceptions
427 Subscription::start($other, $this->getProfile());
428 } catch (Exception $e) {
435 function mutuallySubscribed(Profile $other)
437 return $this->getProfile()->mutuallySubscribed($other);
440 function mutuallySubscribedUsers()
442 // 3-way join; probably should get cached
443 $UT = common_config('db','type')=='pgsql'?'"user"':'user';
444 $qry = "SELECT $UT.* " .
445 "FROM subscription sub1 JOIN $UT ON sub1.subscribed = $UT.id " .
446 "JOIN subscription sub2 ON $UT.id = sub2.subscriber " .
447 'WHERE sub1.subscriber = %d and sub2.subscribed = %d ' .
448 "ORDER BY $UT.nickname";
450 $user->query(sprintf($qry, $this->id, $this->id));
455 function getReplies($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0)
457 return $this->getProfile()->getReplies($offset, $limit, $since_id, $before_id);
460 function getTaggedNotices($tag, $offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0) {
461 return $this->getProfile()->getTaggedNotices($tag, $offset, $limit, $since_id, $before_id);
464 function getNotices($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0)
466 return $this->getProfile()->getNotices($offset, $limit, $since_id, $before_id);
469 function getSelfTags()
471 return Profile_tag::getTagsArray($this->id, $this->id, $this->id);
474 function setSelfTags($newtags, $privacy)
476 return Profile_tag::setTags($this->id, $this->id, $newtags, $privacy);
479 function block(Profile $other)
481 // Add a new block record
483 // no blocking (and thus unsubbing from) yourself
485 if ($this->id == $other->id) {
486 common_log(LOG_WARNING,
488 "Profile ID %d (%s) tried to block themself.",
496 $block = new Profile_block();
498 // Begin a transaction
500 $block->query('BEGIN');
502 $block->blocker = $this->id;
503 $block->blocked = $other->id;
505 $result = $block->insert();
508 common_log_db_error($block, 'INSERT', __FILE__);
512 $self = $this->getProfile();
513 if (Subscription::exists($other, $self)) {
514 Subscription::cancel($other, $self);
516 if (Subscription::exists($self, $other)) {
517 Subscription::cancel($self, $other);
520 $block->query('COMMIT');
525 function unblock(Profile $other)
527 // Get the block record
529 $block = Profile_block::exists($this->getProfile(), $other);
535 $result = $block->delete();
538 common_log_db_error($block, 'DELETE', __FILE__);
545 function isMember(User_group $group)
547 return $this->getProfile()->isMember($group);
550 function isAdmin(User_group $group)
552 return $this->getProfile()->isAdmin($group);
555 function getGroups($offset=0, $limit=null)
557 return $this->getProfile()->getGroups($offset, $limit);
561 * Request to join the given group.
562 * May throw exceptions on failure.
564 * @param User_group $group
565 * @return Group_member
567 function joinGroup(User_group $group)
569 return $this->getProfile()->joinGroup($group);
573 * Leave a group that this user is a member of.
575 * @param User_group $group
577 function leaveGroup(User_group $group)
579 return $this->getProfile()->leaveGroup($group);
582 function getSubscribed($offset=0, $limit=null)
584 return $this->getProfile()->getSubscribed($offset, $limit);
587 function getSubscribers($offset=0, $limit=null)
589 return $this->getProfile()->getSubscribers($offset, $limit);
592 function getTaggedSubscribers($tag, $offset=0, $limit=null)
594 return $this->getProfile()->getTaggedSubscribers($tag, $offset, $limit);
597 function getTaggedSubscriptions($tag, $offset=0, $limit=null)
599 return $this->getProfile()->getTaggedSubscriptions($tag, $offset, $limit);
602 function hasRight($right)
604 return $this->getProfile()->hasRight($right);
607 function delete($useWhere=false)
609 if (empty($this->id)) {
610 common_log(LOG_WARNING, "Ambiguous User->delete(); skipping related tables.");
611 return parent::delete($useWhere);
615 $profile = $this->getProfile();
617 } catch (UserNoProfileException $unp) {
618 common_log(LOG_INFO, "User {$this->nickname} has no profile; continuing deletion.");
628 Event::handle('UserDeleteRelated', array($this, &$related));
630 foreach ($related as $cls) {
632 $inst->user_id = $this->id;
636 $this->_deleteTags();
637 $this->_deleteBlocks();
639 return parent::delete($useWhere);
642 function _deleteTags()
644 $tag = new Profile_tag();
645 $tag->tagger = $this->id;
649 function _deleteBlocks()
651 $block = new Profile_block();
652 $block->blocker = $this->id;
654 // XXX delete group block? Reset blocker?
657 function hasRole($name)
659 return $this->getProfile()->hasRole($name);
662 function grantRole($name)
664 return $this->getProfile()->grantRole($name);
667 function revokeRole($name)
669 return $this->getProfile()->revokeRole($name);
672 function isSandboxed()
674 return $this->getProfile()->isSandboxed();
677 function isSilenced()
679 return $this->getProfile()->isSilenced();
682 function receivesEmailNotifications()
684 // We could do this in one large if statement, but that's not as easy to read
685 // Don't send notifications if we don't know the user's email address or it is
686 // explicitly undesired by the user's own settings.
687 if (empty($this->email) || !$this->emailnotifyattn) {
690 // Don't send notifications to a user who is sandboxed or silenced
691 if ($this->isSandboxed() || $this->isSilenced()) {
697 function repeatedByMe($offset=0, $limit=20, $since_id=null, $max_id=null)
699 $stream = new RepeatedByMeNoticeStream($this);
700 return $stream->getNotices($offset, $limit, $since_id, $max_id);
704 function repeatsOfMe($offset=0, $limit=20, $since_id=null, $max_id=null)
706 $stream = new RepeatsOfMeNoticeStream($this);
708 return $stream->getNotices($offset, $limit, $since_id, $max_id);
711 public function repeatedToMe($offset=0, $limit=20, $since_id=null, $max_id=null)
713 return $this->getProfile()->repeatedToMe($offset, $limit, $since_id, $max_id);
716 public static function siteOwner()
718 $owner = self::cacheGet('user:site_owner');
720 if ($owner === false) { // cache miss
722 $pr = new Profile_role();
723 $pr->role = Profile_role::OWNER;
724 $pr->orderBy('created');
727 if (!$pr->find(true)) {
728 throw new NoResultException($pr);
731 $owner = User::getKV('id', $pr->profile_id);
733 self::cacheSet('user:site_owner', $owner);
736 if ($owner instanceof User) {
740 throw new ServerException(_('No site owner configured.'));
744 * Pull the primary site account to use in single-user mode.
745 * If a valid user nickname is listed in 'singleuser':'nickname'
746 * in the config, this will be used; otherwise the site owner
747 * account is taken by default.
750 * @throws ServerException if no valid single user account is present
751 * @throws ServerException if called when not in single-user mode
753 public static function singleUser()
755 if (!common_config('singleuser', 'enabled')) {
756 // TRANS: Server exception.
757 throw new ServerException(_('Single-user mode code called when not enabled.'));
760 if ($nickname = common_config('singleuser', 'nickname')) {
761 $user = User::getKV('nickname', $nickname);
762 if ($user instanceof User) {
767 // If there was no nickname or no user by that nickname,
768 // try the site owner. Throws exception if not configured.
769 return User::siteOwner();
773 * This is kind of a hack for using external setup code that's trying to
774 * build single-user sites.
776 * Will still return a username if the config singleuser/nickname is set
777 * even if the account doesn't exist, which normally indicates that the
778 * site is horribly misconfigured.
780 * At the moment, we need to let it through so that router setup can
781 * complete, otherwise we won't be able to create the account.
783 * This will be easier when we can more easily create the account and
784 * *then* switch the site to 1user mode without jumping through hoops.
787 * @throws ServerException if no valid single user account is present
788 * @throws ServerException if called when not in single-user mode
790 static function singleUserNickname()
793 $user = User::singleUser();
794 return $user->nickname;
795 } catch (Exception $e) {
796 if (common_config('singleuser', 'enabled') && common_config('singleuser', 'nickname')) {
797 common_log(LOG_WARNING, "Warning: code attempting to pull single-user nickname when the account does not exist. If this is not setup time, this is probably a bug.");
798 return common_config('singleuser', 'nickname');
805 * Find and shorten links in the given text using this user's URL shortening
808 * By default, links will be left untouched if the text is shorter than the
809 * configured maximum notice length. Pass true for the $always parameter
810 * to force all links to be shortened regardless.
812 * Side effects: may save file and file_redirection records for referenced URLs.
814 * @param string $text
815 * @param boolean $always
818 public function shortenLinks($text, $always=false)
820 return common_shorten_links($text, $always, $this);
824 * Get a list of OAuth client applications that have access to this
827 function getConnectedApps($offset = 0, $limit = null)
831 'FROM oauth_application_user u, oauth_application a ' .
832 'WHERE u.profile_id = %d ' .
833 'AND a.id = u.application_id ' .
834 'AND u.access_type > 0 ' .
835 'ORDER BY u.created DESC ';
838 if (common_config('db','type') == 'pgsql') {
839 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
841 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
845 $apps = new Oauth_application_user();
847 $cnt = $apps->query(sprintf($qry, $this->id));
853 * Magic function called at serialize() time.
855 * We use this to drop a couple process-specific references
856 * from DB_DataObject which can cause trouble in future
859 * @return array of variable names to include in serialization.
864 $vars = parent::__sleep();
865 $skip = array('_profile');
866 return array_diff($vars, $skip);
869 static function recoverPassword($nore)
871 // $confirm_email will be used as a fallback if our user doesn't have a confirmed email
872 $confirm_email = null;
874 if (common_is_email($nore)) {
875 $user = User::getKV('email', common_canonical_email($nore));
877 // See if it's an unconfirmed email address
878 if (!$user instanceof User) {
879 // Warning: it may actually be legit to have multiple folks
880 // who have claimed, but not yet confirmed, the same address.
881 // We'll only send to the first one that comes up.
882 $confirm_email = new Confirm_address();
883 $confirm_email->address = common_canonical_email($nore);
884 $confirm_email->address_type = 'email';
885 if ($confirm_email->find(true)) {
886 $user = User::getKV('id', $confirm_email->user_id);
890 // No luck finding anyone by that email address.
891 if (!$user instanceof User) {
892 if (common_config('site', 'fakeaddressrecovery')) {
893 // Return without actually doing anything! We fake address recovery
894 // to avoid revealing which email addresses are registered with the site.
897 // TRANS: Information on password recovery form if no known e-mail address was specified.
898 throw new ClientException(_('No user with that email address exists here.'));
901 // This might throw a NicknameException on bad nicknames
902 $user = User::getKV('nickname', common_canonical_nickname($nore));
903 if (!$user instanceof User) {
904 // TRANS: Information on password recovery form if no known username was specified.
905 throw new ClientException(_('No user with that nickname exists here.'));
909 // Try to get an unconfirmed email address if they used a user name
910 if (empty($user->email) && $confirm_email === null) {
911 $confirm_email = new Confirm_address();
912 $confirm_email->user_id = $user->id;
913 $confirm_email->address_type = 'email';
914 $confirm_email->find();
915 if (!$confirm_email->fetch()) {
916 // Nothing found, so let's reset it to null
917 $confirm_email = null;
921 if (empty($user->email) && !$confirm_email instanceof Confirm_address) {
922 // TRANS: Client error displayed on password recovery form if a user does not have a registered e-mail address.
923 throw new ClientException(_('No registered email address for that user.'));
926 // Success! We have a valid user and a confirmed or unconfirmed email address
928 $confirm = new Confirm_address();
929 $confirm->code = common_confirmation_code(128);
930 $confirm->address_type = 'recover';
931 $confirm->user_id = $user->id;
932 $confirm->address = $user->email ?: $confirm_email->address;
934 if (!$confirm->insert()) {
935 common_log_db_error($confirm, 'INSERT', __FILE__);
936 // TRANS: Server error displayed if e-mail address confirmation fails in the database on the password recovery form.
937 throw new ServerException(_('Error saving address confirmation.'));
940 // @todo FIXME: needs i18n.
941 $body = "Hey, $user->nickname.";
943 $body .= 'Someone just asked for a new password ' .
944 'for this account on ' . common_config('site', 'name') . '.';
946 $body .= 'If it was you, and you want to confirm, use the URL below:';
948 $body .= "\t".common_local_url('recoverpassword',
949 array('code' => $confirm->code));
951 $body .= 'If not, just ignore this message.';
953 $body .= 'Thanks for your time, ';
955 $body .= common_config('site', 'name');
958 $headers = _mail_prepare_headers('recoverpassword', $user->nickname, $user->nickname);
959 // TRANS: Subject for password recovery e-mail.
960 mail_to_user($user, _('Password recovery requested'), $body, $headers, $confirm->address);
963 function streamModeOnly()
965 if (common_config('oldschool', 'enabled')) {
966 $osp = Old_school_prefs::getKV('user_id', $this->id);
968 return $osp->stream_mode_only;
975 function streamNicknames()
977 if (common_config('oldschool', 'enabled')) {
978 $osp = Old_school_prefs::getKV('user_id', $this->id);
980 return $osp->stream_nicknames;
986 function registrationActivity()
988 $profile = $this->getProfile();
990 $service = new ActivityObject();
992 $service->type = ActivityObject::SERVICE;
993 $service->title = common_config('site', 'name');
994 $service->link = common_root_url();
995 $service->id = $service->link;
997 $act = new Activity();
999 $act->actor = $profile->asActivityObject();
1000 $act->verb = ActivityVerb::JOIN;
1002 $act->objects[] = $service;
1004 $act->id = TagURI::mint('user:register:%d',
1007 $act->time = strtotime($this->created);
1009 $act->title = _("Register");
1011 $act->content = sprintf(_('%1$s joined %2$s.'),
1012 $profile->getBestName(),
1017 public function isPrivateStream()
1019 return $this->getProfile()->isPrivateStream();
1022 public function delPref($namespace, $topic)
1024 return $this->getProfile()->delPref($namespace, $topic);
1027 public function getPref($namespace, $topic, $default=null)
1029 return $this->getProfile()->getPref($namespace, $topic, $default);
1032 public function getConfigPref($namespace, $topic)
1034 return $this->getProfile()->getConfigPref($namespace, $topic);
1037 public function setPref($namespace, $topic, $data)
1039 return $this->getProfile()->setPref($namespace, $topic, $data);