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 $object = new ActivityObject();
154 $object->type = 'http://activitystrea.ms/schema/1.0/group';
155 $object->id = $this->uri;
156 $self = $this->localGroup();
158 // @fixme put a standard getAvatar() interface on groups too
159 if ($self->homepage_logo) {
160 $object->avatar = $self->homepage_logo;
161 $map = array('png' => 'image/png',
162 'jpg' => 'image/jpeg',
163 'jpeg' => 'image/jpeg',
164 'gif' => 'image/gif');
165 $extension = pathinfo(parse_url($object->avatar, PHP_URL_PATH), PATHINFO_EXTENSION);
166 if (isset($map[$extension])) {
167 // @fixme this ain't used/saved yet
168 $object->avatarType = $map[$extension];
172 $object->link = $this->uri; // @fixme accurate?
175 return ActivityObject::fromProfile($this->localProfile());
180 * Returns an XML string fragment with profile information as an
181 * Activity Streams noun object with the given element type.
183 * Assumes that 'activity' namespace has been previously defined.
185 * @fixme replace with wrappers on asActivityObject when it's got everything.
187 * @param string $element one of 'actor', 'subject', 'object', 'target'
190 function asActivityNoun($element)
192 $xs = new XMLStringer(true);
193 $avatarHref = Avatar::defaultImage(AVATAR_PROFILE_SIZE);
194 $avatarType = 'image/png';
195 if ($this->isGroup()) {
196 $type = 'http://activitystrea.ms/schema/1.0/group';
197 $self = $this->localGroup();
199 // @fixme put a standard getAvatar() interface on groups too
200 if ($self->homepage_logo) {
201 $avatarHref = $self->homepage_logo;
202 $map = array('png' => 'image/png',
203 'jpg' => 'image/jpeg',
204 'jpeg' => 'image/jpeg',
205 'gif' => 'image/gif');
206 $extension = pathinfo(parse_url($avatarHref, PHP_URL_PATH), PATHINFO_EXTENSION);
207 if (isset($map[$extension])) {
208 $avatarType = $map[$extension];
212 $type = 'http://activitystrea.ms/schema/1.0/person';
213 $self = $this->localProfile();
214 $avatar = $self->getAvatar(AVATAR_PROFILE_SIZE);
216 $avatarHref = $avatar->url;
217 $avatarType = $avatar->mediatype;
220 $xs->elementStart('activity:' . $element);
222 'activity:object-type',
230 $xs->element('title', null, $self->getBestName());
234 'type' => $avatarType,
235 'href' => $avatarHref
240 $xs->elementEnd('activity:' . $element);
242 return $xs->getString();
246 * @return boolean true if this is a remote group
250 if ($this->profile_id && !$this->group_id) {
252 } else if ($this->group_id && !$this->profile_id) {
254 } else if ($this->group_id && $this->profile_id) {
255 throw new ServerException("Invalid ostatus_profile state: both group and profile IDs set for $this->uri");
257 throw new ServerException("Invalid ostatus_profile state: both group and profile IDs empty for $this->uri");
262 * Subscribe a local user to this remote user.
263 * PuSH subscription will be started if necessary, and we'll
264 * send a Salmon notification to the remote server if available
265 * notifying them of the sub.
268 * @return boolean success
269 * @throws FeedException
271 public function subscribeLocalToRemote(User $user)
273 if ($this->isGroup()) {
274 throw new ServerException("Can't subscribe to a remote group");
277 if ($this->subscribe()) {
278 if ($user->subscribeTo($this->localProfile())) {
279 $this->notify($user->getProfile(), ActivityVerb::FOLLOW, $this);
287 * Mark this remote profile as subscribing to the given local user,
288 * and send appropriate notifications to the user.
290 * This will generally be in response to a subscription notification
291 * from a foreign site to our local Salmon response channel.
294 * @return boolean success
296 public function subscribeRemoteToLocal(User $user)
298 if ($this->isGroup()) {
299 throw new ServerException("Remote groups can't subscribe to local users");
302 Subscription::start($this->localProfile(), $user->getProfile());
308 * Send a subscription request to the hub for this feed.
309 * The hub will later send us a confirmation POST to /main/push/callback.
311 * @return bool true on success, false on failure
312 * @throws ServerException if feed state is not valid
314 public function subscribe()
316 $feedsub = FeedSub::ensureFeed($this->feeduri);
317 if ($feedsub->sub_state == 'active' || $feedsub->sub_state == 'subscribe') {
319 } else if ($feedsub->sub_state == '' || $feedsub->sub_state == 'inactive') {
320 return $feedsub->subscribe();
321 } else if ('unsubscribe') {
322 throw new FeedSubException("Unsub is pending, can't subscribe...");
327 * Send a PuSH unsubscription request to the hub for this feed.
328 * The hub will later send us a confirmation POST to /main/push/callback.
330 * @return bool true on success, false on failure
331 * @throws ServerException if feed state is not valid
333 public function unsubscribe() {
334 $feedsub = FeedSub::staticGet('uri', $this->feeduri);
338 if ($feedsub->sub_state == 'active') {
339 return $feedsub->unsubscribe();
340 } else if ($feedsub->sub_state == '' || $feedsub->sub_state == 'inactive' || $feedsub->sub_state == 'unsubscribe') {
342 } else if ($feedsub->sub_state == 'subscribe') {
343 throw new FeedSubException("Feed is awaiting subscription, can't unsub...");
348 * Check if this remote profile has any active local subscriptions, and
349 * if not drop the PuSH subscription feed.
353 public function garbageCollect()
355 if ($this->isGroup()) {
356 $members = $this->localGroup()->getMembers(0, 1);
357 $count = $members->N;
359 $count = $this->localProfile()->subscriberCount();
362 common_log(LOG_INFO, "Unsubscribing from now-unused remote feed $this->feeduri");
363 $this->unsubscribe();
371 * Send an Activity Streams notification to the remote Salmon endpoint,
374 * @param Profile $actor Actor who did the activity
375 * @param string $verb Activity::SUBSCRIBE or Activity::JOIN
376 * @param Object $object object of the action; must define asActivityNoun($tag)
378 public function notify($actor, $verb, $object=null)
380 if (!($actor instanceof Profile)) {
381 $type = gettype($actor);
382 if ($type == 'object') {
383 $type = get_class($actor);
385 throw new ServerException("Invalid actor passed to " . __METHOD__ . ": " . $type);
387 if ($object == null) {
390 if ($this->salmonuri) {
393 $id = TagURI::mint('%s:%s:%s',
396 common_date_iso8601(time()));
398 // @fixme consolidate all these NS settings somewhere
399 $attributes = array('xmlns' => Activity::ATOM,
400 'xmlns:activity' => 'http://activitystrea.ms/spec/1.0/',
401 'xmlns:thr' => 'http://purl.org/syndication/thread/1.0',
402 'xmlns:georss' => 'http://www.georss.org/georss',
403 'xmlns:ostatus' => 'http://ostatus.org/schema/1.0',
404 'xmlns:poco' => 'http://portablecontacts.net/spec/1.0');
406 $entry = new XMLStringer();
407 $entry->elementStart('entry', $attributes);
408 $entry->element('id', null, $id);
409 $entry->element('title', null, $text);
410 $entry->element('summary', null, $text);
411 $entry->element('published', null, common_date_w3dtf(common_sql_now()));
413 $entry->element('activity:verb', null, $verb);
414 $entry->raw($actor->asAtomAuthor());
415 $entry->raw($actor->asActivityActor());
416 $entry->raw($object->asActivityNoun('object'));
417 $entry->elementEnd('entry');
419 $xml = $entry->getString();
420 common_log(LOG_INFO, "Posting to Salmon endpoint $this->salmonuri: $xml");
422 $salmon = new Salmon(); // ?
423 return $salmon->post($this->salmonuri, $xml);
429 * Send a Salmon notification ping immediately, and confirm that we got
430 * an acceptable response from the remote site.
432 * @param mixed $entry XML string, Notice, or Activity
433 * @return boolean success
435 public function notifyActivity($entry)
437 if ($this->salmonuri) {
438 $salmon = new Salmon();
439 return $salmon->post($this->salmonuri, $this->notifyPrepXml($entry));
446 * Queue a Salmon notification for later. If queues are disabled we'll
447 * send immediately but won't get the return value.
449 * @param mixed $entry XML string, Notice, or Activity
450 * @return boolean success
452 public function notifyDeferred($entry)
454 if ($this->salmonuri) {
455 $data = array('salmonuri' => $this->salmonuri,
456 'entry' => $this->notifyPrepXml($entry));
458 $qm = QueueManager::get();
459 return $qm->enqueue($data, 'salmon');
465 protected function notifyPrepXml($entry)
467 $preamble = '<?xml version="1.0" encoding="UTF-8" ?' . '>';
468 if (is_string($entry)) {
470 } else if ($entry instanceof Activity) {
471 return $preamble . $entry->asString(true);
472 } else if ($entry instanceof Notice) {
473 return $preamble . $entry->asAtomEntry(true, true);
475 throw new ServerException("Invalid type passed to Ostatus_profile::notify; must be XML string or Activity entry");
479 function getBestName()
481 if ($this->isGroup()) {
482 return $this->localGroup()->getBestName();
484 return $this->localProfile()->getBestName();
488 function atomFeed($actor)
490 $feed = new Atom10Feed();
491 // @fixme should these be set up somewhere else?
492 $feed->addNamespace('activity', 'http://activitystrea.ms/spec/1.0/');
493 $feed->addNamespace('thr', 'http://purl.org/syndication/thread/1.0');
494 $feed->addNamespace('georss', 'http://www.georss.org/georss');
495 $feed->addNamespace('ostatus', 'http://ostatus.org/schema/1.0');
497 $taguribase = common_config('integration', 'taguri');
498 $feed->setId("tag:{$taguribase}:UserTimeline:{$actor->id}"); // ???
500 $feed->setTitle($actor->getBestName() . ' timeline'); // @fixme
501 $feed->setUpdated(time());
502 $feed->setPublished(time());
504 $feed->addLink(common_local_url('ApiTimelineUser',
505 array('id' => $actor->id,
507 array('rel' => 'self',
508 'type' => 'application/atom+xml'));
510 $feed->addLink(common_local_url('userbyid',
511 array('id' => $actor->id)),
512 array('rel' => 'alternate',
513 'type' => 'text/html'));
519 * Read and post notices for updates from the feed.
520 * Currently assumes that all items in the feed are new,
521 * coming from a PuSH hub.
523 * @param DOMDocument $feed
525 public function processFeed($feed, $source)
527 $entries = $feed->getElementsByTagNameNS(Activity::ATOM, 'entry');
528 if ($entries->length == 0) {
529 common_log(LOG_ERR, __METHOD__ . ": no entries in feed update, ignoring");
533 for ($i = 0; $i < $entries->length; $i++) {
534 $entry = $entries->item($i);
535 $this->processEntry($entry, $feed, $source);
540 * Process a posted entry from this feed source.
542 * @param DOMElement $entry
543 * @param DOMElement $feed for context
545 public function processEntry($entry, $feed, $source)
547 $activity = new Activity($entry, $feed);
549 if ($activity->verb == ActivityVerb::POST) {
550 $this->processPost($activity, $source);
552 common_log(LOG_INFO, "Ignoring activity with unrecognized verb $activity->verb");
557 * Process an incoming post activity from this remote feed.
558 * @param Activity $activity
559 * @param string $method 'push' or 'salmon'
560 * @return mixed saved Notice or false
561 * @fixme break up this function, it's getting nasty long
563 public function processPost($activity, $method)
565 if ($this->isGroup()) {
566 // A group feed will contain posts from multiple authors.
567 // @fixme validate these profiles in some way!
568 $oprofile = self::ensureActorProfile($activity);
569 if ($oprofile->isGroup()) {
570 // Groups can't post notices in StatusNet.
571 common_log(LOG_WARNING, "OStatus: skipping post with group listed as author: $oprofile->uri in feed from $this->uri");
575 // Individual user feeds may contain only posts from themselves.
576 // Authorship is validated against the profile URI on upper layers,
577 // through PuSH setup or Salmon signature checks.
578 $actorUri = self::getActorProfileURI($activity);
579 if ($actorUri == $this->uri) {
580 // Check if profile info has changed and update it
581 $this->updateFromActivityObject($activity->actor);
583 common_log(LOG_WARNING, "OStatus: skipping post with bad author: got $actorUri expected $this->uri");
589 // The id URI will be used as a unique identifier for for the notice,
590 // protecting against duplicate saves. It isn't required to be a URL;
591 // tag: URIs for instance are found in Google Buzz feeds.
592 $sourceUri = $activity->object->id;
593 $dupe = Notice::staticGet('uri', $sourceUri);
595 common_log(LOG_INFO, "OStatus: ignoring duplicate post: $sourceUri");
599 // We'll also want to save a web link to the original notice, if provided.
601 if ($activity->object->link) {
602 $sourceUrl = $activity->object->link;
603 } else if ($activity->link) {
604 $sourceUrl = $activity->link;
605 } else if (preg_match('!^https?://!', $activity->object->id)) {
606 $sourceUrl = $activity->object->id;
609 // Get (safe!) HTML and text versions of the content
610 $rendered = $this->purify($activity->object->content);
611 $content = html_entity_decode(strip_tags($rendered));
613 $shortened = common_shorten_links($content);
615 // If it's too long, try using the summary, and make the
616 // HTML an attachment.
620 if (Notice::contentTooLong($shortened)) {
621 $attachment = $this->saveHTMLFile($activity->object->title, $rendered);
622 $summary = $activity->object->summary;
623 if (empty($summary)) {
626 $shortSummary = common_shorten_links($summary);
627 if (Notice::contentTooLong($shortSummary)) {
628 $url = common_shorten_url(common_local_url('attachment',
629 array('attachment' => $attachment->id)));
630 $shortSummary = substr($shortSummary,
632 Notice::maxContent() - (mb_strlen($url) + 2));
633 $shortSummary .= '… ' . $url;
634 $content = $shortSummary;
635 $rendered = common_render_text($content);
639 $options = array('is_local' => Notice::REMOTE_OMB,
642 'rendered' => $rendered,
643 'replies' => array(),
648 // Check for optional attributes...
650 if (!empty($activity->time)) {
651 $options['created'] = common_sql_date($activity->time);
654 if ($activity->context) {
655 // Any individual or group attn: targets?
656 $replies = $activity->context->attention;
657 $options['groups'] = $this->filterReplies($oprofile, $replies);
658 $options['replies'] = $replies;
660 // Maintain direct reply associations
661 // @fixme what about conversation ID?
662 if (!empty($activity->context->replyToID)) {
663 $orig = Notice::staticGet('uri',
664 $activity->context->replyToID);
666 $options['reply_to'] = $orig->id;
670 $location = $activity->context->location;
672 $options['lat'] = $location->lat;
673 $options['lon'] = $location->lon;
674 if ($location->location_id) {
675 $options['location_ns'] = $location->location_ns;
676 $options['location_id'] = $location->location_id;
681 // Atom categories <-> hashtags
682 foreach ($activity->categories as $cat) {
684 $term = common_canonical_tag($cat->term);
686 $options['tags'][] = $term;
692 $saved = Notice::saveNew($oprofile->profile_id,
697 Ostatus_source::saveNew($saved, $this, $method);
698 if (!empty($attachment)) {
699 File_to_post::processNew($attachment->id, $saved->id);
702 } catch (Exception $e) {
703 common_log(LOG_ERR, "OStatus save of remote message $sourceUri failed: " . $e->getMessage());
706 common_log(LOG_INFO, "OStatus saved remote message $sourceUri as notice id $saved->id");
713 protected function purify($html)
715 require_once INSTALLDIR.'/extlib/htmLawed/htmLawed.php';
716 $config = array('safe' => 1);
717 return htmLawed($html, $config);
721 * Filters a list of recipient ID URIs to just those for local delivery.
722 * @param Ostatus_profile local profile of sender
723 * @param array in/out &$attention_uris set of URIs, will be pruned on output
724 * @return array of group IDs
726 protected function filterReplies($sender, &$attention_uris)
728 common_log(LOG_DEBUG, "Original reply recipients: " . implode(', ', $attention_uris));
731 foreach ($attention_uris as $recipient) {
732 // Is the recipient a local user?
733 $user = User::staticGet('uri', $recipient);
735 // @fixme sender verification, spam etc?
736 $replies[] = $recipient;
740 // Is the recipient a remote group?
741 $oprofile = Ostatus_profile::staticGet('uri', $recipient);
743 if ($oprofile->isGroup()) {
744 // Deliver to local members of this remote group.
745 // @fixme sender verification?
746 $groups[] = $oprofile->group_id;
748 common_log(LOG_DEBUG, "Skipping reply to remote profile $recipient");
753 // Is the recipient a local group?
754 // @fixme we need a uri on user_group
755 // $group = User_group::staticGet('uri', $recipient);
756 $template = common_local_url('groupbyid', array('id' => '31337'));
757 $template = preg_quote($template, '/');
758 $template = str_replace('31337', '(\d+)', $template);
759 if (preg_match("/$template/", $recipient, $matches)) {
761 $group = User_group::staticGet('id', $id);
763 // Deliver to all members of this local group if allowed.
764 $profile = $sender->localProfile();
765 if ($profile->isMember($group)) {
766 $groups[] = $group->id;
768 common_log(LOG_DEBUG, "Skipping reply to local group $group->nickname as sender $profile->id is not a member");
772 common_log(LOG_DEBUG, "Skipping reply to bogus group $recipient");
776 common_log(LOG_DEBUG, "Skipping reply to unrecognized profile $recipient");
779 $attention_uris = $replies;
780 common_log(LOG_DEBUG, "Local reply recipients: " . implode(', ', $replies));
781 common_log(LOG_DEBUG, "Local group recipients: " . implode(', ', $groups));
786 * @param string $profile_url
787 * @return Ostatus_profile
788 * @throws FeedSubException
790 public static function ensureProfile($profile_uri, $hints=array())
792 // Get the canonical feed URI and check it
793 $discover = new FeedDiscovery();
794 $feeduri = $discover->discoverFromURL($profile_uri);
796 //$feedsub = FeedSub::ensureFeed($feeduri, $discover->feed);
797 $huburi = $discover->getAtomLink('hub');
798 $salmonuri = $discover->getAtomLink('salmon');
801 // We can only deal with folks with a PuSH hub
802 throw new FeedSubNoHubException();
805 // Try to get a profile from the feed activity:subject
807 $feedEl = $discover->feed->documentElement;
809 $subject = ActivityUtils::child($feedEl, Activity::SUBJECT, Activity::SPEC);
811 if (!empty($subject)) {
812 $subjObject = new ActivityObject($subject);
813 return self::ensureActivityObjectProfile($subjObject, $feeduri, $salmonuri, $hints);
816 // Otherwise, try the feed author
818 $author = ActivityUtils::child($feedEl, Activity::AUTHOR, Activity::ATOM);
820 if (!empty($author)) {
821 $authorObject = new ActivityObject($author);
822 return self::ensureActivityObjectProfile($authorObject, $feeduri, $salmonuri, $hints);
825 // Sheesh. Not a very nice feed! Let's try fingerpoken in the
828 $entries = $discover->feed->getElementsByTagNameNS(Activity::ATOM, 'entry');
830 if (!empty($entries) && $entries->length > 0) {
832 $entry = $entries->item(0);
834 $actor = ActivityUtils::child($entry, Activity::ACTOR, Activity::SPEC);
836 if (!empty($actor)) {
837 $actorObject = new ActivityObject($actor);
838 return self::ensureActivityObjectProfile($actorObject, $feeduri, $salmonuri, $hints);
842 $author = ActivityUtils::child($entry, Activity::AUTHOR, Activity::ATOM);
844 if (!empty($author)) {
845 $authorObject = new ActivityObject($author);
846 return self::ensureActivityObjectProfile($authorObject, $feeduri, $salmonuri, $hints);
850 // XXX: make some educated guesses here
852 throw new FeedSubException("Can't find enough profile information to make a feed.");
857 * Download and update given avatar image
859 * @throws Exception in various failure cases
861 protected function updateAvatar($url)
863 if ($url == $this->avatar) {
864 // We've already got this one.
868 if ($this->isGroup()) {
869 $self = $this->localGroup();
871 $self = $this->localProfile();
874 throw new ServerException(sprintf(
875 _m("Tried to update avatar for unsaved remote profile %s"),
879 // @fixme this should be better encapsulated
880 // ripped from oauthstore.php (for old OMB client)
881 $temp_filename = tempnam(sys_get_temp_dir(), 'listener_avatar');
882 if (!copy($url, $temp_filename)) {
883 throw new ServerException(sprintf(_m("Unable to fetch avatar from %s"), $url));
886 if ($this->isGroup()) {
887 $id = $this->group_id;
889 $id = $this->profile_id;
891 // @fixme should we be using different ids?
892 $imagefile = new ImageFile($id, $temp_filename);
893 $filename = Avatar::filename($id,
894 image_type_to_extension($imagefile->type),
897 rename($temp_filename, Avatar::path($filename));
898 $self->setOriginal($filename);
900 $orig = clone($this);
901 $this->avatar = $url;
902 $this->update($orig);
906 * Pull avatar URL from ActivityObject or profile hints
908 * @param ActivityObject $object
909 * @param array $hints
910 * @return mixed URL string or false
913 protected static function getActivityObjectAvatar($object, $hints=array())
915 if ($object->avatar) {
916 return $object->avatar;
917 } else if (array_key_exists('avatar', $hints)) {
918 return $hints['avatar'];
924 * Get an appropriate avatar image source URL, if available.
926 * @param ActivityObject $actor
927 * @param DOMElement $feed
931 protected static function getAvatar($actor, $feed)
935 if ($actor->avatar) {
936 $url = trim($actor->avatar);
939 // Check <atom:logo> and <atom:icon> on the feed
940 $els = $feed->childNodes();
941 if ($els && $els->length) {
942 for ($i = 0; $i < $els->length; $i++) {
943 $el = $els->item($i);
944 if ($el->namespaceURI == Activity::ATOM) {
945 if (empty($url) && $el->localName == 'logo') {
946 $url = trim($el->textContent);
949 if (empty($icon) && $el->localName == 'icon') {
951 $icon = trim($el->textContent);
956 if ($icon && !$url) {
961 $opts = array('allowed_schemes' => array('http', 'https'));
962 if (Validate::uri($url, $opts)) {
966 return common_path('plugins/OStatus/images/96px-Feed-icon.svg.png');
970 * Fetch, or build if necessary, an Ostatus_profile for the actor
971 * in a given Activity Streams activity.
973 * @param Activity $activity
974 * @param string $feeduri if we already know the canonical feed URI!
975 * @param string $salmonuri if we already know the salmon return channel URI
976 * @return Ostatus_profile
979 public static function ensureActorProfile($activity, $feeduri=null, $salmonuri=null)
981 return self::ensureActivityObjectProfile($activity->actor, $feeduri, $salmonuri);
984 public static function ensureActivityObjectProfile($object, $feeduri=null, $salmonuri=null, $hints=array())
986 $profile = self::getActivityObjectProfile($object);
988 $profile->updateFromActivityObject($object, $hints);
990 $profile = self::createActivityObjectProfile($object, $feeduri, $salmonuri, $hints);
996 * @param Activity $activity
997 * @return mixed matching Ostatus_profile or false if none known
999 public static function getActorProfile($activity)
1001 return self::getActivityObjectProfile($activity->actor);
1004 protected static function getActivityObjectProfile($object)
1006 $uri = self::getActivityObjectProfileURI($object);
1007 return Ostatus_profile::staticGet('uri', $uri);
1010 protected static function getActorProfileURI($activity)
1012 return self::getActivityObjectProfileURI($activity->actor);
1016 * @param Activity $activity
1018 * @throws ServerException
1020 protected static function getActivityObjectProfileURI($object)
1022 $opts = array('allowed_schemes' => array('http', 'https'));
1023 if ($object->id && Validate::uri($object->id, $opts)) {
1026 if ($object->link && Validate::uri($object->link, $opts)) {
1027 return $object->link;
1029 throw new ServerException("No author ID URI found");
1033 * @fixme validate stuff somewhere
1036 protected static function createActorProfile($activity, $feeduri=null, $salmonuri=null)
1038 $actor = $activity->actor;
1040 self::createActivityObjectProfile($actor, $feeduri, $salmonuri);
1044 * Create local ostatus_profile and profile/user_group entries for
1045 * the provided remote user or group.
1047 * @param ActivityObject $object
1048 * @param string $feeduri
1049 * @param string $salmonuri
1050 * @param array $hints
1052 * @fixme fold $feeduri/$salmonuri into $hints
1053 * @return Ostatus_profile
1055 protected static function createActivityObjectProfile($object, $feeduri=null, $salmonuri=null, $hints=array())
1057 $homeuri = $object->id;
1060 common_log(LOG_DEBUG, __METHOD__ . " empty actor profile URI: " . var_export($activity, true));
1061 throw new ServerException("No profile URI");
1064 if (empty($feeduri)) {
1065 if (array_key_exists('feedurl', $hints)) {
1066 $feeduri = $hints['feedurl'];
1070 if (empty($salmonuri)) {
1071 if (array_key_exists('salmon', $hints)) {
1072 $salmonuri = $hints['salmon'];
1076 if (!$feeduri || !$salmonuri) {
1077 // Get the canonical feed URI and check it
1078 $discover = new FeedDiscovery();
1079 $feeduri = $discover->discoverFromURL($homeuri);
1081 $huburi = $discover->getAtomLink('hub');
1082 $salmonuri = $discover->getAtomLink('salmon');
1085 // We can only deal with folks with a PuSH hub
1086 throw new FeedSubNoHubException();
1090 $oprofile = new Ostatus_profile();
1092 $oprofile->uri = $homeuri;
1093 $oprofile->feeduri = $feeduri;
1094 $oprofile->salmonuri = $salmonuri;
1096 $oprofile->created = common_sql_now();
1097 $oprofile->modified = common_sql_now();
1099 if ($object->type == ActivityObject::PERSON) {
1100 $profile = new Profile();
1101 $profile->created = common_sql_now();
1102 self::updateProfile($profile, $object, $hints);
1104 $oprofile->profile_id = $profile->insert();
1105 if (!$oprofile->profile_id) {
1106 throw new ServerException("Can't save local profile");
1109 $group = new User_group();
1110 $group->uri = $homeuri;
1111 $group->created = common_sql_now();
1112 self::updateGroup($group, $object, $hints);
1114 $oprofile->group_id = $group->insert();
1115 if (!$oprofile->group_id) {
1116 throw new ServerException("Can't save local profile");
1120 $ok = $oprofile->insert();
1123 $avatar = self::getActivityObjectAvatar($object, $hints);
1125 $oprofile->updateAvatar($avatar);
1129 throw new ServerException("Can't save OStatus profile");
1134 * Save any updated profile information to our local copy.
1135 * @param ActivityObject $object
1136 * @param array $hints
1138 public function updateFromActivityObject($object, $hints=array())
1140 if ($this->isGroup()) {
1141 $group = $this->localGroup();
1142 self::updateGroup($group, $object, $hints);
1144 $profile = $this->localProfile();
1145 self::updateProfile($profile, $object, $hints);
1147 $avatar = self::getActivityObjectAvatar($object, $hints);
1149 $this->updateAvatar($avatar);
1153 protected static function updateProfile($profile, $object, $hints=array())
1155 $orig = clone($profile);
1157 $profile->nickname = self::getActivityObjectNickname($object, $hints);
1158 $profile->fullname = $object->title;
1159 if (!empty($object->link)) {
1160 $profile->profileurl = $object->link;
1161 } else if (array_key_exists('profileurl', $hints)) {
1162 $profile->profileurl = $hints['profileurl'];
1165 $profile->bio = self::getActivityObjectBio($object, $hints);
1166 $profile->location = self::getActivityObjectLocation($object, $hints);
1167 $profile->homepage = self::getActivityObjectHomepage($object, $hints);
1169 if (!empty($object->geopoint)) {
1170 $location = ActivityContext::locationFromPoint($object->geopoint);
1171 if (!empty($location)) {
1172 $profile->lat = $location->lat;
1173 $profile->lon = $location->lon;
1177 // @fixme tags/categories
1178 // @todo tags from categories
1181 common_log(LOG_DEBUG, "Updating OStatus profile $profile->id from remote info $object->id: " . var_export($object, true) . var_export($hints, true));
1182 $profile->update($orig);
1186 protected static function updateGroup($group, $object, $hints=array())
1188 $orig = clone($group);
1190 $group->nickname = self::getActivityObjectNickname($object, $hints);
1191 $group->fullname = $object->title;
1193 if (!empty($object->link)) {
1194 $group->mainpage = $object->link;
1195 } else if (array_key_exists('profileurl', $hints)) {
1196 $group->mainpage = $hints['profileurl'];
1199 // @todo tags from categories
1200 $group->description = self::getActivityObjectBio($object, $hints);
1201 $group->location = self::getActivityObjectLocation($object, $hints);
1202 $group->homepage = self::getActivityObjectHomepage($object, $hints);
1205 common_log(LOG_DEBUG, "Updating OStatus group $group->id from remote info $object->id: " . var_export($object, true) . var_export($hints, true));
1206 $group->update($orig);
1210 protected static function getActivityObjectHomepage($object, $hints=array())
1213 $poco = $object->poco;
1215 if (!empty($poco)) {
1216 $url = $poco->getPrimaryURL();
1217 if ($url->type == 'homepage') {
1218 $homepage = $url->value;
1222 // @todo Try for a another PoCo URL?
1227 protected static function getActivityObjectLocation($object, $hints=array())
1231 if (!empty($object->poco)) {
1232 if (isset($object->poco->address->formatted)) {
1233 $location = $object->poco->address->formatted;
1234 if (mb_strlen($location) > 255) {
1235 $location = mb_substr($note, 0, 255 - 3) . ' … ';
1240 // @todo Try to find location some othe way? Via goerss point?
1245 protected static function getActivityObjectBio($object, $hints=array())
1249 if (!empty($object->poco)) {
1250 $note = $object->poco->note;
1251 if (!empty($note)) {
1252 if (mb_strlen($note) > Profile::maxBio()) {
1253 // XXX: truncate ok?
1254 $bio = mb_substr($note, 0, Profile::maxBio() - 3) . ' … ';
1261 // @todo Try to get bio info some other way?
1266 protected static function getActivityObjectNickname($object, $hints=array())
1268 if ($object->poco) {
1269 if (!empty($object->poco->preferredUsername)) {
1270 return common_nicknamize($object->poco->preferredUsername);
1273 if (!empty($object->nickname)) {
1274 return common_nicknamize($object->nickname);
1277 // Try the definitive ID
1279 $nickname = self::nicknameFromURI($object->id);
1281 // Try a Webfinger if one was passed (way) down
1283 if (empty($nickname)) {
1284 if (array_key_exists('webfinger', $hints)) {
1285 $nickname = self::nicknameFromURI($hints['webfinger']);
1291 if (empty($nickname)) {
1292 $nickname = common_nicknamize($object->title);
1298 protected static function nicknameFromURI($uri)
1300 preg_match('/(\w+):/', $uri, $matches);
1302 $protocol = $matches[1];
1304 switch ($protocol) {
1307 if (preg_match("/^$protocol:(.*)?@.*\$/", $uri, $matches)) {
1308 return common_canonical_nickname($matches[1]);
1312 return common_url_to_nickname($uri);
1319 public static function ensureWebfinger($addr)
1321 // First, look it up
1323 $oprofile = Ostatus_profile::staticGet('uri', 'acct:'.$addr);
1325 if (!empty($oprofile)) {
1329 // Now, try some discovery
1331 $wf = new Webfinger();
1333 $result = $wf->lookup($addr);
1339 foreach ($result->links as $link) {
1340 switch ($link['rel']) {
1341 case Webfinger::PROFILEPAGE:
1342 $profileUrl = $link['href'];
1345 $salmonEndpoint = $link['href'];
1347 case Webfinger::UPDATESFROM:
1348 $feedUrl = $link['href'];
1351 common_log(LOG_NOTICE, "Don't know what to do with rel = '{$link['rel']}'");
1356 $hints = array('webfinger' => $addr,
1357 'profileurl' => $profileUrl,
1358 'feedurl' => $feedUrl,
1359 'salmon' => $salmonEndpoint);
1361 // If we got a feed URL, try that
1363 if (isset($feedUrl)) {
1365 $oprofile = self::ensureProfile($feedUrl, $hints);
1367 } catch (Exception $e) {
1368 common_log(LOG_WARNING, "Failed creating profile from feed URL '$feedUrl': " . $e->getMessage());
1373 // If we got a profile page, try that!
1375 if (isset($profileUrl)) {
1377 $oprofile = self::ensureProfile($profileUrl, $hints);
1379 } catch (Exception $e) {
1380 common_log(LOG_WARNING, "Failed creating profile from profile URL '$profileUrl': " . $e->getMessage());
1388 if (isset($salmonEndpoint)) {
1390 // An account URL, a salmon endpoint, and a dream? Not much to go
1391 // on, but let's give it a try
1393 $uri = 'acct:'.$addr;
1395 $profile = new Profile();
1397 $profile->nickname = self::nicknameFromUri($uri);
1398 $profile->created = common_sql_now();
1400 if (isset($profileUrl)) {
1401 $profile->profileurl = $profileUrl;
1404 $profile_id = $profile->insert();
1407 common_log_db_error($profile, 'INSERT', __FILE__);
1408 throw new Exception("Couldn't save profile for '$addr'");
1411 $oprofile = new Ostatus_profile();
1413 $oprofile->uri = $uri;
1414 $oprofile->salmonuri = $salmonEndpoint;
1415 $oprofile->profile_id = $profile_id;
1416 $oprofile->created = common_sql_now();
1418 if (isset($feedUrl)) {
1419 $profile->feeduri = $feedUrl;
1422 $result = $oprofile->insert();
1425 common_log_db_error($oprofile, 'INSERT', __FILE__);
1426 throw new Exception("Couldn't save ostatus_profile for '$addr'");
1435 function saveHTMLFile($title, $rendered)
1437 $final = sprintf("<!DOCTYPE html>\n<html><head><title>%s</title></head>".
1438 '<body><div>%s</div></body></html>',
1439 htmlspecialchars($title),
1442 $filename = File::filename($this->localProfile(),
1443 'ostatus', // ignored?
1446 $filepath = File::path($filename);
1448 file_put_contents($filepath, $final);
1452 $file->filename = $filename;
1453 $file->url = File::url($filename);
1454 $file->size = filesize($filepath);
1455 $file->date = time();
1456 $file->mimetype = 'text/html';
1458 $file_id = $file->insert();
1460 if ($file_id === false) {
1461 common_log_db_error($file, "INSERT", __FILE__);
1462 throw new ServerException(_('Could not store HTML content of long post as file.'));