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