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