]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/User.php
Merge branch 'testing' of git@gitorious.org:statusnet/mainline into 0.9.x
[quix0rs-gnu-social.git] / classes / User.php
1 <?php
2 /*
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2008, 2009, StatusNet, 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('STATUSNET') && !defined('LACONICA')) {
21     exit(1);
22 }
23
24 /**
25  * Table Definition for user
26  */
27
28 require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
29 require_once 'Validate.php';
30
31 class User extends Memcached_DataObject
32 {
33     ###START_AUTOCODE
34     /* the code below is auto generated do not remove the above tag */
35
36     public $__table = 'user';                            // table name
37     public $id;                              // int(4)  primary_key not_null
38     public $nickname;                        // varchar(64)  unique_key
39     public $password;                        // varchar(255)
40     public $email;                           // varchar(255)  unique_key
41     public $incomingemail;                   // varchar(255)  unique_key
42     public $emailnotifysub;                  // tinyint(1)   default_1
43     public $emailnotifyfav;                  // tinyint(1)   default_1
44     public $emailnotifynudge;                // tinyint(1)   default_1
45     public $emailnotifymsg;                  // tinyint(1)   default_1
46     public $emailnotifyattn;                 // tinyint(1)   default_1
47     public $emailmicroid;                    // tinyint(1)   default_1
48     public $language;                        // varchar(50)
49     public $timezone;                        // varchar(50)
50     public $emailpost;                       // tinyint(1)   default_1
51     public $jabber;                          // varchar(255)  unique_key
52     public $jabbernotify;                    // tinyint(1)
53     public $jabberreplies;                   // tinyint(1)
54     public $jabbermicroid;                   // tinyint(1)   default_1
55     public $updatefrompresence;              // tinyint(1)
56     public $sms;                             // varchar(64)  unique_key
57     public $carrier;                         // int(4)
58     public $smsnotify;                       // tinyint(1)
59     public $smsreplies;                      // tinyint(1)
60     public $smsemail;                        // varchar(255)
61     public $uri;                             // varchar(255)  unique_key
62     public $autosubscribe;                   // tinyint(1)
63     public $urlshorteningservice;            // varchar(50)   default_ur1.ca
64     public $inboxed;                         // tinyint(1)
65     public $design_id;                       // int(4)
66     public $viewdesigns;                     // tinyint(1)   default_1
67     public $created;                         // datetime()   not_null
68     public $modified;                        // timestamp()   not_null default_CURRENT_TIMESTAMP
69
70     /* Static get */
71     function staticGet($k,$v=NULL) { return Memcached_DataObject::staticGet('User',$k,$v); }
72
73     /* the code above is auto generated do not remove the tag below */
74     ###END_AUTOCODE
75
76     function getProfile()
77     {
78         return Profile::staticGet('id', $this->id);
79     }
80
81     function isSubscribed($other)
82     {
83         return Subscription::exists($this->getProfile(), $other);
84     }
85
86     // 'update' won't write key columns, so we have to do it ourselves.
87
88     function updateKeys(&$orig)
89     {
90         $parts = array();
91         foreach (array('nickname', 'email', 'jabber', 'incomingemail', 'sms', 'carrier', 'smsemail', 'language', 'timezone') as $k) {
92             if (strcmp($this->$k, $orig->$k) != 0) {
93                 $parts[] = $k . ' = ' . $this->_quote($this->$k);
94             }
95         }
96         if (count($parts) == 0) {
97             // No changes
98             return true;
99         }
100         $toupdate = implode(', ', $parts);
101
102         $table = common_database_tablename($this->tableName());
103         $qry = 'UPDATE ' . $table . ' SET ' . $toupdate .
104           ' WHERE id = ' . $this->id;
105         $orig->decache();
106         $result = $this->query($qry);
107         if ($result) {
108             $this->encache();
109         }
110         return $result;
111     }
112
113     static function allowed_nickname($nickname)
114     {
115         // XXX: should already be validated for size, content, etc.
116         $blacklist = common_config('nickname', 'blacklist');
117
118         //all directory and file names should be blacklisted
119         $d = dir(INSTALLDIR);
120         while (false !== ($entry = $d->read())) {
121             $blacklist[]=$entry;
122         }
123         $d->close();
124
125         //all top level names in the router should be blacklisted
126         $router = Router::get();
127         foreach(array_keys($router->m->getPaths()) as $path){
128             if(preg_match('/^\/(.*?)[\/\?]/',$path,$matches)){
129                 $blacklist[]=$matches[1];
130             }
131         }
132         return !in_array($nickname, $blacklist);
133     }
134
135     /**
136      * Get the most recent notice posted by this user, if any.
137      *
138      * @return mixed Notice or null
139      */
140     function getCurrentNotice()
141     {
142         $profile = $this->getProfile();
143         if (!$profile) {
144             return null;
145         }
146         return $profile->getCurrentNotice();
147     }
148
149     function getCarrier()
150     {
151         return Sms_carrier::staticGet('id', $this->carrier);
152     }
153
154     function subscribeTo($other)
155     {
156         $sub = new Subscription();
157         $sub->subscriber = $this->id;
158         $sub->subscribed = $other->id;
159
160         $sub->created = common_sql_now(); // current time
161
162         if (!$sub->insert()) {
163             return false;
164         }
165
166         return true;
167     }
168
169     function hasBlocked($other)
170     {
171         $profile = $this->getProfile();
172         return $profile->hasBlocked($other);
173     }
174
175     /**
176      * Register a new user account and profile and set up default subscriptions.
177      * If a new-user welcome message is configured, this will be sent.
178      *
179      * @param array $fields associative array of optional properties
180      *              string 'bio'
181      *              string 'email'
182      *              bool 'email_confirmed' pass true to mark email as pre-confirmed
183      *              string 'fullname'
184      *              string 'homepage'
185      *              string 'location' informal string description of geolocation
186      *              float 'lat' decimal latitude for geolocation
187      *              float 'lon' decimal longitude for geolocation
188      *              int 'location_id' geoname identifier
189      *              int 'location_ns' geoname namespace to interpret location_id
190      *              string 'nickname' REQUIRED
191      *              string 'password' (may be missing for eg OpenID registrations)
192      *              string 'code' invite code
193      *              ?string 'uri' permalink to notice; defaults to local notice URL
194      * @return mixed User object or false on failure
195      */
196     static function register($fields) {
197
198         // MAGICALLY put fields into current scope
199
200         extract($fields);
201
202         $profile = new Profile();
203
204         if(!empty($email))
205         {
206             $email = common_canonical_email($email);
207         }
208
209         $nickname = common_canonical_nickname($nickname);
210         $profile->nickname = $nickname;
211         if(! User::allowed_nickname($nickname)){
212             common_log(LOG_WARNING, sprintf("Attempted to register a nickname that is not allowed: %s", $profile->nickname),
213                        __FILE__);
214             return false;
215         }
216         $profile->profileurl = common_profile_url($nickname);
217
218         if (!empty($fullname)) {
219             $profile->fullname = $fullname;
220         }
221         if (!empty($homepage)) {
222             $profile->homepage = $homepage;
223         }
224         if (!empty($bio)) {
225             $profile->bio = $bio;
226         }
227         if (!empty($location)) {
228             $profile->location = $location;
229
230             $loc = Location::fromName($location);
231
232             if (!empty($loc)) {
233                 $profile->lat         = $loc->lat;
234                 $profile->lon         = $loc->lon;
235                 $profile->location_id = $loc->location_id;
236                 $profile->location_ns = $loc->location_ns;
237             }
238         }
239
240         $profile->created = common_sql_now();
241
242         $user = new User();
243
244         $user->nickname = $nickname;
245
246         // Users who respond to invite email have proven their ownership of that address
247
248         if (!empty($code)) {
249             $invite = Invitation::staticGet($code);
250             if ($invite && $invite->address && $invite->address_type == 'email' && $invite->address == $email) {
251                 $user->email = $invite->address;
252             }
253         }
254
255         if(isset($email_confirmed) && $email_confirmed) {
256             $user->email = $email;
257         }
258
259         // This flag is ignored but still set to 1
260
261         $user->inboxed = 1;
262
263         $user->created = common_sql_now();
264
265         if (Event::handle('StartUserRegister', array(&$user, &$profile))) {
266
267             $profile->query('BEGIN');
268
269             $id = $profile->insert();
270
271             if (empty($id)) {
272                 common_log_db_error($profile, 'INSERT', __FILE__);
273                 return false;
274             }
275
276             $user->id = $id;
277             $user->uri = common_user_uri($user);
278             if (!empty($password)) { // may not have a password for OpenID users
279                 $user->password = common_munge_password($password, $id);
280             }
281
282             $result = $user->insert();
283
284             if (!$result) {
285                 common_log_db_error($user, 'INSERT', __FILE__);
286                 return false;
287             }
288
289             // Everyone gets an inbox
290
291             $inbox = new Inbox();
292
293             $inbox->user_id = $user->id;
294             $inbox->notice_ids = '';
295
296             $result = $inbox->insert();
297
298             if (!$result) {
299                 common_log_db_error($inbox, 'INSERT', __FILE__);
300                 return false;
301             }
302
303             // Everyone is subscribed to themself
304
305             $subscription = new Subscription();
306             $subscription->subscriber = $user->id;
307             $subscription->subscribed = $user->id;
308             $subscription->created = $user->created;
309
310             $result = $subscription->insert();
311
312             if (!$result) {
313                 common_log_db_error($subscription, 'INSERT', __FILE__);
314                 return false;
315             }
316
317             if (!empty($email) && !$user->email) {
318
319                 $confirm = new Confirm_address();
320                 $confirm->code = common_confirmation_code(128);
321                 $confirm->user_id = $user->id;
322                 $confirm->address = $email;
323                 $confirm->address_type = 'email';
324
325                 $result = $confirm->insert();
326
327                 if (!$result) {
328                     common_log_db_error($confirm, 'INSERT', __FILE__);
329                     return false;
330                 }
331             }
332
333             if (!empty($code) && $user->email) {
334                 $user->emailChanged();
335             }
336
337             // Default system subscription
338
339             $defnick = common_config('newuser', 'default');
340
341             if (!empty($defnick)) {
342                 $defuser = User::staticGet('nickname', $defnick);
343                 if (empty($defuser)) {
344                     common_log(LOG_WARNING, sprintf("Default user %s does not exist.", $defnick),
345                                __FILE__);
346                 } else {
347                     $defsub = new Subscription();
348                     $defsub->subscriber = $user->id;
349                     $defsub->subscribed = $defuser->id;
350                     $defsub->created = $user->created;
351
352                     $result = $defsub->insert();
353
354                     if (!$result) {
355                         common_log_db_error($defsub, 'INSERT', __FILE__);
356                         return false;
357                     }
358                 }
359             }
360
361             $profile->query('COMMIT');
362
363             if (!empty($email) && !$user->email) {
364                 mail_confirm_address($user, $confirm->code, $profile->nickname, $email);
365             }
366
367             // Welcome message
368
369             $welcome = common_config('newuser', 'welcome');
370
371             if (!empty($welcome)) {
372                 $welcomeuser = User::staticGet('nickname', $welcome);
373                 if (empty($welcomeuser)) {
374                     common_log(LOG_WARNING, sprintf("Welcome user %s does not exist.", $defnick),
375                                __FILE__);
376                 } else {
377                     $notice = Notice::saveNew($welcomeuser->id,
378                                               sprintf(_('Welcome to %1$s, @%2$s!'),
379                                                       common_config('site', 'name'),
380                                                       $user->nickname),
381                                               'system');
382
383                 }
384             }
385
386             Event::handle('EndUserRegister', array(&$profile, &$user));
387         }
388
389         return $user;
390     }
391
392     // Things we do when the email changes
393
394     function emailChanged()
395     {
396
397         $invites = new Invitation();
398         $invites->address = $this->email;
399         $invites->address_type = 'email';
400
401         if ($invites->find()) {
402             while ($invites->fetch()) {
403                 $other = User::staticGet($invites->user_id);
404                 subs_subscribe_to($other, $this);
405             }
406         }
407     }
408
409     function hasFave($notice)
410     {
411         $cache = common_memcache();
412
413         // XXX: Kind of a hack.
414
415         if ($cache) {
416             // This is the stream of favorite notices, in rev chron
417             // order. This forces it into cache.
418
419             $ids = Fave::stream($this->id, 0, NOTICE_CACHE_WINDOW);
420
421             // If it's in the list, then it's a fave
422
423             if (in_array($notice->id, $ids)) {
424                 return true;
425             }
426
427             // If we're not past the end of the cache window,
428             // then the cache has all available faves, so this one
429             // is not a fave.
430
431             if (count($ids) < NOTICE_CACHE_WINDOW) {
432                 return false;
433             }
434
435             // Otherwise, cache doesn't have all faves;
436             // fall through to the default
437         }
438
439         $fave = Fave::pkeyGet(array('user_id' => $this->id,
440                                     'notice_id' => $notice->id));
441         return ((is_null($fave)) ? false : true);
442     }
443
444     function mutuallySubscribed($other)
445     {
446         return $this->isSubscribed($other) &&
447           $other->isSubscribed($this);
448     }
449
450     function mutuallySubscribedUsers()
451     {
452         // 3-way join; probably should get cached
453         $UT = common_config('db','type')=='pgsql'?'"user"':'user';
454         $qry = "SELECT $UT.* " .
455           "FROM subscription sub1 JOIN $UT ON sub1.subscribed = $UT.id " .
456           "JOIN subscription sub2 ON $UT.id = sub2.subscriber " .
457           'WHERE sub1.subscriber = %d and sub2.subscribed = %d ' .
458           "ORDER BY $UT.nickname";
459         $user = new User();
460         $user->query(sprintf($qry, $this->id, $this->id));
461
462         return $user;
463     }
464
465     function getReplies($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0)
466     {
467         $ids = Reply::stream($this->id, $offset, $limit, $since_id, $before_id);
468         return Notice::getStreamByIds($ids);
469     }
470
471     function getTaggedNotices($tag, $offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0) {
472         $profile = $this->getProfile();
473         if (!$profile) {
474             return null;
475         } else {
476             return $profile->getTaggedNotices($tag, $offset, $limit, $since_id, $before_id);
477         }
478     }
479
480     function getNotices($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0)
481     {
482         $profile = $this->getProfile();
483         if (!$profile) {
484             return null;
485         } else {
486             return $profile->getNotices($offset, $limit, $since_id, $before_id);
487         }
488     }
489
490     function favoriteNotices($offset=0, $limit=NOTICES_PER_PAGE, $own=false)
491     {
492         $ids = Fave::stream($this->id, $offset, $limit, $own);
493         return Notice::getStreamByIds($ids);
494     }
495
496     function noticesWithFriends($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0)
497     {
498         return Inbox::streamNotices($this->id, $offset, $limit, $since_id, $before_id, false);
499     }
500
501     function noticeInbox($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0)
502     {
503         return Inbox::streamNotices($this->id, $offset, $limit, $since_id, $before_id, true);
504     }
505
506     function friendsTimeline($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0)
507     {
508         return Inbox::streamNotices($this->id, $offset, $limit, $since_id, $before_id, false);
509     }
510
511     function ownFriendsTimeline($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0)
512     {
513         return Inbox::streamNotices($this->id, $offset, $limit, $since_id, $before_id, true);
514     }
515
516     function blowFavesCache()
517     {
518         $cache = common_memcache();
519         if ($cache) {
520             // Faves don't happen chronologically, so we need to blow
521             // ;last cache, too
522             $cache->delete(common_cache_key('fave:ids_by_user:'.$this->id));
523             $cache->delete(common_cache_key('fave:ids_by_user:'.$this->id.';last'));
524             $cache->delete(common_cache_key('fave:ids_by_user_own:'.$this->id));
525             $cache->delete(common_cache_key('fave:ids_by_user_own:'.$this->id.';last'));
526         }
527         $profile = $this->getProfile();
528         $profile->blowFaveCount();
529     }
530
531     function getSelfTags()
532     {
533         return Profile_tag::getTags($this->id, $this->id);
534     }
535
536     function setSelfTags($newtags)
537     {
538         return Profile_tag::setTags($this->id, $this->id, $newtags);
539     }
540
541     function block($other)
542     {
543         // Add a new block record
544
545         // no blocking (and thus unsubbing from) yourself
546
547         if ($this->id == $other->id) {
548             common_log(LOG_WARNING,
549                 sprintf(
550                     "Profile ID %d (%s) tried to block his or herself.",
551                     $profile->id,
552                     $profile->nickname
553                 )
554             );
555             return false;
556         }
557
558         $block = new Profile_block();
559
560         // Begin a transaction
561
562         $block->query('BEGIN');
563
564         $block->blocker = $this->id;
565         $block->blocked = $other->id;
566
567         $result = $block->insert();
568
569         if (!$result) {
570             common_log_db_error($block, 'INSERT', __FILE__);
571             return false;
572         }
573
574         // Cancel their subscription, if it exists
575
576         $otherUser = User::staticGet('id', $other->id);
577
578         if (!empty($otherUser)) {
579             subs_unsubscribe_to($otherUser, $this->getProfile());
580         }
581
582         $block->query('COMMIT');
583
584         return true;
585     }
586
587     function unblock($other)
588     {
589         // Get the block record
590
591         $block = Profile_block::get($this->id, $other->id);
592
593         if (!$block) {
594             return false;
595         }
596
597         $result = $block->delete();
598
599         if (!$result) {
600             common_log_db_error($block, 'DELETE', __FILE__);
601             return false;
602         }
603
604         return true;
605     }
606
607     function isMember($group)
608     {
609         $profile = $this->getProfile();
610         return $profile->isMember($group);
611     }
612
613     function isAdmin($group)
614     {
615         $profile = $this->getProfile();
616         return $profile->isAdmin($group);
617     }
618
619     function getGroups($offset=0, $limit=null)
620     {
621         $profile = $this->getProfile();
622         return $profile->getGroups($offset, $limit);
623     }
624
625     function getSubscriptions($offset=0, $limit=null)
626     {
627         $profile = $this->getProfile();
628         assert(!empty($profile));
629         return $profile->getSubscriptions($offset, $limit);
630     }
631
632     function getSubscribers($offset=0, $limit=null)
633     {
634         $profile = $this->getProfile();
635         assert(!empty($profile));
636         return $profile->getSubscribers($offset, $limit);
637     }
638
639     function getTaggedSubscribers($tag, $offset=0, $limit=null)
640     {
641         $qry =
642           'SELECT profile.* ' .
643           'FROM profile JOIN subscription ' .
644           'ON profile.id = subscription.subscriber ' .
645           'JOIN profile_tag ON (profile_tag.tagged = subscription.subscriber ' .
646           'AND profile_tag.tagger = subscription.subscribed) ' .
647           'WHERE subscription.subscribed = %d ' .
648           "AND profile_tag.tag = '%s' " .
649           'AND subscription.subscribed != subscription.subscriber ' .
650           'ORDER BY subscription.created DESC ';
651
652         if ($offset) {
653             $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
654         }
655
656         $profile = new Profile();
657
658         $cnt = $profile->query(sprintf($qry, $this->id, $tag));
659
660         return $profile;
661     }
662
663     function getTaggedSubscriptions($tag, $offset=0, $limit=null)
664     {
665         $qry =
666           'SELECT profile.* ' .
667           'FROM profile JOIN subscription ' .
668           'ON profile.id = subscription.subscribed ' .
669           'JOIN profile_tag on (profile_tag.tagged = subscription.subscribed ' .
670           'AND profile_tag.tagger = subscription.subscriber) ' .
671           'WHERE subscription.subscriber = %d ' .
672           "AND profile_tag.tag = '%s' " .
673           'AND subscription.subscribed != subscription.subscriber ' .
674           'ORDER BY subscription.created DESC ';
675
676         $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
677
678         $profile = new Profile();
679
680         $profile->query(sprintf($qry, $this->id, $tag));
681
682         return $profile;
683     }
684
685     function getDesign()
686     {
687         return Design::staticGet('id', $this->design_id);
688     }
689
690     function hasRight($right)
691     {
692         $profile = $this->getProfile();
693         return $profile->hasRight($right);
694     }
695
696     function delete()
697     {
698         $profile = $this->getProfile();
699         if ($profile) {
700             $profile->delete();
701         }
702
703         $related = array('Fave',
704                          'Confirm_address',
705                          'Remember_me',
706                          'Foreign_link',
707                          'Invitation',
708                          );
709         Event::handle('UserDeleteRelated', array($this, &$related));
710
711         foreach ($related as $cls) {
712             $inst = new $cls();
713             $inst->user_id = $this->id;
714             $inst->delete();
715         }
716
717         $this->_deleteTags();
718         $this->_deleteBlocks();
719
720         parent::delete();
721     }
722
723     function _deleteTags()
724     {
725         $tag = new Profile_tag();
726         $tag->tagger = $this->id;
727         $tag->delete();
728     }
729
730     function _deleteBlocks()
731     {
732         $block = new Profile_block();
733         $block->blocker = $this->id;
734         $block->delete();
735         // XXX delete group block? Reset blocker?
736     }
737
738     function hasRole($name)
739     {
740         $profile = $this->getProfile();
741         return $profile->hasRole($name);
742     }
743
744     function grantRole($name)
745     {
746         $profile = $this->getProfile();
747         return $profile->grantRole($name);
748     }
749
750     function revokeRole($name)
751     {
752         $profile = $this->getProfile();
753         return $profile->revokeRole($name);
754     }
755
756     function isSandboxed()
757     {
758         $profile = $this->getProfile();
759         return $profile->isSandboxed();
760     }
761
762     function isSilenced()
763     {
764         $profile = $this->getProfile();
765         return $profile->isSilenced();
766     }
767
768     function repeatedByMe($offset=0, $limit=20, $since_id=null, $max_id=null)
769     {
770         $ids = Notice::stream(array($this, '_repeatedByMeDirect'),
771                               array(),
772                               'user:repeated_by_me:'.$this->id,
773                               $offset, $limit, $since_id, $max_id, null);
774
775         return Notice::getStreamByIds($ids);
776     }
777
778     function _repeatedByMeDirect($offset, $limit, $since_id, $max_id)
779     {
780         $notice = new Notice();
781
782         $notice->selectAdd(); // clears it
783         $notice->selectAdd('id');
784
785         $notice->profile_id = $this->id;
786         $notice->whereAdd('repeat_of IS NOT NULL');
787
788         $notice->orderBy('id DESC');
789
790         if (!is_null($offset)) {
791             $notice->limit($offset, $limit);
792         }
793
794         if ($since_id != 0) {
795             $notice->whereAdd('id > ' . $since_id);
796         }
797
798         if ($max_id != 0) {
799             $notice->whereAdd('id <= ' . $max_id);
800         }
801
802         $ids = array();
803
804         if ($notice->find()) {
805             while ($notice->fetch()) {
806                 $ids[] = $notice->id;
807             }
808         }
809
810         $notice->free();
811         $notice = NULL;
812
813         return $ids;
814     }
815
816     function repeatsOfMe($offset=0, $limit=20, $since_id=null, $max_id=null)
817     {
818         $ids = Notice::stream(array($this, '_repeatsOfMeDirect'),
819                               array(),
820                               'user:repeats_of_me:'.$this->id,
821                               $offset, $limit, $since_id, $max_id);
822
823         return Notice::getStreamByIds($ids);
824     }
825
826     function _repeatsOfMeDirect($offset, $limit, $since_id, $max_id)
827     {
828         $qry =
829           'SELECT DISTINCT original.id AS id ' .
830           'FROM notice original JOIN notice rept ON original.id = rept.repeat_of ' .
831           'WHERE original.profile_id = ' . $this->id . ' ';
832
833         if ($since_id != 0) {
834             $qry .= 'AND original.id > ' . $since_id . ' ';
835         }
836
837         if ($max_id != 0) {
838             $qry .= 'AND original.id <= ' . $max_id . ' ';
839         }
840
841         // NOTE: we sort by fave time, not by notice time!
842
843         $qry .= 'ORDER BY original.id DESC ';
844
845         if (!is_null($offset)) {
846             $qry .= "LIMIT $limit OFFSET $offset";
847         }
848
849         $ids = array();
850
851         $notice = new Notice();
852
853         $notice->query($qry);
854
855         while ($notice->fetch()) {
856             $ids[] = $notice->id;
857         }
858
859         $notice->free();
860         $notice = NULL;
861
862         return $ids;
863     }
864
865     function repeatedToMe($offset=0, $limit=20, $since_id=null, $max_id=null)
866     {
867         throw new Exception("Not implemented since inbox change.");
868     }
869
870     function shareLocation()
871     {
872         $cfg = common_config('location', 'share');
873
874         if ($cfg == 'always') {
875             return true;
876         } else if ($cfg == 'never') {
877             return false;
878         } else { // user
879             $share = true;
880
881             $prefs = User_location_prefs::staticGet('user_id', $this->id);
882
883             if (empty($prefs)) {
884                 $share = common_config('location', 'sharedefault');
885             } else {
886                 $share = $prefs->share_location;
887                 $prefs->free();
888             }
889
890             return $share;
891         }
892     }
893
894     static function siteOwner()
895     {
896         $owner = self::cacheGet('user:site_owner');
897
898         if ($owner === false) { // cache miss
899
900             $pr = new Profile_role();
901
902             $pr->role = Profile_role::OWNER;
903
904             $pr->orderBy('created');
905
906             $pr->limit(1);
907
908             if ($pr->find(true)) {
909                 $owner = User::staticGet('id', $pr->profile_id);
910             } else {
911                 $owner = null;
912             }
913
914             self::cacheSet('user:site_owner', $owner);
915         }
916
917         return $owner;
918     }
919 }