123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310 |
- <?php
- if (!defined('GNUSOCIAL')) { exit(1); }
- class SalmonAction extends Action
- {
- protected $needPost = true;
- protected $oprofile = null;
- protected $actor = null;
- var $format = 'text';
- var $xml = null;
- var $activity = null;
- var $target = null;
- protected function prepare(array $args=array())
- {
- GNUsocial::setApi(true);
- parent::prepare($args);
- if (!isset($_SERVER['CONTENT_TYPE'])) {
-
- throw new ClientException(_m('Salmon requires a Content-type header.'));
- }
- $envxml = null;
- switch ($_SERVER['CONTENT_TYPE']) {
- case 'application/magic-envelope+xml':
- $envxml = file_get_contents('php://input');
- break;
- case 'application/x-www-form-urlencoded':
- $envxml = Magicsig::base64_url_decode($this->trimmed('xml'));
- break;
- default:
-
- throw new ClientException(_m('Salmon requires "application/magic-envelope+xml". For Diaspora we also accept "application/x-www-form-urlencoded" with an "xml" parameter.', 415));
- }
- if (empty($envxml)) {
- throw new ClientException('No magic envelope supplied in POST.');
- }
- try {
- $magic_env = new MagicEnvelope($envxml);
- $entry = $magic_env->getPayload();
- $this->activity = new Activity($entry->documentElement);
- if (empty($this->activity->actor->id)) {
- common_log(LOG_ERR, "broken actor: " . var_export($this->activity->actor->id, true));
- common_log(LOG_ERR, "activity with no actor: " . var_export($this->activity, true));
-
- throw new ClientException(_m('Activity in salmon slap has no actor id.'));
- }
-
- $this->ensureProfiles();
- } catch (Exception $e) {
- common_debug('Salmon envelope parsing failed with: '.$e->getMessage());
-
- throw new ClientException($e->getMessage());
- }
-
- $magic_env->verify($this->actor);
- common_debug('Salmon slap is carrying activity URI=='._ve($this->activity->id));
- return true;
- }
-
- protected function handle()
- {
- parent::handle();
- assert($this->activity instanceof Activity);
- assert($this->target instanceof Profile);
- common_log(LOG_DEBUG, "Got a " . $this->activity->verb);
- try {
- $options = [ 'source' => 'ostatus' ];
- common_debug('Save salmon slap directly with Notice::saveActivity for actor=='.$this->actor->getID());
- $stored = Notice::saveActivity($this->activity, $this->actor, $options);
- common_debug('Save salmon slap finished, notice id=='.$stored->getID());
- return true;
- } catch (AlreadyFulfilledException $e) {
-
-
-
- common_log(LOG_INFO, 'Salmon slap carried an event which had already been fulfilled.');
- return true;
- } catch (NoticeSaveException $e) {
- common_debug('Notice::saveActivity did not save our '._ve($this->activity->verb).' activity, trying old-fashioned salmon saving.');
- }
- try {
- if (Event::handle('StartHandleSalmonTarget', array($this->activity, $this->target)) &&
- Event::handle('StartHandleSalmon', array($this->activity))) {
- switch ($this->activity->verb) {
- case ActivityVerb::POST:
- $this->handlePost();
- break;
- case ActivityVerb::SHARE:
- $this->handleShare();
- break;
- case ActivityVerb::FOLLOW:
- case ActivityVerb::FRIEND:
- $this->handleFollow();
- break;
- case ActivityVerb::UNFOLLOW:
- $this->handleUnfollow();
- break;
- case ActivityVerb::JOIN:
- $this->handleJoin();
- break;
- case ActivityVerb::LEAVE:
- $this->handleLeave();
- break;
- case ActivityVerb::TAG:
- $this->handleTag();
- break;
- case ActivityVerb::UNTAG:
- $this->handleUntag();
- break;
- case ActivityVerb::UPDATE_PROFILE:
- $this->handleUpdateProfile();
- break;
- default:
-
- throw new ClientException(_m('Unrecognized activity type.'));
- }
- Event::handle('EndHandleSalmon', array($this->activity));
- Event::handle('EndHandleSalmonTarget', array($this->activity, $this->target));
- }
- } catch (AlreadyFulfilledException $e) {
-
-
-
- common_log(LOG_INFO, 'Salmon slap carried an event which had already been fulfilled.');
- }
- }
- function handlePost()
- {
-
- throw new ClientException(_m('This target does not understand posts.'));
- }
- function handleFollow()
- {
-
- throw new ClientException(_m('This target does not understand follows.'));
- }
- function handleUnfollow()
- {
-
- throw new ClientException(_m('This target does not understand unfollows.'));
- }
- function handleShare()
- {
-
- throw new ClientException(_m('This target does not understand share events.'));
- }
- function handleJoin()
- {
-
- throw new ClientException(_m('This target does not understand joins.'));
- }
- function handleLeave()
- {
-
- throw new ClientException(_m('This target does not understand leave events.'));
- }
- function handleTag()
- {
-
- throw new ClientException(_m('This target does not understand list events.'));
- }
- function handleUntag()
- {
-
- throw new ClientException(_m('This target does not understand unlist events.'));
- }
-
- function handleUpdateProfile()
- {
- $oprofile = Ostatus_profile::getActorProfile($this->activity);
- if ($oprofile instanceof Ostatus_profile) {
- common_log(LOG_INFO, "Got a profile-update ping from $oprofile->uri");
- $oprofile->updateFromActivityObject($this->activity->actor);
- } else {
- common_log(LOG_INFO, "Ignoring profile-update ping from unknown " . $this->activity->actor->id);
- }
- }
- function ensureProfiles()
- {
- try {
- $this->oprofile = Ostatus_profile::getActorProfile($this->activity);
- if (!$this->oprofile instanceof Ostatus_profile) {
- throw new UnknownUriException($this->activity->actor->id);
- }
- } catch (UnknownUriException $e) {
-
-
-
-
-
-
-
-
-
-
-
-
- common_debug('No local Profile object found for a magicsigned activity author URI: '.$e->object_uri);
- $disco = new Discovery();
- $xrd = $disco->lookup($e->object_uri);
-
-
- $all_ids = array_merge(array($xrd->subject), $xrd->aliases);
- if (!in_array($e->object_uri, $all_ids)) {
- common_debug('The activity author URI we got was not listed itself when doing discovery on it.');
- throw $e;
- }
-
- foreach ($all_ids as $aliased_uri) {
- $oprofile = Ostatus_profile::getKV('uri', $aliased_uri);
- if (!$oprofile instanceof Ostatus_profile) {
- continue;
- }
-
-
- common_debug('Found a local Ostatus_profile for "'.$e->object_uri.'" with this URI: '.$aliased_uri);
-
-
- $xrd = $disco->lookup($oprofile->getUri());
- $doublecheck_aliases = array_merge(array($xrd->subject), $xrd->aliases);
- common_debug('Trying to match known "'.$aliased_uri.'" against its returned aliases: '.implode(' ', $doublecheck_aliases));
-
-
-
- if (in_array($e->object_uri, $doublecheck_aliases)) {
- $oprofile->updateUriKeys($e->object_uri, DiscoveryHints::fromXRD($xrd));
- $this->oprofile = $oprofile;
- break;
- }
- }
-
- if (!$this->oprofile instanceof Ostatus_profile) {
- common_debug("We do not have a local profile to connect to this activity's author. Let's create one.");
-
- $this->oprofile = Ostatus_profile::ensureActivityObjectProfile($this->activity->actor);
- }
- }
- assert($this->oprofile instanceof Ostatus_profile);
- $this->actor = $this->oprofile->localProfile();
- }
- function saveNotice()
- {
- if (!$this->oprofile instanceof Ostatus_profile) {
- common_debug('Ostatus_profile missing in ' . get_class(). ' profile: '.var_export($this->profile, true));
- }
- return $this->oprofile->processPost($this->activity, 'salmon');
- }
- }
|