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 $emailmicroid; // tinyint(1) default_1
47 public $language; // varchar(50)
48 public $timezone; // varchar(50)
49 public $emailpost; // tinyint(1) default_1
50 public $jabber; // varchar(255) unique_key
51 public $jabbernotify; // tinyint(1)
52 public $jabberreplies; // tinyint(1)
53 public $jabbermicroid; // tinyint(1) default_1
54 public $updatefrompresence; // tinyint(1)
55 public $sms; // varchar(64) unique_key
56 public $carrier; // int(4)
57 public $smsnotify; // tinyint(1)
58 public $smsreplies; // tinyint(1)
59 public $smsemail; // varchar(255)
60 public $uri; // varchar(255) unique_key
61 public $autosubscribe; // tinyint(1)
62 public $created; // datetime() not_null
63 public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
66 function staticGet($k,$v=NULL) { return DB_DataObject::staticGet('User',$k,$v); }
68 /* the code above is auto generated do not remove the tag below */
71 function getProfile() {
72 $profile = DB_DataObject::factory('profile');
73 $profile->id = $this->id;
74 if ($profile->find()) {
81 function isSubscribed($other) {
82 assert(!is_null($other));
83 $sub = DB_DataObject::factory('subscription');
84 $sub->subscriber = $this->id;
85 $sub->subscribed = $other->id;
89 # 'update' won't write key columns, so we have to do it ourselves.
91 function updateKeys(&$orig) {
93 foreach (array('nickname', 'email', 'jabber', 'incomingemail', 'sms', 'carrier', 'smsemail', 'language', 'timezone') as $k) {
94 if (strcmp($this->$k, $orig->$k) != 0) {
95 $parts[] = $k . ' = ' . $this->_quote($this->$k);
98 if (count($parts) == 0) {
102 $toupdate = implode(', ', $parts);
103 $qry = 'UPDATE ' . $this->tableName() . ' SET ' . $toupdate .
104 ' WHERE id = ' . $this->id;
105 return $this->query($qry);
108 function allowed_nickname($nickname) {
109 # XXX: should already be validated for size, content, etc.
110 static $blacklist = array('rss', 'xrds', 'doc', 'main',
111 'settings', 'notice', 'user',
112 'search', 'avatar', 'tag', 'tags');
113 $merged = array_merge($blacklist, common_config('nickname', 'blacklist'));
114 return !in_array($nickname, $merged);
117 function getCurrentNotice($dt=NULL) {
118 $profile = $this->getProfile();
122 return $profile->getCurrentNotice($dt);
125 function getCarrier() {
126 return Sms_carrier::staticGet($this->carrier);
129 function subscribeTo($other) {
130 $sub = new Subscription();
131 $sub->subscriber = $this->id;
132 $sub->subscribed = $other->id;
134 $sub->created = common_sql_now(); # current time
136 if (!$sub->insert()) {
143 function noticesWithFriends($offset=0, $limit=20) {
145 $notice = new Notice();
147 $notice->query('SELECT notice.* ' .
148 'FROM notice JOIN subscription on notice.profile_id = subscription.subscribed ' .
149 'WHERE subscription.subscriber = ' . $this->id . ' ' .
150 'ORDER BY created DESC, notice.id DESC ' .
151 'LIMIT ' . $offset . ', ' . $limit);
156 static function register($fields) {
158 # MAGICALLY put fields into current scope
162 $profile = new Profile();
164 $profile->query('BEGIN');
166 $profile->nickname = $nickname;
167 $profile->profileurl = common_profile_url($nickname);
170 $profile->fullname = $fullname;
173 $profile->homepage = $homepage;
176 $profile->bio = $bio;
179 $profile->location = $location;
182 $profile->created = common_sql_now();
184 $id = $profile->insert();
187 common_log_db_error($profile, 'INSERT', __FILE__);
194 $user->nickname = $nickname;
196 if ($password) { # may not have a password for OpenID users
197 $user->password = common_munge_password($password, $id);
200 # Users who respond to invite email have proven their ownership of that address
203 $invite = Invite::staticGet($code);
204 if ($invite && $invite->address && $invite->address_type == 'email') {
205 $user->email = $invite->address;
209 $user->created = common_sql_now();
210 $user->uri = common_user_uri($user);
212 $result = $user->insert();
215 common_log_db_error($user, 'INSERT', __FILE__);
219 # Everyone is subscribed to themself
221 $subscription = new Subscription();
222 $subscription->subscriber = $user->id;
223 $subscription->subscribed = $user->id;
224 $subscription->created = $user->created;
226 $result = $subscription->insert();
229 common_log_db_error($subscription, 'INSERT', __FILE__);
233 if ($email && !$code) {
235 $confirm = new Confirm_address();
236 $confirm->code = common_confirmation_code(128);
237 $confirm->user_id = $user->id;
238 $confirm->address = $email;
239 $confirm->address_type = 'email';
241 $result = $confirm->insert();
243 common_log_db_error($confirm, 'INSERT', __FILE__);
248 if ($code && $user->email) {
249 $user->emailChanged();
252 $profile->query('COMMIT');
254 if ($email && !$code) {
255 mail_confirm_address($confirm->code,
263 # Things we do when the email changes
265 function emailChanged() {
267 $invites = new Invitation();
268 $invites->address = $user->email;
269 $invites->address_type = 'email';
271 if ($invites->find()) {
272 while ($invites->fetch()) {
273 $other = User::staticGet($invites->user_id);
274 subs_subscribe_to($other, $this);