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);
105 $table = $this->tableName();
106 if(common_config('db','quote_identifiers')) {
107 $table = '"' . $table . '"';
109 $qry = 'UPDATE ' . $table . ' SET ' . $toupdate .
110 ' WHERE id = ' . $this->id;
111 return $this->query($qry);
114 function allowed_nickname($nickname) {
115 # XXX: should already be validated for size, content, etc.
116 static $blacklist = array('rss', 'xrds', 'doc', 'main',
117 'settings', 'notice', 'user',
118 'search', 'avatar', 'tag', 'tags',
120 $merged = array_merge($blacklist, common_config('nickname', 'blacklist'));
121 return !in_array($nickname, $merged);
124 function getCurrentNotice($dt=NULL) {
125 $profile = $this->getProfile();
129 return $profile->getCurrentNotice($dt);
132 function getCarrier() {
133 return Sms_carrier::staticGet($this->carrier);
136 function subscribeTo($other) {
137 $sub = new Subscription();
138 $sub->subscriber = $this->id;
139 $sub->subscribed = $other->id;
141 $sub->created = common_sql_now(); # current time
143 if (!$sub->insert()) {
150 function noticesWithFriends($offset=0, $limit=20) {
152 $notice = new Notice();
154 $query='SELECT notice.* ' .
155 'FROM notice JOIN subscription on notice.profile_id = subscription.subscribed ' .
156 'WHERE subscription.subscriber = ' . $this->id . ' ' .
157 'ORDER BY created DESC, notice.id DESC ';
158 if(common_config('db','type')=='pgsql') {
159 $query=$query . 'LIMIT ' . $limit . ' OFFSET ' . $offset;
161 $query=$query . 'LIMIT ' . $offset . ', ' . $limit;
163 $notice->query($query);
168 function favoriteNotices($offset=0, $limit=20) {
170 $notice = new Notice();
172 $notice->query('SELECT notice.* ' .
173 'FROM notice JOIN fave on notice.id = fave.notice_id ' .
174 'WHERE fave.user_id = ' . $this->id . ' ' .
175 'ORDER BY notice.created DESC, notice.id DESC ' .
176 'LIMIT ' . $offset . ', ' . $limit);
181 function noticesWithFriendsWindow() {
183 $cache = new Memcache();
184 $res = $cache->connect(common_config('memcached', 'server'), common_config('memcached', 'port'));
190 $notices = $cache->get(common_cache_key('user:notices_with_friends:' . $this->id));
196 $notice = new Notice();
198 $notice->query('SELECT notice.* ' .
199 'FROM notice JOIN subscription on notice.profile_id = subscription.subscribed ' .
200 'WHERE subscription.subscriber = ' . $this->id . ' ' .
201 'ORDER BY created DESC, notice.id DESC ' .
202 'LIMIT 0, ' . WITHFRIENDS_CACHE_WINDOW);
206 while ($notice->fetch()) {
207 $notices[] = clone($notice);
210 $cache->set(common_cache_key('user:notices_with_friends:' . $this->id), $notices);
214 static function register($fields) {
216 # MAGICALLY put fields into current scope
220 $profile = new Profile();
222 $profile->query('BEGIN');
224 $profile->nickname = $nickname;
225 $profile->profileurl = common_profile_url($nickname);
228 $profile->fullname = $fullname;
231 $profile->homepage = $homepage;
234 $profile->bio = $bio;
237 $profile->location = $location;
240 $profile->created = common_sql_now();
242 $id = $profile->insert();
245 common_log_db_error($profile, 'INSERT', __FILE__);
252 $user->nickname = $nickname;
254 if ($password) { # may not have a password for OpenID users
255 $user->password = common_munge_password($password, $id);
258 # Users who respond to invite email have proven their ownership of that address
261 $invite = Invitation::staticGet($code);
262 if ($invite && $invite->address && $invite->address_type == 'email' && $invite->address == $email) {
263 $user->email = $invite->address;
267 $user->created = common_sql_now();
268 $user->uri = common_user_uri($user);
270 $result = $user->insert();
273 common_log_db_error($user, 'INSERT', __FILE__);
277 # Everyone is subscribed to themself
279 $subscription = new Subscription();
280 $subscription->subscriber = $user->id;
281 $subscription->subscribed = $user->id;
282 $subscription->created = $user->created;
284 $result = $subscription->insert();
287 common_log_db_error($subscription, 'INSERT', __FILE__);
291 if ($email && !$user->email) {
293 $confirm = new Confirm_address();
294 $confirm->code = common_confirmation_code(128);
295 $confirm->user_id = $user->id;
296 $confirm->address = $email;
297 $confirm->address_type = 'email';
299 $result = $confirm->insert();
301 common_log_db_error($confirm, 'INSERT', __FILE__);
306 if ($code && $user->email) {
307 $user->emailChanged();
310 $profile->query('COMMIT');
312 if ($email && !$user->email) {
313 mail_confirm_address($confirm->code,
321 # Things we do when the email changes
323 function emailChanged() {
325 $invites = new Invitation();
326 $invites->address = $user->email;
327 $invites->address_type = 'email';
329 if ($invites->find()) {
330 while ($invites->fetch()) {
331 $other = User::staticGet($invites->user_id);
332 subs_subscribe_to($other, $this);
337 function hasFave($notice) {
339 $fave->user_id = $this->id;
340 $fave->notice_id = $notice->id;