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)) {
71 return Avatar::pkeyGet(array('profile_id' => $this->id,
73 'height' => $height));
76 function getOriginalAvatar()
78 $avatar = DB_DataObject::factory('avatar');
79 $avatar->profile_id = $this->id;
80 $avatar->original = true;
81 if ($avatar->find(true)) {
88 function setOriginal($filename)
90 $imagefile = new ImageFile($this->id, Avatar::path($filename));
92 $avatar = new Avatar();
93 $avatar->profile_id = $this->id;
94 $avatar->width = $imagefile->width;
95 $avatar->height = $imagefile->height;
96 $avatar->mediatype = image_type_to_mime_type($imagefile->type);
97 $avatar->filename = $filename;
98 $avatar->original = true;
99 $avatar->url = Avatar::url($filename);
100 $avatar->created = DB_DataObject_Cast::dateTime(); # current time
102 // XXX: start a transaction here
104 if (!$this->delete_avatars() || !$avatar->insert()) {
105 @unlink(Avatar::path($filename));
109 foreach (array(AVATAR_PROFILE_SIZE, AVATAR_STREAM_SIZE, AVATAR_MINI_SIZE) as $size) {
110 // We don't do a scaled one if original is our scaled size
111 if (!($avatar->width == $size && $avatar->height == $size)) {
112 $scaled_filename = $imagefile->resize($size);
114 //$scaled = DB_DataObject::factory('avatar');
115 $scaled = new Avatar();
116 $scaled->profile_id = $this->id;
117 $scaled->width = $size;
118 $scaled->height = $size;
119 $scaled->original = false;
120 $scaled->mediatype = image_type_to_mime_type($imagefile->type);
121 $scaled->filename = $scaled_filename;
122 $scaled->url = Avatar::url($scaled_filename);
123 $scaled->created = DB_DataObject_Cast::dateTime(); # current time
125 if (!$scaled->insert()) {
135 * Delete attached avatars for this user from the database and filesystem.
136 * This should be used instead of a batch delete() to ensure that files
137 * get removed correctly.
139 * @param boolean $original true to delete only the original-size file
142 function delete_avatars($original=true)
144 $avatar = new Avatar();
145 $avatar->profile_id = $this->id;
147 while ($avatar->fetch()) {
148 if ($avatar->original) {
149 if ($original == false) {
159 * Gets either the full name (if filled) or the nickname.
163 function getBestName()
165 return ($this->fullname) ? $this->fullname : $this->nickname;
169 * Gets the full name (if filled) with nickname as a parenthetical, or the nickname alone
170 * if no fullname is provided.
174 function getFancyName()
176 if ($this->fullname) {
177 // TRANS: Full name of a profile or group (%1$s) followed by nickname (%2$s) in parentheses.
178 return sprintf(_m('FANCYNAME','%1$s (%2$s)'), $this->fullname, $this->nickname);
180 return $this->nickname;
185 * Get the most recent notice posted by this user, if any.
187 * @return mixed Notice or null
189 function getCurrentNotice()
191 $notice = $this->getNotices(0, 1);
193 if ($notice->fetch()) {
194 if ($notice instanceof ArrayWrapper) {
195 // hack for things trying to work with single notices
196 return $notice->_items[0];
204 function getTaggedNotices($tag, $offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $max_id=0)
206 $stream = new TaggedProfileNoticeStream($this, $tag);
208 return $stream->getNotices($offset, $limit, $since_id, $max_id);
211 function getNotices($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $max_id=0)
213 $stream = new ProfileNoticeStream($this);
215 return $stream->getNotices($offset, $limit, $since_id, $max_id);
218 function isMember($group)
220 $gm = Group_member::pkeyGet(array('profile_id' => $this->id,
221 'group_id' => $group->id));
222 return (!empty($gm));
225 function isAdmin($group)
227 $gm = Group_member::pkeyGet(array('profile_id' => $this->id,
228 'group_id' => $group->id));
229 return (!empty($gm) && $gm->is_admin);
232 function isPendingMember($group)
234 $request = Group_join_queue::pkeyGet(array('profile_id' => $this->id,
235 'group_id' => $group->id));
236 return !empty($request);
239 function getGroups($offset=0, $limit=PROFILES_PER_PAGE)
243 $keypart = sprintf('profile:groups:%d', $this->id);
245 $idstring = self::cacheGet($keypart);
247 if ($idstring !== false) {
248 $ids = explode(',', $idstring);
250 $gm = new Group_member();
252 $gm->profile_id = $this->id;
255 while ($gm->fetch()) {
256 $ids[] = $gm->group_id;
260 self::cacheSet($keypart, implode(',', $ids));
265 foreach ($ids as $id) {
266 $group = User_group::staticGet('id', $id);
267 if (!empty($group)) {
272 return new ArrayWrapper($groups);
275 function isTagged($peopletag)
277 $tag = Profile_tag::pkeyGet(array('tagger' => $peopletag->tagger,
278 'tagged' => $this->id,
279 'tag' => $peopletag->tag));
283 function canTag($tagged)
285 if (empty($tagged)) {
289 if ($tagged->id == $this->id) {
293 $all = common_config('peopletag', 'allow_tagging', 'all');
294 $local = common_config('peopletag', 'allow_tagging', 'local');
295 $remote = common_config('peopletag', 'allow_tagging', 'remote');
296 $subs = common_config('peopletag', 'allow_tagging', 'subs');
302 $tagged_user = $tagged->getUser();
303 if (!empty($tagged_user)) {
308 return (Subscription::exists($this, $tagged) ||
309 Subscription::exists($tagged, $this));
310 } else if ($remote) {
316 function getOwnedTags($auth_user, $offset=0, $limit=null, $since_id=0, $max_id=0)
318 $tags = new Profile_list();
319 $tags->tagger = $this->id;
321 if (($auth_user instanceof User || $auth_user instanceof Profile) &&
322 $auth_user->id === $this->id) {
323 // no condition, get both private and public tags
325 $tags->private = false;
328 $tags->selectAdd('id as "cursor"');
331 $tags->whereAdd('id > '.$since_id);
335 $tags->whereAdd('id <= '.$max_id);
338 if($offset>=0 && !is_null($limit)) {
339 $tags->limit($offset, $limit);
342 $tags->orderBy('id DESC');
348 function getOtherTags($auth_user=null, $offset=0, $limit=null, $since_id=0, $max_id=0)
350 $lists = new Profile_list();
352 $tags = new Profile_tag();
353 $tags->tagged = $this->id;
355 $lists->joinAdd($tags);
356 #@fixme: postgres (round(date_part('epoch', my_date)))
357 $lists->selectAdd('unix_timestamp(profile_tag.modified) as "cursor"');
359 if ($auth_user instanceof User || $auth_user instanceof Profile) {
360 $lists->whereAdd('( ( profile_list.private = false ) ' .
361 'OR ( profile_list.tagger = ' . $auth_user->id . ' AND ' .
362 'profile_list.private = true ) )');
364 $lists->private = false;
368 $lists->whereAdd('cursor > '.$since_id);
372 $lists->whereAdd('cursor <= '.$max_id);
375 if($offset>=0 && !is_null($limit)) {
376 $lists->limit($offset, $limit);
379 $lists->orderBy('profile_tag.modified DESC');
385 function getPrivateTags($offset=0, $limit=null, $since_id=0, $max_id=0)
387 $tags = new Profile_list();
388 $tags->private = true;
389 $tags->tagger = $this->id;
392 $tags->whereAdd('id > '.$since_id);
396 $tags->whereAdd('id <= '.$max_id);
399 if($offset>=0 && !is_null($limit)) {
400 $tags->limit($offset, $limit);
403 $tags->orderBy('id DESC');
409 function hasLocalTags()
411 $tags = new Profile_tag();
413 $tags->joinAdd(array('tagger', 'user:id'));
414 $tags->whereAdd('tagged = '.$this->id);
415 $tags->whereAdd('tagger != '.$this->id);
420 return ($tags->N == 0) ? false : true;
423 function getTagSubscriptions($offset=0, $limit=null, $since_id=0, $max_id=0)
425 $lists = new Profile_list();
426 $subs = new Profile_tag_subscription();
428 $lists->joinAdd($subs);
429 #@fixme: postgres (round(date_part('epoch', my_date)))
430 $lists->selectAdd('unix_timestamp(profile_tag_subscription.created) as "cursor"');
432 $lists->whereAdd('profile_tag_subscription.profile_id = '.$this->id);
435 $lists->whereAdd('cursor > '.$since_id);
439 $lists->whereAdd('cursor <= '.$max_id);
442 if($offset>=0 && !is_null($limit)) {
443 $lists->limit($offset, $limit);
446 $lists->orderBy('"cursor" DESC');
453 * Request to join the given group.
454 * May throw exceptions on failure.
456 * @param User_group $group
457 * @return mixed: Group_member on success, Group_join_queue if pending approval, null on some cancels?
459 function joinGroup(User_group $group)
462 if ($group->join_policy == User_group::JOIN_POLICY_MODERATE) {
463 $join = Group_join_queue::saveNew($this, $group);
465 if (Event::handle('StartJoinGroup', array($group, $this))) {
466 $join = Group_member::join($group->id, $this->id);
467 self::blow('profile:groups:%d', $this->id);
468 Event::handle('EndJoinGroup', array($group, $this));
472 // Send any applicable notifications...
479 * Leave a group that this profile is a member of.
481 * @param User_group $group
483 function leaveGroup(User_group $group)
485 if (Event::handle('StartLeaveGroup', array($group, $this))) {
486 Group_member::leave($group->id, $this->id);
487 self::blow('profile:groups:%d', $this->id);
488 Event::handle('EndLeaveGroup', array($group, $this));
492 function avatarUrl($size=AVATAR_PROFILE_SIZE)
494 $avatar = $this->getAvatar($size);
496 return $avatar->displayUrl();
498 return Avatar::defaultImage($size);
502 function getSubscriptions($offset=0, $limit=null)
504 $subs = Subscription::bySubscriber($this->id,
510 while ($subs->fetch()) {
511 $profile = Profile::staticGet($subs->subscribed);
513 $profiles[] = $profile;
517 return new ArrayWrapper($profiles);
520 function getSubscribers($offset=0, $limit=null)
522 $subs = Subscription::bySubscribed($this->id,
528 while ($subs->fetch()) {
529 $profile = Profile::staticGet($subs->subscriber);
531 $profiles[] = $profile;
535 return new ArrayWrapper($profiles);
538 function getTaggedSubscribers($tag)
541 'SELECT profile.* ' .
542 'FROM profile JOIN (subscription, profile_tag, profile_list) ' .
543 'ON profile.id = subscription.subscriber ' .
544 'AND profile.id = profile_tag.tagged ' .
545 'AND profile_tag.tagger = profile_list.tagger AND profile_tag.tag = profile_list.tag ' .
546 'WHERE subscription.subscribed = %d ' .
547 'AND subscription.subscribed != subscription.subscriber ' .
548 'AND profile_tag.tagger = %d AND profile_tag.tag = "%s" ' .
549 'AND profile_list.private = false ' .
550 'ORDER BY subscription.created DESC';
552 $profile = new Profile();
555 $cnt = $profile->query(sprintf($qry, $this->id, $this->id, $tag));
557 while ($profile->fetch()) {
558 $tagged[] = clone($profile);
564 * Get pending subscribers, who have not yet been approved.
570 function getRequests($offset=0, $limit=null)
573 'SELECT profile.* ' .
574 'FROM profile JOIN subscription_queue '.
575 'ON profile.id = subscription_queue.subscriber ' .
576 'WHERE subscription_queue.subscribed = %d ' .
577 'ORDER BY subscription_queue.created DESC ';
579 if ($limit != null) {
580 if (common_config('db','type') == 'pgsql') {
581 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
583 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
587 $members = new Profile();
589 $members->query(sprintf($qry, $this->id));
593 function subscriptionCount()
595 $c = Cache::instance();
598 $cnt = $c->get(Cache::key('profile:subscription_count:'.$this->id));
599 if (is_integer($cnt)) {
604 $sub = new Subscription();
605 $sub->subscriber = $this->id;
607 $cnt = (int) $sub->count('distinct subscribed');
609 $cnt = ($cnt > 0) ? $cnt - 1 : $cnt;
612 $c->set(Cache::key('profile:subscription_count:'.$this->id), $cnt);
618 function subscriberCount()
620 $c = Cache::instance();
622 $cnt = $c->get(Cache::key('profile:subscriber_count:'.$this->id));
623 if (is_integer($cnt)) {
628 $sub = new Subscription();
629 $sub->subscribed = $this->id;
630 $sub->whereAdd('subscriber != subscribed');
631 $cnt = (int) $sub->count('distinct subscriber');
634 $c->set(Cache::key('profile:subscriber_count:'.$this->id), $cnt);
641 * Is this profile subscribed to another profile?
643 * @param Profile $other
646 function isSubscribed($other)
648 return Subscription::exists($this, $other);
652 * Check if a pending subscription request is outstanding for this...
654 * @param Profile $other
657 function hasPendingSubscription($other)
659 return Subscription_queue::exists($this, $other);
663 * Are these two profiles subscribed to each other?
665 * @param Profile $other
668 function mutuallySubscribed($other)
670 return $this->isSubscribed($other) &&
671 $other->isSubscribed($this);
674 function hasFave($notice)
676 $fave = Fave::pkeyGet(array('user_id' => $this->id,
677 'notice_id' => $notice->id));
678 return ((is_null($fave)) ? false : true);
683 $c = Cache::instance();
685 $cnt = $c->get(Cache::key('profile:fave_count:'.$this->id));
686 if (is_integer($cnt)) {
692 $faves->user_id = $this->id;
693 $cnt = (int) $faves->count('distinct notice_id');
696 $c->set(Cache::key('profile:fave_count:'.$this->id), $cnt);
702 function noticeCount()
704 $c = Cache::instance();
707 $cnt = $c->get(Cache::key('profile:notice_count:'.$this->id));
708 if (is_integer($cnt)) {
713 $notices = new Notice();
714 $notices->profile_id = $this->id;
715 $cnt = (int) $notices->count('distinct id');
718 $c->set(Cache::key('profile:notice_count:'.$this->id), $cnt);
724 function blowFavesCache()
726 $cache = Cache::instance();
728 // Faves don't happen chronologically, so we need to blow
730 $cache->delete(Cache::key('fave:ids_by_user:'.$this->id));
731 $cache->delete(Cache::key('fave:ids_by_user:'.$this->id.';last'));
732 $cache->delete(Cache::key('fave:ids_by_user_own:'.$this->id));
733 $cache->delete(Cache::key('fave:ids_by_user_own:'.$this->id.';last'));
735 $this->blowFaveCount();
738 function blowSubscriberCount()
740 $c = Cache::instance();
742 $c->delete(Cache::key('profile:subscriber_count:'.$this->id));
746 function blowSubscriptionCount()
748 $c = Cache::instance();
750 $c->delete(Cache::key('profile:subscription_count:'.$this->id));
754 function blowFaveCount()
756 $c = Cache::instance();
758 $c->delete(Cache::key('profile:fave_count:'.$this->id));
762 function blowNoticeCount()
764 $c = Cache::instance();
766 $c->delete(Cache::key('profile:notice_count:'.$this->id));
770 static function maxBio()
772 $biolimit = common_config('profile', 'biolimit');
773 // null => use global limit (distinct from 0!)
774 if (is_null($biolimit)) {
775 $biolimit = common_config('site', 'textlimit');
780 static function bioTooLong($bio)
782 $biolimit = self::maxBio();
783 return ($biolimit > 0 && !empty($bio) && (mb_strlen($bio) > $biolimit));
788 $this->_deleteNotices();
789 $this->_deleteSubscriptions();
790 $this->_deleteMessages();
791 $this->_deleteTags();
792 $this->_deleteBlocks();
793 $this->delete_avatars();
795 // Warning: delete() will run on the batch objects,
796 // not on individual objects.
797 $related = array('Reply',
800 Event::handle('ProfileDeleteRelated', array($this, &$related));
802 foreach ($related as $cls) {
804 $inst->profile_id = $this->id;
811 function _deleteNotices()
813 $notice = new Notice();
814 $notice->profile_id = $this->id;
816 if ($notice->find()) {
817 while ($notice->fetch()) {
818 $other = clone($notice);
824 function _deleteSubscriptions()
826 $sub = new Subscription();
827 $sub->subscriber = $this->id;
831 while ($sub->fetch()) {
832 $other = Profile::staticGet('id', $sub->subscribed);
836 if ($other->id == $this->id) {
839 Subscription::cancel($this, $other);
842 $subd = new Subscription();
843 $subd->subscribed = $this->id;
846 while ($subd->fetch()) {
847 $other = Profile::staticGet('id', $subd->subscriber);
851 if ($other->id == $this->id) {
854 Subscription::cancel($other, $this);
857 $self = new Subscription();
859 $self->subscriber = $this->id;
860 $self->subscribed = $this->id;
865 function _deleteMessages()
867 $msg = new Message();
868 $msg->from_profile = $this->id;
871 $msg = new Message();
872 $msg->to_profile = $this->id;
876 function _deleteTags()
878 $tag = new Profile_tag();
879 $tag->tagged = $this->id;
883 function _deleteBlocks()
885 $block = new Profile_block();
886 $block->blocked = $this->id;
889 $block = new Group_block();
890 $block->blocked = $this->id;
894 // XXX: identical to Notice::getLocation.
896 function getLocation()
900 if (!empty($this->location_id) && !empty($this->location_ns)) {
901 $location = Location::fromId($this->location_id, $this->location_ns);
904 if (is_null($location)) { // no ID, or Location::fromId() failed
905 if (!empty($this->lat) && !empty($this->lon)) {
906 $location = Location::fromLatLon($this->lat, $this->lon);
910 if (is_null($location)) { // still haven't found it!
911 if (!empty($this->location)) {
912 $location = Location::fromName($this->location);
919 function hasRole($name)
922 if (Event::handle('StartHasRole', array($this, $name, &$has_role))) {
923 $role = Profile_role::pkeyGet(array('profile_id' => $this->id,
925 $has_role = !empty($role);
926 Event::handle('EndHasRole', array($this, $name, $has_role));
931 function grantRole($name)
933 if (Event::handle('StartGrantRole', array($this, $name))) {
935 $role = new Profile_role();
937 $role->profile_id = $this->id;
939 $role->created = common_sql_now();
941 $result = $role->insert();
944 throw new Exception("Can't save role '$name' for profile '{$this->id}'");
947 if ($name == 'owner') {
948 User::blow('user:site_owner');
951 Event::handle('EndGrantRole', array($this, $name));
957 function revokeRole($name)
959 if (Event::handle('StartRevokeRole', array($this, $name))) {
961 $role = Profile_role::pkeyGet(array('profile_id' => $this->id,
965 // TRANS: Exception thrown when trying to revoke an existing role for a user that does not exist.
966 // TRANS: %1$s is the role name, %2$s is the user ID (number).
967 throw new Exception(sprintf(_('Cannot revoke role "%1$s" for user #%2$d; does not exist.'),$name, $this->id));
970 $result = $role->delete();
973 common_log_db_error($role, 'DELETE', __FILE__);
974 // TRANS: Exception thrown when trying to revoke a role for a user with a failing database query.
975 // TRANS: %1$s is the role name, %2$s is the user ID (number).
976 throw new Exception(sprintf(_('Cannot revoke role "%1$s" for user #%2$d; database error.'),$name, $this->id));
979 if ($name == 'owner') {
980 User::blow('user:site_owner');
983 Event::handle('EndRevokeRole', array($this, $name));
989 function isSandboxed()
991 return $this->hasRole(Profile_role::SANDBOXED);
994 function isSilenced()
996 return $this->hasRole(Profile_role::SILENCED);
1001 $this->grantRole(Profile_role::SANDBOXED);
1004 function unsandbox()
1006 $this->revokeRole(Profile_role::SANDBOXED);
1011 $this->grantRole(Profile_role::SILENCED);
1014 function unsilence()
1016 $this->revokeRole(Profile_role::SILENCED);
1020 * Does this user have the right to do X?
1022 * With our role-based authorization, this is merely a lookup for whether the user
1023 * has a particular role. The implementation currently uses a switch statement
1024 * to determine if the user has the pre-defined role to exercise the right. Future
1025 * implementations may allow per-site roles, and different mappings of roles to rights.
1027 * @param $right string Name of the right, usually a constant in class Right
1028 * @return boolean whether the user has the right in question
1030 function hasRight($right)
1034 if ($this->hasRole(Profile_role::DELETED)) {
1038 if (Event::handle('UserRightsCheck', array($this, $right, &$result))) {
1041 case Right::DELETEOTHERSNOTICE:
1042 case Right::MAKEGROUPADMIN:
1043 case Right::SANDBOXUSER:
1044 case Right::SILENCEUSER:
1045 case Right::DELETEUSER:
1046 case Right::DELETEGROUP:
1047 $result = $this->hasRole(Profile_role::MODERATOR);
1049 case Right::CONFIGURESITE:
1050 $result = $this->hasRole(Profile_role::ADMINISTRATOR);
1052 case Right::GRANTROLE:
1053 case Right::REVOKEROLE:
1054 $result = $this->hasRole(Profile_role::OWNER);
1056 case Right::NEWNOTICE:
1057 case Right::NEWMESSAGE:
1058 case Right::SUBSCRIBE:
1059 case Right::CREATEGROUP:
1060 $result = !$this->isSilenced();
1062 case Right::PUBLICNOTICE:
1063 case Right::EMAILONREPLY:
1064 case Right::EMAILONSUBSCRIBE:
1065 case Right::EMAILONFAVE:
1066 $result = !$this->isSandboxed();
1068 case Right::WEBLOGIN:
1069 $result = !$this->isSilenced();
1072 $result = !$this->isSilenced();
1074 case Right::BACKUPACCOUNT:
1075 $result = common_config('profile', 'backup');
1077 case Right::RESTOREACCOUNT:
1078 $result = common_config('profile', 'restore');
1080 case Right::DELETEACCOUNT:
1081 $result = common_config('profile', 'delete');
1083 case Right::MOVEACCOUNT:
1084 $result = common_config('profile', 'move');
1094 function hasRepeated($notice_id)
1096 // XXX: not really a pkey, but should work
1098 $notice = Memcached_DataObject::pkeyGet('Notice',
1099 array('profile_id' => $this->id,
1100 'repeat_of' => $notice_id));
1102 return !empty($notice);
1106 * Returns an XML string fragment with limited profile information
1107 * as an Atom <author> element.
1109 * Assumes that Atom has been previously set up as the base namespace.
1111 * @param Profile $cur the current authenticated user
1115 function asAtomAuthor($cur = null)
1117 $xs = new XMLStringer(true);
1119 $xs->elementStart('author');
1120 $xs->element('name', null, $this->nickname);
1121 $xs->element('uri', null, $this->getUri());
1124 $attrs['following'] = $cur->isSubscribed($this) ? 'true' : 'false';
1125 $attrs['blocking'] = $cur->hasBlocked($this) ? 'true' : 'false';
1126 $xs->element('statusnet:profile_info', $attrs, null);
1128 $xs->elementEnd('author');
1130 return $xs->getString();
1134 * Extra profile info for atom entries
1136 * Clients use some extra profile info in the atom stream.
1137 * This gives it to them.
1139 * @param User $cur Current user
1141 * @return array representation of <statusnet:profile_info> element or null
1144 function profileInfo($cur)
1146 $profileInfoAttr = array('local_id' => $this->id);
1149 // Whether the current user is a subscribed to this profile
1150 $profileInfoAttr['following'] = $cur->isSubscribed($this) ? 'true' : 'false';
1151 // Whether the current user is has blocked this profile
1152 $profileInfoAttr['blocking'] = $cur->hasBlocked($this) ? 'true' : 'false';
1155 return array('statusnet:profile_info', $profileInfoAttr, null);
1159 * Returns an XML string fragment with profile information as an
1160 * Activity Streams <activity:actor> element.
1162 * Assumes that 'activity' namespace has been previously defined.
1166 function asActivityActor()
1168 return $this->asActivityNoun('actor');
1172 * Returns an XML string fragment with profile information as an
1173 * Activity Streams noun object with the given element type.
1175 * Assumes that 'activity', 'georss', and 'poco' namespace has been
1176 * previously defined.
1178 * @param string $element one of 'actor', 'subject', 'object', 'target'
1182 function asActivityNoun($element)
1184 $noun = ActivityObject::fromProfile($this);
1185 return $noun->asString('activity:' . $element);
1189 * Returns the best URI for a profile. Plugins may override.
1191 * @return string $uri
1197 // give plugins a chance to set the URI
1198 if (Event::handle('StartGetProfileUri', array($this, &$uri))) {
1200 // check for a local user first
1201 $user = User::staticGet('id', $this->id);
1203 if (!empty($user)) {
1206 // return OMB profile if any
1207 $remote = Remote_profile::staticGet('id', $this->id);
1208 if (!empty($remote)) {
1209 $uri = $remote->uri;
1212 Event::handle('EndGetProfileUri', array($this, &$uri));
1218 function hasBlocked($other)
1220 $block = Profile_block::get($this->id, $other->id);
1222 if (empty($block)) {
1231 function getAtomFeed()
1235 if (Event::handle('StartProfileGetAtomFeed', array($this, &$feed))) {
1236 $user = User::staticGet('id', $this->id);
1237 if (!empty($user)) {
1238 $feed = common_local_url('ApiTimelineUser', array('id' => $user->id,
1239 'format' => 'atom'));
1241 Event::handle('EndProfileGetAtomFeed', array($this, $feed));
1247 static function fromURI($uri)
1251 if (Event::handle('StartGetProfileFromURI', array($uri, &$profile))) {
1252 // Get a local user or remote (OMB 0.1) profile
1253 $user = User::staticGet('uri', $uri);
1254 if (!empty($user)) {
1255 $profile = $user->getProfile();
1257 $remote_profile = Remote_profile::staticGet('uri', $uri);
1258 if (!empty($remote_profile)) {
1259 $profile = Profile::staticGet('id', $remote_profile->profile_id);
1262 Event::handle('EndGetProfileFromURI', array($uri, $profile));
1268 function canRead(Notice $notice)
1270 if ($notice->scope & Notice::SITE_SCOPE) {
1271 $user = $this->getUser();
1277 if ($notice->scope & Notice::ADDRESSEE_SCOPE) {
1278 $replies = $notice->getReplies();
1280 if (!in_array($this->id, $replies)) {
1281 $groups = $notice->getGroups();
1285 foreach ($groups as $group) {
1286 if ($this->isMember($group)) {
1298 if ($notice->scope & Notice::FOLLOWER_SCOPE) {
1299 $author = $notice->getProfile();
1300 if (!Subscription::exists($this, $author)) {