]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OStatus/classes/Ostatus_profile.php
Merge remote-tracking branch 'upstream/master'
[quix0rs-gnu-social.git] / plugins / OStatus / classes / Ostatus_profile.php
1 <?php
2 /*
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2009-2010, StatusNet, Inc.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 if (!defined('STATUSNET')) {
21     exit(1);
22 }
23
24 /**
25  * @package OStatusPlugin
26  * @maintainer Brion Vibber <brion@status.net>
27  */
28 class Ostatus_profile extends Managed_DataObject
29 {
30     public $__table = 'ostatus_profile';
31
32     public $uri;
33
34     public $profile_id;
35     public $group_id;
36     public $peopletag_id;
37
38     public $feeduri;
39     public $salmonuri;
40     public $avatar; // remote URL of the last avatar we saved
41
42     public $created;
43     public $modified;
44
45     /**
46      * Return table definition for Schema setup and DB_DataObject usage.
47      *
48      * @return array array of column definitions
49      */
50     static function schemaDef()
51     {
52         return array(
53             'fields' => array(
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),
63             ),
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'),
70             ),
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')),
75             ),
76         );
77     }
78
79     public function getUri()
80     {
81         return $this->uri;
82     }
83
84     public function fromProfile(Profile $profile)
85     {
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);
89         }
90     }
91
92     /**
93      * Fetch the locally stored profile for this feed
94      * @return Profile
95      * @throws NoProfileException if it was not found
96      */
97     public function localProfile()
98     {
99         if ($this->isGroup()) {
100             return $this->localGroup()->getProfile();
101         }
102
103         $profile = Profile::getKV('id', $this->profile_id);
104         if (!$profile instanceof Profile) {
105             throw new NoProfileException($this->profile_id);
106         }
107         return $profile;
108     }
109
110     /**
111      * Fetch the StatusNet-side profile for this feed
112      * @return Profile
113      */
114     public function localGroup()
115     {
116         $group = User_group::getKV('id', $this->group_id);
117
118         if (!$group instanceof User_group) {
119             throw new NoSuchGroupException(array('id'=>$this->group_id));
120         }
121
122         return $group;
123     }
124
125     /**
126      * Fetch the StatusNet-side peopletag for this feed
127      * @return Profile
128      */
129     public function localPeopletag()
130     {
131         if ($this->peopletag_id) {
132             return Profile_list::getKV('id', $this->peopletag_id);
133         }
134         return null;
135     }
136
137     /**
138      * Returns an ActivityObject describing this remote user or group profile.
139      * Can then be used to generate Atom chunks.
140      *
141      * @return ActivityObject
142      */
143     function asActivityObject()
144     {
145         if ($this->isGroup()) {
146             return ActivityObject::fromGroup($this->localGroup());
147         } else if ($this->isPeopletag()) {
148             return ActivityObject::fromPeopletag($this->localPeopletag());
149         } else {
150             return $this->localProfile()->asActivityObject();
151         }
152     }
153
154     /**
155      * Returns an XML string fragment with profile information as an
156      * Activity Streams noun object with the given element type.
157      *
158      * Assumes that 'activity' namespace has been previously defined.
159      *
160      * @todo FIXME: Replace with wrappers on asActivityObject when it's got everything.
161      *
162      * @param string $element one of 'actor', 'subject', 'object', 'target'
163      * @return string
164      */
165     function asActivityNoun($element)
166     {
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);
173         } else {
174             $noun = $this->localProfile()->asActivityObject();
175             return $noun->asString('activity:' . $element);
176         }
177     }
178
179     /**
180      * @return boolean true if this is a remote group
181      */
182     function isGroup()
183     {
184         if ($this->profile_id || $this->peopletag_id && !$this->group_id) {
185             return false;
186         } else if ($this->group_id && !$this->profile_id && !$this->peopletag_id) {
187             return true;
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()));
191         } else {
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()));
194         }
195     }
196
197     /**
198      * @return boolean true if this is a remote peopletag
199      */
200     function isPeopletag()
201     {
202         if ($this->profile_id || $this->group_id && !$this->peopletag_id) {
203             return false;
204         } else if ($this->peopletag_id && !$this->profile_id && !$this->group_id) {
205             return true;
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()));
209         } else {
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()));
212         }
213     }
214
215     /**
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.
218      *
219      * @return void
220      * @throws ServerException if feed state is not valid or subscription fails.
221      */
222     public function subscribe()
223     {
224         $feedsub = FeedSub::ensureFeed($this->feeduri);
225         if ($feedsub->sub_state == 'active') {
226             // Active subscription, we don't need to do anything.
227             return;
228         }
229
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();
233     }
234
235     /**
236      * Check if this remote profile has any active local subscriptions, and
237      * if not drop the PuSH subscription feed.
238      *
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.
241      */
242     public function unsubscribe() {
243         return $this->garbageCollect();
244     }
245
246     /**
247      * Check if this remote profile has any active local subscriptions, and
248      * if not drop the PuSH subscription feed.
249      *
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.
252      */
253     public function garbageCollect()
254     {
255         $feedsub = FeedSub::getKV('uri', $this->feeduri);
256         if ($feedsub instanceof FeedSub) {
257             return $feedsub->garbageCollect();
258         }
259         // Since there's no FeedSub we can assume it's already garbage collected
260         return true;
261     }
262
263     /**
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.
266      *
267      * This gets called via the FeedSubSubscriberCount event when running
268      * FeedSub::garbageCollect().
269      *
270      * @return int
271      * @throws NoProfileException if there is no local profile for the object
272      */
273     public function subscriberCount()
274     {
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;
281         } else {
282             $profile = $this->localProfile();
283             if ($profile->hasLocalTags()) {
284                 $count = 1;
285             } else {
286                 $count = $profile->subscriberCount();
287             }
288         }
289         common_log(LOG_INFO, __METHOD__ . " SUB COUNT BEFORE: $count");
290
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");
295
296         return $count;
297     }
298
299     /**
300      * Send an Activity Streams notification to the remote Salmon endpoint,
301      * if so configured.
302      *
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)
306      */
307     public function notify(Profile $actor, $verb, $object=null, $target=null)
308     {
309         if ($object == null) {
310             $object = $this;
311         }
312         if (empty($this->salmonuri)) {
313             return false;
314         }
315         $text = 'update';
316         $id = TagURI::mint('%s:%s:%s',
317                            $verb,
318                            $actor->getURI(),
319                            common_date_iso8601(time()));
320
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');
329
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()));
336
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'));
343         }
344         $entry->elementEnd('entry');
345
346         $xml = $entry->getString();
347         common_log(LOG_INFO, "Posting to Salmon endpoint $this->salmonuri: $xml");
348
349         Salmon::post($this->salmonuri, $xml, $actor->getUser());
350     }
351
352     /**
353      * Send a Salmon notification ping immediately, and confirm that we got
354      * an acceptable response from the remote site.
355      *
356      * @param mixed $entry XML string, Notice, or Activity
357      * @param Profile $actor
358      * @return boolean success
359      */
360     public function notifyActivity($entry, Profile $actor)
361     {
362         if ($this->salmonuri) {
363             return Salmon::post($this->salmonuri, $this->notifyPrepXml($entry), $actor->getUser());
364         }
365         common_debug(__CLASS__.' error: No salmonuri for Ostatus_profile uri: '.$this->uri);
366
367         return false;
368     }
369
370     /**
371      * Queue a Salmon notification for later. If queues are disabled we'll
372      * send immediately but won't get the return value.
373      *
374      * @param mixed $entry XML string, Notice, or Activity
375      * @param Profile $actor Acting profile
376      * @return boolean success
377      */
378     public function notifyDeferred($entry, Profile $actor)
379     {
380         if ($this->salmonuri) {
381             $data = array('salmonuri' => $this->salmonuri,
382                           'entry' => $this->notifyPrepXml($entry),
383                           'actor' => $actor->id);
384
385             $qm = QueueManager::get();
386             return $qm->enqueue($data, 'salmon');
387         }
388
389         return false;
390     }
391
392     protected function notifyPrepXml($entry)
393     {
394         $preamble = '<?xml version="1.0" encoding="UTF-8" ?' . '>';
395         if (is_string($entry)) {
396             return $entry;
397         } else if ($entry instanceof Activity) {
398             return $preamble . $entry->asString(true);
399         } else if ($entry instanceof Notice) {
400             return $preamble . $entry->asAtomEntry(true, true);
401         } else {
402             // TRANS: Server exception.
403             throw new ServerException(_m('Invalid type passed to Ostatus_profile::notify. It must be XML string or Activity entry.'));
404         }
405     }
406
407     function getBestName()
408     {
409         if ($this->isGroup()) {
410             return $this->localGroup()->getBestName();
411         } else if ($this->isPeopletag()) {
412             return $this->localPeopletag()->getBestName();
413         } else {
414             return $this->localProfile()->getBestName();
415         }
416     }
417
418     /**
419      * Read and post notices for updates from the feed.
420      * Currently assumes that all items in the feed are new,
421      * coming from a PuSH hub.
422      *
423      * @param DOMDocument $doc
424      * @param string $source identifier ("push")
425      */
426     public function processFeed(DOMDocument $doc, $source)
427     {
428         $feed = $doc->documentElement;
429
430         if ($feed->localName == 'feed' && $feed->namespaceURI == Activity::ATOM) {
431             $this->processAtomFeed($feed, $source);
432         } else if ($feed->localName == 'rss') { // @todo FIXME: Check namespace.
433             $this->processRssFeed($feed, $source);
434         } else {
435             // TRANS: Exception.
436             throw new Exception(_m('Unknown feed format.'));
437         }
438     }
439
440     public function processAtomFeed(DOMElement $feed, $source)
441     {
442         $entries = $feed->getElementsByTagNameNS(Activity::ATOM, 'entry');
443         if ($entries->length == 0) {
444             common_log(LOG_ERR, __METHOD__ . ": no entries in feed update, ignoring");
445             return;
446         }
447
448         for ($i = 0; $i < $entries->length; $i++) {
449             $entry = $entries->item($i);
450             $this->processEntry($entry, $feed, $source);
451         }
452     }
453
454     public function processRssFeed(DOMElement $rss, $source)
455     {
456         $channels = $rss->getElementsByTagName('channel');
457
458         if ($channels->length == 0) {
459             // TRANS: Exception.
460             throw new Exception(_m('RSS feed without a channel.'));
461         } else if ($channels->length > 1) {
462             common_log(LOG_WARNING, __METHOD__ . ": more than one channel in an RSS feed");
463         }
464
465         $channel = $channels->item(0);
466
467         $items = $channel->getElementsByTagName('item');
468
469         for ($i = 0; $i < $items->length; $i++) {
470             $item = $items->item($i);
471             $this->processEntry($item, $channel, $source);
472         }
473     }
474
475     /**
476      * Process a posted entry from this feed source.
477      *
478      * @param DOMElement $entry
479      * @param DOMElement $feed for context
480      * @param string $source identifier ("push" or "salmon")
481      *
482      * @return Notice Notice representing the new (or existing) activity
483      */
484     public function processEntry(DOMElement $entry, DOMElement $feed, $source)
485     {
486         $activity = new Activity($entry, $feed);
487         return $this->processActivity($activity, $source);
488     }
489
490     // TODO: Make this throw an exception
491     public function processActivity(Activity $activity, $source)
492     {
493         $notice = null;
494
495         // The "WithProfile" events were added later.
496
497         if (Event::handle('StartHandleFeedEntryWithProfile', array($activity, $this->localProfile(), &$notice)) &&
498             Event::handle('StartHandleFeedEntry', array($activity))) {
499
500             switch ($activity->verb) {
501             case ActivityVerb::POST:
502                 // @todo process all activity objects
503                 switch ($activity->objects[0]->type) {
504                 case ActivityObject::ARTICLE:
505                 case ActivityObject::BLOGENTRY:
506                 case ActivityObject::NOTE:
507                 case ActivityObject::STATUS:
508                 case ActivityObject::COMMENT:
509                 case null:
510                     $notice = $this->processPost($activity, $source);
511                     break;
512                 default:
513                     // TRANS: Client exception.
514                     throw new ClientException(_m('Cannot handle that kind of post.'));
515                 }
516                 break;
517             case ActivityVerb::SHARE:
518                 $notice = $this->processShare($activity, $source);
519                 break;
520             default:
521                 common_log(LOG_INFO, "Ignoring activity with unrecognized verb $activity->verb");
522             }
523
524             Event::handle('EndHandleFeedEntry', array($activity));
525             Event::handle('EndHandleFeedEntryWithProfile', array($activity, $this, $notice));
526         }
527
528         return $notice;
529     }
530
531     public function processShare(Activity $activity, $method)
532     {
533         $notice = null;
534
535         try {
536             $profile = ActivityUtils::checkAuthorship($activity, $this->localProfile());
537         } catch (ServerException $e) {
538             return null;
539         }
540
541         // The id URI will be used as a unique identifier for the notice,
542         // protecting against duplicate saves. It isn't required to be a URL;
543         // tag: URIs for instance are found in Google Buzz feeds.
544         $dupe = Notice::getKV('uri', $activity->id);
545         if ($dupe instanceof Notice) {
546             common_log(LOG_INFO, "OStatus: ignoring duplicate post: {$activity->id}");
547             return $dupe;
548         }
549
550         if (count($activity->objects) != 1) {
551             // TRANS: Client exception thrown when trying to share multiple activities at once.
552             throw new ClientException(_m('Can only handle share activities with exactly one object.'));
553         }
554
555         $shared = $activity->objects[0];
556
557         if (!$shared instanceof Activity) {
558             // TRANS: Client exception thrown when trying to share a non-activity object.
559             throw new ClientException(_m('Can only handle shared activities.'));
560         }
561
562         $sharedId = $shared->id;
563         if (!empty($shared->objects[0]->id)) {
564             // Because StatusNet since commit 8cc4660 sets $shared->id to a TagURI which
565             // fucks up federation, because the URI is no longer recognised by the origin.
566             // So we set it to the object ID if it exists, otherwise we trust $shared->id
567             $sharedId = $shared->objects[0]->id;
568         }
569         if (empty($sharedId)) {
570             throw new ClientException(_m('Shared activity does not have an id'));
571         }
572
573         // First check if we have the shared activity. This has to be done first, because
574         // we can't use these functions to "ensureActivityObjectProfile" of a local user,
575         // who might be the creator of the shared activity in question.
576         $sharedNotice = Notice::getKV('uri', $sharedId);
577         if (!$sharedNotice instanceof Notice) {
578             // If no locally stored notice is found, process it!
579             // TODO: Remember to check Deleted_notice!
580             // TODO: If a post is shared that we can't retrieve - what to do?
581             try {
582                 $other = self::ensureActivityObjectProfile($shared->actor);
583                 $sharedNotice = $other->processActivity($shared, $method);
584                 if (!$sharedNotice instanceof Notice) {
585                     // And if we apparently can't get the shared notice, we'll abort the whole thing.
586                     // TRANS: Client exception thrown when saving an activity share fails.
587                     // TRANS: %s is a share ID.
588                     throw new ClientException(sprintf(_m('Failed to save activity %s.'), $sharedId));
589                 }
590             } catch (FeedSubException $e) {
591                 // Remote feed could not be found or verified, should we
592                 // transform this into an "RT @user Blah, blah, blah..."?
593                 common_log(LOG_INFO, __METHOD__ . ' got a ' . get_class($e) . ': ' . $e->getMessage());
594                 return null;
595             }
596         }
597
598         // We'll want to save a web link to the original notice, if provided.
599
600         $sourceUrl = null;
601         if ($activity->link) {
602             $sourceUrl = $activity->link;
603         } else if ($activity->link) {
604             $sourceUrl = $activity->link;
605         } else if (preg_match('!^https?://!', $activity->id)) {
606             $sourceUrl = $activity->id;
607         }
608
609         // Use summary as fallback for content
610
611         if (!empty($activity->content)) {
612             $sourceContent = $activity->content;
613         } else if (!empty($activity->summary)) {
614             $sourceContent = $activity->summary;
615         } else if (!empty($activity->title)) {
616             $sourceContent = $activity->title;
617         } else {
618             // @todo FIXME: Fetch from $sourceUrl?
619             // TRANS: Client exception. %s is a source URI.
620             throw new ClientException(sprintf(_m('No content for notice %s.'), $activity->id));
621         }
622
623         // Get (safe!) HTML and text versions of the content
624
625         $rendered = $this->purify($sourceContent);
626         $content = common_strip_html($rendered);
627
628         $shortened = common_shorten_links($content);
629
630         // If it's too long, try using the summary, and make the
631         // HTML an attachment.
632
633         $attachment = null;
634
635         if (Notice::contentTooLong($shortened)) {
636             $attachment = $this->saveHTMLFile($activity->title, $rendered);
637             $summary = common_strip_html($activity->summary);
638             if (empty($summary)) {
639                 $summary = $content;
640             }
641             $shortSummary = common_shorten_links($summary);
642             if (Notice::contentTooLong($shortSummary)) {
643                 $url = common_shorten_url($sourceUrl);
644                 $shortSummary = substr($shortSummary,
645                                        0,
646                                        Notice::maxContent() - (mb_strlen($url) + 2));
647                 $content = $shortSummary . ' ' . $url;
648
649                 // We mark up the attachment link specially for the HTML output
650                 // so we can fold-out the full version inline.
651
652                 // @todo FIXME i18n: This tooltip will be saved with the site's default language
653                 // TRANS: Shown when a notice is longer than supported and/or when attachments are present. At runtime
654                 // TRANS: this will usually be replaced with localised text from StatusNet core messages.
655                 $showMoreText = _m('Show more');
656                 $attachUrl = common_local_url('attachment',
657                                               array('attachment' => $attachment->id));
658                 $rendered = common_render_text($shortSummary) .
659                             '<a href="' . htmlspecialchars($attachUrl) .'"'.
660                             ' class="attachment more"' .
661                             ' title="'. htmlspecialchars($showMoreText) . '">' .
662                             '&#8230;' .
663                             '</a>';
664             }
665         }
666
667         $options = array('is_local' => Notice::REMOTE,
668                          'url' => $sourceUrl,
669                          'uri' => $activity->id,
670                          'rendered' => $rendered,
671                          'replies' => array(),
672                          'groups' => array(),
673                          'peopletags' => array(),
674                          'tags' => array(),
675                          'urls' => array(),
676                          'repeat_of' => $sharedNotice->id,
677                          'scope' => $sharedNotice->scope);
678
679         // Check for optional attributes...
680
681         if (!empty($activity->time)) {
682             $options['created'] = common_sql_date($activity->time);
683         }
684
685         if ($activity->context) {
686             // TODO: context->attention
687             list($options['groups'], $options['replies'])
688                 = self::filterAttention($profile, $activity->context->attention);
689
690             // Maintain direct reply associations
691             // @todo FIXME: What about conversation ID?
692             if (!empty($activity->context->replyToID)) {
693                 $orig = Notice::getKV('uri',
694                                           $activity->context->replyToID);
695                 if ($orig instanceof Notice) {
696                     $options['reply_to'] = $orig->id;
697                 }
698             }
699
700             $location = $activity->context->location;
701             if ($location) {
702                 $options['lat'] = $location->lat;
703                 $options['lon'] = $location->lon;
704                 if ($location->location_id) {
705                     $options['location_ns'] = $location->location_ns;
706                     $options['location_id'] = $location->location_id;
707                 }
708             }
709         }
710
711         if ($this->isPeopletag()) {
712             $options['peopletags'][] = $this->localPeopletag();
713         }
714
715         // Atom categories <-> hashtags
716         foreach ($activity->categories as $cat) {
717             if ($cat->term) {
718                 $term = common_canonical_tag($cat->term);
719                 if ($term) {
720                     $options['tags'][] = $term;
721                 }
722             }
723         }
724
725         // Atom enclosures -> attachment URLs
726         foreach ($activity->enclosures as $href) {
727             // @todo FIXME: Save these locally or....?
728             $options['urls'][] = $href;
729         }
730
731         $notice = Notice::saveNew($profile->id,
732                                   $content,
733                                   'ostatus',
734                                   $options);
735
736         return $notice;
737     }
738
739     /**
740      * Process an incoming post activity from this remote feed.
741      * @param Activity $activity
742      * @param string $method 'push' or 'salmon'
743      * @return mixed saved Notice or false
744      * @todo FIXME: Break up this function, it's getting nasty long
745      */
746     public function processPost(Activity $activity, $method)
747     {
748         $notice = null;
749
750         $profile = ActivityUtils::checkAuthorship($activity, $this->localProfile());
751
752         // It's not always an ActivityObject::NOTE, but... let's just say it is.
753
754         $note = $activity->objects[0];
755
756         // The id URI will be used as a unique identifier for the notice,
757         // protecting against duplicate saves. It isn't required to be a URL;
758         // tag: URIs for instance are found in Google Buzz feeds.
759         $sourceUri = $note->id;
760         $dupe = Notice::getKV('uri', $sourceUri);
761         if ($dupe instanceof Notice) {
762             common_log(LOG_INFO, "OStatus: ignoring duplicate post: $sourceUri");
763             return $dupe;
764         }
765
766         // We'll also want to save a web link to the original notice, if provided.
767         $sourceUrl = null;
768         if ($note->link) {
769             $sourceUrl = $note->link;
770         } else if ($activity->link) {
771             $sourceUrl = $activity->link;
772         } else if (preg_match('!^https?://!', $note->id)) {
773             $sourceUrl = $note->id;
774         }
775
776         // Use summary as fallback for content
777
778         if (!empty($note->content)) {
779             $sourceContent = $note->content;
780         } else if (!empty($note->summary)) {
781             $sourceContent = $note->summary;
782         } else if (!empty($note->title)) {
783             $sourceContent = $note->title;
784         } else {
785             // @todo FIXME: Fetch from $sourceUrl?
786             // TRANS: Client exception. %s is a source URI.
787             throw new ClientException(sprintf(_m('No content for notice %s.'),$sourceUri));
788         }
789
790         // Get (safe!) HTML and text versions of the content
791
792         $rendered = $this->purify($sourceContent);
793         $content = common_strip_html($rendered);
794
795         $shortened = common_shorten_links($content);
796
797         // If it's too long, try using the summary, and make the
798         // HTML an attachment.
799
800         $attachment = null;
801
802         if (Notice::contentTooLong($shortened)) {
803             $attachment = $this->saveHTMLFile($note->title, $rendered);
804             $summary = common_strip_html($note->summary);
805             if (empty($summary)) {
806                 $summary = $content;
807             }
808             $shortSummary = common_shorten_links($summary);
809             if (Notice::contentTooLong($shortSummary)) {
810                 $url = common_shorten_url($sourceUrl);
811                 $shortSummary = substr($shortSummary,
812                                        0,
813                                        Notice::maxContent() - (mb_strlen($url) + 2));
814                 $content = $shortSummary . ' ' . $url;
815
816                 // We mark up the attachment link specially for the HTML output
817                 // so we can fold-out the full version inline.
818
819                 // @todo FIXME i18n: This tooltip will be saved with the site's default language
820                 // TRANS: Shown when a notice is longer than supported and/or when attachments are present. At runtime
821                 // TRANS: this will usually be replaced with localised text from StatusNet core messages.
822                 $showMoreText = _m('Show more');
823                 $attachUrl = common_local_url('attachment',
824                                               array('attachment' => $attachment->id));
825                 $rendered = common_render_text($shortSummary) .
826                             '<a href="' . htmlspecialchars($attachUrl) .'"'.
827                             ' class="attachment more"' .
828                             ' title="'. htmlspecialchars($showMoreText) . '">' .
829                             '&#8230;' .
830                             '</a>';
831             }
832         }
833
834         $options = array('is_local' => Notice::REMOTE,
835                         'url' => $sourceUrl,
836                         'uri' => $sourceUri,
837                         'rendered' => $rendered,
838                         'replies' => array(),
839                         'groups' => array(),
840                         'peopletags' => array(),
841                         'tags' => array(),
842                         'urls' => array());
843
844         // Check for optional attributes...
845
846         if (!empty($activity->time)) {
847             $options['created'] = common_sql_date($activity->time);
848         }
849
850         if ($activity->context) {
851             // TODO: context->attention
852             list($options['groups'], $options['replies'])
853                 = self::filterAttention($profile, $activity->context->attention);
854
855             // Maintain direct reply associations
856             // @todo FIXME: What about conversation ID?
857             if (!empty($activity->context->replyToID)) {
858                 $orig = Notice::getKV('uri', $activity->context->replyToID);
859                 if ($orig instanceof Notice) {
860                     $options['reply_to'] = $orig->id;
861                 }
862             }
863             if (!empty($activity->context->conversation)) {
864                 // we store the URI here, Notice class can look it up later
865                 $options['conversation'] = $activity->context->conversation;
866             }
867
868             $location = $activity->context->location;
869             if ($location) {
870                 $options['lat'] = $location->lat;
871                 $options['lon'] = $location->lon;
872                 if ($location->location_id) {
873                     $options['location_ns'] = $location->location_ns;
874                     $options['location_id'] = $location->location_id;
875                 }
876             }
877         }
878
879         if ($this->isPeopletag()) {
880             $options['peopletags'][] = $this->localPeopletag();
881         }
882
883         // Atom categories <-> hashtags
884         foreach ($activity->categories as $cat) {
885             if ($cat->term) {
886                 $term = common_canonical_tag($cat->term);
887                 if ($term) {
888                     $options['tags'][] = $term;
889                 }
890             }
891         }
892
893         // Atom enclosures -> attachment URLs
894         foreach ($activity->enclosures as $href) {
895             // @todo FIXME: Save these locally or....?
896             $options['urls'][] = $href;
897         }
898
899         try {
900             $saved = Notice::saveNew($profile->id,
901                                      $content,
902                                      'ostatus',
903                                      $options);
904             if ($saved instanceof Notice) {
905                 Ostatus_source::saveNew($saved, $this, $method);
906                 if (!empty($attachment)) {
907                     File_to_post::processNew($attachment->id, $saved->id);
908                 }
909             }
910         } catch (Exception $e) {
911             common_log(LOG_ERR, "OStatus save of remote message $sourceUri failed: " . $e->getMessage());
912             throw $e;
913         }
914         common_log(LOG_INFO, "OStatus saved remote message $sourceUri as notice id $saved->id");
915         return $saved;
916     }
917
918     /**
919      * Clean up HTML
920      */
921     protected function purify($html)
922     {
923         require_once INSTALLDIR.'/extlib/htmLawed/htmLawed.php';
924         $config = array('safe' => 1,
925                         'deny_attribute' => 'id,style,on*');
926         return htmLawed($html, $config);
927     }
928
929     /**
930      * Filters a list of recipient ID URIs to just those for local delivery.
931      * @param Profile local profile of sender
932      * @param array in/out &$attention_uris set of URIs, will be pruned on output
933      * @return array of group IDs
934      */
935     static public function filterAttention(Profile $sender, array $attention)
936     {
937         common_debug("Original reply recipients: " . implode(', ', array_keys($attention)));
938         $groups = array();
939         $replies = array();
940         foreach ($attention as $recipient=>$type) {
941             // Is the recipient a local user?
942             $user = User::getKV('uri', $recipient);
943             if ($user instanceof User) {
944                 // @todo FIXME: Sender verification, spam etc?
945                 $replies[] = $recipient;
946                 continue;
947             }
948
949             // Is the recipient a local group?
950             // TODO: $group = User_group::getKV('uri', $recipient);
951             $id = OStatusPlugin::localGroupFromUrl($recipient);
952             if ($id) {
953                 $group = User_group::getKV('id', $id);
954                 if ($group instanceof User_group) {
955                     // Deliver to all members of this local group if allowed.
956                     if ($sender->isMember($group)) {
957                         $groups[] = $group->id;
958                     } else {
959                         common_debug(sprintf('Skipping reply to local group %s as sender %d is not a member', $group->getNickname(), $sender->id));
960                     }
961                     continue;
962                 } else {
963                     common_debug("Skipping reply to bogus group $recipient");
964                 }
965             }
966
967             // Is the recipient a remote user or group?
968             try {
969                 $oprofile = self::ensureProfileURI($recipient);
970                 if ($oprofile->isGroup()) {
971                     // Deliver to local members of this remote group.
972                     // @todo FIXME: Sender verification?
973                     $groups[] = $oprofile->group_id;
974                 } else {
975                     // may be canonicalized or something
976                     $replies[] = $oprofile->getUri();
977                 }
978                 continue;
979             } catch (Exception $e) {
980                 // Neither a recognizable local nor remote user!
981                 common_debug("Skipping reply to unrecognized profile $recipient: " . $e->getMessage());
982             }
983
984         }
985         common_debug("Local reply recipients: " . implode(', ', $replies));
986         common_debug("Local group recipients: " . implode(', ', $groups));
987         return array($groups, $replies);
988     }
989
990     /**
991      * Look up and if necessary create an Ostatus_profile for the remote entity
992      * with the given profile page URL. This should never return null -- you
993      * will either get an object or an exception will be thrown.
994      *
995      * @param string $profile_url
996      * @return Ostatus_profile
997      * @throws Exception on various error conditions
998      * @throws OStatusShadowException if this reference would obscure a local user/group
999      */
1000     public static function ensureProfileURL($profile_url, array $hints=array())
1001     {
1002         $oprofile = self::getFromProfileURL($profile_url);
1003
1004         if ($oprofile instanceof Ostatus_profile) {
1005             return $oprofile;
1006         }
1007
1008         $hints['profileurl'] = $profile_url;
1009
1010         // Fetch the URL
1011         // XXX: HTTP caching
1012
1013         $client = new HTTPClient();
1014         $client->setHeader('Accept', 'text/html,application/xhtml+xml');
1015         $response = $client->get($profile_url);
1016
1017         if (!$response->isOk()) {
1018             // TRANS: Exception. %s is a profile URL.
1019             throw new Exception(sprintf(_m('Could not reach profile page %s.'),$profile_url));
1020         }
1021
1022         // Check if we have a non-canonical URL
1023
1024         $finalUrl = $response->getUrl();
1025
1026         if ($finalUrl != $profile_url) {
1027
1028             $hints['profileurl'] = $finalUrl;
1029
1030             $oprofile = self::getFromProfileURL($finalUrl);
1031
1032             if ($oprofile instanceof Ostatus_profile) {
1033                 return $oprofile;
1034             }
1035         }
1036
1037         // Try to get some hCard data
1038
1039         $body = $response->getBody();
1040
1041         $hcardHints = DiscoveryHints::hcardHints($body, $finalUrl);
1042
1043         if (!empty($hcardHints)) {
1044             $hints = array_merge($hints, $hcardHints);
1045         }
1046
1047         // Check if they've got an LRDD header
1048
1049         $lrdd = LinkHeader::getLink($response, 'lrdd');
1050         try {
1051             $xrd = new XML_XRD();
1052             $xrd->loadFile($lrdd);
1053             $xrdHints = DiscoveryHints::fromXRD($xrd);
1054             $hints = array_merge($hints, $xrdHints);
1055         } catch (Exception $e) {
1056             // No hints available from XRD
1057         }
1058
1059         // If discovery found a feedurl (probably from LRDD), use it.
1060
1061         if (array_key_exists('feedurl', $hints)) {
1062             return self::ensureFeedURL($hints['feedurl'], $hints);
1063         }
1064
1065         // Get the feed URL from HTML
1066
1067         $discover = new FeedDiscovery();
1068
1069         $feedurl = $discover->discoverFromHTML($finalUrl, $body);
1070
1071         if (!empty($feedurl)) {
1072             $hints['feedurl'] = $feedurl;
1073             return self::ensureFeedURL($feedurl, $hints);
1074         }
1075
1076         // TRANS: Exception. %s is a URL.
1077         throw new Exception(sprintf(_m('Could not find a feed URL for profile page %s.'),$finalUrl));
1078     }
1079
1080     /**
1081      * Look up the Ostatus_profile, if present, for a remote entity with the
1082      * given profile page URL. Will return null for both unknown and invalid
1083      * remote profiles.
1084      *
1085      * @return mixed Ostatus_profile or null
1086      * @throws OStatusShadowException for local profiles
1087      */
1088     static function getFromProfileURL($profile_url)
1089     {
1090         $profile = Profile::getKV('profileurl', $profile_url);
1091         if (!$profile instanceof Profile) {
1092             return null;
1093         }
1094
1095         try {
1096             $oprofile = self::getFromProfile($profile);
1097             // We found the profile, return it!
1098             return $oprofile;
1099         } catch (NoResultException $e) {
1100             // Could not find an OStatus profile, is it instead a local user?
1101             $user = User::getKV('id', $profile->id);
1102             if ($user instanceof User) {
1103                 // @todo i18n FIXME: use sprintf and add i18n (?)
1104                 throw new OStatusShadowException($profile, "'$profile_url' is the profile for local user '{$user->nickname}'.");
1105             }
1106         }
1107
1108         // Continue discovery; it's a remote profile
1109         // for OMB or some other protocol, may also
1110         // support OStatus
1111
1112         return null;
1113     }
1114
1115     static function getFromProfile(Profile $profile)
1116     {
1117         $oprofile = new Ostatus_profile();
1118         $oprofile->profile_id = $profile->id;
1119         if (!$oprofile->find(true)) {
1120             throw new NoResultException($oprofile);
1121         }
1122         return $oprofile;
1123     }
1124
1125     /**
1126      * Look up and if necessary create an Ostatus_profile for remote entity
1127      * with the given update feed. This should never return null -- you will
1128      * either get an object or an exception will be thrown.
1129      *
1130      * @return Ostatus_profile
1131      * @throws Exception
1132      */
1133     public static function ensureFeedURL($feed_url, array $hints=array())
1134     {
1135         $discover = new FeedDiscovery();
1136
1137         $feeduri = $discover->discoverFromFeedURL($feed_url);
1138         $hints['feedurl'] = $feeduri;
1139
1140         $huburi = $discover->getHubLink();
1141         $hints['hub'] = $huburi;
1142
1143         // XXX: NS_REPLIES is deprecated anyway, so let's remove it in the future.
1144         $salmonuri = $discover->getAtomLink(Salmon::REL_SALMON)
1145                         ?: $discover->getAtomLink(Salmon::NS_REPLIES);
1146         $hints['salmon'] = $salmonuri;
1147
1148         if (!$huburi && !common_config('feedsub', 'fallback_hub') && !common_config('feedsub', 'nohub')) {
1149             // We can only deal with folks with a PuSH hub
1150             // unless we have something similar available locally.
1151             throw new FeedSubNoHubException();
1152         }
1153
1154         $feedEl = $discover->root;
1155
1156         if ($feedEl->tagName == 'feed') {
1157             return self::ensureAtomFeed($feedEl, $hints);
1158         } else if ($feedEl->tagName == 'channel') {
1159             return self::ensureRssChannel($feedEl, $hints);
1160         } else {
1161             throw new FeedSubBadXmlException($feeduri);
1162         }
1163     }
1164
1165     /**
1166      * Look up and, if necessary, create an Ostatus_profile for the remote
1167      * profile with the given Atom feed - actually loaded from the feed.
1168      * This should never return null -- you will either get an object or
1169      * an exception will be thrown.
1170      *
1171      * @param DOMElement $feedEl root element of a loaded Atom feed
1172      * @param array $hints additional discovery information passed from higher levels
1173      * @todo FIXME: Should this be marked public?
1174      * @return Ostatus_profile
1175      * @throws Exception
1176      */
1177     public static function ensureAtomFeed(DOMElement $feedEl, array $hints)
1178     {
1179         $author = ActivityUtils::getFeedAuthor($feedEl);
1180
1181         if (empty($author)) {
1182             // XXX: make some educated guesses here
1183             // TRANS: Feed sub exception.
1184             throw new FeedSubException(_m('Cannot find enough profile '.
1185                                           'information to make a feed.'));
1186         }
1187
1188         return self::ensureActivityObjectProfile($author, $hints);
1189     }
1190
1191     /**
1192      * Look up and, if necessary, create an Ostatus_profile for the remote
1193      * profile with the given RSS feed - actually loaded from the feed.
1194      * This should never return null -- you will either get an object or
1195      * an exception will be thrown.
1196      *
1197      * @param DOMElement $feedEl root element of a loaded RSS feed
1198      * @param array $hints additional discovery information passed from higher levels
1199      * @todo FIXME: Should this be marked public?
1200      * @return Ostatus_profile
1201      * @throws Exception
1202      */
1203     public static function ensureRssChannel(DOMElement $feedEl, array $hints)
1204     {
1205         // Special-case for Posterous. They have some nice metadata in their
1206         // posterous:author elements. We should use them instead of the channel.
1207
1208         $items = $feedEl->getElementsByTagName('item');
1209
1210         if ($items->length > 0) {
1211             $item = $items->item(0);
1212             $authorEl = ActivityUtils::child($item, ActivityObject::AUTHOR, ActivityObject::POSTEROUS);
1213             if (!empty($authorEl)) {
1214                 $obj = ActivityObject::fromPosterousAuthor($authorEl);
1215                 // Posterous has multiple authors per feed, and multiple feeds
1216                 // per author. We check if this is the "main" feed for this author.
1217                 if (array_key_exists('profileurl', $hints) &&
1218                     !empty($obj->poco) &&
1219                     common_url_to_nickname($hints['profileurl']) == $obj->poco->preferredUsername) {
1220                     return self::ensureActivityObjectProfile($obj, $hints);
1221                 }
1222             }
1223         }
1224
1225         // @todo FIXME: We should check whether this feed has elements
1226         // with different <author> or <dc:creator> elements, and... I dunno.
1227         // Do something about that.
1228
1229         $obj = ActivityObject::fromRssChannel($feedEl);
1230
1231         return self::ensureActivityObjectProfile($obj, $hints);
1232     }
1233
1234     /**
1235      * Download and update given avatar image
1236      *
1237      * @param string $url
1238      * @return Avatar    The Avatar we have on disk. (seldom used)
1239      * @throws Exception in various failure cases
1240      */
1241     public function updateAvatar($url, $force=false)
1242     {
1243         try {
1244             // If avatar URL differs: update. If URLs were identical but we're forced: update.
1245             if ($url == $this->avatar && !$force) {
1246                 // If there's no locally stored avatar, throw an exception and continue fetching below.
1247                 $avatar = Avatar::getUploaded($this->localProfile()) instanceof Avatar;
1248                 return $avatar;
1249             }
1250         } catch (NoAvatarException $e) {
1251             // No avatar available, let's fetch it.
1252         }
1253
1254         if (!common_valid_http_url($url)) {
1255             // TRANS: Server exception. %s is a URL.
1256             throw new ServerException(sprintf(_m('Invalid avatar URL %s.'), $url));
1257         }
1258
1259         $self = $this->localProfile();
1260
1261         // @todo FIXME: This should be better encapsulated
1262         // ripped from oauthstore.php (for old OMB client)
1263         $temp_filename = tempnam(common_get_temp_dir(), 'listener_avatar');
1264         try {
1265             $imgData = HTTPClient::quickGet($url);
1266             // Make sure it's at least an image file. ImageFile can do the rest.
1267             if (false === getimagesizefromstring($imgData)) {
1268                 throw new UnsupportedMediaException(_('Downloaded group avatar was not an image.'));
1269             }
1270             file_put_contents($temp_filename, $imgData);
1271             unset($imgData);    // No need to carry this in memory.
1272
1273             if ($this->isGroup()) {
1274                 $id = $this->group_id;
1275             } else {
1276                 $id = $this->profile_id;
1277             }
1278             // @todo FIXME: Should we be using different ids?
1279             $imagefile = new ImageFile($id, $temp_filename);
1280             $filename = Avatar::filename($id,
1281                                          image_type_to_extension($imagefile->type),
1282                                          null,
1283                                          common_timestamp());
1284             rename($temp_filename, Avatar::path($filename));
1285         } catch (Exception $e) {
1286             unlink($temp_filename);
1287             throw $e;
1288         }
1289         // @todo FIXME: Hardcoded chmod is lame, but seems to be necessary to
1290         // keep from accidentally saving images from command-line (queues)
1291         // that can't be read from web server, which causes hard-to-notice
1292         // problems later on:
1293         //
1294         // http://status.net/open-source/issues/2663
1295         chmod(Avatar::path($filename), 0644);
1296
1297         $self->setOriginal($filename);
1298
1299         $orig = clone($this);
1300         $this->avatar = $url;
1301         $this->update($orig);
1302
1303         return Avatar::getUploaded($self);
1304     }
1305
1306     /**
1307      * Pull avatar URL from ActivityObject or profile hints
1308      *
1309      * @param ActivityObject $object
1310      * @param array $hints
1311      * @return mixed URL string or false
1312      */
1313     public static function getActivityObjectAvatar(ActivityObject $object, array $hints=array())
1314     {
1315         if ($object->avatarLinks) {
1316             $best = false;
1317             // Take the exact-size avatar, or the largest avatar, or the first avatar if all sizeless
1318             foreach ($object->avatarLinks as $avatar) {
1319                 if ($avatar->width == AVATAR_PROFILE_SIZE && $avatar->height = AVATAR_PROFILE_SIZE) {
1320                     // Exact match!
1321                     $best = $avatar;
1322                     break;
1323                 }
1324                 if (!$best || $avatar->width > $best->width) {
1325                     $best = $avatar;
1326                 }
1327             }
1328             return $best->url;
1329         } else if (array_key_exists('avatar', $hints)) {
1330             return $hints['avatar'];
1331         }
1332         return false;
1333     }
1334
1335     /**
1336      * Get an appropriate avatar image source URL, if available.
1337      *
1338      * @param ActivityObject $actor
1339      * @param DOMElement $feed
1340      * @return string
1341      */
1342     protected static function getAvatar(ActivityObject $actor, DOMElement $feed)
1343     {
1344         $url = '';
1345         $icon = '';
1346         if ($actor->avatar) {
1347             $url = trim($actor->avatar);
1348         }
1349         if (!$url) {
1350             // Check <atom:logo> and <atom:icon> on the feed
1351             $els = $feed->childNodes();
1352             if ($els && $els->length) {
1353                 for ($i = 0; $i < $els->length; $i++) {
1354                     $el = $els->item($i);
1355                     if ($el->namespaceURI == Activity::ATOM) {
1356                         if (empty($url) && $el->localName == 'logo') {
1357                             $url = trim($el->textContent);
1358                             break;
1359                         }
1360                         if (empty($icon) && $el->localName == 'icon') {
1361                             // Use as a fallback
1362                             $icon = trim($el->textContent);
1363                         }
1364                     }
1365                 }
1366             }
1367             if ($icon && !$url) {
1368                 $url = $icon;
1369             }
1370         }
1371         if ($url) {
1372             $opts = array('allowed_schemes' => array('http', 'https'));
1373             if (common_valid_http_url($url)) {
1374                 return $url;
1375             }
1376         }
1377
1378         return Plugin::staticPath('OStatus', 'images/96px-Feed-icon.svg.png');
1379     }
1380
1381     /**
1382      * Fetch, or build if necessary, an Ostatus_profile for the actor
1383      * in a given Activity Streams activity.
1384      * This should never return null -- you will either get an object or
1385      * an exception will be thrown.
1386      *
1387      * @param Activity $activity
1388      * @param string $feeduri if we already know the canonical feed URI!
1389      * @param string $salmonuri if we already know the salmon return channel URI
1390      * @return Ostatus_profile
1391      * @throws Exception
1392      */
1393     public static function ensureActorProfile(Activity $activity, array $hints=array())
1394     {
1395         return self::ensureActivityObjectProfile($activity->actor, $hints);
1396     }
1397
1398     /**
1399      * Fetch, or build if necessary, an Ostatus_profile for the profile
1400      * in a given Activity Streams object (can be subject, actor, or object).
1401      * This should never return null -- you will either get an object or
1402      * an exception will be thrown.
1403      *
1404      * @param ActivityObject $object
1405      * @param array $hints additional discovery information passed from higher levels
1406      * @return Ostatus_profile
1407      * @throws Exception
1408      */
1409     public static function ensureActivityObjectProfile(ActivityObject $object, array $hints=array())
1410     {
1411         $profile = self::getActivityObjectProfile($object);
1412         if ($profile instanceof Ostatus_profile) {
1413             $profile->updateFromActivityObject($object, $hints);
1414         } else {
1415             $profile = self::createActivityObjectProfile($object, $hints);
1416         }
1417         return $profile;
1418     }
1419
1420     /**
1421      * @param Activity $activity
1422      * @return mixed matching Ostatus_profile or false if none known
1423      * @throws ServerException if feed info invalid
1424      */
1425     public static function getActorProfile(Activity $activity)
1426     {
1427         return self::getActivityObjectProfile($activity->actor);
1428     }
1429
1430     /**
1431      * @param ActivityObject $activity
1432      * @return mixed matching Ostatus_profile or false if none known
1433      * @throws ServerException if feed info invalid
1434      */
1435     protected static function getActivityObjectProfile(ActivityObject $object)
1436     {
1437         $uri = self::getActivityObjectProfileURI($object);
1438         return Ostatus_profile::getKV('uri', $uri);
1439     }
1440
1441     /**
1442      * Get the identifier URI for the remote entity described
1443      * by this ActivityObject. This URI is *not* guaranteed to be
1444      * a resolvable HTTP/HTTPS URL.
1445      *
1446      * @param ActivityObject $object
1447      * @return string
1448      * @throws ServerException if feed info invalid
1449      */
1450     protected static function getActivityObjectProfileURI(ActivityObject $object)
1451     {
1452         if ($object->id) {
1453             if (ActivityUtils::validateUri($object->id)) {
1454                 return $object->id;
1455             }
1456         }
1457
1458         // If the id is missing or invalid (we've seen feeds mistakenly listing
1459         // things like local usernames in that field) then we'll use the profile
1460         // page link, if valid.
1461         if ($object->link && common_valid_http_url($object->link)) {
1462             return $object->link;
1463         }
1464         // TRANS: Server exception.
1465         throw new ServerException(_m('No author ID URI found.'));
1466     }
1467
1468     /**
1469      * @todo FIXME: Validate stuff somewhere.
1470      */
1471
1472     /**
1473      * Create local ostatus_profile and profile/user_group entries for
1474      * the provided remote user or group.
1475      * This should never return null -- you will either get an object or
1476      * an exception will be thrown.
1477      *
1478      * @param ActivityObject $object
1479      * @param array $hints
1480      *
1481      * @return Ostatus_profile
1482      */
1483     protected static function createActivityObjectProfile(ActivityObject $object, array $hints=array())
1484     {
1485         $homeuri = $object->id;
1486         $discover = false;
1487
1488         if (!$homeuri) {
1489             common_debug(__METHOD__ . " empty actor profile URI: " . var_export($activity, true));
1490             // TRANS: Exception.
1491             throw new Exception(_m('No profile URI.'));
1492         }
1493
1494         $user = User::getKV('uri', $homeuri);
1495         if ($user instanceof User) {
1496             // TRANS: Exception.
1497             throw new Exception(_m('Local user cannot be referenced as remote.'));
1498         }
1499
1500         if (OStatusPlugin::localGroupFromUrl($homeuri)) {
1501             // TRANS: Exception.
1502             throw new Exception(_m('Local group cannot be referenced as remote.'));
1503         }
1504
1505         $ptag = Profile_list::getKV('uri', $homeuri);
1506         if ($ptag instanceof Profile_list) {
1507             $local_user = User::getKV('id', $ptag->tagger);
1508             if ($local_user instanceof User) {
1509                 // TRANS: Exception.
1510                 throw new Exception(_m('Local list cannot be referenced as remote.'));
1511             }
1512         }
1513
1514         if (array_key_exists('feedurl', $hints)) {
1515             $feeduri = $hints['feedurl'];
1516         } else {
1517             $discover = new FeedDiscovery();
1518             $feeduri = $discover->discoverFromURL($homeuri);
1519         }
1520
1521         if (array_key_exists('salmon', $hints)) {
1522             $salmonuri = $hints['salmon'];
1523         } else {
1524             if (!$discover) {
1525                 $discover = new FeedDiscovery();
1526                 $discover->discoverFromFeedURL($hints['feedurl']);
1527             }
1528             // XXX: NS_REPLIES is deprecated anyway, so let's remove it in the future.
1529             $salmonuri = $discover->getAtomLink(Salmon::REL_SALMON)
1530                             ?: $discover->getAtomLink(Salmon::NS_REPLIES);
1531         }
1532
1533         if (array_key_exists('hub', $hints)) {
1534             $huburi = $hints['hub'];
1535         } else {
1536             if (!$discover) {
1537                 $discover = new FeedDiscovery();
1538                 $discover->discoverFromFeedURL($hints['feedurl']);
1539             }
1540             $huburi = $discover->getHubLink();
1541         }
1542
1543         if (!$huburi && !common_config('feedsub', 'fallback_hub') && !common_config('feedsub', 'nohub')) {
1544             // We can only deal with folks with a PuSH hub
1545             throw new FeedSubNoHubException();
1546         }
1547
1548         $oprofile = new Ostatus_profile();
1549
1550         $oprofile->uri        = $homeuri;
1551         $oprofile->feeduri    = $feeduri;
1552         $oprofile->salmonuri  = $salmonuri;
1553
1554         $oprofile->created    = common_sql_now();
1555         $oprofile->modified   = common_sql_now();
1556
1557         if ($object->type == ActivityObject::PERSON) {
1558             $profile = new Profile();
1559             $profile->created = common_sql_now();
1560             self::updateProfile($profile, $object, $hints);
1561
1562             $oprofile->profile_id = $profile->insert();
1563             if ($oprofile->profile_id === false) {
1564                 // TRANS: Server exception.
1565                 throw new ServerException(_m('Cannot save local profile.'));
1566             }
1567         } else if ($object->type == ActivityObject::GROUP) {
1568             $profile = new Profile();
1569             $profile->query('BEGIN');
1570
1571             $group = new User_group();
1572             $group->uri = $homeuri;
1573             $group->created = common_sql_now();
1574             self::updateGroup($group, $object, $hints);
1575
1576             // TODO: We should do this directly in User_group->insert()!
1577             // currently it's duplicated in User_group->update()
1578             // AND User_group->register()!!!
1579             $fields = array(/*group field => profile field*/
1580                         'nickname'      => 'nickname',
1581                         'fullname'      => 'fullname',
1582                         'mainpage'      => 'profileurl',
1583                         'homepage'      => 'homepage',
1584                         'description'   => 'bio',
1585                         'location'      => 'location',
1586                         'created'       => 'created',
1587                         'modified'      => 'modified',
1588                         );
1589             foreach ($fields as $gf=>$pf) {
1590                 $profile->$pf = $group->$gf;
1591             }
1592             $profile_id = $profile->insert();
1593             if ($profile_id === false) {
1594                 $profile->query('ROLLBACK');
1595                 throw new ServerException(_('Profile insertion failed.'));
1596             }
1597
1598             $group->profile_id = $profile_id;
1599
1600             $oprofile->group_id = $group->insert();
1601             if ($oprofile->group_id === false) {
1602                 $profile->query('ROLLBACK');
1603                 // TRANS: Server exception.
1604                 throw new ServerException(_m('Cannot save local profile.'));
1605             }
1606
1607             $profile->query('COMMIT');
1608         } else if ($object->type == ActivityObject::_LIST) {
1609             $ptag = new Profile_list();
1610             $ptag->uri = $homeuri;
1611             $ptag->created = common_sql_now();
1612             self::updatePeopletag($ptag, $object, $hints);
1613
1614             $oprofile->peopletag_id = $ptag->insert();
1615             if ($oprofile->peopletag_id === false) {
1616                 // TRANS: Server exception.
1617                 throw new ServerException(_m('Cannot save local list.'));
1618             }
1619         }
1620
1621         $ok = $oprofile->insert();
1622
1623         if ($ok === false) {
1624             // TRANS: Server exception.
1625             throw new ServerException(_m('Cannot save OStatus profile.'));
1626         }
1627
1628         $avatar = self::getActivityObjectAvatar($object, $hints);
1629
1630         if ($avatar) {
1631             try {
1632                 $oprofile->updateAvatar($avatar);
1633             } catch (Exception $ex) {
1634                 // Profile is saved, but Avatar is messed up. We're
1635                 // just going to continue.
1636                 common_log(LOG_WARNING, "Exception saving OStatus profile avatar: ". $ex->getMessage());
1637             }
1638         }
1639
1640         return $oprofile;
1641     }
1642
1643     /**
1644      * Save any updated profile information to our local copy.
1645      * @param ActivityObject $object
1646      * @param array $hints
1647      */
1648     public function updateFromActivityObject(ActivityObject $object, array $hints=array())
1649     {
1650         if ($this->isGroup()) {
1651             $group = $this->localGroup();
1652             self::updateGroup($group, $object, $hints);
1653         } else if ($this->isPeopletag()) {
1654             $ptag = $this->localPeopletag();
1655             self::updatePeopletag($ptag, $object, $hints);
1656         } else {
1657             $profile = $this->localProfile();
1658             self::updateProfile($profile, $object, $hints);
1659         }
1660
1661         $avatar = self::getActivityObjectAvatar($object, $hints);
1662         if ($avatar && !isset($ptag)) {
1663             try {
1664                 $this->updateAvatar($avatar);
1665             } catch (Exception $ex) {
1666                 common_log(LOG_WARNING, "Exception saving OStatus profile avatar: " . $ex->getMessage());
1667             }
1668         }
1669     }
1670
1671     public static function updateProfile(Profile $profile, ActivityObject $object, array $hints=array())
1672     {
1673         $orig = clone($profile);
1674
1675         // Existing nickname is better than nothing.
1676
1677         if (!array_key_exists('nickname', $hints)) {
1678             $hints['nickname'] = $profile->nickname;
1679         }
1680
1681         $nickname = self::getActivityObjectNickname($object, $hints);
1682
1683         if (!empty($nickname)) {
1684             $profile->nickname = $nickname;
1685         }
1686
1687         if (!empty($object->title)) {
1688             $profile->fullname = $object->title;
1689         } else if (array_key_exists('fullname', $hints)) {
1690             $profile->fullname = $hints['fullname'];
1691         }
1692
1693         if (!empty($object->link)) {
1694             $profile->profileurl = $object->link;
1695         } else if (array_key_exists('profileurl', $hints)) {
1696             $profile->profileurl = $hints['profileurl'];
1697         } else if (common_valid_http_url($object->id)) {
1698             $profile->profileurl = $object->id;
1699         }
1700
1701         $bio = self::getActivityObjectBio($object, $hints);
1702
1703         if (!empty($bio)) {
1704             $profile->bio = $bio;
1705         }
1706
1707         $location = self::getActivityObjectLocation($object, $hints);
1708
1709         if (!empty($location)) {
1710             $profile->location = $location;
1711         }
1712
1713         $homepage = self::getActivityObjectHomepage($object, $hints);
1714
1715         if (!empty($homepage)) {
1716             $profile->homepage = $homepage;
1717         }
1718
1719         if (!empty($object->geopoint)) {
1720             $location = ActivityContext::locationFromPoint($object->geopoint);
1721             if (!empty($location)) {
1722                 $profile->lat = $location->lat;
1723                 $profile->lon = $location->lon;
1724             }
1725         }
1726
1727         // @todo FIXME: tags/categories
1728         // @todo tags from categories
1729
1730         if ($profile->id) {
1731             common_debug("Updating OStatus profile $profile->id from remote info $object->id: " . var_export($object, true) . var_export($hints, true));
1732             $profile->update($orig);
1733         }
1734     }
1735
1736     protected static function updateGroup(User_group $group, ActivityObject $object, array $hints=array())
1737     {
1738         $orig = clone($group);
1739
1740         $group->nickname = self::getActivityObjectNickname($object, $hints);
1741         $group->fullname = $object->title;
1742
1743         if (!empty($object->link)) {
1744             $group->mainpage = $object->link;
1745         } else if (array_key_exists('profileurl', $hints)) {
1746             $group->mainpage = $hints['profileurl'];
1747         }
1748
1749         // @todo tags from categories
1750         $group->description = self::getActivityObjectBio($object, $hints);
1751         $group->location = self::getActivityObjectLocation($object, $hints);
1752         $group->homepage = self::getActivityObjectHomepage($object, $hints);
1753
1754         if ($group->id) {   // If no id, we haven't called insert() yet, so don't run update()
1755             common_debug("Updating OStatus group $group->id from remote info $object->id: " . var_export($object, true) . var_export($hints, true));
1756             $group->update($orig);
1757         }
1758     }
1759
1760     protected static function updatePeopletag(Peopletag $tag, ActivityObject $object, array $hints=array()) {
1761         $orig = clone($tag);
1762
1763         $tag->tag = $object->title;
1764
1765         if (!empty($object->link)) {
1766             $tag->mainpage = $object->link;
1767         } else if (array_key_exists('profileurl', $hints)) {
1768             $tag->mainpage = $hints['profileurl'];
1769         }
1770
1771         $tag->description = $object->summary;
1772         $tagger = self::ensureActivityObjectProfile($object->owner);
1773         $tag->tagger = $tagger->profile_id;
1774
1775         if ($tag->id) {
1776             common_debug("Updating OStatus peopletag $tag->id from remote info $object->id: " . var_export($object, true) . var_export($hints, true));
1777             $tag->update($orig);
1778         }
1779     }
1780
1781     protected static function getActivityObjectHomepage(ActivityObject $object, array $hints=array())
1782     {
1783         $homepage = null;
1784         $poco     = $object->poco;
1785
1786         if (!empty($poco)) {
1787             $url = $poco->getPrimaryURL();
1788             if ($url && $url->type == 'homepage') {
1789                 $homepage = $url->value;
1790             }
1791         }
1792
1793         // @todo Try for a another PoCo URL?
1794
1795         return $homepage;
1796     }
1797
1798     protected static function getActivityObjectLocation(ActivityObject $object, array $hints=array())
1799     {
1800         $location = null;
1801
1802         if (!empty($object->poco) &&
1803             isset($object->poco->address->formatted)) {
1804             $location = $object->poco->address->formatted;
1805         } else if (array_key_exists('location', $hints)) {
1806             $location = $hints['location'];
1807         }
1808
1809         if (!empty($location)) {
1810             if (mb_strlen($location) > 255) {
1811                 $location = mb_substr($note, 0, 255 - 3) . ' â€¦ ';
1812             }
1813         }
1814
1815         // @todo Try to find location some othe way? Via goerss point?
1816
1817         return $location;
1818     }
1819
1820     protected static function getActivityObjectBio(ActivityObject $object, array $hints=array())
1821     {
1822         $bio  = null;
1823
1824         if (!empty($object->poco)) {
1825             $note = $object->poco->note;
1826         } else if (array_key_exists('bio', $hints)) {
1827             $note = $hints['bio'];
1828         }
1829
1830         if (!empty($note)) {
1831             if (Profile::bioTooLong($note)) {
1832                 // XXX: truncate ok?
1833                 $bio = mb_substr($note, 0, Profile::maxBio() - 3) . ' â€¦ ';
1834             } else {
1835                 $bio = $note;
1836             }
1837         }
1838
1839         // @todo Try to get bio info some other way?
1840
1841         return $bio;
1842     }
1843
1844     public static function getActivityObjectNickname(ActivityObject $object, array $hints=array())
1845     {
1846         if ($object->poco) {
1847             if (!empty($object->poco->preferredUsername)) {
1848                 return common_nicknamize($object->poco->preferredUsername);
1849             }
1850         }
1851
1852         if (!empty($object->nickname)) {
1853             return common_nicknamize($object->nickname);
1854         }
1855
1856         if (array_key_exists('nickname', $hints)) {
1857             return $hints['nickname'];
1858         }
1859
1860         // Try the profile url (like foo.example.com or example.com/user/foo)
1861         if (!empty($object->link)) {
1862             $profileUrl = $object->link;
1863         } else if (!empty($hints['profileurl'])) {
1864             $profileUrl = $hints['profileurl'];
1865         }
1866
1867         if (!empty($profileUrl)) {
1868             $nickname = self::nicknameFromURI($profileUrl);
1869         }
1870
1871         // Try the URI (may be a tag:, http:, acct:, ...
1872
1873         if (empty($nickname)) {
1874             $nickname = self::nicknameFromURI($object->id);
1875         }
1876
1877         // Try a Webfinger if one was passed (way) down
1878
1879         if (empty($nickname)) {
1880             if (array_key_exists('webfinger', $hints)) {
1881                 $nickname = self::nicknameFromURI($hints['webfinger']);
1882             }
1883         }
1884
1885         // Try the name
1886
1887         if (empty($nickname)) {
1888             $nickname = common_nicknamize($object->title);
1889         }
1890
1891         return $nickname;
1892     }
1893
1894     protected static function nicknameFromURI($uri)
1895     {
1896         if (preg_match('/(\w+):/', $uri, $matches)) {
1897             $protocol = $matches[1];
1898         } else {
1899             return null;
1900         }
1901
1902         switch ($protocol) {
1903         case 'acct':
1904         case 'mailto':
1905             if (preg_match("/^$protocol:(.*)?@.*\$/", $uri, $matches)) {
1906                 return common_canonical_nickname($matches[1]);
1907             }
1908             return null;
1909         case 'http':
1910             return common_url_to_nickname($uri);
1911             break;
1912         default:
1913             return null;
1914         }
1915     }
1916
1917     /**
1918      * Look up, and if necessary create, an Ostatus_profile for the remote
1919      * entity with the given webfinger address.
1920      * This should never return null -- you will either get an object or
1921      * an exception will be thrown.
1922      *
1923      * @param string $addr webfinger address
1924      * @return Ostatus_profile
1925      * @throws Exception on error conditions
1926      * @throws OStatusShadowException if this reference would obscure a local user/group
1927      */
1928     public static function ensureWebfinger($addr)
1929     {
1930         // First, try the cache
1931
1932         $uri = self::cacheGet(sprintf('ostatus_profile:webfinger:%s', $addr));
1933
1934         if ($uri !== false) {
1935             if (is_null($uri)) {
1936                 // Negative cache entry
1937                 // TRANS: Exception.
1938                 throw new Exception(_m('Not a valid webfinger address.'));
1939             }
1940             $oprofile = Ostatus_profile::getKV('uri', $uri);
1941             if ($oprofile instanceof Ostatus_profile) {
1942                 return $oprofile;
1943             }
1944         }
1945
1946         // Try looking it up
1947         $oprofile = Ostatus_profile::getKV('uri', Discovery::normalize($addr));
1948
1949         if ($oprofile instanceof Ostatus_profile) {
1950             self::cacheSet(sprintf('ostatus_profile:webfinger:%s', $addr), $oprofile->getUri());
1951             return $oprofile;
1952         }
1953
1954         // Now, try some discovery
1955
1956         $disco = new Discovery();
1957
1958         try {
1959             $xrd = $disco->lookup($addr);
1960         } catch (Exception $e) {
1961             // Save negative cache entry so we don't waste time looking it up again.
1962             // @todo FIXME: Distinguish temporary failures?
1963             self::cacheSet(sprintf('ostatus_profile:webfinger:%s', $addr), null);
1964             // TRANS: Exception.
1965             throw new Exception(_m('Not a valid webfinger address.'));
1966         }
1967
1968         $hints = array('webfinger' => $addr);
1969
1970         $dhints = DiscoveryHints::fromXRD($xrd);
1971
1972         $hints = array_merge($hints, $dhints);
1973
1974         // If there's an Hcard, let's grab its info
1975         if (array_key_exists('hcard', $hints)) {
1976             if (!array_key_exists('profileurl', $hints) ||
1977                 $hints['hcard'] != $hints['profileurl']) {
1978                 $hcardHints = DiscoveryHints::fromHcardUrl($hints['hcard']);
1979                 $hints = array_merge($hcardHints, $hints);
1980             }
1981         }
1982
1983         // If we got a feed URL, try that
1984         $feedUrl = null;
1985         if (array_key_exists('feedurl', $hints)) {
1986             $feedUrl = $hints['feedurl'];
1987             try {
1988                 common_log(LOG_INFO, "Discovery on acct:$addr with feed URL " . $hints['feedurl']);
1989                 $oprofile = self::ensureFeedURL($hints['feedurl'], $hints);
1990                 self::cacheSet(sprintf('ostatus_profile:webfinger:%s', $addr), $oprofile->getUri());
1991                 return $oprofile;
1992             } catch (Exception $e) {
1993                 common_log(LOG_WARNING, "Failed creating profile from feed URL '$feedUrl': " . $e->getMessage());
1994                 // keep looking
1995             }
1996         }
1997
1998         // If we got a profile page, try that!
1999         $profileUrl = null;
2000         if (array_key_exists('profileurl', $hints)) {
2001             $profileUrl = $hints['profileurl'];
2002             try {
2003                 common_log(LOG_INFO, "Discovery on acct:$addr with profile URL $profileUrl");
2004                 $oprofile = self::ensureProfileURL($hints['profileurl'], $hints);
2005                 self::cacheSet(sprintf('ostatus_profile:webfinger:%s', $addr), $oprofile->getUri());
2006                 return $oprofile;
2007             } catch (OStatusShadowException $e) {
2008                 // We've ended up with a remote reference to a local user or group.
2009                 // @todo FIXME: Ideally we should be able to say who it was so we can
2010                 // go back and refer to it the regular way
2011                 throw $e;
2012             } catch (Exception $e) {
2013                 common_log(LOG_WARNING, "Failed creating profile from profile URL '$profileUrl': " . $e->getMessage());
2014                 // keep looking
2015                 //
2016                 // @todo FIXME: This means an error discovering from profile page
2017                 // may give us a corrupt entry using the webfinger URI, which
2018                 // will obscure the correct page-keyed profile later on.
2019             }
2020         }
2021
2022         // XXX: try hcard
2023         // XXX: try FOAF
2024
2025         if (array_key_exists('salmon', $hints)) {
2026             $salmonEndpoint = $hints['salmon'];
2027
2028             // An account URL, a salmon endpoint, and a dream? Not much to go
2029             // on, but let's give it a try
2030
2031             $uri = 'acct:'.$addr;
2032
2033             $profile = new Profile();
2034
2035             $profile->nickname = self::nicknameFromUri($uri);
2036             $profile->created  = common_sql_now();
2037
2038             if (!is_null($profileUrl)) {
2039                 $profile->profileurl = $profileUrl;
2040             }
2041
2042             $profile_id = $profile->insert();
2043
2044             if ($profile_id === false) {
2045                 common_log_db_error($profile, 'INSERT', __FILE__);
2046                 // TRANS: Exception. %s is a webfinger address.
2047                 throw new Exception(sprintf(_m('Could not save profile for "%s".'),$addr));
2048             }
2049
2050             $oprofile = new Ostatus_profile();
2051
2052             $oprofile->uri        = $uri;
2053             $oprofile->salmonuri  = $salmonEndpoint;
2054             $oprofile->profile_id = $profile_id;
2055             $oprofile->created    = common_sql_now();
2056
2057             if (!is_null($feedUrl)) {
2058                 $oprofile->feeduri = $feedUrl;
2059             }
2060
2061             $result = $oprofile->insert();
2062
2063             if ($result === false) {
2064                 $profile->delete();
2065                 common_log_db_error($oprofile, 'INSERT', __FILE__);
2066                 // TRANS: Exception. %s is a webfinger address.
2067                 throw new Exception(sprintf(_m('Could not save OStatus profile for "%s".'),$addr));
2068             }
2069
2070             self::cacheSet(sprintf('ostatus_profile:webfinger:%s', $addr), $oprofile->getUri());
2071             return $oprofile;
2072         }
2073
2074         // TRANS: Exception. %s is a webfinger address.
2075         throw new Exception(sprintf(_m('Could not find a valid profile for "%s".'),$addr));
2076     }
2077
2078     /**
2079      * Store the full-length scrubbed HTML of a remote notice to an attachment
2080      * file on our server. We'll link to this at the end of the cropped version.
2081      *
2082      * @param string $title plaintext for HTML page's title
2083      * @param string $rendered HTML fragment for HTML page's body
2084      * @return File
2085      */
2086     function saveHTMLFile($title, $rendered)
2087     {
2088         $final = sprintf("<!DOCTYPE html>\n" .
2089                          '<html><head>' .
2090                          '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">' .
2091                          '<title>%s</title>' .
2092                          '</head>' .
2093                          '<body>%s</body></html>',
2094                          htmlspecialchars($title),
2095                          $rendered);
2096
2097         $filename = File::filename($this->localProfile(),
2098                                    'ostatus', // ignored?
2099                                    'text/html');
2100
2101         $filepath = File::path($filename);
2102
2103         file_put_contents($filepath, $final);
2104
2105         $file = new File;
2106
2107         $file->filename = $filename;
2108         $file->url      = File::url($filename);
2109         $file->size     = filesize($filepath);
2110         $file->date     = time();
2111         $file->mimetype = 'text/html';
2112
2113         $file_id = $file->insert();
2114
2115         if ($file_id === false) {
2116             common_log_db_error($file, "INSERT", __FILE__);
2117             // TRANS: Server exception.
2118             throw new ServerException(_m('Could not store HTML content of long post as file.'));
2119         }
2120
2121         return $file;
2122     }
2123
2124     static function ensureProfileURI($uri)
2125     {
2126         $oprofile = null;
2127
2128         // First, try to query it
2129
2130         $oprofile = Ostatus_profile::getKV('uri', $uri);
2131
2132         if ($oprofile instanceof Ostatus_profile) {
2133             return $oprofile;
2134         }
2135
2136         // If unfound, do discovery stuff
2137         if (preg_match("/^(\w+)\:(.*)/", $uri, $match)) {
2138             $protocol = $match[1];
2139             switch ($protocol) {
2140             case 'http':
2141             case 'https':
2142                 $oprofile = self::ensureProfileURL($uri);
2143                 break;
2144             case 'acct':
2145             case 'mailto':
2146                 $rest = $match[2];
2147                 $oprofile = self::ensureWebfinger($rest);
2148                 break;
2149             default:
2150                 // TRANS: Server exception.
2151                 // TRANS: %1$s is a protocol, %2$s is a URI.
2152                 throw new ServerException(sprintf(_m('Unrecognized URI protocol for profile: %1$s (%2$s).'),
2153                                                   $protocol,
2154                                                   $uri));
2155                 break;
2156             }
2157         } else {
2158             // TRANS: Server exception. %s is a URI.
2159             throw new ServerException(sprintf(_m('No URI protocol for profile: %s.'),$uri));
2160         }
2161
2162         return $oprofile;
2163     }
2164
2165     public function checkAuthorship(Activity $activity)
2166     {
2167         if ($this->isGroup() || $this->isPeopletag()) {
2168             // A group or propletag feed will contain posts from multiple authors.
2169             $oprofile = self::ensureActorProfile($activity);
2170             if ($oprofile->isGroup() || $oprofile->isPeopletag()) {
2171                 // Groups can't post notices in StatusNet.
2172                 common_log(LOG_WARNING,
2173                     "OStatus: skipping post with group listed ".
2174                     "as author: " . $oprofile->getUri() . " in feed from " . $this->getUri());
2175                 throw new ServerException('Activity author is a non-actor');
2176             }
2177         } else {
2178             $actor = $activity->actor;
2179
2180             if (!$actor instanceof Profile) {
2181                 // OK here! assume the default
2182             } else if ($actor->id == $this->getUri() || $actor->link == $this->getUri()) {
2183                 $this->updateFromActivityObject($actor);
2184             } else if ($actor->id) {
2185                 // We have an ActivityStreams actor with an explicit ID that doesn't match the feed owner.
2186                 // This isn't what we expect from mainline OStatus person feeds!
2187                 // Group feeds go down another path, with different validation...
2188                 // Most likely this is a plain ol' blog feed of some kind which
2189                 // doesn't match our expectations. We'll take the entry, but ignore
2190                 // the <author> info.
2191                 common_log(LOG_WARNING, "Got an actor '{$actor->title}' ({$actor->id}) on single-user feed for " . $this->getUri());
2192             } else {
2193                 // Plain <author> without ActivityStreams actor info.
2194                 // We'll just ignore this info for now and save the update under the feed's identity.
2195             }
2196
2197             $oprofile = $this;
2198         }
2199
2200         return $oprofile->localProfile();
2201     }
2202 }
2203
2204 /**
2205  * Exception indicating we've got a remote reference to a local user,
2206  * not a remote user!
2207  *
2208  * If we can ue a local profile after all, it's available as $e->profile.
2209  */
2210 class OStatusShadowException extends Exception
2211 {
2212     public $profile;
2213
2214     /**
2215      * @param Profile $profile
2216      * @param string $message
2217      */
2218     function __construct($profile, $message) {
2219         $this->profile = $profile;
2220         parent::__construct($message);
2221     }
2222 }