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) { return Memcached_DataObject::staticGet('User',$k,$v); }
67 /* the code above is auto generated do not remove the tag below */
70 function getProfile() {
71 return Profile::staticGet('id', $this->id);
74 function isSubscribed($other) {
75 assert(!is_null($other));
76 # XXX: cache results of this query
77 $sub = Subscription::pkeyGet(array('subscriber' => $this->id,
78 'subscribed' => $other->id));
79 return (is_null($sub)) ? false : true;
82 # 'update' won't write key columns, so we have to do it ourselves.
84 function updateKeys(&$orig) {
86 foreach (array('nickname', 'email', 'jabber', 'incomingemail', 'sms', 'carrier', 'smsemail', 'language', 'timezone') as $k) {
87 if (strcmp($this->$k, $orig->$k) != 0) {
88 $parts[] = $k . ' = ' . $this->_quote($this->$k);
91 if (count($parts) == 0) {
95 $toupdate = implode(', ', $parts);
97 $table = $this->tableName();
98 if(common_config('db','quote_identifiers')) {
99 $table = '"' . $table . '"';
101 $qry = 'UPDATE ' . $table . ' SET ' . $toupdate .
102 ' WHERE id = ' . $this->id;
104 $result = $this->query($qry);
111 function allowed_nickname($nickname) {
112 # XXX: should already be validated for size, content, etc.
113 static $blacklist = array('rss', 'xrds', 'doc', 'main',
114 'settings', 'notice', 'user',
115 'search', 'avatar', 'tag', 'tags',
117 $merged = array_merge($blacklist, common_config('nickname', 'blacklist'));
118 return !in_array($nickname, $merged);
121 function getCurrentNotice($dt=NULL) {
122 $profile = $this->getProfile();
126 return $profile->getCurrentNotice($dt);
129 function getCarrier() {
130 return Sms_carrier::staticGet('id', $this->carrier);
133 function subscribeTo($other) {
134 $sub = new Subscription();
135 $sub->subscriber = $this->id;
136 $sub->subscribed = $other->id;
138 $sub->created = common_sql_now(); # current time
140 if (!$sub->insert()) {
147 function hasBlocked($other) {
149 $block = Profile_block::get($this->id, $other->id);
151 if (is_null($block)) {
161 static function register($fields) {
163 # MAGICALLY put fields into current scope
167 $profile = new Profile();
169 $profile->query('BEGIN');
171 $profile->nickname = $nickname;
172 $profile->profileurl = common_profile_url($nickname);
175 $profile->fullname = $fullname;
178 $profile->homepage = $homepage;
181 $profile->bio = $bio;
184 $profile->location = $location;
187 $profile->created = common_sql_now();
189 $id = $profile->insert();
192 common_log_db_error($profile, 'INSERT', __FILE__);
199 $user->nickname = $nickname;
201 if ($password) { # may not have a password for OpenID users
202 $user->password = common_munge_password($password, $id);
205 # Users who respond to invite email have proven their ownership of that address
208 $invite = Invitation::staticGet($code);
209 if ($invite && $invite->address && $invite->address_type == 'email' && $invite->address == $email) {
210 $user->email = $invite->address;
214 $inboxes = common_config('inboxes', 'enabled');
216 if ($inboxes === true || $inboxes == 'transitional') {
220 $user->created = common_sql_now();
221 $user->uri = common_user_uri($user);
223 $result = $user->insert();
226 common_log_db_error($user, 'INSERT', __FILE__);
230 # Everyone is subscribed to themself
232 $subscription = new Subscription();
233 $subscription->subscriber = $user->id;
234 $subscription->subscribed = $user->id;
235 $subscription->created = $user->created;
237 $result = $subscription->insert();
240 common_log_db_error($subscription, 'INSERT', __FILE__);
244 if ($email && !$user->email) {
246 $confirm = new Confirm_address();
247 $confirm->code = common_confirmation_code(128);
248 $confirm->user_id = $user->id;
249 $confirm->address = $email;
250 $confirm->address_type = 'email';
252 $result = $confirm->insert();
254 common_log_db_error($confirm, 'INSERT', __FILE__);
259 if ($code && $user->email) {
260 $user->emailChanged();
263 $profile->query('COMMIT');
265 if ($email && !$user->email) {
266 mail_confirm_address($user, $confirm->code, $profile->nickname, $email);
272 # Things we do when the email changes
274 function emailChanged() {
276 $invites = new Invitation();
277 $invites->address = $this->email;
278 $invites->address_type = 'email';
280 if ($invites->find()) {
281 while ($invites->fetch()) {
282 $other = User::staticGet($invites->user_id);
283 subs_subscribe_to($other, $this);
288 function hasFave($notice) {
289 $cache = common_memcache();
291 # XXX: Kind of a hack.
293 # This is the stream of favorite notices, in rev chron
294 # order. This forces it into cache.
295 $faves = $this->favoriteNotices(0, NOTICE_CACHE_WINDOW);
297 while ($faves->fetch()) {
298 if ($faves->id < $notice->id) {
299 # If we passed it, it's not a fave
301 } else if ($faves->id == $notice->id) {
302 # If it matches a cached notice, then it's a fave
307 # If we're not past the end of the cache window,
308 # then the cache has all available faves, so this one
310 if ($cnt < NOTICE_CACHE_WINDOW) {
313 # Otherwise, cache doesn't have all faves;
314 # fall through to the default
316 $fave = Fave::pkeyGet(array('user_id' => $this->id,
317 'notice_id' => $notice->id));
318 return ((is_null($fave)) ? false : true);
320 function mutuallySubscribed($other) {
321 return $this->isSubscribed($other) &&
322 $other->isSubscribed($this);
325 function mutuallySubscribedUsers() {
327 # 3-way join; probably should get cached
328 $qry = 'SELECT user.* ' .
329 'FROM subscription sub1 JOIN user ON sub1.subscribed = user.id ' .
330 'JOIN subscription sub2 ON user.id = sub2.subscriber ' .
331 'WHERE sub1.subscriber = %d and sub2.subscribed = %d ' .
332 'ORDER BY user.nickname';
334 $user->query(sprintf($qry, $this->id, $this->id));
339 function getReplies($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0, $since=NULL) {
342 'FROM notice JOIN reply ON notice.id = reply.notice_id ' .
343 'WHERE reply.profile_id = %d ';
344 return Notice::getStream(sprintf($qry, $this->id),
345 'user:replies:'.$this->id,
346 $offset, $limit, $since_id, $before_id, NULL, $since);
349 function getNotices($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0, $since=NULL) {
350 $profile = $this->getProfile();
354 return $profile->getNotices($offset, $limit, $since_id, $before_id);
358 function favoriteNotices($offset=0, $limit=NOTICES_PER_PAGE) {
361 'FROM notice JOIN fave ON notice.id = fave.notice_id ' .
362 'WHERE fave.user_id = %d ';
363 return Notice::getStream(sprintf($qry, $this->id),
364 'user:faves:'.$this->id,
368 function noticesWithFriends($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0, $since=NULL) {
369 $enabled = common_config('inboxes', 'enabled');
371 # Complicated code, depending on whether we support inboxes yet
372 # XXX: make this go away when inboxes become mandatory
374 if ($enabled === false ||
375 ($enabled == 'transitional' && $this->inboxed == 0)) {
378 'FROM notice JOIN subscription ON notice.profile_id = subscription.subscribed ' .
379 'WHERE subscription.subscriber = %d ';
381 } else if ($enabled === true ||
382 ($enabled == 'transitional' && $this->inboxed == 1)) {
386 'FROM notice JOIN notice_inbox ON notice.id = notice_inbox.notice_id ' .
387 'WHERE notice_inbox.user_id = %d ';
390 return Notice::getStream(sprintf($qry, $this->id),
391 'user:notices_with_friends:' . $this->id,
392 $offset, $limit, $since_id, $before_id,
396 function blowFavesCache() {
397 $cache = common_memcache();
399 # Faves don't happen chronologically, so we need to blow
401 $cache->delete(common_cache_key('user:faves:'.$this->id));
402 $cache->delete(common_cache_key('user:faves:'.$this->id).';last');
406 function getSelfTags() {
407 return Profile_tag::getTags($this->id, $this->id);
410 function setSelfTags($newtags) {
411 return Profile_tag::setTags($this->id, $this->id, $newtags);
414 function block($other) {
416 # Add a new block record
418 $block = new Profile_block();
420 # Begin a transaction
422 $block->query('BEGIN');
424 $block->blocker = $this->id;
425 $block->blocked = $other->id;
427 $result = $block->insert();
430 common_log_db_error($block, 'INSERT', __FILE__);
434 # Cancel their subscription, if it exists
436 $sub = Subscription::pkeyGet(array('subscriber' => $other->id,
437 'subscribed' => $this->id));
440 $result = $sub->delete();
442 common_log_db_error($sub, 'DELETE', __FILE__);
447 $block->query('COMMIT');
452 function unblock($other) {
454 # Get the block record
456 $block = Profile_block::get($this->id, $other->id);
462 $result = $block->delete();
465 common_log_db_error($block, 'DELETE', __FILE__);