3 require_once('include/salmon.php');
4 require_once('include/ostatus.php');
5 require_once('include/crypto.php');
6 require_once('include/items.php');
7 require_once('include/follow.php');
9 if(! function_exists('salmon_return')) {
10 function salmon_return($val) {
14 if($val >= 200 && $val < 300)
17 logger('mod-salmon returns ' . $val);
18 header($_SERVER["SERVER_PROTOCOL"] . ' ' . $val . ' ' . $err);
23 if(! function_exists('salmon_post')) {
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);
101 $key = get_salmon_key($author_link,$keyhash);
104 logger('mod-salmon: Could not retrieve author key.');
105 http_status_exit(400);
108 $key_info = explode('.',$key);
110 $m = base64url_decode($key_info[1]);
111 $e = base64url_decode($key_info[2]);
113 logger('mod-salmon: key details: ' . print_r($key_info,true), LOGGER_DEBUG);
115 $pubkey = metopem($m,$e);
117 // We should have everything we need now. Let's see if it verifies.
119 $verify = rsa_verify($compliant_format,$signature,$pubkey);
122 logger('mod-salmon: message did not verify using protocol. Trying padding hack.');
123 $verify = rsa_verify($signed_data,$signature,$pubkey);
127 logger('mod-salmon: message did not verify using padding. Trying old statusnet hack.');
128 $verify = rsa_verify($stnet_signed_data,$signature,$pubkey);
132 logger('mod-salmon: Message did not verify. Discarding.');
133 http_status_exit(400);
136 logger('mod-salmon: Message verified.');
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'])
156 logger('mod-salmon: Author unknown to us.');
157 if(get_pconfig($importer['uid'],'system','ostatus_autofriend')) {
158 $result = new_contact($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((count($r)) && (($r[0]['readonly']) || ($r[0]['rel'] == CONTACT_IS_FOLLOWER) || ($r[0]['blocked']))) {
175 if(count($r) && $r[0]['blocked']) {
176 logger('mod-salmon: Ignoring this author.');
177 http_status_exit(202);
181 // Placeholder for hub discovery.
184 $contact_rec = ((count($r)) ? $r[0] : null);
186 ostatus_import($data,$importer,$contact_rec, $hub);
188 http_status_exit(200);