]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/User.php
better header for not-modified message
[quix0rs-gnu-social.git] / classes / User.php
1 <?php
2 /*
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, Controlez-Vous, Inc.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 if (!defined('LACONICA')) { exit(1); }
21
22 /**
23  * Table Definition for user
24  */
25 require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
26 require_once 'Validate.php';
27
28 class User extends Memcached_DataObject
29 {
30     ###START_AUTOCODE
31     /* the code below is auto generated do not remove the above tag */
32
33     public $__table = 'user';                            // table name
34     public $id;                              // int(4)  primary_key not_null
35     public $nickname;                        // varchar(64)  unique_key
36     public $password;                        // varchar(255)
37     public $email;                           // varchar(255)  unique_key
38     public $incomingemail;                   // varchar(255)  unique_key
39     public $emailnotifysub;                  // tinyint(1)   default_1
40     public $emailnotifyfav;                  // tinyint(1)   default_1
41     public $emailnotifynudge;                // tinyint(1)   default_1
42     public $emailnotifymsg;                  // tinyint(1)   default_1
43     public $emailmicroid;                    // tinyint(1)   default_1
44     public $language;                        // varchar(50)
45     public $timezone;                        // varchar(50)
46     public $emailpost;                       // tinyint(1)   default_1
47     public $jabber;                          // varchar(255)  unique_key
48     public $jabbernotify;                    // tinyint(1)
49     public $jabberreplies;                   // tinyint(1)
50     public $jabbermicroid;                   // tinyint(1)   default_1
51     public $updatefrompresence;              // tinyint(1)
52     public $sms;                             // varchar(64)  unique_key
53     public $carrier;                         // int(4)
54     public $smsnotify;                       // tinyint(1)
55     public $smsreplies;                      // tinyint(1)
56     public $smsemail;                        // varchar(255)
57     public $uri;                             // varchar(255)  unique_key
58     public $autosubscribe;                   // tinyint(1)
59     public $urlshorteningservice;            // varchar(50)   default_ur1.ca
60     public $inboxed;                         // tinyint(1)
61     public $created;                         // datetime()   not_null
62     public $modified;                        // timestamp()   not_null default_CURRENT_TIMESTAMP
63
64     /* Static get */
65     function staticGet($k,$v=NULL) { return Memcached_DataObject::staticGet('User',$k,$v); }
66
67     /* the code above is auto generated do not remove the tag below */
68     ###END_AUTOCODE
69
70         function getProfile() {
71                 return Profile::staticGet('id', $this->id);
72         }
73
74         function isSubscribed($other) {
75                 assert(!is_null($other));
76                 # XXX: cache results of this query
77                 $sub = Subscription::pkeyGet(array('subscriber' => $this->id,
78                                                                                    'subscribed' => $other->id));
79                 return (is_null($sub)) ? false : true;
80         }
81
82         # 'update' won't write key columns, so we have to do it ourselves.
83
84         function updateKeys(&$orig) {
85                 $parts = array();
86                 foreach (array('nickname', 'email', 'jabber', 'incomingemail', 'sms', 'carrier', 'smsemail', 'language', 'timezone') as $k) {
87                         if (strcmp($this->$k, $orig->$k) != 0) {
88                                 $parts[] = $k . ' = ' . $this->_quote($this->$k);
89                         }
90                 }
91                 if (count($parts) == 0) {
92                         # No changes
93                         return true;
94                 }
95                 $toupdate = implode(', ', $parts);
96
97                 $table = $this->tableName();
98                 if(common_config('db','quote_identifiers')) {
99                         $table = '"' . $table . '"';
100                 }
101                 $qry = 'UPDATE ' . $table . ' SET ' . $toupdate .
102                   ' WHERE id = ' . $this->id;
103                 $orig->decache();
104                 $result = $this->query($qry);
105                 if ($result) {
106                         $this->encache();
107                 }
108                 return $result;
109         }
110
111         function allowed_nickname($nickname) {
112                 # XXX: should already be validated for size, content, etc.
113                 static $blacklist = array('rss', 'xrds', 'doc', 'main',
114                                                                   'settings', 'notice', 'user',
115                                                                   'search', 'avatar', 'tag', 'tags',
116                                                                   'api', 'message');
117                 $merged = array_merge($blacklist, common_config('nickname', 'blacklist'));
118                 return !in_array($nickname, $merged);
119         }
120
121         function getCurrentNotice($dt=NULL) {
122                 $profile = $this->getProfile();
123                 if (!$profile) {
124                         return NULL;
125                 }
126                 return $profile->getCurrentNotice($dt);
127         }
128
129         function getCarrier() {
130                 return Sms_carrier::staticGet('id', $this->carrier);
131         }
132
133         function subscribeTo($other) {
134                 $sub = new Subscription();
135                 $sub->subscriber = $this->id;
136                 $sub->subscribed = $other->id;
137
138                 $sub->created = common_sql_now(); # current time
139
140                 if (!$sub->insert()) {
141                         return false;
142                 }
143
144                 return true;
145         }
146
147         static function register($fields) {
148
149                 # MAGICALLY put fields into current scope
150
151                 extract($fields);
152
153                 $profile = new Profile();
154
155                 $profile->query('BEGIN');
156
157                 $profile->nickname = $nickname;
158                 $profile->profileurl = common_profile_url($nickname);
159
160                 if ($fullname) {
161                         $profile->fullname = $fullname;
162                 }
163                 if ($homepage) {
164                         $profile->homepage = $homepage;
165                 }
166                 if ($bio) {
167                         $profile->bio = $bio;
168                 }
169                 if ($location) {
170                         $profile->location = $location;
171                 }
172
173                 $profile->created = common_sql_now();
174
175                 $id = $profile->insert();
176
177                 if (!$id) {
178                         common_log_db_error($profile, 'INSERT', __FILE__);
179                     return FALSE;
180                 }
181
182                 $user = new User();
183
184                 $user->id = $id;
185                 $user->nickname = $nickname;
186
187                 if ($password) { # may not have a password for OpenID users
188                         $user->password = common_munge_password($password, $id);
189                 }
190
191                 # Users who respond to invite email have proven their ownership of that address
192
193                 if ($code) {
194                         $invite = Invitation::staticGet($code);
195                         if ($invite && $invite->address && $invite->address_type == 'email' && $invite->address == $email) {
196                                 $user->email = $invite->address;
197                         }
198                 }
199
200                 $inboxes = common_config('inboxes', 'enabled');
201
202                 if ($inboxes === true || $inboxes == 'transitional') {
203                         $user->inboxed = 1;
204                 }
205
206                 $user->created = common_sql_now();
207                 $user->uri = common_user_uri($user);
208
209                 $result = $user->insert();
210
211                 if (!$result) {
212                         common_log_db_error($user, 'INSERT', __FILE__);
213                         return FALSE;
214                 }
215
216                 # Everyone is subscribed to themself
217
218                 $subscription = new Subscription();
219                 $subscription->subscriber = $user->id;
220                 $subscription->subscribed = $user->id;
221                 $subscription->created = $user->created;
222
223                 $result = $subscription->insert();
224
225                 if (!$result) {
226                         common_log_db_error($subscription, 'INSERT', __FILE__);
227                         return FALSE;
228                 }
229
230                 if ($email && !$user->email) {
231
232                         $confirm = new Confirm_address();
233                         $confirm->code = common_confirmation_code(128);
234                         $confirm->user_id = $user->id;
235                         $confirm->address = $email;
236                         $confirm->address_type = 'email';
237
238                         $result = $confirm->insert();
239                         if (!$result) {
240                                 common_log_db_error($confirm, 'INSERT', __FILE__);
241                                 return FALSE;
242                         }
243                 }
244
245                 if ($code && $user->email) {
246                         $user->emailChanged();
247                 }
248
249                 $profile->query('COMMIT');
250
251                 if ($email && !$user->email) {
252                         mail_confirm_address($confirm->code,
253                                                                  $profile->nickname,
254                                                                  $email);
255                 }
256
257                 return $user;
258         }
259
260         # Things we do when the email changes
261
262         function emailChanged() {
263
264                 $invites = new Invitation();
265                 $invites->address = $this->email;
266                 $invites->address_type = 'email';
267
268                 if ($invites->find()) {
269                         while ($invites->fetch()) {
270                                 $other = User::staticGet($invites->user_id);
271                                 subs_subscribe_to($other, $this);
272                         }
273                 }
274         }
275
276         function hasFave($notice) {
277                 $cache = common_memcache();
278
279                 # XXX: Kind of a hack.
280
281                 if ($cache) {
282                         # This is the stream of favorite notices, in rev chron
283                         # order. This forces it into cache.
284                         $faves = $this->favoriteNotices(0, NOTICE_CACHE_WINDOW);
285                         $cnt = 0;
286
287                         while ($faves->fetch()) {
288                                 if ($faves->id < $notice->id) {
289                                         # If we passed it, it's not a fave
290                                         return false;
291                                 } else if ($faves->id == $notice->id) {
292                                         # If it matches a cached notice, then it's a fave
293                                         return true;
294                                 }
295                                 $cnt++;
296                         }
297                         # If we're not past the end of the cache window,
298                         # then the cache has all available faves, so this one
299                         # is not a fave.
300                         if ($cnt < NOTICE_CACHE_WINDOW) {
301                                 return false;
302                         }
303                         # Otherwise, cache doesn't have all faves;
304                         # fall through to the default
305                 }
306
307                 $fave = Fave::pkeyGet(array('user_id' => $this->id,
308                                                                         'notice_id' => $notice->id));
309                 return ((is_null($fave)) ? false : true);
310         }
311
312         function mutuallySubscribed($other) {
313                 return $this->isSubscribed($other) &&
314                   $other->isSubscribed($this);
315         }
316
317         function mutuallySubscribedUsers() {
318
319                 # 3-way join; probably should get cached
320
321                 $qry = 'SELECT user.* ' .
322                   'FROM subscription sub1 JOIN user ON sub1.subscribed = user.id ' .
323                   'JOIN subscription sub2 ON user.id = sub2.subscriber ' .
324                   'WHERE sub1.subscriber = %d and sub2.subscribed = %d ' .
325                   'ORDER BY user.nickname';
326
327                 $user = new User();
328                 $user->query(sprintf($qry, $this->id, $this->id));
329
330                 return $user;
331         }
332
333         function getReplies($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0) {
334                 $qry =
335                   'SELECT notice.* ' .
336                   'FROM notice JOIN reply ON notice.id = reply.notice_id ' .
337                   'WHERE reply.profile_id = %d ';
338
339                 return Notice::getStream(sprintf($qry, $this->id),
340                                                                  'user:replies:'.$this->id,
341                                                                  $offset, $limit, $since_id, $before_id);
342         }
343
344         function getNotices($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0) {
345         $profile = $this->getProfile();
346         if (!$profile) {
347             return NULL;
348         } else {
349             return $profile->getNotices($offset, $limit, $since_id, $before_id);
350         }
351         }
352
353         function favoriteNotices($offset=0, $limit=NOTICES_PER_PAGE) {
354                 $qry =
355                   'SELECT notice.* ' .
356                   'FROM notice JOIN fave ON notice.id = fave.notice_id ' .
357                   'WHERE fave.user_id = %d ';
358
359                 return Notice::getStream(sprintf($qry, $this->id),
360                                                                  'user:faves:'.$this->id,
361                                                                  $offset, $limit);
362         }
363
364         function noticesWithFriends($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0) {
365                 $enabled = common_config('inboxes', 'enabled');
366
367                 # Complicated code, depending on whether we support inboxes yet
368                 # XXX: make this go away when inboxes become mandatory
369
370                 if ($enabled === false ||
371                         ($enabled == 'transitional' && $this->inboxed == 0)) {
372                         $qry =
373                           'SELECT notice.* ' .
374                           'FROM notice JOIN subscription ON notice.profile_id = subscription.subscribed ' .
375                           'WHERE subscription.subscriber = %d ';
376                         $order = NULL;
377                 } else if ($enabled === true ||
378                                    ($enabled == 'transitional' && $this->inboxed == 1)) {
379                         $qry =
380                           'SELECT notice.* ' .
381                           'FROM notice JOIN notice_inbox ON notice.id = notice_inbox.notice_id ' .
382                           'WHERE notice_inbox.user_id = %d ';
383                         # NOTE: we override ORDER
384                         $order = 'ORDER BY notice_inbox.created DESC, notice_inbox.notice_id DESC ';
385                 }
386
387                 return Notice::getStream(sprintf($qry, $this->id),
388                                                                  'user:notices_with_friends:' . $this->id,
389                                                                  $offset, $limit, $since_id, $before_id,
390                                                                  $order);
391         }
392
393         function blowFavesCache() {
394                 $cache = common_memcache();
395                 if ($cache) {
396                         # Faves don't happen chronologically, so we need to blow
397                         # ;last cache, too
398                         $cache->delete(common_cache_key('user:faves:'.$this->id));
399                         $cache->delete(common_cache_key('user:faves:'.$this->id).';last');
400                 }
401         }
402
403         function getSelfTags() {
404                 return Profile_tag::getTags($this->id, $this->id);
405         }
406
407         function setSelfTags($newtags) {
408                 return Profile_tag::setTags($this->id, $this->id, $newtags);
409         }
410 }