7 use Friendica\Core\Logger;
8 use Friendica\Core\PConfig;
9 use Friendica\Core\Protocol;
10 use Friendica\Database\DBA;
12 use Friendica\Model\Contact;
13 use Friendica\Protocol\ActivityNamespace;
14 use Friendica\Protocol\OStatus;
15 use Friendica\Protocol\Salmon;
16 use Friendica\Util\Crypto;
17 use Friendica\Util\Network;
18 use Friendica\Util\Strings;
20 function salmon_post(App $a, $xml = '') {
23 $xml = Network::postdata();
26 Logger::log('new salmon ' . $xml, Logger::DATA);
28 $nick = (($a->argc > 1) ? Strings::escapeTags(trim($a->argv[1])) : '');
30 $r = q("SELECT * FROM `user` WHERE `nickname` = '%s' AND `account_expired` = 0 AND `account_removed` = 0 LIMIT 1",
33 if (! DBA::isResult($r)) {
34 throw new \Friendica\Network\HTTPException\InternalServerErrorException();
41 $dom = simplexml_load_string($xml,'SimpleXMLElement',0, ActivityNamespace::SALMON_ME);
45 // figure out where in the DOM tree our data is hiding
46 if (!empty($dom->provenance->data))
47 $base = $dom->provenance;
48 elseif (!empty($dom->env->data))
50 elseif (!empty($dom->data))
54 Logger::log('unable to locate salmon data in xml ');
55 throw new \Friendica\Network\HTTPException\BadRequestException();
58 // Stash the signature away for now. We have to find their key or it won't be good for anything.
61 $signature = Strings::base64UrlDecode($base->sig);
65 // strip whitespace so our data element will return to one big base64 blob
66 $data = str_replace([" ","\t","\r","\n"],["","","",""],$base->data);
68 // stash away some other stuff for later
70 $type = $base->data[0]->attributes()->type[0];
71 $keyhash = $base->sig[0]->attributes()->keyhash[0];
72 $encoding = $base->encoding;
75 // Salmon magic signatures have evolved and there is no way of knowing ahead of time which
76 // flavour we have. We'll try and verify it regardless.
78 $stnet_signed_data = $data;
80 $signed_data = $data . '.' . Strings::base64UrlEncode($type) . '.' . Strings::base64UrlEncode($encoding) . '.' . Strings::base64UrlEncode($alg);
82 $compliant_format = str_replace('=', '', $signed_data);
86 $data = Strings::base64UrlDecode($data);
88 $author = OStatus::salmonAuthor($data, $importer);
89 $author_link = $author["author-link"];
92 Logger::log('Could not retrieve author URI.');
93 throw new \Friendica\Network\HTTPException\BadRequestException();
96 // Once we have the author URI, go to the web and try to find their public key
98 Logger::log('Fetching key for ' . $author_link);
100 $key = Salmon::getKey($author_link, $keyhash);
103 Logger::log('Could not retrieve author key.');
104 throw new \Friendica\Network\HTTPException\BadRequestException();
107 $key_info = explode('.',$key);
109 $m = Strings::base64UrlDecode($key_info[1]);
110 $e = Strings::base64UrlDecode($key_info[2]);
112 Logger::log('key details: ' . print_r($key_info,true), Logger::DEBUG);
114 $pubkey = Crypto::meToPem($m, $e);
116 // We should have everything we need now. Let's see if it verifies.
118 // Try GNU Social format
119 $verify = Crypto::rsaVerify($signed_data, $signature, $pubkey);
123 Logger::log('message did not verify using protocol. Trying compliant format.');
124 $verify = Crypto::rsaVerify($compliant_format, $signature, $pubkey);
129 Logger::log('message did not verify using padding. Trying old statusnet format.');
130 $verify = Crypto::rsaVerify($stnet_signed_data, $signature, $pubkey);
135 Logger::log('Message did not verify. Discarding.');
136 throw new \Friendica\Network\HTTPException\BadRequestException();
139 Logger::log('Message verified with mode '.$mode);
144 * If we reached this point, the message is good. Now let's figure out if the author is allowed to send us stuff.
148 $r = q("SELECT * FROM `contact` WHERE `network` IN ('%s', '%s')
149 AND (`nurl` = '%s' OR `alias` = '%s' OR `alias` = '%s')
150 AND `uid` = %d LIMIT 1",
151 DBA::escape(Protocol::OSTATUS),
152 DBA::escape(Protocol::DFRN),
153 DBA::escape(Strings::normaliseLink($author_link)),
154 DBA::escape($author_link),
155 DBA::escape(Strings::normaliseLink($author_link)),
156 intval($importer['uid'])
159 if (!DBA::isResult($r)) {
160 Logger::log('Author ' . $author_link . ' unknown to user ' . $importer['uid'] . '.');
162 if (DI::pConfig()->get($importer['uid'], 'system', 'ostatus_autofriend')) {
163 $result = Contact::createFromProbe($importer['uid'], $author_link);
165 if ($result['success']) {
166 $r = q("SELECT * FROM `contact` WHERE `network` = '%s' AND ( `url` = '%s' OR `alias` = '%s')
167 AND `uid` = %d LIMIT 1",
168 DBA::escape(Protocol::OSTATUS),
169 DBA::escape($author_link),
170 DBA::escape($author_link),
171 intval($importer['uid'])
177 // Have we ignored the person?
178 // If so we can not accept this post.
180 //if((DBA::isResult($r)) && (($r[0]['readonly']) || ($r[0]['rel'] == Contact::FOLLOWER) || ($r[0]['blocked']))) {
181 if (DBA::isResult($r) && $r[0]['blocked']) {
182 Logger::log('Ignoring this author.');
183 throw new \Friendica\Network\HTTPException\AcceptedException();
186 // Placeholder for hub discovery.
189 $contact_rec = ((DBA::isResult($r)) ? $r[0] : []);
191 OStatus::import($data, $importer, $contact_rec, $hub);
193 throw new \Friendica\Network\HTTPException\OKException();