]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/User.php
Merge branch '0.9.x' of gitorious.org:statusnet/mainline 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         $profile = Profile::staticGet('id', $this->id);
74         if (empty($profile)) {
75             throw new UserNoProfileException($this);
76         }
77         return $profile;
78     }
79
80     function isSubscribed($other)
81     {
82         return Subscription::exists($this->getProfile(), $other);
83     }
84
85     // 'update' won't write key columns, so we have to do it ourselves.
86
87     function updateKeys(&$orig)
88     {
89         $this->_connect();
90         $parts = array();
91         foreach (array('nickname', 'email', 'incomingemail', 'sms', 'carrier', 'smsemail', 'language', 'timezone') as $k) {
92             if (strcmp($this->$k, $orig->$k) != 0) {
93                 $parts[] = $k . ' = ' . $this->_quote($this->$k);
94             }
95         }
96         if (count($parts) == 0) {
97             // No changes
98             return true;
99         }
100         $toupdate = implode(', ', $parts);
101
102         $table = common_database_tablename($this->tableName());
103         $qry = 'UPDATE ' . $table . ' SET ' . $toupdate .
104           ' WHERE id = ' . $this->id;
105         $orig->decache();
106         $result = $this->query($qry);
107         if ($result) {
108             $this->encache();
109         }
110         return $result;
111     }
112
113     static function allowed_nickname($nickname)
114     {
115         // XXX: should already be validated for size, content, etc.
116         $blacklist = common_config('nickname', 'blacklist');
117
118         //all directory and file names should be blacklisted
119         $d = dir(INSTALLDIR);
120         while (false !== ($entry = $d->read())) {
121             $blacklist[]=$entry;
122         }
123         $d->close();
124
125         //all top level names in the router should be blacklisted
126         $router = Router::get();
127         foreach(array_keys($router->m->getPaths()) as $path){
128             if(preg_match('/^\/(.*?)[\/\?]/',$path,$matches)){
129                 $blacklist[]=$matches[1];
130             }
131         }
132         return !in_array($nickname, $blacklist);
133     }
134
135     /**
136      * Get the most recent notice posted by this user, if any.
137      *
138      * @return mixed Notice or null
139      */
140     function getCurrentNotice()
141     {
142         $profile = $this->getProfile();
143         return $profile->getCurrentNotice();
144     }
145
146     function getCarrier()
147     {
148         return Sms_carrier::staticGet('id', $this->carrier);
149     }
150
151     /**
152      * @deprecated use Subscription::start($sub, $other);
153      */
154     function subscribeTo($other)
155     {
156         return Subscription::start($this->getProfile(), $other);
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         // Set default-on options here, otherwise they'll be disabled
254         // initially for sites using caching, since the initial encache
255         // doesn't know about the defaults in the database.
256         $user->emailnotifysub = 1;
257         $user->emailnotifyfav = 1;
258         $user->emailnotifynudge = 1;
259         $user->emailnotifymsg = 1;
260         $user->emailnotifyattn = 1;
261         $user->emailmicroid = 1;
262         $user->emailpost = 1;
263         $user->jabbermicroid = 1;
264         $user->viewdesigns = 1;
265
266         $user->created = common_sql_now();
267
268         if (Event::handle('StartUserRegister', array(&$user, &$profile))) {
269
270             $profile->query('BEGIN');
271
272             $id = $profile->insert();
273
274             if (empty($id)) {
275                 common_log_db_error($profile, 'INSERT', __FILE__);
276                 return false;
277             }
278
279             $user->id = $id;
280
281             if (!empty($uri)) {
282                 $user->uri = $uri;
283             } else {
284                 $user->uri = common_user_uri($user);
285             }
286
287             if (!empty($password)) { // may not have a password for OpenID users
288                 $user->password = common_munge_password($password, $id);
289             }
290
291             $result = $user->insert();
292
293             if (!$result) {
294                 common_log_db_error($user, 'INSERT', __FILE__);
295                 return false;
296             }
297
298             // Everyone gets an inbox
299
300             $inbox = new Inbox();
301
302             $inbox->user_id = $user->id;
303             $inbox->notice_ids = '';
304
305             $result = $inbox->insert();
306
307             if (!$result) {
308                 common_log_db_error($inbox, 'INSERT', __FILE__);
309                 return false;
310             }
311
312             // Everyone is subscribed to themself
313
314             $subscription = new Subscription();
315             $subscription->subscriber = $user->id;
316             $subscription->subscribed = $user->id;
317             $subscription->created = $user->created;
318
319             $result = $subscription->insert();
320
321             if (!$result) {
322                 common_log_db_error($subscription, 'INSERT', __FILE__);
323                 return false;
324             }
325
326             if (!empty($email) && !$user->email) {
327
328                 $confirm = new Confirm_address();
329                 $confirm->code = common_confirmation_code(128);
330                 $confirm->user_id = $user->id;
331                 $confirm->address = $email;
332                 $confirm->address_type = 'email';
333
334                 $result = $confirm->insert();
335
336                 if (!$result) {
337                     common_log_db_error($confirm, 'INSERT', __FILE__);
338                     return false;
339                 }
340             }
341
342             if (!empty($code) && $user->email) {
343                 $user->emailChanged();
344             }
345
346             // Default system subscription
347
348             $defnick = common_config('newuser', 'default');
349
350             if (!empty($defnick)) {
351                 $defuser = User::staticGet('nickname', $defnick);
352                 if (empty($defuser)) {
353                     common_log(LOG_WARNING, sprintf("Default user %s does not exist.", $defnick),
354                                __FILE__);
355                 } else {
356                     Subscription::start($user, $defuser);
357                 }
358             }
359
360             $profile->query('COMMIT');
361
362             if (!empty($email) && !$user->email) {
363                 mail_confirm_address($user, $confirm->code, $profile->nickname, $email);
364             }
365
366             // Welcome message
367
368             $welcome = common_config('newuser', 'welcome');
369
370             if (!empty($welcome)) {
371                 $welcomeuser = User::staticGet('nickname', $welcome);
372                 if (empty($welcomeuser)) {
373                     common_log(LOG_WARNING, sprintf("Welcome user %s does not exist.", $defnick),
374                                __FILE__);
375                 } else {
376                     $notice = Notice::saveNew($welcomeuser->id,
377                                               // TRANS: Notice given on user registration.
378                                               // TRANS: %1$s is the sitename, $2$s is the registering user's nickname.
379                                               sprintf(_('Welcome to %1$s, @%2$s!'),
380                                                       common_config('site', 'name'),
381                                                       $user->nickname),
382                                               'system');
383                 }
384             }
385
386             Event::handle('EndUserRegister', array(&$profile, &$user));
387         }
388
389         return $user;
390     }
391
392     // Things we do when the email changes
393     function emailChanged()
394     {
395
396         $invites = new Invitation();
397         $invites->address = $this->email;
398         $invites->address_type = 'email';
399
400         if ($invites->find()) {
401             while ($invites->fetch()) {
402                 $other = User::staticGet($invites->user_id);
403                 subs_subscribe_to($other, $this);
404             }
405         }
406     }
407
408     function hasFave($notice)
409     {
410         $profile = $this->getProfile();
411         return $profile->hasFave($notice);
412     }
413
414     function mutuallySubscribed($other)
415     {
416         return $this->isSubscribed($other) &&
417           $other->isSubscribed($this);
418     }
419
420     function mutuallySubscribedUsers()
421     {
422         // 3-way join; probably should get cached
423         $UT = common_config('db','type')=='pgsql'?'"user"':'user';
424         $qry = "SELECT $UT.* " .
425           "FROM subscription sub1 JOIN $UT ON sub1.subscribed = $UT.id " .
426           "JOIN subscription sub2 ON $UT.id = sub2.subscriber " .
427           'WHERE sub1.subscriber = %d and sub2.subscribed = %d ' .
428           "ORDER BY $UT.nickname";
429         $user = new User();
430         $user->query(sprintf($qry, $this->id, $this->id));
431
432         return $user;
433     }
434
435     function getReplies($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0)
436     {
437         $ids = Reply::stream($this->id, $offset, $limit, $since_id, $before_id);
438         return Notice::getStreamByIds($ids);
439     }
440
441     function getTaggedNotices($tag, $offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0) {
442         $profile = $this->getProfile();
443         return $profile->getTaggedNotices($tag, $offset, $limit, $since_id, $before_id);
444     }
445
446     function getNotices($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0)
447     {
448         $profile = $this->getProfile();
449         return $profile->getNotices($offset, $limit, $since_id, $before_id);
450     }
451
452     function favoriteNotices($own=false, $offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $max_id=0)
453     {
454         $ids = Fave::stream($this->id, $offset, $limit, $own, $since_id, $max_id);
455         return Notice::getStreamByIds($ids);
456     }
457
458     function noticesWithFriends($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0)
459     {
460         return Inbox::streamNotices($this->id, $offset, $limit, $since_id, $before_id, false);
461     }
462
463     function noticeInbox($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0)
464     {
465         return Inbox::streamNotices($this->id, $offset, $limit, $since_id, $before_id, true);
466     }
467
468     function friendsTimeline($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0)
469     {
470         return Inbox::streamNotices($this->id, $offset, $limit, $since_id, $before_id, false);
471     }
472
473     function ownFriendsTimeline($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0)
474     {
475         return Inbox::streamNotices($this->id, $offset, $limit, $since_id, $before_id, true);
476     }
477
478     function blowFavesCache()
479     {
480         $profile = $this->getProfile();
481         $profile->blowFavesCache();
482     }
483
484     function getSelfTags()
485     {
486         return Profile_tag::getTags($this->id, $this->id);
487     }
488
489     function setSelfTags($newtags)
490     {
491         return Profile_tag::setTags($this->id, $this->id, $newtags);
492     }
493
494     function block($other)
495     {
496         // Add a new block record
497
498         // no blocking (and thus unsubbing from) yourself
499
500         if ($this->id == $other->id) {
501             common_log(LOG_WARNING,
502                 sprintf(
503                     "Profile ID %d (%s) tried to block themself.",
504                     $this->id,
505                     $this->nickname
506                 )
507             );
508             return false;
509         }
510
511         $block = new Profile_block();
512
513         // Begin a transaction
514
515         $block->query('BEGIN');
516
517         $block->blocker = $this->id;
518         $block->blocked = $other->id;
519
520         $result = $block->insert();
521
522         if (!$result) {
523             common_log_db_error($block, 'INSERT', __FILE__);
524             return false;
525         }
526
527         $self = $this->getProfile();
528         if (Subscription::exists($other, $self)) {
529             Subscription::cancel($other, $self);
530         }
531         if (Subscription::exists($self, $other)) {
532             Subscription::cancel($self, $other);
533         }
534
535         $block->query('COMMIT');
536
537         return true;
538     }
539
540     function unblock($other)
541     {
542         // Get the block record
543
544         $block = Profile_block::get($this->id, $other->id);
545
546         if (!$block) {
547             return false;
548         }
549
550         $result = $block->delete();
551
552         if (!$result) {
553             common_log_db_error($block, 'DELETE', __FILE__);
554             return false;
555         }
556
557         return true;
558     }
559
560     function isMember($group)
561     {
562         $profile = $this->getProfile();
563         return $profile->isMember($group);
564     }
565
566     function isAdmin($group)
567     {
568         $profile = $this->getProfile();
569         return $profile->isAdmin($group);
570     }
571
572     function getGroups($offset=0, $limit=null)
573     {
574         $profile = $this->getProfile();
575         return $profile->getGroups($offset, $limit);
576     }
577
578     function getSubscriptions($offset=0, $limit=null)
579     {
580         $profile = $this->getProfile();
581         return $profile->getSubscriptions($offset, $limit);
582     }
583
584     function getSubscribers($offset=0, $limit=null)
585     {
586         $profile = $this->getProfile();
587         return $profile->getSubscribers($offset, $limit);
588     }
589
590     function getTaggedSubscribers($tag, $offset=0, $limit=null)
591     {
592         $qry =
593           'SELECT profile.* ' .
594           'FROM profile JOIN subscription ' .
595           'ON profile.id = subscription.subscriber ' .
596           'JOIN profile_tag ON (profile_tag.tagged = subscription.subscriber ' .
597           'AND profile_tag.tagger = subscription.subscribed) ' .
598           'WHERE subscription.subscribed = %d ' .
599           "AND profile_tag.tag = '%s' " .
600           'AND subscription.subscribed != subscription.subscriber ' .
601           'ORDER BY subscription.created DESC ';
602
603         if ($offset) {
604             $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
605         }
606
607         $profile = new Profile();
608
609         $cnt = $profile->query(sprintf($qry, $this->id, $tag));
610
611         return $profile;
612     }
613
614     function getTaggedSubscriptions($tag, $offset=0, $limit=null)
615     {
616         $qry =
617           'SELECT profile.* ' .
618           'FROM profile JOIN subscription ' .
619           'ON profile.id = subscription.subscribed ' .
620           'JOIN profile_tag on (profile_tag.tagged = subscription.subscribed ' .
621           'AND profile_tag.tagger = subscription.subscriber) ' .
622           'WHERE subscription.subscriber = %d ' .
623           "AND profile_tag.tag = '%s' " .
624           'AND subscription.subscribed != subscription.subscriber ' .
625           'ORDER BY subscription.created DESC ';
626
627         $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
628
629         $profile = new Profile();
630
631         $profile->query(sprintf($qry, $this->id, $tag));
632
633         return $profile;
634     }
635
636     function getDesign()
637     {
638         return Design::staticGet('id', $this->design_id);
639     }
640
641     function hasRight($right)
642     {
643         $profile = $this->getProfile();
644         return $profile->hasRight($right);
645     }
646
647     function delete()
648     {
649         try {
650             $profile = $this->getProfile();
651             $profile->delete();
652         } catch (UserNoProfileException $unp) {
653             common_log(LOG_INFO, "User {$this->nickname} has no profile; continuing deletion.");
654         }
655
656         $related = array('Fave',
657                          'Confirm_address',
658                          'Remember_me',
659                          'Foreign_link',
660                          'Invitation',
661                          );
662
663         Event::handle('UserDeleteRelated', array($this, &$related));
664
665         foreach ($related as $cls) {
666             $inst = new $cls();
667             $inst->user_id = $this->id;
668             $inst->delete();
669         }
670
671         $this->_deleteTags();
672         $this->_deleteBlocks();
673
674         parent::delete();
675     }
676
677     function _deleteTags()
678     {
679         $tag = new Profile_tag();
680         $tag->tagger = $this->id;
681         $tag->delete();
682     }
683
684     function _deleteBlocks()
685     {
686         $block = new Profile_block();
687         $block->blocker = $this->id;
688         $block->delete();
689         // XXX delete group block? Reset blocker?
690     }
691
692     function hasRole($name)
693     {
694         $profile = $this->getProfile();
695         return $profile->hasRole($name);
696     }
697
698     function grantRole($name)
699     {
700         $profile = $this->getProfile();
701         return $profile->grantRole($name);
702     }
703
704     function revokeRole($name)
705     {
706         $profile = $this->getProfile();
707         return $profile->revokeRole($name);
708     }
709
710     function isSandboxed()
711     {
712         $profile = $this->getProfile();
713         return $profile->isSandboxed();
714     }
715
716     function isSilenced()
717     {
718         $profile = $this->getProfile();
719         return $profile->isSilenced();
720     }
721
722     function repeatedByMe($offset=0, $limit=20, $since_id=null, $max_id=null)
723     {
724         $ids = Notice::stream(array($this, '_repeatedByMeDirect'),
725                               array(),
726                               'user:repeated_by_me:'.$this->id,
727                               $offset, $limit, $since_id, $max_id, null);
728
729         return Notice::getStreamByIds($ids);
730     }
731
732     function _repeatedByMeDirect($offset, $limit, $since_id, $max_id)
733     {
734         $notice = new Notice();
735
736         $notice->selectAdd(); // clears it
737         $notice->selectAdd('id');
738
739         $notice->profile_id = $this->id;
740         $notice->whereAdd('repeat_of IS NOT NULL');
741
742         $notice->orderBy('id DESC');
743
744         if (!is_null($offset)) {
745             $notice->limit($offset, $limit);
746         }
747
748         if ($since_id != 0) {
749             $notice->whereAdd('id > ' . $since_id);
750         }
751
752         if ($max_id != 0) {
753             $notice->whereAdd('id <= ' . $max_id);
754         }
755
756         $ids = array();
757
758         if ($notice->find()) {
759             while ($notice->fetch()) {
760                 $ids[] = $notice->id;
761             }
762         }
763
764         $notice->free();
765         $notice = NULL;
766
767         return $ids;
768     }
769
770     function repeatsOfMe($offset=0, $limit=20, $since_id=null, $max_id=null)
771     {
772         $ids = Notice::stream(array($this, '_repeatsOfMeDirect'),
773                               array(),
774                               'user:repeats_of_me:'.$this->id,
775                               $offset, $limit, $since_id, $max_id);
776
777         return Notice::getStreamByIds($ids);
778     }
779
780     function _repeatsOfMeDirect($offset, $limit, $since_id, $max_id)
781     {
782         $qry =
783           'SELECT DISTINCT original.id AS id ' .
784           'FROM notice original JOIN notice rept ON original.id = rept.repeat_of ' .
785           'WHERE original.profile_id = ' . $this->id . ' ';
786
787         if ($since_id != 0) {
788             $qry .= 'AND original.id > ' . $since_id . ' ';
789         }
790
791         if ($max_id != 0) {
792             $qry .= 'AND original.id <= ' . $max_id . ' ';
793         }
794
795         // NOTE: we sort by fave time, not by notice time!
796
797         $qry .= 'ORDER BY original.id DESC ';
798
799         if (!is_null($offset)) {
800             $qry .= "LIMIT $limit OFFSET $offset";
801         }
802
803         $ids = array();
804
805         $notice = new Notice();
806
807         $notice->query($qry);
808
809         while ($notice->fetch()) {
810             $ids[] = $notice->id;
811         }
812
813         $notice->free();
814         $notice = NULL;
815
816         return $ids;
817     }
818
819     function repeatedToMe($offset=0, $limit=20, $since_id=null, $max_id=null)
820     {
821         throw new Exception("Not implemented since inbox change.");
822     }
823
824     function shareLocation()
825     {
826         $cfg = common_config('location', 'share');
827
828         if ($cfg == 'always') {
829             return true;
830         } else if ($cfg == 'never') {
831             return false;
832         } else { // user
833             $share = true;
834
835             $prefs = User_location_prefs::staticGet('user_id', $this->id);
836
837             if (empty($prefs)) {
838                 $share = common_config('location', 'sharedefault');
839             } else {
840                 $share = $prefs->share_location;
841                 $prefs->free();
842             }
843
844             return $share;
845         }
846     }
847
848     static function siteOwner()
849     {
850         $owner = self::cacheGet('user:site_owner');
851
852         if ($owner === false) { // cache miss
853
854             $pr = new Profile_role();
855
856             $pr->role = Profile_role::OWNER;
857
858             $pr->orderBy('created');
859
860             $pr->limit(1);
861
862             if ($pr->find(true)) {
863                 $owner = User::staticGet('id', $pr->profile_id);
864             } else {
865                 $owner = null;
866             }
867
868             self::cacheSet('user:site_owner', $owner);
869         }
870
871         return $owner;
872     }
873 }