6 use Friendica\Core\Logger;
7 use Friendica\Core\PConfig;
8 use Friendica\Core\Protocol;
9 use Friendica\Core\System;
10 use Friendica\Database\DBA;
11 use Friendica\Model\Contact;
12 use Friendica\Protocol\OStatus;
13 use Friendica\Protocol\Salmon;
14 use Friendica\Util\Crypto;
15 use Friendica\Util\Strings;
17 function salmon_post(App $a, $xml = '') {
20 $xml = file_get_contents('php://input');
23 Logger::log('new salmon ' . $xml, Logger::DATA);
25 $nick = (($a->argc > 1) ? Strings::escapeTags(trim($a->argv[1])) : '');
26 $mentions = (($a->argc > 2 && $a->argv[2] === 'mention') ? true : false);
28 $r = q("SELECT * FROM `user` WHERE `nickname` = '%s' AND `account_expired` = 0 AND `account_removed` = 0 LIMIT 1",
31 if (! DBA::isResult($r)) {
32 System::httpExit(500);
39 $dom = simplexml_load_string($xml,'SimpleXMLElement',0,NAMESPACE_SALMON_ME);
43 // figure out where in the DOM tree our data is hiding
44 if (!empty($dom->provenance->data))
45 $base = $dom->provenance;
46 elseif (!empty($dom->env->data))
48 elseif (!empty($dom->data))
52 Logger::log('unable to locate salmon data in xml ');
53 System::httpExit(400);
56 // Stash the signature away for now. We have to find their key or it won't be good for anything.
59 $signature = Strings::base64UrlDecode($base->sig);
63 // strip whitespace so our data element will return to one big base64 blob
64 $data = str_replace([" ","\t","\r","\n"],["","","",""],$base->data);
66 // stash away some other stuff for later
68 $type = $base->data[0]->attributes()->type[0];
69 $keyhash = $base->sig[0]->attributes()->keyhash[0];
70 $encoding = $base->encoding;
73 // Salmon magic signatures have evolved and there is no way of knowing ahead of time which
74 // flavour we have. We'll try and verify it regardless.
76 $stnet_signed_data = $data;
78 $signed_data = $data . '.' . Strings::base64UrlEncode($type) . '.' . Strings::base64UrlEncode($encoding) . '.' . Strings::base64UrlEncode($alg);
80 $compliant_format = str_replace('=', '', $signed_data);
84 $data = Strings::base64UrlDecode($data);
86 $author = OStatus::salmonAuthor($data, $importer);
87 $author_link = $author["author-link"];
90 Logger::log('Could not retrieve author URI.');
91 System::httpExit(400);
94 // Once we have the author URI, go to the web and try to find their public key
96 Logger::log('Fetching key for ' . $author_link);
98 $key = Salmon::getKey($author_link, $keyhash);
101 Logger::log('Could not retrieve author key.');
102 System::httpExit(400);
105 $key_info = explode('.',$key);
107 $m = Strings::base64UrlDecode($key_info[1]);
108 $e = Strings::base64UrlDecode($key_info[2]);
110 Logger::log('key details: ' . print_r($key_info,true), Logger::DEBUG);
112 $pubkey = Crypto::meToPem($m, $e);
114 // We should have everything we need now. Let's see if it verifies.
116 // Try GNU Social format
117 $verify = Crypto::rsaVerify($signed_data, $signature, $pubkey);
121 Logger::log('message did not verify using protocol. Trying compliant format.');
122 $verify = Crypto::rsaVerify($compliant_format, $signature, $pubkey);
127 Logger::log('message did not verify using padding. Trying old statusnet format.');
128 $verify = Crypto::rsaVerify($stnet_signed_data, $signature, $pubkey);
133 Logger::log('Message did not verify. Discarding.');
134 System::httpExit(400);
137 Logger::log('Message verified with mode '.$mode);
142 * If we reached this point, the message is good. Now let's figure out if the author is allowed to send us stuff.
146 $r = q("SELECT * FROM `contact` WHERE `network` IN ('%s', '%s')
147 AND (`nurl` = '%s' OR `alias` = '%s' OR `alias` = '%s')
148 AND `uid` = %d LIMIT 1",
149 DBA::escape(Protocol::OSTATUS),
150 DBA::escape(Protocol::DFRN),
151 DBA::escape(Strings::normaliseLink($author_link)),
152 DBA::escape($author_link),
153 DBA::escape(Strings::normaliseLink($author_link)),
154 intval($importer['uid'])
157 if (!DBA::isResult($r)) {
158 Logger::log('Author ' . $author_link . ' unknown to user ' . $importer['uid'] . '.');
160 if (PConfig::get($importer['uid'], 'system', 'ostatus_autofriend')) {
161 $result = Contact::createFromProbe($importer['uid'], $author_link);
163 if ($result['success']) {
164 $r = q("SELECT * FROM `contact` WHERE `network` = '%s' AND ( `url` = '%s' OR `alias` = '%s')
165 AND `uid` = %d LIMIT 1",
166 DBA::escape(Protocol::OSTATUS),
167 DBA::escape($author_link),
168 DBA::escape($author_link),
169 intval($importer['uid'])
175 // Have we ignored the person?
176 // If so we can not accept this post.
178 //if((DBA::isResult($r)) && (($r[0]['readonly']) || ($r[0]['rel'] == Contact::FOLLOWER) || ($r[0]['blocked']))) {
179 if (DBA::isResult($r) && $r[0]['blocked']) {
180 Logger::log('Ignoring this author.');
181 System::httpExit(202);
185 // Placeholder for hub discovery.
188 $contact_rec = ((DBA::isResult($r)) ? $r[0] : null);
190 OStatus::import($data, $importer, $contact_rec, $hub);
192 System::httpExit(200);