]> git.mxchange.org Git - friendica.git/commitdiff
Salmon move to src
authorAdam Magness <adam.magness@gmail.com>
Sat, 2 Dec 2017 14:32:45 +0000 (09:32 -0500)
committerAdam Magness <adam.magness@gmail.com>
Sat, 2 Dec 2017 14:32:45 +0000 (09:32 -0500)
Salmon class, update function name and function calls. Some standards as well.

include/crypto.php
include/follow.php
include/items.php
include/salmon.php [deleted file]
mod/post.php
mod/receive.php
mod/salmon.php
src/Object/Contact.php
src/Protocol/Salmon.php [new file with mode: 0644]
src/Worker/Notifier.php
src/Worker/Queue.php

index 703439baa1615b61365ece621ee28e9d7836b4a7..dfc44c1792dfc4f056e7cc05cbe935a374a42f61 100644 (file)
@@ -91,9 +91,9 @@ function metopem($m, $e) {
        return $key;
 }
 
-function pubrsatome($key,&$m,&$e) {
-       require_once('library/asn1.php');
-       require_once('include/salmon.php');
+function pubrsatome($key, &$m, &$e)
+{
+       require_once 'library/asn1.php';
 
        $lines = explode("\n", $key);
        unset($lines[0]);
@@ -117,8 +117,8 @@ function pemtorsa($key) {
        return metorsa($m, $e);
 }
 
-function pemtome($key, &$m, &$e) {
-       require_once('include/salmon.php');
+function pemtome($key, &$m, &$e)
+{
        $lines = explode("\n", $key);
        unset($lines[0]);
        unset($lines[count($lines)]);
index ac07c4085eaff752d8bc1e25643a3a952a34cdca..14b9c68ed59fd3476154a2ec367ab84d63adde71 100644 (file)
@@ -12,9 +12,9 @@ use Friendica\Object\Contact;
 use Friendica\Protocol\Diaspora;
 use Friendica\Protocol\OStatus;
 use Friendica\Protocol\PortableContact;
+use Friendica\Protocol\Salmon;
 
 require_once 'include/group.php';
-require_once 'include/salmon.php';
 
 function update_contact($id) {
        /*
@@ -268,7 +268,7 @@ function new_contact($uid, $url, $interactive = false, $network = '') {
                        $item['verb'] = ACTIVITY_FOLLOW;
                        $item['follow'] = $contact["url"];
                        $slap = OStatus::salmon($item, $r[0]);
-                       slapper($r[0], $contact['notify'], $slap);
+                       Salmon::slapper($r[0], $contact['notify'], $slap);
                }
 
                if ($contact['network'] == NETWORK_DIASPORA) {
index 7cbcfaaaf53e36536beaf7562e3e38b2bd6b9b31..36a7ac2a8c0bc6765258afa10058706c05c7836e 100644 (file)
@@ -17,7 +17,6 @@ use Friendica\Util\Lock;
 
 require_once 'include/bbcode.php';
 require_once 'include/oembed.php';
-require_once 'include/salmon.php';
 require_once 'include/crypto.php';
 require_once 'include/tags.php';
 require_once 'include/files.php';
diff --git a/include/salmon.php b/include/salmon.php
deleted file mode 100644 (file)
index 264b92e..0000000
+++ /dev/null
@@ -1,185 +0,0 @@
-<?php
-/**
- * @file include/salmon.php
- */
-use Friendica\Network\Probe;
-use Friendica\Util\XML;
-
-require_once 'include/crypto.php';
-
-function get_salmon_key($uri, $keyhash)
-{
-       $ret = array();
-
-       logger('Fetching salmon key for '.$uri);
-
-       $arr = Probe::lrdd($uri);
-
-       if (is_array($arr)) {
-               foreach ($arr as $a) {
-                       if ($a['@attributes']['rel'] === 'magic-public-key') {
-                               $ret[] = $a['@attributes']['href'];
-                       }
-               }
-       } else {
-               return '';
-       }
-
-       // We have found at least one key URL
-       // If it's inline, parse it - otherwise get the key
-
-       if (count($ret) > 0) {
-               for ($x = 0; $x < count($ret); $x ++) {
-                       if (substr($ret[$x], 0, 5) === 'data:') {
-                               if (strstr($ret[$x], ',')) {
-                                       $ret[$x] = substr($ret[$x], strpos($ret[$x], ',') + 1);
-                               } else {
-                                       $ret[$x] = substr($ret[$x], 5);
-                               }
-                       } elseif (normalise_link($ret[$x]) == 'http://') {
-                               $ret[$x] = fetch_url($ret[$x]);
-                       }
-               }
-       }
-
-
-       logger('Key located: ' . print_r($ret, true));
-
-       if (count($ret) == 1) {
-               // We only found one one key so we don't care if the hash matches.
-               // If it's the wrong key we'll find out soon enough because
-               // message verification will fail. This also covers some older
-               // software which don't supply a keyhash. As long as they only
-               // have one key we'll be right.
-
-               return $ret[0];
-       } else {
-               foreach ($ret as $a) {
-                       $hash = base64url_encode(hash('sha256', $a));
-                       if ($hash == $keyhash) {
-                               return $a;
-                       }
-               }
-       }
-
-       return '';
-}
-
-
-
-function slapper($owner, $url, $slap)
-{
-       // does contact have a salmon endpoint?
-
-       if (! strlen($url)) {
-               return;
-       }
-
-       if (! $owner['sprvkey']) {
-               logger(sprintf("user '%s' (%d) does not have a salmon private key. Send failed.",
-               $owner['username'], $owner['uid']));
-               return;
-       }
-
-       logger('slapper called for '.$url.'. Data: ' . $slap);
-
-       // create a magic envelope
-
-       $data      = base64url_encode($slap);
-       $data_type = 'application/atom+xml';
-       $encoding  = 'base64url';
-       $algorithm = 'RSA-SHA256';
-       $keyhash   = base64url_encode(hash('sha256', salmon_key($owner['spubkey'])), true);
-
-       $precomputed = '.' . base64url_encode($data_type) . '.' . base64url_encode($encoding) . '.' . base64url_encode($algorithm);
-
-       // GNU Social format
-       $signature   = base64url_encode(rsa_sign($data . $precomputed, $owner['sprvkey']));
-
-       // Compliant format
-       $signature2  = base64url_encode(rsa_sign(str_replace('=', '', $data . $precomputed), $owner['sprvkey']));
-
-       // Old Status.net format
-       $signature3  = base64url_encode(rsa_sign($data, $owner['sprvkey']));
-
-       // At first try the non compliant method that works for GNU Social
-       $xmldata = array("me:env" => array("me:data" => $data,
-                       "@attributes" => array("type" => $data_type),
-                       "me:encoding" => $encoding,
-                       "me:alg" => $algorithm,
-                       "me:sig" => $signature,
-                       "@attributes2" => array("key_id" => $keyhash)));
-
-       $namespaces = array("me" => "http://salmon-protocol.org/ns/magic-env");
-
-       $salmon = XML::fromArray($xmldata, $xml, false, $namespaces);
-
-       // slap them
-       post_url($url, $salmon, array(
-               'Content-type: application/magic-envelope+xml',
-               'Content-length: ' . strlen($salmon)
-       ));
-
-       $a = get_app();
-       $return_code = $a->get_curl_code();
-
-       // check for success, e.g. 2xx
-
-       if ($return_code > 299) {
-               logger('GNU Social salmon failed. Falling back to compliant mode');
-
-               // Now try the compliant mode that normally isn't used for GNU Social
-               $xmldata = array("me:env" => array("me:data" => $data,
-                               "@attributes" => array("type" => $data_type),
-                               "me:encoding" => $encoding,
-                               "me:alg" => $algorithm,
-                               "me:sig" => $signature2,
-                               "@attributes2" => array("key_id" => $keyhash)));
-
-               $namespaces = array("me" => "http://salmon-protocol.org/ns/magic-env");
-
-               $salmon = XML::fromArray($xmldata, $xml, false, $namespaces);
-
-               // slap them
-               post_url($url, $salmon, array(
-                       'Content-type: application/magic-envelope+xml',
-                       'Content-length: ' . strlen($salmon)
-               ));
-               $return_code = $a->get_curl_code();
-       }
-
-       if ($return_code > 299) {
-               logger('compliant salmon failed. Falling back to old status.net');
-
-               // Last try. This will most likely fail as well.
-               $xmldata = array("me:env" => array("me:data" => $data,
-                               "@attributes" => array("type" => $data_type),
-                               "me:encoding" => $encoding,
-                               "me:alg" => $algorithm,
-                               "me:sig" => $signature3,
-                               "@attributes2" => array("key_id" => $keyhash)));
-
-               $namespaces = array("me" => "http://salmon-protocol.org/ns/magic-env");
-
-               $salmon = XML::fromArray($xmldata, $xml, false, $namespaces);
-
-               // slap them
-               post_url($url, $salmon, array(
-                       'Content-type: application/magic-envelope+xml',
-                       'Content-length: ' . strlen($salmon))
-               );
-               $return_code = $a->get_curl_code();
-       }
-
-       logger('slapper for '.$url.' returned ' . $return_code);
-
-       if (! $return_code) {
-               return -1;
-       }
-
-       if (($return_code == 503) && (stristr($a->get_curl_headers(), 'retry-after'))) {
-               return -1;
-       }
-
-       return ((($return_code >= 200) && ($return_code < 300)) ? 0 : 1);
-}
index 8e279644bd2a7e70298a7220d73de0929e8c1ab9..58d7695d41ef4975283445809f226e4758a40292 100644 (file)
@@ -1,54 +1,55 @@
 <?php
-
 /**
- * Zot endpoint
+ * @file mod/post.php
+ * @brief Zot endpoint
  */
 
 use Friendica\App;
 use Friendica\Database\DBM;
+use dba;
 
-require_once('include/salmon.php');
-require_once('include/crypto.php');
+require_once 'include/crypto.php';
 // not yet ready for prime time
 //require_once('include/zot.php');
 
-function post_post(App $a) {
-
+/**
+ * @param object $a App
+ * @return void
+ */
+function post_post(App $a)
+{
        $bulk_delivery = false;
 
        if ($a->argc == 1) {
                $bulk_delivery = true;
-       }
-       else {
+       } else {
                $nickname = $a->argv[2];
-               $r = q("SELECT * FROM `user` WHERE `nickname` = '%s'
-                               AND `account_expired` = 0 AND `account_removed` = 0 LIMIT 1",
-                       dbesc($nickname)
-               );
+               $r = dba::select('user', array(), array('nickname' => $nickname, 'account_expired' => 0, 'account_removed' => 0), array('limit' => 1));
                if (! DBM::is_result($r)) {
                        http_status_exit(500);
                }
 
-               $importer = $r[0];
+               $importer = $r;
        }
 
        $xml = file_get_contents('php://input');
 
        logger('mod-post: new zot: ' . $xml, LOGGER_DATA);
 
-       if(! $xml)
+       if (! $xml) {
                http_status_exit(500);
+       }
 
-       $msg = zot_decode($importer,$xml);
+       $msg = zot_decode($importer, $xml);
 
-       logger('mod-post: decoded msg: ' . print_r($msg,true), LOGGER_DATA);
+       logger('mod-post: decoded msg: ' . print_r($msg, true), LOGGER_DATA);
 
-       if(! is_array($msg))
+       if (! is_array($msg)) {
                http_status_exit(500);
+       }
 
        $ret = 0;
-       $ret = zot_incoming($bulk_delivery, $importer,$msg);
+       $ret = zot_incoming($bulk_delivery, $importer, $msg);
        http_status_exit(($ret) ? $ret : 200);
        // NOTREACHED
 }
-
index 8241325bf99398941f8227da1725fda6778836a5..467a0d00a5982a3622c45b8bef04b3fda2bca7d9 100644 (file)
@@ -1,7 +1,7 @@
 <?php
-
 /**
- * Diaspora endpoint
+ * @file mod/receive.php
+ * @brief Diaspora endpoint
  */
 
 use Friendica\App;
@@ -9,10 +9,14 @@ use Friendica\Core\Config;
 use Friendica\Database\DBM;
 use Friendica\Protocol\Diaspora;
 
-require_once('include/salmon.php');
-require_once('include/crypto.php');
+require_once 'include/crypto.php';
 
-function receive_post(App $a) {
+/**
+ * @param object $a App
+ * @return void
+ */
+function receive_post(App $a)
+{
        $enabled = intval(Config::get('system', 'diaspora_enabled'));
        if (!$enabled) {
                logger('mod-diaspora: disabled');
@@ -44,7 +48,7 @@ function receive_post(App $a) {
 
        if (!$xml) {
                $postdata = file_get_contents("php://input");
-               if ($postdata == '') {
+               if ($postdata == '') {
                        http_status_exit(500);
                }
 
@@ -80,4 +84,3 @@ function receive_post(App $a) {
        http_status_exit(($ret) ? 200 : 500);
        // NOTREACHED
 }
-
index 919204a48b2f65fbb4348c0842c480958d42c0fc..4d8b130f94cca342700f343a58299b8a7b8651b8 100644 (file)
@@ -6,8 +6,8 @@ use Friendica\App;
 use Friendica\Core\PConfig;
 use Friendica\Database\DBM;
 use Friendica\Protocol\OStatus;
+use Friendica\Protocol\Salmon;
 
-require_once 'include/salmon.php';
 require_once 'include/crypto.php';
 require_once 'include/items.php';
 require_once 'include/follow.php';
@@ -103,7 +103,7 @@ function salmon_post(App $a) {
 
        logger('mod-salmon: Fetching key for ' . $author_link);
 
-       $key = get_salmon_key($author_link,$keyhash);
+       $key = Salmon::getKey($author_link, $keyhash);
 
        if(! $key) {
                logger('mod-salmon: Could not retrieve author key.');
index 72f287a3ef04ea35187e5d87a0d085ff22c01ea3..23c3649df70b44c8e891888ae8d7214d9284dde1 100644 (file)
@@ -17,6 +17,7 @@ use Friendica\Object\Photo;
 use Friendica\Protocol\Diaspora;
 use Friendica\Protocol\DFRN;
 use Friendica\Protocol\OStatus;
+use Friendica\Protocol\Salmon;
 use dba;
 
 require_once 'boot.php';
@@ -71,8 +72,7 @@ class Contact extends BaseObject
                        $slap = OStatus::salmon($item, $user);
 
                        if ((x($contact, 'notify')) && (strlen($contact['notify']))) {
-                               require_once 'include/salmon.php';
-                               slapper($user, $contact['notify'], $slap);
+                               Salmon::slapper($user, $contact['notify'], $slap);
                        }
                } elseif ($contact['network'] === NETWORK_DIASPORA) {
                        Diaspora::sendUnshare($user, $contact);
diff --git a/src/Protocol/Salmon.php b/src/Protocol/Salmon.php
new file mode 100644 (file)
index 0000000..d26a322
--- /dev/null
@@ -0,0 +1,204 @@
+<?php
+/**
+ * @file src/Protocol/Salmon.php
+ */
+namespace Friendica\Protocol;
+
+use Friendica\Network\Probe;
+use Friendica\Util\XML;
+
+require_once 'include/crypto.php';
+
+/**
+ * @brief Salmon Protocol class
+ * The Salmon Protocol is a message exchange protocol running over HTTP designed to decentralize commentary
+ * and annotations made against newsfeed articles such as blog posts.
+ */
+class Salmon
+{
+       /**
+        * @param string $uri     Uniform Resource Identifier
+        * @param string $keyhash encoded key
+        * @return mixed
+        */
+       public static function getKey($uri, $keyhash)
+       {
+               $ret = array();
+
+               logger('Fetching salmon key for '.$uri);
+
+               $arr = Probe::lrdd($uri);
+
+               if (is_array($arr)) {
+                       foreach ($arr as $a) {
+                               if ($a['@attributes']['rel'] === 'magic-public-key') {
+                                       $ret[] = $a['@attributes']['href'];
+                               }
+                       }
+               } else {
+                       return '';
+               }
+
+               // We have found at least one key URL
+               // If it's inline, parse it - otherwise get the key
+
+               if (count($ret) > 0) {
+                       for ($x = 0; $x < count($ret); $x ++) {
+                               if (substr($ret[$x], 0, 5) === 'data:') {
+                                       if (strstr($ret[$x], ',')) {
+                                               $ret[$x] = substr($ret[$x], strpos($ret[$x], ',') + 1);
+                                       } else {
+                                               $ret[$x] = substr($ret[$x], 5);
+                                       }
+                               } elseif (normalise_link($ret[$x]) == 'http://') {
+                                       $ret[$x] = fetch_url($ret[$x]);
+                               }
+                       }
+               }
+
+
+               logger('Key located: ' . print_r($ret, true));
+
+               if (count($ret) == 1) {
+                       // We only found one one key so we don't care if the hash matches.
+                       // If it's the wrong key we'll find out soon enough because
+                       // message verification will fail. This also covers some older
+                       // software which don't supply a keyhash. As long as they only
+                       // have one key we'll be right.
+
+                       return $ret[0];
+               } else {
+                       foreach ($ret as $a) {
+                               $hash = base64url_encode(hash('sha256', $a));
+                               if ($hash == $keyhash) {
+                                       return $a;
+                               }
+                       }
+               }
+
+               return '';
+       }
+
+       /**
+        * @param array  $owner owner
+        * @param string $url   url
+        * @param string $slap  slap
+        * @return integer
+        */
+       public static function slapper($owner, $url, $slap)
+       {
+               // does contact have a salmon endpoint?
+
+               if (! strlen($url)) {
+                       return;
+               }
+
+               if (! $owner['sprvkey']) {
+                       logger(sprintf("user '%s' (%d) does not have a salmon private key. Send failed.",
+                       $owner['username'], $owner['uid']));
+                       return;
+               }
+
+               logger('slapper called for '.$url.'. Data: ' . $slap);
+
+               // create a magic envelope
+
+               $data      = base64url_encode($slap);
+               $data_type = 'application/atom+xml';
+               $encoding  = 'base64url';
+               $algorithm = 'RSA-SHA256';
+               $keyhash   = base64url_encode(hash('sha256', salmon_key($owner['spubkey'])), true);
+
+               $precomputed = '.' . base64url_encode($data_type) . '.' . base64url_encode($encoding) . '.' . base64url_encode($algorithm);
+
+               // GNU Social format
+               $signature   = base64url_encode(rsa_sign($data . $precomputed, $owner['sprvkey']));
+
+               // Compliant format
+               $signature2  = base64url_encode(rsa_sign(str_replace('=', '', $data . $precomputed), $owner['sprvkey']));
+
+               // Old Status.net format
+               $signature3  = base64url_encode(rsa_sign($data, $owner['sprvkey']));
+
+               // At first try the non compliant method that works for GNU Social
+               $xmldata = array("me:env" => array("me:data" => $data,
+                               "@attributes" => array("type" => $data_type),
+                               "me:encoding" => $encoding,
+                               "me:alg" => $algorithm,
+                               "me:sig" => $signature,
+                               "@attributes2" => array("key_id" => $keyhash)));
+
+               $namespaces = array("me" => "http://salmon-protocol.org/ns/magic-env");
+
+               $salmon = XML::fromArray($xmldata, $xml, false, $namespaces);
+
+               // slap them
+               post_url($url, $salmon, array(
+                       'Content-type: application/magic-envelope+xml',
+                       'Content-length: ' . strlen($salmon)
+               ));
+
+               $a = get_app();
+               $return_code = $a->get_curl_code();
+
+               // check for success, e.g. 2xx
+
+               if ($return_code > 299) {
+                       logger('GNU Social salmon failed. Falling back to compliant mode');
+
+                       // Now try the compliant mode that normally isn't used for GNU Social
+                       $xmldata = array("me:env" => array("me:data" => $data,
+                                       "@attributes" => array("type" => $data_type),
+                                       "me:encoding" => $encoding,
+                                       "me:alg" => $algorithm,
+                                       "me:sig" => $signature2,
+                                       "@attributes2" => array("key_id" => $keyhash)));
+
+                       $namespaces = array("me" => "http://salmon-protocol.org/ns/magic-env");
+
+                       $salmon = XML::fromArray($xmldata, $xml, false, $namespaces);
+
+                       // slap them
+                       post_url($url, $salmon, array(
+                               'Content-type: application/magic-envelope+xml',
+                               'Content-length: ' . strlen($salmon)
+                       ));
+                       $return_code = $a->get_curl_code();
+               }
+
+               if ($return_code > 299) {
+                       logger('compliant salmon failed. Falling back to old status.net');
+
+                       // Last try. This will most likely fail as well.
+                       $xmldata = array("me:env" => array("me:data" => $data,
+                                       "@attributes" => array("type" => $data_type),
+                                       "me:encoding" => $encoding,
+                                       "me:alg" => $algorithm,
+                                       "me:sig" => $signature3,
+                                       "@attributes2" => array("key_id" => $keyhash)));
+
+                       $namespaces = array("me" => "http://salmon-protocol.org/ns/magic-env");
+
+                       $salmon = XML::fromArray($xmldata, $xml, false, $namespaces);
+
+                       // slap them
+                       post_url($url, $salmon, array(
+                               'Content-type: application/magic-envelope+xml',
+                               'Content-length: ' . strlen($salmon))
+                       );
+                       $return_code = $a->get_curl_code();
+               }
+
+               logger('slapper for '.$url.' returned ' . $return_code);
+
+               if (! $return_code) {
+                       return -1;
+               }
+
+               if (($return_code == 503) && (stristr($a->get_curl_headers(), 'retry-after'))) {
+                       return -1;
+               }
+
+               return (($return_code >= 200) && ($return_code < 300)) ? 0 : 1;
+       }
+}
index e64351f17d0e69b7e5329ffeeece8dc04ba11124..4404b879b7fb04bc6cef55be0979c8fa527becd1 100644 (file)
@@ -11,11 +11,11 @@ use Friendica\Network\Probe;
 use Friendica\Object\Contact;
 use Friendica\Protocol\Diaspora;
 use Friendica\Protocol\OStatus;
+use Friendica\Protocol\Salmon;
 use dba;
 
 require_once 'include/queue_fn.php';
 require_once 'include/html2plain.php';
-require_once 'include/salmon.php';
 require_once 'include/datetime.php';
 require_once 'include/items.php';
 require_once 'include/bbcode.php';
@@ -505,11 +505,11 @@ class Notifier {
                // They are especially used for notifications to OStatus users that don't follow us.
 
                if ($slap && count($url_recipients) && ($public_message || $push_notify) && $normal_mode) {
-                       if (!Config::get('system','dfrn_only')) {
+                       if (!Config::get('system', 'dfrn_only')) {
                                foreach ($url_recipients as $url) {
                                        if ($url) {
                                                logger('notifier: urldelivery: ' . $url);
-                                               $deliver_status = slapper($owner,$url,$slap);
+                                               $deliver_status = Salmon::slapper($owner, $url, $slap);
                                                /// @TODO Redeliver/queue these items on failure, though there is no contact record
                                        }
                                }
index 47f1c8044b0b50039f7681989e086feff31b3142..cb00addc9497d823bbdd0f55094f7a924d6bcea6 100644 (file)
@@ -1,9 +1,7 @@
 <?php
-
 /**
  * @file src/Worker/Queue.php
  */
-
 namespace Friendica\Worker;
 
 use Friendica\Core\Cache;
@@ -13,13 +11,13 @@ use Friendica\Database\DBM;
 use Friendica\Protocol\Diaspora;
 use Friendica\Protocol\DFRN;
 use Friendica\Protocol\PortableContact;
+use Friendica\Protocol\Salmon;
 use dba;
 
 require_once 'include/queue_fn.php';
 require_once 'include/datetime.php';
 require_once 'include/items.php';
 require_once 'include/bbcode.php';
-require_once 'include/salmon.php';
 
 class Queue
 {
@@ -141,7 +139,7 @@ class Queue
                        case NETWORK_OSTATUS:
                                if ($contact['notify']) {
                                        logger('queue: slapdelivery: item ' . $q_item['id'] . ' for ' . $contact['name'] . ' <' . $contact['url'] . '>');
-                                       $deliver_status = slapper($owner, $contact['notify'], $data);
+                                       $deliver_status = Salmon::slapper($owner, $contact['notify'], $data);
 
                                        if ($deliver_status == (-1)) {
                                                update_queue_time($q_item['id']);