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/>.
20 if (!defined('STATUSNET')) {
25 * @package OStatusPlugin
26 * @maintainer Brion Vibber <brion@status.net>
28 class Ostatus_profile extends Managed_DataObject
30 public $__table = 'ostatus_profile';
40 public $avatar; // remote URL of the last avatar we saved
46 * Return table definition for Schema setup and DB_DataObject usage.
48 * @return array array of column definitions
50 static function schemaDef()
54 'uri' => array('type' => 'varchar', 'length' => 255, 'not null' => true),
55 'profile_id' => array('type' => 'integer'),
56 'group_id' => array('type' => 'integer'),
57 'peopletag_id' => array('type' => 'integer'),
58 'feeduri' => array('type' => 'varchar', 'length' => 255),
59 'salmonuri' => array('type' => 'varchar', 'length' => 255),
60 'avatar' => array('type' => 'text'),
61 'created' => array('type' => 'datetime', 'not null' => true),
62 'modified' => array('type' => 'datetime', 'not null' => true),
64 'primary key' => array('uri'),
65 'unique keys' => array(
66 'ostatus_profile_profile_id_key' => array('profile_id'),
67 'ostatus_profile_group_id_key' => array('group_id'),
68 'ostatus_profile_peopletag_id_key' => array('peopletag_id'),
69 'ostatus_profile_feeduri_key' => array('feeduri'),
71 'foreign keys' => array(
72 'ostatus_profile_profile_id_fkey' => array('profile', array('profile_id' => 'id')),
73 'ostatus_profile_group_id_fkey' => array('user_group', array('group_id' => 'id')),
74 'ostatus_profile_peopletag_id_fkey' => array('profile_list', array('peopletag_id' => 'id')),
79 public function getUri()
84 public function fromProfile(Profile $profile)
86 $oprofile = Ostatus_profile::getKV('profile_id', $profile->id);
87 if (!$oprofile instanceof Ostatus_profile) {
88 throw new Exception('No Ostatus_profile for Profile ID: '.$profile->id);
93 * Fetch the locally stored profile for this feed
95 * @throws NoProfileException if it was not found
97 public function localProfile()
99 if ($this->isGroup()) {
100 return $this->localGroup()->getProfile();
103 $profile = Profile::getKV('id', $this->profile_id);
104 if (!$profile instanceof Profile) {
105 throw new NoProfileException($this->profile_id);
111 * Fetch the StatusNet-side profile for this feed
114 public function localGroup()
116 $group = User_group::getKV('id', $this->group_id);
118 if (!$group instanceof User_group) {
119 throw new NoSuchGroupException(array('id'=>$this->group_id));
126 * Fetch the StatusNet-side peopletag for this feed
129 public function localPeopletag()
131 if ($this->peopletag_id) {
132 return Profile_list::getKV('id', $this->peopletag_id);
138 * Returns an ActivityObject describing this remote user or group profile.
139 * Can then be used to generate Atom chunks.
141 * @return ActivityObject
143 function asActivityObject()
145 if ($this->isGroup()) {
146 return ActivityObject::fromGroup($this->localGroup());
147 } else if ($this->isPeopletag()) {
148 return ActivityObject::fromPeopletag($this->localPeopletag());
150 return $this->localProfile()->asActivityObject();
155 * Returns an XML string fragment with profile information as an
156 * Activity Streams noun object with the given element type.
158 * Assumes that 'activity' namespace has been previously defined.
160 * @todo FIXME: Replace with wrappers on asActivityObject when it's got everything.
162 * @param string $element one of 'actor', 'subject', 'object', 'target'
165 function asActivityNoun($element)
167 if ($this->isGroup()) {
168 $noun = ActivityObject::fromGroup($this->localGroup());
169 return $noun->asString('activity:' . $element);
170 } else if ($this->isPeopletag()) {
171 $noun = ActivityObject::fromPeopletag($this->localPeopletag());
172 return $noun->asString('activity:' . $element);
174 $noun = $this->localProfile()->asActivityObject();
175 return $noun->asString('activity:' . $element);
180 * @return boolean true if this is a remote group
184 if ($this->profile_id || $this->peopletag_id && !$this->group_id) {
186 } else if ($this->group_id && !$this->profile_id && !$this->peopletag_id) {
188 } else if ($this->group_id && ($this->profile_id || $this->peopletag_id)) {
189 // TRANS: Server exception. %s is a URI
190 throw new ServerException(sprintf(_m('Invalid ostatus_profile state: Two or more IDs set for %s.'), $this->getUri()));
192 // TRANS: Server exception. %s is a URI
193 throw new ServerException(sprintf(_m('Invalid ostatus_profile state: All IDs empty for %s.'), $this->getUri()));
198 * @return boolean true if this is a remote peopletag
200 function isPeopletag()
202 if ($this->profile_id || $this->group_id && !$this->peopletag_id) {
204 } else if ($this->peopletag_id && !$this->profile_id && !$this->group_id) {
206 } else if ($this->peopletag_id && ($this->profile_id || $this->group_id)) {
207 // TRANS: Server exception. %s is a URI
208 throw new ServerException(sprintf(_m('Invalid ostatus_profile state: Two or more IDs set for %s.'), $this->getUri()));
210 // TRANS: Server exception. %s is a URI
211 throw new ServerException(sprintf(_m('Invalid ostatus_profile state: All IDs empty for %s.'), $this->getUri()));
216 * Send a subscription request to the hub for this feed.
217 * The hub will later send us a confirmation POST to /main/push/callback.
220 * @throws ServerException if feed state is not valid or subscription fails.
222 public function subscribe()
224 $feedsub = FeedSub::ensureFeed($this->feeduri);
225 if ($feedsub->sub_state == 'active') {
226 // Active subscription, we don't need to do anything.
230 // Inactive or we got left in an inconsistent state.
231 // Run a subscription request to make sure we're current!
232 return $feedsub->subscribe();
236 * Check if this remote profile has any active local subscriptions, and
237 * if not drop the PuSH subscription feed.
239 * @return boolean true if subscription is removed, false if there are still subscribers to the feed
240 * @throws Exception of various kinds on failure.
242 public function unsubscribe() {
243 return $this->garbageCollect();
247 * Check if this remote profile has any active local subscriptions, and
248 * if not drop the PuSH subscription feed.
250 * @return boolean true if subscription is removed, false if there are still subscribers to the feed
251 * @throws Exception of various kinds on failure.
253 public function garbageCollect()
255 $feedsub = FeedSub::getKV('uri', $this->feeduri);
256 if ($feedsub instanceof FeedSub) {
257 return $feedsub->garbageCollect();
259 // Since there's no FeedSub we can assume it's already garbage collected
264 * Check if this remote profile has any active local subscriptions, so the
265 * PuSH subscription layer can decide if it can drop the feed.
267 * This gets called via the FeedSubSubscriberCount event when running
268 * FeedSub::garbageCollect().
271 * @throws NoProfileException if there is no local profile for the object
273 public function subscriberCount()
275 if ($this->isGroup()) {
276 $members = $this->localGroup()->getMembers(0, 1);
277 $count = $members->N;
278 } else if ($this->isPeopletag()) {
279 $subscribers = $this->localPeopletag()->getSubscribers(0, 1);
280 $count = $subscribers->N;
282 $profile = $this->localProfile();
283 if ($profile->hasLocalTags()) {
286 $count = $profile->subscriberCount();
289 common_log(LOG_INFO, __METHOD__ . " SUB COUNT BEFORE: $count");
291 // Other plugins may be piggybacking on OStatus without having
292 // an active group or user-to-user subscription we know about.
293 Event::handle('Ostatus_profileSubscriberCount', array($this, &$count));
294 common_log(LOG_INFO, __METHOD__ . " SUB COUNT AFTER: $count");
300 * Send an Activity Streams notification to the remote Salmon endpoint,
303 * @param Profile $actor Actor who did the activity
304 * @param string $verb Activity::SUBSCRIBE or Activity::JOIN
305 * @param Object $object object of the action; must define asActivityNoun($tag)
307 public function notify(Profile $actor, $verb, $object=null, $target=null)
309 if ($object == null) {
312 if (empty($this->salmonuri)) {
316 $id = TagURI::mint('%s:%s:%s',
319 common_date_iso8601(time()));
321 // @todo FIXME: Consolidate all these NS settings somewhere.
322 $attributes = array('xmlns' => Activity::ATOM,
323 'xmlns:activity' => 'http://activitystrea.ms/spec/1.0/',
324 'xmlns:thr' => 'http://purl.org/syndication/thread/1.0',
325 'xmlns:georss' => 'http://www.georss.org/georss',
326 'xmlns:ostatus' => 'http://ostatus.org/schema/1.0',
327 'xmlns:poco' => 'http://portablecontacts.net/spec/1.0',
328 'xmlns:media' => 'http://purl.org/syndication/atommedia');
330 $entry = new XMLStringer();
331 $entry->elementStart('entry', $attributes);
332 $entry->element('id', null, $id);
333 $entry->element('title', null, $text);
334 $entry->element('summary', null, $text);
335 $entry->element('published', null, common_date_w3dtf(common_sql_now()));
337 $entry->element('activity:verb', null, $verb);
338 $entry->raw($actor->asAtomAuthor());
339 $entry->raw($actor->asActivityActor());
340 $entry->raw($object->asActivityNoun('object'));
341 if ($target != null) {
342 $entry->raw($target->asActivityNoun('target'));
344 $entry->elementEnd('entry');
346 $xml = $entry->getString();
347 common_log(LOG_INFO, "Posting to Salmon endpoint $this->salmonuri: $xml");
349 Salmon::post($this->salmonuri, $xml, $actor->getUser());
353 * Send a Salmon notification ping immediately, and confirm that we got
354 * an acceptable response from the remote site.
356 * @param mixed $entry XML string, Notice, or Activity
357 * @param Profile $actor
358 * @return boolean success
360 public function notifyActivity($entry, Profile $actor)
362 if ($this->salmonuri) {
363 return Salmon::post($this->salmonuri, $this->notifyPrepXml($entry), $actor->getUser());
365 common_debug(__CLASS__.' error: No salmonuri for Ostatus_profile uri: '.$this->uri);
371 * Queue a Salmon notification for later. If queues are disabled we'll
372 * send immediately but won't get the return value.
374 * @param mixed $entry XML string, Notice, or Activity
375 * @return boolean success
377 public function notifyDeferred($entry, $actor)
379 if ($this->salmonuri) {
380 $data = array('salmonuri' => $this->salmonuri,
381 'entry' => $this->notifyPrepXml($entry),
382 'actor' => $actor->id);
384 $qm = QueueManager::get();
385 return $qm->enqueue($data, 'salmon');
391 protected function notifyPrepXml($entry)
393 $preamble = '<?xml version="1.0" encoding="UTF-8" ?' . '>';
394 if (is_string($entry)) {
396 } else if ($entry instanceof Activity) {
397 return $preamble . $entry->asString(true);
398 } else if ($entry instanceof Notice) {
399 return $preamble . $entry->asAtomEntry(true, true);
401 // TRANS: Server exception.
402 throw new ServerException(_m('Invalid type passed to Ostatus_profile::notify. It must be XML string or Activity entry.'));
406 function getBestName()
408 if ($this->isGroup()) {
409 return $this->localGroup()->getBestName();
410 } else if ($this->isPeopletag()) {
411 return $this->localPeopletag()->getBestName();
413 return $this->localProfile()->getBestName();
418 * Read and post notices for updates from the feed.
419 * Currently assumes that all items in the feed are new,
420 * coming from a PuSH hub.
422 * @param DOMDocument $doc
423 * @param string $source identifier ("push")
425 public function processFeed(DOMDocument $doc, $source)
427 $feed = $doc->documentElement;
429 if ($feed->localName == 'feed' && $feed->namespaceURI == Activity::ATOM) {
430 $this->processAtomFeed($feed, $source);
431 } else if ($feed->localName == 'rss') { // @todo FIXME: Check namespace.
432 $this->processRssFeed($feed, $source);
435 throw new Exception(_m('Unknown feed format.'));
439 public function processAtomFeed(DOMElement $feed, $source)
441 $entries = $feed->getElementsByTagNameNS(Activity::ATOM, 'entry');
442 if ($entries->length == 0) {
443 common_log(LOG_ERR, __METHOD__ . ": no entries in feed update, ignoring");
447 for ($i = 0; $i < $entries->length; $i++) {
448 $entry = $entries->item($i);
449 $this->processEntry($entry, $feed, $source);
453 public function processRssFeed(DOMElement $rss, $source)
455 $channels = $rss->getElementsByTagName('channel');
457 if ($channels->length == 0) {
459 throw new Exception(_m('RSS feed without a channel.'));
460 } else if ($channels->length > 1) {
461 common_log(LOG_WARNING, __METHOD__ . ": more than one channel in an RSS feed");
464 $channel = $channels->item(0);
466 $items = $channel->getElementsByTagName('item');
468 for ($i = 0; $i < $items->length; $i++) {
469 $item = $items->item($i);
470 $this->processEntry($item, $channel, $source);
475 * Process a posted entry from this feed source.
477 * @param DOMElement $entry
478 * @param DOMElement $feed for context
479 * @param string $source identifier ("push" or "salmon")
481 * @return Notice Notice representing the new (or existing) activity
483 public function processEntry($entry, $feed, $source)
485 $activity = new Activity($entry, $feed);
486 return $this->processActivity($activity, $source);
489 // TODO: Make this throw an exception
490 public function processActivity($activity, $source)
494 // The "WithProfile" events were added later.
496 if (Event::handle('StartHandleFeedEntryWithProfile', array($activity, $this->localProfile(), &$notice)) &&
497 Event::handle('StartHandleFeedEntry', array($activity))) {
499 switch ($activity->verb) {
500 case ActivityVerb::POST:
501 // @todo process all activity objects
502 switch ($activity->objects[0]->type) {
503 case ActivityObject::ARTICLE:
504 case ActivityObject::BLOGENTRY:
505 case ActivityObject::NOTE:
506 case ActivityObject::STATUS:
507 case ActivityObject::COMMENT:
509 $notice = $this->processPost($activity, $source);
512 // TRANS: Client exception.
513 throw new ClientException(_m('Cannot handle that kind of post.'));
516 case ActivityVerb::SHARE:
517 $notice = $this->processShare($activity, $source);
520 common_log(LOG_INFO, "Ignoring activity with unrecognized verb $activity->verb");
523 Event::handle('EndHandleFeedEntry', array($activity));
524 Event::handle('EndHandleFeedEntryWithProfile', array($activity, $this, $notice));
530 public function processShare($activity, $method)
535 $profile = ActivityUtils::checkAuthorship($activity, $this->localProfile());
536 } catch (ServerException $e) {
540 // The id URI will be used as a unique identifier for the notice,
541 // protecting against duplicate saves. It isn't required to be a URL;
542 // tag: URIs for instance are found in Google Buzz feeds.
543 $dupe = Notice::getKV('uri', $activity->id);
544 if ($dupe instanceof Notice) {
545 common_log(LOG_INFO, "OStatus: ignoring duplicate post: {$activity->id}");
549 if (count($activity->objects) != 1) {
550 // TRANS: Client exception thrown when trying to share multiple activities at once.
551 throw new ClientException(_m('Can only handle share activities with exactly one object.'));
554 $shared = $activity->objects[0];
556 if (!$shared instanceof Activity) {
557 // TRANS: Client exception thrown when trying to share a non-activity object.
558 throw new ClientException(_m('Can only handle shared activities.'));
561 $sharedId = $shared->id;
562 if (!empty($shared->objects[0]->id)) {
563 // Because StatusNet since commit 8cc4660 sets $shared->id to a TagURI which
564 // fucks up federation, because the URI is no longer recognised by the origin.
565 // So we set it to the object ID if it exists, otherwise we trust $shared->id
566 $sharedId = $shared->objects[0]->id;
568 if (empty($sharedId)) {
569 throw new ClientException(_m('Shared activity does not have an id'));
572 // First check if we have the shared activity. This has to be done first, because
573 // we can't use these functions to "ensureActivityObjectProfile" of a local user,
574 // who might be the creator of the shared activity in question.
575 $sharedNotice = Notice::getKV('uri', $sharedId);
576 if (!$sharedNotice instanceof Notice) {
577 // If no locally stored notice is found, process it!
578 // TODO: Remember to check Deleted_notice!
579 // TODO: If a post is shared that we can't retrieve - what to do?
581 $other = self::ensureActivityObjectProfile($shared->actor);
582 $sharedNotice = $other->processActivity($shared, $method);
583 if (!$sharedNotice instanceof Notice) {
584 // And if we apparently can't get the shared notice, we'll abort the whole thing.
585 // TRANS: Client exception thrown when saving an activity share fails.
586 // TRANS: %s is a share ID.
587 throw new ClientException(sprintf(_m('Failed to save activity %s.'), $sharedId));
589 } catch (FeedSubException $e) {
590 // Remote feed could not be found or verified, should we
591 // transform this into an "RT @user Blah, blah, blah..."?
592 common_log(LOG_INFO, __METHOD__ . ' got a ' . get_class($e) . ': ' . $e->getMessage());
597 // We'll want to save a web link to the original notice, if provided.
600 if ($activity->link) {
601 $sourceUrl = $activity->link;
602 } else if ($activity->link) {
603 $sourceUrl = $activity->link;
604 } else if (preg_match('!^https?://!', $activity->id)) {
605 $sourceUrl = $activity->id;
608 // Use summary as fallback for content
610 if (!empty($activity->content)) {
611 $sourceContent = $activity->content;
612 } else if (!empty($activity->summary)) {
613 $sourceContent = $activity->summary;
614 } else if (!empty($activity->title)) {
615 $sourceContent = $activity->title;
617 // @todo FIXME: Fetch from $sourceUrl?
618 // TRANS: Client exception. %s is a source URI.
619 throw new ClientException(sprintf(_m('No content for notice %s.'), $activity->id));
622 // Get (safe!) HTML and text versions of the content
624 $rendered = $this->purify($sourceContent);
625 $content = common_strip_html($rendered);
627 $shortened = common_shorten_links($content);
629 // If it's too long, try using the summary, and make the
630 // HTML an attachment.
634 if (Notice::contentTooLong($shortened)) {
635 $attachment = $this->saveHTMLFile($activity->title, $rendered);
636 $summary = common_strip_html($activity->summary);
637 if (empty($summary)) {
640 $shortSummary = common_shorten_links($summary);
641 if (Notice::contentTooLong($shortSummary)) {
642 $url = common_shorten_url($sourceUrl);
643 $shortSummary = substr($shortSummary,
645 Notice::maxContent() - (mb_strlen($url) + 2));
646 $content = $shortSummary . ' ' . $url;
648 // We mark up the attachment link specially for the HTML output
649 // so we can fold-out the full version inline.
651 // @todo FIXME i18n: This tooltip will be saved with the site's default language
652 // TRANS: Shown when a notice is longer than supported and/or when attachments are present. At runtime
653 // TRANS: this will usually be replaced with localised text from StatusNet core messages.
654 $showMoreText = _m('Show more');
655 $attachUrl = common_local_url('attachment',
656 array('attachment' => $attachment->id));
657 $rendered = common_render_text($shortSummary) .
658 '<a href="' . htmlspecialchars($attachUrl) .'"'.
659 ' class="attachment more"' .
660 ' title="'. htmlspecialchars($showMoreText) . '">' .
666 $options = array('is_local' => Notice::REMOTE,
668 'uri' => $activity->id,
669 'rendered' => $rendered,
670 'replies' => array(),
672 'peopletags' => array(),
675 'repeat_of' => $sharedNotice->id,
676 'scope' => $sharedNotice->scope);
678 // Check for optional attributes...
680 if (!empty($activity->time)) {
681 $options['created'] = common_sql_date($activity->time);
684 if ($activity->context) {
685 // TODO: context->attention
686 list($options['groups'], $options['replies'])
687 = self::filterAttention($profile, $activity->context->attention);
689 // Maintain direct reply associations
690 // @todo FIXME: What about conversation ID?
691 if (!empty($activity->context->replyToID)) {
692 $orig = Notice::getKV('uri',
693 $activity->context->replyToID);
694 if ($orig instanceof Notice) {
695 $options['reply_to'] = $orig->id;
699 $location = $activity->context->location;
701 $options['lat'] = $location->lat;
702 $options['lon'] = $location->lon;
703 if ($location->location_id) {
704 $options['location_ns'] = $location->location_ns;
705 $options['location_id'] = $location->location_id;
710 if ($this->isPeopletag()) {
711 $options['peopletags'][] = $this->localPeopletag();
714 // Atom categories <-> hashtags
715 foreach ($activity->categories as $cat) {
717 $term = common_canonical_tag($cat->term);
719 $options['tags'][] = $term;
724 // Atom enclosures -> attachment URLs
725 foreach ($activity->enclosures as $href) {
726 // @todo FIXME: Save these locally or....?
727 $options['urls'][] = $href;
730 $notice = Notice::saveNew($profile->id,
739 * Process an incoming post activity from this remote feed.
740 * @param Activity $activity
741 * @param string $method 'push' or 'salmon'
742 * @return mixed saved Notice or false
743 * @todo FIXME: Break up this function, it's getting nasty long
745 public function processPost($activity, $method)
749 $profile = ActivityUtils::checkAuthorship($activity, $this->localProfile());
751 // It's not always an ActivityObject::NOTE, but... let's just say it is.
753 $note = $activity->objects[0];
755 // The id URI will be used as a unique identifier for the notice,
756 // protecting against duplicate saves. It isn't required to be a URL;
757 // tag: URIs for instance are found in Google Buzz feeds.
758 $sourceUri = $note->id;
759 $dupe = Notice::getKV('uri', $sourceUri);
760 if ($dupe instanceof Notice) {
761 common_log(LOG_INFO, "OStatus: ignoring duplicate post: $sourceUri");
765 // We'll also want to save a web link to the original notice, if provided.
768 $sourceUrl = $note->link;
769 } else if ($activity->link) {
770 $sourceUrl = $activity->link;
771 } else if (preg_match('!^https?://!', $note->id)) {
772 $sourceUrl = $note->id;
775 // Use summary as fallback for content
777 if (!empty($note->content)) {
778 $sourceContent = $note->content;
779 } else if (!empty($note->summary)) {
780 $sourceContent = $note->summary;
781 } else if (!empty($note->title)) {
782 $sourceContent = $note->title;
784 // @todo FIXME: Fetch from $sourceUrl?
785 // TRANS: Client exception. %s is a source URI.
786 throw new ClientException(sprintf(_m('No content for notice %s.'),$sourceUri));
789 // Get (safe!) HTML and text versions of the content
791 $rendered = $this->purify($sourceContent);
792 $content = common_strip_html($rendered);
794 $shortened = common_shorten_links($content);
796 // If it's too long, try using the summary, and make the
797 // HTML an attachment.
801 if (Notice::contentTooLong($shortened)) {
802 $attachment = $this->saveHTMLFile($note->title, $rendered);
803 $summary = common_strip_html($note->summary);
804 if (empty($summary)) {
807 $shortSummary = common_shorten_links($summary);
808 if (Notice::contentTooLong($shortSummary)) {
809 $url = common_shorten_url($sourceUrl);
810 $shortSummary = substr($shortSummary,
812 Notice::maxContent() - (mb_strlen($url) + 2));
813 $content = $shortSummary . ' ' . $url;
815 // We mark up the attachment link specially for the HTML output
816 // so we can fold-out the full version inline.
818 // @todo FIXME i18n: This tooltip will be saved with the site's default language
819 // TRANS: Shown when a notice is longer than supported and/or when attachments are present. At runtime
820 // TRANS: this will usually be replaced with localised text from StatusNet core messages.
821 $showMoreText = _m('Show more');
822 $attachUrl = common_local_url('attachment',
823 array('attachment' => $attachment->id));
824 $rendered = common_render_text($shortSummary) .
825 '<a href="' . htmlspecialchars($attachUrl) .'"'.
826 ' class="attachment more"' .
827 ' title="'. htmlspecialchars($showMoreText) . '">' .
833 $options = array('is_local' => Notice::REMOTE,
836 'rendered' => $rendered,
837 'replies' => array(),
839 'peopletags' => array(),
843 // Check for optional attributes...
845 if (!empty($activity->time)) {
846 $options['created'] = common_sql_date($activity->time);
849 if ($activity->context) {
850 // TODO: context->attention
851 list($options['groups'], $options['replies'])
852 = self::filterAttention($profile, $activity->context->attention);
854 // Maintain direct reply associations
855 // @todo FIXME: What about conversation ID?
856 if (!empty($activity->context->replyToID)) {
857 $orig = Notice::getKV('uri', $activity->context->replyToID);
858 if ($orig instanceof Notice) {
859 $options['reply_to'] = $orig->id;
862 if (!empty($activity->context->conversation)) {
863 // we store the URI here, Notice class can look it up later
864 $options['conversation'] = $activity->context->conversation;
867 $location = $activity->context->location;
869 $options['lat'] = $location->lat;
870 $options['lon'] = $location->lon;
871 if ($location->location_id) {
872 $options['location_ns'] = $location->location_ns;
873 $options['location_id'] = $location->location_id;
878 if ($this->isPeopletag()) {
879 $options['peopletags'][] = $this->localPeopletag();
882 // Atom categories <-> hashtags
883 foreach ($activity->categories as $cat) {
885 $term = common_canonical_tag($cat->term);
887 $options['tags'][] = $term;
892 // Atom enclosures -> attachment URLs
893 foreach ($activity->enclosures as $href) {
894 // @todo FIXME: Save these locally or....?
895 $options['urls'][] = $href;
899 $saved = Notice::saveNew($profile->id,
903 if ($saved instanceof Notice) {
904 Ostatus_source::saveNew($saved, $this, $method);
905 if (!empty($attachment)) {
906 File_to_post::processNew($attachment->id, $saved->id);
909 } catch (Exception $e) {
910 common_log(LOG_ERR, "OStatus save of remote message $sourceUri failed: " . $e->getMessage());
913 common_log(LOG_INFO, "OStatus saved remote message $sourceUri as notice id $saved->id");
920 protected function purify($html)
922 require_once INSTALLDIR.'/extlib/htmLawed/htmLawed.php';
923 $config = array('safe' => 1,
924 'deny_attribute' => 'id,style,on*');
925 return htmLawed($html, $config);
929 * Filters a list of recipient ID URIs to just those for local delivery.
930 * @param Profile local profile of sender
931 * @param array in/out &$attention_uris set of URIs, will be pruned on output
932 * @return array of group IDs
934 static public function filterAttention(Profile $sender, array $attention)
936 common_log(LOG_DEBUG, "Original reply recipients: " . implode(', ', array_keys($attention)));
939 foreach ($attention as $recipient=>$type) {
940 // Is the recipient a local user?
941 $user = User::getKV('uri', $recipient);
942 if ($user instanceof User) {
943 // @todo FIXME: Sender verification, spam etc?
944 $replies[] = $recipient;
948 // Is the recipient a local group?
949 // TODO: $group = User_group::getKV('uri', $recipient);
950 $id = OStatusPlugin::localGroupFromUrl($recipient);
952 $group = User_group::getKV('id', $id);
953 if ($group instanceof User_group) {
954 // Deliver to all members of this local group if allowed.
955 if ($sender->isMember($group)) {
956 $groups[] = $group->id;
958 common_log(LOG_DEBUG, sprintf('Skipping reply to local group %s as sender %d is not a member', $group->getNickname(), $sender->id));
962 common_log(LOG_DEBUG, "Skipping reply to bogus group $recipient");
966 // Is the recipient a remote user or group?
968 $oprofile = self::ensureProfileURI($recipient);
969 if ($oprofile->isGroup()) {
970 // Deliver to local members of this remote group.
971 // @todo FIXME: Sender verification?
972 $groups[] = $oprofile->group_id;
974 // may be canonicalized or something
975 $replies[] = $oprofile->getUri();
978 } catch (Exception $e) {
979 // Neither a recognizable local nor remote user!
980 common_log(LOG_DEBUG, "Skipping reply to unrecognized profile $recipient: " . $e->getMessage());
984 common_log(LOG_DEBUG, "Local reply recipients: " . implode(', ', $replies));
985 common_log(LOG_DEBUG, "Local group recipients: " . implode(', ', $groups));
986 return array($groups, $replies);
990 * Look up and if necessary create an Ostatus_profile for the remote entity
991 * with the given profile page URL. This should never return null -- you
992 * will either get an object or an exception will be thrown.
994 * @param string $profile_url
995 * @return Ostatus_profile
996 * @throws Exception on various error conditions
997 * @throws OStatusShadowException if this reference would obscure a local user/group
999 public static function ensureProfileURL($profile_url, array $hints=array())
1001 $oprofile = self::getFromProfileURL($profile_url);
1003 if ($oprofile instanceof Ostatus_profile) {
1007 $hints['profileurl'] = $profile_url;
1010 // XXX: HTTP caching
1012 $client = new HTTPClient();
1013 $client->setHeader('Accept', 'text/html,application/xhtml+xml');
1014 $response = $client->get($profile_url);
1016 if (!$response->isOk()) {
1017 // TRANS: Exception. %s is a profile URL.
1018 throw new Exception(sprintf(_m('Could not reach profile page %s.'),$profile_url));
1021 // Check if we have a non-canonical URL
1023 $finalUrl = $response->getUrl();
1025 if ($finalUrl != $profile_url) {
1027 $hints['profileurl'] = $finalUrl;
1029 $oprofile = self::getFromProfileURL($finalUrl);
1031 if ($oprofile instanceof Ostatus_profile) {
1036 // Try to get some hCard data
1038 $body = $response->getBody();
1040 $hcardHints = DiscoveryHints::hcardHints($body, $finalUrl);
1042 if (!empty($hcardHints)) {
1043 $hints = array_merge($hints, $hcardHints);
1046 // Check if they've got an LRDD header
1048 $lrdd = LinkHeader::getLink($response, 'lrdd');
1050 $xrd = new XML_XRD();
1051 $xrd->loadFile($lrdd);
1052 $xrdHints = DiscoveryHints::fromXRD($xrd);
1053 $hints = array_merge($hints, $xrdHints);
1054 } catch (Exception $e) {
1055 // No hints available from XRD
1058 // If discovery found a feedurl (probably from LRDD), use it.
1060 if (array_key_exists('feedurl', $hints)) {
1061 return self::ensureFeedURL($hints['feedurl'], $hints);
1064 // Get the feed URL from HTML
1066 $discover = new FeedDiscovery();
1068 $feedurl = $discover->discoverFromHTML($finalUrl, $body);
1070 if (!empty($feedurl)) {
1071 $hints['feedurl'] = $feedurl;
1072 return self::ensureFeedURL($feedurl, $hints);
1075 // TRANS: Exception. %s is a URL.
1076 throw new Exception(sprintf(_m('Could not find a feed URL for profile page %s.'),$finalUrl));
1080 * Look up the Ostatus_profile, if present, for a remote entity with the
1081 * given profile page URL. Will return null for both unknown and invalid
1084 * @return mixed Ostatus_profile or null
1085 * @throws OStatusShadowException for local profiles
1087 static function getFromProfileURL($profile_url)
1089 $profile = Profile::getKV('profileurl', $profile_url);
1090 if (!$profile instanceof Profile) {
1095 $oprofile = self::getFromProfile($profile);
1096 // We found the profile, return it!
1098 } catch (NoResultException $e) {
1099 // Could not find an OStatus profile, is it instead a local user?
1100 $user = User::getKV('id', $profile->id);
1101 if ($user instanceof User) {
1102 // @todo i18n FIXME: use sprintf and add i18n (?)
1103 throw new OStatusShadowException($profile, "'$profile_url' is the profile for local user '{$user->nickname}'.");
1107 // Continue discovery; it's a remote profile
1108 // for OMB or some other protocol, may also
1114 static function getFromProfile(Profile $profile)
1116 $oprofile = new Ostatus_profile();
1117 $oprofile->profile_id = $profile->id;
1118 if (!$oprofile->find(true)) {
1119 throw new NoResultException($oprofile);
1125 * Look up and if necessary create an Ostatus_profile for remote entity
1126 * with the given update feed. This should never return null -- you will
1127 * either get an object or an exception will be thrown.
1129 * @return Ostatus_profile
1132 public static function ensureFeedURL($feed_url, array $hints=array())
1134 $discover = new FeedDiscovery();
1136 $feeduri = $discover->discoverFromFeedURL($feed_url);
1137 $hints['feedurl'] = $feeduri;
1139 $huburi = $discover->getHubLink();
1140 $hints['hub'] = $huburi;
1142 // XXX: NS_REPLIES is deprecated anyway, so let's remove it in the future.
1143 $salmonuri = $discover->getAtomLink(Salmon::REL_SALMON)
1144 ?: $discover->getAtomLink(Salmon::NS_REPLIES);
1145 $hints['salmon'] = $salmonuri;
1147 if (!$huburi && !common_config('feedsub', 'fallback_hub') && !common_config('feedsub', 'nohub')) {
1148 // We can only deal with folks with a PuSH hub
1149 // unless we have something similar available locally.
1150 throw new FeedSubNoHubException();
1153 $feedEl = $discover->root;
1155 if ($feedEl->tagName == 'feed') {
1156 return self::ensureAtomFeed($feedEl, $hints);
1157 } else if ($feedEl->tagName == 'channel') {
1158 return self::ensureRssChannel($feedEl, $hints);
1160 throw new FeedSubBadXmlException($feeduri);
1165 * Look up and, if necessary, create an Ostatus_profile for the remote
1166 * profile with the given Atom feed - actually loaded from the feed.
1167 * This should never return null -- you will either get an object or
1168 * an exception will be thrown.
1170 * @param DOMElement $feedEl root element of a loaded Atom feed
1171 * @param array $hints additional discovery information passed from higher levels
1172 * @todo FIXME: Should this be marked public?
1173 * @return Ostatus_profile
1176 public static function ensureAtomFeed(DOMElement $feedEl, array $hints)
1178 $author = ActivityUtils::getFeedAuthor($feedEl);
1180 if (empty($author)) {
1181 // XXX: make some educated guesses here
1182 // TRANS: Feed sub exception.
1183 throw new FeedSubException(_m('Cannot find enough profile '.
1184 'information to make a feed.'));
1187 return self::ensureActivityObjectProfile($author, $hints);
1191 * Look up and, if necessary, create an Ostatus_profile for the remote
1192 * profile with the given RSS feed - actually loaded from the feed.
1193 * This should never return null -- you will either get an object or
1194 * an exception will be thrown.
1196 * @param DOMElement $feedEl root element of a loaded RSS feed
1197 * @param array $hints additional discovery information passed from higher levels
1198 * @todo FIXME: Should this be marked public?
1199 * @return Ostatus_profile
1202 public static function ensureRssChannel(DOMElement $feedEl, array $hints)
1204 // Special-case for Posterous. They have some nice metadata in their
1205 // posterous:author elements. We should use them instead of the channel.
1207 $items = $feedEl->getElementsByTagName('item');
1209 if ($items->length > 0) {
1210 $item = $items->item(0);
1211 $authorEl = ActivityUtils::child($item, ActivityObject::AUTHOR, ActivityObject::POSTEROUS);
1212 if (!empty($authorEl)) {
1213 $obj = ActivityObject::fromPosterousAuthor($authorEl);
1214 // Posterous has multiple authors per feed, and multiple feeds
1215 // per author. We check if this is the "main" feed for this author.
1216 if (array_key_exists('profileurl', $hints) &&
1217 !empty($obj->poco) &&
1218 common_url_to_nickname($hints['profileurl']) == $obj->poco->preferredUsername) {
1219 return self::ensureActivityObjectProfile($obj, $hints);
1224 // @todo FIXME: We should check whether this feed has elements
1225 // with different <author> or <dc:creator> elements, and... I dunno.
1226 // Do something about that.
1228 $obj = ActivityObject::fromRssChannel($feedEl);
1230 return self::ensureActivityObjectProfile($obj, $hints);
1234 * Download and update given avatar image
1236 * @param string $url
1237 * @return Avatar The Avatar we have on disk. (seldom used)
1238 * @throws Exception in various failure cases
1240 public function updateAvatar($url, $force)
1243 // If avatar URL differs: update. If URLs were identical but we're forced: update.
1244 if ($url == $this->avatar && !$force) {
1245 // If there's no locally stored avatar, throw an exception and continue fetching below.
1246 $avatar = Avatar::getUploaded($this->localProfile()) instanceof Avatar;
1249 } catch (NoAvatarException $e) {
1250 // No avatar available, let's fetch it.
1253 if (!common_valid_http_url($url)) {
1254 // TRANS: Server exception. %s is a URL.
1255 throw new ServerException(sprintf(_m('Invalid avatar URL %s.'), $url));
1258 $self = $this->localProfile();
1260 // @todo FIXME: This should be better encapsulated
1261 // ripped from oauthstore.php (for old OMB client)
1262 $temp_filename = tempnam(sys_get_temp_dir(), 'listener_avatar');
1264 $imgData = HTTPClient::quickGet($url);
1265 // Make sure it's at least an image file. ImageFile can do the rest.
1266 if (false === getimagesizefromstring($imgData)) {
1267 throw new UnsupportedMediaException(_('Downloaded group avatar was not an image.'));
1269 file_put_contents($temp_filename, $imgData);
1270 unset($imgData); // No need to carry this in memory.
1272 if ($this->isGroup()) {
1273 $id = $this->group_id;
1275 $id = $this->profile_id;
1277 // @todo FIXME: Should we be using different ids?
1278 $imagefile = new ImageFile($id, $temp_filename);
1279 $filename = Avatar::filename($id,
1280 image_type_to_extension($imagefile->type),
1282 common_timestamp());
1283 rename($temp_filename, Avatar::path($filename));
1284 } catch (Exception $e) {
1285 unlink($temp_filename);
1288 // @todo FIXME: Hardcoded chmod is lame, but seems to be necessary to
1289 // keep from accidentally saving images from command-line (queues)
1290 // that can't be read from web server, which causes hard-to-notice
1291 // problems later on:
1293 // http://status.net/open-source/issues/2663
1294 chmod(Avatar::path($filename), 0644);
1296 $self->setOriginal($filename);
1298 $orig = clone($this);
1299 $this->avatar = $url;
1300 $this->update($orig);
1302 return Avatar::getUploaded($self);
1306 * Pull avatar URL from ActivityObject or profile hints
1308 * @param ActivityObject $object
1309 * @param array $hints
1310 * @return mixed URL string or false
1312 public static function getActivityObjectAvatar(ActivityObject $object, array $hints=array())
1314 if ($object->avatarLinks) {
1316 // Take the exact-size avatar, or the largest avatar, or the first avatar if all sizeless
1317 foreach ($object->avatarLinks as $avatar) {
1318 if ($avatar->width == AVATAR_PROFILE_SIZE && $avatar->height = AVATAR_PROFILE_SIZE) {
1323 if (!$best || $avatar->width > $best->width) {
1328 } else if (array_key_exists('avatar', $hints)) {
1329 return $hints['avatar'];
1335 * Get an appropriate avatar image source URL, if available.
1337 * @param ActivityObject $actor
1338 * @param DOMElement $feed
1341 protected static function getAvatar(ActivityObject $actor, DOMElement $feed)
1345 if ($actor->avatar) {
1346 $url = trim($actor->avatar);
1349 // Check <atom:logo> and <atom:icon> on the feed
1350 $els = $feed->childNodes();
1351 if ($els && $els->length) {
1352 for ($i = 0; $i < $els->length; $i++) {
1353 $el = $els->item($i);
1354 if ($el->namespaceURI == Activity::ATOM) {
1355 if (empty($url) && $el->localName == 'logo') {
1356 $url = trim($el->textContent);
1359 if (empty($icon) && $el->localName == 'icon') {
1360 // Use as a fallback
1361 $icon = trim($el->textContent);
1366 if ($icon && !$url) {
1371 $opts = array('allowed_schemes' => array('http', 'https'));
1372 if (common_valid_http_url($url)) {
1377 return Plugin::staticPath('OStatus', 'images/96px-Feed-icon.svg.png');
1381 * Fetch, or build if necessary, an Ostatus_profile for the actor
1382 * in a given Activity Streams activity.
1383 * This should never return null -- you will either get an object or
1384 * an exception will be thrown.
1386 * @param Activity $activity
1387 * @param string $feeduri if we already know the canonical feed URI!
1388 * @param string $salmonuri if we already know the salmon return channel URI
1389 * @return Ostatus_profile
1392 public static function ensureActorProfile(Activity $activity, array $hints=array())
1394 return self::ensureActivityObjectProfile($activity->actor, $hints);
1398 * Fetch, or build if necessary, an Ostatus_profile for the profile
1399 * in a given Activity Streams object (can be subject, actor, or object).
1400 * This should never return null -- you will either get an object or
1401 * an exception will be thrown.
1403 * @param ActivityObject $object
1404 * @param array $hints additional discovery information passed from higher levels
1405 * @return Ostatus_profile
1408 public static function ensureActivityObjectProfile(ActivityObject $object, array $hints=array())
1410 $profile = self::getActivityObjectProfile($object);
1411 if ($profile instanceof Ostatus_profile) {
1412 $profile->updateFromActivityObject($object, $hints);
1414 $profile = self::createActivityObjectProfile($object, $hints);
1420 * @param Activity $activity
1421 * @return mixed matching Ostatus_profile or false if none known
1422 * @throws ServerException if feed info invalid
1424 public static function getActorProfile(Activity $activity)
1426 return self::getActivityObjectProfile($activity->actor);
1430 * @param ActivityObject $activity
1431 * @return mixed matching Ostatus_profile or false if none known
1432 * @throws ServerException if feed info invalid
1434 protected static function getActivityObjectProfile(ActivityObject $object)
1436 $uri = self::getActivityObjectProfileURI($object);
1437 return Ostatus_profile::getKV('uri', $uri);
1441 * Get the identifier URI for the remote entity described
1442 * by this ActivityObject. This URI is *not* guaranteed to be
1443 * a resolvable HTTP/HTTPS URL.
1445 * @param ActivityObject $object
1447 * @throws ServerException if feed info invalid
1449 protected static function getActivityObjectProfileURI(ActivityObject $object)
1452 if (ActivityUtils::validateUri($object->id)) {
1457 // If the id is missing or invalid (we've seen feeds mistakenly listing
1458 // things like local usernames in that field) then we'll use the profile
1459 // page link, if valid.
1460 if ($object->link && common_valid_http_url($object->link)) {
1461 return $object->link;
1463 // TRANS: Server exception.
1464 throw new ServerException(_m('No author ID URI found.'));
1468 * @todo FIXME: Validate stuff somewhere.
1472 * Create local ostatus_profile and profile/user_group entries for
1473 * the provided remote user or group.
1474 * This should never return null -- you will either get an object or
1475 * an exception will be thrown.
1477 * @param ActivityObject $object
1478 * @param array $hints
1480 * @return Ostatus_profile
1482 protected static function createActivityObjectProfile(ActivityObject $object, array $hints=array())
1484 $homeuri = $object->id;
1488 common_log(LOG_DEBUG, __METHOD__ . " empty actor profile URI: " . var_export($activity, true));
1489 // TRANS: Exception.
1490 throw new Exception(_m('No profile URI.'));
1493 $user = User::getKV('uri', $homeuri);
1494 if ($user instanceof User) {
1495 // TRANS: Exception.
1496 throw new Exception(_m('Local user cannot be referenced as remote.'));
1499 if (OStatusPlugin::localGroupFromUrl($homeuri)) {
1500 // TRANS: Exception.
1501 throw new Exception(_m('Local group cannot be referenced as remote.'));
1504 $ptag = Profile_list::getKV('uri', $homeuri);
1505 if ($ptag instanceof Profile_list) {
1506 $local_user = User::getKV('id', $ptag->tagger);
1507 if ($local_user instanceof User) {
1508 // TRANS: Exception.
1509 throw new Exception(_m('Local list cannot be referenced as remote.'));
1513 if (array_key_exists('feedurl', $hints)) {
1514 $feeduri = $hints['feedurl'];
1516 $discover = new FeedDiscovery();
1517 $feeduri = $discover->discoverFromURL($homeuri);
1520 if (array_key_exists('salmon', $hints)) {
1521 $salmonuri = $hints['salmon'];
1524 $discover = new FeedDiscovery();
1525 $discover->discoverFromFeedURL($hints['feedurl']);
1527 // XXX: NS_REPLIES is deprecated anyway, so let's remove it in the future.
1528 $salmonuri = $discover->getAtomLink(Salmon::REL_SALMON)
1529 ?: $discover->getAtomLink(Salmon::NS_REPLIES);
1532 if (array_key_exists('hub', $hints)) {
1533 $huburi = $hints['hub'];
1536 $discover = new FeedDiscovery();
1537 $discover->discoverFromFeedURL($hints['feedurl']);
1539 $huburi = $discover->getHubLink();
1542 if (!$huburi && !common_config('feedsub', 'fallback_hub') && !common_config('feedsub', 'nohub')) {
1543 // We can only deal with folks with a PuSH hub
1544 throw new FeedSubNoHubException();
1547 $oprofile = new Ostatus_profile();
1549 $oprofile->uri = $homeuri;
1550 $oprofile->feeduri = $feeduri;
1551 $oprofile->salmonuri = $salmonuri;
1553 $oprofile->created = common_sql_now();
1554 $oprofile->modified = common_sql_now();
1556 if ($object->type == ActivityObject::PERSON) {
1557 $profile = new Profile();
1558 $profile->created = common_sql_now();
1559 self::updateProfile($profile, $object, $hints);
1561 $oprofile->profile_id = $profile->insert();
1562 if ($oprofile->profile_id === false) {
1563 // TRANS: Server exception.
1564 throw new ServerException(_m('Cannot save local profile.'));
1566 } else if ($object->type == ActivityObject::GROUP) {
1567 $profile = new Profile();
1568 $profile->query('BEGIN');
1570 $group = new User_group();
1571 $group->uri = $homeuri;
1572 $group->created = common_sql_now();
1573 self::updateGroup($group, $object, $hints);
1575 // TODO: We should do this directly in User_group->insert()!
1576 // currently it's duplicated in User_group->update()
1577 // AND User_group->register()!!!
1578 $fields = array(/*group field => profile field*/
1579 'nickname' => 'nickname',
1580 'fullname' => 'fullname',
1581 'mainpage' => 'profileurl',
1582 'homepage' => 'homepage',
1583 'description' => 'bio',
1584 'location' => 'location',
1585 'created' => 'created',
1586 'modified' => 'modified',
1588 foreach ($fields as $gf=>$pf) {
1589 $profile->$pf = $group->$gf;
1591 $profile_id = $profile->insert();
1592 if ($profile_id === false) {
1593 $profile->query('ROLLBACK');
1594 throw new ServerException(_('Profile insertion failed.'));
1597 $group->profile_id = $profile_id;
1599 $oprofile->group_id = $group->insert();
1600 if ($oprofile->group_id === false) {
1601 $profile->query('ROLLBACK');
1602 // TRANS: Server exception.
1603 throw new ServerException(_m('Cannot save local profile.'));
1606 $profile->query('COMMIT');
1607 } else if ($object->type == ActivityObject::_LIST) {
1608 $ptag = new Profile_list();
1609 $ptag->uri = $homeuri;
1610 $ptag->created = common_sql_now();
1611 self::updatePeopletag($ptag, $object, $hints);
1613 $oprofile->peopletag_id = $ptag->insert();
1614 if ($oprofile->peopletag_id === false) {
1615 // TRANS: Server exception.
1616 throw new ServerException(_m('Cannot save local list.'));
1620 $ok = $oprofile->insert();
1622 if ($ok === false) {
1623 // TRANS: Server exception.
1624 throw new ServerException(_m('Cannot save OStatus profile.'));
1627 $avatar = self::getActivityObjectAvatar($object, $hints);
1631 $oprofile->updateAvatar($avatar);
1632 } catch (Exception $ex) {
1633 // Profile is saved, but Avatar is messed up. We're
1634 // just going to continue.
1635 common_log(LOG_WARNING, "Exception saving OStatus profile avatar: ". $ex->getMessage());
1643 * Save any updated profile information to our local copy.
1644 * @param ActivityObject $object
1645 * @param array $hints
1647 public function updateFromActivityObject(ActivityObject $object, array $hints=array())
1649 if ($this->isGroup()) {
1650 $group = $this->localGroup();
1651 self::updateGroup($group, $object, $hints);
1652 } else if ($this->isPeopletag()) {
1653 $ptag = $this->localPeopletag();
1654 self::updatePeopletag($ptag, $object, $hints);
1656 $profile = $this->localProfile();
1657 self::updateProfile($profile, $object, $hints);
1660 $avatar = self::getActivityObjectAvatar($object, $hints);
1661 if ($avatar && !isset($ptag)) {
1663 $this->updateAvatar($avatar);
1664 } catch (Exception $ex) {
1665 common_log(LOG_WARNING, "Exception saving OStatus profile avatar: " . $ex->getMessage());
1670 public static function updateProfile(Profile $profile, ActivityObject $object, array $hints=array())
1672 $orig = clone($profile);
1674 // Existing nickname is better than nothing.
1676 if (!array_key_exists('nickname', $hints)) {
1677 $hints['nickname'] = $profile->nickname;
1680 $nickname = self::getActivityObjectNickname($object, $hints);
1682 if (!empty($nickname)) {
1683 $profile->nickname = $nickname;
1686 if (!empty($object->title)) {
1687 $profile->fullname = $object->title;
1688 } else if (array_key_exists('fullname', $hints)) {
1689 $profile->fullname = $hints['fullname'];
1692 if (!empty($object->link)) {
1693 $profile->profileurl = $object->link;
1694 } else if (array_key_exists('profileurl', $hints)) {
1695 $profile->profileurl = $hints['profileurl'];
1696 } else if (common_valid_http_url($object->id)) {
1697 $profile->profileurl = $object->id;
1700 $bio = self::getActivityObjectBio($object, $hints);
1703 $profile->bio = $bio;
1706 $location = self::getActivityObjectLocation($object, $hints);
1708 if (!empty($location)) {
1709 $profile->location = $location;
1712 $homepage = self::getActivityObjectHomepage($object, $hints);
1714 if (!empty($homepage)) {
1715 $profile->homepage = $homepage;
1718 if (!empty($object->geopoint)) {
1719 $location = ActivityContext::locationFromPoint($object->geopoint);
1720 if (!empty($location)) {
1721 $profile->lat = $location->lat;
1722 $profile->lon = $location->lon;
1726 // @todo FIXME: tags/categories
1727 // @todo tags from categories
1730 common_log(LOG_DEBUG, "Updating OStatus profile $profile->id from remote info $object->id: " . var_export($object, true) . var_export($hints, true));
1731 $profile->update($orig);
1735 protected static function updateGroup(User_group $group, ActivityObject $object, array $hints=array())
1737 $orig = clone($group);
1739 $group->nickname = self::getActivityObjectNickname($object, $hints);
1740 $group->fullname = $object->title;
1742 if (!empty($object->link)) {
1743 $group->mainpage = $object->link;
1744 } else if (array_key_exists('profileurl', $hints)) {
1745 $group->mainpage = $hints['profileurl'];
1748 // @todo tags from categories
1749 $group->description = self::getActivityObjectBio($object, $hints);
1750 $group->location = self::getActivityObjectLocation($object, $hints);
1751 $group->homepage = self::getActivityObjectHomepage($object, $hints);
1753 if ($group->id) { // If no id, we haven't called insert() yet, so don't run update()
1754 common_log(LOG_DEBUG, "Updating OStatus group $group->id from remote info $object->id: " . var_export($object, true) . var_export($hints, true));
1755 $group->update($orig);
1759 protected static function updatePeopletag($tag, ActivityObject $object, array $hints=array()) {
1760 $orig = clone($tag);
1762 $tag->tag = $object->title;
1764 if (!empty($object->link)) {
1765 $tag->mainpage = $object->link;
1766 } else if (array_key_exists('profileurl', $hints)) {
1767 $tag->mainpage = $hints['profileurl'];
1770 $tag->description = $object->summary;
1771 $tagger = self::ensureActivityObjectProfile($object->owner);
1772 $tag->tagger = $tagger->profile_id;
1775 common_log(LOG_DEBUG, "Updating OStatus peopletag $tag->id from remote info $object->id: " . var_export($object, true) . var_export($hints, true));
1776 $tag->update($orig);
1780 protected static function getActivityObjectHomepage(ActivityObject $object, array $hints=array())
1783 $poco = $object->poco;
1785 if (!empty($poco)) {
1786 $url = $poco->getPrimaryURL();
1787 if ($url && $url->type == 'homepage') {
1788 $homepage = $url->value;
1792 // @todo Try for a another PoCo URL?
1797 protected static function getActivityObjectLocation(ActivityObject $object, array $hints=array())
1801 if (!empty($object->poco) &&
1802 isset($object->poco->address->formatted)) {
1803 $location = $object->poco->address->formatted;
1804 } else if (array_key_exists('location', $hints)) {
1805 $location = $hints['location'];
1808 if (!empty($location)) {
1809 if (mb_strlen($location) > 255) {
1810 $location = mb_substr($note, 0, 255 - 3) . ' … ';
1814 // @todo Try to find location some othe way? Via goerss point?
1819 protected static function getActivityObjectBio(ActivityObject $object, array $hints=array())
1823 if (!empty($object->poco)) {
1824 $note = $object->poco->note;
1825 } else if (array_key_exists('bio', $hints)) {
1826 $note = $hints['bio'];
1829 if (!empty($note)) {
1830 if (Profile::bioTooLong($note)) {
1831 // XXX: truncate ok?
1832 $bio = mb_substr($note, 0, Profile::maxBio() - 3) . ' … ';
1838 // @todo Try to get bio info some other way?
1843 public static function getActivityObjectNickname(ActivityObject $object, array $hints=array())
1845 if ($object->poco) {
1846 if (!empty($object->poco->preferredUsername)) {
1847 return common_nicknamize($object->poco->preferredUsername);
1851 if (!empty($object->nickname)) {
1852 return common_nicknamize($object->nickname);
1855 if (array_key_exists('nickname', $hints)) {
1856 return $hints['nickname'];
1859 // Try the profile url (like foo.example.com or example.com/user/foo)
1860 if (!empty($object->link)) {
1861 $profileUrl = $object->link;
1862 } else if (!empty($hints['profileurl'])) {
1863 $profileUrl = $hints['profileurl'];
1866 if (!empty($profileUrl)) {
1867 $nickname = self::nicknameFromURI($profileUrl);
1870 // Try the URI (may be a tag:, http:, acct:, ...
1872 if (empty($nickname)) {
1873 $nickname = self::nicknameFromURI($object->id);
1876 // Try a Webfinger if one was passed (way) down
1878 if (empty($nickname)) {
1879 if (array_key_exists('webfinger', $hints)) {
1880 $nickname = self::nicknameFromURI($hints['webfinger']);
1886 if (empty($nickname)) {
1887 $nickname = common_nicknamize($object->title);
1893 protected static function nicknameFromURI($uri)
1895 if (preg_match('/(\w+):/', $uri, $matches)) {
1896 $protocol = $matches[1];
1901 switch ($protocol) {
1904 if (preg_match("/^$protocol:(.*)?@.*\$/", $uri, $matches)) {
1905 return common_canonical_nickname($matches[1]);
1909 return common_url_to_nickname($uri);
1917 * Look up, and if necessary create, an Ostatus_profile for the remote
1918 * entity with the given webfinger address.
1919 * This should never return null -- you will either get an object or
1920 * an exception will be thrown.
1922 * @param string $addr webfinger address
1923 * @return Ostatus_profile
1924 * @throws Exception on error conditions
1925 * @throws OStatusShadowException if this reference would obscure a local user/group
1927 public static function ensureWebfinger($addr)
1929 // First, try the cache
1931 $uri = self::cacheGet(sprintf('ostatus_profile:webfinger:%s', $addr));
1933 if ($uri !== false) {
1934 if (is_null($uri)) {
1935 // Negative cache entry
1936 // TRANS: Exception.
1937 throw new Exception(_m('Not a valid webfinger address.'));
1939 $oprofile = Ostatus_profile::getKV('uri', $uri);
1940 if ($oprofile instanceof Ostatus_profile) {
1945 // Try looking it up
1946 $oprofile = Ostatus_profile::getKV('uri', Discovery::normalize($addr));
1948 if ($oprofile instanceof Ostatus_profile) {
1949 self::cacheSet(sprintf('ostatus_profile:webfinger:%s', $addr), $oprofile->getUri());
1953 // Now, try some discovery
1955 $disco = new Discovery();
1958 $xrd = $disco->lookup($addr);
1959 } catch (Exception $e) {
1960 // Save negative cache entry so we don't waste time looking it up again.
1961 // @todo FIXME: Distinguish temporary failures?
1962 self::cacheSet(sprintf('ostatus_profile:webfinger:%s', $addr), null);
1963 // TRANS: Exception.
1964 throw new Exception(_m('Not a valid webfinger address.'));
1967 $hints = array('webfinger' => $addr);
1969 $dhints = DiscoveryHints::fromXRD($xrd);
1971 $hints = array_merge($hints, $dhints);
1973 // If there's an Hcard, let's grab its info
1974 if (array_key_exists('hcard', $hints)) {
1975 if (!array_key_exists('profileurl', $hints) ||
1976 $hints['hcard'] != $hints['profileurl']) {
1977 $hcardHints = DiscoveryHints::fromHcardUrl($hints['hcard']);
1978 $hints = array_merge($hcardHints, $hints);
1982 // If we got a feed URL, try that
1984 if (array_key_exists('feedurl', $hints)) {
1985 $feedUrl = $hints['feedurl'];
1987 common_log(LOG_INFO, "Discovery on acct:$addr with feed URL " . $hints['feedurl']);
1988 $oprofile = self::ensureFeedURL($hints['feedurl'], $hints);
1989 self::cacheSet(sprintf('ostatus_profile:webfinger:%s', $addr), $oprofile->getUri());
1991 } catch (Exception $e) {
1992 common_log(LOG_WARNING, "Failed creating profile from feed URL '$feedUrl': " . $e->getMessage());
1997 // If we got a profile page, try that!
1999 if (array_key_exists('profileurl', $hints)) {
2000 $profileUrl = $hints['profileurl'];
2002 common_log(LOG_INFO, "Discovery on acct:$addr with profile URL $profileUrl");
2003 $oprofile = self::ensureProfileURL($hints['profileurl'], $hints);
2004 self::cacheSet(sprintf('ostatus_profile:webfinger:%s', $addr), $oprofile->getUri());
2006 } catch (OStatusShadowException $e) {
2007 // We've ended up with a remote reference to a local user or group.
2008 // @todo FIXME: Ideally we should be able to say who it was so we can
2009 // go back and refer to it the regular way
2011 } catch (Exception $e) {
2012 common_log(LOG_WARNING, "Failed creating profile from profile URL '$profileUrl': " . $e->getMessage());
2015 // @todo FIXME: This means an error discovering from profile page
2016 // may give us a corrupt entry using the webfinger URI, which
2017 // will obscure the correct page-keyed profile later on.
2024 if (array_key_exists('salmon', $hints)) {
2025 $salmonEndpoint = $hints['salmon'];
2027 // An account URL, a salmon endpoint, and a dream? Not much to go
2028 // on, but let's give it a try
2030 $uri = 'acct:'.$addr;
2032 $profile = new Profile();
2034 $profile->nickname = self::nicknameFromUri($uri);
2035 $profile->created = common_sql_now();
2037 if (!is_null($profileUrl)) {
2038 $profile->profileurl = $profileUrl;
2041 $profile_id = $profile->insert();
2043 if ($profile_id === false) {
2044 common_log_db_error($profile, 'INSERT', __FILE__);
2045 // TRANS: Exception. %s is a webfinger address.
2046 throw new Exception(sprintf(_m('Could not save profile for "%s".'),$addr));
2049 $oprofile = new Ostatus_profile();
2051 $oprofile->uri = $uri;
2052 $oprofile->salmonuri = $salmonEndpoint;
2053 $oprofile->profile_id = $profile_id;
2054 $oprofile->created = common_sql_now();
2056 if (!is_null($feedUrl)) {
2057 $oprofile->feeduri = $feedUrl;
2060 $result = $oprofile->insert();
2062 if ($result === false) {
2064 common_log_db_error($oprofile, 'INSERT', __FILE__);
2065 // TRANS: Exception. %s is a webfinger address.
2066 throw new Exception(sprintf(_m('Could not save OStatus profile for "%s".'),$addr));
2069 self::cacheSet(sprintf('ostatus_profile:webfinger:%s', $addr), $oprofile->getUri());
2073 // TRANS: Exception. %s is a webfinger address.
2074 throw new Exception(sprintf(_m('Could not find a valid profile for "%s".'),$addr));
2078 * Store the full-length scrubbed HTML of a remote notice to an attachment
2079 * file on our server. We'll link to this at the end of the cropped version.
2081 * @param string $title plaintext for HTML page's title
2082 * @param string $rendered HTML fragment for HTML page's body
2085 function saveHTMLFile($title, $rendered)
2087 $final = sprintf("<!DOCTYPE html>\n" .
2089 '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">' .
2090 '<title>%s</title>' .
2092 '<body>%s</body></html>',
2093 htmlspecialchars($title),
2096 $filename = File::filename($this->localProfile(),
2097 'ostatus', // ignored?
2100 $filepath = File::path($filename);
2102 file_put_contents($filepath, $final);
2106 $file->filename = $filename;
2107 $file->url = File::url($filename);
2108 $file->size = filesize($filepath);
2109 $file->date = time();
2110 $file->mimetype = 'text/html';
2112 $file_id = $file->insert();
2114 if ($file_id === false) {
2115 common_log_db_error($file, "INSERT", __FILE__);
2116 // TRANS: Server exception.
2117 throw new ServerException(_m('Could not store HTML content of long post as file.'));
2123 static function ensureProfileURI($uri)
2127 // First, try to query it
2129 $oprofile = Ostatus_profile::getKV('uri', $uri);
2131 if ($oprofile instanceof Ostatus_profile) {
2135 // If unfound, do discovery stuff
2136 if (preg_match("/^(\w+)\:(.*)/", $uri, $match)) {
2137 $protocol = $match[1];
2138 switch ($protocol) {
2141 $oprofile = self::ensureProfileURL($uri);
2146 $oprofile = self::ensureWebfinger($rest);
2149 // TRANS: Server exception.
2150 // TRANS: %1$s is a protocol, %2$s is a URI.
2151 throw new ServerException(sprintf(_m('Unrecognized URI protocol for profile: %1$s (%2$s).'),
2157 // TRANS: Server exception. %s is a URI.
2158 throw new ServerException(sprintf(_m('No URI protocol for profile: %s.'),$uri));
2164 public function checkAuthorship(Activity $activity)
2166 if ($this->isGroup() || $this->isPeopletag()) {
2167 // A group or propletag feed will contain posts from multiple authors.
2168 $oprofile = self::ensureActorProfile($activity);
2169 if ($oprofile->isGroup() || $oprofile->isPeopletag()) {
2170 // Groups can't post notices in StatusNet.
2171 common_log(LOG_WARNING,
2172 "OStatus: skipping post with group listed ".
2173 "as author: " . $oprofile->getUri() . " in feed from " . $this->getUri());
2174 throw new ServerException('Activity author is a non-actor');
2177 $actor = $activity->actor;
2179 if (empty($actor)) {
2180 // OK here! assume the default
2181 } else if ($actor->id == $this->getUri() || $actor->link == $this->getUri()) {
2182 $this->updateFromActivityObject($actor);
2183 } else if ($actor->id) {
2184 // We have an ActivityStreams actor with an explicit ID that doesn't match the feed owner.
2185 // This isn't what we expect from mainline OStatus person feeds!
2186 // Group feeds go down another path, with different validation...
2187 // Most likely this is a plain ol' blog feed of some kind which
2188 // doesn't match our expectations. We'll take the entry, but ignore
2189 // the <author> info.
2190 common_log(LOG_WARNING, "Got an actor '{$actor->title}' ({$actor->id}) on single-user feed for " . $this->getUri());
2192 // Plain <author> without ActivityStreams actor info.
2193 // We'll just ignore this info for now and save the update under the feed's identity.
2199 return $oprofile->localProfile();
2204 * Exception indicating we've got a remote reference to a local user,
2205 * not a remote user!
2207 * If we can ue a local profile after all, it's available as $e->profile.
2209 class OStatusShadowException extends Exception
2214 * @param Profile $profile
2215 * @param string $message
2217 function __construct($profile, $message) {
2218 $this->profile = $profile;
2219 parent::__construct($message);