3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2008-2011, StatusNet, Inc.
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.
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.
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/>.
20 if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
23 * Table Definition for profile
25 require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
27 class Profile extends Memcached_DataObject
30 /* the code below is auto generated do not remove the above tag */
32 public $__table = 'profile'; // table name
33 public $id; // int(4) primary_key not_null
34 public $nickname; // varchar(64) multiple_key not_null
35 public $fullname; // varchar(255) multiple_key
36 public $profileurl; // varchar(255)
37 public $homepage; // varchar(255) multiple_key
38 public $bio; // text() multiple_key
39 public $location; // varchar(255) multiple_key
40 public $lat; // decimal(10,7)
41 public $lon; // decimal(10,7)
42 public $location_id; // int(4)
43 public $location_ns; // int(4)
44 public $created; // datetime() not_null
45 public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
48 function staticGet($k,$v=NULL) {
49 return Memcached_DataObject::staticGet('Profile',$k,$v);
52 function multiGet($keyCol, $keyVals, $skipNulls=true)
54 return parent::multiGet('Profile', $keyCol, $keyVals, $skipNulls);
57 /* the code above is auto generated do not remove the tag below */
60 protected $_user = -1; // Uninitialized value distinct from null
64 if (is_int($this->_user) && $this->_user == -1) {
65 $this->_user = User::staticGet('id', $this->id);
71 protected $_avatars = array();
73 function getAvatar($width, $height=null)
75 if (is_null($height)) {
79 if (array_key_exists($width, $this->_avatars)) {
80 return $this->_avatars[$width];
85 if (Event::handle('StartProfileGetAvatar', array($this, $width, &$avatar))) {
86 $avatar = Avatar::pkeyGet(array('profile_id' => $this->id,
88 'height' => $height));
89 Event::handle('EndProfileGetAvatar', array($this, $width, &$avatar));
92 $this->_avatars[$width] = $avatar;
97 function _fillAvatar($width, $avatar)
99 $this->_avatars[$width] = $avatar;
102 function getOriginalAvatar()
104 $avatar = DB_DataObject::factory('avatar');
105 $avatar->profile_id = $this->id;
106 $avatar->original = true;
107 if ($avatar->find(true)) {
114 function setOriginal($filename)
116 $imagefile = new ImageFile($this->id, Avatar::path($filename));
118 $avatar = new Avatar();
119 $avatar->profile_id = $this->id;
120 $avatar->width = $imagefile->width;
121 $avatar->height = $imagefile->height;
122 $avatar->mediatype = image_type_to_mime_type($imagefile->type);
123 $avatar->filename = $filename;
124 $avatar->original = true;
125 $avatar->url = Avatar::url($filename);
126 $avatar->created = DB_DataObject_Cast::dateTime(); # current time
128 // XXX: start a transaction here
130 if (!$this->delete_avatars() || !$avatar->insert()) {
131 @unlink(Avatar::path($filename));
135 foreach (array(AVATAR_PROFILE_SIZE, AVATAR_STREAM_SIZE, AVATAR_MINI_SIZE) as $size) {
136 // We don't do a scaled one if original is our scaled size
137 if (!($avatar->width == $size && $avatar->height == $size)) {
138 $scaled_filename = $imagefile->resize($size);
140 //$scaled = DB_DataObject::factory('avatar');
141 $scaled = new Avatar();
142 $scaled->profile_id = $this->id;
143 $scaled->width = $size;
144 $scaled->height = $size;
145 $scaled->original = false;
146 $scaled->mediatype = image_type_to_mime_type($imagefile->type);
147 $scaled->filename = $scaled_filename;
148 $scaled->url = Avatar::url($scaled_filename);
149 $scaled->created = DB_DataObject_Cast::dateTime(); # current time
151 if (!$scaled->insert()) {
161 * Delete attached avatars for this user from the database and filesystem.
162 * This should be used instead of a batch delete() to ensure that files
163 * get removed correctly.
165 * @param boolean $original true to delete only the original-size file
168 function delete_avatars($original=true)
170 $avatar = new Avatar();
171 $avatar->profile_id = $this->id;
173 while ($avatar->fetch()) {
174 if ($avatar->original) {
175 if ($original == false) {
185 * Gets either the full name (if filled) or the nickname.
189 function getBestName()
191 return ($this->fullname) ? $this->fullname : $this->nickname;
195 * Gets the full name (if filled) with nickname as a parenthetical, or the nickname alone
196 * if no fullname is provided.
200 function getFancyName()
202 if ($this->fullname) {
203 // TRANS: Full name of a profile or group (%1$s) followed by nickname (%2$s) in parentheses.
204 return sprintf(_m('FANCYNAME','%1$s (%2$s)'), $this->fullname, $this->nickname);
206 return $this->nickname;
211 * Get the most recent notice posted by this user, if any.
213 * @return mixed Notice or null
215 function getCurrentNotice()
217 $notice = $this->getNotices(0, 1);
219 if ($notice->fetch()) {
220 if ($notice instanceof ArrayWrapper) {
221 // hack for things trying to work with single notices
222 return $notice->_items[0];
230 function getTaggedNotices($tag, $offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $max_id=0)
232 $stream = new TaggedProfileNoticeStream($this, $tag);
234 return $stream->getNotices($offset, $limit, $since_id, $max_id);
237 function getNotices($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $max_id=0)
239 $stream = new ProfileNoticeStream($this);
241 return $stream->getNotices($offset, $limit, $since_id, $max_id);
244 function isMember($group)
246 $groups = $this->getGroups(0, null);
247 $gs = $groups->fetchAll();
248 foreach ($gs as $g) {
249 if ($group->id == $g->id) {
256 function isAdmin($group)
258 $gm = Group_member::pkeyGet(array('profile_id' => $this->id,
259 'group_id' => $group->id));
260 return (!empty($gm) && $gm->is_admin);
263 function isPendingMember($group)
265 $request = Group_join_queue::pkeyGet(array('profile_id' => $this->id,
266 'group_id' => $group->id));
267 return !empty($request);
270 function getGroups($offset=0, $limit=PROFILES_PER_PAGE)
274 $keypart = sprintf('profile:groups:%d', $this->id);
276 $idstring = self::cacheGet($keypart);
278 if ($idstring !== false) {
279 $ids = explode(',', $idstring);
281 $gm = new Group_member();
283 $gm->profile_id = $this->id;
286 while ($gm->fetch()) {
287 $ids[] = $gm->group_id;
291 self::cacheSet($keypart, implode(',', $ids));
294 return User_group::multiGet('id', $ids);
297 function isTagged($peopletag)
299 $tag = Profile_tag::pkeyGet(array('tagger' => $peopletag->tagger,
300 'tagged' => $this->id,
301 'tag' => $peopletag->tag));
305 function canTag($tagged)
307 if (empty($tagged)) {
311 if ($tagged->id == $this->id) {
315 $all = common_config('peopletag', 'allow_tagging', 'all');
316 $local = common_config('peopletag', 'allow_tagging', 'local');
317 $remote = common_config('peopletag', 'allow_tagging', 'remote');
318 $subs = common_config('peopletag', 'allow_tagging', 'subs');
324 $tagged_user = $tagged->getUser();
325 if (!empty($tagged_user)) {
330 return (Subscription::exists($this, $tagged) ||
331 Subscription::exists($tagged, $this));
332 } else if ($remote) {
338 function getLists($auth_user, $offset=0, $limit=null, $since_id=0, $max_id=0)
342 $keypart = sprintf('profile:lists:%d', $this->id);
344 $idstr = self::cacheGet($keypart);
346 if ($idstr !== false) {
347 $ids = explode(',', $idstr);
349 $list = new Profile_list();
351 $list->selectAdd('id');
352 $list->tagger = $this->id;
353 $list->selectAdd('id as "cursor"');
356 $list->whereAdd('id > '.$since_id);
360 $list->whereAdd('id <= '.$max_id);
363 if($offset>=0 && !is_null($limit)) {
364 $list->limit($offset, $limit);
367 $list->orderBy('id DESC');
370 while ($list->fetch()) {
375 self::cacheSet($keypart, implode(',', $ids));
378 $showPrivate = (($auth_user instanceof User ||
379 $auth_user instanceof Profile) &&
380 $auth_user->id === $this->id);
384 foreach ($ids as $id) {
385 $list = Profile_list::staticGet('id', $id);
387 ($showPrivate || !$list->private)) {
389 if (!isset($list->cursor)) {
390 $list->cursor = $list->id;
397 return new ArrayWrapper($lists);
400 function getOtherTags($auth_user=null, $offset=0, $limit=null, $since_id=0, $max_id=0)
402 $lists = new Profile_list();
404 $tags = new Profile_tag();
405 $tags->tagged = $this->id;
407 $lists->joinAdd($tags);
408 #@fixme: postgres (round(date_part('epoch', my_date)))
409 $lists->selectAdd('unix_timestamp(profile_tag.modified) as "cursor"');
411 if ($auth_user instanceof User || $auth_user instanceof Profile) {
412 $lists->whereAdd('( ( profile_list.private = false ) ' .
413 'OR ( profile_list.tagger = ' . $auth_user->id . ' AND ' .
414 'profile_list.private = true ) )');
416 $lists->private = false;
420 $lists->whereAdd('cursor > '.$since_id);
424 $lists->whereAdd('cursor <= '.$max_id);
427 if($offset>=0 && !is_null($limit)) {
428 $lists->limit($offset, $limit);
431 $lists->orderBy('profile_tag.modified DESC');
437 function getPrivateTags($offset=0, $limit=null, $since_id=0, $max_id=0)
439 $tags = new Profile_list();
440 $tags->private = true;
441 $tags->tagger = $this->id;
444 $tags->whereAdd('id > '.$since_id);
448 $tags->whereAdd('id <= '.$max_id);
451 if($offset>=0 && !is_null($limit)) {
452 $tags->limit($offset, $limit);
455 $tags->orderBy('id DESC');
461 function hasLocalTags()
463 $tags = new Profile_tag();
465 $tags->joinAdd(array('tagger', 'user:id'));
466 $tags->whereAdd('tagged = '.$this->id);
467 $tags->whereAdd('tagger != '.$this->id);
472 return ($tags->N == 0) ? false : true;
475 function getTagSubscriptions($offset=0, $limit=null, $since_id=0, $max_id=0)
477 $lists = new Profile_list();
478 $subs = new Profile_tag_subscription();
480 $lists->joinAdd($subs);
481 #@fixme: postgres (round(date_part('epoch', my_date)))
482 $lists->selectAdd('unix_timestamp(profile_tag_subscription.created) as "cursor"');
484 $lists->whereAdd('profile_tag_subscription.profile_id = '.$this->id);
487 $lists->whereAdd('cursor > '.$since_id);
491 $lists->whereAdd('cursor <= '.$max_id);
494 if($offset>=0 && !is_null($limit)) {
495 $lists->limit($offset, $limit);
498 $lists->orderBy('"cursor" DESC');
505 * Request to join the given group.
506 * May throw exceptions on failure.
508 * @param User_group $group
509 * @return mixed: Group_member on success, Group_join_queue if pending approval, null on some cancels?
511 function joinGroup(User_group $group)
514 if ($group->join_policy == User_group::JOIN_POLICY_MODERATE) {
515 $join = Group_join_queue::saveNew($this, $group);
517 if (Event::handle('StartJoinGroup', array($group, $this))) {
518 $join = Group_member::join($group->id, $this->id);
519 self::blow('profile:groups:%d', $this->id);
520 Event::handle('EndJoinGroup', array($group, $this));
524 // Send any applicable notifications...
531 * Leave a group that this profile is a member of.
533 * @param User_group $group
535 function leaveGroup(User_group $group)
537 if (Event::handle('StartLeaveGroup', array($group, $this))) {
538 Group_member::leave($group->id, $this->id);
539 self::blow('profile:groups:%d', $this->id);
540 Event::handle('EndLeaveGroup', array($group, $this));
544 function avatarUrl($size=AVATAR_PROFILE_SIZE)
546 $avatar = $this->getAvatar($size);
548 return $avatar->displayUrl();
550 return Avatar::defaultImage($size);
554 function getSubscriptions($offset=0, $limit=null)
556 $subs = Subscription::bySubscriber($this->id,
562 while ($subs->fetch()) {
563 $profile = Profile::staticGet($subs->subscribed);
565 $profiles[] = $profile;
569 return new ArrayWrapper($profiles);
572 function getSubscribers($offset=0, $limit=null)
574 $subs = Subscription::bySubscribed($this->id,
580 while ($subs->fetch()) {
581 $profile = Profile::staticGet($subs->subscriber);
583 $profiles[] = $profile;
587 return new ArrayWrapper($profiles);
590 function getTaggedSubscribers($tag)
593 'SELECT profile.* ' .
594 'FROM profile JOIN (subscription, profile_tag, profile_list) ' .
595 'ON profile.id = subscription.subscriber ' .
596 'AND profile.id = profile_tag.tagged ' .
597 'AND profile_tag.tagger = profile_list.tagger AND profile_tag.tag = profile_list.tag ' .
598 'WHERE subscription.subscribed = %d ' .
599 'AND subscription.subscribed != subscription.subscriber ' .
600 'AND profile_tag.tagger = %d AND profile_tag.tag = "%s" ' .
601 'AND profile_list.private = false ' .
602 'ORDER BY subscription.created DESC';
604 $profile = new Profile();
607 $cnt = $profile->query(sprintf($qry, $this->id, $this->id, $tag));
609 while ($profile->fetch()) {
610 $tagged[] = clone($profile);
616 * Get pending subscribers, who have not yet been approved.
622 function getRequests($offset=0, $limit=null)
625 'SELECT profile.* ' .
626 'FROM profile JOIN subscription_queue '.
627 'ON profile.id = subscription_queue.subscriber ' .
628 'WHERE subscription_queue.subscribed = %d ' .
629 'ORDER BY subscription_queue.created DESC ';
631 if ($limit != null) {
632 if (common_config('db','type') == 'pgsql') {
633 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
635 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
639 $members = new Profile();
641 $members->query(sprintf($qry, $this->id));
645 function subscriptionCount()
647 $c = Cache::instance();
650 $cnt = $c->get(Cache::key('profile:subscription_count:'.$this->id));
651 if (is_integer($cnt)) {
656 $sub = new Subscription();
657 $sub->subscriber = $this->id;
659 $cnt = (int) $sub->count('distinct subscribed');
661 $cnt = ($cnt > 0) ? $cnt - 1 : $cnt;
664 $c->set(Cache::key('profile:subscription_count:'.$this->id), $cnt);
670 function subscriberCount()
672 $c = Cache::instance();
674 $cnt = $c->get(Cache::key('profile:subscriber_count:'.$this->id));
675 if (is_integer($cnt)) {
680 $sub = new Subscription();
681 $sub->subscribed = $this->id;
682 $sub->whereAdd('subscriber != subscribed');
683 $cnt = (int) $sub->count('distinct subscriber');
686 $c->set(Cache::key('profile:subscriber_count:'.$this->id), $cnt);
693 * Is this profile subscribed to another profile?
695 * @param Profile $other
698 function isSubscribed($other)
700 return Subscription::exists($this, $other);
704 * Check if a pending subscription request is outstanding for this...
706 * @param Profile $other
709 function hasPendingSubscription($other)
711 return Subscription_queue::exists($this, $other);
715 * Are these two profiles subscribed to each other?
717 * @param Profile $other
720 function mutuallySubscribed($other)
722 return $this->isSubscribed($other) &&
723 $other->isSubscribed($this);
726 function hasFave($notice)
728 $fave = Fave::pkeyGet(array('user_id' => $this->id,
729 'notice_id' => $notice->id));
730 return ((is_null($fave)) ? false : true);
735 $c = Cache::instance();
737 $cnt = $c->get(Cache::key('profile:fave_count:'.$this->id));
738 if (is_integer($cnt)) {
744 $faves->user_id = $this->id;
745 $cnt = (int) $faves->count('distinct notice_id');
748 $c->set(Cache::key('profile:fave_count:'.$this->id), $cnt);
754 function noticeCount()
756 $c = Cache::instance();
759 $cnt = $c->get(Cache::key('profile:notice_count:'.$this->id));
760 if (is_integer($cnt)) {
765 $notices = new Notice();
766 $notices->profile_id = $this->id;
767 $cnt = (int) $notices->count('distinct id');
770 $c->set(Cache::key('profile:notice_count:'.$this->id), $cnt);
776 function blowFavesCache()
778 $cache = Cache::instance();
780 // Faves don't happen chronologically, so we need to blow
782 $cache->delete(Cache::key('fave:ids_by_user:'.$this->id));
783 $cache->delete(Cache::key('fave:ids_by_user:'.$this->id.';last'));
784 $cache->delete(Cache::key('fave:ids_by_user_own:'.$this->id));
785 $cache->delete(Cache::key('fave:ids_by_user_own:'.$this->id.';last'));
787 $this->blowFaveCount();
790 function blowSubscriberCount()
792 $c = Cache::instance();
794 $c->delete(Cache::key('profile:subscriber_count:'.$this->id));
798 function blowSubscriptionCount()
800 $c = Cache::instance();
802 $c->delete(Cache::key('profile:subscription_count:'.$this->id));
806 function blowFaveCount()
808 $c = Cache::instance();
810 $c->delete(Cache::key('profile:fave_count:'.$this->id));
814 function blowNoticeCount()
816 $c = Cache::instance();
818 $c->delete(Cache::key('profile:notice_count:'.$this->id));
822 static function maxBio()
824 $biolimit = common_config('profile', 'biolimit');
825 // null => use global limit (distinct from 0!)
826 if (is_null($biolimit)) {
827 $biolimit = common_config('site', 'textlimit');
832 static function bioTooLong($bio)
834 $biolimit = self::maxBio();
835 return ($biolimit > 0 && !empty($bio) && (mb_strlen($bio) > $biolimit));
840 $this->_deleteNotices();
841 $this->_deleteSubscriptions();
842 $this->_deleteMessages();
843 $this->_deleteTags();
844 $this->_deleteBlocks();
845 $this->delete_avatars();
847 // Warning: delete() will run on the batch objects,
848 // not on individual objects.
849 $related = array('Reply',
852 Event::handle('ProfileDeleteRelated', array($this, &$related));
854 foreach ($related as $cls) {
856 $inst->profile_id = $this->id;
863 function _deleteNotices()
865 $notice = new Notice();
866 $notice->profile_id = $this->id;
868 if ($notice->find()) {
869 while ($notice->fetch()) {
870 $other = clone($notice);
876 function _deleteSubscriptions()
878 $sub = new Subscription();
879 $sub->subscriber = $this->id;
883 while ($sub->fetch()) {
884 $other = Profile::staticGet('id', $sub->subscribed);
888 if ($other->id == $this->id) {
891 Subscription::cancel($this, $other);
894 $subd = new Subscription();
895 $subd->subscribed = $this->id;
898 while ($subd->fetch()) {
899 $other = Profile::staticGet('id', $subd->subscriber);
903 if ($other->id == $this->id) {
906 Subscription::cancel($other, $this);
909 $self = new Subscription();
911 $self->subscriber = $this->id;
912 $self->subscribed = $this->id;
917 function _deleteMessages()
919 $msg = new Message();
920 $msg->from_profile = $this->id;
923 $msg = new Message();
924 $msg->to_profile = $this->id;
928 function _deleteTags()
930 $tag = new Profile_tag();
931 $tag->tagged = $this->id;
935 function _deleteBlocks()
937 $block = new Profile_block();
938 $block->blocked = $this->id;
941 $block = new Group_block();
942 $block->blocked = $this->id;
946 // XXX: identical to Notice::getLocation.
948 function getLocation()
952 if (!empty($this->location_id) && !empty($this->location_ns)) {
953 $location = Location::fromId($this->location_id, $this->location_ns);
956 if (is_null($location)) { // no ID, or Location::fromId() failed
957 if (!empty($this->lat) && !empty($this->lon)) {
958 $location = Location::fromLatLon($this->lat, $this->lon);
962 if (is_null($location)) { // still haven't found it!
963 if (!empty($this->location)) {
964 $location = Location::fromName($this->location);
971 function hasRole($name)
974 if (Event::handle('StartHasRole', array($this, $name, &$has_role))) {
975 $role = Profile_role::pkeyGet(array('profile_id' => $this->id,
977 $has_role = !empty($role);
978 Event::handle('EndHasRole', array($this, $name, $has_role));
983 function grantRole($name)
985 if (Event::handle('StartGrantRole', array($this, $name))) {
987 $role = new Profile_role();
989 $role->profile_id = $this->id;
991 $role->created = common_sql_now();
993 $result = $role->insert();
996 throw new Exception("Can't save role '$name' for profile '{$this->id}'");
999 if ($name == 'owner') {
1000 User::blow('user:site_owner');
1003 Event::handle('EndGrantRole', array($this, $name));
1009 function revokeRole($name)
1011 if (Event::handle('StartRevokeRole', array($this, $name))) {
1013 $role = Profile_role::pkeyGet(array('profile_id' => $this->id,
1017 // TRANS: Exception thrown when trying to revoke an existing role for a user that does not exist.
1018 // TRANS: %1$s is the role name, %2$s is the user ID (number).
1019 throw new Exception(sprintf(_('Cannot revoke role "%1$s" for user #%2$d; does not exist.'),$name, $this->id));
1022 $result = $role->delete();
1025 common_log_db_error($role, 'DELETE', __FILE__);
1026 // TRANS: Exception thrown when trying to revoke a role for a user with a failing database query.
1027 // TRANS: %1$s is the role name, %2$s is the user ID (number).
1028 throw new Exception(sprintf(_('Cannot revoke role "%1$s" for user #%2$d; database error.'),$name, $this->id));
1031 if ($name == 'owner') {
1032 User::blow('user:site_owner');
1035 Event::handle('EndRevokeRole', array($this, $name));
1041 function isSandboxed()
1043 return $this->hasRole(Profile_role::SANDBOXED);
1046 function isSilenced()
1048 return $this->hasRole(Profile_role::SILENCED);
1053 $this->grantRole(Profile_role::SANDBOXED);
1056 function unsandbox()
1058 $this->revokeRole(Profile_role::SANDBOXED);
1063 $this->grantRole(Profile_role::SILENCED);
1066 function unsilence()
1068 $this->revokeRole(Profile_role::SILENCED);
1072 * Does this user have the right to do X?
1074 * With our role-based authorization, this is merely a lookup for whether the user
1075 * has a particular role. The implementation currently uses a switch statement
1076 * to determine if the user has the pre-defined role to exercise the right. Future
1077 * implementations may allow per-site roles, and different mappings of roles to rights.
1079 * @param $right string Name of the right, usually a constant in class Right
1080 * @return boolean whether the user has the right in question
1082 function hasRight($right)
1086 if ($this->hasRole(Profile_role::DELETED)) {
1090 if (Event::handle('UserRightsCheck', array($this, $right, &$result))) {
1093 case Right::DELETEOTHERSNOTICE:
1094 case Right::MAKEGROUPADMIN:
1095 case Right::SANDBOXUSER:
1096 case Right::SILENCEUSER:
1097 case Right::DELETEUSER:
1098 case Right::DELETEGROUP:
1099 $result = $this->hasRole(Profile_role::MODERATOR);
1101 case Right::CONFIGURESITE:
1102 $result = $this->hasRole(Profile_role::ADMINISTRATOR);
1104 case Right::GRANTROLE:
1105 case Right::REVOKEROLE:
1106 $result = $this->hasRole(Profile_role::OWNER);
1108 case Right::NEWNOTICE:
1109 case Right::NEWMESSAGE:
1110 case Right::SUBSCRIBE:
1111 case Right::CREATEGROUP:
1112 $result = !$this->isSilenced();
1114 case Right::PUBLICNOTICE:
1115 case Right::EMAILONREPLY:
1116 case Right::EMAILONSUBSCRIBE:
1117 case Right::EMAILONFAVE:
1118 $result = !$this->isSandboxed();
1120 case Right::WEBLOGIN:
1121 $result = !$this->isSilenced();
1124 $result = !$this->isSilenced();
1126 case Right::BACKUPACCOUNT:
1127 $result = common_config('profile', 'backup');
1129 case Right::RESTOREACCOUNT:
1130 $result = common_config('profile', 'restore');
1132 case Right::DELETEACCOUNT:
1133 $result = common_config('profile', 'delete');
1135 case Right::MOVEACCOUNT:
1136 $result = common_config('profile', 'move');
1146 function hasRepeated($notice_id)
1148 // XXX: not really a pkey, but should work
1150 $notice = Memcached_DataObject::pkeyGet('Notice',
1151 array('profile_id' => $this->id,
1152 'repeat_of' => $notice_id));
1154 return !empty($notice);
1158 * Returns an XML string fragment with limited profile information
1159 * as an Atom <author> element.
1161 * Assumes that Atom has been previously set up as the base namespace.
1163 * @param Profile $cur the current authenticated user
1167 function asAtomAuthor($cur = null)
1169 $xs = new XMLStringer(true);
1171 $xs->elementStart('author');
1172 $xs->element('name', null, $this->nickname);
1173 $xs->element('uri', null, $this->getUri());
1176 $attrs['following'] = $cur->isSubscribed($this) ? 'true' : 'false';
1177 $attrs['blocking'] = $cur->hasBlocked($this) ? 'true' : 'false';
1178 $xs->element('statusnet:profile_info', $attrs, null);
1180 $xs->elementEnd('author');
1182 return $xs->getString();
1186 * Extra profile info for atom entries
1188 * Clients use some extra profile info in the atom stream.
1189 * This gives it to them.
1191 * @param User $cur Current user
1193 * @return array representation of <statusnet:profile_info> element or null
1196 function profileInfo($cur)
1198 $profileInfoAttr = array('local_id' => $this->id);
1201 // Whether the current user is a subscribed to this profile
1202 $profileInfoAttr['following'] = $cur->isSubscribed($this) ? 'true' : 'false';
1203 // Whether the current user is has blocked this profile
1204 $profileInfoAttr['blocking'] = $cur->hasBlocked($this) ? 'true' : 'false';
1207 return array('statusnet:profile_info', $profileInfoAttr, null);
1211 * Returns an XML string fragment with profile information as an
1212 * Activity Streams <activity:actor> element.
1214 * Assumes that 'activity' namespace has been previously defined.
1218 function asActivityActor()
1220 return $this->asActivityNoun('actor');
1224 * Returns an XML string fragment with profile information as an
1225 * Activity Streams noun object with the given element type.
1227 * Assumes that 'activity', 'georss', and 'poco' namespace has been
1228 * previously defined.
1230 * @param string $element one of 'actor', 'subject', 'object', 'target'
1234 function asActivityNoun($element)
1236 $noun = ActivityObject::fromProfile($this);
1237 return $noun->asString('activity:' . $element);
1241 * Returns the best URI for a profile. Plugins may override.
1243 * @return string $uri
1249 // give plugins a chance to set the URI
1250 if (Event::handle('StartGetProfileUri', array($this, &$uri))) {
1252 // check for a local user first
1253 $user = User::staticGet('id', $this->id);
1255 if (!empty($user)) {
1259 Event::handle('EndGetProfileUri', array($this, &$uri));
1265 function hasBlocked($other)
1267 $block = Profile_block::get($this->id, $other->id);
1269 if (empty($block)) {
1278 function getAtomFeed()
1282 if (Event::handle('StartProfileGetAtomFeed', array($this, &$feed))) {
1283 $user = User::staticGet('id', $this->id);
1284 if (!empty($user)) {
1285 $feed = common_local_url('ApiTimelineUser', array('id' => $user->id,
1286 'format' => 'atom'));
1288 Event::handle('EndProfileGetAtomFeed', array($this, $feed));
1294 static function fromURI($uri)
1298 if (Event::handle('StartGetProfileFromURI', array($uri, &$profile))) {
1299 // Get a local user or remote (OMB 0.1) profile
1300 $user = User::staticGet('uri', $uri);
1301 if (!empty($user)) {
1302 $profile = $user->getProfile();
1304 Event::handle('EndGetProfileFromURI', array($uri, $profile));
1310 function canRead(Notice $notice)
1312 if ($notice->scope & Notice::SITE_SCOPE) {
1313 $user = $this->getUser();
1319 if ($notice->scope & Notice::ADDRESSEE_SCOPE) {
1320 $replies = $notice->getReplies();
1322 if (!in_array($this->id, $replies)) {
1323 $groups = $notice->getGroups();
1327 foreach ($groups as $group) {
1328 if ($this->isMember($group)) {
1340 if ($notice->scope & Notice::FOLLOWER_SCOPE) {
1341 $author = $notice->getProfile();
1342 if (!Subscription::exists($this, $author)) {
1350 static function current()
1352 $user = common_current_user();
1356 $profile = $user->getProfile();
1362 * Magic function called at serialize() time.
1364 * We use this to drop a couple process-specific references
1365 * from DB_DataObject which can cause trouble in future
1368 * @return array of variable names to include in serialization.
1373 $vars = parent::__sleep();
1374 $skip = array('_user', '_avatars');
1375 return array_diff($vars, $skip);
1378 static function fillAvatars(&$profiles, $width)
1381 foreach ($profiles as $profile) {
1382 $ids[] = $profile->id;
1385 $avatars = Avatar::pivotGet('profile_id', $ids, array('width' => $width,
1386 'height' => $width));
1388 foreach ($profiles as $profile) {
1389 $profile->_fillAvatar($width, $avatars[$profile->id]);