]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/User.php
Merge commit 'origin/testing' 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     function getCurrentNotice($dt=null)
136     {
137         $profile = $this->getProfile();
138         if (!$profile) {
139             return null;
140         }
141         return $profile->getCurrentNotice($dt);
142     }
143
144     function getCarrier()
145     {
146         return Sms_carrier::staticGet('id', $this->carrier);
147     }
148
149     function subscribeTo($other)
150     {
151         $sub = new Subscription();
152         $sub->subscriber = $this->id;
153         $sub->subscribed = $other->id;
154
155         $sub->created = common_sql_now(); // current time
156
157         if (!$sub->insert()) {
158             return false;
159         }
160
161         return true;
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         $user->created = common_sql_now();
259
260         if (Event::handle('StartUserRegister', array(&$user, &$profile))) {
261
262             $profile->query('BEGIN');
263
264             $id = $profile->insert();
265
266             if (empty($id)) {
267                 common_log_db_error($profile, 'INSERT', __FILE__);
268                 return false;
269             }
270
271             $user->id = $id;
272             $user->uri = common_user_uri($user);
273             if (!empty($password)) { // may not have a password for OpenID users
274                 $user->password = common_munge_password($password, $id);
275             }
276
277             $result = $user->insert();
278
279             if (!$result) {
280                 common_log_db_error($user, 'INSERT', __FILE__);
281                 return false;
282             }
283
284             // Everyone gets an inbox
285
286             $inbox = new Inbox();
287
288             $inbox->user_id = $user->id;
289             $inbox->notice_ids = '';
290
291             $result = $inbox->insert();
292
293             if (!$result) {
294                 common_log_db_error($inbox, 'INSERT', __FILE__);
295                 return false;
296             }
297
298             // Everyone is subscribed to themself
299
300             $subscription = new Subscription();
301             $subscription->subscriber = $user->id;
302             $subscription->subscribed = $user->id;
303             $subscription->created = $user->created;
304
305             $result = $subscription->insert();
306
307             if (!$result) {
308                 common_log_db_error($subscription, 'INSERT', __FILE__);
309                 return false;
310             }
311
312             if (!empty($email) && !$user->email) {
313
314                 $confirm = new Confirm_address();
315                 $confirm->code = common_confirmation_code(128);
316                 $confirm->user_id = $user->id;
317                 $confirm->address = $email;
318                 $confirm->address_type = 'email';
319
320                 $result = $confirm->insert();
321
322                 if (!$result) {
323                     common_log_db_error($confirm, 'INSERT', __FILE__);
324                     return false;
325                 }
326             }
327
328             if (!empty($code) && $user->email) {
329                 $user->emailChanged();
330             }
331
332             // Default system subscription
333
334             $defnick = common_config('newuser', 'default');
335
336             if (!empty($defnick)) {
337                 $defuser = User::staticGet('nickname', $defnick);
338                 if (empty($defuser)) {
339                     common_log(LOG_WARNING, sprintf("Default user %s does not exist.", $defnick),
340                                __FILE__);
341                 } else {
342                     $defsub = new Subscription();
343                     $defsub->subscriber = $user->id;
344                     $defsub->subscribed = $defuser->id;
345                     $defsub->created = $user->created;
346
347                     $result = $defsub->insert();
348
349                     if (!$result) {
350                         common_log_db_error($defsub, 'INSERT', __FILE__);
351                         return false;
352                     }
353                 }
354             }
355
356             $profile->query('COMMIT');
357
358             if (!empty($email) && !$user->email) {
359                 mail_confirm_address($user, $confirm->code, $profile->nickname, $email);
360             }
361
362             // Welcome message
363
364             $welcome = common_config('newuser', 'welcome');
365
366             if (!empty($welcome)) {
367                 $welcomeuser = User::staticGet('nickname', $welcome);
368                 if (empty($welcomeuser)) {
369                     common_log(LOG_WARNING, sprintf("Welcome user %s does not exist.", $defnick),
370                                __FILE__);
371                 } else {
372                     $notice = Notice::saveNew($welcomeuser->id,
373                                               sprintf(_('Welcome to %1$s, @%2$s!'),
374                                                       common_config('site', 'name'),
375                                                       $user->nickname),
376                                               'system');
377
378                 }
379             }
380
381             Event::handle('EndUserRegister', array(&$profile, &$user));
382         }
383
384         return $user;
385     }
386
387     // Things we do when the email changes
388
389     function emailChanged()
390     {
391
392         $invites = new Invitation();
393         $invites->address = $this->email;
394         $invites->address_type = 'email';
395
396         if ($invites->find()) {
397             while ($invites->fetch()) {
398                 $other = User::staticGet($invites->user_id);
399                 subs_subscribe_to($other, $this);
400             }
401         }
402     }
403
404     function hasFave($notice)
405     {
406         $cache = common_memcache();
407
408         // XXX: Kind of a hack.
409
410         if ($cache) {
411             // This is the stream of favorite notices, in rev chron
412             // order. This forces it into cache.
413
414             $ids = Fave::stream($this->id, 0, NOTICE_CACHE_WINDOW);
415
416             // If it's in the list, then it's a fave
417
418             if (in_array($notice->id, $ids)) {
419                 return true;
420             }
421
422             // If we're not past the end of the cache window,
423             // then the cache has all available faves, so this one
424             // is not a fave.
425
426             if (count($ids) < NOTICE_CACHE_WINDOW) {
427                 return false;
428             }
429
430             // Otherwise, cache doesn't have all faves;
431             // fall through to the default
432         }
433
434         $fave = Fave::pkeyGet(array('user_id' => $this->id,
435                                     'notice_id' => $notice->id));
436         return ((is_null($fave)) ? false : true);
437     }
438
439     function mutuallySubscribed($other)
440     {
441         return $this->isSubscribed($other) &&
442           $other->isSubscribed($this);
443     }
444
445     function mutuallySubscribedUsers()
446     {
447         // 3-way join; probably should get cached
448         $UT = common_config('db','type')=='pgsql'?'"user"':'user';
449         $qry = "SELECT $UT.* " .
450           "FROM subscription sub1 JOIN $UT ON sub1.subscribed = $UT.id " .
451           "JOIN subscription sub2 ON $UT.id = sub2.subscriber " .
452           'WHERE sub1.subscriber = %d and sub2.subscribed = %d ' .
453           "ORDER BY $UT.nickname";
454         $user = new User();
455         $user->query(sprintf($qry, $this->id, $this->id));
456
457         return $user;
458     }
459
460     function getReplies($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0)
461     {
462         $ids = Reply::stream($this->id, $offset, $limit, $since_id, $before_id);
463         return Notice::getStreamByIds($ids);
464     }
465
466     function getTaggedNotices($tag, $offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0) {
467         $profile = $this->getProfile();
468         if (!$profile) {
469             return null;
470         } else {
471             return $profile->getTaggedNotices($tag, $offset, $limit, $since_id, $before_id);
472         }
473     }
474
475     function getNotices($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0)
476     {
477         $profile = $this->getProfile();
478         if (!$profile) {
479             return null;
480         } else {
481             return $profile->getNotices($offset, $limit, $since_id, $before_id);
482         }
483     }
484
485     function favoriteNotices($offset=0, $limit=NOTICES_PER_PAGE, $own=false)
486     {
487         $ids = Fave::stream($this->id, $offset, $limit, $own);
488         return Notice::getStreamByIds($ids);
489     }
490
491     function noticesWithFriends($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, false);
494     }
495
496     function noticeInbox($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, true);
499     }
500
501     function friendsTimeline($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, false);
504     }
505
506     function ownFriendsTimeline($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, true);
509     }
510
511     function blowFavesCache()
512     {
513         $cache = common_memcache();
514         if ($cache) {
515             // Faves don't happen chronologically, so we need to blow
516             // ;last cache, too
517             $cache->delete(common_cache_key('fave:ids_by_user:'.$this->id));
518             $cache->delete(common_cache_key('fave:ids_by_user:'.$this->id.';last'));
519             $cache->delete(common_cache_key('fave:ids_by_user_own:'.$this->id));
520             $cache->delete(common_cache_key('fave:ids_by_user_own:'.$this->id.';last'));
521         }
522         $profile = $this->getProfile();
523         $profile->blowFaveCount();
524     }
525
526     function getSelfTags()
527     {
528         return Profile_tag::getTags($this->id, $this->id);
529     }
530
531     function setSelfTags($newtags)
532     {
533         return Profile_tag::setTags($this->id, $this->id, $newtags);
534     }
535
536     function block($other)
537     {
538         // Add a new block record
539
540         // no blocking (and thus unsubbing from) yourself
541
542         if ($this->id == $other->id) {
543             common_log(LOG_WARNING,
544                 sprintf(
545                     "Profile ID %d (%s) tried to block his or herself.",
546                     $profile->id,
547                     $profile->nickname
548                 )
549             );
550             return false;
551         }
552
553         $block = new Profile_block();
554
555         // Begin a transaction
556
557         $block->query('BEGIN');
558
559         $block->blocker = $this->id;
560         $block->blocked = $other->id;
561
562         $result = $block->insert();
563
564         if (!$result) {
565             common_log_db_error($block, 'INSERT', __FILE__);
566             return false;
567         }
568
569         // Cancel their subscription, if it exists
570
571         $otherUser = User::staticGet('id', $other->id);
572
573         if (!empty($otherUser)) {
574             subs_unsubscribe_to($otherUser, $this->getProfile());
575         }
576
577         $block->query('COMMIT');
578
579         return true;
580     }
581
582     function unblock($other)
583     {
584         // Get the block record
585
586         $block = Profile_block::get($this->id, $other->id);
587
588         if (!$block) {
589             return false;
590         }
591
592         $result = $block->delete();
593
594         if (!$result) {
595             common_log_db_error($block, 'DELETE', __FILE__);
596             return false;
597         }
598
599         return true;
600     }
601
602     function isMember($group)
603     {
604         $profile = $this->getProfile();
605         return $profile->isMember($group);
606     }
607
608     function isAdmin($group)
609     {
610         $profile = $this->getProfile();
611         return $profile->isAdmin($group);
612     }
613
614     function getGroups($offset=0, $limit=null)
615     {
616         $profile = $this->getProfile();
617         return $profile->getGroups($offset, $limit);
618     }
619
620     function getSubscriptions($offset=0, $limit=null)
621     {
622         $profile = $this->getProfile();
623         assert(!empty($profile));
624         return $profile->getSubscriptions($offset, $limit);
625     }
626
627     function getSubscribers($offset=0, $limit=null)
628     {
629         $profile = $this->getProfile();
630         assert(!empty($profile));
631         return $profile->getSubscribers($offset, $limit);
632     }
633
634     function getTaggedSubscribers($tag, $offset=0, $limit=null)
635     {
636         $qry =
637           'SELECT profile.* ' .
638           'FROM profile JOIN subscription ' .
639           'ON profile.id = subscription.subscriber ' .
640           'JOIN profile_tag ON (profile_tag.tagged = subscription.subscriber ' .
641           'AND profile_tag.tagger = subscription.subscribed) ' .
642           'WHERE subscription.subscribed = %d ' .
643           "AND profile_tag.tag = '%s' " .
644           'AND subscription.subscribed != subscription.subscriber ' .
645           'ORDER BY subscription.created DESC ';
646
647         if ($offset) {
648             $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
649         }
650
651         $profile = new Profile();
652
653         $cnt = $profile->query(sprintf($qry, $this->id, $tag));
654
655         return $profile;
656     }
657
658     function getTaggedSubscriptions($tag, $offset=0, $limit=null)
659     {
660         $qry =
661           'SELECT profile.* ' .
662           'FROM profile JOIN subscription ' .
663           'ON profile.id = subscription.subscribed ' .
664           'JOIN profile_tag on (profile_tag.tagged = subscription.subscribed ' .
665           'AND profile_tag.tagger = subscription.subscriber) ' .
666           'WHERE subscription.subscriber = %d ' .
667           "AND profile_tag.tag = '%s' " .
668           'AND subscription.subscribed != subscription.subscriber ' .
669           'ORDER BY subscription.created DESC ';
670
671         $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
672
673         $profile = new Profile();
674
675         $profile->query(sprintf($qry, $this->id, $tag));
676
677         return $profile;
678     }
679
680     function getDesign()
681     {
682         return Design::staticGet('id', $this->design_id);
683     }
684
685     function hasRight($right)
686     {
687         $profile = $this->getProfile();
688         return $profile->hasRight($right);
689     }
690
691     function delete()
692     {
693         $profile = $this->getProfile();
694         if ($profile) {
695             $profile->delete();
696         }
697
698         $related = array('Fave',
699                          'Confirm_address',
700                          'Remember_me',
701                          'Foreign_link',
702                          'Invitation',
703                          );
704         Event::handle('UserDeleteRelated', array($this, &$related));
705
706         foreach ($related as $cls) {
707             $inst = new $cls();
708             $inst->user_id = $this->id;
709             $inst->delete();
710         }
711
712         $this->_deleteTags();
713         $this->_deleteBlocks();
714
715         parent::delete();
716     }
717
718     function _deleteTags()
719     {
720         $tag = new Profile_tag();
721         $tag->tagger = $this->id;
722         $tag->delete();
723     }
724
725     function _deleteBlocks()
726     {
727         $block = new Profile_block();
728         $block->blocker = $this->id;
729         $block->delete();
730         // XXX delete group block? Reset blocker?
731     }
732
733     function hasRole($name)
734     {
735         $profile = $this->getProfile();
736         return $profile->hasRole($name);
737     }
738
739     function grantRole($name)
740     {
741         $profile = $this->getProfile();
742         return $profile->grantRole($name);
743     }
744
745     function revokeRole($name)
746     {
747         $profile = $this->getProfile();
748         return $profile->revokeRole($name);
749     }
750
751     function isSandboxed()
752     {
753         $profile = $this->getProfile();
754         return $profile->isSandboxed();
755     }
756
757     function isSilenced()
758     {
759         $profile = $this->getProfile();
760         return $profile->isSilenced();
761     }
762
763     function repeatedByMe($offset=0, $limit=20, $since_id=null, $max_id=null)
764     {
765         $ids = Notice::stream(array($this, '_repeatedByMeDirect'),
766                               array(),
767                               'user:repeated_by_me:'.$this->id,
768                               $offset, $limit, $since_id, $max_id, null);
769
770         return Notice::getStreamByIds($ids);
771     }
772
773     function _repeatedByMeDirect($offset, $limit, $since_id, $max_id)
774     {
775         $notice = new Notice();
776
777         $notice->selectAdd(); // clears it
778         $notice->selectAdd('id');
779
780         $notice->profile_id = $this->id;
781         $notice->whereAdd('repeat_of IS NOT NULL');
782
783         $notice->orderBy('id DESC');
784
785         if (!is_null($offset)) {
786             $notice->limit($offset, $limit);
787         }
788
789         if ($since_id != 0) {
790             $notice->whereAdd('id > ' . $since_id);
791         }
792
793         if ($max_id != 0) {
794             $notice->whereAdd('id <= ' . $max_id);
795         }
796
797         $ids = array();
798
799         if ($notice->find()) {
800             while ($notice->fetch()) {
801                 $ids[] = $notice->id;
802             }
803         }
804
805         $notice->free();
806         $notice = NULL;
807
808         return $ids;
809     }
810
811     function repeatsOfMe($offset=0, $limit=20, $since_id=null, $max_id=null)
812     {
813         $ids = Notice::stream(array($this, '_repeatsOfMeDirect'),
814                               array(),
815                               'user:repeats_of_me:'.$this->id,
816                               $offset, $limit, $since_id, $max_id);
817
818         return Notice::getStreamByIds($ids);
819     }
820
821     function _repeatsOfMeDirect($offset, $limit, $since_id, $max_id)
822     {
823         $qry =
824           'SELECT DISTINCT original.id AS id ' .
825           'FROM notice original JOIN notice rept ON original.id = rept.repeat_of ' .
826           'WHERE original.profile_id = ' . $this->id . ' ';
827
828         if ($since_id != 0) {
829             $qry .= 'AND original.id > ' . $since_id . ' ';
830         }
831
832         if ($max_id != 0) {
833             $qry .= 'AND original.id <= ' . $max_id . ' ';
834         }
835
836         // NOTE: we sort by fave time, not by notice time!
837
838         $qry .= 'ORDER BY original.id DESC ';
839
840         if (!is_null($offset)) {
841             $qry .= "LIMIT $limit OFFSET $offset";
842         }
843
844         $ids = array();
845
846         $notice = new Notice();
847
848         $notice->query($qry);
849
850         while ($notice->fetch()) {
851             $ids[] = $notice->id;
852         }
853
854         $notice->free();
855         $notice = NULL;
856
857         return $ids;
858     }
859
860     function repeatedToMe($offset=0, $limit=20, $since_id=null, $max_id=null)
861     {
862         throw new Exception("Not implemented since inbox change.");
863     }
864
865     function shareLocation()
866     {
867         $cfg = common_config('location', 'share');
868
869         if ($cfg == 'always') {
870             return true;
871         } else if ($cfg == 'never') {
872             return false;
873         } else { // user
874             $share = true;
875
876             $prefs = User_location_prefs::staticGet('user_id', $this->id);
877
878             if (empty($prefs)) {
879                 $share = common_config('location', 'sharedefault');
880             } else {
881                 $share = $prefs->share_location;
882                 $prefs->free();
883             }
884
885             return $share;
886         }
887     }
888
889     static function siteOwner()
890     {
891         $owner = self::cacheGet('user:site_owner');
892
893         if ($owner === false) { // cache miss
894
895             $pr = new Profile_role();
896
897             $pr->role = Profile_role::OWNER;
898
899             $pr->orderBy('created');
900
901             $pr->limit(1);
902
903             if ($pr->find(true)) {
904                 $owner = User::staticGet('id', $pr->profile_id);
905             } else {
906                 $owner = null;
907             }
908
909             self::cacheSet('user:site_owner', $owner);
910         }
911
912         return $owner;
913     }
914 }