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',
126 $merged = array_merge($blacklist, common_config('nickname', 'blacklist'));
127 return !in_array($nickname, $merged);
130 function getCurrentNotice($dt=null)
132 $profile = $this->getProfile();
136 return $profile->getCurrentNotice($dt);
139 function getCarrier()
141 return Sms_carrier::staticGet('id', $this->carrier);
144 function subscribeTo($other)
146 $sub = new Subscription();
147 $sub->subscriber = $this->id;
148 $sub->subscribed = $other->id;
150 $sub->created = common_sql_now(); # current time
152 if (!$sub->insert()) {
159 function hasBlocked($other)
162 $block = Profile_block::get($this->id, $other->id);
164 if (is_null($block)) {
174 static function register($fields) {
176 # MAGICALLY put fields into current scope
180 $profile = new Profile();
182 $profile->query('BEGIN');
184 $profile->nickname = $nickname;
185 $profile->profileurl = common_profile_url($nickname);
187 if (!empty($fullname)) {
188 $profile->fullname = $fullname;
190 if (!empty($homepage)) {
191 $profile->homepage = $homepage;
194 $profile->bio = $bio;
196 if (!empty($location)) {
197 $profile->location = $location;
200 $profile->created = common_sql_now();
202 $id = $profile->insert();
205 common_log_db_error($profile, 'INSERT', __FILE__);
212 $user->nickname = $nickname;
214 if (!empty($password)) { # may not have a password for OpenID users
215 $user->password = common_munge_password($password, $id);
218 # Users who respond to invite email have proven their ownership of that address
221 $invite = Invitation::staticGet($code);
222 if ($invite && $invite->address && $invite->address_type == 'email' && $invite->address == $email) {
223 $user->email = $invite->address;
227 $inboxes = common_config('inboxes', 'enabled');
229 if ($inboxes === true || $inboxes == 'transitional') {
233 $user->created = common_sql_now();
234 $user->uri = common_user_uri($user);
236 $result = $user->insert();
239 common_log_db_error($user, 'INSERT', __FILE__);
243 # Everyone is subscribed to themself
245 $subscription = new Subscription();
246 $subscription->subscriber = $user->id;
247 $subscription->subscribed = $user->id;
248 $subscription->created = $user->created;
250 $result = $subscription->insert();
253 common_log_db_error($subscription, 'INSERT', __FILE__);
257 if (!empty($email) && !$user->email) {
259 $confirm = new Confirm_address();
260 $confirm->code = common_confirmation_code(128);
261 $confirm->user_id = $user->id;
262 $confirm->address = $email;
263 $confirm->address_type = 'email';
265 $result = $confirm->insert();
267 common_log_db_error($confirm, 'INSERT', __FILE__);
272 if (!empty($code) && $user->email) {
273 $user->emailChanged();
276 $profile->query('COMMIT');
278 if ($email && !$user->email) {
279 mail_confirm_address($user, $confirm->code, $profile->nickname, $email);
285 # Things we do when the email changes
287 function emailChanged()
290 $invites = new Invitation();
291 $invites->address = $this->email;
292 $invites->address_type = 'email';
294 if ($invites->find()) {
295 while ($invites->fetch()) {
296 $other = User::staticGet($invites->user_id);
297 subs_subscribe_to($other, $this);
302 function hasFave($notice)
304 $cache = common_memcache();
306 # XXX: Kind of a hack.
308 # This is the stream of favorite notices, in rev chron
309 # order. This forces it into cache.
310 $faves = $this->favoriteNotices(0, NOTICE_CACHE_WINDOW);
312 while ($faves->fetch()) {
313 if ($faves->id < $notice->id) {
314 # If we passed it, it's not a fave
316 } else if ($faves->id == $notice->id) {
317 # If it matches a cached notice, then it's a fave
322 # If we're not past the end of the cache window,
323 # then the cache has all available faves, so this one
325 if ($cnt < NOTICE_CACHE_WINDOW) {
328 # Otherwise, cache doesn't have all faves;
329 # fall through to the default
331 $fave = Fave::pkeyGet(array('user_id' => $this->id,
332 'notice_id' => $notice->id));
333 return ((is_null($fave)) ? false : true);
335 function mutuallySubscribed($other)
337 return $this->isSubscribed($other) &&
338 $other->isSubscribed($this);
341 function mutuallySubscribedUsers()
344 # 3-way join; probably should get cached
345 $UT = common_config('db','type')=='pgsql'?'"user"':'user';
346 $qry = "SELECT $UT.* " .
347 "FROM subscription sub1 JOIN $UT ON sub1.subscribed = $UT.id " .
348 "JOIN subscription sub2 ON $UT.id = sub2.subscriber " .
349 'WHERE sub1.subscriber = %d and sub2.subscribed = %d ' .
350 "ORDER BY $UT.nickname";
352 $user->query(sprintf($qry, $this->id, $this->id));
357 function getReplies($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0, $since=null)
361 'FROM notice JOIN reply ON notice.id = reply.notice_id ' .
362 'WHERE reply.profile_id = %d ';
363 return Notice::getStream(sprintf($qry, $this->id),
364 'user:replies:'.$this->id,
365 $offset, $limit, $since_id, $before_id, null, $since);
368 function getNotices($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0, $since=null)
370 $profile = $this->getProfile();
374 return $profile->getNotices($offset, $limit, $since_id, $before_id);
378 function favoriteNotices($offset=0, $limit=NOTICES_PER_PAGE)
382 'FROM notice JOIN fave ON notice.id = fave.notice_id ' .
383 'WHERE fave.user_id = %d ';
384 return Notice::getStream(sprintf($qry, $this->id),
385 'user:faves:'.$this->id,
389 function noticesWithFriends($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0, $since=null)
391 $enabled = common_config('inboxes', 'enabled');
393 # Complicated code, depending on whether we support inboxes yet
394 # XXX: make this go away when inboxes become mandatory
396 if ($enabled === false ||
397 ($enabled == 'transitional' && $this->inboxed == 0)) {
400 'FROM notice JOIN subscription ON notice.profile_id = subscription.subscribed ' .
401 'WHERE subscription.subscriber = %d ';
403 } else if ($enabled === true ||
404 ($enabled == 'transitional' && $this->inboxed == 1)) {
408 'FROM notice JOIN notice_inbox ON notice.id = notice_inbox.notice_id ' .
409 'WHERE notice_inbox.user_id = %d ';
410 # NOTE: we override ORDER
413 return Notice::getStream(sprintf($qry, $this->id),
414 'user:notices_with_friends:' . $this->id,
415 $offset, $limit, $since_id, $before_id,
419 function blowFavesCache()
421 $cache = common_memcache();
423 # Faves don't happen chronologically, so we need to blow
425 $cache->delete(common_cache_key('user:faves:'.$this->id));
426 $cache->delete(common_cache_key('user:faves:'.$this->id).';last');
430 function getSelfTags()
432 return Profile_tag::getTags($this->id, $this->id);
435 function setSelfTags($newtags)
437 return Profile_tag::setTags($this->id, $this->id, $newtags);
440 function block($other)
443 # Add a new block record
445 $block = new Profile_block();
447 # Begin a transaction
449 $block->query('BEGIN');
451 $block->blocker = $this->id;
452 $block->blocked = $other->id;
454 $result = $block->insert();
457 common_log_db_error($block, 'INSERT', __FILE__);
461 # Cancel their subscription, if it exists
463 $sub = Subscription::pkeyGet(array('subscriber' => $other->id,
464 'subscribed' => $this->id));
467 $result = $sub->delete();
469 common_log_db_error($sub, 'DELETE', __FILE__);
474 $block->query('COMMIT');
479 function unblock($other)
482 # Get the block record
484 $block = Profile_block::get($this->id, $other->id);
490 $result = $block->delete();
493 common_log_db_error($block, 'DELETE', __FILE__);
500 function isMember($group)
502 $profile = $this->getProfile();
503 return $profile->isMember($group);
506 function isAdmin($group)
508 $profile = $this->getProfile();
509 return $profile->isAdmin($group);
512 function getGroups($offset=0, $limit=null)
515 'SELECT user_group.* ' .
516 'FROM user_group JOIN group_member '.
517 'ON user_group.id = group_member.group_id ' .
518 'WHERE group_member.profile_id = %d ' .
519 'ORDER BY group_member.created DESC ';
522 if (common_config('db','type') == 'pgsql') {
523 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
525 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
529 $groups = new User_group();
531 $cnt = $groups->query(sprintf($qry, $this->id));
536 function getSubscriptions($offset=0, $limit=null)
539 'SELECT profile.* ' .
540 'FROM profile JOIN subscription ' .
541 'ON profile.id = subscription.subscribed ' .
542 'WHERE subscription.subscriber = %d ' .
543 'AND subscription.subscribed != subscription.subscriber ' .
544 'ORDER BY subscription.created DESC ';
546 if (common_config('db','type') == 'pgsql') {
547 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
549 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
552 $profile = new Profile();
554 $profile->query(sprintf($qry, $this->id));
559 function getSubscribers($offset=0, $limit=null)
562 'SELECT profile.* ' .
563 'FROM profile JOIN subscription ' .
564 'ON profile.id = subscription.subscriber ' .
565 'WHERE subscription.subscribed = %d ' .
566 'AND subscription.subscribed != subscription.subscriber ' .
567 'ORDER BY subscription.created DESC ';
570 if (common_config('db','type') == 'pgsql') {
571 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
573 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
577 $profile = new Profile();
579 $cnt = $profile->query(sprintf($qry, $this->id));
584 function getTaggedSubscribers($tag, $offset=0, $limit=null)
587 'SELECT profile.* ' .
588 'FROM profile JOIN subscription ' .
589 'ON profile.id = subscription.subscriber ' .
590 'JOIN profile_tag ON (profile_tag.tagged = subscription.subscriber ' .
591 'AND profile_tag.tagger = subscription.subscribed) ' .
592 'WHERE subscription.subscribed = %d ' .
593 "AND profile_tag.tag = '%s' " .
594 'AND subscription.subscribed != subscription.subscriber ' .
595 'ORDER BY subscription.created DESC ';
598 if (common_config('db','type') == 'pgsql') {
599 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
601 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
605 $profile = new Profile();
607 $cnt = $profile->query(sprintf($qry, $this->id, $tag));
612 function getTaggedSubscriptions($tag, $offset=0, $limit=null)
615 'SELECT profile.* ' .
616 'FROM profile JOIN subscription ' .
617 'ON profile.id = subscription.subscribed ' .
618 'JOIN profile_tag on (profile_tag.tagged = subscription.subscribed ' .
619 'AND profile_tag.tagger = subscription.subscriber) ' .
620 'WHERE subscription.subscriber = %d ' .
621 "AND profile_tag.tag = '%s' " .
622 'AND subscription.subscribed != subscription.subscriber ' .
623 'ORDER BY subscription.created DESC ';
625 if (common_config('db','type') == 'pgsql') {
626 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
628 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
631 $profile = new Profile();
633 $profile->query(sprintf($qry, $this->id, $tag));
640 $oid = new User_openid();
642 $oid->user_id = $this->id;