4 use Friendica\Core\PConfig;
5 use Friendica\Database\DBM;
7 require_once('include/salmon.php');
8 require_once('include/ostatus.php');
9 require_once('include/crypto.php');
10 require_once('include/items.php');
11 require_once('include/follow.php');
13 function salmon_return($val) {
17 if($val >= 200 && $val < 300)
20 logger('mod-salmon returns ' . $val);
21 header($_SERVER["SERVER_PROTOCOL"] . ' ' . $val . ' ' . $err);
26 function salmon_post(App $a) {
28 $xml = file_get_contents('php://input');
30 logger('mod-salmon: new salmon ' . $xml, LOGGER_DATA);
32 $nick = (($a->argc > 1) ? notags(trim($a->argv[1])) : '');
33 $mentions = (($a->argc > 2 && $a->argv[2] === 'mention') ? true : false);
35 $r = q("SELECT * FROM `user` WHERE `nickname` = '%s' AND `account_expired` = 0 AND `account_removed` = 0 LIMIT 1",
38 if (! DBM::is_result($r)) {
39 http_status_exit(500);
46 $dom = simplexml_load_string($xml,'SimpleXMLElement',0,NAMESPACE_SALMON_ME);
48 // figure out where in the DOM tree our data is hiding
50 if($dom->provenance->data)
51 $base = $dom->provenance;
52 elseif($dom->env->data)
58 logger('mod-salmon: unable to locate salmon data in xml ');
59 http_status_exit(400);
62 // Stash the signature away for now. We have to find their key or it won't be good for anything.
65 $signature = base64url_decode($base->sig);
69 // strip whitespace so our data element will return to one big base64 blob
70 $data = str_replace(array(" ","\t","\r","\n"),array("","","",""),$base->data);
72 // stash away some other stuff for later
74 $type = $base->data[0]->attributes()->type[0];
75 $keyhash = $base->sig[0]->attributes()->keyhash[0];
76 $encoding = $base->encoding;
79 // Salmon magic signatures have evolved and there is no way of knowing ahead of time which
80 // flavour we have. We'll try and verify it regardless.
82 $stnet_signed_data = $data;
84 $signed_data = $data . '.' . base64url_encode($type) . '.' . base64url_encode($encoding) . '.' . base64url_encode($alg);
86 $compliant_format = str_replace('=', '', $signed_data);
90 $data = base64url_decode($data);
92 $author = ostatus::salmon_author($data,$importer);
93 $author_link = $author["author-link"];
96 logger('mod-salmon: Could not retrieve author URI.');
97 http_status_exit(400);
100 // Once we have the author URI, go to the web and try to find their public key
102 logger('mod-salmon: Fetching key for ' . $author_link);
104 $key = get_salmon_key($author_link,$keyhash);
107 logger('mod-salmon: Could not retrieve author key.');
108 http_status_exit(400);
111 $key_info = explode('.',$key);
113 $m = base64url_decode($key_info[1]);
114 $e = base64url_decode($key_info[2]);
116 logger('mod-salmon: key details: ' . print_r($key_info,true), LOGGER_DEBUG);
118 $pubkey = metopem($m,$e);
120 // We should have everything we need now. Let's see if it verifies.
122 // Try GNU Social format
123 $verify = rsa_verify($signed_data, $signature, $pubkey);
127 logger('mod-salmon: message did not verify using protocol. Trying compliant format.');
128 $verify = rsa_verify($compliant_format, $signature, $pubkey);
133 logger('mod-salmon: message did not verify using padding. Trying old statusnet format.');
134 $verify = rsa_verify($stnet_signed_data, $signature, $pubkey);
139 logger('mod-salmon: Message did not verify. Discarding.');
140 http_status_exit(400);
143 logger('mod-salmon: Message verified with mode '.$mode);
148 * If we reached this point, the message is good. Now let's figure out if the author is allowed to send us stuff.
152 $r = q("SELECT * FROM `contact` WHERE `network` IN ('%s', '%s')
153 AND (`nurl` = '%s' OR `alias` = '%s' OR `alias` = '%s')
154 AND `uid` = %d LIMIT 1",
155 dbesc(NETWORK_OSTATUS),
157 dbesc(normalise_link($author_link)),
159 dbesc(normalise_link($author_link)),
160 intval($importer['uid'])
162 if (! DBM::is_result($r)) {
163 logger('mod-salmon: Author unknown to us.');
164 if(PConfig::get($importer['uid'],'system','ostatus_autofriend')) {
165 $result = new_contact($importer['uid'],$author_link);
166 if($result['success']) {
167 $r = q("SELECT * FROM `contact` WHERE `network` = '%s' AND ( `url` = '%s' OR `alias` = '%s')
168 AND `uid` = %d LIMIT 1",
169 dbesc(NETWORK_OSTATUS),
172 intval($importer['uid'])
178 // Have we ignored the person?
179 // If so we can not accept this post.
181 //if((DBM::is_result($r)) && (($r[0]['readonly']) || ($r[0]['rel'] == CONTACT_IS_FOLLOWER) || ($r[0]['blocked']))) {
182 if (DBM::is_result($r) && $r[0]['blocked']) {
183 logger('mod-salmon: Ignoring this author.');
184 http_status_exit(202);
188 // Placeholder for hub discovery.
191 $contact_rec = ((DBM::is_result($r)) ? $r[0] : null);
193 ostatus::import($data,$importer,$contact_rec, $hub);
195 http_status_exit(200);