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/crypto.php');
9 require_once('library/simplepie/simplepie.inc');
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 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 // Remove the xml declaration
90 $data = preg_replace('/\<\?xml[^\?].*\?\>/','',$data);
92 // Create a fake feed wrapper so simplepie doesn't choke
94 $tpl = get_markup_template('fake_feed.tpl');
96 $base = substr($data,strpos($data,'<entry'));
98 $feedxml = $tpl . $base . '</feed>';
100 logger('mod-salmon: Processed feed: ' . $feedxml);
102 // Now parse it like a normal atom feed to scrape out the author URI
104 $feed = new SimplePie();
105 $feed->set_raw_data($feedxml);
106 $feed->enable_order_by_date(false);
109 logger('mod-salmon: Feed parsed.');
111 if($feed->get_item_quantity()) {
112 foreach($feed->get_items() as $item) {
113 $author = $item->get_author();
114 $author_link = unxmlify($author->get_link());
120 logger('mod-salmon: Could not retrieve author URI.');
121 http_status_exit(400);
124 // Once we have the author URI, go to the web and try to find their public key
126 logger('mod-salmon: Fetching key for ' . $author_link );
129 $key = get_salmon_key($author_link,$keyhash);
132 logger('mod-salmon: Could not retrieve author key.');
133 http_status_exit(400);
136 $key_info = explode('.',$key);
138 $m = base64url_decode($key_info[1]);
139 $e = base64url_decode($key_info[2]);
141 logger('mod-salmon: key details: ' . print_r($key_info,true), LOGGER_DEBUG);
143 $pubkey = metopem($m,$e);
145 // We should have everything we need now. Let's see if it verifies.
147 $verify = rsa_verify($compliant_format,$signature,$pubkey);
150 logger('mod-salmon: message did not verify using protocol. Trying padding hack.');
151 $verify = rsa_verify($signed_data,$signature,$pubkey);
155 logger('mod-salmon: message did not verify using padding. Trying old statusnet hack.');
156 $verify = rsa_verify($stnet_signed_data,$signature,$pubkey);
160 logger('mod-salmon: Message did not verify. Discarding.');
161 http_status_exit(400);
164 logger('mod-salmon: Message verified.');
169 * If we reached this point, the message is good. Now let's figure out if the author is allowed to send us stuff.
173 $r = q("SELECT * FROM `contact` WHERE `network` = '%s' AND ( `url` = '%s' OR `alias` = '%s' )
174 AND `uid` = %d LIMIT 1",
175 dbesc(NETWORK_OSTATUS),
178 intval($importer['uid'])
181 logger('mod-salmon: Author unknown to us.');
182 if(get_pconfig($importer['uid'],'system','ostatus_autofriend')) {
183 require_once('include/follow.php');
184 $result = new_contact($importer['uid'],$author_link);
185 if($result['success']) {
186 $r = q("SELECT * FROM `contact` WHERE `network` = '%s' AND ( `url` = '%s' OR `alias` = '%s' )
187 AND `uid` = %d LIMIT 1",
188 dbesc(NETWORK_OSTATUS),
191 intval($importer['uid'])
197 // is this a follower? Or have we ignored the person?
198 // If so we can not accept this post.
200 if((count($r)) && (($r[0]['readonly']) || ($r[0]['rel'] == CONTACT_IS_FOLLOWER) || ($r[0]['blocked']))) {
201 logger('mod-salmon: Ignoring this author.');
202 http_status_exit(202);
206 require_once('include/items.php');
208 // Placeholder for hub discovery. We shouldn't find any hubs
209 // since we supplied the fake feed header - and it doesn't have any.
215 * anti-spam measure: consume_feed will accept a follow activity from
216 * this person (and nothing else) if there is no existing contact record.
220 $contact_rec = ((count($r)) ? $r[0] : null);
222 consume_feed($feedxml,$importer,$contact_rec,$hub);
224 http_status_exit(200);