4 use Friendica\Core\PConfig;
6 require_once('include/salmon.php');
7 require_once('include/ostatus.php');
8 require_once('include/crypto.php');
9 require_once('include/items.php');
10 require_once('include/follow.php');
12 function salmon_return($val) {
16 if($val >= 200 && $val < 300)
19 logger('mod-salmon returns ' . $val);
20 header($_SERVER["SERVER_PROTOCOL"] . ' ' . $val . ' ' . $err);
25 function salmon_post(App $a) {
27 $xml = file_get_contents('php://input');
29 logger('mod-salmon: new salmon ' . $xml, LOGGER_DATA);
31 $nick = (($a->argc > 1) ? notags(trim($a->argv[1])) : '');
32 $mentions = (($a->argc > 2 && $a->argv[2] === 'mention') ? true : false);
34 $r = q("SELECT * FROM `user` WHERE `nickname` = '%s' AND `account_expired` = 0 AND `account_removed` = 0 LIMIT 1",
37 if (! dbm::is_result($r)) {
38 http_status_exit(500);
45 $dom = simplexml_load_string($xml,'SimpleXMLElement',0,NAMESPACE_SALMON_ME);
47 // figure out where in the DOM tree our data is hiding
49 if($dom->provenance->data)
50 $base = $dom->provenance;
51 elseif($dom->env->data)
57 logger('mod-salmon: unable to locate salmon data in xml ');
58 http_status_exit(400);
61 // Stash the signature away for now. We have to find their key or it won't be good for anything.
64 $signature = base64url_decode($base->sig);
68 // strip whitespace so our data element will return to one big base64 blob
69 $data = str_replace(array(" ","\t","\r","\n"),array("","","",""),$base->data);
71 // stash away some other stuff for later
73 $type = $base->data[0]->attributes()->type[0];
74 $keyhash = $base->sig[0]->attributes()->keyhash[0];
75 $encoding = $base->encoding;
78 // Salmon magic signatures have evolved and there is no way of knowing ahead of time which
79 // flavour we have. We'll try and verify it regardless.
81 $stnet_signed_data = $data;
83 $signed_data = $data . '.' . base64url_encode($type) . '.' . base64url_encode($encoding) . '.' . base64url_encode($alg);
85 $compliant_format = str_replace('=', '', $signed_data);
89 $data = base64url_decode($data);
91 $author = ostatus::salmon_author($data,$importer);
92 $author_link = $author["author-link"];
95 logger('mod-salmon: Could not retrieve author URI.');
96 http_status_exit(400);
99 // Once we have the author URI, go to the web and try to find their public key
101 logger('mod-salmon: Fetching key for ' . $author_link);
103 $key = get_salmon_key($author_link,$keyhash);
106 logger('mod-salmon: Could not retrieve author key.');
107 http_status_exit(400);
110 $key_info = explode('.',$key);
112 $m = base64url_decode($key_info[1]);
113 $e = base64url_decode($key_info[2]);
115 logger('mod-salmon: key details: ' . print_r($key_info,true), LOGGER_DEBUG);
117 $pubkey = metopem($m,$e);
119 // We should have everything we need now. Let's see if it verifies.
121 // Try GNU Social format
122 $verify = rsa_verify($signed_data, $signature, $pubkey);
126 logger('mod-salmon: message did not verify using protocol. Trying compliant format.');
127 $verify = rsa_verify($compliant_format, $signature, $pubkey);
132 logger('mod-salmon: message did not verify using padding. Trying old statusnet format.');
133 $verify = rsa_verify($stnet_signed_data, $signature, $pubkey);
138 logger('mod-salmon: Message did not verify. Discarding.');
139 http_status_exit(400);
142 logger('mod-salmon: Message verified with mode '.$mode);
147 * If we reached this point, the message is good. Now let's figure out if the author is allowed to send us stuff.
151 $r = q("SELECT * FROM `contact` WHERE `network` IN ('%s', '%s')
152 AND (`nurl` = '%s' OR `alias` = '%s' OR `alias` = '%s')
153 AND `uid` = %d LIMIT 1",
154 dbesc(NETWORK_OSTATUS),
156 dbesc(normalise_link($author_link)),
158 dbesc(normalise_link($author_link)),
159 intval($importer['uid'])
161 if (! dbm::is_result($r)) {
162 logger('mod-salmon: Author unknown to us.');
163 if(PConfig::get($importer['uid'],'system','ostatus_autofriend')) {
164 $result = new_contact($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 dbesc(NETWORK_OSTATUS),
171 intval($importer['uid'])
177 // Have we ignored the person?
178 // If so we can not accept this post.
180 //if((dbm::is_result($r)) && (($r[0]['readonly']) || ($r[0]['rel'] == CONTACT_IS_FOLLOWER) || ($r[0]['blocked']))) {
181 if (dbm::is_result($r) && $r[0]['blocked']) {
182 logger('mod-salmon: Ignoring this author.');
183 http_status_exit(202);
187 // Placeholder for hub discovery.
190 $contact_rec = ((dbm::is_result($r)) ? $r[0] : null);
192 ostatus::import($data,$importer,$contact_rec, $hub);
194 http_status_exit(200);