]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/User.php
a64dc449d8fa5e5db22b342dd3a20efff997d1d1
[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     function hasBlocked($other) {
148
149         $block = Profile_block::pkeyGet(array('blocker' => $this->id,
150                                               'blocked' => $other->id));
151
152         if (is_null($block)) {
153             $result = false;
154         } else {
155             $result = true;
156             $block->free();
157         }
158
159         return $result;
160     }
161
162         static function register($fields) {
163
164                 # MAGICALLY put fields into current scope
165
166                 extract($fields);
167
168                 $profile = new Profile();
169
170                 $profile->query('BEGIN');
171
172                 $profile->nickname = $nickname;
173                 $profile->profileurl = common_profile_url($nickname);
174
175                 if ($fullname) {
176                         $profile->fullname = $fullname;
177                 }
178                 if ($homepage) {
179                         $profile->homepage = $homepage;
180                 }
181                 if ($bio) {
182                         $profile->bio = $bio;
183                 }
184                 if ($location) {
185                         $profile->location = $location;
186                 }
187
188                 $profile->created = common_sql_now();
189
190                 $id = $profile->insert();
191
192                 if (!$id) {
193                         common_log_db_error($profile, 'INSERT', __FILE__);
194                     return FALSE;
195                 }
196
197                 $user = new User();
198
199                 $user->id = $id;
200                 $user->nickname = $nickname;
201
202                 if ($password) { # may not have a password for OpenID users
203                         $user->password = common_munge_password($password, $id);
204                 }
205
206                 # Users who respond to invite email have proven their ownership of that address
207
208                 if ($code) {
209                         $invite = Invitation::staticGet($code);
210                         if ($invite && $invite->address && $invite->address_type == 'email' && $invite->address == $email) {
211                                 $user->email = $invite->address;
212                         }
213                 }
214
215                 $inboxes = common_config('inboxes', 'enabled');
216                 
217                 if ($inboxes === true || $inboxes == 'transitional') {
218                         $user->inboxed = 1;
219                 }
220                 
221                 $user->created = common_sql_now();
222                 $user->uri = common_user_uri($user);
223
224                 $result = $user->insert();
225
226                 if (!$result) {
227                         common_log_db_error($user, 'INSERT', __FILE__);
228                         return FALSE;
229                 }
230
231                 # Everyone is subscribed to themself
232
233                 $subscription = new Subscription();
234                 $subscription->subscriber = $user->id;
235                 $subscription->subscribed = $user->id;
236                 $subscription->created = $user->created;
237
238                 $result = $subscription->insert();
239
240                 if (!$result) {
241                         common_log_db_error($subscription, 'INSERT', __FILE__);
242                         return FALSE;
243                 }
244
245                 if ($email && !$user->email) {
246
247                         $confirm = new Confirm_address();
248                         $confirm->code = common_confirmation_code(128);
249                         $confirm->user_id = $user->id;
250                         $confirm->address = $email;
251                         $confirm->address_type = 'email';
252
253                         $result = $confirm->insert();
254                         if (!$result) {
255                                 common_log_db_error($confirm, 'INSERT', __FILE__);
256                                 return FALSE;
257                         }
258                 }
259
260                 if ($code && $user->email) {
261                         $user->emailChanged();
262                 }
263
264                 $profile->query('COMMIT');
265
266                 if ($email && !$user->email) {
267                         mail_confirm_address($confirm->code,
268                                                                  $profile->nickname,
269                                                                  $email);
270                 }
271
272                 return $user;
273         }
274
275         # Things we do when the email changes
276
277         function emailChanged() {
278
279                 $invites = new Invitation();
280                 $invites->address = $this->email;
281                 $invites->address_type = 'email';
282
283                 if ($invites->find()) {
284                         while ($invites->fetch()) {
285                                 $other = User::staticGet($invites->user_id);
286                                 subs_subscribe_to($other, $this);
287                         }
288                 }
289         }
290
291         function hasFave($notice) {
292                 $cache = common_memcache();
293
294                 # XXX: Kind of a hack.
295                 
296                 if ($cache) {
297                         # This is the stream of favorite notices, in rev chron
298                         # order. This forces it into cache.
299                         $faves = $this->favoriteNotices(0, NOTICE_CACHE_WINDOW);
300                         $cnt = 0;
301                         
302                         while ($faves->fetch()) {
303                                 if ($faves->id < $notice->id) {
304                                         # If we passed it, it's not a fave
305                                         return false;
306                                 } else if ($faves->id == $notice->id) {
307                                         # If it matches a cached notice, then it's a fave
308                                         return true;
309                                 }
310                                 $cnt++;
311                         }
312                         # If we're not past the end of the cache window,
313                         # then the cache has all available faves, so this one
314                         # is not a fave.
315                         if ($cnt < NOTICE_CACHE_WINDOW) {
316                                 return false;
317                         }
318                         # Otherwise, cache doesn't have all faves;
319                         # fall through to the default
320                 }
321                 
322                 $fave = Fave::pkeyGet(array('user_id' => $this->id,
323                                                                         'notice_id' => $notice->id));
324                 return ((is_null($fave)) ? false : true);
325         }
326         
327         function mutuallySubscribed($other) {
328                 return $this->isSubscribed($other) &&
329                   $other->isSubscribed($this);
330         }
331         
332         function mutuallySubscribedUsers() {
333
334                 # 3-way join; probably should get cached
335                 
336                 $qry = 'SELECT user.* ' .
337                   'FROM subscription sub1 JOIN user ON sub1.subscribed = user.id ' .
338                   'JOIN subscription sub2 ON user.id = sub2.subscriber ' .
339                   'WHERE sub1.subscriber = %d and sub2.subscribed = %d ' .
340                   'ORDER BY user.nickname';
341                 
342                 $user = new User();
343                 $user->query(sprintf($qry, $this->id, $this->id));
344
345                 return $user;
346         }
347
348         function getReplies($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0) {
349                 $qry =
350                   'SELECT notice.* ' .
351                   'FROM notice JOIN reply ON notice.id = reply.notice_id ' .
352                   'WHERE reply.profile_id = %d ';
353                 
354                 return Notice::getStream(sprintf($qry, $this->id),
355                                                                  'user:replies:'.$this->id,
356                                                                  $offset, $limit, $since_id, $before_id);
357         }
358         
359         function getNotices($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0) {
360                 $qry =
361                   'SELECT * ' .
362                   'FROM notice ' .
363                   'WHERE profile_id = %d ';
364                 
365                 return Notice::getStream(sprintf($qry, $this->id),
366                                                                  'user:notices:'.$this->id,
367                                                                  $offset, $limit, $since_id, $before_id);
368         }
369         
370         function favoriteNotices($offset=0, $limit=NOTICES_PER_PAGE) {
371                 $qry =
372                   'SELECT notice.* ' .
373                   'FROM notice JOIN fave ON notice.id = fave.notice_id ' .
374                   'WHERE fave.user_id = %d ';
375                 
376                 return Notice::getStream(sprintf($qry, $this->id),
377                                                                  'user:faves:'.$this->id,
378                                                                  $offset, $limit);
379         }
380         
381         function noticesWithFriends($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0) {
382                 $enabled = common_config('inboxes', 'enabled');
383
384                 # Complicated code, depending on whether we support inboxes yet
385                 # XXX: make this go away when inboxes become mandatory
386                 
387                 if ($enabled === false || 
388                         ($enabled == 'transitional' && $this->inboxed == 0)) {
389                         $qry =
390                           'SELECT notice.* ' .
391                           'FROM notice JOIN subscription ON notice.profile_id = subscription.subscribed ' .
392                           'WHERE subscription.subscriber = %d ';
393                         $order = NULL;
394                 } else if ($enabled === true ||
395                                    ($enabled == 'transitional' && $this->inboxed == 1)) {                                  
396                         $qry =
397                           'SELECT notice.* ' .
398                           'FROM notice JOIN notice_inbox ON notice.id = notice_inbox.notice_id ' .
399                           'WHERE notice_inbox.user_id = %d ';
400                         # NOTE: we override ORDER
401                         $order = 'ORDER BY notice_inbox.created DESC, notice_inbox.notice_id DESC ';
402                 }
403                 
404                 return Notice::getStream(sprintf($qry, $this->id),
405                                                                  'user:notices_with_friends:' . $this->id,
406                                                                  $offset, $limit, $since_id, $before_id,
407                                                                  $order);
408         }
409         
410         function blowFavesCache() {
411                 $cache = common_memcache();
412                 if ($cache) {
413                         # Faves don't happen chronologically, so we need to blow
414                         # ;last cache, too
415                         $cache->delete(common_cache_key('user:faves:'.$this->id));
416                         $cache->delete(common_cache_key('user:faves:'.$this->id).';last');
417                 }
418         }
419         
420         function getSelfTags() {
421                 return Profile_tag::getTags($this->id, $this->id);
422         }
423         
424         function setSelfTags($newtags) {
425                 return Profile_tag::setTags($this->id, $this->id, $newtags);
426         }
427
428     function block($other) {
429
430         # Add a new block record
431
432         $block = new Profile_block();
433
434         # Begin a transaction
435
436         $block->query('BEGIN');
437
438         $block->blocker = $this->id;
439         $block->blocked = $other->id;
440
441         $result = $block->insert();
442
443         if (!$result) {
444             common_log_db_error($block, 'INSERT', __FILE__);
445             return false;
446         }
447
448         # Cancel their subscription, if it exists
449
450                 $sub = Subscription::pkeyGet(array('subscriber' => $other->id,
451                                                                                    'subscribed' => $this->id));
452
453         if ($sub) {
454             $result = $sub->delete();
455             if (!$result) {
456                 common_log_db_error($sub, 'DELETE', __FILE__);
457                 return false;
458             }
459         }
460
461         $block->query('COMMIT');
462
463         return true;
464     }
465
466     function unblock($other) {
467
468         # Get the block record
469
470         $block = Profile_block::pkeyGet(array('blocker' => $this->id,
471                                               'blocked' => $other->id));
472
473         if (!$block) {
474             return false;
475         }
476
477         $result = $block->delete();
478
479         if (!$result) {
480             common_log_db_error($block, 'DELETE', __FILE__);
481             return false;
482         }
483
484         return true;
485     }
486
487 }