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