]> git.mxchange.org Git - friendica.git/blobdiff - mod/salmon.php
salmon protocol changes magicsig draft-experimental, fixes to hostxrd
[friendica.git] / mod / salmon.php
index 2ae6aa6287e1027201b8abccbbc136e4912c7000..300ad87466806cf2d982673f66b4d30d6b4b2f03 100644 (file)
@@ -1,14 +1,11 @@
 <?php
 
 
-// TODO: 
-// add relevant contacts so they can use this
-
 // There is a lot of debug stuff in here because this is quite a
 // complicated process to try and sort out. 
 
 require_once('include/salmon.php');
-require_once('simplepie/simplepie.inc');
+require_once('library/simplepie/simplepie.inc');
 
 function salmon_return($val) {
 
@@ -75,12 +72,16 @@ function salmon_post(&$a) {
        $encoding = $base->encoding;
        $alg = $base->alg;
 
-       // If we're talking to status.net or one of their ilk, they aren't following the magic envelope spec
-       // and only signed the data element. We'll be nice and let them validate anyway. 
+       // Salmon magic signatures have evolved and there is no way of knowing ahead of time which
+       // flavour we have. We'll try and verify it regardless.
 
        $stnet_signed_data = $data;
+
        $signed_data = $data  . '.' . base64url_encode($type) . '.' . base64url_encode($encoding) . '.' . base64url_encode($alg);
 
+       $compliant_format = str_replace('=','',$signed_data);
+
+
        // decode the data
        $data = base64url_decode($data);
 
@@ -89,7 +90,7 @@ function salmon_post(&$a) {
 
        // Create a fake feed wrapper so simplepie doesn't choke
 
-       $tpl = load_view_file('view/fake_feed.tpl');
+       $tpl = get_markup_template('fake_feed.tpl');
        
        $base = substr($data,strpos($data,'<entry'));
 
@@ -133,9 +134,9 @@ function salmon_post(&$a) {
 
        // Setup RSA stuff to verify the signature
 
-       set_include_path(get_include_path() . PATH_SEPARATOR . 'phpsec');
+       set_include_path(get_include_path() . PATH_SEPARATOR . 'library/phpsec');
 
-       require_once('phpsec/Crypt/RSA.php');
+       require_once('library/phpsec/Crypt/RSA.php');
 
        $key_info = explode('.',$key);
 
@@ -153,13 +154,16 @@ function salmon_post(&$a) {
     $rsa->exponent = new Math_BigInteger($e, 256);
 
        // We should have everything we need now. Let's see if it verifies.
-       // If it fails with the proper data format, try again using just the data
-       // (e.g. status.net)
 
-    $verify = $rsa->verify($signed_data,$signature);
+    $verify = $rsa->verify($compliant_format,$signature);
 
        if(! $verify) {
-               logger('mod-salmon: message did not verify using protocol. Trying statusnet hack.');
+               logger('mod-salmon: message did not verify using protocol. Trying padding hack.');
+           $verify = $rsa->verify($signed_data,$signature);
+    }
+
+       if(! $verify) {
+               logger('mod-salmon: message did not verify using padding. Trying old statusnet hack.');
            $verify = $rsa->verify($stnet_signed_data,$signature);
     }
 
@@ -177,7 +181,7 @@ function salmon_post(&$a) {
        *
        */
 
-       $r = q("SELECT * FROM `contact` WHERE `network` = 'stat' AND ( `url` = '%s' OR `lrdd` = '%s') 
+       $r = q("SELECT * FROM `contact` WHERE `network` = 'stat' AND ( `url` = '%s' OR `alias` = '%s') 
                AND `uid` = %d LIMIT 1",
                dbesc($author_link),
                dbesc($author_link),
@@ -186,7 +190,11 @@ function salmon_post(&$a) {
        if(! count($r)) {
                logger('mod-salmon: Author unknown to us.');
        }       
-       if((count($r)) && ($r[0]['readonly'])) {
+
+       // is this a follower? Or have we ignored the person?
+       // If so we can not accept this post.
+
+       if((count($r)) && (($r[0]['readonly']) || ($r[0]['rel'] == REL_VIP) || ($r[0]['blocked']))) {
                logger('mod-salmon: Ignoring this author.');
                salmon_return(202);
                // NOTREACHED
@@ -199,7 +207,12 @@ function salmon_post(&$a) {
 
        $hub = '';
 
-       // consume_feed will only accept a follow activity from this person if there is no contact record.
+       /**
+        *
+        * anti-spam measure: consume_feed will accept a follow activity from 
+        * this person (and nothing else) if there is no existing contact record.
+        *
+        */
 
        $contact_rec = ((count($r)) ? $r[0] : null);