3 * Laconica - a distributed open-source microblogging tool
4 * Copyright (C) 2008, 2009, Control Yourself, 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('LACONICA')) {
25 * Table Definition for user
28 require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
29 require_once 'Validate.php';
31 class User extends Memcached_DataObject
34 /* the code below is auto generated do not remove the above tag */
36 public $__table = 'user'; // table name
37 public $id; // int(4) primary_key not_null
38 public $nickname; // varchar(64) unique_key
39 public $password; // varchar(255)
40 public $email; // varchar(255) unique_key
41 public $incomingemail; // varchar(255) unique_key
42 public $emailnotifysub; // tinyint(1) default_1
43 public $emailnotifyfav; // tinyint(1) default_1
44 public $emailnotifynudge; // tinyint(1) default_1
45 public $emailnotifymsg; // tinyint(1) default_1
46 public $emailnotifyattn; // tinyint(1) default_1
47 public $emailmicroid; // tinyint(1) default_1
48 public $language; // varchar(50)
49 public $timezone; // varchar(50)
50 public $emailpost; // tinyint(1) default_1
51 public $jabber; // varchar(255) unique_key
52 public $jabbernotify; // tinyint(1)
53 public $jabberreplies; // tinyint(1)
54 public $jabbermicroid; // tinyint(1) default_1
55 public $updatefrompresence; // tinyint(1)
56 public $sms; // varchar(64) unique_key
57 public $carrier; // int(4)
58 public $smsnotify; // tinyint(1)
59 public $smsreplies; // tinyint(1)
60 public $smsemail; // varchar(255)
61 public $uri; // varchar(255) unique_key
62 public $autosubscribe; // tinyint(1)
63 public $urlshorteningservice; // varchar(50) default_ur1.ca
64 public $inboxed; // tinyint(1)
65 public $created; // datetime() not_null
66 public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
69 function staticGet($k,$v=NULL)
71 return Memcached_DataObject::staticGet('User',$k,$v);
74 /* the code above is auto generated do not remove the tag below */
79 return Profile::staticGet('id', $this->id);
82 function isSubscribed($other)
84 assert(!is_null($other));
85 // XXX: cache results of this query
86 $sub = Subscription::pkeyGet(array('subscriber' => $this->id,
87 'subscribed' => $other->id));
88 return (is_null($sub)) ? false : true;
91 // 'update' won't write key columns, so we have to do it ourselves.
93 function updateKeys(&$orig)
96 foreach (array('nickname', 'email', 'jabber', 'incomingemail', 'sms', 'carrier', 'smsemail', 'language', 'timezone') as $k) {
97 if (strcmp($this->$k, $orig->$k) != 0) {
98 $parts[] = $k . ' = ' . $this->_quote($this->$k);
101 if (count($parts) == 0) {
105 $toupdate = implode(', ', $parts);
107 $table = $this->tableName();
108 if(common_config('db','quote_identifiers')) {
109 $table = '"' . $table . '"';
111 $qry = 'UPDATE ' . $table . ' SET ' . $toupdate .
112 ' WHERE id = ' . $this->id;
114 $result = $this->query($qry);
121 function allowed_nickname($nickname)
123 // XXX: should already be validated for size, content, etc.
124 static $blacklist = array('rss', 'xrds', 'doc', 'main',
125 'settings', 'notice', 'user',
126 'search', 'avatar', 'tag', 'tags',
127 'api', 'message', 'group', 'groups',
129 $merged = array_merge($blacklist, common_config('nickname', 'blacklist'));
130 return !in_array($nickname, $merged);
133 function getCurrentNotice($dt=null)
135 $profile = $this->getProfile();
139 return $profile->getCurrentNotice($dt);
142 function getCarrier()
144 return Sms_carrier::staticGet('id', $this->carrier);
147 function subscribeTo($other)
149 $sub = new Subscription();
150 $sub->subscriber = $this->id;
151 $sub->subscribed = $other->id;
153 $sub->created = common_sql_now(); // current time
155 if (!$sub->insert()) {
162 function hasBlocked($other)
165 $block = Profile_block::get($this->id, $other->id);
167 if (is_null($block)) {
177 static function register($fields) {
179 // MAGICALLY put fields into current scope
183 $profile = new Profile();
185 $profile->query('BEGIN');
187 $profile->nickname = $nickname;
188 $profile->profileurl = common_profile_url($nickname);
190 if (!empty($fullname)) {
191 $profile->fullname = $fullname;
193 if (!empty($homepage)) {
194 $profile->homepage = $homepage;
197 $profile->bio = $bio;
199 if (!empty($location)) {
200 $profile->location = $location;
203 $profile->created = common_sql_now();
205 $id = $profile->insert();
208 common_log_db_error($profile, 'INSERT', __FILE__);
215 $user->nickname = $nickname;
217 if (!empty($password)) { // may not have a password for OpenID users
218 $user->password = common_munge_password($password, $id);
221 // Users who respond to invite email have proven their ownership of that address
224 $invite = Invitation::staticGet($code);
225 if ($invite && $invite->address && $invite->address_type == 'email' && $invite->address == $email) {
226 $user->email = $invite->address;
230 $inboxes = common_config('inboxes', 'enabled');
232 if ($inboxes === true || $inboxes == 'transitional') {
236 $user->created = common_sql_now();
237 $user->uri = common_user_uri($user);
239 $result = $user->insert();
242 common_log_db_error($user, 'INSERT', __FILE__);
246 // Everyone is subscribed to themself
248 $subscription = new Subscription();
249 $subscription->subscriber = $user->id;
250 $subscription->subscribed = $user->id;
251 $subscription->created = $user->created;
253 $result = $subscription->insert();
256 common_log_db_error($subscription, 'INSERT', __FILE__);
260 if (!empty($email) && !$user->email) {
262 $confirm = new Confirm_address();
263 $confirm->code = common_confirmation_code(128);
264 $confirm->user_id = $user->id;
265 $confirm->address = $email;
266 $confirm->address_type = 'email';
268 $result = $confirm->insert();
270 common_log_db_error($confirm, 'INSERT', __FILE__);
275 if (!empty($code) && $user->email) {
276 $user->emailChanged();
279 // Default system subscription
281 $defnick = common_config('newuser', 'default');
283 if (!empty($defnick)) {
284 $defuser = User::staticGet('nickname', $defnick);
285 if (empty($defuser)) {
286 common_log(LOG_WARNING, sprintf("Default user %s does not exist.", $defnick),
289 $defsub = new Subscription();
290 $defsub->subscriber = $user->id;
291 $defsub->subscribed = $defuser->id;
292 $defsub->created = $user->created;
294 $result = $defsub->insert();
297 common_log_db_error($defsub, 'INSERT', __FILE__);
303 $profile->query('COMMIT');
305 if ($email && !$user->email) {
306 mail_confirm_address($user, $confirm->code, $profile->nickname, $email);
311 $welcome = common_config('newuser', 'welcome');
313 if (!empty($welcome)) {
314 $welcomeuser = User::staticGet('nickname', $welcome);
315 if (empty($welcomeuser)) {
316 common_log(LOG_WARNING, sprintf("Welcome user %s does not exist.", $defnick),
319 $notice = Notice::saveNew($welcomeuser->id,
320 sprintf(_('Welcome to %1$s, @%2$s!'),
321 common_config('site', 'name'),
330 // Things we do when the email changes
332 function emailChanged()
335 $invites = new Invitation();
336 $invites->address = $this->email;
337 $invites->address_type = 'email';
339 if ($invites->find()) {
340 while ($invites->fetch()) {
341 $other = User::staticGet($invites->user_id);
342 subs_subscribe_to($other, $this);
347 function hasFave($notice)
349 $cache = common_memcache();
351 // XXX: Kind of a hack.
354 // This is the stream of favorite notices, in rev chron
355 // order. This forces it into cache.
357 $ids = Fave::stream($this->id, 0, NOTICE_CACHE_WINDOW);
359 // If it's in the list, then it's a fave
361 if (in_array($notice->id, $ids)) {
365 // If we're not past the end of the cache window,
366 // then the cache has all available faves, so this one
369 if (count($ids) < NOTICE_CACHE_WINDOW) {
373 // Otherwise, cache doesn't have all faves;
374 // fall through to the default
377 $fave = Fave::pkeyGet(array('user_id' => $this->id,
378 'notice_id' => $notice->id));
379 return ((is_null($fave)) ? false : true);
382 function mutuallySubscribed($other)
384 return $this->isSubscribed($other) &&
385 $other->isSubscribed($this);
388 function mutuallySubscribedUsers()
390 // 3-way join; probably should get cached
391 $UT = common_config('db','type')=='pgsql'?'"user"':'user';
392 $qry = "SELECT $UT.* " .
393 "FROM subscription sub1 JOIN $UT ON sub1.subscribed = $UT.id " .
394 "JOIN subscription sub2 ON $UT.id = sub2.subscriber " .
395 'WHERE sub1.subscriber = %d and sub2.subscribed = %d ' .
396 "ORDER BY $UT.nickname";
398 $user->query(sprintf($qry, $this->id, $this->id));
403 function getReplies($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0, $since=null)
405 $ids = Reply::stream($this->id, $offset, $limit, $since_id, $before_id, $since);
406 common_debug("Ids = " . implode(',', $ids));
407 return Notice::getStreamByIds($ids);
410 function getTaggedNotices($tag, $offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0, $since=null) {
411 $profile = $this->getProfile();
415 return $profile->getTaggedNotices($tag, $offset, $limit, $since_id, $before_id, $since);
419 function getNotices($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0, $since=null)
421 $profile = $this->getProfile();
425 return $profile->getNotices($offset, $limit, $since_id, $before_id, $since);
429 function favoriteNotices($offset=0, $limit=NOTICES_PER_PAGE)
431 $ids = Fave::stream($this->id, $offset, $limit);
432 return Notice::getStreamByIds($ids);
435 function noticesWithFriends($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0, $since=null)
437 $enabled = common_config('inboxes', 'enabled');
439 // Complicated code, depending on whether we support inboxes yet
440 // XXX: make this go away when inboxes become mandatory
442 if ($enabled === false ||
443 ($enabled == 'transitional' && $this->inboxed == 0)) {
446 'FROM notice JOIN subscription ON notice.profile_id = subscription.subscribed ' .
447 'WHERE subscription.subscriber = %d ';
448 return Notice::getStream(sprintf($qry, $this->id),
449 'user:notices_with_friends:' . $this->id,
450 $offset, $limit, $since_id, $before_id,
452 } else if ($enabled === true ||
453 ($enabled == 'transitional' && $this->inboxed == 1)) {
455 $ids = Notice_inbox::stream($this->id, $offset, $limit, $since_id, $before_id, $since);
457 return Notice::getStreamByIds($ids);
461 function blowFavesCache()
463 $cache = common_memcache();
465 // Faves don't happen chronologically, so we need to blow
467 $cache->delete(common_cache_key('fave:ids_by_user:'.$this->id));
468 $cache->delete(common_cache_key('fave:ids_by_user:'.$this->id.';last'));
472 function getSelfTags()
474 return Profile_tag::getTags($this->id, $this->id);
477 function setSelfTags($newtags)
479 return Profile_tag::setTags($this->id, $this->id, $newtags);
482 function block($other)
484 // Add a new block record
486 $block = new Profile_block();
488 // Begin a transaction
490 $block->query('BEGIN');
492 $block->blocker = $this->id;
493 $block->blocked = $other->id;
495 $result = $block->insert();
498 common_log_db_error($block, 'INSERT', __FILE__);
502 // Cancel their subscription, if it exists
504 $sub = Subscription::pkeyGet(array('subscriber' => $other->id,
505 'subscribed' => $this->id));
508 $result = $sub->delete();
510 common_log_db_error($sub, 'DELETE', __FILE__);
515 $block->query('COMMIT');
520 function unblock($other)
522 // Get the block record
524 $block = Profile_block::get($this->id, $other->id);
530 $result = $block->delete();
533 common_log_db_error($block, 'DELETE', __FILE__);
540 function isMember($group)
542 $profile = $this->getProfile();
543 return $profile->isMember($group);
546 function isAdmin($group)
548 $profile = $this->getProfile();
549 return $profile->isAdmin($group);
552 function getGroups($offset=0, $limit=null)
555 'SELECT user_group.* ' .
556 'FROM user_group JOIN group_member '.
557 'ON user_group.id = group_member.group_id ' .
558 'WHERE group_member.profile_id = %d ' .
559 'ORDER BY group_member.created DESC ';
562 if (common_config('db','type') == 'pgsql') {
563 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
565 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
569 $groups = new User_group();
571 $cnt = $groups->query(sprintf($qry, $this->id));
576 function getSubscriptions($offset=0, $limit=null)
579 'SELECT profile.* ' .
580 'FROM profile JOIN subscription ' .
581 'ON profile.id = subscription.subscribed ' .
582 'WHERE subscription.subscriber = %d ' .
583 'AND subscription.subscribed != subscription.subscriber ' .
584 'ORDER BY subscription.created DESC ';
586 if (common_config('db','type') == 'pgsql') {
587 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
589 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
592 $profile = new Profile();
594 $profile->query(sprintf($qry, $this->id));
599 function getSubscribers($offset=0, $limit=null)
602 'SELECT profile.* ' .
603 'FROM profile JOIN subscription ' .
604 'ON profile.id = subscription.subscriber ' .
605 'WHERE subscription.subscribed = %d ' .
606 'AND subscription.subscribed != subscription.subscriber ' .
607 'ORDER BY subscription.created DESC ';
610 if (common_config('db','type') == 'pgsql') {
611 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
613 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
617 $profile = new Profile();
619 $cnt = $profile->query(sprintf($qry, $this->id));
624 function getTaggedSubscribers($tag, $offset=0, $limit=null)
627 'SELECT profile.* ' .
628 'FROM profile JOIN subscription ' .
629 'ON profile.id = subscription.subscriber ' .
630 'JOIN profile_tag ON (profile_tag.tagged = subscription.subscriber ' .
631 'AND profile_tag.tagger = subscription.subscribed) ' .
632 'WHERE subscription.subscribed = %d ' .
633 "AND profile_tag.tag = '%s' " .
634 'AND subscription.subscribed != subscription.subscriber ' .
635 'ORDER BY subscription.created DESC ';
638 if (common_config('db','type') == 'pgsql') {
639 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
641 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
645 $profile = new Profile();
647 $cnt = $profile->query(sprintf($qry, $this->id, $tag));
652 function getTaggedSubscriptions($tag, $offset=0, $limit=null)
655 'SELECT profile.* ' .
656 'FROM profile JOIN subscription ' .
657 'ON profile.id = subscription.subscribed ' .
658 'JOIN profile_tag on (profile_tag.tagged = subscription.subscribed ' .
659 'AND profile_tag.tagger = subscription.subscriber) ' .
660 'WHERE subscription.subscriber = %d ' .
661 "AND profile_tag.tag = '%s' " .
662 'AND subscription.subscribed != subscription.subscriber ' .
663 'ORDER BY subscription.created DESC ';
665 if (common_config('db','type') == 'pgsql') {
666 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
668 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
671 $profile = new Profile();
673 $profile->query(sprintf($qry, $this->id, $tag));
680 $oid = new User_openid();
682 $oid->user_id = $this->id;