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';
27 require_once(INSTALLDIR.'/lib/noticewrapper.php');
29 class User extends Memcached_DataObject
32 /* the code below is auto generated do not remove the above tag */
34 public $__table = 'user'; // table name
35 public $id; // int(4) primary_key not_null
36 public $nickname; // varchar(64) unique_key
37 public $password; // varchar(255)
38 public $email; // varchar(255) unique_key
39 public $incomingemail; // varchar(255) unique_key
40 public $emailnotifysub; // tinyint(1) default_1
41 public $emailnotifyfav; // 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 $created; // datetime() not_null
60 public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
63 function staticGet($k,$v=NULL) { return Memcached_DataObject::staticGet('User',$k,$v); }
65 /* the code above is auto generated do not remove the tag below */
68 function getProfile() {
69 $profile = DB_DataObject::factory('profile');
70 $profile->id = $this->id;
71 if ($profile->find()) {
78 function isSubscribed($other) {
79 assert(!is_null($other));
80 $sub = DB_DataObject::factory('subscription');
81 $sub->subscriber = $this->id;
82 $sub->subscribed = $other->id;
86 # 'update' won't write key columns, so we have to do it ourselves.
88 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) {
116 # XXX: should already be validated for size, content, etc.
117 static $blacklist = array('rss', 'xrds', 'doc', 'main',
118 'settings', 'notice', 'user',
119 'search', 'avatar', 'tag', 'tags',
121 $merged = array_merge($blacklist, common_config('nickname', 'blacklist'));
122 return !in_array($nickname, $merged);
125 function getCurrentNotice($dt=NULL) {
126 $profile = $this->getProfile();
130 return $profile->getCurrentNotice($dt);
133 function getCarrier() {
134 return Sms_carrier::staticGet($this->carrier);
137 function subscribeTo($other) {
138 $sub = new Subscription();
139 $sub->subscriber = $this->id;
140 $sub->subscribed = $other->id;
142 $sub->created = common_sql_now(); # current time
144 if (!$sub->insert()) {
151 function noticesWithFriendsWindow() {
154 $notice = new Notice();
156 $notice->query('SELECT notice.* ' .
157 'FROM notice JOIN subscription on notice.profile_id = subscription.subscribed ' .
158 'WHERE subscription.subscriber = ' . $this->id . ' ' .
159 'ORDER BY created DESC, notice.id DESC ' .
160 'LIMIT 0, ' . WITHFRIENDS_CACHE_WINDOW);
164 static function register($fields) {
166 # MAGICALLY put fields into current scope
170 $profile = new Profile();
172 $profile->query('BEGIN');
174 $profile->nickname = $nickname;
175 $profile->profileurl = common_profile_url($nickname);
178 $profile->fullname = $fullname;
181 $profile->homepage = $homepage;
184 $profile->bio = $bio;
187 $profile->location = $location;
190 $profile->created = common_sql_now();
192 $id = $profile->insert();
195 common_log_db_error($profile, 'INSERT', __FILE__);
202 $user->nickname = $nickname;
204 if ($password) { # may not have a password for OpenID users
205 $user->password = common_munge_password($password, $id);
208 # Users who respond to invite email have proven their ownership of that address
211 $invite = Invitation::staticGet($code);
212 if ($invite && $invite->address && $invite->address_type == 'email' && $invite->address == $email) {
213 $user->email = $invite->address;
217 $user->created = common_sql_now();
218 $user->uri = common_user_uri($user);
220 $result = $user->insert();
223 common_log_db_error($user, 'INSERT', __FILE__);
227 # Everyone is subscribed to themself
229 $subscription = new Subscription();
230 $subscription->subscriber = $user->id;
231 $subscription->subscribed = $user->id;
232 $subscription->created = $user->created;
234 $result = $subscription->insert();
237 common_log_db_error($subscription, 'INSERT', __FILE__);
241 if ($email && !$user->email) {
243 $confirm = new Confirm_address();
244 $confirm->code = common_confirmation_code(128);
245 $confirm->user_id = $user->id;
246 $confirm->address = $email;
247 $confirm->address_type = 'email';
249 $result = $confirm->insert();
251 common_log_db_error($confirm, 'INSERT', __FILE__);
256 if ($code && $user->email) {
257 $user->emailChanged();
260 $profile->query('COMMIT');
262 if ($email && !$user->email) {
263 mail_confirm_address($confirm->code,
271 # Things we do when the email changes
273 function emailChanged() {
275 $invites = new Invitation();
276 $invites->address = $this->email;
277 $invites->address_type = 'email';
279 if ($invites->find()) {
280 while ($invites->fetch()) {
281 $other = User::staticGet($invites->user_id);
282 subs_subscribe_to($other, $this);
287 function hasFave($notice) {
289 $fave->user_id = $this->id;
290 $fave->notice_id = $notice->id;
301 function mutuallySubscribed($other) {
302 return $this->isSubscribed($other) &&
303 $other->isSubscribed($this);
306 function mutuallySubscribedUsers() {
308 # 3-way join; probably should get cached
310 $qry = 'SELECT user.* ' .
311 'FROM subscription sub1 JOIN user ON sub1.subscribed = user.id ' .
312 'JOIN subscription sub2 ON user.id = sub2.subscriber ' .
313 'WHERE sub1.subscriber = %d and sub2.subscribed = %d ' .
314 'ORDER BY user.nickname';
317 $user->query(sprintf($qry, $this->id, $this->id));
322 function getReplies($offset=0, $limit=NOTICES_PER_PAGE) {
325 'FROM notice JOIN reply ON notice.id = reply.notice_id ' .
326 'WHERE reply.profile_id = %d ';
328 return Notice::getStream(sprintf($qry, $this->id),
329 'user:replies:'.$this->id,
333 function getNotices($offset=0, $limit=NOTICES_PER_PAGE) {
337 'WHERE profile_id = %d ';
339 return Notice::getStream(sprintf($qry, $this->id),
340 'user:notices:'.$this->id,
344 function favoriteNotices($offset=0, $limit=NOTICES_PER_PAGE) {
347 'FROM notice JOIN fave ON notice.id = fave.notice_id ' .
348 'WHERE fave.profile_id = %d ';
350 return Notice::getStream(sprintf($qry, $this->id),
351 'user:faves:'.$this->id,
355 function noticesWithFriends($offset=0, $limit=NOTICES_PER_PAGE) {
358 'FROM notice JOIN subscription ON notice.profile_id = subscription.subscribed ' .
359 'WHERE subscription.subscriber = %d';
361 return Notice::getStream(sprintf($qry, $this->id),
362 'user:notices_with_friends:' . $this->id,
366 function blowFavesCache() {
367 $cache = common_memcache();
369 $cache->delete(common_cache_key('user:faves:'.$this->id));