GitHost.io will be shut down on June 1, 2019. At that point this instance will be unreachable and all data will be irrevocably deleted. More details at https://about.gitlab.com/gitlab-hosted/#githost-is-shutting-down-on-june-1st-2019

Ostatus_profile.php 66.9 KB
Newer Older
1 2
<?php
/*
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 * StatusNet - the distributed open-source microblogging tool
 * Copyright (C) 2009-2010, StatusNet, Inc.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

20
if (!defined('GNUSOCIAL')) { exit(1); }
21

22
/**
23
 * @package OStatusPlugin
24 25
 * @author Brion Vibber <brion@status.net>
 * @maintainer Mikael Nordfeldth <mmn@hethane.se>
26
 */
27
class Ostatus_profile extends Managed_DataObject
28
{
29
    public $__table = 'ostatus_profile';
30

31 32
    public $uri;

33
    public $profile_id;
34
    public $group_id;
35
    public $peopletag_id;
36 37

    public $feeduri;
38
    public $salmonuri;
39
    public $avatar; // remote URL of the last avatar we saved
40

41
    public $created;
42
    public $modified;
43

44
    /**
45
     * Return table definition for Schema setup and DB_DataObject usage.
46 47 48 49 50
     *
     * @return array array of column definitions
     */
    static function schemaDef()
    {
51 52
        return array(
            'fields' => array(
53
                'uri' => array('type' => 'varchar', 'length' => 191, 'not null' => true),
54 55
                'profile_id' => array('type' => 'integer'),
                'group_id' => array('type' => 'integer'),
56
                'peopletag_id' => array('type' => 'integer'),
57 58
                'feeduri' => array('type' => 'varchar', 'length' => 191),
                'salmonuri' => array('type' => 'varchar', 'length' => 191),
59 60 61 62 63 64
                'avatar' => array('type' => 'text'),
                'created' => array('type' => 'datetime', 'not null' => true),
                'modified' => array('type' => 'datetime', 'not null' => true),
            ),
            'primary key' => array('uri'),
            'unique keys' => array(
65 66 67 68
                'ostatus_profile_profile_id_key' => array('profile_id'),
                'ostatus_profile_group_id_key' => array('group_id'),
                'ostatus_profile_peopletag_id_key' => array('peopletag_id'),
                'ostatus_profile_feeduri_key' => array('feeduri'),
69 70
            ),
            'foreign keys' => array(
71 72
                'ostatus_profile_profile_id_fkey' => array('profile', array('profile_id' => 'id')),
                'ostatus_profile_group_id_fkey' => array('user_group', array('group_id' => 'id')),
73
                'ostatus_profile_peopletag_id_fkey' => array('profile_list', array('peopletag_id' => 'id')),
74 75
            ),
        );
76 77
    }

78 79 80 81 82
    public function getUri()
    {
        return $this->uri;
    }

mmn's avatar
mmn committed
83 84 85 86 87 88 89 90
    public function fromProfile(Profile $profile)
    {
        $oprofile = Ostatus_profile::getKV('profile_id', $profile->id);
        if (!$oprofile instanceof Ostatus_profile) {
            throw new Exception('No Ostatus_profile for Profile ID: '.$profile->id);
        }
    }

91
    /**
92
     * Fetch the locally stored profile for this feed
93
     * @return Profile
94
     * @throws NoProfileException if it was not found
95
     */
96
    public function localProfile()
97
    {
98 99 100 101
        if ($this->isGroup()) {
            return $this->localGroup()->getProfile();
        }

102
        $profile = Profile::getKV('id', $this->profile_id);
103 104
        if (!$profile instanceof Profile) {
            throw new NoProfileException($this->profile_id);
105
        }
106
        return $profile;
107 108 109 110 111 112 113 114
    }

    /**
     * Fetch the StatusNet-side profile for this feed
     * @return Profile
     */
    public function localGroup()
    {
115 116 117 118
        $group = User_group::getKV('id', $this->group_id);

        if (!$group instanceof User_group) {
            throw new NoSuchGroupException(array('id'=>$this->group_id));
119
        }
120 121

        return $group;
122 123
    }

124 125 126 127 128 129 130
    /**
     * Fetch the StatusNet-side peopletag for this feed
     * @return Profile
     */
    public function localPeopletag()
    {
        if ($this->peopletag_id) {
131
            return Profile_list::getKV('id', $this->peopletag_id);
132 133 134 135
        }
        return null;
    }

136 137 138 139 140 141 142 143 144
    /**
     * Returns an ActivityObject describing this remote user or group profile.
     * Can then be used to generate Atom chunks.
     *
     * @return ActivityObject
     */
    function asActivityObject()
    {
        if ($this->isGroup()) {
145
            return ActivityObject::fromGroup($this->localGroup());
146 147
        } else if ($this->isPeopletag()) {
            return ActivityObject::fromPeopletag($this->localPeopletag());
148
        } else {
149
            return $this->localProfile()->asActivityObject();
150 151 152
        }
    }

153 154 155 156 157 158
    /**
     * Returns an XML string fragment with profile information as an
     * Activity Streams noun object with the given element type.
     *
     * Assumes that 'activity' namespace has been previously defined.
     *
159
     * @todo FIXME: Replace with wrappers on asActivityObject when it's got everything.
160
     *
161 162 163 164 165 166
     * @param string $element one of 'actor', 'subject', 'object', 'target'
     * @return string
     */
    function asActivityNoun($element)
    {
        if ($this->isGroup()) {
167 168
            $noun = ActivityObject::fromGroup($this->localGroup());
            return $noun->asString('activity:' . $element);
169 170 171
        } else if ($this->isPeopletag()) {
            $noun = ActivityObject::fromPeopletag($this->localPeopletag());
            return $noun->asString('activity:' . $element);
172
        } else {
173
            $noun = $this->localProfile()->asActivityObject();
174
            return $noun->asString('activity:' . $element);
175 176 177
        }
    }

178
    /**
179
     * @return boolean true if this is a remote group
180 181 182
     */
    function isGroup()
    {
183
        if ($this->profile_id || $this->peopletag_id && !$this->group_id) {
184
            return false;
185
        } else if ($this->group_id && !$this->profile_id && !$this->peopletag_id) {
186
            return true;
187 188
        } else if ($this->group_id && ($this->profile_id || $this->peopletag_id)) {
            // TRANS: Server exception. %s is a URI
189
            throw new ServerException(sprintf(_m('Invalid ostatus_profile state: Two or more IDs set for %s.'), $this->getUri()));
190
        } else {
191
            // TRANS: Server exception. %s is a URI
192
            throw new ServerException(sprintf(_m('Invalid ostatus_profile state: All IDs empty for %s.'), $this->getUri()));
193 194 195
        }
    }

196 197 198 199 200
    public function isPerson()
    {
        return $this->localProfile()->isPerson();
    }

201 202 203 204 205 206 207 208 209 210 211
    /**
     * @return boolean true if this is a remote peopletag
     */
    function isPeopletag()
    {
        if ($this->profile_id || $this->group_id && !$this->peopletag_id) {
            return false;
        } else if ($this->peopletag_id && !$this->profile_id && !$this->group_id) {
            return true;
        } else if ($this->peopletag_id && ($this->profile_id || $this->group_id)) {
            // TRANS: Server exception. %s is a URI
212
            throw new ServerException(sprintf(_m('Invalid ostatus_profile state: Two or more IDs set for %s.'), $this->getUri()));
213 214
        } else {
            // TRANS: Server exception. %s is a URI
215
            throw new ServerException(sprintf(_m('Invalid ostatus_profile state: All IDs empty for %s.'), $this->getUri()));
216
        }
217 218
    }

219
    /**
220 221
     * Send a subscription request to the hub for this feed.
     * The hub will later send us a confirmation POST to /main/push/callback.
222
     *
223 224
     * @return void
     * @throws ServerException if feed state is not valid or subscription fails.
225
     */
226
    public function subscribe()
227
    {
228
        $feedsub = FeedSub::ensureFeed($this->feeduri);
229 230
        if ($feedsub->sub_state == 'active') {
            // Active subscription, we don't need to do anything.
231
            return;
232
        }
233 234 235 236

        // Inactive or we got left in an inconsistent state.
        // Run a subscription request to make sure we're current!
        return $feedsub->subscribe();
237 238 239
    }

    /**
240 241
     * Check if this remote profile has any active local subscriptions, and
     * if not drop the PuSH subscription feed.
242
     *
243 244
     * @return boolean true if subscription is removed, false if there are still subscribers to the feed
     * @throws Exception of various kinds on failure.
245
     */
246
    public function unsubscribe() {
247
        return $this->garbageCollect();
248 249
    }

250 251 252 253
    /**
     * Check if this remote profile has any active local subscriptions, and
     * if not drop the PuSH subscription feed.
     *
254 255
     * @return boolean true if subscription is removed, false if there are still subscribers to the feed
     * @throws Exception of various kinds on failure.
256 257
     */
    public function garbageCollect()
258
    {
259
        $feedsub = FeedSub::getKV('uri', $this->feeduri);
260 261 262 263 264
        if ($feedsub instanceof FeedSub) {
            return $feedsub->garbageCollect();
        }
        // Since there's no FeedSub we can assume it's already garbage collected
        return true;
265 266 267 268 269 270 271 272 273 274
    }

    /**
     * Check if this remote profile has any active local subscriptions, so the
     * PuSH subscription layer can decide if it can drop the feed.
     *
     * This gets called via the FeedSubSubscriberCount event when running
     * FeedSub::garbageCollect().
     *
     * @return int
275
     * @throws NoProfileException if there is no local profile for the object
276 277
     */
    public function subscriberCount()
278 279 280 281
    {
        if ($this->isGroup()) {
            $members = $this->localGroup()->getMembers(0, 1);
            $count = $members->N;
282 283 284
        } else if ($this->isPeopletag()) {
            $subscribers = $this->localPeopletag()->getSubscribers(0, 1);
            $count = $subscribers->N;
285
        } else {
286 287 288
            $profile = $this->localProfile();
            if ($profile->hasLocalTags()) {
                $count = 1;
289 290
            } else {
                $count = $profile->subscriberCount();
291
            }
292
        }
293 294 295 296 297 298 299 300
        common_log(LOG_INFO, __METHOD__ . " SUB COUNT BEFORE: $count");

        // Other plugins may be piggybacking on OStatus without having
        // an active group or user-to-user subscription we know about.
        Event::handle('Ostatus_profileSubscriberCount', array($this, &$count));
        common_log(LOG_INFO, __METHOD__ . " SUB COUNT AFTER: $count");

        return $count;
301 302
    }

303 304 305 306
    /**
     * Send an Activity Streams notification to the remote Salmon endpoint,
     * if so configured.
     *
307 308 309
     * @param Profile $actor  Actor who did the activity
     * @param string  $verb   Activity::SUBSCRIBE or Activity::JOIN
     * @param Object  $object object of the action; must define asActivityNoun($tag)
310
     */
311
    public function notify(Profile $actor, $verb, $object=null, $target=null)
312 313
    {
        if ($object == null) {
314
            $object = $this;
315
        }
316 317 318 319 320 321 322 323
        if (empty($this->salmonuri)) {
            return false;
        }
        $text = 'update';
        $id = TagURI::mint('%s:%s:%s',
                           $verb,
                           $actor->getURI(),
                           common_date_iso8601(time()));
324

325 326 327 328 329 330 331 332
        // @todo FIXME: Consolidate all these NS settings somewhere.
        $attributes = array('xmlns' => Activity::ATOM,
                            'xmlns:activity' => 'http://activitystrea.ms/spec/1.0/',
                            'xmlns:thr' => 'http://purl.org/syndication/thread/1.0',
                            'xmlns:georss' => 'http://www.georss.org/georss',
                            'xmlns:ostatus' => 'http://ostatus.org/schema/1.0',
                            'xmlns:poco' => 'http://portablecontacts.net/spec/1.0',
                            'xmlns:media' => 'http://purl.org/syndication/atommedia');
333

334 335 336 337 338 339
        $entry = new XMLStringer();
        $entry->elementStart('entry', $attributes);
        $entry->element('id', null, $id);
        $entry->element('title', null, $text);
        $entry->element('summary', null, $text);
        $entry->element('published', null, common_date_w3dtf(common_sql_now()));
340

341 342 343 344 345 346 347 348
        $entry->element('activity:verb', null, $verb);
        $entry->raw($actor->asAtomAuthor());
        $entry->raw($actor->asActivityActor());
        $entry->raw($object->asActivityNoun('object'));
        if ($target != null) {
            $entry->raw($target->asActivityNoun('target'));
        }
        $entry->elementEnd('entry');
349

350 351
        $xml = $entry->getString();
        common_log(LOG_INFO, "Posting to Salmon endpoint $this->salmonuri: $xml");
352

353
        Salmon::post($this->salmonuri, $xml, $actor);
354 355
    }

356 357 358 359 360
    /**
     * Send a Salmon notification ping immediately, and confirm that we got
     * an acceptable response from the remote site.
     *
     * @param mixed $entry XML string, Notice, or Activity
361
     * @param Profile $actor
362 363
     * @return boolean success
     */
364
    public function notifyActivity($entry, Profile $actor)
365 366
    {
        if ($this->salmonuri) {
367
            return Salmon::post($this->salmonuri, $this->notifyPrepXml($entry), $actor, $this->localProfile());
368
        }
369
        common_debug(__CLASS__.' error: No salmonuri for Ostatus_profile uri: '.$this->uri);
370

371 372
        return false;
    }
373

374 375 376 377 378 379 380
    /**
     * Queue a Salmon notification for later. If queues are disabled we'll
     * send immediately but won't get the return value.
     *
     * @param mixed $entry XML string, Notice, or Activity
     * @return boolean success
     */
381
    public function notifyDeferred($entry, $actor)
382 383
    {
        if ($this->salmonuri) {
384 385 386 387 388 389 390 391 392 393 394 395 396
            try {
                common_debug("OSTATUS: user {$actor->getNickname()} ({$actor->getID()}) wants to ping {$this->localProfile()->getNickname()} on {$this->salmonuri}");
                $data = array('salmonuri' => $this->salmonuri,
                              'entry' => $this->notifyPrepXml($entry),
                              'actor' => $actor->getID(),
                              'target' => $this->localProfile()->getID());

                $qm = QueueManager::get();
                return $qm->enqueue($data, 'salmon');
            } catch (Exception $e) {
                common_log(LOG_ERR, 'OSTATUS: Something went wrong when creating a Salmon slap: '._ve($e->getMessage()));
                return false;
            }
397 398
        }

399
        return false;
400 401
    }

402 403 404 405 406 407 408 409 410 411
    protected function notifyPrepXml($entry)
    {
        $preamble = '<?xml version="1.0" encoding="UTF-8" ?' . '>';
        if (is_string($entry)) {
            return $entry;
        } else if ($entry instanceof Activity) {
            return $preamble . $entry->asString(true);
        } else if ($entry instanceof Notice) {
            return $preamble . $entry->asAtomEntry(true, true);
        } else {
Siebrand Mazeland's avatar
Siebrand Mazeland committed
412 413
            // TRANS: Server exception.
            throw new ServerException(_m('Invalid type passed to Ostatus_profile::notify. It must be XML string or Activity entry.'));
414 415 416
        }
    }

417 418 419 420
    function getBestName()
    {
        if ($this->isGroup()) {
            return $this->localGroup()->getBestName();
421 422
        } else if ($this->isPeopletag()) {
            return $this->localPeopletag()->getBestName();
423 424 425 426 427
        } else {
            return $this->localProfile()->getBestName();
        }
    }

428 429 430 431 432
    /**
     * Read and post notices for updates from the feed.
     * Currently assumes that all items in the feed are new,
     * coming from a PuSH hub.
     *
433 434
     * @param DOMDocument $doc
     * @param string $source identifier ("push")
435
     */
436
    public function processFeed(DOMDocument $doc, $source)
437
    {
438 439
        $feed = $doc->documentElement;

440 441
        if ($feed->localName == 'feed' && $feed->namespaceURI == Activity::ATOM) {
            $this->processAtomFeed($feed, $source);
442
        } else if ($feed->localName == 'rss') { // @todo FIXME: Check namespace.
443 444
            $this->processRssFeed($feed, $source);
        } else {
445
            // TRANS: Exception.
Siebrand Mazeland's avatar
Siebrand Mazeland committed
446
            throw new Exception(_m('Unknown feed format.'));
447
        }
448
    }
449

450 451
    public function processAtomFeed(DOMElement $feed, $source)
    {
452 453 454 455 456 457
        $entries = $feed->getElementsByTagNameNS(Activity::ATOM, 'entry');
        if ($entries->length == 0) {
            common_log(LOG_ERR, __METHOD__ . ": no entries in feed update, ignoring");
            return;
        }

458
        $this->processEntries($entries, $feed, $source);
459 460
    }

461 462 463 464 465
    public function processRssFeed(DOMElement $rss, $source)
    {
        $channels = $rss->getElementsByTagName('channel');

        if ($channels->length == 0) {
466
            // TRANS: Exception.
Siebrand Mazeland's avatar
Siebrand Mazeland committed
467
            throw new Exception(_m('RSS feed without a channel.'));
468 469 470 471 472 473 474 475
        } else if ($channels->length > 1) {
            common_log(LOG_WARNING, __METHOD__ . ": more than one channel in an RSS feed");
        }

        $channel = $channels->item(0);

        $items = $channel->getElementsByTagName('item');

476 477 478 479 480 481 482 483 484 485 486
        $this->processEntries($items, $channel, $source);
    }

    public function processEntries(DOMNodeList $entries, DOMElement $feed, $source)
    {
        for ($i = 0; $i < $entries->length; $i++) {
            $entry = $entries->item($i);
            try {
                $this->processEntry($entry, $feed, $source);
            } catch (AlreadyFulfilledException $e) {
                common_debug('We already had this entry: '.$e->getMessage());
487 488 489
            } catch (ServerException $e) {
                // FIXME: This should be UnknownUriException and the ActivityUtils:: findLocalObject should only test one URI
                common_log(LOG_ERR, 'Entry threw exception while processing a feed from '.$source.': '.$e->getMessage());
490
            }
491 492 493
        }
    }

494 495 496 497 498
    /**
     * Process a posted entry from this feed source.
     *
     * @param DOMElement $entry
     * @param DOMElement $feed for context
499
     * @param string $source identifier ("push" or "salmon")
500 501
     *
     * @return Notice Notice representing the new (or existing) activity
502
     */
503
    public function processEntry(DOMElement $entry, DOMElement $feed, $source)
504 505
    {
        $activity = new Activity($entry, $feed);
506
        return $this->processActivity($activity, $source);
507 508
    }

509
    // TODO: Make this throw an exception
510
    public function processActivity(Activity $activity, $source)
511
    {
512 513
        $notice = null;

514
        // The "WithProfile" events were added later.
515

516
        if (Event::handle('StartHandleFeedEntryWithProfile', array($activity, $this->localProfile(), &$notice)) &&
517
            Event::handle('StartHandleFeedEntry', array($activity))) {
518

519
            common_log(LOG_INFO, "Ignoring activity with unrecognized verb $activity->verb");
520

521
            Event::handle('EndHandleFeedEntry', array($activity));
522
            Event::handle('EndHandleFeedEntryWithProfile', array($activity, $this, $notice));
523
        }
524

525
        return $notice;
526 527 528 529 530
    }

    /**
     * Process an incoming post activity from this remote feed.
     * @param Activity $activity
531 532
     * @param string $method 'push' or 'salmon'
     * @return mixed saved Notice or false
533
     */
534
    public function processPost($activity, $method)
535
    {
536
        $actor = ActivityUtils::checkAuthorship($activity, $this->localProfile());
537

538
        $options = array('is_local' => Notice::REMOTE);
539

540
        try {
541 542
            $stored = Notice::saveActivity($activity, $actor, $options);
            Ostatus_source::saveNew($stored, $this, $method);
543
        } catch (Exception $e) {
544 545
            common_log(LOG_ERR, "OStatus save of remote message $sourceUri failed: " . $e->getMessage());
            throw $e;
546
        }
547
        return $stored;
548
    }
549

550 551
    /**
     * Filters a list of recipient ID URIs to just those for local delivery.
552
     * @param Profile local profile of sender
553 554 555
     * @param array in/out &$attention_uris set of URIs, will be pruned on output
     * @return array of group IDs
     */
556
    static public function filterAttention(Profile $sender, array $attention)
557
    {
558
        common_log(LOG_DEBUG, "Original reply recipients: " . implode(', ', array_keys($attention)));
559 560
        $groups = array();
        $replies = array();
561
        foreach ($attention as $recipient=>$type) {
562
            // Is the recipient a local user?
563
            $user = User::getKV('uri', $recipient);
564
            if ($user instanceof User) {
565
                // @todo FIXME: Sender verification, spam etc?
566 567 568 569 570
                $replies[] = $recipient;
                continue;
            }

            // Is the recipient a local group?
571
            // TODO: $group = User_group::getKV('uri', $recipient);
572 573
            $id = OStatusPlugin::localGroupFromUrl($recipient);
            if ($id) {
574
                $group = User_group::getKV('id', $id);
575
                if ($group instanceof User_group) {
576 577 578 579 580
                    // Deliver to all members of this local group if allowed.
                    if ($sender->isMember($group)) {
                        $groups[] = $group->id;
                    } else {
                        common_log(LOG_DEBUG, sprintf('Skipping reply to local group %s as sender %d is not a member', $group->getNickname(), $sender->id));
581 582
                    }
                    continue;
583 584
                } else {
                    common_log(LOG_DEBUG, "Skipping reply to bogus group $recipient");
585 586
                }
            }
587

588 589
            // Is the recipient a remote user or group?
            try {
590
                $oprofile = self::ensureProfileURI($recipient);
591 592
                if ($oprofile->isGroup()) {
                    // Deliver to local members of this remote group.
593
                    // @todo FIXME: Sender verification?
594 595 596
                    $groups[] = $oprofile->group_id;
                } else {
                    // may be canonicalized or something
597
                    $replies[] = $oprofile->getUri();
598 599 600 601 602 603
                }
                continue;
            } catch (Exception $e) {
                // Neither a recognizable local nor remote user!
                common_log(LOG_DEBUG, "Skipping reply to unrecognized profile $recipient: " . $e->getMessage());
            }
604

605
        }
606 607
        common_log(LOG_DEBUG, "Local reply recipients: " . implode(', ', $replies));
        common_log(LOG_DEBUG, "Local group recipients: " . implode(', ', $groups));
608
        return array($groups, $replies);
609 610
    }

611
    /**
612 613 614 615
     * Look up and if necessary create an Ostatus_profile for the remote entity
     * with the given profile page URL. This should never return null -- you
     * will either get an object or an exception will be thrown.
     *
616 617
     * @param string $profile_url
     * @return Ostatus_profile
618 619
     * @throws Exception on various error conditions
     * @throws OStatusShadowException if this reference would obscure a local user/group
620
     */
mmn's avatar
mmn committed
621
    public static function ensureProfileURL($profile_url, array $hints=array())
622
    {
623 624
        $oprofile = self::getFromProfileURL($profile_url);

625
        if ($oprofile instanceof Ostatus_profile) {
626 627 628 629 630 631 632 633 634 635 636 637 638
            return $oprofile;
        }

        $hints['profileurl'] = $profile_url;

        // Fetch the URL
        // XXX: HTTP caching

        $client = new HTTPClient();
        $client->setHeader('Accept', 'text/html,application/xhtml+xml');
        $response = $client->get($profile_url);

        if (!$response->isOk()) {
Siebrand Mazeland's avatar
Siebrand Mazeland committed
639 640
            // TRANS: Exception. %s is a profile URL.
            throw new Exception(sprintf(_m('Could not reach profile page %s.'),$profile_url));
641 642 643 644
        }

        // Check if we have a non-canonical URL

645
        $finalUrl = $response->getEffectiveUrl();
646 647 648 649 650 651 652

        if ($finalUrl != $profile_url) {

            $hints['profileurl'] = $finalUrl;

            $oprofile = self::getFromProfileURL($finalUrl);

653
            if ($oprofile instanceof Ostatus_profile) {
654 655 656 657
                return $oprofile;
            }
        }

658 659 660 661
        if (in_array(
            preg_replace('/\s*;.*$/', '', $response->getHeader('Content-Type')),
            array('application/rss+xml', 'application/atom+xml', 'application/xml', 'text/xml'))
        ) {
662
            $hints['feedurl'] = $response->getEffectiveUrl();
663 664
        } else {
            // Try to get some hCard data
665

666
            $body = $response->getBody();
667

668
            $hcardHints = DiscoveryHints::hcardHints($body, $finalUrl);
669

670 671 672
            if (!empty($hcardHints)) {
                $hints = array_merge($hints, $hcardHints);
            }
673 674 675 676
        }

        // Check if they've got an LRDD header

677 678 679 680
        $lrdd = LinkHeader::getLink($response, 'lrdd');
        try {
            $xrd = new XML_XRD();
            $xrd->loadFile($lrdd);
681 682
            $xrdHints = DiscoveryHints::fromXRD($xrd);
            $hints = array_merge($hints, $xrdHints);
683 684
        } catch (Exception $e) {
            // No hints available from XRD
685 686 687 688 689 690 691 692 693 694
        }

        // If discovery found a feedurl (probably from LRDD), use it.

        if (array_key_exists('feedurl', $hints)) {
            return self::ensureFeedURL($hints['feedurl'], $hints);
        }

        // Get the feed URL from HTML

695
        $discover = new FeedDiscovery();
696 697 698 699 700 701 702

        $feedurl = $discover->discoverFromHTML($finalUrl, $body);

        if (!empty($feedurl)) {
            $hints['feedurl'] = $feedurl;
            return self::ensureFeedURL($feedurl, $hints);
        }
703

704
        // TRANS: Exception. %s is a URL.
Siebrand Mazeland's avatar
Siebrand Mazeland committed
705
        throw new Exception(sprintf(_m('Could not find a feed URL for profile page %s.'),$finalUrl));
706 707
    }

708 709 710 711 712 713
    /**
     * Look up the Ostatus_profile, if present, for a remote entity with the
     * given profile page URL. Will return null for both unknown and invalid
     * remote profiles.
     *
     * @return mixed Ostatus_profile or null
714
     * @throws OStatusShadowException for local profiles
715
     */
716 717
    static function getFromProfileURL($profile_url)
    {
718
        $profile = Profile::getKV('profileurl', $profile_url);
719
        if (!$profile instanceof Profile) {
720 721 722
            return null;
        }

723 724 725
        try {
            $oprofile = self::getFromProfile($profile);
            // We found the profile, return it!
726
            return $oprofile;
727 728 729 730 731 732 733
        } catch (NoResultException $e) {
            // Could not find an OStatus profile, is it instead a local user?
            $user = User::getKV('id', $profile->id);
            if ($user instanceof User) {
                // @todo i18n FIXME: use sprintf and add i18n (?)
                throw new OStatusShadowException($profile, "'$profile_url' is the profile for local user '{$user->nickname}'.");
            }
734 735 736 737 738 739 740 741 742
        }

        // Continue discovery; it's a remote profile
        // for OMB or some other protocol, may also
        // support OStatus

        return null;
    }

743 744 745 746 747 748 749 750 751 752
    static function getFromProfile(Profile $profile)
    {
        $oprofile = new Ostatus_profile();
        $oprofile->profile_id = $profile->id;
        if (!$oprofile->find(true)) {
            throw new NoResultException($oprofile);
        }
        return $oprofile;
    }

753 754 755 756 757 758 759 760
    /**
     * Look up and if necessary create an Ostatus_profile for remote entity
     * with the given update feed. This should never return null -- you will
     * either get an object or an exception will be thrown.
     *
     * @return Ostatus_profile
     * @throws Exception
     */
mmn's avatar
mmn committed
761
    public static function ensureFeedURL($feed_url, array $hints=array())
762
    {
763 764 765 766 767
        $oprofile = Ostatus_profile::getKV('feeduri', $feed_url);
        if ($oprofile instanceof Ostatus_profile) {
            return $oprofile;
        }

768 769 770 771 772
        $discover = new FeedDiscovery();

        $feeduri = $discover->discoverFromFeedURL($feed_url);
        $hints['feedurl'] = $feeduri;

773
        $huburi = $discover->getHubLink();
774
        $hints['hub'] = $huburi;
775 776 777 778

        // XXX: NS_REPLIES is deprecated anyway, so let's remove it in the future.
        $salmonuri = $discover->getAtomLink(Salmon::REL_SALMON)
                        ?: $discover->getAtomLink(Salmon::NS_REPLIES);
779
        $hints['salmon'] = $salmonuri;
780

781
        if (!$huburi && !common_config('feedsub', 'fallback_hub') && !common_config('feedsub', 'nohub')) {
782
            // We can only deal with folks with a PuSH hub
783
            // unless we have something similar available locally.
784 785 786
            throw new FeedSubNoHubException();
        }

787 788 789 790 791 792 793 794 795 796
        $feedEl = $discover->root;

        if ($feedEl->tagName == 'feed') {
            return self::ensureAtomFeed($feedEl, $hints);
        } else if ($feedEl->tagName == 'channel') {
            return self::ensureRssChannel($feedEl, $hints);
        } else {
            throw new FeedSubBadXmlException($feeduri);
        }
    }
797

798 799 800 801 802 803 804 805
    /**
     * Look up and, if necessary, create an Ostatus_profile for the remote
     * profile with the given Atom feed - actually loaded from the feed.
     * This should never return null -- you will either get an object or
     * an exception will be thrown.
     *
     * @param DOMElement $feedEl root element of a loaded Atom feed
     * @param array $hints additional discovery information passed from higher levels
806
     * @todo FIXME: Should this be marked public?
807 808 809
     * @return Ostatus_profile
     * @throws Exception
     */
mmn's avatar
mmn committed
810
    public static function ensureAtomFeed(DOMElement $feedEl, array $hints)
811
    {
812
        $author = ActivityUtils::getFeedAuthor($feedEl);
813

814 815 816
        if (empty($author)) {
            // XXX: make some educated guesses here
            // TRANS: Feed sub exception.
817
            throw new FeedSubException(_m('Cannot find enough profile '.
818
                                          'information to make a feed.'));
819
        }
820

821
        return self::ensureActivityObjectProfile($author, $hints);
822 823
    }

824 825 826 827 828 829 830 831
    /**
     * Look up and, if necessary, create an Ostatus_profile for the remote
     * profile with the given RSS feed - actually loaded from the feed.
     * This should never return null -- you will either get an object or
     * an exception will be thrown.
     *
     * @param DOMElement $feedEl root element of a loaded RSS feed
     * @param array $hints additional discovery information passed from higher levels
832
     * @todo FIXME: Should this be marked public?
833 834 835
     * @return Ostatus_profile
     * @throws Exception
     */
mmn's avatar
mmn committed
836
    public static function ensureRssChannel(DOMElement $feedEl, array $hints)
837
    {
838 839 840 841 842 843 844 845 846 847
        // Special-case for Posterous. They have some nice metadata in their
        // posterous:author elements. We should use them instead of the channel.

        $items = $feedEl->getElementsByTagName('item');

        if ($items->length > 0) {
            $item = $items->item(0);
            $authorEl = ActivityUtils::child($item, ActivityObject::AUTHOR, ActivityObject::POSTEROUS);
            if (!empty($authorEl)) {
                $obj = ActivityObject::fromPosterousAuthor($authorEl);
848 849 850 851 852 853 854
                // Posterous has multiple authors per feed, and multiple feeds
                // per author. We check if this is the "main" feed for this author.
                if (array_key_exists('profileurl', $hints) &&
                    !empty($obj->poco) &&
                    common_url_to_nickname($hints['profileurl']) == $obj->poco->preferredUsername) {
                    return self::ensureActivityObjectProfile($obj, $hints);
                }
855 856 857
            }
        }

858 859
        $obj = ActivityUtils::getFeedAuthor($feedEl);

860
        // @todo FIXME: We should check whether this feed has elements
861 862 863
        // with different <author> or <dc:creator> elements, and... I dunno.
        // Do something about that.

864
        if(empty($obj)) { $obj = ActivityObject::fromRssChannel($feedEl); }
865 866 867 868

        return self::ensureActivityObjectProfile($obj, $hints);
    }

869 870
    /**
     * Download and update given avatar image
871
     *
872
     * @param string $url
873
     * @return Avatar    The Avatar we have on disk. (seldom used)
874 875
     * @throws Exception in various failure cases
     */
876
    public function updateAvatar($url, $force=false)
877
    {
878 879 880 881 882 883 884 885 886
        try {
            // If avatar URL differs: update. If URLs were identical but we're forced: update.
            if ($url == $this->avatar && !$force) {
                // If there's no locally stored avatar, throw an exception and continue fetching below.
                $avatar = Avatar::getUploaded($this->localProfile()) instanceof Avatar;
                return $avatar;
            }
        } catch (NoAvatarException $e) {
            // No avatar available, let's fetch it.
887
        }
888

889
        if (!common_valid_http_url($url)) {
890
            // TRANS: Server exception. %s is a URL.
891
            throw new ServerException(sprintf(_m('Invalid avatar URL %s.'), $url));
892
        }
893

894
        $self = $this->localProfile();
895

896
        // @todo FIXME: This should be better encapsulated
897 898
        // ripped from oauthstore.php (for old OMB client)
        $temp_filename = tempnam(sys_get_temp_dir(), 'listener_avatar');
899
        try {
900 901 902 903
            $imgData = HTTPClient::quickGet($url);
            // Make sure it's at least an image file. ImageFile can do the rest.
            if (false === getimagesizefromstring($imgData)) {
                throw new UnsupportedMediaException(_('Downloaded group avatar was not an image.'));
904
            }
905 906
            file_put_contents($temp_filename, $imgData);
            unset($imgData);    // No need to carry this in memory.
907

908 909 910 911 912
            if ($this->isGroup()) {
                $id = $this->group_id;
            } else {
                $id = $this->profile_id;
            }
913
            $imagefile = new ImageFile(null, $temp_filename);
914 915 916 917 918 919 920 921
            $filename = Avatar::filename($id,
                                         image_type_to_extension($imagefile->type),
                                         null,
                                         common_timestamp());
            rename($temp_filename, Avatar::path($filename));
        } catch (Exception $e) {
            unlink($temp_filename);
            throw $e;
922
        }
923
        // @todo FIXME: Hardcoded chmod is lame, but seems to be necessary to
924 925 926 927 928 929 930
        // keep from accidentally saving images from command-line (queues)
        // that can't be read from web server, which causes hard-to-notice
        // problems later on:
        //
        // http://status.net/open-source/issues/2663
        chmod(Avatar::path($filename), 0644);

mmn's avatar
mmn committed
931
        $self->setOriginal($filename);
932 933 934 935

        $orig = clone($this);
        $this->avatar = $url;
        $this->update($orig);
936 937

        return Avatar::getUploaded($self);
938 939
    }

940 941 942 943 944 945 946
    /**
     * Pull avatar URL from ActivityObject or profile hints
     *
     * @param ActivityObject $object
     * @param array $hints
     * @return mixed URL string or false
     */
mmn's avatar
mmn committed
947
    public static function getActivityObjectAvatar(ActivityObject $object, array $hints=array())
948
    {
949 950 951 952 953 954 955 956 957 958 959 960 961 962
        if ($object->avatarLinks) {
            $best = false;
            // Take the exact-size avatar, or the largest avatar, or the first avatar if all sizeless
            foreach ($object->avatarLinks as $avatar) {
                if ($avatar->width == AVATAR_PROFILE_SIZE && $avatar->height = AVATAR_PROFILE_SIZE) {
                    // Exact match!
                    $best = $avatar;
                    break;
                }
                if (!$best || $avatar->width > $best->width) {
                    $best = $avatar;
                }
            }
            return $best->url;
963 964 965 966
        } else if (array_key_exists('avatar', $hints)) {
            return $hints['avatar'];
        }
        return false;
967 968
    }

969 970 971 972 973 974 975
    /**
     * Get an appropriate avatar image source URL, if available.
     *
     * @param ActivityObject $actor
     * @param DOMElement $feed
     * @return string
     */
mmn's avatar
mmn committed
976
    protected static function getAvatar(ActivityObject $actor, DOMElement $feed)
977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006
    {
        $url = '';
        $icon = '';
        if ($actor->avatar) {
            $url = trim($actor->avatar);
        }
        if (!$url) {
            // Check <atom:logo> and <atom:icon> on the feed
            $els = $feed->childNodes();
            if ($els && $els->length) {
                for ($i = 0; $i < $els->length; $i++) {
                    $el = $els->item($i);
                    if ($el->namespaceURI == Activity::ATOM) {
                        if (empty($url) && $el->localName == 'logo') {
                            $url = trim($el->textContent);
                            break;
                        }
                        if (empty($icon) && $el->localName == 'icon') {
                            // Use as a fallback
                            $icon = trim($el->textContent);
                        }
                    }
                }
            }
            if ($icon && !$url) {
                $url = $icon;
            }
        }
        if ($url) {
            $opts = array('allowed_schemes' => array('http', 'https'));
1007
            if (common_valid_http_url($url)) {
1008 1009 1010
                return $url;
            }
        }
1011 1012

        return Plugin::staticPath('OStatus', 'images/96px-Feed-icon.svg.png');
1013 1014 1015
    }

    /**
1016 1017
     * Fetch, or build if necessary, an Ostatus_profile for the actor
     * in a given Activity Streams activity.
1018 1019
     * This should never return null -- you will either get an object or
     * an exception will be thrown.
1020 1021 1022
     *
     * @param Activity $activity
     * @param string $feeduri if we already know the canonical feed URI!
1023
     * @param string $salmonuri if we already know the salmon return channel URI
1024
     * @return Ostatus_profile
1025
     * @throws Exception
1026
     */
mmn's avatar
mmn committed
1027
    public static function ensureActorProfile(Activity $activity, array $hints=array())
1028
    {
1029
        return self::ensureActivityObjectProfile($activity->actor, $hints);
1030 1031
    }

1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042
    /**
     * Fetch, or build if necessary, an Ostatus_profile for the profile
     * in a given Activity Streams object (can be subject, actor, or object).
     * This should never return null -- you will either get an object or
     * an exception will be thrown.
     *
     * @param ActivityObject $object
     * @param array $hints additional discovery information passed from higher levels
     * @return Ostatus_profile
     * @throws Exception
     */
mmn's avatar
mmn committed
1043
    public static function ensureActivityObjectProfile(ActivityObject $object, array $hints=array())
1044 1045
    {
        $profile = self::getActivityObjectProfile($object);
1046
        if ($profile instanceof Ostatus_profile) {
1047 1048
            $profile->updateFromActivityObject($object, $hints);
        } else {
1049
            $profile = self::createActivityObjectProfile($object, $hints);
1050 1051 1052 1053 1054 1055 1056
        }
        return $profile;
    }

    /**
     * @param Activity $activity
     * @return mixed matching Ostatus_profile or false if none known
1057
     * @throws ServerException if feed info invalid
1058
     */
mmn's avatar
mmn committed
1059
    public static function getActorProfile(Activity $activity)
1060
    {
1061 1062 1063
        return self::getActivityObjectProfile($activity->actor);
    }

1064 1065 1066 1067 1068
    /**
     * @param ActivityObject $activity
     * @return mixed matching Ostatus_profile or false if none known
     * @throws ServerException if feed info invalid
     */
mmn's avatar
mmn committed
1069
    protected static function getActivityObjectProfile(ActivityObject $object)
1070 1071
    {
        $uri = self::getActivityObjectProfileURI($object);
1072
        return Ostatus_profile::getKV('uri', $uri);
1073 1074
    }

1075
    /**
1076 1077 1078 1079 1080
     * Get the identifier URI for the remote entity described
     * by this ActivityObject. This URI is *not* guaranteed to be
     * a resolvable HTTP/HTTPS URL.
     *
     * @param ActivityObject $object
1081
     * @return string
1082
     * @throws ServerException if feed info invalid
1083
     */
mmn's avatar
mmn committed
1084
    protected static function getActivityObjectProfileURI(ActivityObject $object)
1085
    {
1086
        if ($object->id) {
1087
            if (ActivityUtils::validateUri($object->id)) {
1088 1089
                return $object->id;
            }
1090
        }
1091 1092 1093 1094 1095

        // If the id is missing or invalid (we've seen feeds mistakenly listing
        // things like local usernames in that field) then we'll use the profile
        // page link, if valid.
        if ($object->link && common_valid_http_url($object->link)) {
1096
            return $object->link;
1097
        }
Siebrand Mazeland's avatar
Siebrand Mazeland committed
1098
        // TRANS: Server exception.
1099
        throw new ServerException(_m('No author ID URI found.'));
1100 1101 1102
    }

    /**
1103
     * @todo FIXME: Validate stuff somewhere.
1104
     */
1105

1106 1107 1108
    /**
     * Create local ostatus_profile and profile/user_group entries for
     * the provided remote user or group.
1109 1110
     * This should never return null -- you will either get an object or
     * an exception will be thrown.
1111 1112 1113 1114 1115 1116
     *
     * @param ActivityObject $object
     * @param array $hints
     *
     * @return Ostatus_profile
     */
mmn's avatar
mmn committed
1117
    protected static function createActivityObjectProfile(ActivityObject $object, array $hints=array())
1118
    {
1119 1120
        $homeuri = $object->id;
        $discover = false;
1121 1122 1123

        if (!$homeuri) {
            common_log(LOG_DEBUG, __METHOD__ . " empty actor profile URI: " . var_export($activity, true));
Siebrand Mazeland's avatar
Siebrand Mazeland committed
1124
            // TRANS: Exception.
1125
            throw new Exception(_m('No profile URI.'));
1126 1127
        }

1128
        $user = User::getKV('uri', $homeuri);
1129
        if ($user instanceof User) {
Siebrand Mazeland's avatar
Siebrand Mazeland committed
1130
            // TRANS: Exception.
1131
            throw new Exception(_m('Local user cannot be referenced as remote.'));
1132 1133 1134
        }

        if (OStatusPlugin::localGroupFromUrl($homeuri)) {
Siebrand Mazeland's avatar
Siebrand Mazeland committed
1135
            // TRANS: Exception.
1136
            throw new Exception(_m('Local group cannot be referenced as remote.'));
1137
        }
1138

1139
        $ptag = Profile_list::getKV('uri', $homeuri);
1140
        if ($ptag instanceof Profile_list) {
1141
            $local_user = User::getKV('id', $ptag->tagger);
1142
            if ($local_user instanceof User) {
Siebrand Mazeland's avatar
Siebrand Mazeland committed
1143
                // TRANS: Exception.
1144
                throw new Exception(_m('Local list cannot be referenced as remote.'));
1145 1146 1147
            }
        }

1148 1149 1150 1151 1152
        if (array_key_exists('feedurl', $hints)) {
            $feeduri = $hints['feedurl'];
        } else {
            $discover = new FeedDiscovery();
            $feeduri = $discover->discoverFromURL($homeuri);
1153 1154
        }

1155 1156 1157 1158 1159 1160
        if (array_key_exists('salmon', $hints)) {
            $salmonuri = $hints['salmon'];
        } else {
            if (!$discover) {
                $discover = new FeedDiscovery();
                $discover->discoverFromFeedURL($hints['feedurl']);
1161
            }
1162 1163 1164
            // XXX: NS_REPLIES is deprecated anyway, so let's remove it in the future.
            $salmonuri = $discover->getAtomLink(Salmon::REL_SALMON)
                            ?: $discover->getAtomLink(Salmon::NS_REPLIES);
1165 1166
        }

1167 1168 1169 1170 1171 1172 1173
        if (array_key_exists('hub', $hints)) {
            $huburi = $hints['hub'];
        } else {
            if (!$discover) {
                $discover = new FeedDiscovery();
                $discover->discoverFromFeedURL($hints['feedurl']);
            }
1174
            $huburi = $discover->getHubLink();
1175
        }
1176

1177
        if (!$huburi && !common_config('feedsub', 'fallback_hub') && !common_config('feedsub', 'nohub')) {
1178 1179
            // We can only deal with folks with a PuSH hub
            throw new FeedSubNoHubException();
1180 1181
        }

1182
        $oprofile = new Ostatus_profile();
1183 1184 1185 1186

        $oprofile->uri        = $homeuri;
        $oprofile->feeduri    = $feeduri;
        $oprofile->salmonuri  = $salmonuri;
1187

1188 1189
        $oprofile->created    = common_sql_now();
        $oprofile->modified   = common_sql_now();
1190

1191 1192
        if ($object->type == ActivityObject::PERSON) {
            $profile = new Profile();
1193
            $profile->created = common_sql_now();
1194 1195
            self::updateProfile($profile, $object, $hints);

1196
            $oprofile->profile_id = $profile->insert();
1197
            if ($oprofile->profile_id === false) {
1198
                // TRANS: Server exception.
1199
                throw new ServerException(_m('Cannot save local profile.'));
1200
            }
1201
        } else if ($object->type == ActivityObject::GROUP) {
1202 1203 1204
            $profile = new Profile();
            $profile->query('BEGIN');

1205
            $group = new User_group();
1206
            $group->uri = $homeuri;
1207
            $group->created = common_sql_now();
1208
            self::updateGroup($group, $object, $hints);
1209

1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233
            // TODO: We should do this directly in User_group->insert()!
            // currently it's duplicated in User_group->update()
            // AND User_group->register()!!!
            $fields = array(/*group field => profile field*/
                        'nickname'      => 'nickname',
                        'fullname'      => 'fullname',
                        'mainpage'      => 'profileurl',
                        'homepage'      => 'homepage',
                        'description'   => 'bio',
                        'location'      => 'location',
                        'created'       => 'created',
                        'modified'      => 'modified',
                        );
            foreach ($fields as $gf=>$pf) {
                $profile->$pf = $group->$gf;
            }
            $profile_id = $profile->insert();
            if ($profile_id === false) {
                $profile->query('ROLLBACK');
                throw new ServerException(_('Profile insertion failed.'));
            }

            $group->profile_id = $profile_id;

1234
            $oprofile->group_id = $group->insert();
1235 1236
            if ($oprofile->group_id === false) {
                $profile->query('ROLLBACK');
1237
                // TRANS: Server exception.
1238
                throw new ServerException(_m('Cannot save local profile.'));
1239
            }
1240 1241

            $profile->query('COMMIT');
1242 1243 1244 1245 1246 12