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 function getAvatar($width, $height=null)
73 if (is_null($height)) {
79 if (Event::handle('StartProfileGetAvatar', array($this, $width, &$avatar))) {
80 $avatar = Avatar::pkeyGet(array('profile_id' => $this->id,
82 'height' => $height));
83 Event::handle('EndProfileGetAvatar', array($this, $width, &$avatar));
89 function getOriginalAvatar()
91 $avatar = DB_DataObject::factory('avatar');
92 $avatar->profile_id = $this->id;
93 $avatar->original = true;
94 if ($avatar->find(true)) {
101 function setOriginal($filename)
103 $imagefile = new ImageFile($this->id, Avatar::path($filename));
105 $avatar = new Avatar();
106 $avatar->profile_id = $this->id;
107 $avatar->width = $imagefile->width;
108 $avatar->height = $imagefile->height;
109 $avatar->mediatype = image_type_to_mime_type($imagefile->type);
110 $avatar->filename = $filename;
111 $avatar->original = true;
112 $avatar->url = Avatar::url($filename);
113 $avatar->created = DB_DataObject_Cast::dateTime(); # current time
115 // XXX: start a transaction here
117 if (!$this->delete_avatars() || !$avatar->insert()) {
118 @unlink(Avatar::path($filename));
122 foreach (array(AVATAR_PROFILE_SIZE, AVATAR_STREAM_SIZE, AVATAR_MINI_SIZE) as $size) {
123 // We don't do a scaled one if original is our scaled size
124 if (!($avatar->width == $size && $avatar->height == $size)) {
125 $scaled_filename = $imagefile->resize($size);
127 //$scaled = DB_DataObject::factory('avatar');
128 $scaled = new Avatar();
129 $scaled->profile_id = $this->id;
130 $scaled->width = $size;
131 $scaled->height = $size;
132 $scaled->original = false;
133 $scaled->mediatype = image_type_to_mime_type($imagefile->type);
134 $scaled->filename = $scaled_filename;
135 $scaled->url = Avatar::url($scaled_filename);
136 $scaled->created = DB_DataObject_Cast::dateTime(); # current time
138 if (!$scaled->insert()) {
148 * Delete attached avatars for this user from the database and filesystem.
149 * This should be used instead of a batch delete() to ensure that files
150 * get removed correctly.
152 * @param boolean $original true to delete only the original-size file
155 function delete_avatars($original=true)
157 $avatar = new Avatar();
158 $avatar->profile_id = $this->id;
160 while ($avatar->fetch()) {
161 if ($avatar->original) {
162 if ($original == false) {
172 * Gets either the full name (if filled) or the nickname.
176 function getBestName()
178 return ($this->fullname) ? $this->fullname : $this->nickname;
182 * Gets the full name (if filled) with nickname as a parenthetical, or the nickname alone
183 * if no fullname is provided.
187 function getFancyName()
189 if ($this->fullname) {
190 // TRANS: Full name of a profile or group (%1$s) followed by nickname (%2$s) in parentheses.
191 return sprintf(_m('FANCYNAME','%1$s (%2$s)'), $this->fullname, $this->nickname);
193 return $this->nickname;
198 * Get the most recent notice posted by this user, if any.
200 * @return mixed Notice or null
202 function getCurrentNotice()
204 $notice = $this->getNotices(0, 1);
206 if ($notice->fetch()) {
207 if ($notice instanceof ArrayWrapper) {
208 // hack for things trying to work with single notices
209 return $notice->_items[0];
217 function getTaggedNotices($tag, $offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $max_id=0)
219 $stream = new TaggedProfileNoticeStream($this, $tag);
221 return $stream->getNotices($offset, $limit, $since_id, $max_id);
224 function getNotices($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $max_id=0)
226 $stream = new ProfileNoticeStream($this);
228 return $stream->getNotices($offset, $limit, $since_id, $max_id);
231 function isMember($group)
233 $gm = Group_member::pkeyGet(array('profile_id' => $this->id,
234 'group_id' => $group->id));
235 return (!empty($gm));
238 function isAdmin($group)
240 $gm = Group_member::pkeyGet(array('profile_id' => $this->id,
241 'group_id' => $group->id));
242 return (!empty($gm) && $gm->is_admin);
245 function isPendingMember($group)
247 $request = Group_join_queue::pkeyGet(array('profile_id' => $this->id,
248 'group_id' => $group->id));
249 return !empty($request);
252 function getGroups($offset=0, $limit=PROFILES_PER_PAGE)
256 $keypart = sprintf('profile:groups:%d', $this->id);
258 $idstring = self::cacheGet($keypart);
260 if ($idstring !== false) {
261 $ids = explode(',', $idstring);
263 $gm = new Group_member();
265 $gm->profile_id = $this->id;
268 while ($gm->fetch()) {
269 $ids[] = $gm->group_id;
273 self::cacheSet($keypart, implode(',', $ids));
278 foreach ($ids as $id) {
279 $group = User_group::staticGet('id', $id);
280 if (!empty($group)) {
285 return new ArrayWrapper($groups);
288 function isTagged($peopletag)
290 $tag = Profile_tag::pkeyGet(array('tagger' => $peopletag->tagger,
291 'tagged' => $this->id,
292 'tag' => $peopletag->tag));
296 function canTag($tagged)
298 if (empty($tagged)) {
302 if ($tagged->id == $this->id) {
306 $all = common_config('peopletag', 'allow_tagging', 'all');
307 $local = common_config('peopletag', 'allow_tagging', 'local');
308 $remote = common_config('peopletag', 'allow_tagging', 'remote');
309 $subs = common_config('peopletag', 'allow_tagging', 'subs');
315 $tagged_user = $tagged->getUser();
316 if (!empty($tagged_user)) {
321 return (Subscription::exists($this, $tagged) ||
322 Subscription::exists($tagged, $this));
323 } else if ($remote) {
329 function getLists($auth_user, $offset=0, $limit=null, $since_id=0, $max_id=0)
333 $keypart = sprintf('profile:lists:%d', $this->id);
335 $idstr = self::cacheGet($keypart);
337 if ($idstr !== false) {
338 $ids = explode(',', $idstr);
340 $list = new Profile_list();
342 $list->selectAdd('id');
343 $list->tagger = $this->id;
344 $list->selectAdd('id as "cursor"');
347 $list->whereAdd('id > '.$since_id);
351 $list->whereAdd('id <= '.$max_id);
354 if($offset>=0 && !is_null($limit)) {
355 $list->limit($offset, $limit);
358 $list->orderBy('id DESC');
361 while ($list->fetch()) {
366 self::cacheSet($keypart, implode(',', $ids));
369 $showPrivate = (($auth_user instanceof User ||
370 $auth_user instanceof Profile) &&
371 $auth_user->id === $this->id);
375 foreach ($ids as $id) {
376 $list = Profile_list::staticGet('id', $id);
378 ($showPrivate || !$list->private)) {
380 if (!isset($list->cursor)) {
381 $list->cursor = $list->id;
388 return new ArrayWrapper($lists);
391 function getOtherTags($auth_user=null, $offset=0, $limit=null, $since_id=0, $max_id=0)
393 $lists = new Profile_list();
395 $tags = new Profile_tag();
396 $tags->tagged = $this->id;
398 $lists->joinAdd($tags);
399 #@fixme: postgres (round(date_part('epoch', my_date)))
400 $lists->selectAdd('unix_timestamp(profile_tag.modified) as "cursor"');
402 if ($auth_user instanceof User || $auth_user instanceof Profile) {
403 $lists->whereAdd('( ( profile_list.private = false ) ' .
404 'OR ( profile_list.tagger = ' . $auth_user->id . ' AND ' .
405 'profile_list.private = true ) )');
407 $lists->private = false;
411 $lists->whereAdd('cursor > '.$since_id);
415 $lists->whereAdd('cursor <= '.$max_id);
418 if($offset>=0 && !is_null($limit)) {
419 $lists->limit($offset, $limit);
422 $lists->orderBy('profile_tag.modified DESC');
428 function getPrivateTags($offset=0, $limit=null, $since_id=0, $max_id=0)
430 $tags = new Profile_list();
431 $tags->private = true;
432 $tags->tagger = $this->id;
435 $tags->whereAdd('id > '.$since_id);
439 $tags->whereAdd('id <= '.$max_id);
442 if($offset>=0 && !is_null($limit)) {
443 $tags->limit($offset, $limit);
446 $tags->orderBy('id DESC');
452 function hasLocalTags()
454 $tags = new Profile_tag();
456 $tags->joinAdd(array('tagger', 'user:id'));
457 $tags->whereAdd('tagged = '.$this->id);
458 $tags->whereAdd('tagger != '.$this->id);
463 return ($tags->N == 0) ? false : true;
466 function getTagSubscriptions($offset=0, $limit=null, $since_id=0, $max_id=0)
468 $lists = new Profile_list();
469 $subs = new Profile_tag_subscription();
471 $lists->joinAdd($subs);
472 #@fixme: postgres (round(date_part('epoch', my_date)))
473 $lists->selectAdd('unix_timestamp(profile_tag_subscription.created) as "cursor"');
475 $lists->whereAdd('profile_tag_subscription.profile_id = '.$this->id);
478 $lists->whereAdd('cursor > '.$since_id);
482 $lists->whereAdd('cursor <= '.$max_id);
485 if($offset>=0 && !is_null($limit)) {
486 $lists->limit($offset, $limit);
489 $lists->orderBy('"cursor" DESC');
496 * Request to join the given group.
497 * May throw exceptions on failure.
499 * @param User_group $group
500 * @return mixed: Group_member on success, Group_join_queue if pending approval, null on some cancels?
502 function joinGroup(User_group $group)
505 if ($group->join_policy == User_group::JOIN_POLICY_MODERATE) {
506 $join = Group_join_queue::saveNew($this, $group);
508 if (Event::handle('StartJoinGroup', array($group, $this))) {
509 $join = Group_member::join($group->id, $this->id);
510 self::blow('profile:groups:%d', $this->id);
511 Event::handle('EndJoinGroup', array($group, $this));
515 // Send any applicable notifications...
522 * Leave a group that this profile is a member of.
524 * @param User_group $group
526 function leaveGroup(User_group $group)
528 if (Event::handle('StartLeaveGroup', array($group, $this))) {
529 Group_member::leave($group->id, $this->id);
530 self::blow('profile:groups:%d', $this->id);
531 Event::handle('EndLeaveGroup', array($group, $this));
535 function avatarUrl($size=AVATAR_PROFILE_SIZE)
537 $avatar = $this->getAvatar($size);
539 return $avatar->displayUrl();
541 return Avatar::defaultImage($size);
545 function getSubscriptions($offset=0, $limit=null)
547 $subs = Subscription::bySubscriber($this->id,
553 while ($subs->fetch()) {
554 $profile = Profile::staticGet($subs->subscribed);
556 $profiles[] = $profile;
560 return new ArrayWrapper($profiles);
563 function getSubscribers($offset=0, $limit=null)
565 $subs = Subscription::bySubscribed($this->id,
571 while ($subs->fetch()) {
572 $profile = Profile::staticGet($subs->subscriber);
574 $profiles[] = $profile;
578 return new ArrayWrapper($profiles);
581 function getTaggedSubscribers($tag)
584 'SELECT profile.* ' .
585 'FROM profile JOIN (subscription, profile_tag, profile_list) ' .
586 'ON profile.id = subscription.subscriber ' .
587 'AND profile.id = profile_tag.tagged ' .
588 'AND profile_tag.tagger = profile_list.tagger AND profile_tag.tag = profile_list.tag ' .
589 'WHERE subscription.subscribed = %d ' .
590 'AND subscription.subscribed != subscription.subscriber ' .
591 'AND profile_tag.tagger = %d AND profile_tag.tag = "%s" ' .
592 'AND profile_list.private = false ' .
593 'ORDER BY subscription.created DESC';
595 $profile = new Profile();
598 $cnt = $profile->query(sprintf($qry, $this->id, $this->id, $tag));
600 while ($profile->fetch()) {
601 $tagged[] = clone($profile);
607 * Get pending subscribers, who have not yet been approved.
613 function getRequests($offset=0, $limit=null)
616 'SELECT profile.* ' .
617 'FROM profile JOIN subscription_queue '.
618 'ON profile.id = subscription_queue.subscriber ' .
619 'WHERE subscription_queue.subscribed = %d ' .
620 'ORDER BY subscription_queue.created DESC ';
622 if ($limit != null) {
623 if (common_config('db','type') == 'pgsql') {
624 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
626 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
630 $members = new Profile();
632 $members->query(sprintf($qry, $this->id));
636 function subscriptionCount()
638 $c = Cache::instance();
641 $cnt = $c->get(Cache::key('profile:subscription_count:'.$this->id));
642 if (is_integer($cnt)) {
647 $sub = new Subscription();
648 $sub->subscriber = $this->id;
650 $cnt = (int) $sub->count('distinct subscribed');
652 $cnt = ($cnt > 0) ? $cnt - 1 : $cnt;
655 $c->set(Cache::key('profile:subscription_count:'.$this->id), $cnt);
661 function subscriberCount()
663 $c = Cache::instance();
665 $cnt = $c->get(Cache::key('profile:subscriber_count:'.$this->id));
666 if (is_integer($cnt)) {
671 $sub = new Subscription();
672 $sub->subscribed = $this->id;
673 $sub->whereAdd('subscriber != subscribed');
674 $cnt = (int) $sub->count('distinct subscriber');
677 $c->set(Cache::key('profile:subscriber_count:'.$this->id), $cnt);
684 * Is this profile subscribed to another profile?
686 * @param Profile $other
689 function isSubscribed($other)
691 return Subscription::exists($this, $other);
695 * Check if a pending subscription request is outstanding for this...
697 * @param Profile $other
700 function hasPendingSubscription($other)
702 return Subscription_queue::exists($this, $other);
706 * Are these two profiles subscribed to each other?
708 * @param Profile $other
711 function mutuallySubscribed($other)
713 return $this->isSubscribed($other) &&
714 $other->isSubscribed($this);
717 function hasFave($notice)
719 $fave = Fave::pkeyGet(array('user_id' => $this->id,
720 'notice_id' => $notice->id));
721 return ((is_null($fave)) ? false : true);
726 $c = Cache::instance();
728 $cnt = $c->get(Cache::key('profile:fave_count:'.$this->id));
729 if (is_integer($cnt)) {
735 $faves->user_id = $this->id;
736 $cnt = (int) $faves->count('distinct notice_id');
739 $c->set(Cache::key('profile:fave_count:'.$this->id), $cnt);
745 function noticeCount()
747 $c = Cache::instance();
750 $cnt = $c->get(Cache::key('profile:notice_count:'.$this->id));
751 if (is_integer($cnt)) {
756 $notices = new Notice();
757 $notices->profile_id = $this->id;
758 $cnt = (int) $notices->count('distinct id');
761 $c->set(Cache::key('profile:notice_count:'.$this->id), $cnt);
767 function blowFavesCache()
769 $cache = Cache::instance();
771 // Faves don't happen chronologically, so we need to blow
773 $cache->delete(Cache::key('fave:ids_by_user:'.$this->id));
774 $cache->delete(Cache::key('fave:ids_by_user:'.$this->id.';last'));
775 $cache->delete(Cache::key('fave:ids_by_user_own:'.$this->id));
776 $cache->delete(Cache::key('fave:ids_by_user_own:'.$this->id.';last'));
778 $this->blowFaveCount();
781 function blowSubscriberCount()
783 $c = Cache::instance();
785 $c->delete(Cache::key('profile:subscriber_count:'.$this->id));
789 function blowSubscriptionCount()
791 $c = Cache::instance();
793 $c->delete(Cache::key('profile:subscription_count:'.$this->id));
797 function blowFaveCount()
799 $c = Cache::instance();
801 $c->delete(Cache::key('profile:fave_count:'.$this->id));
805 function blowNoticeCount()
807 $c = Cache::instance();
809 $c->delete(Cache::key('profile:notice_count:'.$this->id));
813 static function maxBio()
815 $biolimit = common_config('profile', 'biolimit');
816 // null => use global limit (distinct from 0!)
817 if (is_null($biolimit)) {
818 $biolimit = common_config('site', 'textlimit');
823 static function bioTooLong($bio)
825 $biolimit = self::maxBio();
826 return ($biolimit > 0 && !empty($bio) && (mb_strlen($bio) > $biolimit));
831 $this->_deleteNotices();
832 $this->_deleteSubscriptions();
833 $this->_deleteMessages();
834 $this->_deleteTags();
835 $this->_deleteBlocks();
836 $this->delete_avatars();
838 // Warning: delete() will run on the batch objects,
839 // not on individual objects.
840 $related = array('Reply',
843 Event::handle('ProfileDeleteRelated', array($this, &$related));
845 foreach ($related as $cls) {
847 $inst->profile_id = $this->id;
854 function _deleteNotices()
856 $notice = new Notice();
857 $notice->profile_id = $this->id;
859 if ($notice->find()) {
860 while ($notice->fetch()) {
861 $other = clone($notice);
867 function _deleteSubscriptions()
869 $sub = new Subscription();
870 $sub->subscriber = $this->id;
874 while ($sub->fetch()) {
875 $other = Profile::staticGet('id', $sub->subscribed);
879 if ($other->id == $this->id) {
882 Subscription::cancel($this, $other);
885 $subd = new Subscription();
886 $subd->subscribed = $this->id;
889 while ($subd->fetch()) {
890 $other = Profile::staticGet('id', $subd->subscriber);
894 if ($other->id == $this->id) {
897 Subscription::cancel($other, $this);
900 $self = new Subscription();
902 $self->subscriber = $this->id;
903 $self->subscribed = $this->id;
908 function _deleteMessages()
910 $msg = new Message();
911 $msg->from_profile = $this->id;
914 $msg = new Message();
915 $msg->to_profile = $this->id;
919 function _deleteTags()
921 $tag = new Profile_tag();
922 $tag->tagged = $this->id;
926 function _deleteBlocks()
928 $block = new Profile_block();
929 $block->blocked = $this->id;
932 $block = new Group_block();
933 $block->blocked = $this->id;
937 // XXX: identical to Notice::getLocation.
939 function getLocation()
943 if (!empty($this->location_id) && !empty($this->location_ns)) {
944 $location = Location::fromId($this->location_id, $this->location_ns);
947 if (is_null($location)) { // no ID, or Location::fromId() failed
948 if (!empty($this->lat) && !empty($this->lon)) {
949 $location = Location::fromLatLon($this->lat, $this->lon);
953 if (is_null($location)) { // still haven't found it!
954 if (!empty($this->location)) {
955 $location = Location::fromName($this->location);
962 function hasRole($name)
965 if (Event::handle('StartHasRole', array($this, $name, &$has_role))) {
966 $role = Profile_role::pkeyGet(array('profile_id' => $this->id,
968 $has_role = !empty($role);
969 Event::handle('EndHasRole', array($this, $name, $has_role));
974 function grantRole($name)
976 if (Event::handle('StartGrantRole', array($this, $name))) {
978 $role = new Profile_role();
980 $role->profile_id = $this->id;
982 $role->created = common_sql_now();
984 $result = $role->insert();
987 throw new Exception("Can't save role '$name' for profile '{$this->id}'");
990 if ($name == 'owner') {
991 User::blow('user:site_owner');
994 Event::handle('EndGrantRole', array($this, $name));
1000 function revokeRole($name)
1002 if (Event::handle('StartRevokeRole', array($this, $name))) {
1004 $role = Profile_role::pkeyGet(array('profile_id' => $this->id,
1008 // TRANS: Exception thrown when trying to revoke an existing role for a user that does not exist.
1009 // TRANS: %1$s is the role name, %2$s is the user ID (number).
1010 throw new Exception(sprintf(_('Cannot revoke role "%1$s" for user #%2$d; does not exist.'),$name, $this->id));
1013 $result = $role->delete();
1016 common_log_db_error($role, 'DELETE', __FILE__);
1017 // TRANS: Exception thrown when trying to revoke a role for a user with a failing database query.
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; database error.'),$name, $this->id));
1022 if ($name == 'owner') {
1023 User::blow('user:site_owner');
1026 Event::handle('EndRevokeRole', array($this, $name));
1032 function isSandboxed()
1034 return $this->hasRole(Profile_role::SANDBOXED);
1037 function isSilenced()
1039 return $this->hasRole(Profile_role::SILENCED);
1044 $this->grantRole(Profile_role::SANDBOXED);
1047 function unsandbox()
1049 $this->revokeRole(Profile_role::SANDBOXED);
1054 $this->grantRole(Profile_role::SILENCED);
1057 function unsilence()
1059 $this->revokeRole(Profile_role::SILENCED);
1063 * Does this user have the right to do X?
1065 * With our role-based authorization, this is merely a lookup for whether the user
1066 * has a particular role. The implementation currently uses a switch statement
1067 * to determine if the user has the pre-defined role to exercise the right. Future
1068 * implementations may allow per-site roles, and different mappings of roles to rights.
1070 * @param $right string Name of the right, usually a constant in class Right
1071 * @return boolean whether the user has the right in question
1073 function hasRight($right)
1077 if ($this->hasRole(Profile_role::DELETED)) {
1081 if (Event::handle('UserRightsCheck', array($this, $right, &$result))) {
1084 case Right::DELETEOTHERSNOTICE:
1085 case Right::MAKEGROUPADMIN:
1086 case Right::SANDBOXUSER:
1087 case Right::SILENCEUSER:
1088 case Right::DELETEUSER:
1089 case Right::DELETEGROUP:
1090 $result = $this->hasRole(Profile_role::MODERATOR);
1092 case Right::CONFIGURESITE:
1093 $result = $this->hasRole(Profile_role::ADMINISTRATOR);
1095 case Right::GRANTROLE:
1096 case Right::REVOKEROLE:
1097 $result = $this->hasRole(Profile_role::OWNER);
1099 case Right::NEWNOTICE:
1100 case Right::NEWMESSAGE:
1101 case Right::SUBSCRIBE:
1102 case Right::CREATEGROUP:
1103 $result = !$this->isSilenced();
1105 case Right::PUBLICNOTICE:
1106 case Right::EMAILONREPLY:
1107 case Right::EMAILONSUBSCRIBE:
1108 case Right::EMAILONFAVE:
1109 $result = !$this->isSandboxed();
1111 case Right::WEBLOGIN:
1112 $result = !$this->isSilenced();
1115 $result = !$this->isSilenced();
1117 case Right::BACKUPACCOUNT:
1118 $result = common_config('profile', 'backup');
1120 case Right::RESTOREACCOUNT:
1121 $result = common_config('profile', 'restore');
1123 case Right::DELETEACCOUNT:
1124 $result = common_config('profile', 'delete');
1126 case Right::MOVEACCOUNT:
1127 $result = common_config('profile', 'move');
1137 function hasRepeated($notice_id)
1139 // XXX: not really a pkey, but should work
1141 $notice = Memcached_DataObject::pkeyGet('Notice',
1142 array('profile_id' => $this->id,
1143 'repeat_of' => $notice_id));
1145 return !empty($notice);
1149 * Returns an XML string fragment with limited profile information
1150 * as an Atom <author> element.
1152 * Assumes that Atom has been previously set up as the base namespace.
1154 * @param Profile $cur the current authenticated user
1158 function asAtomAuthor($cur = null)
1160 $xs = new XMLStringer(true);
1162 $xs->elementStart('author');
1163 $xs->element('name', null, $this->nickname);
1164 $xs->element('uri', null, $this->getUri());
1167 $attrs['following'] = $cur->isSubscribed($this) ? 'true' : 'false';
1168 $attrs['blocking'] = $cur->hasBlocked($this) ? 'true' : 'false';
1169 $xs->element('statusnet:profile_info', $attrs, null);
1171 $xs->elementEnd('author');
1173 return $xs->getString();
1177 * Extra profile info for atom entries
1179 * Clients use some extra profile info in the atom stream.
1180 * This gives it to them.
1182 * @param User $cur Current user
1184 * @return array representation of <statusnet:profile_info> element or null
1187 function profileInfo($cur)
1189 $profileInfoAttr = array('local_id' => $this->id);
1192 // Whether the current user is a subscribed to this profile
1193 $profileInfoAttr['following'] = $cur->isSubscribed($this) ? 'true' : 'false';
1194 // Whether the current user is has blocked this profile
1195 $profileInfoAttr['blocking'] = $cur->hasBlocked($this) ? 'true' : 'false';
1198 return array('statusnet:profile_info', $profileInfoAttr, null);
1202 * Returns an XML string fragment with profile information as an
1203 * Activity Streams <activity:actor> element.
1205 * Assumes that 'activity' namespace has been previously defined.
1209 function asActivityActor()
1211 return $this->asActivityNoun('actor');
1215 * Returns an XML string fragment with profile information as an
1216 * Activity Streams noun object with the given element type.
1218 * Assumes that 'activity', 'georss', and 'poco' namespace has been
1219 * previously defined.
1221 * @param string $element one of 'actor', 'subject', 'object', 'target'
1225 function asActivityNoun($element)
1227 $noun = ActivityObject::fromProfile($this);
1228 return $noun->asString('activity:' . $element);
1232 * Returns the best URI for a profile. Plugins may override.
1234 * @return string $uri
1240 // give plugins a chance to set the URI
1241 if (Event::handle('StartGetProfileUri', array($this, &$uri))) {
1243 // check for a local user first
1244 $user = User::staticGet('id', $this->id);
1246 if (!empty($user)) {
1250 Event::handle('EndGetProfileUri', array($this, &$uri));
1256 function hasBlocked($other)
1258 $block = Profile_block::get($this->id, $other->id);
1260 if (empty($block)) {
1269 function getAtomFeed()
1273 if (Event::handle('StartProfileGetAtomFeed', array($this, &$feed))) {
1274 $user = User::staticGet('id', $this->id);
1275 if (!empty($user)) {
1276 $feed = common_local_url('ApiTimelineUser', array('id' => $user->id,
1277 'format' => 'atom'));
1279 Event::handle('EndProfileGetAtomFeed', array($this, $feed));
1285 static function fromURI($uri)
1289 if (Event::handle('StartGetProfileFromURI', array($uri, &$profile))) {
1290 // Get a local user or remote (OMB 0.1) profile
1291 $user = User::staticGet('uri', $uri);
1292 if (!empty($user)) {
1293 $profile = $user->getProfile();
1295 Event::handle('EndGetProfileFromURI', array($uri, $profile));
1301 function canRead(Notice $notice)
1303 if ($notice->scope & Notice::SITE_SCOPE) {
1304 $user = $this->getUser();
1310 if ($notice->scope & Notice::ADDRESSEE_SCOPE) {
1311 $replies = $notice->getReplies();
1313 if (!in_array($this->id, $replies)) {
1314 $groups = $notice->getGroups();
1318 foreach ($groups as $group) {
1319 if ($this->isMember($group)) {
1331 if ($notice->scope & Notice::FOLLOWER_SCOPE) {
1332 $author = $notice->getProfile();
1333 if (!Subscription::exists($this, $author)) {
1341 static function current()
1343 $user = common_current_user();
1347 $profile = $user->getProfile();
1353 * Magic function called at serialize() time.
1355 * We use this to drop a couple process-specific references
1356 * from DB_DataObject which can cause trouble in future
1359 * @return array of variable names to include in serialization.
1364 $vars = parent::__sleep();
1365 $skip = array('_user');
1366 return array_diff($vars, $skip);