3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2009-2010, 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/>.
21 * @package OStatusPlugin
22 * @maintainer Brion Vibber <brion@status.net>
25 class Ostatus_profile extends Memcached_DataObject
27 public $__table = 'ostatus_profile';
36 public $avatar; // remote URL of the last avatar we saved
41 public /*static*/ function staticGet($k, $v=null)
43 return parent::staticGet(__CLASS__, $k, $v);
47 * return table definition for DB_DataObject
49 * DB_DataObject needs to know something about the table to manipulate
50 * instances. This method provides all the DB_DataObject needs to know.
52 * @return array array of column definitions
57 return array('uri' => DB_DATAOBJECT_STR + DB_DATAOBJECT_NOTNULL,
58 'profile_id' => DB_DATAOBJECT_INT,
59 'group_id' => DB_DATAOBJECT_INT,
60 'feeduri' => DB_DATAOBJECT_STR,
61 'salmonuri' => DB_DATAOBJECT_STR,
62 'avatar' => DB_DATAOBJECT_STR,
63 'created' => DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME + DB_DATAOBJECT_NOTNULL,
64 'modified' => DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME + DB_DATAOBJECT_NOTNULL);
67 static function schemaDef()
69 return array(new ColumnDef('uri', 'varchar',
71 new ColumnDef('profile_id', 'integer',
73 new ColumnDef('group_id', 'integer',
75 new ColumnDef('feeduri', 'varchar',
77 new ColumnDef('salmonuri', 'text',
79 new ColumnDef('avatar', 'text',
81 new ColumnDef('created', 'datetime',
83 new ColumnDef('modified', 'datetime',
88 * return key definitions for DB_DataObject
90 * DB_DataObject needs to know about keys that the table has; this function
93 * @return array key definitions
98 return array_keys($this->keyTypes());
102 * return key definitions for Memcached_DataObject
104 * Our caching system uses the same key definitions, but uses a different
105 * method to get them.
107 * @return array key definitions
112 return array('uri' => 'K', 'profile_id' => 'U', 'group_id' => 'U', 'feeduri' => 'U');
115 function sequenceKey()
117 return array(false, false, false);
121 * Fetch the StatusNet-side profile for this feed
124 public function localProfile()
126 if ($this->profile_id) {
127 return Profile::staticGet('id', $this->profile_id);
133 * Fetch the StatusNet-side profile for this feed
136 public function localGroup()
138 if ($this->group_id) {
139 return User_group::staticGet('id', $this->group_id);
145 * Returns an ActivityObject describing this remote user or group profile.
146 * Can then be used to generate Atom chunks.
148 * @return ActivityObject
150 function asActivityObject()
152 if ($this->isGroup()) {
153 return ActivityObject::fromGroup($this->localGroup());
155 return ActivityObject::fromProfile($this->localProfile());
160 * Returns an XML string fragment with profile information as an
161 * Activity Streams noun object with the given element type.
163 * Assumes that 'activity' namespace has been previously defined.
165 * @fixme replace with wrappers on asActivityObject when it's got everything.
167 * @param string $element one of 'actor', 'subject', 'object', 'target'
170 function asActivityNoun($element)
172 if ($this->isGroup()) {
173 $noun = ActivityObject::fromGroup($this->localGroup());
174 return $noun->asString('activity:' . $element);
176 $noun = ActivityObject::fromProfile($this->localProfile());
177 return $noun->asString('activity:' . $element);
182 * @return boolean true if this is a remote group
186 if ($this->profile_id && !$this->group_id) {
188 } else if ($this->group_id && !$this->profile_id) {
190 } else if ($this->group_id && $this->profile_id) {
191 throw new ServerException("Invalid ostatus_profile state: both group and profile IDs set for $this->uri");
193 throw new ServerException("Invalid ostatus_profile state: both group and profile IDs empty for $this->uri");
198 * Subscribe a local user to this remote user.
199 * PuSH subscription will be started if necessary, and we'll
200 * send a Salmon notification to the remote server if available
201 * notifying them of the sub.
204 * @return boolean success
205 * @throws FeedException
207 public function subscribeLocalToRemote(User $user)
209 if ($this->isGroup()) {
210 throw new ServerException("Can't subscribe to a remote group");
213 if ($this->subscribe()) {
214 if ($user->subscribeTo($this->localProfile())) {
215 $this->notify($user->getProfile(), ActivityVerb::FOLLOW, $this);
223 * Mark this remote profile as subscribing to the given local user,
224 * and send appropriate notifications to the user.
226 * This will generally be in response to a subscription notification
227 * from a foreign site to our local Salmon response channel.
230 * @return boolean success
232 public function subscribeRemoteToLocal(User $user)
234 if ($this->isGroup()) {
235 throw new ServerException("Remote groups can't subscribe to local users");
238 Subscription::start($this->localProfile(), $user->getProfile());
244 * Send a subscription request to the hub for this feed.
245 * The hub will later send us a confirmation POST to /main/push/callback.
247 * @return bool true on success, false on failure
248 * @throws ServerException if feed state is not valid
250 public function subscribe()
252 $feedsub = FeedSub::ensureFeed($this->feeduri);
253 if ($feedsub->sub_state == 'active' || $feedsub->sub_state == 'subscribe') {
255 } else if ($feedsub->sub_state == '' || $feedsub->sub_state == 'inactive') {
256 return $feedsub->subscribe();
257 } else if ('unsubscribe') {
258 throw new FeedSubException("Unsub is pending, can't subscribe...");
263 * Send a PuSH unsubscription request to the hub for this feed.
264 * The hub will later send us a confirmation POST to /main/push/callback.
266 * @return bool true on success, false on failure
267 * @throws ServerException if feed state is not valid
269 public function unsubscribe() {
270 $feedsub = FeedSub::staticGet('uri', $this->feeduri);
274 if ($feedsub->sub_state == 'active') {
275 return $feedsub->unsubscribe();
276 } else if ($feedsub->sub_state == '' || $feedsub->sub_state == 'inactive' || $feedsub->sub_state == 'unsubscribe') {
278 } else if ($feedsub->sub_state == 'subscribe') {
279 throw new FeedSubException("Feed is awaiting subscription, can't unsub...");
284 * Check if this remote profile has any active local subscriptions, and
285 * if not drop the PuSH subscription feed.
289 public function garbageCollect()
291 if ($this->isGroup()) {
292 $members = $this->localGroup()->getMembers(0, 1);
293 $count = $members->N;
295 $count = $this->localProfile()->subscriberCount();
298 common_log(LOG_INFO, "Unsubscribing from now-unused remote feed $this->feeduri");
299 $this->unsubscribe();
307 * Send an Activity Streams notification to the remote Salmon endpoint,
310 * @param Profile $actor Actor who did the activity
311 * @param string $verb Activity::SUBSCRIBE or Activity::JOIN
312 * @param Object $object object of the action; must define asActivityNoun($tag)
314 public function notify($actor, $verb, $object=null)
316 if (!($actor instanceof Profile)) {
317 $type = gettype($actor);
318 if ($type == 'object') {
319 $type = get_class($actor);
321 throw new ServerException("Invalid actor passed to " . __METHOD__ . ": " . $type);
323 if ($object == null) {
326 if ($this->salmonuri) {
329 $id = TagURI::mint('%s:%s:%s',
332 common_date_iso8601(time()));
334 // @fixme consolidate all these NS settings somewhere
335 $attributes = array('xmlns' => Activity::ATOM,
336 'xmlns:activity' => 'http://activitystrea.ms/spec/1.0/',
337 'xmlns:thr' => 'http://purl.org/syndication/thread/1.0',
338 'xmlns:georss' => 'http://www.georss.org/georss',
339 'xmlns:ostatus' => 'http://ostatus.org/schema/1.0',
340 'xmlns:poco' => 'http://portablecontacts.net/spec/1.0',
341 'xmlns:media' => 'http://purl.org/syndication/atommedia');
343 $entry = new XMLStringer();
344 $entry->elementStart('entry', $attributes);
345 $entry->element('id', null, $id);
346 $entry->element('title', null, $text);
347 $entry->element('summary', null, $text);
348 $entry->element('published', null, common_date_w3dtf(common_sql_now()));
350 $entry->element('activity:verb', null, $verb);
351 $entry->raw($actor->asAtomAuthor());
352 $entry->raw($actor->asActivityActor());
353 $entry->raw($object->asActivityNoun('object'));
354 $entry->elementEnd('entry');
356 $xml = $entry->getString();
357 common_log(LOG_INFO, "Posting to Salmon endpoint $this->salmonuri: $xml");
359 $salmon = new Salmon(); // ?
360 return $salmon->post($this->salmonuri, $xml, $actor);
366 * Send a Salmon notification ping immediately, and confirm that we got
367 * an acceptable response from the remote site.
369 * @param mixed $entry XML string, Notice, or Activity
370 * @return boolean success
372 public function notifyActivity($entry, $actor)
374 if ($this->salmonuri) {
375 $salmon = new Salmon();
376 return $salmon->post($this->salmonuri, $this->notifyPrepXml($entry), $actor);
383 * Queue a Salmon notification for later. If queues are disabled we'll
384 * send immediately but won't get the return value.
386 * @param mixed $entry XML string, Notice, or Activity
387 * @return boolean success
389 public function notifyDeferred($entry, $actor)
391 if ($this->salmonuri) {
392 $data = array('salmonuri' => $this->salmonuri,
393 'entry' => $this->notifyPrepXml($entry),
394 'actor' => $actor->id);
396 $qm = QueueManager::get();
397 return $qm->enqueue($data, 'salmon');
403 protected function notifyPrepXml($entry)
405 $preamble = '<?xml version="1.0" encoding="UTF-8" ?' . '>';
406 if (is_string($entry)) {
408 } else if ($entry instanceof Activity) {
409 return $preamble . $entry->asString(true);
410 } else if ($entry instanceof Notice) {
411 return $preamble . $entry->asAtomEntry(true, true);
413 throw new ServerException("Invalid type passed to Ostatus_profile::notify; must be XML string or Activity entry");
417 function getBestName()
419 if ($this->isGroup()) {
420 return $this->localGroup()->getBestName();
422 return $this->localProfile()->getBestName();
427 * Read and post notices for updates from the feed.
428 * Currently assumes that all items in the feed are new,
429 * coming from a PuSH hub.
431 * @param DOMDocument $doc
432 * @param string $source identifier ("push")
434 public function processFeed(DOMDocument $doc, $source)
436 $feed = $doc->documentElement;
438 if ($feed->localName != 'feed' || $feed->namespaceURI != Activity::ATOM) {
439 common_log(LOG_ERR, __METHOD__ . ": not an Atom feed, ignoring");
443 $entries = $feed->getElementsByTagNameNS(Activity::ATOM, 'entry');
444 if ($entries->length == 0) {
445 common_log(LOG_ERR, __METHOD__ . ": no entries in feed update, ignoring");
449 for ($i = 0; $i < $entries->length; $i++) {
450 $entry = $entries->item($i);
451 $this->processEntry($entry, $feed, $source);
456 * Process a posted entry from this feed source.
458 * @param DOMElement $entry
459 * @param DOMElement $feed for context
460 * @param string $source identifier ("push" or "salmon")
462 public function processEntry($entry, $feed, $source)
464 $activity = new Activity($entry, $feed);
466 if ($activity->verb == ActivityVerb::POST) {
467 $this->processPost($activity, $source);
469 common_log(LOG_INFO, "Ignoring activity with unrecognized verb $activity->verb");
474 * Process an incoming post activity from this remote feed.
475 * @param Activity $activity
476 * @param string $method 'push' or 'salmon'
477 * @return mixed saved Notice or false
478 * @fixme break up this function, it's getting nasty long
480 public function processPost($activity, $method)
482 if ($this->isGroup()) {
483 // A group feed will contain posts from multiple authors.
484 // @fixme validate these profiles in some way!
485 $oprofile = self::ensureActorProfile($activity);
486 if ($oprofile->isGroup()) {
487 // Groups can't post notices in StatusNet.
488 common_log(LOG_WARNING, "OStatus: skipping post with group listed as author: $oprofile->uri in feed from $this->uri");
492 // Individual user feeds may contain only posts from themselves.
493 // Authorship is validated against the profile URI on upper layers,
494 // through PuSH setup or Salmon signature checks.
495 $actorUri = self::getActorProfileURI($activity);
496 if ($actorUri == $this->uri) {
497 // Check if profile info has changed and update it
498 $this->updateFromActivityObject($activity->actor);
500 common_log(LOG_WARNING, "OStatus: skipping post with bad author: got $actorUri expected $this->uri");
506 // The id URI will be used as a unique identifier for for the notice,
507 // protecting against duplicate saves. It isn't required to be a URL;
508 // tag: URIs for instance are found in Google Buzz feeds.
509 $sourceUri = $activity->object->id;
510 $dupe = Notice::staticGet('uri', $sourceUri);
512 common_log(LOG_INFO, "OStatus: ignoring duplicate post: $sourceUri");
516 // We'll also want to save a web link to the original notice, if provided.
518 if ($activity->object->link) {
519 $sourceUrl = $activity->object->link;
520 } else if ($activity->link) {
521 $sourceUrl = $activity->link;
522 } else if (preg_match('!^https?://!', $activity->object->id)) {
523 $sourceUrl = $activity->object->id;
526 // Get (safe!) HTML and text versions of the content
527 $rendered = $this->purify($activity->object->content);
528 $content = html_entity_decode(strip_tags($rendered));
530 $shortened = common_shorten_links($content);
532 // If it's too long, try using the summary, and make the
533 // HTML an attachment.
537 if (Notice::contentTooLong($shortened)) {
538 $attachment = $this->saveHTMLFile($activity->object->title, $rendered);
539 $summary = $activity->object->summary;
540 if (empty($summary)) {
543 $shortSummary = common_shorten_links($summary);
544 if (Notice::contentTooLong($shortSummary)) {
545 $url = common_shorten_url(common_local_url('attachment',
546 array('attachment' => $attachment->id)));
547 $shortSummary = substr($shortSummary,
549 Notice::maxContent() - (mb_strlen($url) + 2));
550 $shortSummary .= '… ' . $url;
551 $content = $shortSummary;
552 $rendered = common_render_text($content);
556 $options = array('is_local' => Notice::REMOTE_OMB,
559 'rendered' => $rendered,
560 'replies' => array(),
565 // Check for optional attributes...
567 if (!empty($activity->time)) {
568 $options['created'] = common_sql_date($activity->time);
571 if ($activity->context) {
572 // Any individual or group attn: targets?
573 $replies = $activity->context->attention;
574 $options['groups'] = $this->filterReplies($oprofile, $replies);
575 $options['replies'] = $replies;
577 // Maintain direct reply associations
578 // @fixme what about conversation ID?
579 if (!empty($activity->context->replyToID)) {
580 $orig = Notice::staticGet('uri',
581 $activity->context->replyToID);
583 $options['reply_to'] = $orig->id;
587 $location = $activity->context->location;
589 $options['lat'] = $location->lat;
590 $options['lon'] = $location->lon;
591 if ($location->location_id) {
592 $options['location_ns'] = $location->location_ns;
593 $options['location_id'] = $location->location_id;
598 // Atom categories <-> hashtags
599 foreach ($activity->categories as $cat) {
601 $term = common_canonical_tag($cat->term);
603 $options['tags'][] = $term;
608 // Atom enclosures -> attachment URLs
609 foreach ($activity->enclosures as $href) {
610 // @fixme save these locally or....?
611 $options['urls'][] = $href;
615 $saved = Notice::saveNew($oprofile->profile_id,
620 Ostatus_source::saveNew($saved, $this, $method);
621 if (!empty($attachment)) {
622 File_to_post::processNew($attachment->id, $saved->id);
625 } catch (Exception $e) {
626 common_log(LOG_ERR, "OStatus save of remote message $sourceUri failed: " . $e->getMessage());
629 common_log(LOG_INFO, "OStatus saved remote message $sourceUri as notice id $saved->id");
636 protected function purify($html)
638 require_once INSTALLDIR.'/extlib/htmLawed/htmLawed.php';
639 $config = array('safe' => 1,
640 'deny_attribute' => 'id,style,on*');
641 return htmLawed($html, $config);
645 * Filters a list of recipient ID URIs to just those for local delivery.
646 * @param Ostatus_profile local profile of sender
647 * @param array in/out &$attention_uris set of URIs, will be pruned on output
648 * @return array of group IDs
650 protected function filterReplies($sender, &$attention_uris)
652 common_log(LOG_DEBUG, "Original reply recipients: " . implode(', ', $attention_uris));
655 foreach ($attention_uris as $recipient) {
656 // Is the recipient a local user?
657 $user = User::staticGet('uri', $recipient);
659 // @fixme sender verification, spam etc?
660 $replies[] = $recipient;
664 // Is the recipient a remote group?
665 $oprofile = Ostatus_profile::staticGet('uri', $recipient);
667 if ($oprofile->isGroup()) {
668 // Deliver to local members of this remote group.
669 // @fixme sender verification?
670 $groups[] = $oprofile->group_id;
672 common_log(LOG_DEBUG, "Skipping reply to remote profile $recipient");
677 // Is the recipient a local group?
678 // @fixme uri on user_group isn't reliable yet
679 // $group = User_group::staticGet('uri', $recipient);
680 $id = OStatusPlugin::localGroupFromUrl($recipient);
682 $group = User_group::staticGet('id', $id);
684 // Deliver to all members of this local group if allowed.
685 $profile = $sender->localProfile();
686 if ($profile->isMember($group)) {
687 $groups[] = $group->id;
689 common_log(LOG_DEBUG, "Skipping reply to local group $group->nickname as sender $profile->id is not a member");
693 common_log(LOG_DEBUG, "Skipping reply to bogus group $recipient");
697 common_log(LOG_DEBUG, "Skipping reply to unrecognized profile $recipient");
700 $attention_uris = $replies;
701 common_log(LOG_DEBUG, "Local reply recipients: " . implode(', ', $replies));
702 common_log(LOG_DEBUG, "Local group recipients: " . implode(', ', $groups));
707 * @param string $profile_url
708 * @return Ostatus_profile
709 * @throws FeedSubException
712 public static function ensureProfileURL($profile_url, $hints=array())
714 $oprofile = self::getFromProfileURL($profile_url);
716 if (!empty($oprofile)) {
720 $hints['profileurl'] = $profile_url;
725 $client = new HTTPClient();
726 $client->setHeader('Accept', 'text/html,application/xhtml+xml');
727 $response = $client->get($profile_url);
729 if (!$response->isOk()) {
733 // Check if we have a non-canonical URL
735 $finalUrl = $response->getUrl();
737 if ($finalUrl != $profile_url) {
739 $hints['profileurl'] = $finalUrl;
741 $oprofile = self::getFromProfileURL($finalUrl);
743 if (!empty($oprofile)) {
748 // Try to get some hCard data
750 $body = $response->getBody();
752 $hcardHints = DiscoveryHints::hcardHints($body, $finalUrl);
754 if (!empty($hcardHints)) {
755 $hints = array_merge($hints, $hcardHints);
758 // Check if they've got an LRDD header
760 $lrdd = LinkHeader::getLink($response, 'lrdd', 'application/xrd+xml');
764 $xrd = Discovery::fetchXrd($lrdd);
765 $xrdHints = DiscoveryHints::fromXRD($xrd);
767 $hints = array_merge($hints, $xrdHints);
770 // If discovery found a feedurl (probably from LRDD), use it.
772 if (array_key_exists('feedurl', $hints)) {
773 return self::ensureFeedURL($hints['feedurl'], $hints);
776 // Get the feed URL from HTML
778 $discover = new FeedDiscovery();
780 $feedurl = $discover->discoverFromHTML($finalUrl, $body);
782 if (!empty($feedurl)) {
783 $hints['feedurl'] = $feedurl;
785 return self::ensureFeedURL($feedurl, $hints);
789 static function getFromProfileURL($profile_url)
791 $profile = Profile::staticGet('profileurl', $profile_url);
793 if (empty($profile)) {
797 // Is it a known Ostatus profile?
799 $oprofile = Ostatus_profile::staticGet('profile_id', $profile->id);
801 if (!empty($oprofile)) {
805 // Is it a local user?
807 $user = User::staticGet('id', $profile->id);
810 throw new Exception("'$profile_url' is the profile for local user '{$user->nickname}'.");
813 // Continue discovery; it's a remote profile
814 // for OMB or some other protocol, may also
820 public static function ensureFeedURL($feed_url, $hints=array())
822 $discover = new FeedDiscovery();
824 $feeduri = $discover->discoverFromFeedURL($feed_url);
825 $hints['feedurl'] = $feeduri;
827 $huburi = $discover->getAtomLink('hub');
828 $hints['hub'] = $huburi;
829 $salmonuri = $discover->getAtomLink(Salmon::NS_REPLIES);
830 $hints['salmon'] = $salmonuri;
833 // We can only deal with folks with a PuSH hub
834 throw new FeedSubNoHubException();
837 // Try to get a profile from the feed activity:subject
839 $feedEl = $discover->feed->documentElement;
841 $subject = ActivityUtils::child($feedEl, Activity::SUBJECT, Activity::SPEC);
843 if (!empty($subject)) {
844 $subjObject = new ActivityObject($subject);
845 return self::ensureActivityObjectProfile($subjObject, $hints);
848 // Otherwise, try the feed author
850 $author = ActivityUtils::child($feedEl, Activity::AUTHOR, Activity::ATOM);
852 if (!empty($author)) {
853 $authorObject = new ActivityObject($author);
854 return self::ensureActivityObjectProfile($authorObject, $hints);
857 // Sheesh. Not a very nice feed! Let's try fingerpoken in the
860 $entries = $discover->feed->getElementsByTagNameNS(Activity::ATOM, 'entry');
862 if (!empty($entries) && $entries->length > 0) {
864 $entry = $entries->item(0);
866 $actor = ActivityUtils::child($entry, Activity::ACTOR, Activity::SPEC);
868 if (!empty($actor)) {
869 $actorObject = new ActivityObject($actor);
870 return self::ensureActivityObjectProfile($actorObject, $hints);
874 $author = ActivityUtils::child($entry, Activity::AUTHOR, Activity::ATOM);
876 if (!empty($author)) {
877 $authorObject = new ActivityObject($author);
878 return self::ensureActivityObjectProfile($authorObject, $hints);
882 // XXX: make some educated guesses here
884 throw new FeedSubException("Can't find enough profile information to make a feed.");
889 * Download and update given avatar image
891 * @throws Exception in various failure cases
893 protected function updateAvatar($url)
895 if ($url == $this->avatar) {
896 // We've already got this one.
900 if ($this->isGroup()) {
901 $self = $this->localGroup();
903 $self = $this->localProfile();
906 throw new ServerException(sprintf(
907 _m("Tried to update avatar for unsaved remote profile %s"),
911 // @fixme this should be better encapsulated
912 // ripped from oauthstore.php (for old OMB client)
913 $temp_filename = tempnam(sys_get_temp_dir(), 'listener_avatar');
914 if (!copy($url, $temp_filename)) {
915 throw new ServerException(sprintf(_m("Unable to fetch avatar from %s"), $url));
918 if ($this->isGroup()) {
919 $id = $this->group_id;
921 $id = $this->profile_id;
923 // @fixme should we be using different ids?
924 $imagefile = new ImageFile($id, $temp_filename);
925 $filename = Avatar::filename($id,
926 image_type_to_extension($imagefile->type),
929 rename($temp_filename, Avatar::path($filename));
930 $self->setOriginal($filename);
932 $orig = clone($this);
933 $this->avatar = $url;
934 $this->update($orig);
938 * Pull avatar URL from ActivityObject or profile hints
940 * @param ActivityObject $object
941 * @param array $hints
942 * @return mixed URL string or false
945 protected static function getActivityObjectAvatar($object, $hints=array())
947 if ($object->avatarLinks) {
949 // Take the exact-size avatar, or the largest avatar, or the first avatar if all sizeless
950 foreach ($object->avatarLinks as $avatar) {
951 if ($avatar->width == AVATAR_PROFILE_SIZE && $avatar->height = AVATAR_PROFILE_SIZE) {
956 if (!$best || $avatar->width > $best->width) {
961 } else if (array_key_exists('avatar', $hints)) {
962 return $hints['avatar'];
968 * Get an appropriate avatar image source URL, if available.
970 * @param ActivityObject $actor
971 * @param DOMElement $feed
975 protected static function getAvatar($actor, $feed)
979 if ($actor->avatar) {
980 $url = trim($actor->avatar);
983 // Check <atom:logo> and <atom:icon> on the feed
984 $els = $feed->childNodes();
985 if ($els && $els->length) {
986 for ($i = 0; $i < $els->length; $i++) {
987 $el = $els->item($i);
988 if ($el->namespaceURI == Activity::ATOM) {
989 if (empty($url) && $el->localName == 'logo') {
990 $url = trim($el->textContent);
993 if (empty($icon) && $el->localName == 'icon') {
995 $icon = trim($el->textContent);
1000 if ($icon && !$url) {
1005 $opts = array('allowed_schemes' => array('http', 'https'));
1006 if (Validate::uri($url, $opts)) {
1010 return common_path('plugins/OStatus/images/96px-Feed-icon.svg.png');
1014 * Fetch, or build if necessary, an Ostatus_profile for the actor
1015 * in a given Activity Streams activity.
1017 * @param Activity $activity
1018 * @param string $feeduri if we already know the canonical feed URI!
1019 * @param string $salmonuri if we already know the salmon return channel URI
1020 * @return Ostatus_profile
1023 public static function ensureActorProfile($activity, $hints=array())
1025 return self::ensureActivityObjectProfile($activity->actor, $hints);
1028 public static function ensureActivityObjectProfile($object, $hints=array())
1030 $profile = self::getActivityObjectProfile($object);
1032 $profile->updateFromActivityObject($object, $hints);
1034 $profile = self::createActivityObjectProfile($object, $hints);
1040 * @param Activity $activity
1041 * @return mixed matching Ostatus_profile or false if none known
1043 public static function getActorProfile($activity)
1045 return self::getActivityObjectProfile($activity->actor);
1048 protected static function getActivityObjectProfile($object)
1050 $uri = self::getActivityObjectProfileURI($object);
1051 return Ostatus_profile::staticGet('uri', $uri);
1054 protected static function getActorProfileURI($activity)
1056 return self::getActivityObjectProfileURI($activity->actor);
1060 * @param Activity $activity
1062 * @throws ServerException
1064 protected static function getActivityObjectProfileURI($object)
1066 $opts = array('allowed_schemes' => array('http', 'https'));
1067 if ($object->id && Validate::uri($object->id, $opts)) {
1070 if ($object->link && Validate::uri($object->link, $opts)) {
1071 return $object->link;
1073 throw new ServerException("No author ID URI found");
1077 * @fixme validate stuff somewhere
1081 * Create local ostatus_profile and profile/user_group entries for
1082 * the provided remote user or group.
1084 * @param ActivityObject $object
1085 * @param array $hints
1087 * @return Ostatus_profile
1089 protected static function createActivityObjectProfile($object, $hints=array())
1091 $homeuri = $object->id;
1095 common_log(LOG_DEBUG, __METHOD__ . " empty actor profile URI: " . var_export($activity, true));
1096 throw new Exception("No profile URI");
1099 if (OStatusPlugin::localProfileFromUrl($homeuri)) {
1100 throw new Exception("Local user can't be referenced as remote.");
1103 if (OStatusPlugin::localGroupFromUrl($homeuri)) {
1104 throw new Exception("Local group can't be referenced as remote.");
1107 if (array_key_exists('feedurl', $hints)) {
1108 $feeduri = $hints['feedurl'];
1110 $discover = new FeedDiscovery();
1111 $feeduri = $discover->discoverFromURL($homeuri);
1114 if (array_key_exists('salmon', $hints)) {
1115 $salmonuri = $hints['salmon'];
1118 $discover = new FeedDiscovery();
1119 $discover->discoverFromFeedURL($hints['feedurl']);
1121 $salmonuri = $discover->getAtomLink(Salmon::NS_REPLIES);
1124 if (array_key_exists('hub', $hints)) {
1125 $huburi = $hints['hub'];
1128 $discover = new FeedDiscovery();
1129 $discover->discoverFromFeedURL($hints['feedurl']);
1131 $huburi = $discover->getAtomLink('hub');
1135 // We can only deal with folks with a PuSH hub
1136 throw new FeedSubNoHubException();
1139 $oprofile = new Ostatus_profile();
1141 $oprofile->uri = $homeuri;
1142 $oprofile->feeduri = $feeduri;
1143 $oprofile->salmonuri = $salmonuri;
1145 $oprofile->created = common_sql_now();
1146 $oprofile->modified = common_sql_now();
1148 if ($object->type == ActivityObject::PERSON) {
1149 $profile = new Profile();
1150 $profile->created = common_sql_now();
1151 self::updateProfile($profile, $object, $hints);
1153 $oprofile->profile_id = $profile->insert();
1154 if (!$oprofile->profile_id) {
1155 throw new ServerException("Can't save local profile");
1158 $group = new User_group();
1159 $group->uri = $homeuri;
1160 $group->created = common_sql_now();
1161 self::updateGroup($group, $object, $hints);
1163 $oprofile->group_id = $group->insert();
1164 if (!$oprofile->group_id) {
1165 throw new ServerException("Can't save local profile");
1169 $ok = $oprofile->insert();
1172 $avatar = self::getActivityObjectAvatar($object, $hints);
1174 $oprofile->updateAvatar($avatar);
1178 throw new ServerException("Can't save OStatus profile");
1183 * Save any updated profile information to our local copy.
1184 * @param ActivityObject $object
1185 * @param array $hints
1187 public function updateFromActivityObject($object, $hints=array())
1189 if ($this->isGroup()) {
1190 $group = $this->localGroup();
1191 self::updateGroup($group, $object, $hints);
1193 $profile = $this->localProfile();
1194 self::updateProfile($profile, $object, $hints);
1196 $avatar = self::getActivityObjectAvatar($object, $hints);
1198 $this->updateAvatar($avatar);
1202 protected static function updateProfile($profile, $object, $hints=array())
1204 $orig = clone($profile);
1206 $profile->nickname = self::getActivityObjectNickname($object, $hints);
1208 if (!empty($object->title)) {
1209 $profile->fullname = $object->title;
1210 } else if (array_key_exists('fullname', $hints)) {
1211 $profile->fullname = $hints['fullname'];
1214 if (!empty($object->link)) {
1215 $profile->profileurl = $object->link;
1216 } else if (array_key_exists('profileurl', $hints)) {
1217 $profile->profileurl = $hints['profileurl'];
1218 } else if (Validate::uri($object->id, array('allowed_schemes' => array('http', 'https')))) {
1219 $profile->profileurl = $object->id;
1222 $profile->bio = self::getActivityObjectBio($object, $hints);
1223 $profile->location = self::getActivityObjectLocation($object, $hints);
1224 $profile->homepage = self::getActivityObjectHomepage($object, $hints);
1226 if (!empty($object->geopoint)) {
1227 $location = ActivityContext::locationFromPoint($object->geopoint);
1228 if (!empty($location)) {
1229 $profile->lat = $location->lat;
1230 $profile->lon = $location->lon;
1234 // @fixme tags/categories
1235 // @todo tags from categories
1238 common_log(LOG_DEBUG, "Updating OStatus profile $profile->id from remote info $object->id: " . var_export($object, true) . var_export($hints, true));
1239 $profile->update($orig);
1243 protected static function updateGroup($group, $object, $hints=array())
1245 $orig = clone($group);
1247 $group->nickname = self::getActivityObjectNickname($object, $hints);
1248 $group->fullname = $object->title;
1250 if (!empty($object->link)) {
1251 $group->mainpage = $object->link;
1252 } else if (array_key_exists('profileurl', $hints)) {
1253 $group->mainpage = $hints['profileurl'];
1256 // @todo tags from categories
1257 $group->description = self::getActivityObjectBio($object, $hints);
1258 $group->location = self::getActivityObjectLocation($object, $hints);
1259 $group->homepage = self::getActivityObjectHomepage($object, $hints);
1262 common_log(LOG_DEBUG, "Updating OStatus group $group->id from remote info $object->id: " . var_export($object, true) . var_export($hints, true));
1263 $group->update($orig);
1267 protected static function getActivityObjectHomepage($object, $hints=array())
1270 $poco = $object->poco;
1272 if (!empty($poco)) {
1273 $url = $poco->getPrimaryURL();
1274 if ($url && $url->type == 'homepage') {
1275 $homepage = $url->value;
1279 // @todo Try for a another PoCo URL?
1284 protected static function getActivityObjectLocation($object, $hints=array())
1288 if (!empty($object->poco) &&
1289 isset($object->poco->address->formatted)) {
1290 $location = $object->poco->address->formatted;
1291 } else if (array_key_exists('location', $hints)) {
1292 $location = $hints['location'];
1295 if (!empty($location)) {
1296 if (mb_strlen($location) > 255) {
1297 $location = mb_substr($note, 0, 255 - 3) . ' … ';
1301 // @todo Try to find location some othe way? Via goerss point?
1306 protected static function getActivityObjectBio($object, $hints=array())
1310 if (!empty($object->poco)) {
1311 $note = $object->poco->note;
1312 } else if (array_key_exists('bio', $hints)) {
1313 $note = $hints['bio'];
1316 if (!empty($note)) {
1317 if (Profile::bioTooLong($note)) {
1318 // XXX: truncate ok?
1319 $bio = mb_substr($note, 0, Profile::maxBio() - 3) . ' … ';
1325 // @todo Try to get bio info some other way?
1330 protected static function getActivityObjectNickname($object, $hints=array())
1332 if ($object->poco) {
1333 if (!empty($object->poco->preferredUsername)) {
1334 return common_nicknamize($object->poco->preferredUsername);
1338 if (!empty($object->nickname)) {
1339 return common_nicknamize($object->nickname);
1342 if (array_key_exists('nickname', $hints)) {
1343 return $hints['nickname'];
1346 // Try the definitive ID
1348 $nickname = self::nicknameFromURI($object->id);
1350 // Try a Webfinger if one was passed (way) down
1352 if (empty($nickname)) {
1353 if (array_key_exists('webfinger', $hints)) {
1354 $nickname = self::nicknameFromURI($hints['webfinger']);
1360 if (empty($nickname)) {
1361 $nickname = common_nicknamize($object->title);
1367 protected static function nicknameFromURI($uri)
1369 preg_match('/(\w+):/', $uri, $matches);
1371 $protocol = $matches[1];
1373 switch ($protocol) {
1376 if (preg_match("/^$protocol:(.*)?@.*\$/", $uri, $matches)) {
1377 return common_canonical_nickname($matches[1]);
1381 return common_url_to_nickname($uri);
1389 * @param string $addr webfinger address
1390 * @return Ostatus_profile
1391 * @throws Exception on error conditions
1393 public static function ensureWebfinger($addr)
1395 // First, try the cache
1397 $uri = self::cacheGet(sprintf('ostatus_profile:webfinger:%s', $addr));
1399 if ($uri !== false) {
1400 if (is_null($uri)) {
1401 // Negative cache entry
1402 throw new Exception('Not a valid webfinger address.');
1404 $oprofile = Ostatus_profile::staticGet('uri', $uri);
1405 if (!empty($oprofile)) {
1410 // Try looking it up
1412 $oprofile = Ostatus_profile::staticGet('uri', 'acct:'.$addr);
1414 if (!empty($oprofile)) {
1415 self::cacheSet(sprintf('ostatus_profile:webfinger:%s', $addr), $oprofile->uri);
1419 // Now, try some discovery
1421 $disco = new Discovery();
1424 $xrd = $disco->lookup($addr);
1425 } catch (Exception $e) {
1426 // Save negative cache entry so we don't waste time looking it up again.
1427 // @fixme distinguish temporary failures?
1428 self::cacheSet(sprintf('ostatus_profile:webfinger:%s', $addr), null);
1429 throw new Exception('Not a valid webfinger address.');
1432 $hints = array('webfinger' => $addr);
1434 $dhints = DiscoveryHints::fromXRD($xrd);
1436 $hints = array_merge($hints, $dhints);
1438 // If there's an Hcard, let's grab its info
1440 if (array_key_exists('hcard', $hints)) {
1441 if (!array_key_exists('profileurl', $hints) ||
1442 $hints['hcard'] != $hints['profileurl']) {
1443 $hcardHints = DiscoveryHints::fromHcardUrl($hints['hcard']);
1444 $hints = array_merge($hcardHints, $hints);
1448 // If we got a feed URL, try that
1450 if (array_key_exists('feedurl', $hints)) {
1452 common_log(LOG_INFO, "Discovery on acct:$addr with feed URL $feedUrl");
1453 $oprofile = self::ensureFeedURL($hints['feedurl'], $hints);
1454 self::cacheSet(sprintf('ostatus_profile:webfinger:%s', $addr), $oprofile->uri);
1456 } catch (Exception $e) {
1457 common_log(LOG_WARNING, "Failed creating profile from feed URL '$feedUrl': " . $e->getMessage());
1462 // If we got a profile page, try that!
1464 if (array_key_exists('profileurl', $hints)) {
1466 common_log(LOG_INFO, "Discovery on acct:$addr with profile URL $profileUrl");
1467 $oprofile = self::ensureProfileURL($hints['profileurl'], $hints);
1468 self::cacheSet(sprintf('ostatus_profile:webfinger:%s', $addr), $oprofile->uri);
1470 } catch (Exception $e) {
1471 common_log(LOG_WARNING, "Failed creating profile from profile URL '$profileUrl': " . $e->getMessage());
1479 if (array_key_exists('salmon', $hints)) {
1481 $salmonEndpoint = $hints['salmon'];
1483 // An account URL, a salmon endpoint, and a dream? Not much to go
1484 // on, but let's give it a try
1486 $uri = 'acct:'.$addr;
1488 $profile = new Profile();
1490 $profile->nickname = self::nicknameFromUri($uri);
1491 $profile->created = common_sql_now();
1493 if (isset($profileUrl)) {
1494 $profile->profileurl = $profileUrl;
1497 $profile_id = $profile->insert();
1500 common_log_db_error($profile, 'INSERT', __FILE__);
1501 throw new Exception("Couldn't save profile for '$addr'");
1504 $oprofile = new Ostatus_profile();
1506 $oprofile->uri = $uri;
1507 $oprofile->salmonuri = $salmonEndpoint;
1508 $oprofile->profile_id = $profile_id;
1509 $oprofile->created = common_sql_now();
1511 if (isset($feedUrl)) {
1512 $profile->feeduri = $feedUrl;
1515 $result = $oprofile->insert();
1518 common_log_db_error($oprofile, 'INSERT', __FILE__);
1519 throw new Exception("Couldn't save ostatus_profile for '$addr'");
1522 self::cacheSet(sprintf('ostatus_profile:webfinger:%s', $addr), $oprofile->uri);
1526 throw new Exception("Couldn't find a valid profile for '$addr'");
1529 function saveHTMLFile($title, $rendered)
1531 $final = sprintf("<!DOCTYPE html>\n<html><head><title>%s</title></head>".
1532 '<body><div>%s</div></body></html>',
1533 htmlspecialchars($title),
1536 $filename = File::filename($this->localProfile(),
1537 'ostatus', // ignored?
1540 $filepath = File::path($filename);
1542 file_put_contents($filepath, $final);
1546 $file->filename = $filename;
1547 $file->url = File::url($filename);
1548 $file->size = filesize($filepath);
1549 $file->date = time();
1550 $file->mimetype = 'text/html';
1552 $file_id = $file->insert();
1554 if ($file_id === false) {
1555 common_log_db_error($file, "INSERT", __FILE__);
1556 throw new ServerException(_('Could not store HTML content of long post as file.'));