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 $emailnotifymsg; // tinyint(1) default_1
42 public $emailmicroid; // tinyint(1) default_1
43 public $language; // varchar(50)
44 public $timezone; // varchar(50)
45 public $emailpost; // tinyint(1) default_1
46 public $jabber; // varchar(255) unique_key
47 public $jabbernotify; // tinyint(1)
48 public $jabberreplies; // tinyint(1)
49 public $jabbermicroid; // tinyint(1) default_1
50 public $updatefrompresence; // tinyint(1)
51 public $sms; // varchar(64) unique_key
52 public $carrier; // int(4)
53 public $smsnotify; // tinyint(1)
54 public $smsreplies; // tinyint(1)
55 public $smsemail; // varchar(255)
56 public $uri; // varchar(255) unique_key
57 public $autosubscribe; // tinyint(1)
58 public $created; // datetime() not_null
59 public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
62 function staticGet($k,$v=NULL) { return Memcached_DataObject::staticGet('User',$k,$v); }
64 /* the code above is auto generated do not remove the tag below */
67 function getProfile() {
68 return Profile::staticGet('id', $this->id);
71 function isSubscribed($other) {
72 assert(!is_null($other));
73 # XXX: cache results of this query
74 $sub = Subscription::pkeyGet(array('subscriber' => $this->id,
75 'subscribed' => $other->id));
76 return (is_null($sub)) ? false : true;
79 # 'update' won't write key columns, so we have to do it ourselves.
81 function updateKeys(&$orig) {
83 foreach (array('nickname', 'email', 'jabber', 'incomingemail', 'sms', 'carrier', 'smsemail', 'language', 'timezone') as $k) {
84 if (strcmp($this->$k, $orig->$k) != 0) {
85 $parts[] = $k . ' = ' . $this->_quote($this->$k);
88 if (count($parts) == 0) {
92 $toupdate = implode(', ', $parts);
94 $table = $this->tableName();
95 if(common_config('db','quote_identifiers')) {
96 $table = '"' . $table . '"';
98 $qry = 'UPDATE ' . $table . ' SET ' . $toupdate .
99 ' WHERE id = ' . $this->id;
101 $result = $this->query($qry);
108 function allowed_nickname($nickname) {
109 # XXX: should already be validated for size, content, etc.
110 static $blacklist = array('rss', 'xrds', 'doc', 'main',
111 'settings', 'notice', 'user',
112 'search', 'avatar', 'tag', 'tags',
114 $merged = array_merge($blacklist, common_config('nickname', 'blacklist'));
115 return !in_array($nickname, $merged);
118 function getCurrentNotice($dt=NULL) {
119 $profile = $this->getProfile();
123 return $profile->getCurrentNotice($dt);
126 function getCarrier() {
127 return Sms_carrier::staticGet('id', $this->carrier);
130 function subscribeTo($other) {
131 $sub = new Subscription();
132 $sub->subscriber = $this->id;
133 $sub->subscribed = $other->id;
135 $sub->created = common_sql_now(); # current time
137 if (!$sub->insert()) {
144 function noticesWithFriendsWindow() {
146 $notice = new Notice();
148 $notice->query('SELECT notice.* ' .
149 'FROM notice JOIN subscription on notice.profile_id = subscription.subscribed ' .
150 'WHERE subscription.subscriber = ' . $this->id . ' ' .
151 'ORDER BY created DESC, notice.id DESC ' .
152 'LIMIT 0, ' . WITHFRIENDS_CACHE_WINDOW);
156 static function register($fields) {
158 # MAGICALLY put fields into current scope
162 $profile = new Profile();
164 $profile->query('BEGIN');
166 $profile->nickname = $nickname;
167 $profile->profileurl = common_profile_url($nickname);
170 $profile->fullname = $fullname;
173 $profile->homepage = $homepage;
176 $profile->bio = $bio;
179 $profile->location = $location;
182 $profile->created = common_sql_now();
184 $id = $profile->insert();
187 common_log_db_error($profile, 'INSERT', __FILE__);
194 $user->nickname = $nickname;
196 if ($password) { # may not have a password for OpenID users
197 $user->password = common_munge_password($password, $id);
200 # Users who respond to invite email have proven their ownership of that address
203 $invite = Invitation::staticGet($code);
204 if ($invite && $invite->address && $invite->address_type == 'email' && $invite->address == $email) {
205 $user->email = $invite->address;
209 $user->created = common_sql_now();
210 $user->uri = common_user_uri($user);
212 $result = $user->insert();
215 common_log_db_error($user, 'INSERT', __FILE__);
219 # Everyone is subscribed to themself
221 $subscription = new Subscription();
222 $subscription->subscriber = $user->id;
223 $subscription->subscribed = $user->id;
224 $subscription->created = $user->created;
226 $result = $subscription->insert();
229 common_log_db_error($subscription, 'INSERT', __FILE__);
233 if ($email && !$user->email) {
235 $confirm = new Confirm_address();
236 $confirm->code = common_confirmation_code(128);
237 $confirm->user_id = $user->id;
238 $confirm->address = $email;
239 $confirm->address_type = 'email';
241 $result = $confirm->insert();
243 common_log_db_error($confirm, 'INSERT', __FILE__);
248 if ($code && $user->email) {
249 $user->emailChanged();
252 $profile->query('COMMIT');
254 if ($email && !$user->email) {
255 mail_confirm_address($confirm->code,
263 # Things we do when the email changes
265 function emailChanged() {
267 $invites = new Invitation();
268 $invites->address = $this->email;
269 $invites->address_type = 'email';
271 if ($invites->find()) {
272 while ($invites->fetch()) {
273 $other = User::staticGet($invites->user_id);
274 subs_subscribe_to($other, $this);
279 function hasFave($notice) {
280 $fave = Fave::pkeyGet(array('user_id' => $this->id,
281 'notice_id' => $notice->id));
282 return ((is_null($fave)) ? false : true);
285 function mutuallySubscribed($other) {
286 return $this->isSubscribed($other) &&
287 $other->isSubscribed($this);
290 function mutuallySubscribedUsers() {
292 # 3-way join; probably should get cached
294 $qry = 'SELECT user.* ' .
295 'FROM subscription sub1 JOIN user ON sub1.subscribed = user.id ' .
296 'JOIN subscription sub2 ON user.id = sub2.subscriber ' .
297 'WHERE sub1.subscriber = %d and sub2.subscribed = %d ' .
298 'ORDER BY user.nickname';
301 $user->query(sprintf($qry, $this->id, $this->id));
306 function getReplies($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0) {
309 'FROM notice JOIN reply ON notice.id = reply.notice_id ' .
310 'WHERE reply.profile_id = %d ';
313 $qry .= ' AND notice.id > ' . $since_id . ' ';
317 // NOTE: before_id is an extension to Twitter API
318 if ($before_id > 0) {
319 $qry .= ' AND notice.id < ' . $before_id . ' ';
323 return Notice::getStream(sprintf($qry, $this->id),
324 'user:replies:'.$this->id,
328 function getNotices($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0) {
332 'WHERE profile_id = %d ';
335 $qry .= ' AND notice.id > ' . $since_id . ' ';
339 // NOTE: before_id is an extension to Twitter API
340 if ($before_id > 0) {
341 $qry .= ' AND notice.id < ' . $before_id . ' ';
345 return Notice::getStream(sprintf($qry, $this->id),
346 'user:notices:'.$this->id,
350 function favoriteNotices($offset=0, $limit=NOTICES_PER_PAGE) {
353 'FROM notice JOIN fave ON notice.id = fave.notice_id ' .
354 'WHERE fave.user_id = %d ';
356 return Notice::getStream(sprintf($qry, $this->id),
357 'user:faves:'.$this->id,
361 function noticesWithFriends($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0) {
364 'FROM notice JOIN subscription ON notice.profile_id = subscription.subscribed ' .
365 'WHERE subscription.subscriber = %d ';
368 $qry .= ' AND notice.id > ' . $since_id . ' ';
372 // NOTE: before_id is an extension to Twitter API
373 if ($before_id > 0) {
374 $qry .= ' AND notice.id < ' . $before_id . ' ';
378 return Notice::getStream(sprintf($qry, $this->id),
379 'user:notices_with_friends:' . $this->id,
383 function blowFavesCache() {
384 $cache = common_memcache();
386 $cache->delete(common_cache_key('user:faves:'.$this->id));