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); }
22 /* We keep the first three 20-notice pages, plus one for pagination check,
23 * in the memcached cache. */
25 define('WITHFRIENDS_CACHE_WINDOW', 61);
28 * Table Definition for user
30 require_once 'DB/DataObject.php';
31 require_once 'Validate.php';
32 require_once(INSTALLDIR.'/lib/noticewrapper.php');
34 class User extends DB_DataObject
37 /* the code below is auto generated do not remove the above tag */
39 public $__table = 'user'; // table name
40 public $id; // int(4) primary_key not_null
41 public $nickname; // varchar(64) unique_key
42 public $password; // varchar(255)
43 public $email; // varchar(255) unique_key
44 public $incomingemail; // varchar(255) unique_key
45 public $emailnotifysub; // tinyint(1) default_1
46 public $emailnotifyfav; // tinyint(1) default_1
47 public $emailnotifymsg; // tinyint(1) default_1
48 public $emailmicroid; // tinyint(1) default_1
49 public $language; // varchar(50)
50 public $timezone; // varchar(50)
51 public $emailpost; // tinyint(1) default_1
52 public $jabber; // varchar(255) unique_key
53 public $jabbernotify; // tinyint(1)
54 public $jabberreplies; // tinyint(1)
55 public $jabbermicroid; // tinyint(1) default_1
56 public $updatefrompresence; // tinyint(1)
57 public $sms; // varchar(64) unique_key
58 public $carrier; // int(4)
59 public $smsnotify; // tinyint(1)
60 public $smsreplies; // tinyint(1)
61 public $smsemail; // varchar(255)
62 public $uri; // varchar(255) unique_key
63 public $autosubscribe; // tinyint(1)
64 public $created; // datetime() not_null
65 public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
68 function staticGet($k,$v=NULL) { return DB_DataObject::staticGet('User',$k,$v); }
70 /* the code above is auto generated do not remove the tag below */
73 function getProfile() {
74 $profile = DB_DataObject::factory('profile');
75 $profile->id = $this->id;
76 if ($profile->find()) {
83 function isSubscribed($other) {
84 assert(!is_null($other));
85 $sub = DB_DataObject::factory('subscription');
86 $sub->subscriber = $this->id;
87 $sub->subscribed = $other->id;
91 # 'update' won't write key columns, so we have to do it ourselves.
93 function updateKeys(&$orig) {
95 foreach (array('nickname', 'email', 'jabber', 'incomingemail', 'sms', 'carrier', 'smsemail', 'language', 'timezone') as $k) {
96 if (strcmp($this->$k, $orig->$k) != 0) {
97 $parts[] = $k . ' = ' . $this->_quote($this->$k);
100 if (count($parts) == 0) {
104 $toupdate = implode(', ', $parts);
106 $table = $this->tableName();
107 if(common_config('db','quote_identifiers')) {
108 $table = '"' . $table . '"';
110 $qry = 'UPDATE ' . $table . ' SET ' . $toupdate .
111 ' WHERE id = ' . $this->id;
112 return $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 noticesWithFriends($offset=0, $limit=20) {
153 $notice = new Notice();
155 $query='SELECT notice.* ' .
156 'FROM notice JOIN subscription on notice.profile_id = subscription.subscribed ' .
157 'WHERE subscription.subscriber = ' . $this->id . ' ' .
158 'ORDER BY created DESC, notice.id DESC ';
159 if(common_config('db','type')=='pgsql') {
160 $query=$query . 'LIMIT ' . $limit . ' OFFSET ' . $offset;
162 $query=$query . 'LIMIT ' . $offset . ', ' . $limit;
164 $notice->query($query);
169 function favoriteNotices($offset=0, $limit=20) {
171 $notice = new Notice();
173 $notice->query('SELECT notice.* ' .
174 'FROM notice JOIN fave on notice.id = fave.notice_id ' .
175 'WHERE fave.user_id = ' . $this->id . ' ' .
176 'ORDER BY notice.created DESC, notice.id DESC ' .
177 'LIMIT ' . $offset . ', ' . $limit);
182 function noticesWithFriendsWindow() {
184 $cache = new Memcache();
185 $res = $cache->connect(common_config('memcached', 'server'), common_config('memcached', 'port'));
191 $notices = $cache->get(common_cache_key('user:notices_with_friends:' . $this->id));
197 $notice = new Notice();
199 $notice->query('SELECT notice.* ' .
200 'FROM notice JOIN subscription on notice.profile_id = subscription.subscribed ' .
201 'WHERE subscription.subscriber = ' . $this->id . ' ' .
202 'ORDER BY created DESC, notice.id DESC ' .
203 'LIMIT 0, ' . WITHFRIENDS_CACHE_WINDOW);
207 while ($notice->fetch()) {
208 $notices[] = clone($notice);
211 $cache->set(common_cache_key('user:notices_with_friends:' . $this->id), $notices);
215 static function register($fields) {
217 # MAGICALLY put fields into current scope
221 $profile = new Profile();
223 $profile->query('BEGIN');
225 $profile->nickname = $nickname;
226 $profile->profileurl = common_profile_url($nickname);
229 $profile->fullname = $fullname;
232 $profile->homepage = $homepage;
235 $profile->bio = $bio;
238 $profile->location = $location;
241 $profile->created = common_sql_now();
243 $id = $profile->insert();
246 common_log_db_error($profile, 'INSERT', __FILE__);
253 $user->nickname = $nickname;
255 if ($password) { # may not have a password for OpenID users
256 $user->password = common_munge_password($password, $id);
259 # Users who respond to invite email have proven their ownership of that address
262 $invite = Invitation::staticGet($code);
263 if ($invite && $invite->address && $invite->address_type == 'email' && $invite->address == $email) {
264 $user->email = $invite->address;
268 $user->created = common_sql_now();
269 $user->uri = common_user_uri($user);
271 $result = $user->insert();
274 common_log_db_error($user, 'INSERT', __FILE__);
278 # Everyone is subscribed to themself
280 $subscription = new Subscription();
281 $subscription->subscriber = $user->id;
282 $subscription->subscribed = $user->id;
283 $subscription->created = $user->created;
285 $result = $subscription->insert();
288 common_log_db_error($subscription, 'INSERT', __FILE__);
292 if ($email && !$user->email) {
294 $confirm = new Confirm_address();
295 $confirm->code = common_confirmation_code(128);
296 $confirm->user_id = $user->id;
297 $confirm->address = $email;
298 $confirm->address_type = 'email';
300 $result = $confirm->insert();
302 common_log_db_error($confirm, 'INSERT', __FILE__);
307 if ($code && $user->email) {
308 $user->emailChanged();
311 $profile->query('COMMIT');
313 if ($email && !$user->email) {
314 mail_confirm_address($confirm->code,
322 # Things we do when the email changes
324 function emailChanged() {
326 $invites = new Invitation();
327 $invites->address = $user->email;
328 $invites->address_type = 'email';
330 if ($invites->find()) {
331 while ($invites->fetch()) {
332 $other = User::staticGet($invites->user_id);
333 subs_subscribe_to($other, $this);
338 function hasFave($notice) {
340 $fave->user_id = $this->id;
341 $fave->notice_id = $notice->id;
352 function mutuallySubscribed($other) {
353 return $this->isSubscribed($other) &&
354 $other->isSubscribed($this);
357 function mutuallySubscribedUsers() {
359 # 3-way join; probably should get cached
361 $qry = 'SELECT user.* ' .
362 'FROM subscription sub1 JOIN user ON sub1.subscribed = user.id ' .
363 'JOIN subscription sub2 ON user.id = sub2.subscriber ' .
364 'WHERE sub1.subscriber = %d and sub2.subscribed = %d ' .
365 'ORDER BY user.nickname';
368 $user->query(sprintf($qry, $this->id, $this->id));