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 $emailmicroid; // tinyint(1) default_1
48 public $language; // varchar(50)
49 public $timezone; // varchar(50)
50 public $emailpost; // tinyint(1) default_1
51 public $jabber; // varchar(255) unique_key
52 public $jabbernotify; // tinyint(1)
53 public $jabberreplies; // tinyint(1)
54 public $jabbermicroid; // tinyint(1) default_1
55 public $updatefrompresence; // tinyint(1)
56 public $sms; // varchar(64) unique_key
57 public $carrier; // int(4)
58 public $smsnotify; // tinyint(1)
59 public $smsreplies; // tinyint(1)
60 public $smsemail; // varchar(255)
61 public $uri; // varchar(255) unique_key
62 public $autosubscribe; // tinyint(1)
63 public $created; // datetime() not_null
64 public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
67 function staticGet($k,$v=NULL) { return DB_DataObject::staticGet('User',$k,$v); }
69 /* the code above is auto generated do not remove the tag below */
72 function getProfile() {
73 $profile = DB_DataObject::factory('profile');
74 $profile->id = $this->id;
75 if ($profile->find()) {
82 function isSubscribed($other) {
83 assert(!is_null($other));
84 $sub = DB_DataObject::factory('subscription');
85 $sub->subscriber = $this->id;
86 $sub->subscribed = $other->id;
90 # 'update' won't write key columns, so we have to do it ourselves.
92 function updateKeys(&$orig) {
94 foreach (array('nickname', 'email', 'jabber', 'incomingemail', 'sms', 'carrier', 'smsemail', 'language', 'timezone') as $k) {
95 if (strcmp($this->$k, $orig->$k) != 0) {
96 $parts[] = $k . ' = ' . $this->_quote($this->$k);
99 if (count($parts) == 0) {
103 $toupdate = implode(', ', $parts);
104 $qry = 'UPDATE ' . $this->tableName() . ' SET ' . $toupdate .
105 ' WHERE id = ' . $this->id;
106 return $this->query($qry);
109 function allowed_nickname($nickname) {
110 # XXX: should already be validated for size, content, etc.
111 static $blacklist = array('rss', 'xrds', 'doc', 'main',
112 'settings', 'notice', 'user',
113 'search', 'avatar', 'tag', 'tags',
115 $merged = array_merge($blacklist, common_config('nickname', 'blacklist'));
116 return !in_array($nickname, $merged);
119 function getCurrentNotice($dt=NULL) {
120 $profile = $this->getProfile();
124 return $profile->getCurrentNotice($dt);
127 function getCarrier() {
128 return Sms_carrier::staticGet($this->carrier);
131 function subscribeTo($other) {
132 $sub = new Subscription();
133 $sub->subscriber = $this->id;
134 $sub->subscribed = $other->id;
136 $sub->created = common_sql_now(); # current time
138 if (!$sub->insert()) {
145 function noticesWithFriends($offset=0, $limit=20) {
147 $notice = new Notice();
149 $notice->query('SELECT notice.* ' .
150 'FROM notice JOIN subscription on notice.profile_id = subscription.subscribed ' .
151 'WHERE subscription.subscriber = ' . $this->id . ' ' .
152 'ORDER BY created DESC, notice.id DESC ' .
153 'LIMIT ' . $offset . ', ' . $limit);
158 function favoriteNotices($offset=0, $limit=20) {
160 $notice = new Notice();
162 $notice->query('SELECT notice.* ' .
163 'FROM notice JOIN fave on notice.id = fave.notice_id ' .
164 'WHERE fave.user_id = ' . $this->id . ' ' .
165 'ORDER BY notice.created DESC, notice.id DESC ' .
166 'LIMIT ' . $offset . ', ' . $limit);
171 function noticesWithFriendsWindow() {
173 $cache = new Memcache();
174 $res = $cache->connect(common_config('memcached', 'server'), common_config('memcached', 'port'));
180 $notices = $cache->get(common_cache_key('user:notices_with_friends:' . $this->id));
186 $notice = new Notice();
188 $notice->query('SELECT notice.* ' .
189 'FROM notice JOIN subscription on notice.profile_id = subscription.subscribed ' .
190 'WHERE subscription.subscriber = ' . $this->id . ' ' .
191 'ORDER BY created DESC, notice.id DESC ' .
192 'LIMIT 0, ' . WITHFRIENDS_CACHE_WINDOW);
196 while ($notice->fetch()) {
197 $notices[] = clone($notice);
200 $cache->set(common_cache_key('user:notices_with_friends:' . $this->id), $notices);
204 static function register($fields) {
206 # MAGICALLY put fields into current scope
210 $profile = new Profile();
212 $profile->query('BEGIN');
214 $profile->nickname = $nickname;
215 $profile->profileurl = common_profile_url($nickname);
218 $profile->fullname = $fullname;
221 $profile->homepage = $homepage;
224 $profile->bio = $bio;
227 $profile->location = $location;
230 $profile->created = common_sql_now();
232 $id = $profile->insert();
235 common_log_db_error($profile, 'INSERT', __FILE__);
242 $user->nickname = $nickname;
244 if ($password) { # may not have a password for OpenID users
245 $user->password = common_munge_password($password, $id);
248 # Users who respond to invite email have proven their ownership of that address
251 $invite = Invitation::staticGet($code);
252 if ($invite && $invite->address && $invite->address_type == 'email' && $invite->address == $email) {
253 $user->email = $invite->address;
257 $user->created = common_sql_now();
258 $user->uri = common_user_uri($user);
260 $result = $user->insert();
263 common_log_db_error($user, 'INSERT', __FILE__);
267 # Everyone is subscribed to themself
269 $subscription = new Subscription();
270 $subscription->subscriber = $user->id;
271 $subscription->subscribed = $user->id;
272 $subscription->created = $user->created;
274 $result = $subscription->insert();
277 common_log_db_error($subscription, 'INSERT', __FILE__);
281 if ($email && !$user->email) {
283 $confirm = new Confirm_address();
284 $confirm->code = common_confirmation_code(128);
285 $confirm->user_id = $user->id;
286 $confirm->address = $email;
287 $confirm->address_type = 'email';
289 $result = $confirm->insert();
291 common_log_db_error($confirm, 'INSERT', __FILE__);
296 if ($code && $user->email) {
297 $user->emailChanged();
300 $profile->query('COMMIT');
302 if ($email && !$user->email) {
303 mail_confirm_address($confirm->code,
311 # Things we do when the email changes
313 function emailChanged() {
315 $invites = new Invitation();
316 $invites->address = $user->email;
317 $invites->address_type = 'email';
319 if ($invites->find()) {
320 while ($invites->fetch()) {
321 $other = User::staticGet($invites->user_id);
322 subs_subscribe_to($other, $this);
327 function hasFave($notice) {
329 $fave->user_id = $this->id;
330 $fave->notice_id = $notice->id;