5 // add relevant contacts so they can use this
7 // There is a lot of debug stuff in here because this is quite a
8 // complicated process to try and sort out.
10 require_once('include/salmon.php');
11 require_once('simplepie/simplepie.inc');
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(&$a) {
28 $xml = file_get_contents('php://input');
30 logger('mod-salmon: new salmon ' . $xml);
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' LIMIT 1",
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 ');
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 // If we're talking to status.net or one of their ilk, they aren't following the magic envelope spec
79 // and only signed the data element. We'll be nice and let them validate anyway.
81 $stnet_signed_data = $data;
82 $signed_data = $data . '.' . base64url_encode($type) . '.' . base64url_encode($encoding) . '.' . base64url_encode($alg);
85 $data = base64url_decode($data);
87 // Remove the xml declaration
88 $data = preg_replace('/\<\?xml[^\?].*\?\>/','',$data);
90 // Create a fake feed wrapper so simplepie doesn't choke
92 $tpl = load_view_file('view/fake_feed.tpl');
94 $base = substr($data,strpos($data,'<entry'));
96 $feedxml = $tpl . $base . '</feed>';
98 logger('mod-salmon: Processed feed: ' . $feedxml);
100 // Now parse it like a normal atom feed to scrape out the author URI
102 $feed = new SimplePie();
103 $feed->set_raw_data($feedxml);
104 $feed->enable_order_by_date(false);
107 logger('mod-salmon: Feed parsed.');
109 if($feed->get_item_quantity()) {
110 foreach($feed->get_items() as $item) {
111 $author = $item->get_author();
112 $author_link = unxmlify($author->get_link());
118 logger('mod-salmon: Could not retrieve author URI.');
122 // Once we have the author URI, go to the web and try to find their public key
124 logger('mod-salmon: Fetching key for ' . $author_link );
127 $key = get_salmon_key($author_link,$keyhash);
130 logger('mod-salmon: Could not retrieve author key.');
134 // Setup RSA stuff to verify the signature
136 set_include_path(get_include_path() . PATH_SEPARATOR . 'phpsec');
138 require_once('phpsec/Crypt/RSA.php');
140 $key_info = explode('.',$key);
142 $m = base64url_decode($key_info[1]);
143 $e = base64url_decode($key_info[2]);
145 logger('mod-salmon: key details: ' . print_r($key_info,true));
147 $rsa = new CRYPT_RSA();
148 $rsa->signatureMode = CRYPT_RSA_SIGNATURE_PKCS1;
149 $rsa->setHash('sha256');
151 $rsa->modulus = new Math_BigInteger($m, 256);
152 $rsa->k = strlen($rsa->modulus->toBytes());
153 $rsa->exponent = new Math_BigInteger($e, 256);
155 // We should have everything we need now. Let's see if it verifies.
156 // If it fails with the proper data format, try again using just the data
159 $verify = $rsa->verify($signed_data,$signature);
162 logger('mod-salmon: message did not verify using protocol. Trying statusnet hack.');
163 $verify = $rsa->verify($stnet_signed_data,$signature);
167 logger('mod-salmon: Message did not verify. Discarding.');
171 logger('mod-salmon: Message verified.');
176 * If we reached this point, the message is good. Now let's figure out if the author is allowed to send us stuff.
180 $r = q("SELECT * FROM `contact` WHERE `network` = 'stat' AND ( `url` = '%s' OR `lrdd` = '%s')
181 AND `uid` = %d LIMIT 1",
184 intval($importer['uid'])
187 logger('mod-salmon: Author unknown to us.');
189 if((count($r)) && ($r[0]['readonly'])) {
190 logger('mod-salmon: Ignoring this author.');
195 require_once('include/items.php');
197 // Placeholder for hub discovery. We shouldn't find any hubs
198 // since we supplied the fake feed header - and it doesn't have any.
202 // consume_feed will only accept a follow activity from this person if there is no contact record.
204 consume_feed($feedxml,$importer,((count($r)) ? $r[0] : null),$hub);