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('STATUSNET') && !defined('LACONICA')) {
25 * Table Definition for user
28 require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
29 require_once 'Validate.php';
31 class User extends Managed_DataObject
33 const SUBSCRIBE_POLICY_OPEN = 0;
34 const SUBSCRIBE_POLICY_MODERATE = 1;
37 /* the code below is auto generated do not remove the above tag */
39 public $__table = 'user'; // table name
40 public $id; // int(4) primary_key not_null
41 public $nickname; // varchar(64) unique_key
42 public $password; // varchar(255)
43 public $email; // varchar(255) unique_key
44 public $incomingemail; // varchar(255) unique_key
45 public $emailnotifysub; // tinyint(1) default_1
46 public $emailnotifyfav; // tinyint(1) default_1
47 public $emailnotifynudge; // tinyint(1) default_1
48 public $emailnotifymsg; // tinyint(1) default_1
49 public $emailnotifyattn; // tinyint(1) default_1
50 public $emailmicroid; // tinyint(1) default_1
51 public $language; // varchar(50)
52 public $timezone; // varchar(50)
53 public $emailpost; // tinyint(1) default_1
54 public $sms; // varchar(64) unique_key
55 public $carrier; // int(4)
56 public $smsnotify; // tinyint(1)
57 public $smsreplies; // tinyint(1)
58 public $smsemail; // varchar(255)
59 public $uri; // varchar(255) unique_key
60 public $autosubscribe; // tinyint(1)
61 public $subscribe_policy; // tinyint(1)
62 public $urlshorteningservice; // varchar(50) default_ur1.ca
63 public $inboxed; // tinyint(1)
64 public $private_stream; // tinyint(1) default_0
65 public $created; // datetime() not_null
66 public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
68 /* the code above is auto generated do not remove the tag below */
71 public static function schemaDef()
74 'description' => 'local users',
76 'id' => array('type' => 'int', 'not null' => true, 'description' => 'foreign key to profile table'),
77 'nickname' => array('type' => 'varchar', 'length' => 64, 'description' => 'nickname or username, duped in profile'),
78 'password' => array('type' => 'varchar', 'length' => 255, 'description' => 'salted password, can be null for OpenID users'),
79 'email' => array('type' => 'varchar', 'length' => 255, 'description' => 'email address for password recovery etc.'),
80 'incomingemail' => array('type' => 'varchar', 'length' => 255, 'description' => 'email address for post-by-email'),
81 'emailnotifysub' => array('type' => 'int', 'size' => 'tiny', 'default' => 1, 'description' => 'Notify by email of subscriptions'),
82 'emailnotifyfav' => array('type' => 'int', 'size' => 'tiny', 'default' => 1, 'description' => 'Notify by email of favorites'),
83 'emailnotifynudge' => array('type' => 'int', 'size' => 'tiny', 'default' => 1, 'description' => 'Notify by email of nudges'),
84 'emailnotifymsg' => array('type' => 'int', 'size' => 'tiny', 'default' => 1, 'description' => 'Notify by email of direct messages'),
85 'emailnotifyattn' => array('type' => 'int', 'size' => 'tiny', 'default' => 1, 'description' => 'Notify by email of @-replies'),
86 'emailmicroid' => array('type' => 'int', 'size' => 'tiny', 'default' => 1, 'description' => 'whether to publish email microid'),
87 'language' => array('type' => 'varchar', 'length' => 50, 'description' => 'preferred language'),
88 'timezone' => array('type' => 'varchar', 'length' => 50, 'description' => 'timezone'),
89 'emailpost' => array('type' => 'int', 'size' => 'tiny', 'default' => 1, 'description' => 'Post by email'),
90 'sms' => array('type' => 'varchar', 'length' => 64, 'description' => 'sms phone number'),
91 'carrier' => array('type' => 'int', 'description' => 'foreign key to sms_carrier'),
92 'smsnotify' => array('type' => 'int', 'size' => 'tiny', 'default' => 0, 'description' => 'whether to send notices to SMS'),
93 'smsreplies' => array('type' => 'int', 'size' => 'tiny', 'default' => 0, 'description' => 'whether to send notices to SMS on replies'),
94 'smsemail' => array('type' => 'varchar', 'length' => 255, 'description' => 'built from sms and carrier'),
95 'uri' => array('type' => 'varchar', 'length' => 255, 'description' => 'universally unique identifier, usually a tag URI'),
96 'autosubscribe' => array('type' => 'int', 'size' => 'tiny', 'default' => 0, 'description' => 'automatically subscribe to users who subscribe to us'),
97 'subscribe_policy' => array('type' => 'int', 'size' => 'tiny', 'default' => 0, 'description' => '0 = anybody can subscribe; 1 = require approval'),
98 'urlshorteningservice' => array('type' => 'varchar', 'length' => 50, 'default' => 'internal', 'description' => 'service to use for auto-shortening URLs'),
99 'inboxed' => array('type' => 'int', 'size' => 'tiny', 'default' => 0, 'description' => 'has an inbox been created for this user?'),
100 'private_stream' => array('type' => 'int', 'size' => 'tiny', 'default' => 0, 'description' => 'whether to limit all notices to followers only'),
102 'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'),
103 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
105 'primary key' => array('id'),
106 'unique keys' => array(
107 'user_nickname_key' => array('nickname'),
108 'user_email_key' => array('email'),
109 'user_incomingemail_key' => array('incomingemail'),
110 'user_sms_key' => array('sms'),
111 'user_uri_key' => array('uri'),
113 'foreign keys' => array(
114 'user_id_fkey' => array('profile', array('id' => 'id')),
115 'user_carrier_fkey' => array('sms_carrier', array('carrier' => 'id')),
118 'user_smsemail_idx' => array('smsemail'),
123 protected $_profile = null;
128 * @throws UserNoProfileException if user has no profile
130 public function getProfile()
132 if (!($this->_profile instanceof Profile)) {
133 $this->_profile = Profile::getKV('id', $this->id);
134 if (!($this->_profile instanceof Profile)) {
135 throw new UserNoProfileException($this);
139 return $this->_profile;
142 function isSubscribed($other)
144 return $this->getProfile()->isSubscribed($other);
147 function hasPendingSubscription($other)
149 return $this->getProfile()->hasPendingSubscription($other);
152 // 'update' won't write key columns, so we have to do it ourselves.
154 function updateKeys(&$orig)
158 foreach (array('nickname', 'email', 'incomingemail', 'sms', 'carrier', 'smsemail') as $k) {
159 if (strcmp($this->$k, $orig->$k) != 0) {
160 $parts[] = $k . ' = ' . $this->_quote($this->$k);
163 if (count($parts) == 0) {
167 $toupdate = implode(', ', $parts);
169 $table = common_database_tablename($this->tableName());
170 $qry = 'UPDATE ' . $table . ' SET ' . $toupdate .
171 ' WHERE id = ' . $this->id;
173 $result = $this->query($qry);
181 * Get the most recent notice posted by this user, if any.
183 * @return mixed Notice or null
185 function getCurrentNotice()
187 return $this->getProfile()->getCurrentNotice();
190 function getCarrier()
192 return Sms_carrier::getKV('id', $this->carrier);
195 function hasBlocked($other)
197 return $this->getProfile()->hasBlocked($other);
201 * Register a new user account and profile and set up default subscriptions.
202 * If a new-user welcome message is configured, this will be sent.
204 * @param array $fields associative array of optional properties
207 * bool 'email_confirmed' pass true to mark email as pre-confirmed
210 * string 'location' informal string description of geolocation
211 * float 'lat' decimal latitude for geolocation
212 * float 'lon' decimal longitude for geolocation
213 * int 'location_id' geoname identifier
214 * int 'location_ns' geoname namespace to interpret location_id
215 * string 'nickname' REQUIRED
216 * string 'password' (may be missing for eg OpenID registrations)
217 * string 'code' invite code
218 * ?string 'uri' permalink to notice; defaults to local notice URL
219 * @return mixed User object or false on failure
221 static function register(array $fields) {
223 // MAGICALLY put fields into current scope
227 $profile = new Profile();
229 if (!empty($email)) {
230 $email = common_canonical_email($email);
234 $profile->nickname = Nickname::normalize($nickname, true);
235 } catch (NicknameException $e) {
236 common_log(LOG_WARNING, sprintf('Bad nickname during User registration for %s: %s', $profile->nickname, $e->getMessage()), __FILE__);
240 $profile->profileurl = common_profile_url($profile->nickname);
242 if (!empty($fullname)) {
243 $profile->fullname = $fullname;
245 if (!empty($homepage)) {
246 $profile->homepage = $homepage;
249 $profile->bio = $bio;
251 if (!empty($location)) {
252 $profile->location = $location;
254 $loc = Location::fromName($location);
257 $profile->lat = $loc->lat;
258 $profile->lon = $loc->lon;
259 $profile->location_id = $loc->location_id;
260 $profile->location_ns = $loc->location_ns;
264 $profile->created = common_sql_now();
268 $user->nickname = $profile->nickname;
272 // Users who respond to invite email have proven their ownership of that address
275 $invite = Invitation::getKV($code);
276 if ($invite && $invite->address && $invite->address_type == 'email' && $invite->address == $email) {
277 $user->email = $invite->address;
281 if(isset($email_confirmed) && $email_confirmed) {
282 $user->email = $email;
285 // This flag is ignored but still set to 1
289 // Set default-on options here, otherwise they'll be disabled
290 // initially for sites using caching, since the initial encache
291 // doesn't know about the defaults in the database.
292 $user->emailnotifysub = 1;
293 $user->emailnotifyfav = 1;
294 $user->emailnotifynudge = 1;
295 $user->emailnotifymsg = 1;
296 $user->emailnotifyattn = 1;
297 $user->emailmicroid = 1;
298 $user->emailpost = 1;
299 $user->jabbermicroid = 1;
301 $user->created = common_sql_now();
303 if (Event::handle('StartUserRegister', array($profile))) {
305 $profile->query('BEGIN');
307 $id = $profile->insert();
309 common_log_db_error($profile, 'INSERT', __FILE__);
318 $user->uri = common_user_uri($user);
321 if (!empty($password)) { // may not have a password for OpenID users
322 $user->password = common_munge_password($password, $id);
325 $result = $user->insert();
328 common_log_db_error($user, 'INSERT', __FILE__);
329 $profile->query('ROLLBACK');
333 // Everyone gets an inbox
335 $inbox = new Inbox();
337 $inbox->user_id = $user->id;
338 $inbox->notice_ids = '';
340 $result = $inbox->insert();
343 common_log_db_error($inbox, 'INSERT', __FILE__);
347 // Everyone is subscribed to themself
349 $subscription = new Subscription();
350 $subscription->subscriber = $user->id;
351 $subscription->subscribed = $user->id;
352 $subscription->created = $user->created;
354 $result = $subscription->insert();
357 common_log_db_error($subscription, 'INSERT', __FILE__);
358 $profile->query('ROLLBACK');
362 // Mark that this invite was converted
364 if (!empty($invite)) {
365 $invite->convert($user);
368 if (!empty($email) && !$user->email) {
370 $confirm = new Confirm_address();
371 $confirm->code = common_confirmation_code(128);
372 $confirm->user_id = $user->id;
373 $confirm->address = $email;
374 $confirm->address_type = 'email';
376 $result = $confirm->insert();
379 common_log_db_error($confirm, 'INSERT', __FILE__);
380 $profile->query('ROLLBACK');
385 if (!empty($code) && $user->email) {
386 $user->emailChanged();
389 // Default system subscription
391 $defnick = common_config('newuser', 'default');
393 if (!empty($defnick)) {
394 $defuser = User::getKV('nickname', $defnick);
395 if (empty($defuser)) {
396 common_log(LOG_WARNING, sprintf("Default user %s does not exist.", $defnick),
399 Subscription::start($profile, $defuser->getProfile());
403 $profile->query('COMMIT');
405 if (!empty($email) && !$user->email) {
406 mail_confirm_address($user, $confirm->code, $profile->nickname, $email);
411 $welcome = common_config('newuser', 'welcome');
413 if (!empty($welcome)) {
414 $welcomeuser = User::getKV('nickname', $welcome);
415 if (empty($welcomeuser)) {
416 common_log(LOG_WARNING, sprintf("Welcome user %s does not exist.", $defnick),
419 $notice = Notice::saveNew($welcomeuser->id,
420 // TRANS: Notice given on user registration.
421 // TRANS: %1$s is the sitename, $2$s is the registering user's nickname.
422 sprintf(_('Welcome to %1$s, @%2$s!'),
423 common_config('site', 'name'),
429 Event::handle('EndUserRegister', array($profile));
435 // Things we do when the email changes
436 function emailChanged()
439 $invites = new Invitation();
440 $invites->address = $this->email;
441 $invites->address_type = 'email';
443 if ($invites->find()) {
444 while ($invites->fetch()) {
446 $other = Profile::getKV('id', $invites->user_id);
447 if (!($other instanceof Profile)) { // remove when getKV throws exceptions
450 Subscription::start($other, $this->getProfile());
451 } catch (Exception $e) {
458 function hasFave($notice)
460 return $this->getProfile()->hasFave($notice);
463 function mutuallySubscribed($other)
465 return $this->getProfile()->mutuallySubscribed($other);
468 function mutuallySubscribedUsers()
470 // 3-way join; probably should get cached
471 $UT = common_config('db','type')=='pgsql'?'"user"':'user';
472 $qry = "SELECT $UT.* " .
473 "FROM subscription sub1 JOIN $UT ON sub1.subscribed = $UT.id " .
474 "JOIN subscription sub2 ON $UT.id = sub2.subscriber " .
475 'WHERE sub1.subscriber = %d and sub2.subscribed = %d ' .
476 "ORDER BY $UT.nickname";
478 $user->query(sprintf($qry, $this->id, $this->id));
483 function getReplies($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0)
485 return Reply::stream($this->id, $offset, $limit, $since_id, $before_id);
488 function getTaggedNotices($tag, $offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0) {
489 return $this->getProfile()->getTaggedNotices($tag, $offset, $limit, $since_id, $before_id);
492 function getNotices($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0)
494 return $this->getProfile()->getNotices($offset, $limit, $since_id, $before_id);
497 function favoriteNotices($own=false, $offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $max_id=0)
499 return $this->getProfile()->favoriteNotices($own, $offset, $limit, $since_id, $max_id);
502 function noticeInbox($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0)
504 $stream = new InboxNoticeStream($this);
505 return $stream->getNotices($offset, $limit, $since_id, $before_id);
508 // DEPRECATED, use noticeInbox()
510 function noticesWithFriends($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0)
512 return $this->noticeInbox($offset, $limit, $since_id, $before_id);
515 // DEPRECATED, use noticeInbox()
517 function noticesWithFriendsThreaded($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0)
519 return $this->noticeInbox($offset, $limit, $since_id, $before_id);
522 // DEPRECATED, use noticeInbox()
524 function noticeInboxThreaded($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0)
526 return $this->noticeInbox($offset, $limit, $since_id, $before_id);
529 // DEPRECATED, use noticeInbox()
531 function friendsTimeline($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0)
533 return $this->noticeInbox($offset, $limit, $since_id, $before_id);
536 // DEPRECATED, use noticeInbox()
538 function ownFriendsTimeline($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0)
540 $this->noticeInbox($offset, $limit, $since_id, $before_id);
543 function blowFavesCache()
545 $this->getProfile()->blowFavesCache();
548 function getSelfTags()
550 return Profile_tag::getTagsArray($this->id, $this->id, $this->id);
553 function setSelfTags($newtags, $privacy)
555 return Profile_tag::setTags($this->id, $this->id, $newtags, $privacy);
558 function block(Profile $other)
560 // Add a new block record
562 // no blocking (and thus unsubbing from) yourself
564 if ($this->id == $other->id) {
565 common_log(LOG_WARNING,
567 "Profile ID %d (%s) tried to block themself.",
575 $block = new Profile_block();
577 // Begin a transaction
579 $block->query('BEGIN');
581 $block->blocker = $this->id;
582 $block->blocked = $other->id;
584 $result = $block->insert();
587 common_log_db_error($block, 'INSERT', __FILE__);
591 $self = $this->getProfile();
592 if (Subscription::exists($other, $self)) {
593 Subscription::cancel($other, $self);
595 if (Subscription::exists($self, $other)) {
596 Subscription::cancel($self, $other);
599 $block->query('COMMIT');
604 function unblock(Profile $other)
606 // Get the block record
608 $block = Profile_block::exists($this->getProfile(), $other);
614 $result = $block->delete();
617 common_log_db_error($block, 'DELETE', __FILE__);
624 function isMember($group)
626 return $this->getProfile()->isMember($group);
629 function isAdmin($group)
631 return $this->getProfile()->isAdmin($group);
634 function getGroups($offset=0, $limit=null)
636 return $this->getProfile()->getGroups($offset, $limit);
640 * Request to join the given group.
641 * May throw exceptions on failure.
643 * @param User_group $group
644 * @return Group_member
646 function joinGroup(User_group $group)
648 return $this->getProfile()->joinGroup($group);
652 * Leave a group that this user is a member of.
654 * @param User_group $group
656 function leaveGroup(User_group $group)
658 return $this->getProfile()->leaveGroup($group);
661 function getSubscribed($offset=0, $limit=null)
663 return $this->getProfile()->getSubscribed($offset, $limit);
666 function getSubscribers($offset=0, $limit=null)
668 return $this->getProfile()->getSubscribers($offset, $limit);
671 function getTaggedSubscribers($tag, $offset=0, $limit=null)
673 return $this->getProfile()->getTaggedSubscribers($tag, $offset, $limit);
676 function getTaggedSubscriptions($tag, $offset=0, $limit=null)
678 return $this->getProfile()->getTaggedSubscriptions($tag, $offset, $limit);
681 function hasRight($right)
683 return $this->getProfile()->hasRight($right);
688 if (empty($this->id)) {
689 common_log(LOG_WARNING, "Ambiguous User->delete(); skipping related tables.");
690 return parent::delete();
694 $profile = $this->getProfile();
696 } catch (UserNoProfileException $unp) {
697 common_log(LOG_INFO, "User {$this->nickname} has no profile; continuing deletion.");
700 $related = array('Fave',
707 Event::handle('UserDeleteRelated', array($this, &$related));
709 foreach ($related as $cls) {
711 $inst->user_id = $this->id;
715 $this->_deleteTags();
716 $this->_deleteBlocks();
721 function _deleteTags()
723 $tag = new Profile_tag();
724 $tag->tagger = $this->id;
728 function _deleteBlocks()
730 $block = new Profile_block();
731 $block->blocker = $this->id;
733 // XXX delete group block? Reset blocker?
736 function hasRole($name)
738 return $this->getProfile()->hasRole($name);
741 function grantRole($name)
743 return $this->getProfile()->grantRole($name);
746 function revokeRole($name)
748 return $this->getProfile()->revokeRole($name);
751 function isSandboxed()
753 return $this->getProfile()->isSandboxed();
756 function isSilenced()
758 return $this->getProfile()->isSilenced();
761 function repeatedByMe($offset=0, $limit=20, $since_id=null, $max_id=null)
763 $stream = new RepeatedByMeNoticeStream($this);
764 return $stream->getNotices($offset, $limit, $since_id, $max_id);
768 function repeatsOfMe($offset=0, $limit=20, $since_id=null, $max_id=null)
770 $stream = new RepeatsOfMeNoticeStream($this);
772 return $stream->getNotices($offset, $limit, $since_id, $max_id);
776 function repeatedToMe($offset=0, $limit=20, $since_id=null, $max_id=null)
778 // TRANS: Exception thrown when trying view "repeated to me".
779 throw new Exception(_('Not implemented since inbox change.'));
782 public static function siteOwner()
784 $owner = self::cacheGet('user:site_owner');
786 if ($owner === false) { // cache miss
788 $pr = new Profile_role();
789 $pr->role = Profile_role::OWNER;
790 $pr->orderBy('created');
793 if (!$pr->find(true)) {
794 throw new NoResultException($pr);
797 $owner = User::getKV('id', $pr->profile_id);
799 self::cacheSet('user:site_owner', $owner);
802 if ($owner instanceof User) {
806 throw new ServerException(_('No site owner configured.'));
810 * Pull the primary site account to use in single-user mode.
811 * If a valid user nickname is listed in 'singleuser':'nickname'
812 * in the config, this will be used; otherwise the site owner
813 * account is taken by default.
816 * @throws ServerException if no valid single user account is present
817 * @throws ServerException if called when not in single-user mode
819 public static function singleUser()
821 if (!common_config('singleuser', 'enabled')) {
822 // TRANS: Server exception.
823 throw new ServerException(_('Single-user mode code called when not enabled.'));
826 if ($nickname = common_config('singleuser', 'nickname')) {
827 $user = User::getKV('nickname', $nickname);
828 if ($user instanceof User) {
833 // If there was no nickname or no user by that nickname,
834 // try the site owner. Throws exception if not configured.
835 return User::siteOwner();
839 * This is kind of a hack for using external setup code that's trying to
840 * build single-user sites.
842 * Will still return a username if the config singleuser/nickname is set
843 * even if the account doesn't exist, which normally indicates that the
844 * site is horribly misconfigured.
846 * At the moment, we need to let it through so that router setup can
847 * complete, otherwise we won't be able to create the account.
849 * This will be easier when we can more easily create the account and
850 * *then* switch the site to 1user mode without jumping through hoops.
853 * @throws ServerException if no valid single user account is present
854 * @throws ServerException if called when not in single-user mode
856 static function singleUserNickname()
859 $user = User::singleUser();
860 return $user->nickname;
861 } catch (Exception $e) {
862 if (common_config('singleuser', 'enabled') && common_config('singleuser', 'nickname')) {
863 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.");
864 return common_config('singleuser', 'nickname');
871 * Find and shorten links in the given text using this user's URL shortening
874 * By default, links will be left untouched if the text is shorter than the
875 * configured maximum notice length. Pass true for the $always parameter
876 * to force all links to be shortened regardless.
878 * Side effects: may save file and file_redirection records for referenced URLs.
880 * @param string $text
881 * @param boolean $always
884 public function shortenLinks($text, $always=false)
886 return common_shorten_links($text, $always, $this);
890 * Get a list of OAuth client applications that have access to this
893 function getConnectedApps($offset = 0, $limit = null)
897 'FROM oauth_application_user u, oauth_application a ' .
898 'WHERE u.profile_id = %d ' .
899 'AND a.id = u.application_id ' .
900 'AND u.access_type > 0 ' .
901 'ORDER BY u.created DESC ';
904 if (common_config('db','type') == 'pgsql') {
905 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
907 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
911 $apps = new Oauth_application_user();
913 $cnt = $apps->query(sprintf($qry, $this->id));
919 * Magic function called at serialize() time.
921 * We use this to drop a couple process-specific references
922 * from DB_DataObject which can cause trouble in future
925 * @return array of variable names to include in serialization.
930 $vars = parent::__sleep();
931 $skip = array('_profile');
932 return array_diff($vars, $skip);
935 static function recoverPassword($nore)
937 $user = User::getKV('email', common_canonical_email($nore));
941 $user = User::getKV('nickname', common_canonical_nickname($nore));
942 } catch (NicknameException $e) {
947 // See if it's an unconfirmed email address
950 // Warning: it may actually be legit to have multiple folks
951 // who have claimed, but not yet confirmed, the same address.
952 // We'll only send to the first one that comes up.
953 $confirm_email = new Confirm_address();
954 $confirm_email->address = common_canonical_email($nore);
955 $confirm_email->address_type = 'email';
956 $confirm_email->find();
957 if ($confirm_email->fetch()) {
958 $user = User::getKV($confirm_email->user_id);
960 $confirm_email = null;
963 $confirm_email = null;
967 // TRANS: Information on password recovery form if no known username or e-mail address was specified.
968 throw new ClientException(_('No user with that email address or username.'));
972 // Try to get an unconfirmed email address if they used a user name
974 if (!$user->email && !$confirm_email) {
975 $confirm_email = new Confirm_address();
976 $confirm_email->user_id = $user->id;
977 $confirm_email->address_type = 'email';
978 $confirm_email->find();
979 if (!$confirm_email->fetch()) {
980 $confirm_email = null;
984 if (!$user->email && !$confirm_email) {
985 // TRANS: Client error displayed on password recovery form if a user does not have a registered e-mail address.
986 throw new ClientException(_('No registered email address for that user.'));
990 // Success! We have a valid user and a confirmed or unconfirmed email address
992 $confirm = new Confirm_address();
993 $confirm->code = common_confirmation_code(128);
994 $confirm->address_type = 'recover';
995 $confirm->user_id = $user->id;
996 $confirm->address = (!empty($user->email)) ? $user->email : $confirm_email->address;
998 if (!$confirm->insert()) {
999 common_log_db_error($confirm, 'INSERT', __FILE__);
1000 // TRANS: Server error displayed if e-mail address confirmation fails in the database on the password recovery form.
1001 throw new ServerException(_('Error saving address confirmation.'));
1005 // @todo FIXME: needs i18n.
1006 $body = "Hey, $user->nickname.";
1008 $body .= 'Someone just asked for a new password ' .
1009 'for this account on ' . common_config('site', 'name') . '.';
1011 $body .= 'If it was you, and you want to confirm, use the URL below:';
1013 $body .= "\t".common_local_url('recoverpassword',
1014 array('code' => $confirm->code));
1016 $body .= 'If not, just ignore this message.';
1018 $body .= 'Thanks for your time, ';
1020 $body .= common_config('site', 'name');
1023 $headers = _mail_prepare_headers('recoverpassword', $user->nickname, $user->nickname);
1024 // TRANS: Subject for password recovery e-mail.
1025 mail_to_user($user, _('Password recovery requested'), $body, $headers, $confirm->address);
1028 function streamModeOnly()
1030 if (common_config('oldschool', 'enabled')) {
1031 $osp = Old_school_prefs::getKV('user_id', $this->id);
1033 return $osp->stream_mode_only;
1040 function conversationTree()
1042 if (common_config('oldschool', 'enabled')) {
1043 $osp = Old_school_prefs::getKV('user_id', $this->id);
1045 return $osp->conversation_tree;
1052 function streamNicknames()
1054 if (common_config('oldschool', 'enabled')) {
1055 $osp = Old_school_prefs::getKV('user_id', $this->id);
1057 return $osp->stream_nicknames;
1063 function registrationActivity()
1065 $profile = $this->getProfile();
1067 $service = new ActivityObject();
1069 $service->type = ActivityObject::SERVICE;
1070 $service->title = common_config('site', 'name');
1071 $service->link = common_root_url();
1072 $service->id = $service->link;
1074 $act = new Activity();
1076 $act->actor = ActivityObject::fromProfile($profile);
1077 $act->verb = ActivityVerb::JOIN;
1079 $act->objects[] = $service;
1081 $act->id = TagURI::mint('user:register:%d',
1084 $act->time = strtotime($this->created);
1086 $act->title = _("Register");
1088 $act->content = sprintf(_('%1$s joined %2$s.'),
1089 $profile->getBestName(),