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