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 $emailnotifyattn; // tinyint(1) default_1
44 public $emailmicroid; // tinyint(1) default_1
45 public $language; // varchar(50)
46 public $timezone; // varchar(50)
47 public $emailpost; // tinyint(1) default_1
48 public $jabber; // varchar(255) unique_key
49 public $jabbernotify; // tinyint(1)
50 public $jabberreplies; // tinyint(1)
51 public $jabbermicroid; // tinyint(1) default_1
52 public $updatefrompresence; // tinyint(1)
53 public $sms; // varchar(64) unique_key
54 public $carrier; // int(4)
55 public $smsnotify; // tinyint(1)
56 public $smsreplies; // tinyint(1)
57 public $smsemail; // varchar(255)
58 public $uri; // varchar(255) unique_key
59 public $autosubscribe; // tinyint(1)
60 public $urlshorteningservice; // varchar(50) default_ur1.ca
61 public $inboxed; // tinyint(1)
62 public $created; // datetime() not_null
63 public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
66 function staticGet($k,$v=NULL)
68 return Memcached_DataObject::staticGet('User',$k,$v);
71 /* the code above is auto generated do not remove the tag below */
76 return Profile::staticGet('id', $this->id);
79 function isSubscribed($other)
81 assert(!is_null($other));
82 # XXX: cache results of this query
83 $sub = Subscription::pkeyGet(array('subscriber' => $this->id,
84 'subscribed' => $other->id));
85 return (is_null($sub)) ? false : true;
88 # 'update' won't write key columns, so we have to do it ourselves.
90 function updateKeys(&$orig)
93 foreach (array('nickname', 'email', 'jabber', 'incomingemail', 'sms', 'carrier', 'smsemail', 'language', 'timezone') as $k) {
94 if (strcmp($this->$k, $orig->$k) != 0) {
95 $parts[] = $k . ' = ' . $this->_quote($this->$k);
98 if (count($parts) == 0) {
102 $toupdate = implode(', ', $parts);
104 $table = $this->tableName();
105 if(common_config('db','quote_identifiers')) {
106 $table = '"' . $table . '"';
108 $qry = 'UPDATE ' . $table . ' SET ' . $toupdate .
109 ' WHERE id = ' . $this->id;
111 $result = $this->query($qry);
118 function allowed_nickname($nickname)
120 # XXX: should already be validated for size, content, etc.
121 static $blacklist = array('rss', 'xrds', 'doc', 'main',
122 'settings', 'notice', 'user',
123 'search', 'avatar', 'tag', 'tags',
124 'api', 'message', 'group', 'groups');
125 $merged = array_merge($blacklist, common_config('nickname', 'blacklist'));
126 return !in_array($nickname, $merged);
129 function getCurrentNotice($dt=null)
131 $profile = $this->getProfile();
135 return $profile->getCurrentNotice($dt);
138 function getCarrier()
140 return Sms_carrier::staticGet('id', $this->carrier);
143 function subscribeTo($other)
145 $sub = new Subscription();
146 $sub->subscriber = $this->id;
147 $sub->subscribed = $other->id;
149 $sub->created = common_sql_now(); # current time
151 if (!$sub->insert()) {
158 function hasBlocked($other)
161 $block = Profile_block::get($this->id, $other->id);
163 if (is_null($block)) {
173 static function register($fields) {
175 # MAGICALLY put fields into current scope
179 $profile = new Profile();
181 $profile->query('BEGIN');
183 $profile->nickname = $nickname;
184 $profile->profileurl = common_profile_url($nickname);
187 $profile->fullname = $fullname;
190 $profile->homepage = $homepage;
193 $profile->bio = $bio;
196 $profile->location = $location;
199 $profile->created = common_sql_now();
201 $id = $profile->insert();
204 common_log_db_error($profile, 'INSERT', __FILE__);
211 $user->nickname = $nickname;
213 if ($password) { # may not have a password for OpenID users
214 $user->password = common_munge_password($password, $id);
217 # Users who respond to invite email have proven their ownership of that address
220 $invite = Invitation::staticGet($code);
221 if ($invite && $invite->address && $invite->address_type == 'email' && $invite->address == $email) {
222 $user->email = $invite->address;
226 $inboxes = common_config('inboxes', 'enabled');
228 if ($inboxes === true || $inboxes == 'transitional') {
232 $user->created = common_sql_now();
233 $user->uri = common_user_uri($user);
235 $result = $user->insert();
238 common_log_db_error($user, 'INSERT', __FILE__);
242 # Everyone is subscribed to themself
244 $subscription = new Subscription();
245 $subscription->subscriber = $user->id;
246 $subscription->subscribed = $user->id;
247 $subscription->created = $user->created;
249 $result = $subscription->insert();
252 common_log_db_error($subscription, 'INSERT', __FILE__);
256 if ($email && !$user->email) {
258 $confirm = new Confirm_address();
259 $confirm->code = common_confirmation_code(128);
260 $confirm->user_id = $user->id;
261 $confirm->address = $email;
262 $confirm->address_type = 'email';
264 $result = $confirm->insert();
266 common_log_db_error($confirm, 'INSERT', __FILE__);
271 if ($code && $user->email) {
272 $user->emailChanged();
275 $profile->query('COMMIT');
277 if ($email && !$user->email) {
278 mail_confirm_address($user, $confirm->code, $profile->nickname, $email);
284 # Things we do when the email changes
286 function emailChanged()
289 $invites = new Invitation();
290 $invites->address = $this->email;
291 $invites->address_type = 'email';
293 if ($invites->find()) {
294 while ($invites->fetch()) {
295 $other = User::staticGet($invites->user_id);
296 subs_subscribe_to($other, $this);
301 function hasFave($notice)
303 $cache = common_memcache();
305 # XXX: Kind of a hack.
307 # This is the stream of favorite notices, in rev chron
308 # order. This forces it into cache.
309 $faves = $this->favoriteNotices(0, NOTICE_CACHE_WINDOW);
311 while ($faves->fetch()) {
312 if ($faves->id < $notice->id) {
313 # If we passed it, it's not a fave
315 } else if ($faves->id == $notice->id) {
316 # If it matches a cached notice, then it's a fave
321 # If we're not past the end of the cache window,
322 # then the cache has all available faves, so this one
324 if ($cnt < NOTICE_CACHE_WINDOW) {
327 # Otherwise, cache doesn't have all faves;
328 # fall through to the default
330 $fave = Fave::pkeyGet(array('user_id' => $this->id,
331 'notice_id' => $notice->id));
332 return ((is_null($fave)) ? false : true);
334 function mutuallySubscribed($other)
336 return $this->isSubscribed($other) &&
337 $other->isSubscribed($this);
340 function mutuallySubscribedUsers()
343 # 3-way join; probably should get cached
344 $UT = common_config('db','type')=='pgsql'?'"user"':'user';
345 $qry = "SELECT $UT.* " .
346 "FROM subscription sub1 JOIN $UT ON sub1.subscribed = $UT.id " .
347 "JOIN subscription sub2 ON $UT.id = sub2.subscriber " .
348 'WHERE sub1.subscriber = %d and sub2.subscribed = %d ' .
349 "ORDER BY $UT.nickname";
351 $user->query(sprintf($qry, $this->id, $this->id));
356 function getReplies($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0, $since=null)
360 'FROM notice JOIN reply ON notice.id = reply.notice_id ' .
361 'WHERE reply.profile_id = %d ';
362 return Notice::getStream(sprintf($qry, $this->id),
363 'user:replies:'.$this->id,
364 $offset, $limit, $since_id, $before_id, null, $since);
367 function getNotices($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0, $since=null)
369 $profile = $this->getProfile();
373 return $profile->getNotices($offset, $limit, $since_id, $before_id);
377 function favoriteNotices($offset=0, $limit=NOTICES_PER_PAGE)
381 'FROM notice JOIN fave ON notice.id = fave.notice_id ' .
382 'WHERE fave.user_id = %d ';
383 return Notice::getStream(sprintf($qry, $this->id),
384 'user:faves:'.$this->id,
388 function noticesWithFriends($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0, $since=null)
390 $enabled = common_config('inboxes', 'enabled');
392 # Complicated code, depending on whether we support inboxes yet
393 # XXX: make this go away when inboxes become mandatory
395 if ($enabled === false ||
396 ($enabled == 'transitional' && $this->inboxed == 0)) {
399 'FROM notice JOIN subscription ON notice.profile_id = subscription.subscribed ' .
400 'WHERE subscription.subscriber = %d ';
402 } else if ($enabled === true ||
403 ($enabled == 'transitional' && $this->inboxed == 1)) {
407 'FROM notice JOIN notice_inbox ON notice.id = notice_inbox.notice_id ' .
408 'WHERE notice_inbox.user_id = %d ';
409 # NOTE: we override ORDER
412 return Notice::getStream(sprintf($qry, $this->id),
413 'user:notices_with_friends:' . $this->id,
414 $offset, $limit, $since_id, $before_id,
418 function blowFavesCache()
420 $cache = common_memcache();
422 # Faves don't happen chronologically, so we need to blow
424 $cache->delete(common_cache_key('user:faves:'.$this->id));
425 $cache->delete(common_cache_key('user:faves:'.$this->id).';last');
429 function getSelfTags()
431 return Profile_tag::getTags($this->id, $this->id);
434 function setSelfTags($newtags)
436 return Profile_tag::setTags($this->id, $this->id, $newtags);
439 function block($other)
442 # Add a new block record
444 $block = new Profile_block();
446 # Begin a transaction
448 $block->query('BEGIN');
450 $block->blocker = $this->id;
451 $block->blocked = $other->id;
453 $result = $block->insert();
456 common_log_db_error($block, 'INSERT', __FILE__);
460 # Cancel their subscription, if it exists
462 $sub = Subscription::pkeyGet(array('subscriber' => $other->id,
463 'subscribed' => $this->id));
466 $result = $sub->delete();
468 common_log_db_error($sub, 'DELETE', __FILE__);
473 $block->query('COMMIT');
478 function unblock($other)
481 # Get the block record
483 $block = Profile_block::get($this->id, $other->id);
489 $result = $block->delete();
492 common_log_db_error($block, 'DELETE', __FILE__);
499 function isMember($group)
501 $profile = $this->getProfile();
502 return $profile->isMember($group);
505 function isAdmin($group)
507 $profile = $this->getProfile();
508 return $profile->isAdmin($group);
511 function getGroups($offset=0, $limit=null)
514 'SELECT user_group.* ' .
515 'FROM user_group JOIN group_member '.
516 'ON user_group.id = group_member.group_id ' .
517 'WHERE group_member.profile_id = %d ' .
518 'ORDER BY group_member.created DESC ';
521 if (common_config('db','type') == 'pgsql') {
522 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
524 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
528 $groups = new User_group();
530 $cnt = $groups->query(sprintf($qry, $this->id));
535 function getSubscriptions($offset=0, $limit=null)
538 'SELECT profile.* ' .
539 'FROM profile JOIN subscription ' .
540 'ON profile.id = subscription.subscribed ' .
541 'WHERE subscription.subscriber = %d ' .
542 'AND subscription.subscribed != subscription.subscriber ' .
543 'ORDER BY subscription.created DESC ';
545 if (common_config('db','type') == 'pgsql') {
546 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
548 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
551 $profile = new Profile();
553 $profile->query(sprintf($qry, $this->id));
558 function getSubscribers($offset=0, $limit=null)
561 'SELECT profile.* ' .
562 'FROM profile JOIN subscription ' .
563 'ON profile.id = subscription.subscriber ' .
564 'WHERE subscription.subscribed = %d ' .
565 'AND subscription.subscribed != subscription.subscriber ' .
566 'ORDER BY subscription.created DESC ';
569 if (common_config('db','type') == 'pgsql') {
570 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
572 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
576 $profile = new Profile();
578 $cnt = $profile->query(sprintf($qry, $this->id));
583 function getTaggedSubscribers($tag, $offset=0, $limit=null)
586 'SELECT profile.* ' .
587 'FROM profile JOIN subscription ' .
588 'ON profile.id = subscription.subscriber ' .
589 'JOIN profile_tag ON (profile_tag.tagged = subscription.subscriber ' .
590 'AND profile_tag.tagger = subscription.subscribed) ' .
591 'WHERE subscription.subscribed = %d ' .
592 'AND profile_tag.tag = "%s" ' .
593 'AND subscription.subscribed != subscription.subscriber ' .
594 'ORDER BY subscription.created DESC ';
597 if (common_config('db','type') == 'pgsql') {
598 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
600 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
604 $profile = new Profile();
606 $cnt = $profile->query(sprintf($qry, $this->id, $tag));
611 function getTaggedSubscriptions($tag, $offset=0, $limit=null)
614 'SELECT profile.* ' .
615 'FROM profile JOIN subscription ' .
616 'ON profile.id = subscription.subscribed ' .
617 'JOIN profile_tag on (profile_tag.tagged = subscription.subscribed ' .
618 'AND profile_tag.tagger = subscription.subscriber) ' .
619 'WHERE subscription.subscriber = %d ' .
620 'AND profile_tag.tag = "%s" ' .
621 'AND subscription.subscribed != subscription.subscriber ' .
622 'ORDER BY subscription.created DESC ';
624 if (common_config('db','type') == 'pgsql') {
625 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
627 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
630 $profile = new Profile();
632 $profile->query(sprintf($qry, $this->id, $tag));
639 $oid = new User_openid();
641 $oid->user_id = $this->id;