4 // There is a lot of debug stuff in here because this is quite a
5 // complicated process to try and sort out.
7 require_once('include/salmon.php');
8 require_once('include/ostatus.php');
9 require_once('include/crypto.php');
11 function salmon_return($val) {
15 if($val >= 200 && $val < 300)
18 logger('mod-salmon returns ' . $val);
19 header($_SERVER["SERVER_PROTOCOL"] . ' ' . $val . ' ' . $err);
24 function salmon_post(&$a) {
26 $xml = file_get_contents('php://input');
28 logger('mod-salmon: new salmon ' . $xml, LOGGER_DATA);
30 $nick = (($a->argc > 1) ? notags(trim($a->argv[1])) : '');
31 $mentions = (($a->argc > 2 && $a->argv[2] === 'mention') ? true : false);
33 $r = q("SELECT * FROM `user` WHERE `nickname` = '%s' AND `account_expired` = 0 AND `account_removed` = 0 LIMIT 1",
37 http_status_exit(500);
43 $dom = simplexml_load_string($xml,'SimpleXMLElement',0,NAMESPACE_SALMON_ME);
45 // figure out where in the DOM tree our data is hiding
47 if($dom->provenance->data)
48 $base = $dom->provenance;
49 elseif($dom->env->data)
55 logger('mod-salmon: unable to locate salmon data in xml ');
56 http_status_exit(400);
59 // Stash the signature away for now. We have to find their key or it won't be good for anything.
62 $signature = base64url_decode($base->sig);
66 // strip whitespace so our data element will return to one big base64 blob
67 $data = str_replace(array(" ","\t","\r","\n"),array("","","",""),$base->data);
69 // stash away some other stuff for later
71 $type = $base->data[0]->attributes()->type[0];
72 $keyhash = $base->sig[0]->attributes()->keyhash[0];
73 $encoding = $base->encoding;
76 // Salmon magic signatures have evolved and there is no way of knowing ahead of time which
77 // flavour we have. We'll try and verify it regardless.
79 $stnet_signed_data = $data;
81 $signed_data = $data . '.' . base64url_encode($type) . '.' . base64url_encode($encoding) . '.' . base64url_encode($alg);
83 $compliant_format = str_replace('=','',$signed_data);
87 $data = base64url_decode($data);
89 $author = ostatus_salmon_author($data,$importer);
90 $author_link = $author["author-link"];
93 logger('mod-salmon: Could not retrieve author URI.');
94 http_status_exit(400);
97 // Once we have the author URI, go to the web and try to find their public key
99 logger('mod-salmon: Fetching key for ' . $author_link );
102 $key = get_salmon_key($author_link,$keyhash);
105 logger('mod-salmon: Could not retrieve author key.');
106 http_status_exit(400);
109 $key_info = explode('.',$key);
111 $m = base64url_decode($key_info[1]);
112 $e = base64url_decode($key_info[2]);
114 logger('mod-salmon: key details: ' . print_r($key_info,true), LOGGER_DEBUG);
116 $pubkey = metopem($m,$e);
118 // We should have everything we need now. Let's see if it verifies.
120 $verify = rsa_verify($compliant_format,$signature,$pubkey);
123 logger('mod-salmon: message did not verify using protocol. Trying padding hack.');
124 $verify = rsa_verify($signed_data,$signature,$pubkey);
128 logger('mod-salmon: message did not verify using padding. Trying old statusnet hack.');
129 $verify = rsa_verify($stnet_signed_data,$signature,$pubkey);
133 logger('mod-salmon: Message did not verify. Discarding.');
134 http_status_exit(400);
137 logger('mod-salmon: Message verified.');
142 * If we reached this point, the message is good. Now let's figure out if the author is allowed to send us stuff.
146 $r = q("SELECT * FROM `contact` WHERE `network` IN ('%s', '%s')
147 AND (`nurl` = '%s' OR `alias` = '%s' OR `alias` = '%s')
148 AND `uid` = %d LIMIT 1",
149 dbesc(NETWORK_OSTATUS),
151 dbesc(normalise_link($author_link)),
153 dbesc(normalise_link($author_link)),
154 intval($importer['uid'])
157 logger('mod-salmon: Author unknown to us.');
158 if(get_pconfig($importer['uid'],'system','ostatus_autofriend')) {
159 require_once('include/follow.php');
160 $result = new_contact($importer['uid'],$author_link);
161 if($result['success']) {
162 $r = q("SELECT * FROM `contact` WHERE `network` = '%s' AND ( `url` = '%s' OR `alias` = '%s' )
163 AND `uid` = %d LIMIT 1",
164 dbesc(NETWORK_OSTATUS),
167 intval($importer['uid'])
173 // is this a follower? Or have we ignored the person?
174 // If so we can not accept this post.
176 if((count($r)) && (($r[0]['readonly']) || ($r[0]['rel'] == CONTACT_IS_FOLLOWER) || ($r[0]['blocked']))) {
177 logger('mod-salmon: Ignoring this author.');
178 http_status_exit(202);
182 require_once('include/items.php');
184 // Placeholder for hub discovery. We shouldn't find any hubs
185 // since we supplied the fake feed header - and it doesn't have any.
191 * anti-spam measure: consume_feed will accept a follow activity from
192 * this person (and nothing else) if there is no existing contact record.
196 $contact_rec = ((count($r)) ? $r[0] : null);
198 //consume_feed($feedxml,$importer,$contact_rec,$hub);
199 ostatus_import($data,$importer,$contact_rec, $hub);
201 http_status_exit(200);