3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2008, 2009, 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 /* the code above is auto generated do not remove the tag below */
55 protected $_user = -1; // Uninitialized value distinct from null
59 if (is_int($this->_user) && $this->_user == -1) {
60 $this->_user = User::staticGet('id', $this->id);
66 function getAvatar($width, $height=null)
68 if (is_null($height)) {
74 if (Event::handle('StartProfileGetAvatar', array($this, $width, &$avatar))) {
75 $avatar = Avatar::pkeyGet(array('profile_id' => $this->id,
77 'height' => $height));
78 Event::handle('EndProfileGetAvatar', array($this, $width, &$avatar));
84 function getOriginalAvatar()
86 $avatar = DB_DataObject::factory('avatar');
87 $avatar->profile_id = $this->id;
88 $avatar->original = true;
89 if ($avatar->find(true)) {
96 function setOriginal($filename)
98 $imagefile = new ImageFile($this->id, Avatar::path($filename));
100 $avatar = new Avatar();
101 $avatar->profile_id = $this->id;
102 $avatar->width = $imagefile->width;
103 $avatar->height = $imagefile->height;
104 $avatar->mediatype = image_type_to_mime_type($imagefile->type);
105 $avatar->filename = $filename;
106 $avatar->original = true;
107 $avatar->url = Avatar::url($filename);
108 $avatar->created = DB_DataObject_Cast::dateTime(); # current time
110 // XXX: start a transaction here
112 if (!$this->delete_avatars() || !$avatar->insert()) {
113 @unlink(Avatar::path($filename));
117 foreach (array(AVATAR_PROFILE_SIZE, AVATAR_STREAM_SIZE, AVATAR_MINI_SIZE) as $size) {
118 // We don't do a scaled one if original is our scaled size
119 if (!($avatar->width == $size && $avatar->height == $size)) {
120 $scaled_filename = $imagefile->resize($size);
122 //$scaled = DB_DataObject::factory('avatar');
123 $scaled = new Avatar();
124 $scaled->profile_id = $this->id;
125 $scaled->width = $size;
126 $scaled->height = $size;
127 $scaled->original = false;
128 $scaled->mediatype = image_type_to_mime_type($imagefile->type);
129 $scaled->filename = $scaled_filename;
130 $scaled->url = Avatar::url($scaled_filename);
131 $scaled->created = DB_DataObject_Cast::dateTime(); # current time
133 if (!$scaled->insert()) {
143 * Delete attached avatars for this user from the database and filesystem.
144 * This should be used instead of a batch delete() to ensure that files
145 * get removed correctly.
147 * @param boolean $original true to delete only the original-size file
150 function delete_avatars($original=true)
152 $avatar = new Avatar();
153 $avatar->profile_id = $this->id;
155 while ($avatar->fetch()) {
156 if ($avatar->original) {
157 if ($original == false) {
167 * Gets either the full name (if filled) or the nickname.
171 function getBestName()
173 return ($this->fullname) ? $this->fullname : $this->nickname;
177 * Gets the full name (if filled) with nickname as a parenthetical, or the nickname alone
178 * if no fullname is provided.
182 function getFancyName()
184 if ($this->fullname) {
185 // TRANS: Full name of a profile or group (%1$s) followed by nickname (%2$s) in parentheses.
186 return sprintf(_m('FANCYNAME','%1$s (%2$s)'), $this->fullname, $this->nickname);
188 return $this->nickname;
193 * Get the most recent notice posted by this user, if any.
195 * @return mixed Notice or null
197 function getCurrentNotice()
199 $notice = $this->getNotices(0, 1);
201 if ($notice->fetch()) {
202 if ($notice instanceof ArrayWrapper) {
203 // hack for things trying to work with single notices
204 return $notice->_items[0];
212 function getTaggedNotices($tag, $offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $max_id=0)
214 $stream = new TaggedProfileNoticeStream($this, $tag);
216 return $stream->getNotices($offset, $limit, $since_id, $max_id);
219 function getNotices($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $max_id=0)
221 $stream = new ProfileNoticeStream($this);
223 return $stream->getNotices($offset, $limit, $since_id, $max_id);
226 function isMember($group)
228 $gm = Group_member::pkeyGet(array('profile_id' => $this->id,
229 'group_id' => $group->id));
230 return (!empty($gm));
233 function isAdmin($group)
235 $gm = Group_member::pkeyGet(array('profile_id' => $this->id,
236 'group_id' => $group->id));
237 return (!empty($gm) && $gm->is_admin);
240 function isPendingMember($group)
242 $request = Group_join_queue::pkeyGet(array('profile_id' => $this->id,
243 'group_id' => $group->id));
244 return !empty($request);
247 function getGroups($offset=0, $limit=PROFILES_PER_PAGE)
251 $keypart = sprintf('profile:groups:%d', $this->id);
253 $idstring = self::cacheGet($keypart);
255 if ($idstring !== false) {
256 $ids = explode(',', $idstring);
258 $gm = new Group_member();
260 $gm->profile_id = $this->id;
263 while ($gm->fetch()) {
264 $ids[] = $gm->group_id;
268 self::cacheSet($keypart, implode(',', $ids));
273 foreach ($ids as $id) {
274 $group = User_group::staticGet('id', $id);
275 if (!empty($group)) {
280 return new ArrayWrapper($groups);
283 function isTagged($peopletag)
285 $tag = Profile_tag::pkeyGet(array('tagger' => $peopletag->tagger,
286 'tagged' => $this->id,
287 'tag' => $peopletag->tag));
291 function canTag($tagged)
293 if (empty($tagged)) {
297 if ($tagged->id == $this->id) {
301 $all = common_config('peopletag', 'allow_tagging', 'all');
302 $local = common_config('peopletag', 'allow_tagging', 'local');
303 $remote = common_config('peopletag', 'allow_tagging', 'remote');
304 $subs = common_config('peopletag', 'allow_tagging', 'subs');
310 $tagged_user = $tagged->getUser();
311 if (!empty($tagged_user)) {
316 return (Subscription::exists($this, $tagged) ||
317 Subscription::exists($tagged, $this));
318 } else if ($remote) {
324 function getOwnedTags($auth_user, $offset=0, $limit=null, $since_id=0, $max_id=0)
326 $tags = new Profile_list();
327 $tags->tagger = $this->id;
329 if (($auth_user instanceof User || $auth_user instanceof Profile) &&
330 $auth_user->id === $this->id) {
331 // no condition, get both private and public tags
333 $tags->private = false;
336 $tags->selectAdd('id as "cursor"');
339 $tags->whereAdd('id > '.$since_id);
343 $tags->whereAdd('id <= '.$max_id);
346 if($offset>=0 && !is_null($limit)) {
347 $tags->limit($offset, $limit);
350 $tags->orderBy('id DESC');
356 function getOtherTags($auth_user=null, $offset=0, $limit=null, $since_id=0, $max_id=0)
358 $lists = new Profile_list();
360 $tags = new Profile_tag();
361 $tags->tagged = $this->id;
363 $lists->joinAdd($tags);
364 #@fixme: postgres (round(date_part('epoch', my_date)))
365 $lists->selectAdd('unix_timestamp(profile_tag.modified) as "cursor"');
367 if ($auth_user instanceof User || $auth_user instanceof Profile) {
368 $lists->whereAdd('( ( profile_list.private = false ) ' .
369 'OR ( profile_list.tagger = ' . $auth_user->id . ' AND ' .
370 'profile_list.private = true ) )');
372 $lists->private = false;
376 $lists->whereAdd('cursor > '.$since_id);
380 $lists->whereAdd('cursor <= '.$max_id);
383 if($offset>=0 && !is_null($limit)) {
384 $lists->limit($offset, $limit);
387 $lists->orderBy('profile_tag.modified DESC');
393 function getPrivateTags($offset=0, $limit=null, $since_id=0, $max_id=0)
395 $tags = new Profile_list();
396 $tags->private = true;
397 $tags->tagger = $this->id;
400 $tags->whereAdd('id > '.$since_id);
404 $tags->whereAdd('id <= '.$max_id);
407 if($offset>=0 && !is_null($limit)) {
408 $tags->limit($offset, $limit);
411 $tags->orderBy('id DESC');
417 function hasLocalTags()
419 $tags = new Profile_tag();
421 $tags->joinAdd(array('tagger', 'user:id'));
422 $tags->whereAdd('tagged = '.$this->id);
423 $tags->whereAdd('tagger != '.$this->id);
428 return ($tags->N == 0) ? false : true;
431 function getTagSubscriptions($offset=0, $limit=null, $since_id=0, $max_id=0)
433 $lists = new Profile_list();
434 $subs = new Profile_tag_subscription();
436 $lists->joinAdd($subs);
437 #@fixme: postgres (round(date_part('epoch', my_date)))
438 $lists->selectAdd('unix_timestamp(profile_tag_subscription.created) as "cursor"');
440 $lists->whereAdd('profile_tag_subscription.profile_id = '.$this->id);
443 $lists->whereAdd('cursor > '.$since_id);
447 $lists->whereAdd('cursor <= '.$max_id);
450 if($offset>=0 && !is_null($limit)) {
451 $lists->limit($offset, $limit);
454 $lists->orderBy('"cursor" DESC');
461 * Request to join the given group.
462 * May throw exceptions on failure.
464 * @param User_group $group
465 * @return mixed: Group_member on success, Group_join_queue if pending approval, null on some cancels?
467 function joinGroup(User_group $group)
470 if ($group->join_policy == User_group::JOIN_POLICY_MODERATE) {
471 $join = Group_join_queue::saveNew($this, $group);
473 if (Event::handle('StartJoinGroup', array($group, $this))) {
474 $join = Group_member::join($group->id, $this->id);
475 self::blow('profile:groups:%d', $this->id);
476 Event::handle('EndJoinGroup', array($group, $this));
480 // Send any applicable notifications...
487 * Leave a group that this profile is a member of.
489 * @param User_group $group
491 function leaveGroup(User_group $group)
493 if (Event::handle('StartLeaveGroup', array($group, $this))) {
494 Group_member::leave($group->id, $this->id);
495 self::blow('profile:groups:%d', $this->id);
496 Event::handle('EndLeaveGroup', array($group, $this));
500 function avatarUrl($size=AVATAR_PROFILE_SIZE)
502 $avatar = $this->getAvatar($size);
504 return $avatar->displayUrl();
506 return Avatar::defaultImage($size);
510 function getSubscriptions($offset=0, $limit=null)
512 $subs = Subscription::bySubscriber($this->id,
518 while ($subs->fetch()) {
519 $profile = Profile::staticGet($subs->subscribed);
521 $profiles[] = $profile;
525 return new ArrayWrapper($profiles);
528 function getSubscribers($offset=0, $limit=null)
530 $subs = Subscription::bySubscribed($this->id,
536 while ($subs->fetch()) {
537 $profile = Profile::staticGet($subs->subscriber);
539 $profiles[] = $profile;
543 return new ArrayWrapper($profiles);
546 function getTaggedSubscribers($tag)
549 'SELECT profile.* ' .
550 'FROM profile JOIN (subscription, profile_tag, profile_list) ' .
551 'ON profile.id = subscription.subscriber ' .
552 'AND profile.id = profile_tag.tagged ' .
553 'AND profile_tag.tagger = profile_list.tagger AND profile_tag.tag = profile_list.tag ' .
554 'WHERE subscription.subscribed = %d ' .
555 'AND subscription.subscribed != subscription.subscriber ' .
556 'AND profile_tag.tagger = %d AND profile_tag.tag = "%s" ' .
557 'AND profile_list.private = false ' .
558 'ORDER BY subscription.created DESC';
560 $profile = new Profile();
563 $cnt = $profile->query(sprintf($qry, $this->id, $this->id, $tag));
565 while ($profile->fetch()) {
566 $tagged[] = clone($profile);
572 * Get pending subscribers, who have not yet been approved.
578 function getRequests($offset=0, $limit=null)
581 'SELECT profile.* ' .
582 'FROM profile JOIN subscription_queue '.
583 'ON profile.id = subscription_queue.subscriber ' .
584 'WHERE subscription_queue.subscribed = %d ' .
585 'ORDER BY subscription_queue.created DESC ';
587 if ($limit != null) {
588 if (common_config('db','type') == 'pgsql') {
589 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
591 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
595 $members = new Profile();
597 $members->query(sprintf($qry, $this->id));
601 function subscriptionCount()
603 $c = Cache::instance();
606 $cnt = $c->get(Cache::key('profile:subscription_count:'.$this->id));
607 if (is_integer($cnt)) {
612 $sub = new Subscription();
613 $sub->subscriber = $this->id;
615 $cnt = (int) $sub->count('distinct subscribed');
617 $cnt = ($cnt > 0) ? $cnt - 1 : $cnt;
620 $c->set(Cache::key('profile:subscription_count:'.$this->id), $cnt);
626 function subscriberCount()
628 $c = Cache::instance();
630 $cnt = $c->get(Cache::key('profile:subscriber_count:'.$this->id));
631 if (is_integer($cnt)) {
636 $sub = new Subscription();
637 $sub->subscribed = $this->id;
638 $sub->whereAdd('subscriber != subscribed');
639 $cnt = (int) $sub->count('distinct subscriber');
642 $c->set(Cache::key('profile:subscriber_count:'.$this->id), $cnt);
649 * Is this profile subscribed to another profile?
651 * @param Profile $other
654 function isSubscribed($other)
656 return Subscription::exists($this, $other);
660 * Check if a pending subscription request is outstanding for this...
662 * @param Profile $other
665 function hasPendingSubscription($other)
667 return Subscription_queue::exists($this, $other);
671 * Are these two profiles subscribed to each other?
673 * @param Profile $other
676 function mutuallySubscribed($other)
678 return $this->isSubscribed($other) &&
679 $other->isSubscribed($this);
682 function hasFave($notice)
684 $fave = Fave::pkeyGet(array('user_id' => $this->id,
685 'notice_id' => $notice->id));
686 return ((is_null($fave)) ? false : true);
691 $c = Cache::instance();
693 $cnt = $c->get(Cache::key('profile:fave_count:'.$this->id));
694 if (is_integer($cnt)) {
700 $faves->user_id = $this->id;
701 $cnt = (int) $faves->count('distinct notice_id');
704 $c->set(Cache::key('profile:fave_count:'.$this->id), $cnt);
710 function noticeCount()
712 $c = Cache::instance();
715 $cnt = $c->get(Cache::key('profile:notice_count:'.$this->id));
716 if (is_integer($cnt)) {
721 $notices = new Notice();
722 $notices->profile_id = $this->id;
723 $cnt = (int) $notices->count('distinct id');
726 $c->set(Cache::key('profile:notice_count:'.$this->id), $cnt);
732 function blowFavesCache()
734 $cache = Cache::instance();
736 // Faves don't happen chronologically, so we need to blow
738 $cache->delete(Cache::key('fave:ids_by_user:'.$this->id));
739 $cache->delete(Cache::key('fave:ids_by_user:'.$this->id.';last'));
740 $cache->delete(Cache::key('fave:ids_by_user_own:'.$this->id));
741 $cache->delete(Cache::key('fave:ids_by_user_own:'.$this->id.';last'));
743 $this->blowFaveCount();
746 function blowSubscriberCount()
748 $c = Cache::instance();
750 $c->delete(Cache::key('profile:subscriber_count:'.$this->id));
754 function blowSubscriptionCount()
756 $c = Cache::instance();
758 $c->delete(Cache::key('profile:subscription_count:'.$this->id));
762 function blowFaveCount()
764 $c = Cache::instance();
766 $c->delete(Cache::key('profile:fave_count:'.$this->id));
770 function blowNoticeCount()
772 $c = Cache::instance();
774 $c->delete(Cache::key('profile:notice_count:'.$this->id));
778 static function maxBio()
780 $biolimit = common_config('profile', 'biolimit');
781 // null => use global limit (distinct from 0!)
782 if (is_null($biolimit)) {
783 $biolimit = common_config('site', 'textlimit');
788 static function bioTooLong($bio)
790 $biolimit = self::maxBio();
791 return ($biolimit > 0 && !empty($bio) && (mb_strlen($bio) > $biolimit));
796 $this->_deleteNotices();
797 $this->_deleteSubscriptions();
798 $this->_deleteMessages();
799 $this->_deleteTags();
800 $this->_deleteBlocks();
801 $this->delete_avatars();
803 // Warning: delete() will run on the batch objects,
804 // not on individual objects.
805 $related = array('Reply',
808 Event::handle('ProfileDeleteRelated', array($this, &$related));
810 foreach ($related as $cls) {
812 $inst->profile_id = $this->id;
819 function _deleteNotices()
821 $notice = new Notice();
822 $notice->profile_id = $this->id;
824 if ($notice->find()) {
825 while ($notice->fetch()) {
826 $other = clone($notice);
832 function _deleteSubscriptions()
834 $sub = new Subscription();
835 $sub->subscriber = $this->id;
839 while ($sub->fetch()) {
840 $other = Profile::staticGet('id', $sub->subscribed);
844 if ($other->id == $this->id) {
847 Subscription::cancel($this, $other);
850 $subd = new Subscription();
851 $subd->subscribed = $this->id;
854 while ($subd->fetch()) {
855 $other = Profile::staticGet('id', $subd->subscriber);
859 if ($other->id == $this->id) {
862 Subscription::cancel($other, $this);
865 $self = new Subscription();
867 $self->subscriber = $this->id;
868 $self->subscribed = $this->id;
873 function _deleteMessages()
875 $msg = new Message();
876 $msg->from_profile = $this->id;
879 $msg = new Message();
880 $msg->to_profile = $this->id;
884 function _deleteTags()
886 $tag = new Profile_tag();
887 $tag->tagged = $this->id;
891 function _deleteBlocks()
893 $block = new Profile_block();
894 $block->blocked = $this->id;
897 $block = new Group_block();
898 $block->blocked = $this->id;
902 // XXX: identical to Notice::getLocation.
904 function getLocation()
908 if (!empty($this->location_id) && !empty($this->location_ns)) {
909 $location = Location::fromId($this->location_id, $this->location_ns);
912 if (is_null($location)) { // no ID, or Location::fromId() failed
913 if (!empty($this->lat) && !empty($this->lon)) {
914 $location = Location::fromLatLon($this->lat, $this->lon);
918 if (is_null($location)) { // still haven't found it!
919 if (!empty($this->location)) {
920 $location = Location::fromName($this->location);
927 function hasRole($name)
930 if (Event::handle('StartHasRole', array($this, $name, &$has_role))) {
931 $role = Profile_role::pkeyGet(array('profile_id' => $this->id,
933 $has_role = !empty($role);
934 Event::handle('EndHasRole', array($this, $name, $has_role));
939 function grantRole($name)
941 if (Event::handle('StartGrantRole', array($this, $name))) {
943 $role = new Profile_role();
945 $role->profile_id = $this->id;
947 $role->created = common_sql_now();
949 $result = $role->insert();
952 throw new Exception("Can't save role '$name' for profile '{$this->id}'");
955 if ($name == 'owner') {
956 User::blow('user:site_owner');
959 Event::handle('EndGrantRole', array($this, $name));
965 function revokeRole($name)
967 if (Event::handle('StartRevokeRole', array($this, $name))) {
969 $role = Profile_role::pkeyGet(array('profile_id' => $this->id,
973 // TRANS: Exception thrown when trying to revoke an existing role for a user that does not exist.
974 // TRANS: %1$s is the role name, %2$s is the user ID (number).
975 throw new Exception(sprintf(_('Cannot revoke role "%1$s" for user #%2$d; does not exist.'),$name, $this->id));
978 $result = $role->delete();
981 common_log_db_error($role, 'DELETE', __FILE__);
982 // TRANS: Exception thrown when trying to revoke a role for a user with a failing database query.
983 // TRANS: %1$s is the role name, %2$s is the user ID (number).
984 throw new Exception(sprintf(_('Cannot revoke role "%1$s" for user #%2$d; database error.'),$name, $this->id));
987 if ($name == 'owner') {
988 User::blow('user:site_owner');
991 Event::handle('EndRevokeRole', array($this, $name));
997 function isSandboxed()
999 return $this->hasRole(Profile_role::SANDBOXED);
1002 function isSilenced()
1004 return $this->hasRole(Profile_role::SILENCED);
1009 $this->grantRole(Profile_role::SANDBOXED);
1012 function unsandbox()
1014 $this->revokeRole(Profile_role::SANDBOXED);
1019 $this->grantRole(Profile_role::SILENCED);
1022 function unsilence()
1024 $this->revokeRole(Profile_role::SILENCED);
1028 * Does this user have the right to do X?
1030 * With our role-based authorization, this is merely a lookup for whether the user
1031 * has a particular role. The implementation currently uses a switch statement
1032 * to determine if the user has the pre-defined role to exercise the right. Future
1033 * implementations may allow per-site roles, and different mappings of roles to rights.
1035 * @param $right string Name of the right, usually a constant in class Right
1036 * @return boolean whether the user has the right in question
1038 function hasRight($right)
1042 if ($this->hasRole(Profile_role::DELETED)) {
1046 if (Event::handle('UserRightsCheck', array($this, $right, &$result))) {
1049 case Right::DELETEOTHERSNOTICE:
1050 case Right::MAKEGROUPADMIN:
1051 case Right::SANDBOXUSER:
1052 case Right::SILENCEUSER:
1053 case Right::DELETEUSER:
1054 case Right::DELETEGROUP:
1055 $result = $this->hasRole(Profile_role::MODERATOR);
1057 case Right::CONFIGURESITE:
1058 $result = $this->hasRole(Profile_role::ADMINISTRATOR);
1060 case Right::GRANTROLE:
1061 case Right::REVOKEROLE:
1062 $result = $this->hasRole(Profile_role::OWNER);
1064 case Right::NEWNOTICE:
1065 case Right::NEWMESSAGE:
1066 case Right::SUBSCRIBE:
1067 case Right::CREATEGROUP:
1068 $result = !$this->isSilenced();
1070 case Right::PUBLICNOTICE:
1071 case Right::EMAILONREPLY:
1072 case Right::EMAILONSUBSCRIBE:
1073 case Right::EMAILONFAVE:
1074 $result = !$this->isSandboxed();
1076 case Right::WEBLOGIN:
1077 $result = !$this->isSilenced();
1080 $result = !$this->isSilenced();
1082 case Right::BACKUPACCOUNT:
1083 $result = common_config('profile', 'backup');
1085 case Right::RESTOREACCOUNT:
1086 $result = common_config('profile', 'restore');
1088 case Right::DELETEACCOUNT:
1089 $result = common_config('profile', 'delete');
1091 case Right::MOVEACCOUNT:
1092 $result = common_config('profile', 'move');
1102 function hasRepeated($notice_id)
1104 // XXX: not really a pkey, but should work
1106 $notice = Memcached_DataObject::pkeyGet('Notice',
1107 array('profile_id' => $this->id,
1108 'repeat_of' => $notice_id));
1110 return !empty($notice);
1114 * Returns an XML string fragment with limited profile information
1115 * as an Atom <author> element.
1117 * Assumes that Atom has been previously set up as the base namespace.
1119 * @param Profile $cur the current authenticated user
1123 function asAtomAuthor($cur = null)
1125 $xs = new XMLStringer(true);
1127 $xs->elementStart('author');
1128 $xs->element('name', null, $this->nickname);
1129 $xs->element('uri', null, $this->getUri());
1132 $attrs['following'] = $cur->isSubscribed($this) ? 'true' : 'false';
1133 $attrs['blocking'] = $cur->hasBlocked($this) ? 'true' : 'false';
1134 $xs->element('statusnet:profile_info', $attrs, null);
1136 $xs->elementEnd('author');
1138 return $xs->getString();
1142 * Extra profile info for atom entries
1144 * Clients use some extra profile info in the atom stream.
1145 * This gives it to them.
1147 * @param User $cur Current user
1149 * @return array representation of <statusnet:profile_info> element or null
1152 function profileInfo($cur)
1154 $profileInfoAttr = array('local_id' => $this->id);
1157 // Whether the current user is a subscribed to this profile
1158 $profileInfoAttr['following'] = $cur->isSubscribed($this) ? 'true' : 'false';
1159 // Whether the current user is has blocked this profile
1160 $profileInfoAttr['blocking'] = $cur->hasBlocked($this) ? 'true' : 'false';
1163 return array('statusnet:profile_info', $profileInfoAttr, null);
1167 * Returns an XML string fragment with profile information as an
1168 * Activity Streams <activity:actor> element.
1170 * Assumes that 'activity' namespace has been previously defined.
1174 function asActivityActor()
1176 return $this->asActivityNoun('actor');
1180 * Returns an XML string fragment with profile information as an
1181 * Activity Streams noun object with the given element type.
1183 * Assumes that 'activity', 'georss', and 'poco' namespace has been
1184 * previously defined.
1186 * @param string $element one of 'actor', 'subject', 'object', 'target'
1190 function asActivityNoun($element)
1192 $noun = ActivityObject::fromProfile($this);
1193 return $noun->asString('activity:' . $element);
1197 * Returns the best URI for a profile. Plugins may override.
1199 * @return string $uri
1205 // give plugins a chance to set the URI
1206 if (Event::handle('StartGetProfileUri', array($this, &$uri))) {
1208 // check for a local user first
1209 $user = User::staticGet('id', $this->id);
1211 if (!empty($user)) {
1214 // return OMB profile if any
1215 $remote = Remote_profile::staticGet('id', $this->id);
1216 if (!empty($remote)) {
1217 $uri = $remote->uri;
1220 Event::handle('EndGetProfileUri', array($this, &$uri));
1226 function hasBlocked($other)
1228 $block = Profile_block::get($this->id, $other->id);
1230 if (empty($block)) {
1239 function getAtomFeed()
1243 if (Event::handle('StartProfileGetAtomFeed', array($this, &$feed))) {
1244 $user = User::staticGet('id', $this->id);
1245 if (!empty($user)) {
1246 $feed = common_local_url('ApiTimelineUser', array('id' => $user->id,
1247 'format' => 'atom'));
1249 Event::handle('EndProfileGetAtomFeed', array($this, $feed));
1255 static function fromURI($uri)
1259 if (Event::handle('StartGetProfileFromURI', array($uri, &$profile))) {
1260 // Get a local user or remote (OMB 0.1) profile
1261 $user = User::staticGet('uri', $uri);
1262 if (!empty($user)) {
1263 $profile = $user->getProfile();
1265 $remote_profile = Remote_profile::staticGet('uri', $uri);
1266 if (!empty($remote_profile)) {
1267 $profile = Profile::staticGet('id', $remote_profile->profile_id);
1270 Event::handle('EndGetProfileFromURI', array($uri, $profile));
1276 function canRead(Notice $notice)
1278 if ($notice->scope & Notice::SITE_SCOPE) {
1279 $user = $this->getUser();
1285 if ($notice->scope & Notice::ADDRESSEE_SCOPE) {
1286 $replies = $notice->getReplies();
1288 if (!in_array($this->id, $replies)) {
1289 $groups = $notice->getGroups();
1293 foreach ($groups as $group) {
1294 if ($this->isMember($group)) {
1306 if ($notice->scope & Notice::FOLLOWER_SCOPE) {
1307 $author = $notice->getProfile();
1308 if (!Subscription::exists($this, $author)) {
1316 static function current()
1318 $user = common_current_user();
1322 $profile = $user->getProfile();
1331 $keypart = sprintf('profile:lists:%d', $this->id);
1333 $idstr = self::cacheGet($keypart);
1335 if ($idstr !== false) {
1336 $ids = explode(',', $idstr);
1338 $list = new Profile_list();
1340 $list->selectAdd('id');
1341 $list->tagger = $this->id;
1343 if ($list->find()) {
1344 while ($list->fetch()) {
1349 self::cacheSet($keypart, implode(',', $ids));
1354 foreach ($ids as $id) {
1355 $list = Profile_list::staticGet('id', $id);
1356 if (!empty($list) &&
1357 ($showPrivate || !$list->private)) {
1362 return new ArrayWrapper($lists);