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