]> 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         assert(!is_null($other));
79         // XXX: cache results of this query
80         $sub = Subscription::pkeyGet(array('subscriber' => $this->id,
81                                            'subscribed' => $other->id));
82         return (is_null($sub)) ? false : true;
83     }
84
85     // 'update' won't write key columns, so we have to do it ourselves.
86
87     function updateKeys(&$orig)
88     {
89         $parts = array();
90         foreach (array('nickname', 'email', 'incomingemail', 'sms', 'carrier', 'smsemail', 'language', 'timezone') as $k) {
91             if (strcmp($this->$k, $orig->$k) != 0) {
92                 $parts[] = $k . ' = ' . $this->_quote($this->$k);
93             }
94         }
95         if (count($parts) == 0) {
96             // No changes
97             return true;
98         }
99         $toupdate = implode(', ', $parts);
100
101         $table = common_database_tablename($this->tableName());
102         $qry = 'UPDATE ' . $table . ' SET ' . $toupdate .
103           ' WHERE id = ' . $this->id;
104         $orig->decache();
105         $result = $this->query($qry);
106         if ($result) {
107             $this->encache();
108         }
109         return $result;
110     }
111
112     static function allowed_nickname($nickname)
113     {
114         // XXX: should already be validated for size, content, etc.
115         $blacklist = common_config('nickname', 'blacklist');
116
117         //all directory and file names should be blacklisted
118         $d = dir(INSTALLDIR);
119         while (false !== ($entry = $d->read())) {
120             $blacklist[]=$entry;
121         }
122         $d->close();
123
124         //all top level names in the router should be blacklisted
125         $router = Router::get();
126         foreach(array_keys($router->m->getPaths()) as $path){
127             if(preg_match('/^\/(.*?)[\/\?]/',$path,$matches)){
128                 $blacklist[]=$matches[1];
129             }
130         }
131         return !in_array($nickname, $blacklist);
132     }
133
134     function getCurrentNotice($dt=null)
135     {
136         $profile = $this->getProfile();
137         if (!$profile) {
138             return null;
139         }
140         return $profile->getCurrentNotice($dt);
141     }
142
143     function getCarrier()
144     {
145         return Sms_carrier::staticGet('id', $this->carrier);
146     }
147
148     function subscribeTo($other)
149     {
150         $sub = new Subscription();
151         $sub->subscriber = $this->id;
152         $sub->subscribed = $other->id;
153
154         $sub->created = common_sql_now(); // current time
155
156         if (!$sub->insert()) {
157             return false;
158         }
159
160         return true;
161     }
162
163     function hasBlocked($other)
164     {
165
166         $block = Profile_block::get($this->id, $other->id);
167
168         if (is_null($block)) {
169             $result = false;
170         } else {
171             $result = true;
172             $block->free();
173         }
174
175         return $result;
176     }
177
178     /**
179      * Register a new user account and profile and set up default subscriptions.
180      * If a new-user welcome message is configured, this will be sent.
181      *
182      * @param array $fields associative array of optional properties
183      *              string 'bio'
184      *              string 'email'
185      *              bool 'email_confirmed' pass true to mark email as pre-confirmed
186      *              string 'fullname'
187      *              string 'homepage'
188      *              string 'location' informal string description of geolocation
189      *              float 'lat' decimal latitude for geolocation
190      *              float 'lon' decimal longitude for geolocation
191      *              int 'location_id' geoname identifier
192      *              int 'location_ns' geoname namespace to interpret location_id
193      *              string 'nickname' REQUIRED
194      *              string 'password' (may be missing for eg OpenID registrations)
195      *              string 'code' invite code
196      *              ?string 'uri' permalink to notice; defaults to local notice URL
197      * @return mixed User object or false on failure
198      */
199     static function register($fields) {
200
201         // MAGICALLY put fields into current scope
202
203         extract($fields);
204
205         $profile = new Profile();
206
207         if(!empty($email))
208         {
209             $email = common_canonical_email($email);
210         }
211
212         $nickname = common_canonical_nickname($nickname);
213         $profile->nickname = $nickname;
214         if(! User::allowed_nickname($nickname)){
215             common_log(LOG_WARNING, sprintf("Attempted to register a nickname that is not allowed: %s", $profile->nickname),
216                        __FILE__);
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, $since=null)
468     {
469         $ids = Reply::stream($this->id, $offset, $limit, $since_id, $before_id, $since);
470         return Notice::getStreamByIds($ids);
471     }
472
473     function getTaggedNotices($tag, $offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0, $since=null) {
474         $profile = $this->getProfile();
475         if (!$profile) {
476             return null;
477         } else {
478             return $profile->getTaggedNotices($tag, $offset, $limit, $since_id, $before_id, $since);
479         }
480     }
481
482     function getNotices($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0, $since=null)
483     {
484         $profile = $this->getProfile();
485         if (!$profile) {
486             return null;
487         } else {
488             return $profile->getNotices($offset, $limit, $since_id, $before_id, $since);
489         }
490     }
491
492     function favoriteNotices($offset=0, $limit=NOTICES_PER_PAGE, $own=false)
493     {
494         $ids = Fave::stream($this->id, $offset, $limit, $own);
495         return Notice::getStreamByIds($ids);
496     }
497
498     function noticesWithFriends($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0, $since=null)
499     {
500         return Inbox::streamNotices($this->id, $offset, $limit, $since_id, $before_id, $since, false);
501     }
502
503     function noticeInbox($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0, $since=null)
504     {
505         return Inbox::streamNotices($this->id, $offset, $limit, $since_id, $before_id, $since, true);
506     }
507
508     function friendsTimeline($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0, $since=null)
509     {
510         return Inbox::streamNotices($this->id, $offset, $limit, $since_id, $before_id, $since, false);
511     }
512
513     function ownFriendsTimeline($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0, $since=null)
514     {
515         return Inbox::streamNotices($this->id, $offset, $limit, $since_id, $before_id, $since, true);
516     }
517
518     function blowFavesCache()
519     {
520         $cache = common_memcache();
521         if ($cache) {
522             // Faves don't happen chronologically, so we need to blow
523             // ;last cache, too
524             $cache->delete(common_cache_key('fave:ids_by_user:'.$this->id));
525             $cache->delete(common_cache_key('fave:ids_by_user:'.$this->id.';last'));
526             $cache->delete(common_cache_key('fave:ids_by_user_own:'.$this->id));
527             $cache->delete(common_cache_key('fave:ids_by_user_own:'.$this->id.';last'));
528         }
529         $profile = $this->getProfile();
530         $profile->blowFaveCount();
531     }
532
533     function getSelfTags()
534     {
535         return Profile_tag::getTags($this->id, $this->id);
536     }
537
538     function setSelfTags($newtags)
539     {
540         return Profile_tag::setTags($this->id, $this->id, $newtags);
541     }
542
543     function block($other)
544     {
545         // Add a new block record
546
547         // no blocking (and thus unsubbing from) yourself
548
549         if ($this->id == $other->id) {
550             common_log(LOG_WARNING,
551                 sprintf(
552                     "Profile ID %d (%s) tried to block his or herself.",
553                     $profile->id,
554                     $profile->nickname
555                 )
556             );
557             return false;
558         }
559
560         $block = new Profile_block();
561
562         // Begin a transaction
563
564         $block->query('BEGIN');
565
566         $block->blocker = $this->id;
567         $block->blocked = $other->id;
568
569         $result = $block->insert();
570
571         if (!$result) {
572             common_log_db_error($block, 'INSERT', __FILE__);
573             return false;
574         }
575
576         // Cancel their subscription, if it exists
577
578         $otherUser = User::staticGet('id', $other->id);
579
580         if (!empty($otherUser)) {
581             subs_unsubscribe_to($otherUser, $this->getProfile());
582         }
583
584         $block->query('COMMIT');
585
586         return true;
587     }
588
589     function unblock($other)
590     {
591         // Get the block record
592
593         $block = Profile_block::get($this->id, $other->id);
594
595         if (!$block) {
596             return false;
597         }
598
599         $result = $block->delete();
600
601         if (!$result) {
602             common_log_db_error($block, 'DELETE', __FILE__);
603             return false;
604         }
605
606         return true;
607     }
608
609     function isMember($group)
610     {
611         $profile = $this->getProfile();
612         return $profile->isMember($group);
613     }
614
615     function isAdmin($group)
616     {
617         $profile = $this->getProfile();
618         return $profile->isAdmin($group);
619     }
620
621     function getGroups($offset=0, $limit=null)
622     {
623         $qry =
624           'SELECT user_group.* ' .
625           'FROM user_group JOIN group_member '.
626           'ON user_group.id = group_member.group_id ' .
627           'WHERE group_member.profile_id = %d ' .
628           'ORDER BY group_member.created DESC ';
629
630         if ($offset>0 && !is_null($limit)) {
631             if ($offset) {
632                 if (common_config('db','type') == 'pgsql') {
633                     $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
634                 } else {
635                     $qry .= ' LIMIT ' . $offset . ', ' . $limit;
636                 }
637             }
638         }
639
640         $groups = new User_group();
641
642         $cnt = $groups->query(sprintf($qry, $this->id));
643
644         return $groups;
645     }
646
647     function getSubscriptions($offset=0, $limit=null)
648     {
649         $profile = $this->getProfile();
650         assert(!empty($profile));
651         return $profile->getSubscriptions($offset, $limit);
652     }
653
654     function getSubscribers($offset=0, $limit=null)
655     {
656         $profile = $this->getProfile();
657         assert(!empty($profile));
658         return $profile->getSubscribers($offset, $limit);
659     }
660
661     function getTaggedSubscribers($tag, $offset=0, $limit=null)
662     {
663         $qry =
664           'SELECT profile.* ' .
665           'FROM profile JOIN subscription ' .
666           'ON profile.id = subscription.subscriber ' .
667           'JOIN profile_tag ON (profile_tag.tagged = subscription.subscriber ' .
668           'AND profile_tag.tagger = subscription.subscribed) ' .
669           'WHERE subscription.subscribed = %d ' .
670           "AND profile_tag.tag = '%s' " .
671           'AND subscription.subscribed != subscription.subscriber ' .
672           'ORDER BY subscription.created DESC ';
673
674         if ($offset) {
675             $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
676         }
677
678         $profile = new Profile();
679
680         $cnt = $profile->query(sprintf($qry, $this->id, $tag));
681
682         return $profile;
683     }
684
685     function getTaggedSubscriptions($tag, $offset=0, $limit=null)
686     {
687         $qry =
688           'SELECT profile.* ' .
689           'FROM profile JOIN subscription ' .
690           'ON profile.id = subscription.subscribed ' .
691           'JOIN profile_tag on (profile_tag.tagged = subscription.subscribed ' .
692           'AND profile_tag.tagger = subscription.subscriber) ' .
693           'WHERE subscription.subscriber = %d ' .
694           "AND profile_tag.tag = '%s' " .
695           'AND subscription.subscribed != subscription.subscriber ' .
696           'ORDER BY subscription.created DESC ';
697
698         $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
699
700         $profile = new Profile();
701
702         $profile->query(sprintf($qry, $this->id, $tag));
703
704         return $profile;
705     }
706
707     function getDesign()
708     {
709         return Design::staticGet('id', $this->design_id);
710     }
711
712     function hasRight($right)
713     {
714         $profile = $this->getProfile();
715         return $profile->hasRight($right);
716     }
717
718     function delete()
719     {
720         $profile = $this->getProfile();
721         if ($profile) {
722             $profile->delete();
723         }
724
725         $related = array('Fave',
726                          'Confirm_address',
727                          'Remember_me',
728                          'Foreign_link',
729                          'Invitation',
730                          );
731         Event::handle('UserDeleteRelated', array($this, &$related));
732
733         foreach ($related as $cls) {
734             $inst = new $cls();
735             $inst->user_id = $this->id;
736             $inst->delete();
737         }
738
739         $this->_deleteTags();
740         $this->_deleteBlocks();
741
742         parent::delete();
743     }
744
745     function _deleteTags()
746     {
747         $tag = new Profile_tag();
748         $tag->tagger = $this->id;
749         $tag->delete();
750     }
751
752     function _deleteBlocks()
753     {
754         $block = new Profile_block();
755         $block->blocker = $this->id;
756         $block->delete();
757         // XXX delete group block? Reset blocker?
758     }
759
760     function hasRole($name)
761     {
762         $profile = $this->getProfile();
763         return $profile->hasRole($name);
764     }
765
766     function grantRole($name)
767     {
768         $profile = $this->getProfile();
769         return $profile->grantRole($name);
770     }
771
772     function revokeRole($name)
773     {
774         $profile = $this->getProfile();
775         return $profile->revokeRole($name);
776     }
777
778     function isSandboxed()
779     {
780         $profile = $this->getProfile();
781         return $profile->isSandboxed();
782     }
783
784     function isSilenced()
785     {
786         $profile = $this->getProfile();
787         return $profile->isSilenced();
788     }
789
790     function repeatedByMe($offset=0, $limit=20, $since_id=null, $max_id=null)
791     {
792         $ids = Notice::stream(array($this, '_repeatedByMeDirect'),
793                               array(),
794                               'user:repeated_by_me:'.$this->id,
795                               $offset, $limit, $since_id, $max_id, null);
796
797         return Notice::getStreamByIds($ids);
798     }
799
800     function _repeatedByMeDirect($offset, $limit, $since_id, $max_id, $since)
801     {
802         $notice = new Notice();
803
804         $notice->selectAdd(); // clears it
805         $notice->selectAdd('id');
806
807         $notice->profile_id = $this->id;
808         $notice->whereAdd('repeat_of IS NOT NULL');
809
810         $notice->orderBy('id DESC');
811
812         if (!is_null($offset)) {
813             $notice->limit($offset, $limit);
814         }
815
816         if ($since_id != 0) {
817             $notice->whereAdd('id > ' . $since_id);
818         }
819
820         if ($max_id != 0) {
821             $notice->whereAdd('id <= ' . $max_id);
822         }
823
824         if (!is_null($since)) {
825             $notice->whereAdd('created > \'' . date('Y-m-d H:i:s', $since) . '\'');
826         }
827
828         $ids = array();
829
830         if ($notice->find()) {
831             while ($notice->fetch()) {
832                 $ids[] = $notice->id;
833             }
834         }
835
836         $notice->free();
837         $notice = NULL;
838
839         return $ids;
840     }
841
842     function repeatsOfMe($offset=0, $limit=20, $since_id=null, $max_id=null)
843     {
844         $ids = Notice::stream(array($this, '_repeatsOfMeDirect'),
845                               array(),
846                               'user:repeats_of_me:'.$this->id,
847                               $offset, $limit, $since_id, $max_id, null);
848
849         return Notice::getStreamByIds($ids);
850     }
851
852     function _repeatsOfMeDirect($offset, $limit, $since_id, $max_id, $since)
853     {
854         $qry =
855           'SELECT DISTINCT original.id AS id ' .
856           'FROM notice original JOIN notice rept ON original.id = rept.repeat_of ' .
857           'WHERE original.profile_id = ' . $this->id . ' ';
858
859         if ($since_id != 0) {
860             $qry .= 'AND original.id > ' . $since_id . ' ';
861         }
862
863         if ($max_id != 0) {
864             $qry .= 'AND original.id <= ' . $max_id . ' ';
865         }
866
867         if (!is_null($since)) {
868             $qry .= 'AND original.modified > \'' . date('Y-m-d H:i:s', $since) . '\' ';
869         }
870
871         // NOTE: we sort by fave time, not by notice time!
872
873         $qry .= 'ORDER BY original.id DESC ';
874
875         if (!is_null($offset)) {
876             $qry .= "LIMIT $limit OFFSET $offset";
877         }
878
879         $ids = array();
880
881         $notice = new Notice();
882
883         $notice->query($qry);
884
885         while ($notice->fetch()) {
886             $ids[] = $notice->id;
887         }
888
889         $notice->free();
890         $notice = NULL;
891
892         return $ids;
893     }
894
895     function repeatedToMe($offset=0, $limit=20, $since_id=null, $max_id=null)
896     {
897         throw new Exception("Not implemented since inbox change.");
898     }
899
900     function shareLocation()
901     {
902         $cfg = common_config('location', 'share');
903
904         if ($cfg == 'always') {
905             return true;
906         } else if ($cfg == 'never') {
907             return false;
908         } else { // user
909             $share = true;
910
911             $prefs = User_location_prefs::staticGet('user_id', $this->id);
912
913             if (empty($prefs)) {
914                 $share = common_config('location', 'sharedefault');
915             } else {
916                 $share = $prefs->share_location;
917                 $prefs->free();
918             }
919
920             return $share;
921         }
922     }
923
924     static function siteOwner()
925     {
926         $owner = self::cacheGet('user:site_owner');
927
928         if ($owner === false) { // cache miss
929
930             $pr = new Profile_role();
931
932             $pr->role = Profile_role::OWNER;
933
934             $pr->orderBy('created');
935
936             $pr->limit(1);
937
938             if ($pr->find(true)) {
939                 $owner = User::staticGet('id', $pr->profile_id);
940             } else {
941                 $owner = null;
942             }
943
944             self::cacheSet('user:site_owner', $owner);
945         }
946
947         return $owner;
948     }
949 }