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');
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($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 noticesWithFriends($offset=0, $limit=20) {
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 ' . $offset . ', ' . $limit);
157 function favoriteNotices($offset=0, $limit=20) {
159 $notice = new Notice();
161 $notice->query('SELECT notice.* ' .
162 'FROM notice JOIN fave on notice.id = fave.notice_id ' .
163 'WHERE fave.user_id = ' . $this->id . ' ' .
164 'ORDER BY notice.created DESC, notice.id DESC ' .
165 'LIMIT ' . $offset . ', ' . $limit);
170 function noticesWithFriendsWindow() {
172 $cache = new Memcache();
173 $res = $cache->connect(common_config('memcached', 'server'), common_config('memcached', 'port'));
179 $notices = $cache->get(common_cache_key('user:notices_with_friends:' . $this->id));
185 $notice = new Notice();
187 $notice->query('SELECT notice.* ' .
188 'FROM notice JOIN subscription on notice.profile_id = subscription.subscribed ' .
189 'WHERE subscription.subscriber = ' . $this->id . ' ' .
190 'ORDER BY created DESC, notice.id DESC ' .
191 'LIMIT 0, ' . WITHFRIENDS_CACHE_WINDOW);
195 while ($notice->fetch()) {
196 $notices[] = clone($notice);
199 $cache->set(common_cache_key('user:notices_with_friends:' . $this->id), $notices);
203 static function register($fields) {
205 # MAGICALLY put fields into current scope
209 $profile = new Profile();
211 $profile->query('BEGIN');
213 $profile->nickname = $nickname;
214 $profile->profileurl = common_profile_url($nickname);
217 $profile->fullname = $fullname;
220 $profile->homepage = $homepage;
223 $profile->bio = $bio;
226 $profile->location = $location;
229 $profile->created = common_sql_now();
231 $id = $profile->insert();
234 common_log_db_error($profile, 'INSERT', __FILE__);
241 $user->nickname = $nickname;
243 if ($password) { # may not have a password for OpenID users
244 $user->password = common_munge_password($password, $id);
247 # Users who respond to invite email have proven their ownership of that address
250 $invite = Invitation::staticGet($code);
251 if ($invite && $invite->address && $invite->address_type == 'email' && $invite->address == $email) {
252 $user->email = $invite->address;
256 $user->created = common_sql_now();
257 $user->uri = common_user_uri($user);
259 $result = $user->insert();
262 common_log_db_error($user, 'INSERT', __FILE__);
266 # Everyone is subscribed to themself
268 $subscription = new Subscription();
269 $subscription->subscriber = $user->id;
270 $subscription->subscribed = $user->id;
271 $subscription->created = $user->created;
273 $result = $subscription->insert();
276 common_log_db_error($subscription, 'INSERT', __FILE__);
280 if ($email && !$user->email) {
282 $confirm = new Confirm_address();
283 $confirm->code = common_confirmation_code(128);
284 $confirm->user_id = $user->id;
285 $confirm->address = $email;
286 $confirm->address_type = 'email';
288 $result = $confirm->insert();
290 common_log_db_error($confirm, 'INSERT', __FILE__);
295 if ($code && $user->email) {
296 $user->emailChanged();
299 $profile->query('COMMIT');
301 if ($email && !$user->email) {
302 mail_confirm_address($confirm->code,
310 # Things we do when the email changes
312 function emailChanged() {
314 $invites = new Invitation();
315 $invites->address = $user->email;
316 $invites->address_type = 'email';
318 if ($invites->find()) {
319 while ($invites->fetch()) {
320 $other = User::staticGet($invites->user_id);
321 subs_subscribe_to($other, $this);
326 function hasFave($notice) {
328 $fave->user_id = $this->id;
329 $fave->notice_id = $notice->id;