]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OStatus/classes/Ostatus_profile.php
90a8d0ef47ff8643a4461088065e7c7cb5ed4529
[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 /**
21  * @package OStatusPlugin
22  * @maintainer Brion Vibber <brion@status.net>
23  */
24
25 class Ostatus_profile extends Memcached_DataObject
26 {
27     public $__table = 'ostatus_profile';
28
29     public $uri;
30
31     public $profile_id;
32     public $group_id;
33
34     public $feeduri;
35     public $salmonuri;
36     public $avatar; // remote URL of the last avatar we saved
37
38     public $created;
39     public $modified;
40
41     public /*static*/ function staticGet($k, $v=null)
42     {
43         return parent::staticGet(__CLASS__, $k, $v);
44     }
45
46     /**
47      * return table definition for DB_DataObject
48      *
49      * DB_DataObject needs to know something about the table to manipulate
50      * instances. This method provides all the DB_DataObject needs to know.
51      *
52      * @return array array of column definitions
53      */
54
55     function table()
56     {
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);
65     }
66
67     static function schemaDef()
68     {
69         return array(new ColumnDef('uri', 'varchar',
70                                    255, false, 'PRI'),
71                      new ColumnDef('profile_id', 'integer',
72                                    null, true, 'UNI'),
73                      new ColumnDef('group_id', 'integer',
74                                    null, true, 'UNI'),
75                      new ColumnDef('feeduri', 'varchar',
76                                    255, true, 'UNI'),
77                      new ColumnDef('salmonuri', 'text',
78                                    null, true),
79                      new ColumnDef('avatar', 'text',
80                                    null, true),
81                      new ColumnDef('created', 'datetime',
82                                    null, false),
83                      new ColumnDef('modified', 'datetime',
84                                    null, false));
85     }
86
87     /**
88      * return key definitions for DB_DataObject
89      *
90      * DB_DataObject needs to know about keys that the table has; this function
91      * defines them.
92      *
93      * @return array key definitions
94      */
95
96     function keys()
97     {
98         return array_keys($this->keyTypes());
99     }
100
101     /**
102      * return key definitions for Memcached_DataObject
103      *
104      * Our caching system uses the same key definitions, but uses a different
105      * method to get them.
106      *
107      * @return array key definitions
108      */
109
110     function keyTypes()
111     {
112         return array('uri' => 'K', 'profile_id' => 'U', 'group_id' => 'U', 'feeduri' => 'U');
113     }
114
115     function sequenceKey()
116     {
117         return array(false, false, false);
118     }
119
120     /**
121      * Fetch the StatusNet-side profile for this feed
122      * @return Profile
123      */
124     public function localProfile()
125     {
126         if ($this->profile_id) {
127             return Profile::staticGet('id', $this->profile_id);
128         }
129         return null;
130     }
131
132     /**
133      * Fetch the StatusNet-side profile for this feed
134      * @return Profile
135      */
136     public function localGroup()
137     {
138         if ($this->group_id) {
139             return User_group::staticGet('id', $this->group_id);
140         }
141         return null;
142     }
143
144     /**
145      * Returns an ActivityObject describing this remote user or group profile.
146      * Can then be used to generate Atom chunks.
147      *
148      * @return ActivityObject
149      */
150     function asActivityObject()
151     {
152         if ($this->isGroup()) {
153             return ActivityObject::fromGroup($this->localGroup());
154         } else {
155             return ActivityObject::fromProfile($this->localProfile());
156         }
157     }
158
159     /**
160      * Returns an XML string fragment with profile information as an
161      * Activity Streams noun object with the given element type.
162      *
163      * Assumes that 'activity' namespace has been previously defined.
164      *
165      * @fixme replace with wrappers on asActivityObject when it's got everything.
166      *
167      * @param string $element one of 'actor', 'subject', 'object', 'target'
168      * @return string
169      */
170     function asActivityNoun($element)
171     {
172         if ($this->isGroup()) {
173             $noun = ActivityObject::fromGroup($this->localGroup());
174             return $noun->asString('activity:' . $element);
175         } else {
176             $noun = ActivityObject::fromProfile($this->localProfile());
177             return $noun->asString('activity:' . $element);
178         }
179     }
180
181     /**
182      * @return boolean true if this is a remote group
183      */
184     function isGroup()
185     {
186         if ($this->profile_id && !$this->group_id) {
187             return false;
188         } else if ($this->group_id && !$this->profile_id) {
189             return true;
190         } else if ($this->group_id && $this->profile_id) {
191             throw new ServerException("Invalid ostatus_profile state: both group and profile IDs set for $this->uri");
192         } else {
193             throw new ServerException("Invalid ostatus_profile state: both group and profile IDs empty for $this->uri");
194         }
195     }
196
197     /**
198      * Send a subscription request to the hub for this feed.
199      * The hub will later send us a confirmation POST to /main/push/callback.
200      *
201      * @return bool true on success, false on failure
202      * @throws ServerException if feed state is not valid
203      */
204     public function subscribe()
205     {
206         $feedsub = FeedSub::ensureFeed($this->feeduri);
207         if ($feedsub->sub_state == 'active' || $feedsub->sub_state == 'subscribe') {
208             return true;
209         } else if ($feedsub->sub_state == '' || $feedsub->sub_state == 'inactive') {
210             return $feedsub->subscribe();
211         } else if ('unsubscribe') {
212             throw new FeedSubException("Unsub is pending, can't subscribe...");
213         }
214     }
215
216     /**
217      * Send a PuSH unsubscription request to the hub for this feed.
218      * The hub will later send us a confirmation POST to /main/push/callback.
219      *
220      * @return bool true on success, false on failure
221      * @throws ServerException if feed state is not valid
222      */
223     public function unsubscribe() {
224         $feedsub = FeedSub::staticGet('uri', $this->feeduri);
225         if (!$feedsub) {
226             return true;
227         }
228         if ($feedsub->sub_state == 'active') {
229             return $feedsub->unsubscribe();
230         } else if ($feedsub->sub_state == '' || $feedsub->sub_state == 'inactive' || $feedsub->sub_state == 'unsubscribe') {
231             return true;
232         } else if ($feedsub->sub_state == 'subscribe') {
233             throw new FeedSubException("Feed is awaiting subscription, can't unsub...");
234         }
235     }
236
237     /**
238      * Check if this remote profile has any active local subscriptions, and
239      * if not drop the PuSH subscription feed.
240      *
241      * @return boolean
242      */
243     public function garbageCollect()
244     {
245         if ($this->isGroup()) {
246             $members = $this->localGroup()->getMembers(0, 1);
247             $count = $members->N;
248         } else {
249             $count = $this->localProfile()->subscriberCount();
250         }
251         if ($count == 0) {
252             common_log(LOG_INFO, "Unsubscribing from now-unused remote feed $this->feeduri");
253             $this->unsubscribe();
254             return true;
255         } else {
256             return false;
257         }
258     }
259
260     /**
261      * Send an Activity Streams notification to the remote Salmon endpoint,
262      * if so configured.
263      *
264      * @param Profile $actor  Actor who did the activity
265      * @param string  $verb   Activity::SUBSCRIBE or Activity::JOIN
266      * @param Object  $object object of the action; must define asActivityNoun($tag)
267      */
268     public function notify($actor, $verb, $object=null)
269     {
270         if (!($actor instanceof Profile)) {
271             $type = gettype($actor);
272             if ($type == 'object') {
273                 $type = get_class($actor);
274             }
275             throw new ServerException("Invalid actor passed to " . __METHOD__ . ": " . $type);
276         }
277         if ($object == null) {
278             $object = $this;
279         }
280         if ($this->salmonuri) {
281
282             $text = 'update';
283             $id = TagURI::mint('%s:%s:%s',
284                                $verb,
285                                $actor->getURI(),
286                                common_date_iso8601(time()));
287
288             // @fixme consolidate all these NS settings somewhere
289             $attributes = array('xmlns' => Activity::ATOM,
290                                 'xmlns:activity' => 'http://activitystrea.ms/spec/1.0/',
291                                 'xmlns:thr' => 'http://purl.org/syndication/thread/1.0',
292                                 'xmlns:georss' => 'http://www.georss.org/georss',
293                                 'xmlns:ostatus' => 'http://ostatus.org/schema/1.0',
294                                 'xmlns:poco' => 'http://portablecontacts.net/spec/1.0',
295                                 'xmlns:media' => 'http://purl.org/syndication/atommedia');
296
297             $entry = new XMLStringer();
298             $entry->elementStart('entry', $attributes);
299             $entry->element('id', null, $id);
300             $entry->element('title', null, $text);
301             $entry->element('summary', null, $text);
302             $entry->element('published', null, common_date_w3dtf(common_sql_now()));
303
304             $entry->element('activity:verb', null, $verb);
305             $entry->raw($actor->asAtomAuthor());
306             $entry->raw($actor->asActivityActor());
307             $entry->raw($object->asActivityNoun('object'));
308             $entry->elementEnd('entry');
309
310             $xml = $entry->getString();
311             common_log(LOG_INFO, "Posting to Salmon endpoint $this->salmonuri: $xml");
312
313             $salmon = new Salmon(); // ?
314             return $salmon->post($this->salmonuri, $xml, $actor);
315         }
316         return false;
317     }
318
319     /**
320      * Send a Salmon notification ping immediately, and confirm that we got
321      * an acceptable response from the remote site.
322      *
323      * @param mixed $entry XML string, Notice, or Activity
324      * @return boolean success
325      */
326     public function notifyActivity($entry, $actor)
327     {
328         if ($this->salmonuri) {
329             $salmon = new Salmon();
330             return $salmon->post($this->salmonuri, $this->notifyPrepXml($entry), $actor);
331         }
332
333         return false;
334     }
335
336     /**
337      * Queue a Salmon notification for later. If queues are disabled we'll
338      * send immediately but won't get the return value.
339      *
340      * @param mixed $entry XML string, Notice, or Activity
341      * @return boolean success
342      */
343     public function notifyDeferred($entry, $actor)
344     {
345         if ($this->salmonuri) {
346             $data = array('salmonuri' => $this->salmonuri,
347                           'entry' => $this->notifyPrepXml($entry),
348                           'actor' => $actor->id);
349
350             $qm = QueueManager::get();
351             return $qm->enqueue($data, 'salmon');
352         }
353
354         return false;
355     }
356
357     protected function notifyPrepXml($entry)
358     {
359         $preamble = '<?xml version="1.0" encoding="UTF-8" ?' . '>';
360         if (is_string($entry)) {
361             return $entry;
362         } else if ($entry instanceof Activity) {
363             return $preamble . $entry->asString(true);
364         } else if ($entry instanceof Notice) {
365             return $preamble . $entry->asAtomEntry(true, true);
366         } else {
367             throw new ServerException("Invalid type passed to Ostatus_profile::notify; must be XML string or Activity entry");
368         }
369     }
370
371     function getBestName()
372     {
373         if ($this->isGroup()) {
374             return $this->localGroup()->getBestName();
375         } else {
376             return $this->localProfile()->getBestName();
377         }
378     }
379
380     /**
381      * Read and post notices for updates from the feed.
382      * Currently assumes that all items in the feed are new,
383      * coming from a PuSH hub.
384      *
385      * @param DOMDocument $doc
386      * @param string $source identifier ("push")
387      */
388     public function processFeed(DOMDocument $doc, $source)
389     {
390         $feed = $doc->documentElement;
391
392         if ($feed->localName != 'feed' || $feed->namespaceURI != Activity::ATOM) {
393             common_log(LOG_ERR, __METHOD__ . ": not an Atom feed, ignoring");
394             return;
395         }
396
397         $entries = $feed->getElementsByTagNameNS(Activity::ATOM, 'entry');
398         if ($entries->length == 0) {
399             common_log(LOG_ERR, __METHOD__ . ": no entries in feed update, ignoring");
400             return;
401         }
402
403         for ($i = 0; $i < $entries->length; $i++) {
404             $entry = $entries->item($i);
405             $this->processEntry($entry, $feed, $source);
406         }
407     }
408
409     /**
410      * Process a posted entry from this feed source.
411      *
412      * @param DOMElement $entry
413      * @param DOMElement $feed for context
414      * @param string $source identifier ("push" or "salmon")
415      */
416     public function processEntry($entry, $feed, $source)
417     {
418         $activity = new Activity($entry, $feed);
419
420         if ($activity->verb == ActivityVerb::POST) {
421             $this->processPost($activity, $source);
422         } else {
423             common_log(LOG_INFO, "Ignoring activity with unrecognized verb $activity->verb");
424         }
425     }
426
427     /**
428      * Process an incoming post activity from this remote feed.
429      * @param Activity $activity
430      * @param string $method 'push' or 'salmon'
431      * @return mixed saved Notice or false
432      * @fixme break up this function, it's getting nasty long
433      */
434     public function processPost($activity, $method)
435     {
436         if ($this->isGroup()) {
437             // A group feed will contain posts from multiple authors.
438             // @fixme validate these profiles in some way!
439             $oprofile = self::ensureActorProfile($activity);
440             if ($oprofile->isGroup()) {
441                 // Groups can't post notices in StatusNet.
442                 common_log(LOG_WARNING, "OStatus: skipping post with group listed as author: $oprofile->uri in feed from $this->uri");
443                 return false;
444             }
445         } else {
446             // Individual user feeds may contain only posts from themselves.
447             // Authorship is validated against the profile URI on upper layers,
448             // through PuSH setup or Salmon signature checks.
449             $actorUri = self::getActorProfileURI($activity);
450             if ($actorUri == $this->uri) {
451                 // Check if profile info has changed and update it
452                 $this->updateFromActivityObject($activity->actor);
453             } else {
454                 common_log(LOG_WARNING, "OStatus: skipping post with bad author: got $actorUri expected $this->uri");
455                 return false;
456             }
457             $oprofile = $this;
458         }
459
460         // The id URI will be used as a unique identifier for for the notice,
461         // protecting against duplicate saves. It isn't required to be a URL;
462         // tag: URIs for instance are found in Google Buzz feeds.
463         $sourceUri = $activity->object->id;
464         $dupe = Notice::staticGet('uri', $sourceUri);
465         if ($dupe) {
466             common_log(LOG_INFO, "OStatus: ignoring duplicate post: $sourceUri");
467             return false;
468         }
469
470         // We'll also want to save a web link to the original notice, if provided.
471         $sourceUrl = null;
472         if ($activity->object->link) {
473             $sourceUrl = $activity->object->link;
474         } else if ($activity->link) {
475             $sourceUrl = $activity->link;
476         } else if (preg_match('!^https?://!', $activity->object->id)) {
477             $sourceUrl = $activity->object->id;
478         }
479
480         // Get (safe!) HTML and text versions of the content
481         $rendered = $this->purify($activity->object->content);
482         $content = html_entity_decode(strip_tags($rendered));
483
484         $shortened = common_shorten_links($content);
485
486         // If it's too long, try using the summary, and make the
487         // HTML an attachment.
488
489         $attachment = null;
490
491         if (Notice::contentTooLong($shortened)) {
492             $attachment = $this->saveHTMLFile($activity->object->title, $rendered);
493             $summary = $activity->object->summary;
494             if (empty($summary)) {
495                 $summary = $content;
496             }
497             $shortSummary = common_shorten_links($summary);
498             if (Notice::contentTooLong($shortSummary)) {
499                 $url = common_shorten_url(common_local_url('attachment',
500                                                            array('attachment' => $attachment->id)));
501                 $shortSummary = substr($shortSummary,
502                                        0,
503                                        Notice::maxContent() - (mb_strlen($url) + 2));
504                 $shortSummary .= '… ' . $url;
505                 $content = $shortSummary;
506                 $rendered = common_render_text($content);
507             }
508         }
509
510         $options = array('is_local' => Notice::REMOTE_OMB,
511                         'url' => $sourceUrl,
512                         'uri' => $sourceUri,
513                         'rendered' => $rendered,
514                         'replies' => array(),
515                         'groups' => array(),
516                         'tags' => array(),
517                         'urls' => array());
518
519         // Check for optional attributes...
520
521         if (!empty($activity->time)) {
522             $options['created'] = common_sql_date($activity->time);
523         }
524
525         if ($activity->context) {
526             // Any individual or group attn: targets?
527             $replies = $activity->context->attention;
528             $options['groups'] = $this->filterReplies($oprofile, $replies);
529             $options['replies'] = $replies;
530
531             // Maintain direct reply associations
532             // @fixme what about conversation ID?
533             if (!empty($activity->context->replyToID)) {
534                 $orig = Notice::staticGet('uri',
535                                           $activity->context->replyToID);
536                 if (!empty($orig)) {
537                     $options['reply_to'] = $orig->id;
538                 }
539             }
540
541             $location = $activity->context->location;
542             if ($location) {
543                 $options['lat'] = $location->lat;
544                 $options['lon'] = $location->lon;
545                 if ($location->location_id) {
546                     $options['location_ns'] = $location->location_ns;
547                     $options['location_id'] = $location->location_id;
548                 }
549             }
550         }
551
552         // Atom categories <-> hashtags
553         foreach ($activity->categories as $cat) {
554             if ($cat->term) {
555                 $term = common_canonical_tag($cat->term);
556                 if ($term) {
557                     $options['tags'][] = $term;
558                 }
559             }
560         }
561
562         // Atom enclosures -> attachment URLs
563         foreach ($activity->enclosures as $href) {
564             // @fixme save these locally or....?
565             $options['urls'][] = $href;
566         }
567
568         try {
569             $saved = Notice::saveNew($oprofile->profile_id,
570                                      $content,
571                                      'ostatus',
572                                      $options);
573             if ($saved) {
574                 Ostatus_source::saveNew($saved, $this, $method);
575                 if (!empty($attachment)) {
576                     File_to_post::processNew($attachment->id, $saved->id);
577                 }
578             }
579         } catch (Exception $e) {
580             common_log(LOG_ERR, "OStatus save of remote message $sourceUri failed: " . $e->getMessage());
581             throw $e;
582         }
583         common_log(LOG_INFO, "OStatus saved remote message $sourceUri as notice id $saved->id");
584         return $saved;
585     }
586
587     /**
588      * Clean up HTML
589      */
590     protected function purify($html)
591     {
592         require_once INSTALLDIR.'/extlib/htmLawed/htmLawed.php';
593         $config = array('safe' => 1,
594                         'deny_attribute' => 'id,style,on*');
595         return htmLawed($html, $config);
596     }
597
598     /**
599      * Filters a list of recipient ID URIs to just those for local delivery.
600      * @param Ostatus_profile local profile of sender
601      * @param array in/out &$attention_uris set of URIs, will be pruned on output
602      * @return array of group IDs
603      */
604     protected function filterReplies($sender, &$attention_uris)
605     {
606         common_log(LOG_DEBUG, "Original reply recipients: " . implode(', ', $attention_uris));
607         $groups = array();
608         $replies = array();
609         foreach ($attention_uris as $recipient) {
610             // Is the recipient a local user?
611             $user = User::staticGet('uri', $recipient);
612             if ($user) {
613                 // @fixme sender verification, spam etc?
614                 $replies[] = $recipient;
615                 continue;
616             }
617
618             // Is the recipient a remote group?
619             $oprofile = Ostatus_profile::staticGet('uri', $recipient);
620             if ($oprofile) {
621                 if ($oprofile->isGroup()) {
622                     // Deliver to local members of this remote group.
623                     // @fixme sender verification?
624                     $groups[] = $oprofile->group_id;
625                 } else {
626                     common_log(LOG_DEBUG, "Skipping reply to remote profile $recipient");
627                 }
628                 continue;
629             }
630
631             // Is the recipient a local group?
632             // @fixme uri on user_group isn't reliable yet
633             // $group = User_group::staticGet('uri', $recipient);
634             $id = OStatusPlugin::localGroupFromUrl($recipient);
635             if ($id) {
636                 $group = User_group::staticGet('id', $id);
637                 if ($group) {
638                     // Deliver to all members of this local group if allowed.
639                     $profile = $sender->localProfile();
640                     if ($profile->isMember($group)) {
641                         $groups[] = $group->id;
642                     } else {
643                         common_log(LOG_DEBUG, "Skipping reply to local group $group->nickname as sender $profile->id is not a member");
644                     }
645                     continue;
646                 } else {
647                     common_log(LOG_DEBUG, "Skipping reply to bogus group $recipient");
648                 }
649             }
650
651             common_log(LOG_DEBUG, "Skipping reply to unrecognized profile $recipient");
652
653         }
654         $attention_uris = $replies;
655         common_log(LOG_DEBUG, "Local reply recipients: " . implode(', ', $replies));
656         common_log(LOG_DEBUG, "Local group recipients: " . implode(', ', $groups));
657         return $groups;
658     }
659
660     /**
661      * @param string $profile_url
662      * @return Ostatus_profile
663      * @throws FeedSubException
664      */
665
666     public static function ensureProfileURL($profile_url, $hints=array())
667     {
668         $oprofile = self::getFromProfileURL($profile_url);
669
670         if (!empty($oprofile)) {
671             return $oprofile;
672         }
673
674         $hints['profileurl'] = $profile_url;
675
676         // Fetch the URL
677         // XXX: HTTP caching
678
679         $client = new HTTPClient();
680         $client->setHeader('Accept', 'text/html,application/xhtml+xml');
681         $response = $client->get($profile_url);
682
683         if (!$response->isOk()) {
684             return null;
685         }
686
687         // Check if we have a non-canonical URL
688
689         $finalUrl = $response->getUrl();
690
691         if ($finalUrl != $profile_url) {
692
693             $hints['profileurl'] = $finalUrl;
694
695             $oprofile = self::getFromProfileURL($finalUrl);
696
697             if (!empty($oprofile)) {
698                 return $oprofile;
699             }
700         }
701
702         // Try to get some hCard data
703
704         $body = $response->getBody();
705
706         $hcardHints = DiscoveryHints::hcardHints($body, $finalUrl);
707
708         if (!empty($hcardHints)) {
709             $hints = array_merge($hints, $hcardHints);
710         }
711
712         // Check if they've got an LRDD header
713
714         $lrdd = LinkHeader::getLink($response, 'lrdd', 'application/xrd+xml');
715
716         if (!empty($lrdd)) {
717
718             $xrd = Discovery::fetchXrd($lrdd);
719             $xrdHints = DiscoveryHints::fromXRD($xrd);
720
721             $hints = array_merge($hints, $xrdHints);
722         }
723
724         // If discovery found a feedurl (probably from LRDD), use it.
725
726         if (array_key_exists('feedurl', $hints)) {
727             return self::ensureFeedURL($hints['feedurl'], $hints);
728         }
729
730         // Get the feed URL from HTML
731
732         $discover = new FeedDiscovery();
733
734         $feedurl = $discover->discoverFromHTML($finalUrl, $body);
735
736         if (!empty($feedurl)) {
737             $hints['feedurl'] = $feedurl;
738
739             return self::ensureFeedURL($feedurl, $hints);
740         }
741     }
742
743     static function getFromProfileURL($profile_url)
744     {
745         $profile = Profile::staticGet('profileurl', $profile_url);
746
747         if (empty($profile)) {
748             return null;
749         }
750
751         // Is it a known Ostatus profile?
752
753         $oprofile = Ostatus_profile::staticGet('profile_id', $profile->id);
754
755         if (!empty($oprofile)) {
756             return $oprofile;
757         }
758
759         // Is it a local user?
760
761         $user = User::staticGet('id', $profile->id);
762
763         if (!empty($user)) {
764             throw new Exception("'$profile_url' is the profile for local user '{$user->nickname}'.");
765         }
766
767         // Continue discovery; it's a remote profile
768         // for OMB or some other protocol, may also
769         // support OStatus
770
771         return null;
772     }
773
774     public static function ensureFeedURL($feed_url, $hints=array())
775     {
776         $discover = new FeedDiscovery();
777
778         $feeduri = $discover->discoverFromFeedURL($feed_url);
779         $hints['feedurl'] = $feeduri;
780
781         $huburi = $discover->getAtomLink('hub');
782         $hints['hub'] = $huburi;
783         $salmonuri = $discover->getAtomLink(Salmon::NS_REPLIES);
784         $hints['salmon'] = $salmonuri;
785
786         if (!$huburi) {
787             // We can only deal with folks with a PuSH hub
788             throw new FeedSubNoHubException();
789         }
790
791         // Try to get a profile from the feed activity:subject
792
793         $feedEl = $discover->feed->documentElement;
794
795         $subject = ActivityUtils::child($feedEl, Activity::SUBJECT, Activity::SPEC);
796
797         if (!empty($subject)) {
798             $subjObject = new ActivityObject($subject);
799             return self::ensureActivityObjectProfile($subjObject, $hints);
800         }
801
802         // Otherwise, try the feed author
803
804         $author = ActivityUtils::child($feedEl, Activity::AUTHOR, Activity::ATOM);
805
806         if (!empty($author)) {
807             $authorObject = new ActivityObject($author);
808             return self::ensureActivityObjectProfile($authorObject, $hints);
809         }
810
811         // Sheesh. Not a very nice feed! Let's try fingerpoken in the
812         // entries.
813
814         $entries = $discover->feed->getElementsByTagNameNS(Activity::ATOM, 'entry');
815
816         if (!empty($entries) && $entries->length > 0) {
817
818             $entry = $entries->item(0);
819
820             $actor = ActivityUtils::child($entry, Activity::ACTOR, Activity::SPEC);
821
822             if (!empty($actor)) {
823                 $actorObject = new ActivityObject($actor);
824                 return self::ensureActivityObjectProfile($actorObject, $hints);
825
826             }
827
828             $author = ActivityUtils::child($entry, Activity::AUTHOR, Activity::ATOM);
829
830             if (!empty($author)) {
831                 $authorObject = new ActivityObject($author);
832                 return self::ensureActivityObjectProfile($authorObject, $hints);
833             }
834         }
835
836         // XXX: make some educated guesses here
837
838         throw new FeedSubException("Can't find enough profile information to make a feed.");
839     }
840
841     /**
842      *
843      * Download and update given avatar image
844      * @param string $url
845      * @throws Exception in various failure cases
846      */
847     protected function updateAvatar($url)
848     {
849         if ($url == $this->avatar) {
850             // We've already got this one.
851             return;
852         }
853
854         if ($this->isGroup()) {
855             $self = $this->localGroup();
856         } else {
857             $self = $this->localProfile();
858         }
859         if (!$self) {
860             throw new ServerException(sprintf(
861                 _m("Tried to update avatar for unsaved remote profile %s"),
862                 $this->uri));
863         }
864
865         // @fixme this should be better encapsulated
866         // ripped from oauthstore.php (for old OMB client)
867         $temp_filename = tempnam(sys_get_temp_dir(), 'listener_avatar');
868         if (!copy($url, $temp_filename)) {
869             throw new ServerException(sprintf(_m("Unable to fetch avatar from %s"), $url));
870         }
871
872         if ($this->isGroup()) {
873             $id = $this->group_id;
874         } else {
875             $id = $this->profile_id;
876         }
877         // @fixme should we be using different ids?
878         $imagefile = new ImageFile($id, $temp_filename);
879         $filename = Avatar::filename($id,
880                                      image_type_to_extension($imagefile->type),
881                                      null,
882                                      common_timestamp());
883         rename($temp_filename, Avatar::path($filename));
884         $self->setOriginal($filename);
885
886         $orig = clone($this);
887         $this->avatar = $url;
888         $this->update($orig);
889     }
890
891     /**
892      * Pull avatar URL from ActivityObject or profile hints
893      *
894      * @param ActivityObject $object
895      * @param array $hints
896      * @return mixed URL string or false
897      */
898
899     protected static function getActivityObjectAvatar($object, $hints=array())
900     {
901         if ($object->avatarLinks) {
902             $best = false;
903             // Take the exact-size avatar, or the largest avatar, or the first avatar if all sizeless
904             foreach ($object->avatarLinks as $avatar) {
905                 if ($avatar->width == AVATAR_PROFILE_SIZE && $avatar->height = AVATAR_PROFILE_SIZE) {
906                     // Exact match!
907                     $best = $avatar;
908                     break;
909                 }
910                 if (!$best || $avatar->width > $best->width) {
911                     $best = $avatar;
912                 }
913             }
914             return $best->url;
915         } else if (array_key_exists('avatar', $hints)) {
916             return $hints['avatar'];
917         }
918         return false;
919     }
920
921     /**
922      * Get an appropriate avatar image source URL, if available.
923      *
924      * @param ActivityObject $actor
925      * @param DOMElement $feed
926      * @return string
927      */
928
929     protected static function getAvatar($actor, $feed)
930     {
931         $url = '';
932         $icon = '';
933         if ($actor->avatar) {
934             $url = trim($actor->avatar);
935         }
936         if (!$url) {
937             // Check <atom:logo> and <atom:icon> on the feed
938             $els = $feed->childNodes();
939             if ($els && $els->length) {
940                 for ($i = 0; $i < $els->length; $i++) {
941                     $el = $els->item($i);
942                     if ($el->namespaceURI == Activity::ATOM) {
943                         if (empty($url) && $el->localName == 'logo') {
944                             $url = trim($el->textContent);
945                             break;
946                         }
947                         if (empty($icon) && $el->localName == 'icon') {
948                             // Use as a fallback
949                             $icon = trim($el->textContent);
950                         }
951                     }
952                 }
953             }
954             if ($icon && !$url) {
955                 $url = $icon;
956             }
957         }
958         if ($url) {
959             $opts = array('allowed_schemes' => array('http', 'https'));
960             if (Validate::uri($url, $opts)) {
961                 return $url;
962             }
963         }
964         return common_path('plugins/OStatus/images/96px-Feed-icon.svg.png');
965     }
966
967     /**
968      * Fetch, or build if necessary, an Ostatus_profile for the actor
969      * in a given Activity Streams activity.
970      *
971      * @param Activity $activity
972      * @param string $feeduri if we already know the canonical feed URI!
973      * @param string $salmonuri if we already know the salmon return channel URI
974      * @return Ostatus_profile
975      */
976
977     public static function ensureActorProfile($activity, $hints=array())
978     {
979         return self::ensureActivityObjectProfile($activity->actor, $hints);
980     }
981
982     public static function ensureActivityObjectProfile($object, $hints=array())
983     {
984         $profile = self::getActivityObjectProfile($object);
985         if ($profile) {
986             $profile->updateFromActivityObject($object, $hints);
987         } else {
988             $profile = self::createActivityObjectProfile($object, $hints);
989         }
990         return $profile;
991     }
992
993     /**
994      * @param Activity $activity
995      * @return mixed matching Ostatus_profile or false if none known
996      */
997     public static function getActorProfile($activity)
998     {
999         return self::getActivityObjectProfile($activity->actor);
1000     }
1001
1002     protected static function getActivityObjectProfile($object)
1003     {
1004         $uri = self::getActivityObjectProfileURI($object);
1005         return Ostatus_profile::staticGet('uri', $uri);
1006     }
1007
1008     protected static function getActorProfileURI($activity)
1009     {
1010         return self::getActivityObjectProfileURI($activity->actor);
1011     }
1012
1013     /**
1014      * @param Activity $activity
1015      * @return string
1016      * @throws ServerException
1017      */
1018     protected static function getActivityObjectProfileURI($object)
1019     {
1020         $opts = array('allowed_schemes' => array('http', 'https'));
1021         if ($object->id && Validate::uri($object->id, $opts)) {
1022             return $object->id;
1023         }
1024         if ($object->link && Validate::uri($object->link, $opts)) {
1025             return $object->link;
1026         }
1027         throw new ServerException("No author ID URI found");
1028     }
1029
1030     /**
1031      * @fixme validate stuff somewhere
1032      */
1033
1034     /**
1035      * Create local ostatus_profile and profile/user_group entries for
1036      * the provided remote user or group.
1037      *
1038      * @param ActivityObject $object
1039      * @param array $hints
1040      *
1041      * @return Ostatus_profile
1042      */
1043     protected static function createActivityObjectProfile($object, $hints=array())
1044     {
1045         $homeuri = $object->id;
1046         $discover = false;
1047
1048         if (!$homeuri) {
1049             common_log(LOG_DEBUG, __METHOD__ . " empty actor profile URI: " . var_export($activity, true));
1050             throw new Exception("No profile URI");
1051         }
1052
1053         if (OStatusPlugin::localProfileFromUrl($homeuri)) {
1054             throw new Exception("Local user can't be referenced as remote.");
1055         }
1056
1057         if (OStatusPlugin::localGroupFromUrl($homeuri)) {
1058             throw new Exception("Local group can't be referenced as remote.");
1059         }
1060
1061         if (array_key_exists('feedurl', $hints)) {
1062             $feeduri = $hints['feedurl'];
1063         } else {
1064             $discover = new FeedDiscovery();
1065             $feeduri = $discover->discoverFromURL($homeuri);
1066         }
1067
1068         if (array_key_exists('salmon', $hints)) {
1069             $salmonuri = $hints['salmon'];
1070         } else {
1071             if (!$discover) {
1072                 $discover = new FeedDiscovery();
1073                 $discover->discoverFromFeedURL($hints['feedurl']);
1074             }
1075             $salmonuri = $discover->getAtomLink(Salmon::NS_REPLIES);
1076         }
1077
1078         if (array_key_exists('hub', $hints)) {
1079             $huburi = $hints['hub'];
1080         } else {
1081             if (!$discover) {
1082                 $discover = new FeedDiscovery();
1083                 $discover->discoverFromFeedURL($hints['feedurl']);
1084             }
1085             $huburi = $discover->getAtomLink('hub');
1086         }
1087
1088         if (!$huburi) {
1089             // We can only deal with folks with a PuSH hub
1090             throw new FeedSubNoHubException();
1091         }
1092
1093         $oprofile = new Ostatus_profile();
1094
1095         $oprofile->uri        = $homeuri;
1096         $oprofile->feeduri    = $feeduri;
1097         $oprofile->salmonuri  = $salmonuri;
1098
1099         $oprofile->created    = common_sql_now();
1100         $oprofile->modified   = common_sql_now();
1101
1102         if ($object->type == ActivityObject::PERSON) {
1103             $profile = new Profile();
1104             $profile->created = common_sql_now();
1105             self::updateProfile($profile, $object, $hints);
1106
1107             $oprofile->profile_id = $profile->insert();
1108             if (!$oprofile->profile_id) {
1109                 throw new ServerException("Can't save local profile");
1110             }
1111         } else {
1112             $group = new User_group();
1113             $group->uri = $homeuri;
1114             $group->created = common_sql_now();
1115             self::updateGroup($group, $object, $hints);
1116
1117             $oprofile->group_id = $group->insert();
1118             if (!$oprofile->group_id) {
1119                 throw new ServerException("Can't save local profile");
1120             }
1121         }
1122
1123         $ok = $oprofile->insert();
1124
1125         if ($ok) {
1126             $avatar = self::getActivityObjectAvatar($object, $hints);
1127             if ($avatar) {
1128                 $oprofile->updateAvatar($avatar);
1129             }
1130             return $oprofile;
1131         } else {
1132             throw new ServerException("Can't save OStatus profile");
1133         }
1134     }
1135
1136     /**
1137      * Save any updated profile information to our local copy.
1138      * @param ActivityObject $object
1139      * @param array $hints
1140      */
1141     public function updateFromActivityObject($object, $hints=array())
1142     {
1143         if ($this->isGroup()) {
1144             $group = $this->localGroup();
1145             self::updateGroup($group, $object, $hints);
1146         } else {
1147             $profile = $this->localProfile();
1148             self::updateProfile($profile, $object, $hints);
1149         }
1150         $avatar = self::getActivityObjectAvatar($object, $hints);
1151         if ($avatar) {
1152             $this->updateAvatar($avatar);
1153         }
1154     }
1155
1156     protected static function updateProfile($profile, $object, $hints=array())
1157     {
1158         $orig = clone($profile);
1159
1160         $profile->nickname = self::getActivityObjectNickname($object, $hints);
1161
1162         if (!empty($object->title)) {
1163             $profile->fullname = $object->title;
1164         } else if (array_key_exists('fullname', $hints)) {
1165             $profile->fullname = $hints['fullname'];
1166         }
1167
1168         if (!empty($object->link)) {
1169             $profile->profileurl = $object->link;
1170         } else if (array_key_exists('profileurl', $hints)) {
1171             $profile->profileurl = $hints['profileurl'];
1172         } else if (Validate::uri($object->id, array('allowed_schemes' => array('http', 'https')))) {
1173             $profile->profileurl = $object->id;
1174         }
1175
1176         $profile->bio      = self::getActivityObjectBio($object, $hints);
1177         $profile->location = self::getActivityObjectLocation($object, $hints);
1178         $profile->homepage = self::getActivityObjectHomepage($object, $hints);
1179
1180         if (!empty($object->geopoint)) {
1181             $location = ActivityContext::locationFromPoint($object->geopoint);
1182             if (!empty($location)) {
1183                 $profile->lat = $location->lat;
1184                 $profile->lon = $location->lon;
1185             }
1186         }
1187
1188         // @fixme tags/categories
1189         // @todo tags from categories
1190
1191         if ($profile->id) {
1192             common_log(LOG_DEBUG, "Updating OStatus profile $profile->id from remote info $object->id: " . var_export($object, true) . var_export($hints, true));
1193             $profile->update($orig);
1194         }
1195     }
1196
1197     protected static function updateGroup($group, $object, $hints=array())
1198     {
1199         $orig = clone($group);
1200
1201         $group->nickname = self::getActivityObjectNickname($object, $hints);
1202         $group->fullname = $object->title;
1203
1204         if (!empty($object->link)) {
1205             $group->mainpage = $object->link;
1206         } else if (array_key_exists('profileurl', $hints)) {
1207             $group->mainpage = $hints['profileurl'];
1208         }
1209
1210         // @todo tags from categories
1211         $group->description = self::getActivityObjectBio($object, $hints);
1212         $group->location = self::getActivityObjectLocation($object, $hints);
1213         $group->homepage = self::getActivityObjectHomepage($object, $hints);
1214
1215         if ($group->id) {
1216             common_log(LOG_DEBUG, "Updating OStatus group $group->id from remote info $object->id: " . var_export($object, true) . var_export($hints, true));
1217             $group->update($orig);
1218         }
1219     }
1220
1221     protected static function getActivityObjectHomepage($object, $hints=array())
1222     {
1223         $homepage = null;
1224         $poco     = $object->poco;
1225
1226         if (!empty($poco)) {
1227             $url = $poco->getPrimaryURL();
1228             if ($url && $url->type == 'homepage') {
1229                 $homepage = $url->value;
1230             }
1231         }
1232
1233         // @todo Try for a another PoCo URL?
1234
1235         return $homepage;
1236     }
1237
1238     protected static function getActivityObjectLocation($object, $hints=array())
1239     {
1240         $location = null;
1241
1242         if (!empty($object->poco) &&
1243             isset($object->poco->address->formatted)) {
1244             $location = $object->poco->address->formatted;
1245         } else if (array_key_exists('location', $hints)) {
1246             $location = $hints['location'];
1247         }
1248
1249         if (!empty($location)) {
1250             if (mb_strlen($location) > 255) {
1251                 $location = mb_substr($note, 0, 255 - 3) . ' â€¦ ';
1252             }
1253         }
1254
1255         // @todo Try to find location some othe way? Via goerss point?
1256
1257         return $location;
1258     }
1259
1260     protected static function getActivityObjectBio($object, $hints=array())
1261     {
1262         $bio  = null;
1263
1264         if (!empty($object->poco)) {
1265             $note = $object->poco->note;
1266         } else if (array_key_exists('bio', $hints)) {
1267             $note = $hints['bio'];
1268         }
1269
1270         if (!empty($note)) {
1271             if (Profile::bioTooLong($note)) {
1272                 // XXX: truncate ok?
1273                 $bio = mb_substr($note, 0, Profile::maxBio() - 3) . ' â€¦ ';
1274             } else {
1275                 $bio = $note;
1276             }
1277         }
1278
1279         // @todo Try to get bio info some other way?
1280
1281         return $bio;
1282     }
1283
1284     protected static function getActivityObjectNickname($object, $hints=array())
1285     {
1286         if ($object->poco) {
1287             if (!empty($object->poco->preferredUsername)) {
1288                 return common_nicknamize($object->poco->preferredUsername);
1289             }
1290         }
1291
1292         if (!empty($object->nickname)) {
1293             return common_nicknamize($object->nickname);
1294         }
1295
1296         if (array_key_exists('nickname', $hints)) {
1297             return $hints['nickname'];
1298         }
1299
1300         // Try the definitive ID
1301
1302         $nickname = self::nicknameFromURI($object->id);
1303
1304         // Try a Webfinger if one was passed (way) down
1305
1306         if (empty($nickname)) {
1307             if (array_key_exists('webfinger', $hints)) {
1308                 $nickname = self::nicknameFromURI($hints['webfinger']);
1309             }
1310         }
1311
1312         // Try the name
1313
1314         if (empty($nickname)) {
1315             $nickname = common_nicknamize($object->title);
1316         }
1317
1318         return $nickname;
1319     }
1320
1321     protected static function nicknameFromURI($uri)
1322     {
1323         preg_match('/(\w+):/', $uri, $matches);
1324
1325         $protocol = $matches[1];
1326
1327         switch ($protocol) {
1328         case 'acct':
1329         case 'mailto':
1330             if (preg_match("/^$protocol:(.*)?@.*\$/", $uri, $matches)) {
1331                 return common_canonical_nickname($matches[1]);
1332             }
1333             return null;
1334         case 'http':
1335             return common_url_to_nickname($uri);
1336             break;
1337         default:
1338             return null;
1339         }
1340     }
1341
1342     /**
1343      * @param string $addr webfinger address
1344      * @return Ostatus_profile
1345      * @throws Exception on error conditions
1346      */
1347     public static function ensureWebfinger($addr)
1348     {
1349         // First, try the cache
1350
1351         $uri = self::cacheGet(sprintf('ostatus_profile:webfinger:%s', $addr));
1352
1353         if ($uri !== false) {
1354             if (is_null($uri)) {
1355                 // Negative cache entry
1356                 throw new Exception('Not a valid webfinger address.');
1357             }
1358             $oprofile = Ostatus_profile::staticGet('uri', $uri);
1359             if (!empty($oprofile)) {
1360                 return $oprofile;
1361             }
1362         }
1363
1364         // Try looking it up
1365
1366         $oprofile = Ostatus_profile::staticGet('uri', 'acct:'.$addr);
1367
1368         if (!empty($oprofile)) {
1369             self::cacheSet(sprintf('ostatus_profile:webfinger:%s', $addr), $oprofile->uri);
1370             return $oprofile;
1371         }
1372
1373         // Now, try some discovery
1374
1375         $disco = new Discovery();
1376
1377         try {
1378             $xrd = $disco->lookup($addr);
1379         } catch (Exception $e) {
1380             // Save negative cache entry so we don't waste time looking it up again.
1381             // @fixme distinguish temporary failures?
1382             self::cacheSet(sprintf('ostatus_profile:webfinger:%s', $addr), null);
1383             throw new Exception('Not a valid webfinger address.');
1384         }
1385
1386         $hints = array('webfinger' => $addr);
1387
1388         $dhints = DiscoveryHints::fromXRD($xrd);
1389
1390         $hints = array_merge($hints, $dhints);
1391
1392         // If there's an Hcard, let's grab its info
1393
1394         if (array_key_exists('hcard', $hints)) {
1395             if (!array_key_exists('profileurl', $hints) ||
1396                 $hints['hcard'] != $hints['profileurl']) {
1397                 $hcardHints = DiscoveryHints::fromHcardUrl($hints['hcard']);
1398                 $hints = array_merge($hcardHints, $hints);
1399             }
1400         }
1401
1402         // If we got a feed URL, try that
1403
1404         if (array_key_exists('feedurl', $hints)) {
1405             try {
1406                 common_log(LOG_INFO, "Discovery on acct:$addr with feed URL $feedUrl");
1407                 $oprofile = self::ensureFeedURL($hints['feedurl'], $hints);
1408                 self::cacheSet(sprintf('ostatus_profile:webfinger:%s', $addr), $oprofile->uri);
1409                 return $oprofile;
1410             } catch (Exception $e) {
1411                 common_log(LOG_WARNING, "Failed creating profile from feed URL '$feedUrl': " . $e->getMessage());
1412                 // keep looking
1413             }
1414         }
1415
1416         // If we got a profile page, try that!
1417
1418         if (array_key_exists('profileurl', $hints)) {
1419             try {
1420                 common_log(LOG_INFO, "Discovery on acct:$addr with profile URL $profileUrl");
1421                 $oprofile = self::ensureProfileURL($hints['profileurl'], $hints);
1422                 self::cacheSet(sprintf('ostatus_profile:webfinger:%s', $addr), $oprofile->uri);
1423                 return $oprofile;
1424             } catch (Exception $e) {
1425                 common_log(LOG_WARNING, "Failed creating profile from profile URL '$profileUrl': " . $e->getMessage());
1426                 // keep looking
1427             }
1428         }
1429
1430         // XXX: try hcard
1431         // XXX: try FOAF
1432
1433         if (array_key_exists('salmon', $hints)) {
1434
1435             $salmonEndpoint = $hints['salmon'];
1436
1437             // An account URL, a salmon endpoint, and a dream? Not much to go
1438             // on, but let's give it a try
1439
1440             $uri = 'acct:'.$addr;
1441
1442             $profile = new Profile();
1443
1444             $profile->nickname = self::nicknameFromUri($uri);
1445             $profile->created  = common_sql_now();
1446
1447             if (isset($profileUrl)) {
1448                 $profile->profileurl = $profileUrl;
1449             }
1450
1451             $profile_id = $profile->insert();
1452
1453             if (!$profile_id) {
1454                 common_log_db_error($profile, 'INSERT', __FILE__);
1455                 throw new Exception("Couldn't save profile for '$addr'");
1456             }
1457
1458             $oprofile = new Ostatus_profile();
1459
1460             $oprofile->uri        = $uri;
1461             $oprofile->salmonuri  = $salmonEndpoint;
1462             $oprofile->profile_id = $profile_id;
1463             $oprofile->created    = common_sql_now();
1464
1465             if (isset($feedUrl)) {
1466                 $profile->feeduri = $feedUrl;
1467             }
1468
1469             $result = $oprofile->insert();
1470
1471             if (!$result) {
1472                 common_log_db_error($oprofile, 'INSERT', __FILE__);
1473                 throw new Exception("Couldn't save ostatus_profile for '$addr'");
1474             }
1475
1476             self::cacheSet(sprintf('ostatus_profile:webfinger:%s', $addr), $oprofile->uri);
1477             return $oprofile;
1478         }
1479
1480         throw new Exception("Couldn't find a valid profile for '$addr'");
1481     }
1482
1483     function saveHTMLFile($title, $rendered)
1484     {
1485         $final = sprintf("<!DOCTYPE html>\n<html><head><title>%s</title></head>".
1486                          '<body><div>%s</div></body></html>',
1487                          htmlspecialchars($title),
1488                          $rendered);
1489
1490         $filename = File::filename($this->localProfile(),
1491                                    'ostatus', // ignored?
1492                                    'text/html');
1493
1494         $filepath = File::path($filename);
1495
1496         file_put_contents($filepath, $final);
1497
1498         $file = new File;
1499
1500         $file->filename = $filename;
1501         $file->url      = File::url($filename);
1502         $file->size     = filesize($filepath);
1503         $file->date     = time();
1504         $file->mimetype = 'text/html';
1505
1506         $file_id = $file->insert();
1507
1508         if ($file_id === false) {
1509             common_log_db_error($file, "INSERT", __FILE__);
1510             throw new ServerException(_('Could not store HTML content of long post as file.'));
1511         }
1512
1513         return $file;
1514     }
1515 }