]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/User.php
a6a1b11b9f502d7a6a27d155ce840eb92154a0ae
[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)
66     { return Memcached_DataObject::staticGet('User',$k,$v); }
67
68     /* the code above is auto generated do not remove the tag below */
69     ###END_AUTOCODE
70
71     function getProfile()
72     {
73         return Profile::staticGet('id', $this->id);
74     }
75
76     function isSubscribed($other)
77     {
78         assert(!is_null($other));
79         # XXX: cache results of this query
80         $sub = Subscription::pkeyGet(array('subscriber' => $this->id,
81                                            'subscribed' => $other->id));
82         return (is_null($sub)) ? false : true;
83     }
84
85     # 'update' won't write key columns, so we have to do it ourselves.
86
87     function updateKeys(&$orig)
88     {
89         $parts = array();
90         foreach (array('nickname', 'email', 'jabber', 'incomingemail', 'sms', 'carrier', 'smsemail', 'language', 'timezone') as $k) {
91             if (strcmp($this->$k, $orig->$k) != 0) {
92                 $parts[] = $k . ' = ' . $this->_quote($this->$k);
93             }
94         }
95         if (count($parts) == 0) {
96             # No changes
97             return true;
98         }
99         $toupdate = implode(', ', $parts);
100
101         $table = $this->tableName();
102         if(common_config('db','quote_identifiers')) {
103             $table = '"' . $table . '"';
104         }
105         $qry = 'UPDATE ' . $table . ' SET ' . $toupdate .
106           ' WHERE id = ' . $this->id;
107         $orig->decache();
108         $result = $this->query($qry);
109         if ($result) {
110             $this->encache();
111         }
112         return $result;
113     }
114
115     function allowed_nickname($nickname)
116     {
117         # XXX: should already be validated for size, content, etc.
118         static $blacklist = array('rss', 'xrds', 'doc', 'main',
119                                   'settings', 'notice', 'user',
120                                   'search', 'avatar', 'tag', 'tags',
121                                   'api', 'message', 'group', 'groups');
122         $merged = array_merge($blacklist, common_config('nickname', 'blacklist'));
123         return !in_array($nickname, $merged);
124     }
125
126     function getCurrentNotice($dt=null)
127     {
128         $profile = $this->getProfile();
129         if (!$profile) {
130             return null;
131         }
132         return $profile->getCurrentNotice($dt);
133     }
134
135     function getCarrier()
136     {
137         return Sms_carrier::staticGet('id', $this->carrier);
138     }
139
140     function subscribeTo($other)
141     {
142         $sub = new Subscription();
143         $sub->subscriber = $this->id;
144         $sub->subscribed = $other->id;
145
146         $sub->created = common_sql_now(); # current time
147
148         if (!$sub->insert()) {
149             return false;
150         }
151
152         return true;
153     }
154
155     function hasBlocked($other)
156     {
157
158         $block = Profile_block::get($this->id, $other->id);
159
160         if (is_null($block)) {
161             $result = false;
162         } else {
163             $result = true;
164             $block->free();
165         }
166
167         return $result;
168     }
169
170     static function register($fields) {
171
172         # MAGICALLY put fields into current scope
173
174         extract($fields);
175
176         $profile = new Profile();
177
178         $profile->query('BEGIN');
179
180         $profile->nickname = $nickname;
181         $profile->profileurl = common_profile_url($nickname);
182
183         if ($fullname) {
184             $profile->fullname = $fullname;
185         }
186         if ($homepage) {
187             $profile->homepage = $homepage;
188         }
189         if ($bio) {
190             $profile->bio = $bio;
191         }
192         if ($location) {
193             $profile->location = $location;
194         }
195
196         $profile->created = common_sql_now();
197
198         $id = $profile->insert();
199
200         if (!$id) {
201             common_log_db_error($profile, 'INSERT', __FILE__);
202             return false;
203         }
204
205         $user = new User();
206
207         $user->id = $id;
208         $user->nickname = $nickname;
209
210         if ($password) { # may not have a password for OpenID users
211             $user->password = common_munge_password($password, $id);
212         }
213
214         # Users who respond to invite email have proven their ownership of that address
215
216         if ($code) {
217             $invite = Invitation::staticGet($code);
218             if ($invite && $invite->address && $invite->address_type == 'email' && $invite->address == $email) {
219                 $user->email = $invite->address;
220             }
221         }
222
223         $inboxes = common_config('inboxes', 'enabled');
224
225         if ($inboxes === true || $inboxes == 'transitional') {
226             $user->inboxed = 1;
227         }
228
229         $user->created = common_sql_now();
230         $user->uri = common_user_uri($user);
231
232         $result = $user->insert();
233
234         if (!$result) {
235             common_log_db_error($user, 'INSERT', __FILE__);
236             return false;
237         }
238
239         # Everyone is subscribed to themself
240
241         $subscription = new Subscription();
242         $subscription->subscriber = $user->id;
243         $subscription->subscribed = $user->id;
244         $subscription->created = $user->created;
245
246         $result = $subscription->insert();
247
248         if (!$result) {
249             common_log_db_error($subscription, 'INSERT', __FILE__);
250             return false;
251         }
252
253         if ($email && !$user->email) {
254
255             $confirm = new Confirm_address();
256             $confirm->code = common_confirmation_code(128);
257             $confirm->user_id = $user->id;
258             $confirm->address = $email;
259             $confirm->address_type = 'email';
260
261             $result = $confirm->insert();
262             if (!$result) {
263                 common_log_db_error($confirm, 'INSERT', __FILE__);
264                 return false;
265             }
266         }
267
268         if ($code && $user->email) {
269             $user->emailChanged();
270         }
271
272         $profile->query('COMMIT');
273
274         if ($email && !$user->email) {
275             mail_confirm_address($user, $confirm->code, $profile->nickname, $email);
276         }
277
278         return $user;
279     }
280
281     # Things we do when the email changes
282
283     function emailChanged()
284     {
285
286         $invites = new Invitation();
287         $invites->address = $this->email;
288         $invites->address_type = 'email';
289
290         if ($invites->find()) {
291             while ($invites->fetch()) {
292                 $other = User::staticGet($invites->user_id);
293                 subs_subscribe_to($other, $this);
294             }
295         }
296     }
297
298     function hasFave($notice)
299     {
300         $cache = common_memcache();
301
302         # XXX: Kind of a hack.
303         if ($cache) {
304             # This is the stream of favorite notices, in rev chron
305             # order. This forces it into cache.
306             $faves = $this->favoriteNotices(0, NOTICE_CACHE_WINDOW);
307             $cnt = 0;
308             while ($faves->fetch()) {
309                 if ($faves->id < $notice->id) {
310                     # If we passed it, it's not a fave
311                     return false;
312                 } else if ($faves->id == $notice->id) {
313                     # If it matches a cached notice, then it's a fave
314                     return true;
315                 }
316                 $cnt++;
317             }
318             # If we're not past the end of the cache window,
319             # then the cache has all available faves, so this one
320             # is not a fave.
321             if ($cnt < NOTICE_CACHE_WINDOW) {
322                 return false;
323             }
324             # Otherwise, cache doesn't have all faves;
325             # fall through to the default
326         }
327         $fave = Fave::pkeyGet(array('user_id' => $this->id,
328                                     'notice_id' => $notice->id));
329         return ((is_null($fave)) ? false : true);
330     }
331     function mutuallySubscribed($other)
332     {
333         return $this->isSubscribed($other) &&
334           $other->isSubscribed($this);
335     }
336
337         function mutuallySubscribedUsers()
338         {
339
340         # 3-way join; probably should get cached
341         $UT = common_config('db','type')=='pgsql'?'"user"':'user';
342         $qry = "SELECT $UT.* " .
343           "FROM subscription sub1 JOIN $UT ON sub1.subscribed = $UT.id " .
344           "JOIN subscription sub2 ON $UT.id = sub2.subscriber " .
345           'WHERE sub1.subscriber = %d and sub2.subscribed = %d ' .
346           "ORDER BY $UT.nickname";
347         $user = new User();
348         $user->query(sprintf($qry, $this->id, $this->id));
349
350         return $user;
351     }
352
353     function getReplies($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0, $since=null)
354     {
355         $qry =
356           'SELECT notice.* ' .
357           'FROM notice JOIN reply ON notice.id = reply.notice_id ' .
358           'WHERE reply.profile_id = %d ';
359         return Notice::getStream(sprintf($qry, $this->id),
360                                  'user:replies:'.$this->id,
361                                  $offset, $limit, $since_id, $before_id, null, $since);
362     }
363
364         function getNotices($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0, $since=null)
365         {
366         $profile = $this->getProfile();
367         if (!$profile) {
368             return null;
369         } else {
370             return $profile->getNotices($offset, $limit, $since_id, $before_id);
371         }
372     }
373
374       function favoriteNotices($offset=0, $limit=NOTICES_PER_PAGE)
375       {
376         $qry =
377           'SELECT notice.* ' .
378           'FROM notice JOIN fave ON notice.id = fave.notice_id ' .
379           'WHERE fave.user_id = %d ';
380         return Notice::getStream(sprintf($qry, $this->id),
381                                  'user:faves:'.$this->id,
382                                  $offset, $limit);
383     }
384
385         function noticesWithFriends($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0, $since=null)
386         {
387         $enabled = common_config('inboxes', 'enabled');
388
389         # Complicated code, depending on whether we support inboxes yet
390         # XXX: make this go away when inboxes become mandatory
391
392         if ($enabled === false ||
393             ($enabled == 'transitional' && $this->inboxed == 0)) {
394             $qry =
395               'SELECT notice.* ' .
396               'FROM notice JOIN subscription ON notice.profile_id = subscription.subscribed ' .
397               'WHERE subscription.subscriber = %d ';
398             $order = null;
399         } else if ($enabled === true ||
400                ($enabled == 'transitional' && $this->inboxed == 1)) {
401
402             $qry =
403               'SELECT notice.* ' .
404               'FROM notice JOIN notice_inbox ON notice.id = notice_inbox.notice_id ' .
405               'WHERE notice_inbox.user_id = %d ';
406             # NOTE: we override ORDER
407             $order = null;
408         }
409         return Notice::getStream(sprintf($qry, $this->id),
410                                  'user:notices_with_friends:' . $this->id,
411                                  $offset, $limit, $since_id, $before_id,
412                                  $order, $since);
413     }
414
415         function blowFavesCache()
416         {
417         $cache = common_memcache();
418         if ($cache) {
419             # Faves don't happen chronologically, so we need to blow
420             # ;last cache, too
421             $cache->delete(common_cache_key('user:faves:'.$this->id));
422             $cache->delete(common_cache_key('user:faves:'.$this->id).';last');
423         }
424     }
425
426         function getSelfTags()
427         {
428         return Profile_tag::getTags($this->id, $this->id);
429     }
430
431         function setSelfTags($newtags)
432         {
433         return Profile_tag::setTags($this->id, $this->id, $newtags);
434     }
435
436     function block($other)
437     {
438
439         # Add a new block record
440
441         $block = new Profile_block();
442
443         # Begin a transaction
444
445         $block->query('BEGIN');
446
447         $block->blocker = $this->id;
448         $block->blocked = $other->id;
449
450         $result = $block->insert();
451
452         if (!$result) {
453             common_log_db_error($block, 'INSERT', __FILE__);
454             return false;
455         }
456
457         # Cancel their subscription, if it exists
458
459         $sub = Subscription::pkeyGet(array('subscriber' => $other->id,
460                                            'subscribed' => $this->id));
461
462         if ($sub) {
463             $result = $sub->delete();
464             if (!$result) {
465                 common_log_db_error($sub, 'DELETE', __FILE__);
466                 return false;
467             }
468         }
469
470         $block->query('COMMIT');
471
472         return true;
473     }
474
475     function unblock($other)
476     {
477
478         # Get the block record
479
480         $block = Profile_block::get($this->id, $other->id);
481
482         if (!$block) {
483             return false;
484         }
485
486         $result = $block->delete();
487
488         if (!$result) {
489             common_log_db_error($block, 'DELETE', __FILE__);
490             return false;
491         }
492
493         return true;
494     }
495
496     function isMember($group)
497     {
498         $profile = $this->getProfile();
499         return $profile->isMember($group);
500     }
501
502     function isAdmin($group)
503     {
504         $profile = $this->getProfile();
505         return $profile->isAdmin($group);
506     }
507
508     function getGroups($offset=0, $limit=null)
509     {
510         $qry =
511           'SELECT user_group.* ' .
512           'FROM user_group JOIN group_member '.
513           'ON user_group.id = group_member.group_id ' .
514           'WHERE group_member.profile_id = %d ' .
515           'ORDER BY group_member.created DESC ';
516
517         if ($offset) {
518             if (common_config('db','type') == 'pgsql') {
519                 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
520             } else {
521                 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
522             }
523         }
524
525         $groups = new User_group();
526
527         $cnt = $groups->query(sprintf($qry, $this->id));
528
529         return $groups;
530     }
531
532     function getSubscriptions($offset=0, $limit=null)
533     {
534         $qry =
535           'SELECT profile.* ' .
536           'FROM profile JOIN subscription ' .
537           'ON profile.id = subscription.subscribed ' .
538           'WHERE subscription.subscriber = %d ' .
539           'AND subscription.subscribed != subscription.subscriber ' .
540           'ORDER BY subscription.created DESC ';
541
542         if (common_config('db','type') == 'pgsql') {
543             $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
544         } else {
545             $qry .= ' LIMIT ' . $offset . ', ' . $limit;
546         }
547
548         $profile = new Profile();
549
550         $profile->query(sprintf($qry, $this->id));
551
552         return $profile;
553     }
554
555     function getSubscribers($offset=0, $limit=null)
556     {
557         $qry =
558           'SELECT profile.* ' .
559           'FROM profile JOIN subscription ' .
560           'ON profile.id = subscription.subscriber ' .
561           'WHERE subscription.subscribed = %d ' .
562           'AND subscription.subscribed != subscription.subscriber ' .
563           'ORDER BY subscription.created DESC ';
564
565         if ($offset) {
566             if (common_config('db','type') == 'pgsql') {
567                 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
568             } else {
569                 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
570             }
571         }
572
573         $profile = new Profile();
574
575         $cnt = $profile->query(sprintf($qry, $this->id));
576
577         return $profile;
578     }
579
580     function getTaggedSubscribers($tag, $offset=0, $limit=null)
581     {
582         $qry =
583           'SELECT profile.* ' .
584           'FROM profile JOIN subscription ' .
585           'ON profile.id = subscription.subscriber ' .
586           'JOIN profile_tag ON (profile_tag.tagged = subscription.subscriber ' .
587           'AND profile_tag.tagger = subscription.subscribed) ' .
588           'WHERE subscription.subscribed = %d ' .
589           'AND profile_tag.tag = "%s" ' .
590           'AND subscription.subscribed != subscription.subscriber ' .
591           'ORDER BY subscription.created DESC ';
592
593         if ($offset) {
594             if (common_config('db','type') == 'pgsql') {
595                 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
596             } else {
597                 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
598             }
599         }
600
601         $profile = new Profile();
602
603         $cnt = $profile->query(sprintf($qry, $this->id, $tag));
604
605         return $profile;
606     }
607
608     function getTaggedSubscriptions($tag, $offset=0, $limit=null)
609     {
610         $qry =
611           'SELECT profile.* ' .
612           'FROM profile JOIN subscription ' .
613           'ON profile.id = subscription.subscribed ' .
614           'JOIN profile_tag on (profile_tag.tagged = subscription.subscribed ' .
615           'AND profile_tag.tagger = subscription.subscriber) ' .
616           'WHERE subscription.subscriber = %d ' .
617           'AND profile_tag.tag = "%s" ' .
618           'AND subscription.subscribed != subscription.subscriber ' .
619           'ORDER BY subscription.created DESC ';
620
621         if (common_config('db','type') == 'pgsql') {
622             $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
623         } else {
624             $qry .= ' LIMIT ' . $offset . ', ' . $limit;
625         }
626
627         $profile = new Profile();
628
629         $profile->query(sprintf($qry, $this->id, $tag));
630
631         return $profile;
632     }
633
634     function hasOpenID()
635     {
636         $oid = new User_openid();
637
638         $oid->user_id = $this->id;
639
640         $cnt = $oid->find();
641
642         return ($cnt > 0);
643     }
644 }