6 use Friendica\Core\PConfig;
7 use Friendica\Core\System;
8 use Friendica\Database\DBM;
9 use Friendica\Model\Contact;
10 use Friendica\Protocol\OStatus;
11 use Friendica\Protocol\Salmon;
12 use Friendica\Util\Crypto;
14 require_once 'include/items.php';
16 function salmon_post(App $a) {
18 $xml = file_get_contents('php://input');
20 logger('new salmon ' . $xml, LOGGER_DATA);
22 $nick = (($a->argc > 1) ? notags(trim($a->argv[1])) : '');
23 $mentions = (($a->argc > 2 && $a->argv[2] === 'mention') ? true : false);
25 $r = q("SELECT * FROM `user` WHERE `nickname` = '%s' AND `account_expired` = 0 AND `account_removed` = 0 LIMIT 1",
28 if (! DBM::is_result($r)) {
29 System::httpExit(500);
36 $dom = simplexml_load_string($xml,'SimpleXMLElement',0,NAMESPACE_SALMON_ME);
40 // figure out where in the DOM tree our data is hiding
41 if($dom->provenance->data)
42 $base = $dom->provenance;
43 elseif($dom->env->data)
49 logger('unable to locate salmon data in xml ');
50 System::httpExit(400);
53 // Stash the signature away for now. We have to find their key or it won't be good for anything.
56 $signature = base64url_decode($base->sig);
60 // strip whitespace so our data element will return to one big base64 blob
61 $data = str_replace([" ","\t","\r","\n"],["","","",""],$base->data);
63 // stash away some other stuff for later
65 $type = $base->data[0]->attributes()->type[0];
66 $keyhash = $base->sig[0]->attributes()->keyhash[0];
67 $encoding = $base->encoding;
70 // Salmon magic signatures have evolved and there is no way of knowing ahead of time which
71 // flavour we have. We'll try and verify it regardless.
73 $stnet_signed_data = $data;
75 $signed_data = $data . '.' . base64url_encode($type) . '.' . base64url_encode($encoding) . '.' . base64url_encode($alg);
77 $compliant_format = str_replace('=', '', $signed_data);
81 $data = base64url_decode($data);
83 $author = OStatus::salmonAuthor($data, $importer);
84 $author_link = $author["author-link"];
87 logger('Could not retrieve author URI.');
88 System::httpExit(400);
91 // Once we have the author URI, go to the web and try to find their public key
93 logger('Fetching key for ' . $author_link);
95 $key = Salmon::getKey($author_link, $keyhash);
98 logger('Could not retrieve author key.');
99 System::httpExit(400);
102 $key_info = explode('.',$key);
104 $m = base64url_decode($key_info[1]);
105 $e = base64url_decode($key_info[2]);
107 logger('key details: ' . print_r($key_info,true), LOGGER_DEBUG);
109 $pubkey = Crypto::meToPem($m, $e);
111 // We should have everything we need now. Let's see if it verifies.
113 // Try GNU Social format
114 $verify = Crypto::rsaVerify($signed_data, $signature, $pubkey);
118 logger('message did not verify using protocol. Trying compliant format.');
119 $verify = Crypto::rsaVerify($compliant_format, $signature, $pubkey);
124 logger('message did not verify using padding. Trying old statusnet format.');
125 $verify = Crypto::rsaVerify($stnet_signed_data, $signature, $pubkey);
130 logger('Message did not verify. Discarding.');
131 System::httpExit(400);
134 logger('Message verified with mode '.$mode);
139 * If we reached this point, the message is good. Now let's figure out if the author is allowed to send us stuff.
143 $r = q("SELECT * FROM `contact` WHERE `network` IN ('%s', '%s')
144 AND (`nurl` = '%s' OR `alias` = '%s' OR `alias` = '%s')
145 AND `uid` = %d LIMIT 1",
146 dbesc(NETWORK_OSTATUS),
148 dbesc(normalise_link($author_link)),
150 dbesc(normalise_link($author_link)),
151 intval($importer['uid'])
153 if (! DBM::is_result($r)) {
154 logger('Author ' . $author_link . ' unknown to user ' . $importer['uid'] . '.');
155 if(PConfig::get($importer['uid'],'system','ostatus_autofriend')) {
156 $result = Contact::createFromProbe($importer['uid'], $author_link);
157 if($result['success']) {
158 $r = q("SELECT * FROM `contact` WHERE `network` = '%s' AND ( `url` = '%s' OR `alias` = '%s')
159 AND `uid` = %d LIMIT 1",
160 dbesc(NETWORK_OSTATUS),
163 intval($importer['uid'])
169 // Have we ignored the person?
170 // If so we can not accept this post.
172 //if((DBM::is_result($r)) && (($r[0]['readonly']) || ($r[0]['rel'] == CONTACT_IS_FOLLOWER) || ($r[0]['blocked']))) {
173 if (DBM::is_result($r) && $r[0]['blocked']) {
174 logger('Ignoring this author.');
175 System::httpExit(202);
179 // Placeholder for hub discovery.
182 $contact_rec = ((DBM::is_result($r)) ? $r[0] : null);
184 OStatus::import($data, $importer, $contact_rec, $hub);
186 System::httpExit(200);