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