]> 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             return false;
205         }
206         $profile->profileurl = common_profile_url($nickname);
207
208         if (!empty($fullname)) {
209             $profile->fullname = $fullname;
210         }
211         if (!empty($homepage)) {
212             $profile->homepage = $homepage;
213         }
214         if (!empty($bio)) {
215             $profile->bio = $bio;
216         }
217         if (!empty($location)) {
218             $profile->location = $location;
219
220             $loc = Location::fromName($location);
221
222             if (!empty($loc)) {
223                 $profile->lat         = $loc->lat;
224                 $profile->lon         = $loc->lon;
225                 $profile->location_id = $loc->location_id;
226                 $profile->location_ns = $loc->location_ns;
227             }
228         }
229
230         $profile->created = common_sql_now();
231
232         $user = new User();
233
234         $user->nickname = $nickname;
235
236         // Users who respond to invite email have proven their ownership of that address
237
238         if (!empty($code)) {
239             $invite = Invitation::staticGet($code);
240             if ($invite && $invite->address && $invite->address_type == 'email' && $invite->address == $email) {
241                 $user->email = $invite->address;
242             }
243         }
244
245         if(isset($email_confirmed) && $email_confirmed) {
246             $user->email = $email;
247         }
248
249         // This flag is ignored but still set to 1
250
251         $user->inboxed = 1;
252
253         $user->created = common_sql_now();
254
255         if (Event::handle('StartUserRegister', array(&$user, &$profile))) {
256
257             $profile->query('BEGIN');
258
259             $id = $profile->insert();
260
261             if (empty($id)) {
262                 common_log_db_error($profile, 'INSERT', __FILE__);
263                 return false;
264             }
265
266             $user->id = $id;
267             $user->uri = common_user_uri($user);
268             if (!empty($password)) { // may not have a password for OpenID users
269                 $user->password = common_munge_password($password, $id);
270             }
271
272             $result = $user->insert();
273
274             if (!$result) {
275                 common_log_db_error($user, 'INSERT', __FILE__);
276                 return false;
277             }
278
279             // Everyone gets an inbox
280
281             $inbox = new Inbox();
282
283             $inbox->user_id = $user->id;
284             $inbox->notice_ids = '';
285
286             $result = $inbox->insert();
287
288             if (!$result) {
289                 common_log_db_error($inbox, 'INSERT', __FILE__);
290                 return false;
291             }
292
293             // Everyone is subscribed to themself
294
295             $subscription = new Subscription();
296             $subscription->subscriber = $user->id;
297             $subscription->subscribed = $user->id;
298             $subscription->created = $user->created;
299
300             $result = $subscription->insert();
301
302             if (!$result) {
303                 common_log_db_error($subscription, 'INSERT', __FILE__);
304                 return false;
305             }
306
307             if (!empty($email) && !$user->email) {
308
309                 $confirm = new Confirm_address();
310                 $confirm->code = common_confirmation_code(128);
311                 $confirm->user_id = $user->id;
312                 $confirm->address = $email;
313                 $confirm->address_type = 'email';
314
315                 $result = $confirm->insert();
316
317                 if (!$result) {
318                     common_log_db_error($confirm, 'INSERT', __FILE__);
319                     return false;
320                 }
321             }
322
323             if (!empty($code) && $user->email) {
324                 $user->emailChanged();
325             }
326
327             // Default system subscription
328
329             $defnick = common_config('newuser', 'default');
330
331             if (!empty($defnick)) {
332                 $defuser = User::staticGet('nickname', $defnick);
333                 if (empty($defuser)) {
334                     common_log(LOG_WARNING, sprintf("Default user %s does not exist.", $defnick),
335                                __FILE__);
336                 } else {
337                     $defsub = new Subscription();
338                     $defsub->subscriber = $user->id;
339                     $defsub->subscribed = $defuser->id;
340                     $defsub->created = $user->created;
341
342                     $result = $defsub->insert();
343
344                     if (!$result) {
345                         common_log_db_error($defsub, 'INSERT', __FILE__);
346                         return false;
347                     }
348                 }
349             }
350
351             $profile->query('COMMIT');
352
353             if (!empty($email) && !$user->email) {
354                 mail_confirm_address($user, $confirm->code, $profile->nickname, $email);
355             }
356
357             // Welcome message
358
359             $welcome = common_config('newuser', 'welcome');
360
361             if (!empty($welcome)) {
362                 $welcomeuser = User::staticGet('nickname', $welcome);
363                 if (empty($welcomeuser)) {
364                     common_log(LOG_WARNING, sprintf("Welcome user %s does not exist.", $defnick),
365                                __FILE__);
366                 } else {
367                     $notice = Notice::saveNew($welcomeuser->id,
368                                               sprintf(_('Welcome to %1$s, @%2$s!'),
369                                                       common_config('site', 'name'),
370                                                       $user->nickname),
371                                               'system');
372
373                 }
374             }
375
376             Event::handle('EndUserRegister', array(&$profile, &$user));
377         }
378
379         return $user;
380     }
381
382     // Things we do when the email changes
383
384     function emailChanged()
385     {
386
387         $invites = new Invitation();
388         $invites->address = $this->email;
389         $invites->address_type = 'email';
390
391         if ($invites->find()) {
392             while ($invites->fetch()) {
393                 $other = User::staticGet($invites->user_id);
394                 subs_subscribe_to($other, $this);
395             }
396         }
397     }
398
399     function hasFave($notice)
400     {
401         $cache = common_memcache();
402
403         // XXX: Kind of a hack.
404
405         if ($cache) {
406             // This is the stream of favorite notices, in rev chron
407             // order. This forces it into cache.
408
409             $ids = Fave::stream($this->id, 0, NOTICE_CACHE_WINDOW);
410
411             // If it's in the list, then it's a fave
412
413             if (in_array($notice->id, $ids)) {
414                 return true;
415             }
416
417             // If we're not past the end of the cache window,
418             // then the cache has all available faves, so this one
419             // is not a fave.
420
421             if (count($ids) < NOTICE_CACHE_WINDOW) {
422                 return false;
423             }
424
425             // Otherwise, cache doesn't have all faves;
426             // fall through to the default
427         }
428
429         $fave = Fave::pkeyGet(array('user_id' => $this->id,
430                                     'notice_id' => $notice->id));
431         return ((is_null($fave)) ? false : true);
432     }
433
434     function mutuallySubscribed($other)
435     {
436         return $this->isSubscribed($other) &&
437           $other->isSubscribed($this);
438     }
439
440     function mutuallySubscribedUsers()
441     {
442         // 3-way join; probably should get cached
443         $UT = common_config('db','type')=='pgsql'?'"user"':'user';
444         $qry = "SELECT $UT.* " .
445           "FROM subscription sub1 JOIN $UT ON sub1.subscribed = $UT.id " .
446           "JOIN subscription sub2 ON $UT.id = sub2.subscriber " .
447           'WHERE sub1.subscriber = %d and sub2.subscribed = %d ' .
448           "ORDER BY $UT.nickname";
449         $user = new User();
450         $user->query(sprintf($qry, $this->id, $this->id));
451
452         return $user;
453     }
454
455     function getReplies($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0)
456     {
457         $ids = Reply::stream($this->id, $offset, $limit, $since_id, $before_id);
458         return Notice::getStreamByIds($ids);
459     }
460
461     function getTaggedNotices($tag, $offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0) {
462         $profile = $this->getProfile();
463         if (!$profile) {
464             return null;
465         } else {
466             return $profile->getTaggedNotices($tag, $offset, $limit, $since_id, $before_id);
467         }
468     }
469
470     function getNotices($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0)
471     {
472         $profile = $this->getProfile();
473         if (!$profile) {
474             return null;
475         } else {
476             return $profile->getNotices($offset, $limit, $since_id, $before_id);
477         }
478     }
479
480     function favoriteNotices($offset=0, $limit=NOTICES_PER_PAGE, $own=false)
481     {
482         $ids = Fave::stream($this->id, $offset, $limit, $own);
483         return Notice::getStreamByIds($ids);
484     }
485
486     function noticesWithFriends($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0)
487     {
488         return Inbox::streamNotices($this->id, $offset, $limit, $since_id, $before_id, false);
489     }
490
491     function noticeInbox($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0)
492     {
493         return Inbox::streamNotices($this->id, $offset, $limit, $since_id, $before_id, true);
494     }
495
496     function friendsTimeline($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0)
497     {
498         return Inbox::streamNotices($this->id, $offset, $limit, $since_id, $before_id, false);
499     }
500
501     function ownFriendsTimeline($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0)
502     {
503         return Inbox::streamNotices($this->id, $offset, $limit, $since_id, $before_id, true);
504     }
505
506     function blowFavesCache()
507     {
508         $cache = common_memcache();
509         if ($cache) {
510             // Faves don't happen chronologically, so we need to blow
511             // ;last cache, too
512             $cache->delete(common_cache_key('fave:ids_by_user:'.$this->id));
513             $cache->delete(common_cache_key('fave:ids_by_user:'.$this->id.';last'));
514             $cache->delete(common_cache_key('fave:ids_by_user_own:'.$this->id));
515             $cache->delete(common_cache_key('fave:ids_by_user_own:'.$this->id.';last'));
516         }
517         $profile = $this->getProfile();
518         $profile->blowFaveCount();
519     }
520
521     function getSelfTags()
522     {
523         return Profile_tag::getTags($this->id, $this->id);
524     }
525
526     function setSelfTags($newtags)
527     {
528         return Profile_tag::setTags($this->id, $this->id, $newtags);
529     }
530
531     function block($other)
532     {
533         // Add a new block record
534
535         // no blocking (and thus unsubbing from) yourself
536
537         if ($this->id == $other->id) {
538             common_log(LOG_WARNING,
539                 sprintf(
540                     "Profile ID %d (%s) tried to block his or herself.",
541                     $profile->id,
542                     $profile->nickname
543                 )
544             );
545             return false;
546         }
547
548         $block = new Profile_block();
549
550         // Begin a transaction
551
552         $block->query('BEGIN');
553
554         $block->blocker = $this->id;
555         $block->blocked = $other->id;
556
557         $result = $block->insert();
558
559         if (!$result) {
560             common_log_db_error($block, 'INSERT', __FILE__);
561             return false;
562         }
563
564         // Cancel their subscription, if it exists
565
566         $otherUser = User::staticGet('id', $other->id);
567
568         if (!empty($otherUser)) {
569             subs_unsubscribe_to($otherUser, $this->getProfile());
570         }
571
572         $block->query('COMMIT');
573
574         return true;
575     }
576
577     function unblock($other)
578     {
579         // Get the block record
580
581         $block = Profile_block::get($this->id, $other->id);
582
583         if (!$block) {
584             return false;
585         }
586
587         $result = $block->delete();
588
589         if (!$result) {
590             common_log_db_error($block, 'DELETE', __FILE__);
591             return false;
592         }
593
594         return true;
595     }
596
597     function isMember($group)
598     {
599         $profile = $this->getProfile();
600         return $profile->isMember($group);
601     }
602
603     function isAdmin($group)
604     {
605         $profile = $this->getProfile();
606         return $profile->isAdmin($group);
607     }
608
609     function getGroups($offset=0, $limit=null)
610     {
611         $profile = $this->getProfile();
612         return $profile->getGroups($offset, $limit);
613     }
614
615     function getSubscriptions($offset=0, $limit=null)
616     {
617         $profile = $this->getProfile();
618         assert(!empty($profile));
619         return $profile->getSubscriptions($offset, $limit);
620     }
621
622     function getSubscribers($offset=0, $limit=null)
623     {
624         $profile = $this->getProfile();
625         assert(!empty($profile));
626         return $profile->getSubscribers($offset, $limit);
627     }
628
629     function getTaggedSubscribers($tag, $offset=0, $limit=null)
630     {
631         $qry =
632           'SELECT profile.* ' .
633           'FROM profile JOIN subscription ' .
634           'ON profile.id = subscription.subscriber ' .
635           'JOIN profile_tag ON (profile_tag.tagged = subscription.subscriber ' .
636           'AND profile_tag.tagger = subscription.subscribed) ' .
637           'WHERE subscription.subscribed = %d ' .
638           "AND profile_tag.tag = '%s' " .
639           'AND subscription.subscribed != subscription.subscriber ' .
640           'ORDER BY subscription.created DESC ';
641
642         if ($offset) {
643             $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
644         }
645
646         $profile = new Profile();
647
648         $cnt = $profile->query(sprintf($qry, $this->id, $tag));
649
650         return $profile;
651     }
652
653     function getTaggedSubscriptions($tag, $offset=0, $limit=null)
654     {
655         $qry =
656           'SELECT profile.* ' .
657           'FROM profile JOIN subscription ' .
658           'ON profile.id = subscription.subscribed ' .
659           'JOIN profile_tag on (profile_tag.tagged = subscription.subscribed ' .
660           'AND profile_tag.tagger = subscription.subscriber) ' .
661           'WHERE subscription.subscriber = %d ' .
662           "AND profile_tag.tag = '%s' " .
663           'AND subscription.subscribed != subscription.subscriber ' .
664           'ORDER BY subscription.created DESC ';
665
666         $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
667
668         $profile = new Profile();
669
670         $profile->query(sprintf($qry, $this->id, $tag));
671
672         return $profile;
673     }
674
675     function getDesign()
676     {
677         return Design::staticGet('id', $this->design_id);
678     }
679
680     function hasRight($right)
681     {
682         $profile = $this->getProfile();
683         return $profile->hasRight($right);
684     }
685
686     function delete()
687     {
688         $profile = $this->getProfile();
689         if ($profile) {
690             $profile->delete();
691         }
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 }