3 * Laconica - a distributed open-source microblogging tool
4 * Copyright (C) 2008, Controlez-Vous, 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')) { exit(1); }
23 * Table Definition for user
25 require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
26 require_once 'Validate.php';
28 class User extends Memcached_DataObject
31 /* the code below is auto generated do not remove the above tag */
33 public $__table = 'user'; // table name
34 public $id; // int(4) primary_key not_null
35 public $nickname; // varchar(64) unique_key
36 public $password; // varchar(255)
37 public $email; // varchar(255) unique_key
38 public $incomingemail; // varchar(255) unique_key
39 public $emailnotifysub; // tinyint(1) default_1
40 public $emailnotifyfav; // tinyint(1) default_1
41 public $emailnotifynudge; // tinyint(1) default_1
42 public $emailnotifymsg; // tinyint(1) default_1
43 public $emailmicroid; // tinyint(1) default_1
44 public $language; // varchar(50)
45 public $timezone; // varchar(50)
46 public $emailpost; // tinyint(1) default_1
47 public $jabber; // varchar(255) unique_key
48 public $jabbernotify; // tinyint(1)
49 public $jabberreplies; // tinyint(1)
50 public $jabbermicroid; // tinyint(1) default_1
51 public $updatefrompresence; // tinyint(1)
52 public $sms; // varchar(64) unique_key
53 public $carrier; // int(4)
54 public $smsnotify; // tinyint(1)
55 public $smsreplies; // tinyint(1)
56 public $smsemail; // varchar(255)
57 public $uri; // varchar(255) unique_key
58 public $autosubscribe; // tinyint(1)
59 public $urlshorteningservice; // varchar(50) default_ur1.ca
60 public $inboxed; // tinyint(1)
61 public $created; // datetime() not_null
62 public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
65 function staticGet($k,$v=null)
66 { return Memcached_DataObject::staticGet('User',$k,$v); }
68 /* the code above is auto generated do not remove the tag below */
73 return Profile::staticGet('id', $this->id);
76 function isSubscribed($other)
78 assert(!is_null($other));
79 # XXX: cache results of this query
80 $sub = Subscription::pkeyGet(array('subscriber' => $this->id,
81 'subscribed' => $other->id));
82 return (is_null($sub)) ? false : true;
85 # 'update' won't write key columns, so we have to do it ourselves.
87 function updateKeys(&$orig)
90 foreach (array('nickname', 'email', 'jabber', 'incomingemail', 'sms', 'carrier', 'smsemail', 'language', 'timezone') as $k) {
91 if (strcmp($this->$k, $orig->$k) != 0) {
92 $parts[] = $k . ' = ' . $this->_quote($this->$k);
95 if (count($parts) == 0) {
99 $toupdate = implode(', ', $parts);
101 $table = $this->tableName();
102 if(common_config('db','quote_identifiers')) {
103 $table = '"' . $table . '"';
105 $qry = 'UPDATE ' . $table . ' SET ' . $toupdate .
106 ' WHERE id = ' . $this->id;
108 $result = $this->query($qry);
115 function allowed_nickname($nickname)
117 # XXX: should already be validated for size, content, etc.
118 static $blacklist = array('rss', 'xrds', 'doc', 'main',
119 'settings', 'notice', 'user',
120 'search', 'avatar', 'tag', 'tags',
121 'api', 'message', 'group', 'groups');
122 $merged = array_merge($blacklist, common_config('nickname', 'blacklist'));
123 return !in_array($nickname, $merged);
126 function getCurrentNotice($dt=null)
128 $profile = $this->getProfile();
132 return $profile->getCurrentNotice($dt);
135 function getCarrier()
137 return Sms_carrier::staticGet('id', $this->carrier);
140 function subscribeTo($other)
142 $sub = new Subscription();
143 $sub->subscriber = $this->id;
144 $sub->subscribed = $other->id;
146 $sub->created = common_sql_now(); # current time
148 if (!$sub->insert()) {
155 function hasBlocked($other)
158 $block = Profile_block::get($this->id, $other->id);
160 if (is_null($block)) {
170 static function register($fields) {
172 # MAGICALLY put fields into current scope
176 $profile = new Profile();
178 $profile->query('BEGIN');
180 $profile->nickname = $nickname;
181 $profile->profileurl = common_profile_url($nickname);
184 $profile->fullname = $fullname;
187 $profile->homepage = $homepage;
190 $profile->bio = $bio;
193 $profile->location = $location;
196 $profile->created = common_sql_now();
198 $id = $profile->insert();
201 common_log_db_error($profile, 'INSERT', __FILE__);
208 $user->nickname = $nickname;
210 if ($password) { # may not have a password for OpenID users
211 $user->password = common_munge_password($password, $id);
214 # Users who respond to invite email have proven their ownership of that address
217 $invite = Invitation::staticGet($code);
218 if ($invite && $invite->address && $invite->address_type == 'email' && $invite->address == $email) {
219 $user->email = $invite->address;
223 $inboxes = common_config('inboxes', 'enabled');
225 if ($inboxes === true || $inboxes == 'transitional') {
229 $user->created = common_sql_now();
230 $user->uri = common_user_uri($user);
232 $result = $user->insert();
235 common_log_db_error($user, 'INSERT', __FILE__);
239 # Everyone is subscribed to themself
241 $subscription = new Subscription();
242 $subscription->subscriber = $user->id;
243 $subscription->subscribed = $user->id;
244 $subscription->created = $user->created;
246 $result = $subscription->insert();
249 common_log_db_error($subscription, 'INSERT', __FILE__);
253 if ($email && !$user->email) {
255 $confirm = new Confirm_address();
256 $confirm->code = common_confirmation_code(128);
257 $confirm->user_id = $user->id;
258 $confirm->address = $email;
259 $confirm->address_type = 'email';
261 $result = $confirm->insert();
263 common_log_db_error($confirm, 'INSERT', __FILE__);
268 if ($code && $user->email) {
269 $user->emailChanged();
272 $profile->query('COMMIT');
274 if ($email && !$user->email) {
275 mail_confirm_address($user, $confirm->code, $profile->nickname, $email);
281 # Things we do when the email changes
283 function emailChanged()
286 $invites = new Invitation();
287 $invites->address = $this->email;
288 $invites->address_type = 'email';
290 if ($invites->find()) {
291 while ($invites->fetch()) {
292 $other = User::staticGet($invites->user_id);
293 subs_subscribe_to($other, $this);
298 function hasFave($notice)
300 $cache = common_memcache();
302 # XXX: Kind of a hack.
304 # This is the stream of favorite notices, in rev chron
305 # order. This forces it into cache.
306 $faves = $this->favoriteNotices(0, NOTICE_CACHE_WINDOW);
308 while ($faves->fetch()) {
309 if ($faves->id < $notice->id) {
310 # If we passed it, it's not a fave
312 } else if ($faves->id == $notice->id) {
313 # If it matches a cached notice, then it's a fave
318 # If we're not past the end of the cache window,
319 # then the cache has all available faves, so this one
321 if ($cnt < NOTICE_CACHE_WINDOW) {
324 # Otherwise, cache doesn't have all faves;
325 # fall through to the default
327 $fave = Fave::pkeyGet(array('user_id' => $this->id,
328 'notice_id' => $notice->id));
329 return ((is_null($fave)) ? false : true);
331 function mutuallySubscribed($other)
333 return $this->isSubscribed($other) &&
334 $other->isSubscribed($this);
337 function mutuallySubscribedUsers()
340 # 3-way join; probably should get cached
341 $UT = common_config('db','type')=='pgsql'?'"user"':'user';
342 $qry = "SELECT $UT.* " .
343 "FROM subscription sub1 JOIN $UT ON sub1.subscribed = $UT.id " .
344 "JOIN subscription sub2 ON $UT.id = sub2.subscriber " .
345 'WHERE sub1.subscriber = %d and sub2.subscribed = %d ' .
346 "ORDER BY $UT.nickname";
348 $user->query(sprintf($qry, $this->id, $this->id));
353 function getReplies($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0, $since=null)
357 'FROM notice JOIN reply ON notice.id = reply.notice_id ' .
358 'WHERE reply.profile_id = %d ';
359 return Notice::getStream(sprintf($qry, $this->id),
360 'user:replies:'.$this->id,
361 $offset, $limit, $since_id, $before_id, null, $since);
364 function getNotices($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0, $since=null)
366 $profile = $this->getProfile();
370 return $profile->getNotices($offset, $limit, $since_id, $before_id);
374 function favoriteNotices($offset=0, $limit=NOTICES_PER_PAGE)
378 'FROM notice JOIN fave ON notice.id = fave.notice_id ' .
379 'WHERE fave.user_id = %d ';
380 return Notice::getStream(sprintf($qry, $this->id),
381 'user:faves:'.$this->id,
385 function noticesWithFriends($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0, $since=null)
387 $enabled = common_config('inboxes', 'enabled');
389 # Complicated code, depending on whether we support inboxes yet
390 # XXX: make this go away when inboxes become mandatory
392 if ($enabled === false ||
393 ($enabled == 'transitional' && $this->inboxed == 0)) {
396 'FROM notice JOIN subscription ON notice.profile_id = subscription.subscribed ' .
397 'WHERE subscription.subscriber = %d ';
399 } else if ($enabled === true ||
400 ($enabled == 'transitional' && $this->inboxed == 1)) {
404 'FROM notice JOIN notice_inbox ON notice.id = notice_inbox.notice_id ' .
405 'WHERE notice_inbox.user_id = %d ';
406 # NOTE: we override ORDER
409 return Notice::getStream(sprintf($qry, $this->id),
410 'user:notices_with_friends:' . $this->id,
411 $offset, $limit, $since_id, $before_id,
415 function blowFavesCache()
417 $cache = common_memcache();
419 # Faves don't happen chronologically, so we need to blow
421 $cache->delete(common_cache_key('user:faves:'.$this->id));
422 $cache->delete(common_cache_key('user:faves:'.$this->id).';last');
426 function getSelfTags()
428 return Profile_tag::getTags($this->id, $this->id);
431 function setSelfTags($newtags)
433 return Profile_tag::setTags($this->id, $this->id, $newtags);
436 function block($other)
439 # Add a new block record
441 $block = new Profile_block();
443 # Begin a transaction
445 $block->query('BEGIN');
447 $block->blocker = $this->id;
448 $block->blocked = $other->id;
450 $result = $block->insert();
453 common_log_db_error($block, 'INSERT', __FILE__);
457 # Cancel their subscription, if it exists
459 $sub = Subscription::pkeyGet(array('subscriber' => $other->id,
460 'subscribed' => $this->id));
463 $result = $sub->delete();
465 common_log_db_error($sub, 'DELETE', __FILE__);
470 $block->query('COMMIT');
475 function unblock($other)
478 # Get the block record
480 $block = Profile_block::get($this->id, $other->id);
486 $result = $block->delete();
489 common_log_db_error($block, 'DELETE', __FILE__);
496 function isMember($group)
498 $profile = $this->getProfile();
499 return $profile->isMember($group);
502 function isAdmin($group)
504 $profile = $this->getProfile();
505 return $profile->isAdmin($group);
508 function getGroups($offset=0, $limit=null)
511 'SELECT user_group.* ' .
512 'FROM user_group JOIN group_member '.
513 'ON user_group.id = group_member.group_id ' .
514 'WHERE group_member.profile_id = %d ' .
515 'ORDER BY group_member.created DESC ';
518 if (common_config('db','type') == 'pgsql') {
519 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
521 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
525 $groups = new User_group();
527 $cnt = $groups->query(sprintf($qry, $this->id));
532 function getSubscriptions($offset=0, $limit=null)
535 'SELECT profile.* ' .
536 'FROM profile JOIN subscription ' .
537 'ON profile.id = subscription.subscribed ' .
538 'WHERE subscription.subscriber = %d ' .
539 'AND subscription.subscribed != subscription.subscriber ' .
540 'ORDER BY subscription.created DESC ';
542 if (common_config('db','type') == 'pgsql') {
543 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
545 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
548 $profile = new Profile();
550 $profile->query(sprintf($qry, $this->id));
555 function getSubscribers($offset=0, $limit=null)
558 'SELECT profile.* ' .
559 'FROM profile JOIN subscription ' .
560 'ON profile.id = subscription.subscriber ' .
561 'WHERE subscription.subscribed = %d ' .
562 'AND subscription.subscribed != subscription.subscriber ' .
563 'ORDER BY subscription.created DESC ';
566 if (common_config('db','type') == 'pgsql') {
567 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
569 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
573 $profile = new Profile();
575 $cnt = $profile->query(sprintf($qry, $this->id));
580 function getTaggedSubscribers($tag, $offset=0, $limit=null)
583 'SELECT profile.* ' .
584 'FROM profile JOIN subscription ' .
585 'ON profile.id = subscription.subscriber ' .
586 'JOIN profile_tag ON (profile_tag.tagged = subscription.subscriber ' .
587 'AND profile_tag.tagger = subscription.subscribed) ' .
588 'WHERE subscription.subscribed = %d ' .
589 'AND profile_tag.tag = "%s" ' .
590 'AND subscription.subscribed != subscription.subscriber ' .
591 'ORDER BY subscription.created DESC ';
594 if (common_config('db','type') == 'pgsql') {
595 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
597 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
601 $profile = new Profile();
603 $cnt = $profile->query(sprintf($qry, $this->id, $tag));
608 function getTaggedSubscriptions($tag, $offset=0, $limit=null)
611 'SELECT profile.* ' .
612 'FROM profile JOIN subscription ' .
613 'ON profile.id = subscription.subscribed ' .
614 'JOIN profile_tag on (profile_tag.tagged = subscription.subscribed ' .
615 'AND profile_tag.tagger = subscription.subscriber) ' .
616 'WHERE subscription.subscriber = %d ' .
617 'AND profile_tag.tag = "%s" ' .
618 'AND subscription.subscribed != subscription.subscriber ' .
619 'ORDER BY subscription.created DESC ';
621 if (common_config('db','type') == 'pgsql') {
622 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
624 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
627 $profile = new Profile();
629 $profile->query(sprintf($qry, $this->id, $tag));
636 $oid = new User_openid();
638 $oid->user_id = $this->id;