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