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 getLists($auth_user, $offset=0, $limit=null, $since_id=0, $max_id=0)
328 $keypart = sprintf('profile:lists:%d', $this->id);
330 $idstr = self::cacheGet($keypart);
332 if ($idstr !== false) {
333 $ids = explode(',', $idstr);
335 $list = new Profile_list();
337 $list->selectAdd('id');
338 $list->tagger = $this->id;
339 $list->selectAdd('id as "cursor"');
342 $list->whereAdd('id > '.$since_id);
346 $list->whereAdd('id <= '.$max_id);
349 if($offset>=0 && !is_null($limit)) {
350 $list->limit($offset, $limit);
353 $list->orderBy('id DESC');
356 while ($list->fetch()) {
361 self::cacheSet($keypart, implode(',', $ids));
364 $showPrivate = (($auth_user instanceof User ||
365 $auth_user instanceof Profile) &&
366 $auth_user->id === $this->id);
370 foreach ($ids as $id) {
371 $list = Profile_list::staticGet('id', $id);
373 ($showPrivate || !$list->private)) {
375 if (!isset($list->cursor)) {
376 $list->cursor = $list->id;
383 return new ArrayWrapper($lists);
386 function getOtherTags($auth_user=null, $offset=0, $limit=null, $since_id=0, $max_id=0)
388 $lists = new Profile_list();
390 $tags = new Profile_tag();
391 $tags->tagged = $this->id;
393 $lists->joinAdd($tags);
394 #@fixme: postgres (round(date_part('epoch', my_date)))
395 $lists->selectAdd('unix_timestamp(profile_tag.modified) as "cursor"');
397 if ($auth_user instanceof User || $auth_user instanceof Profile) {
398 $lists->whereAdd('( ( profile_list.private = false ) ' .
399 'OR ( profile_list.tagger = ' . $auth_user->id . ' AND ' .
400 'profile_list.private = true ) )');
402 $lists->private = false;
406 $lists->whereAdd('cursor > '.$since_id);
410 $lists->whereAdd('cursor <= '.$max_id);
413 if($offset>=0 && !is_null($limit)) {
414 $lists->limit($offset, $limit);
417 $lists->orderBy('profile_tag.modified DESC');
423 function getPrivateTags($offset=0, $limit=null, $since_id=0, $max_id=0)
425 $tags = new Profile_list();
426 $tags->private = true;
427 $tags->tagger = $this->id;
430 $tags->whereAdd('id > '.$since_id);
434 $tags->whereAdd('id <= '.$max_id);
437 if($offset>=0 && !is_null($limit)) {
438 $tags->limit($offset, $limit);
441 $tags->orderBy('id DESC');
447 function hasLocalTags()
449 $tags = new Profile_tag();
451 $tags->joinAdd(array('tagger', 'user:id'));
452 $tags->whereAdd('tagged = '.$this->id);
453 $tags->whereAdd('tagger != '.$this->id);
458 return ($tags->N == 0) ? false : true;
461 function getTagSubscriptions($offset=0, $limit=null, $since_id=0, $max_id=0)
463 $lists = new Profile_list();
464 $subs = new Profile_tag_subscription();
466 $lists->joinAdd($subs);
467 #@fixme: postgres (round(date_part('epoch', my_date)))
468 $lists->selectAdd('unix_timestamp(profile_tag_subscription.created) as "cursor"');
470 $lists->whereAdd('profile_tag_subscription.profile_id = '.$this->id);
473 $lists->whereAdd('cursor > '.$since_id);
477 $lists->whereAdd('cursor <= '.$max_id);
480 if($offset>=0 && !is_null($limit)) {
481 $lists->limit($offset, $limit);
484 $lists->orderBy('"cursor" DESC');
491 * Request to join the given group.
492 * May throw exceptions on failure.
494 * @param User_group $group
495 * @return mixed: Group_member on success, Group_join_queue if pending approval, null on some cancels?
497 function joinGroup(User_group $group)
500 if ($group->join_policy == User_group::JOIN_POLICY_MODERATE) {
501 $join = Group_join_queue::saveNew($this, $group);
503 if (Event::handle('StartJoinGroup', array($group, $this))) {
504 $join = Group_member::join($group->id, $this->id);
505 self::blow('profile:groups:%d', $this->id);
506 Event::handle('EndJoinGroup', array($group, $this));
510 // Send any applicable notifications...
517 * Leave a group that this profile is a member of.
519 * @param User_group $group
521 function leaveGroup(User_group $group)
523 if (Event::handle('StartLeaveGroup', array($group, $this))) {
524 Group_member::leave($group->id, $this->id);
525 self::blow('profile:groups:%d', $this->id);
526 Event::handle('EndLeaveGroup', array($group, $this));
530 function avatarUrl($size=AVATAR_PROFILE_SIZE)
532 $avatar = $this->getAvatar($size);
534 return $avatar->displayUrl();
536 return Avatar::defaultImage($size);
540 function getSubscriptions($offset=0, $limit=null)
542 $subs = Subscription::bySubscriber($this->id,
548 while ($subs->fetch()) {
549 $profile = Profile::staticGet($subs->subscribed);
551 $profiles[] = $profile;
555 return new ArrayWrapper($profiles);
558 function getSubscribers($offset=0, $limit=null)
560 $subs = Subscription::bySubscribed($this->id,
566 while ($subs->fetch()) {
567 $profile = Profile::staticGet($subs->subscriber);
569 $profiles[] = $profile;
573 return new ArrayWrapper($profiles);
576 function getTaggedSubscribers($tag)
579 'SELECT profile.* ' .
580 'FROM profile JOIN (subscription, profile_tag, profile_list) ' .
581 'ON profile.id = subscription.subscriber ' .
582 'AND profile.id = profile_tag.tagged ' .
583 'AND profile_tag.tagger = profile_list.tagger AND profile_tag.tag = profile_list.tag ' .
584 'WHERE subscription.subscribed = %d ' .
585 'AND subscription.subscribed != subscription.subscriber ' .
586 'AND profile_tag.tagger = %d AND profile_tag.tag = "%s" ' .
587 'AND profile_list.private = false ' .
588 'ORDER BY subscription.created DESC';
590 $profile = new Profile();
593 $cnt = $profile->query(sprintf($qry, $this->id, $this->id, $tag));
595 while ($profile->fetch()) {
596 $tagged[] = clone($profile);
602 * Get pending subscribers, who have not yet been approved.
608 function getRequests($offset=0, $limit=null)
611 'SELECT profile.* ' .
612 'FROM profile JOIN subscription_queue '.
613 'ON profile.id = subscription_queue.subscriber ' .
614 'WHERE subscription_queue.subscribed = %d ' .
615 'ORDER BY subscription_queue.created DESC ';
617 if ($limit != null) {
618 if (common_config('db','type') == 'pgsql') {
619 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
621 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
625 $members = new Profile();
627 $members->query(sprintf($qry, $this->id));
631 function subscriptionCount()
633 $c = Cache::instance();
636 $cnt = $c->get(Cache::key('profile:subscription_count:'.$this->id));
637 if (is_integer($cnt)) {
642 $sub = new Subscription();
643 $sub->subscriber = $this->id;
645 $cnt = (int) $sub->count('distinct subscribed');
647 $cnt = ($cnt > 0) ? $cnt - 1 : $cnt;
650 $c->set(Cache::key('profile:subscription_count:'.$this->id), $cnt);
656 function subscriberCount()
658 $c = Cache::instance();
660 $cnt = $c->get(Cache::key('profile:subscriber_count:'.$this->id));
661 if (is_integer($cnt)) {
666 $sub = new Subscription();
667 $sub->subscribed = $this->id;
668 $sub->whereAdd('subscriber != subscribed');
669 $cnt = (int) $sub->count('distinct subscriber');
672 $c->set(Cache::key('profile:subscriber_count:'.$this->id), $cnt);
679 * Is this profile subscribed to another profile?
681 * @param Profile $other
684 function isSubscribed($other)
686 return Subscription::exists($this, $other);
690 * Check if a pending subscription request is outstanding for this...
692 * @param Profile $other
695 function hasPendingSubscription($other)
697 return Subscription_queue::exists($this, $other);
701 * Are these two profiles subscribed to each other?
703 * @param Profile $other
706 function mutuallySubscribed($other)
708 return $this->isSubscribed($other) &&
709 $other->isSubscribed($this);
712 function hasFave($notice)
714 $fave = Fave::pkeyGet(array('user_id' => $this->id,
715 'notice_id' => $notice->id));
716 return ((is_null($fave)) ? false : true);
721 $c = Cache::instance();
723 $cnt = $c->get(Cache::key('profile:fave_count:'.$this->id));
724 if (is_integer($cnt)) {
730 $faves->user_id = $this->id;
731 $cnt = (int) $faves->count('distinct notice_id');
734 $c->set(Cache::key('profile:fave_count:'.$this->id), $cnt);
740 function noticeCount()
742 $c = Cache::instance();
745 $cnt = $c->get(Cache::key('profile:notice_count:'.$this->id));
746 if (is_integer($cnt)) {
751 $notices = new Notice();
752 $notices->profile_id = $this->id;
753 $cnt = (int) $notices->count('distinct id');
756 $c->set(Cache::key('profile:notice_count:'.$this->id), $cnt);
762 function blowFavesCache()
764 $cache = Cache::instance();
766 // Faves don't happen chronologically, so we need to blow
768 $cache->delete(Cache::key('fave:ids_by_user:'.$this->id));
769 $cache->delete(Cache::key('fave:ids_by_user:'.$this->id.';last'));
770 $cache->delete(Cache::key('fave:ids_by_user_own:'.$this->id));
771 $cache->delete(Cache::key('fave:ids_by_user_own:'.$this->id.';last'));
773 $this->blowFaveCount();
776 function blowSubscriberCount()
778 $c = Cache::instance();
780 $c->delete(Cache::key('profile:subscriber_count:'.$this->id));
784 function blowSubscriptionCount()
786 $c = Cache::instance();
788 $c->delete(Cache::key('profile:subscription_count:'.$this->id));
792 function blowFaveCount()
794 $c = Cache::instance();
796 $c->delete(Cache::key('profile:fave_count:'.$this->id));
800 function blowNoticeCount()
802 $c = Cache::instance();
804 $c->delete(Cache::key('profile:notice_count:'.$this->id));
808 static function maxBio()
810 $biolimit = common_config('profile', 'biolimit');
811 // null => use global limit (distinct from 0!)
812 if (is_null($biolimit)) {
813 $biolimit = common_config('site', 'textlimit');
818 static function bioTooLong($bio)
820 $biolimit = self::maxBio();
821 return ($biolimit > 0 && !empty($bio) && (mb_strlen($bio) > $biolimit));
826 $this->_deleteNotices();
827 $this->_deleteSubscriptions();
828 $this->_deleteMessages();
829 $this->_deleteTags();
830 $this->_deleteBlocks();
831 $this->delete_avatars();
833 // Warning: delete() will run on the batch objects,
834 // not on individual objects.
835 $related = array('Reply',
838 Event::handle('ProfileDeleteRelated', array($this, &$related));
840 foreach ($related as $cls) {
842 $inst->profile_id = $this->id;
849 function _deleteNotices()
851 $notice = new Notice();
852 $notice->profile_id = $this->id;
854 if ($notice->find()) {
855 while ($notice->fetch()) {
856 $other = clone($notice);
862 function _deleteSubscriptions()
864 $sub = new Subscription();
865 $sub->subscriber = $this->id;
869 while ($sub->fetch()) {
870 $other = Profile::staticGet('id', $sub->subscribed);
874 if ($other->id == $this->id) {
877 Subscription::cancel($this, $other);
880 $subd = new Subscription();
881 $subd->subscribed = $this->id;
884 while ($subd->fetch()) {
885 $other = Profile::staticGet('id', $subd->subscriber);
889 if ($other->id == $this->id) {
892 Subscription::cancel($other, $this);
895 $self = new Subscription();
897 $self->subscriber = $this->id;
898 $self->subscribed = $this->id;
903 function _deleteMessages()
905 $msg = new Message();
906 $msg->from_profile = $this->id;
909 $msg = new Message();
910 $msg->to_profile = $this->id;
914 function _deleteTags()
916 $tag = new Profile_tag();
917 $tag->tagged = $this->id;
921 function _deleteBlocks()
923 $block = new Profile_block();
924 $block->blocked = $this->id;
927 $block = new Group_block();
928 $block->blocked = $this->id;
932 // XXX: identical to Notice::getLocation.
934 function getLocation()
938 if (!empty($this->location_id) && !empty($this->location_ns)) {
939 $location = Location::fromId($this->location_id, $this->location_ns);
942 if (is_null($location)) { // no ID, or Location::fromId() failed
943 if (!empty($this->lat) && !empty($this->lon)) {
944 $location = Location::fromLatLon($this->lat, $this->lon);
948 if (is_null($location)) { // still haven't found it!
949 if (!empty($this->location)) {
950 $location = Location::fromName($this->location);
957 function hasRole($name)
960 if (Event::handle('StartHasRole', array($this, $name, &$has_role))) {
961 $role = Profile_role::pkeyGet(array('profile_id' => $this->id,
963 $has_role = !empty($role);
964 Event::handle('EndHasRole', array($this, $name, $has_role));
969 function grantRole($name)
971 if (Event::handle('StartGrantRole', array($this, $name))) {
973 $role = new Profile_role();
975 $role->profile_id = $this->id;
977 $role->created = common_sql_now();
979 $result = $role->insert();
982 throw new Exception("Can't save role '$name' for profile '{$this->id}'");
985 if ($name == 'owner') {
986 User::blow('user:site_owner');
989 Event::handle('EndGrantRole', array($this, $name));
995 function revokeRole($name)
997 if (Event::handle('StartRevokeRole', array($this, $name))) {
999 $role = Profile_role::pkeyGet(array('profile_id' => $this->id,
1003 // TRANS: Exception thrown when trying to revoke an existing role for a user that does not exist.
1004 // TRANS: %1$s is the role name, %2$s is the user ID (number).
1005 throw new Exception(sprintf(_('Cannot revoke role "%1$s" for user #%2$d; does not exist.'),$name, $this->id));
1008 $result = $role->delete();
1011 common_log_db_error($role, 'DELETE', __FILE__);
1012 // TRANS: Exception thrown when trying to revoke a role for a user with a failing database query.
1013 // TRANS: %1$s is the role name, %2$s is the user ID (number).
1014 throw new Exception(sprintf(_('Cannot revoke role "%1$s" for user #%2$d; database error.'),$name, $this->id));
1017 if ($name == 'owner') {
1018 User::blow('user:site_owner');
1021 Event::handle('EndRevokeRole', array($this, $name));
1027 function isSandboxed()
1029 return $this->hasRole(Profile_role::SANDBOXED);
1032 function isSilenced()
1034 return $this->hasRole(Profile_role::SILENCED);
1039 $this->grantRole(Profile_role::SANDBOXED);
1042 function unsandbox()
1044 $this->revokeRole(Profile_role::SANDBOXED);
1049 $this->grantRole(Profile_role::SILENCED);
1052 function unsilence()
1054 $this->revokeRole(Profile_role::SILENCED);
1058 * Does this user have the right to do X?
1060 * With our role-based authorization, this is merely a lookup for whether the user
1061 * has a particular role. The implementation currently uses a switch statement
1062 * to determine if the user has the pre-defined role to exercise the right. Future
1063 * implementations may allow per-site roles, and different mappings of roles to rights.
1065 * @param $right string Name of the right, usually a constant in class Right
1066 * @return boolean whether the user has the right in question
1068 function hasRight($right)
1072 if ($this->hasRole(Profile_role::DELETED)) {
1076 if (Event::handle('UserRightsCheck', array($this, $right, &$result))) {
1079 case Right::DELETEOTHERSNOTICE:
1080 case Right::MAKEGROUPADMIN:
1081 case Right::SANDBOXUSER:
1082 case Right::SILENCEUSER:
1083 case Right::DELETEUSER:
1084 case Right::DELETEGROUP:
1085 $result = $this->hasRole(Profile_role::MODERATOR);
1087 case Right::CONFIGURESITE:
1088 $result = $this->hasRole(Profile_role::ADMINISTRATOR);
1090 case Right::GRANTROLE:
1091 case Right::REVOKEROLE:
1092 $result = $this->hasRole(Profile_role::OWNER);
1094 case Right::NEWNOTICE:
1095 case Right::NEWMESSAGE:
1096 case Right::SUBSCRIBE:
1097 case Right::CREATEGROUP:
1098 $result = !$this->isSilenced();
1100 case Right::PUBLICNOTICE:
1101 case Right::EMAILONREPLY:
1102 case Right::EMAILONSUBSCRIBE:
1103 case Right::EMAILONFAVE:
1104 $result = !$this->isSandboxed();
1106 case Right::WEBLOGIN:
1107 $result = !$this->isSilenced();
1110 $result = !$this->isSilenced();
1112 case Right::BACKUPACCOUNT:
1113 $result = common_config('profile', 'backup');
1115 case Right::RESTOREACCOUNT:
1116 $result = common_config('profile', 'restore');
1118 case Right::DELETEACCOUNT:
1119 $result = common_config('profile', 'delete');
1121 case Right::MOVEACCOUNT:
1122 $result = common_config('profile', 'move');
1132 function hasRepeated($notice_id)
1134 // XXX: not really a pkey, but should work
1136 $notice = Memcached_DataObject::pkeyGet('Notice',
1137 array('profile_id' => $this->id,
1138 'repeat_of' => $notice_id));
1140 return !empty($notice);
1144 * Returns an XML string fragment with limited profile information
1145 * as an Atom <author> element.
1147 * Assumes that Atom has been previously set up as the base namespace.
1149 * @param Profile $cur the current authenticated user
1153 function asAtomAuthor($cur = null)
1155 $xs = new XMLStringer(true);
1157 $xs->elementStart('author');
1158 $xs->element('name', null, $this->nickname);
1159 $xs->element('uri', null, $this->getUri());
1162 $attrs['following'] = $cur->isSubscribed($this) ? 'true' : 'false';
1163 $attrs['blocking'] = $cur->hasBlocked($this) ? 'true' : 'false';
1164 $xs->element('statusnet:profile_info', $attrs, null);
1166 $xs->elementEnd('author');
1168 return $xs->getString();
1172 * Extra profile info for atom entries
1174 * Clients use some extra profile info in the atom stream.
1175 * This gives it to them.
1177 * @param User $cur Current user
1179 * @return array representation of <statusnet:profile_info> element or null
1182 function profileInfo($cur)
1184 $profileInfoAttr = array('local_id' => $this->id);
1187 // Whether the current user is a subscribed to this profile
1188 $profileInfoAttr['following'] = $cur->isSubscribed($this) ? 'true' : 'false';
1189 // Whether the current user is has blocked this profile
1190 $profileInfoAttr['blocking'] = $cur->hasBlocked($this) ? 'true' : 'false';
1193 return array('statusnet:profile_info', $profileInfoAttr, null);
1197 * Returns an XML string fragment with profile information as an
1198 * Activity Streams <activity:actor> element.
1200 * Assumes that 'activity' namespace has been previously defined.
1204 function asActivityActor()
1206 return $this->asActivityNoun('actor');
1210 * Returns an XML string fragment with profile information as an
1211 * Activity Streams noun object with the given element type.
1213 * Assumes that 'activity', 'georss', and 'poco' namespace has been
1214 * previously defined.
1216 * @param string $element one of 'actor', 'subject', 'object', 'target'
1220 function asActivityNoun($element)
1222 $noun = ActivityObject::fromProfile($this);
1223 return $noun->asString('activity:' . $element);
1227 * Returns the best URI for a profile. Plugins may override.
1229 * @return string $uri
1235 // give plugins a chance to set the URI
1236 if (Event::handle('StartGetProfileUri', array($this, &$uri))) {
1238 // check for a local user first
1239 $user = User::staticGet('id', $this->id);
1241 if (!empty($user)) {
1244 // return OMB profile if any
1245 $remote = Remote_profile::staticGet('id', $this->id);
1246 if (!empty($remote)) {
1247 $uri = $remote->uri;
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 $remote_profile = Remote_profile::staticGet('uri', $uri);
1296 if (!empty($remote_profile)) {
1297 $profile = Profile::staticGet('id', $remote_profile->profile_id);
1300 Event::handle('EndGetProfileFromURI', array($uri, $profile));
1306 function canRead(Notice $notice)
1308 if ($notice->scope & Notice::SITE_SCOPE) {
1309 $user = $this->getUser();
1315 if ($notice->scope & Notice::ADDRESSEE_SCOPE) {
1316 $replies = $notice->getReplies();
1318 if (!in_array($this->id, $replies)) {
1319 $groups = $notice->getGroups();
1323 foreach ($groups as $group) {
1324 if ($this->isMember($group)) {
1336 if ($notice->scope & Notice::FOLLOWER_SCOPE) {
1337 $author = $notice->getProfile();
1338 if (!Subscription::exists($this, $author)) {
1346 static function current()
1348 $user = common_current_user();
1352 $profile = $user->getProfile();
1358 * Magic function called at serialize() time.
1360 * We use this to drop a couple process-specific references
1361 * from DB_DataObject which can cause trouble in future
1364 * @return array of variable names to include in serialization.
1369 $vars = parent::__sleep();
1370 $skip = array('_user');
1371 return array_diff($vars, $skip);