]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - plugins/OStatus/lib/salmonaction.php
More verbose salmon debugging
[quix0rs-gnu-social.git] / plugins / OStatus / lib / salmonaction.php
index eb3a62da322e936a4ac4b995618a6d4802f3b8da..13f49f4effa6fcfa4cacba5e7c1d58ba299e681a 100644 (file)
@@ -37,17 +37,31 @@ class SalmonAction extends Action
 
     protected function prepare(array $args=array())
     {
-        StatusNet::setApi(true); // Send smaller error pages
+        GNUsocial::setApi(true); // Send smaller error pages
 
         parent::prepare($args);
 
-        if (!isset($_SERVER['CONTENT_TYPE']) || $_SERVER['CONTENT_TYPE'] != 'application/magic-envelope+xml') {
-            // TRANS: Client error. Do not translate "application/magic-envelope+xml".
-            $this->clientError(_m('Salmon requires "application/magic-envelope+xml".'));
+        if (!isset($_SERVER['CONTENT_TYPE'])) {
+            // TRANS: Client error. Do not translate "Content-type"
+            $this->clientError(_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:
+            // TRANS: Client error. Do not translate the quoted "application/[type]" strings.
+            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 {
-            $envxml = file_get_contents('php://input');
             $magic_env = new MagicEnvelope($envxml);   // parse incoming XML as a MagicEnvelope
 
             $entry = $magic_env->getPayload();  // Not cryptographically verified yet!
@@ -56,23 +70,20 @@ class SalmonAction extends Action
                 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));
                 // TRANS: Exception.
-                throw new Exception(_m('Received a salmon slap from unidentified actor.'));
+                throw new ClientException(_m('Activity in salmon slap has no actor id.'));
             }
-            $profile = $this->profileFromActor($this->activity->actor);
+            // ensureProfiles sets $this->actor and $this->oprofile
+            $this->ensureProfiles();
         } catch (Exception $e) {
             common_debug('Salmon envelope parsing failed with: '.$e->getMessage());
-            $this->clientError($e->getMessage());
+            // convert exception to ClientException
+            throw new ClientException($e->getMessage());
         }
 
-        // Cryptographic verification test
-        if (!$magic_env->verify($profile)) {
-            common_log(LOG_DEBUG, "Salmon signature verification failed.");
-            // TRANS: Client error.
-            $this->clientError(_m('Salmon signature verification failed.'));
-        }
+        // Cryptographic verification test, throws exception on failure
+        $magic_env->verify($this->actor);
 
-        $this->actor    = $profile;
-        $this->oprofile = Ostatus_profile::fromProfile($profile);
+        common_debug('Salmon slap is carrying activity URI=='._ve($this->activity->id));
 
         return true;
     }
@@ -85,7 +96,27 @@ class SalmonAction extends Action
     {
         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) {
+            // The action's results are already fulfilled. Maybe it was a
+            // duplicate? Maybe someone's database is out of sync?
+            // Let's just accept it and move on.
+            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))) {
@@ -196,10 +227,13 @@ class SalmonAction extends Action
         }
     }
 
-    function profileFromActor(ActivityObject $actor)
+    function ensureProfiles()
     {
         try {
-            $profile = Profile::fromUri($actor->id);
+            $this->oprofile = Ostatus_profile::getActorProfile($this->activity);
+            if (!$this->oprofile instanceof Ostatus_profile) {
+                throw new UnknownUriException($this->activity->actor->id);
+            }
         } catch (UnknownUriException $e) {
             // Apparently we didn't find the Profile object based on our URI,
             // so OStatus doesn't have it with this URI in ostatus_profile.
@@ -244,26 +278,30 @@ class SalmonAction extends Action
                 // Step 4: Is the newly introduced https://example.com/user/1 URI in the list of aliases
                 //         presented by http://example.com/user/1 (i.e. do they both say they are the same identity?)
                 if (in_array($e->object_uri, $doublecheck_aliases)) {
-                    common_debug('These identities both say they are each other: "'.$aliased_uri.'" and "'.$e->object_uri);
-                    $profile = $oprofile->localProfile();
+                    $oprofile->updateUriKeys($e->object_uri, DiscoveryHints::fromXRD($xrd));
+                    $this->oprofile = $oprofile;
                     break;  // don't iterate through aliases anymore
                 }
             }
-            // We might end up here after $all_ids is iterated through without a $profile value,
-            // so after this catch we'll have to make sure we don't return a non-Profile value.
-        }
-        if (!$profile instanceof Profile) {
-            common_debug("We do not have a local profile to connect to this activity's author. Let's create one.");
-            // ensureActivityObjectProfile throws exception on failure
-            $oprofile = Ostatus_profile::createActivityObjectProfile($actor);
-            $profile = $oprofile->localProfile();
+
+            // We might end up here after $all_ids is iterated through without a $this->oprofile value,
+            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.");
+                // ensureActivityObjectProfile throws exception on failure
+                $this->oprofile = Ostatus_profile::ensureActivityObjectProfile($this->activity->actor);
+            }
         }
-        assert($profile instanceof Profile);
-        return $profile;
+
+        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');
     }
 }