/** * Handle the request * * @param array $args $_REQUEST data (unused) * * @return void */ protected function handle() { parent::handle(); $discover = new FeedDiscovery(); try { $feeduri = $discover->discoverFromURL($this->url); if ($feeduri) { $huburi = $discover->getHubLink(); } } catch (FeedSubNoFeedException $e) { $this->clientError(_('No feed found'), 403); } catch (FeedSubBadResponseException $e) { $this->clientError(_('No hub found'), 403); } $hub_status = array(); if ($huburi) { $hub_status = array('huburi' => $huburi); } $this->initDocument('json'); $this->showJsonObjects($hub_status); $this->endDocument('json'); }
/** * Create local ostatus_profile and profile/user_group entries for * the provided remote user or group. * This should never return null -- you will either get an object or * an exception will be thrown. * * @param ActivityObject $object * @param array $hints * * @return Ostatus_profile */ protected static function createActivityObjectProfile(ActivityObject $object, array $hints = array()) { $homeuri = $object->id; $discover = false; if (!$homeuri) { common_log(LOG_DEBUG, __METHOD__ . " empty actor profile URI: " . var_export($activity, true)); // TRANS: Exception. throw new Exception(_m('No profile URI.')); } $user = User::getKV('uri', $homeuri); if ($user instanceof User) { // TRANS: Exception. throw new Exception(_m('Local user cannot be referenced as remote.')); } if (OStatusPlugin::localGroupFromUrl($homeuri)) { // TRANS: Exception. throw new Exception(_m('Local group cannot be referenced as remote.')); } $ptag = Profile_list::getKV('uri', $homeuri); if ($ptag instanceof Profile_list) { $local_user = User::getKV('id', $ptag->tagger); if ($local_user instanceof User) { // TRANS: Exception. throw new Exception(_m('Local list cannot be referenced as remote.')); } } if (array_key_exists('feedurl', $hints)) { $feeduri = $hints['feedurl']; } else { $discover = new FeedDiscovery(); $feeduri = $discover->discoverFromURL($homeuri); } if (array_key_exists('salmon', $hints)) { $salmonuri = $hints['salmon']; } else { if (!$discover) { $discover = new FeedDiscovery(); $discover->discoverFromFeedURL($hints['feedurl']); } // 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); } if (array_key_exists('hub', $hints)) { $huburi = $hints['hub']; } else { if (!$discover) { $discover = new FeedDiscovery(); $discover->discoverFromFeedURL($hints['feedurl']); } $huburi = $discover->getHubLink(); } if (!$huburi && !common_config('feedsub', 'fallback_hub') && !common_config('feedsub', 'nohub')) { // We can only deal with folks with a PuSH hub throw new FeedSubNoHubException(); } $oprofile = new Ostatus_profile(); $oprofile->uri = $homeuri; $oprofile->feeduri = $feeduri; $oprofile->salmonuri = $salmonuri; $oprofile->created = common_sql_now(); $oprofile->modified = common_sql_now(); if ($object->type == ActivityObject::PERSON) { $profile = new Profile(); $profile->created = common_sql_now(); self::updateProfile($profile, $object, $hints); $oprofile->profile_id = $profile->insert(); if ($oprofile->profile_id === false) { // TRANS: Server exception. throw new ServerException(_m('Cannot save local profile.')); } } else { if ($object->type == ActivityObject::GROUP) { $profile = new Profile(); $profile->query('BEGIN'); $group = new User_group(); $group->uri = $homeuri; $group->created = common_sql_now(); self::updateGroup($group, $object, $hints); // TODO: We should do this directly in User_group->insert()! // currently it's duplicated in User_group->update() // AND User_group->register()!!! $fields = array('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; $oprofile->group_id = $group->insert(); if ($oprofile->group_id === false) { $profile->query('ROLLBACK'); // TRANS: Server exception. throw new ServerException(_m('Cannot save local profile.')); } $profile->query('COMMIT'); } else { if ($object->type == ActivityObject::_LIST) { $ptag = new Profile_list(); $ptag->uri = $homeuri; $ptag->created = common_sql_now(); self::updatePeopletag($ptag, $object, $hints); $oprofile->peopletag_id = $ptag->insert(); if ($oprofile->peopletag_id === false) { // TRANS: Server exception. throw new ServerException(_m('Cannot save local list.')); } } } } $ok = $oprofile->insert(); if ($ok === false) { // TRANS: Server exception. throw new ServerException(_m('Cannot save OStatus profile.')); } $avatar = self::getActivityObjectAvatar($object, $hints); if ($avatar) { try { $oprofile->updateAvatar($avatar); } catch (Exception $ex) { // Profile is saved, but Avatar is messed up. We're // just going to continue. common_log(LOG_WARNING, "Exception saving OStatus profile avatar: " . $ex->getMessage()); } } return $oprofile; }
/** * Create local ostatus_profile and profile/user_group entries for * the provided remote user or group. * This should never return null -- you will either get an object or * an exception will be thrown. * * @param ActivityObject $object * @param array $hints * * @return Ostatus_profile */ protected static function createActivityObjectProfile($object, $hints = array()) { $homeuri = $object->id; $discover = false; if (!$homeuri) { common_log(LOG_DEBUG, __METHOD__ . " empty actor profile URI: " . var_export($activity, true)); // TRANS: Exception. throw new Exception(_m('No profile URI.')); } $user = User::staticGet('uri', $homeuri); if ($user) { // TRANS: Exception. throw new Exception(_m('Local user cannot be referenced as remote.')); } if (OStatusPlugin::localGroupFromUrl($homeuri)) { // TRANS: Exception. throw new Exception(_m('Local group cannot be referenced as remote.')); } $ptag = Profile_list::staticGet('uri', $homeuri); if ($ptag) { $local_user = User::staticGet('id', $ptag->tagger); if (!empty($local_user)) { // TRANS: Exception. throw new Exception(_m('Local list cannot be referenced as remote.')); } } if (array_key_exists('feedurl', $hints)) { $feeduri = $hints['feedurl']; } else { $discover = new FeedDiscovery(); $feeduri = $discover->discoverFromURL($homeuri); } if (array_key_exists('salmon', $hints)) { $salmonuri = $hints['salmon']; } else { if (!$discover) { $discover = new FeedDiscovery(); $discover->discoverFromFeedURL($hints['feedurl']); } $salmonuri = $discover->getAtomLink(Salmon::NS_REPLIES); } if (array_key_exists('hub', $hints)) { $huburi = $hints['hub']; } else { if (!$discover) { $discover = new FeedDiscovery(); $discover->discoverFromFeedURL($hints['feedurl']); } $huburi = $discover->getHubLink(); } if (!$huburi && !common_config('feedsub', 'fallback_hub')) { // We can only deal with folks with a PuSH hub throw new FeedSubNoHubException(); } $oprofile = new Ostatus_profile(); $oprofile->uri = $homeuri; $oprofile->feeduri = $feeduri; $oprofile->salmonuri = $salmonuri; $oprofile->created = common_sql_now(); $oprofile->modified = common_sql_now(); if ($object->type == ActivityObject::PERSON) { $profile = new Profile(); $profile->created = common_sql_now(); self::updateProfile($profile, $object, $hints); $oprofile->profile_id = $profile->insert(); if (!$oprofile->profile_id) { // TRANS: Server exception. throw new ServerException(_m('Cannot save local profile.')); } } else { if ($object->type == ActivityObject::GROUP) { $group = new User_group(); $group->uri = $homeuri; $group->created = common_sql_now(); self::updateGroup($group, $object, $hints); $oprofile->group_id = $group->insert(); if (!$oprofile->group_id) { // TRANS: Server exception. throw new ServerException(_m('Cannot save local profile.')); } } else { if ($object->type == ActivityObject::_LIST) { $ptag = new Profile_list(); $ptag->uri = $homeuri; $ptag->created = common_sql_now(); self::updatePeopletag($ptag, $object, $hints); $oprofile->peopletag_id = $ptag->insert(); if (!$oprofile->peopletag_id) { // TRANS: Server exception. throw new ServerException(_m('Cannot save local list.')); } } } } $ok = $oprofile->insert(); if (!$ok) { // TRANS: Server exception. throw new ServerException(_m('Cannot save OStatus profile.')); } $avatar = self::getActivityObjectAvatar($object, $hints); if ($avatar) { try { $oprofile->updateAvatar($avatar); } catch (Exception $ex) { // Profile is saved, but Avatar is messed up. We're // just going to continue. common_log(LOG_WARNING, "Exception saving OStatus profile avatar: " . $ex->getMessage()); } } return $oprofile; }
exit(1); } $uri = $args[0]; $oprofile = Ostatus_profile::staticGet('uri', $uri); if (!$oprofile) { print "No OStatus remote profile known for URI {$uri}\n"; exit(1); } print "Old profile state for {$oprofile->uri}\n"; showProfile($oprofile); print "\n"; print "Re-running feed discovery for profile URL {$oprofile->uri}\n"; // @fixme will bork where the URI isn't the profile URL for now $discover = new FeedDiscovery(); $feedurl = $discover->discoverFromURL($oprofile->uri); $huburi = $discover->getHubLink(); $salmonuri = $discover->getAtomLink(Salmon::NS_REPLIES); print " Feed URL: {$feedurl}\n"; print " Hub URL: {$huburi}\n"; print " Salmon URL: {$salmonuri}\n"; if ($feedurl != $oprofile->feeduri || $salmonuri != $oprofile->salmonuri) { print "\n"; print "Updating...\n"; // @fixme update keys :P #$orig = clone($oprofile); #$oprofile->feeduri = $feedurl; #$oprofile->salmonuri = $salmonuri; #$ok = $oprofile->update($orig); $ok = $oprofile->query('UPDATE ostatus_profile SET ' . 'feeduri=\'' . $oprofile->escape($feedurl) . '\',' . 'salmonuri=\'' . $oprofile->escape($salmonuri) . '\' ' . 'WHERE uri=\'' . $oprofile->escape($uri) . '\''); if (!$ok) { print "Failed to update profile record...\n";
/** * @param string $feeduri * @return FeedSub * @throws FeedSubException if feed is invalid or lacks PuSH setup */ public static function ensureFeed($feeduri) { $current = self::staticGet('uri', $feeduri); if ($current) { return $current; } $discover = new FeedDiscovery(); $discover->discoverFromFeedURL($feeduri); $huburi = $discover->getHubLink(); if (!$huburi && !common_config('feedsub', 'fallback_hub')) { throw new FeedSubNoHubException(); } $feedsub = new FeedSub(); $feedsub->uri = $feeduri; $feedsub->huburi = $huburi; $feedsub->sub_state = 'inactive'; $feedsub->created = common_sql_now(); $feedsub->modified = common_sql_now(); $result = $feedsub->insert(); if (empty($result)) { throw new FeedDBException($feedsub); } return $feedsub; }