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, $xml = '') {
19 $xml = file_get_contents('php://input');
22 logger('new salmon ' . $xml, LOGGER_DATA);
24 $nick = (($a->argc > 1) ? notags(trim($a->argv[1])) : '');
25 $mentions = (($a->argc > 2 && $a->argv[2] === 'mention') ? true : false);
27 $r = q("SELECT * FROM `user` WHERE `nickname` = '%s' AND `account_expired` = 0 AND `account_removed` = 0 LIMIT 1",
30 if (! DBM::is_result($r)) {
31 System::httpExit(500);
38 $dom = simplexml_load_string($xml,'SimpleXMLElement',0,NAMESPACE_SALMON_ME);
42 // figure out where in the DOM tree our data is hiding
43 if($dom->provenance->data)
44 $base = $dom->provenance;
45 elseif($dom->env->data)
51 logger('unable to locate salmon data in xml ');
52 System::httpExit(400);
55 // Stash the signature away for now. We have to find their key or it won't be good for anything.
58 $signature = base64url_decode($base->sig);
62 // strip whitespace so our data element will return to one big base64 blob
63 $data = str_replace([" ","\t","\r","\n"],["","","",""],$base->data);
65 // stash away some other stuff for later
67 $type = $base->data[0]->attributes()->type[0];
68 $keyhash = $base->sig[0]->attributes()->keyhash[0];
69 $encoding = $base->encoding;
72 // Salmon magic signatures have evolved and there is no way of knowing ahead of time which
73 // flavour we have. We'll try and verify it regardless.
75 $stnet_signed_data = $data;
77 $signed_data = $data . '.' . base64url_encode($type) . '.' . base64url_encode($encoding) . '.' . base64url_encode($alg);
79 $compliant_format = str_replace('=', '', $signed_data);
83 $data = base64url_decode($data);
85 $author = OStatus::salmonAuthor($data, $importer);
86 $author_link = $author["author-link"];
89 logger('Could not retrieve author URI.');
90 System::httpExit(400);
93 // Once we have the author URI, go to the web and try to find their public key
95 logger('Fetching key for ' . $author_link);
97 $key = Salmon::getKey($author_link, $keyhash);
100 logger('Could not retrieve author key.');
101 System::httpExit(400);
104 $key_info = explode('.',$key);
106 $m = base64url_decode($key_info[1]);
107 $e = base64url_decode($key_info[2]);
109 logger('key details: ' . print_r($key_info,true), LOGGER_DEBUG);
111 $pubkey = Crypto::meToPem($m, $e);
113 // We should have everything we need now. Let's see if it verifies.
115 // Try GNU Social format
116 $verify = Crypto::rsaVerify($signed_data, $signature, $pubkey);
120 logger('message did not verify using protocol. Trying compliant format.');
121 $verify = Crypto::rsaVerify($compliant_format, $signature, $pubkey);
126 logger('message did not verify using padding. Trying old statusnet format.');
127 $verify = Crypto::rsaVerify($stnet_signed_data, $signature, $pubkey);
132 logger('Message did not verify. Discarding.');
133 System::httpExit(400);
136 logger('Message verified with mode '.$mode);
141 * If we reached this point, the message is good. Now let's figure out if the author is allowed to send us stuff.
145 $r = q("SELECT * FROM `contact` WHERE `network` IN ('%s', '%s')
146 AND (`nurl` = '%s' OR `alias` = '%s' OR `alias` = '%s')
147 AND `uid` = %d LIMIT 1",
148 dbesc(NETWORK_OSTATUS),
150 dbesc(normalise_link($author_link)),
152 dbesc(normalise_link($author_link)),
153 intval($importer['uid'])
155 if (! DBM::is_result($r)) {
156 logger('Author ' . $author_link . ' unknown to user ' . $importer['uid'] . '.');
157 if(PConfig::get($importer['uid'],'system','ostatus_autofriend')) {
158 $result = Contact::createFromProbe($importer['uid'], $author_link);
159 if($result['success']) {
160 $r = q("SELECT * FROM `contact` WHERE `network` = '%s' AND ( `url` = '%s' OR `alias` = '%s')
161 AND `uid` = %d LIMIT 1",
162 dbesc(NETWORK_OSTATUS),
165 intval($importer['uid'])
171 // Have we ignored the person?
172 // If so we can not accept this post.
174 //if((DBM::is_result($r)) && (($r[0]['readonly']) || ($r[0]['rel'] == CONTACT_IS_FOLLOWER) || ($r[0]['blocked']))) {
175 if (DBM::is_result($r) && $r[0]['blocked']) {
176 logger('Ignoring this author.');
177 System::httpExit(202);
181 // Placeholder for hub discovery.
184 $contact_rec = ((DBM::is_result($r)) ? $r[0] : null);
186 OStatus::import($data, $importer, $contact_rec, $hub);
188 System::httpExit(200);