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) {
20 header($_SERVER["SERVER_PROTOCOL"] . ' ' . $val . ' ' . $err);
25 function salmon_post(&$a) {
27 $xml = file_get_contents('php://input');
29 $debugging = get_config('system','debugging');
31 file_put_contents('salmon.out','New Salmon: ' . $xml . "\n",FILE_APPEND);
33 $nick = (($a->argc > 1) ? notags(trim($a->argv[1])) : '');
34 $mentions = (($a->argc > 2 && $a->argv[2] === 'mention') ? true : false);
36 $r = q("SELECT * FROM `user` WHERE `nickname` = '%s' LIMIT 1",
46 $dom = simplexml_load_string($xml,'SimpleXMLElement',0,NAMESPACE_SALMON_ME);
50 file_put_contents('salmon.out', "\n" . print_r($dom,true) . "\n" , FILE_APPEND);
52 // figure out where in the DOM tree our data is hiding
54 if($dom->provenance->data)
55 $base = $dom->provenance;
56 elseif($dom->env->data)
63 file_put_contents('salmon.out', "\n" . 'Unable to find salmon data in XML' . "\n" , FILE_APPEND);
67 // Stash the signature away for now. We have to find their key or it won't be good for anything.
70 $signature = base64url_decode($base->sig);
73 file_put_contents('salmon.out', "\n" . 'Encoded Signature: ' . $base->sig . "\n" , FILE_APPEND);
77 // strip whitespace so our data element will return to one big base64 blob
78 $data = str_replace(array(" ","\t","\r","\n"),array("","","",""),$base->data);
80 // stash away some other stuff for later
82 $type = $base->data[0]->attributes()->type[0];
83 $keyhash = $base->sig[0]->attributes()->keyhash[0];
84 $encoding = $base->encoding;
87 // If we're talking to status.net or one of their ilk, they aren't following the magic envelope spec
88 // and only signed the data element. We'll be nice and let them validate anyway.
90 $stnet_signed_data = $data;
91 $signed_data = $data . '.' . base64url_encode($type) . '.' . base64url_encode($encoding) . '.' . base64url_encode($alg);
94 $data = base64url_decode($data);
96 // Remove the xml declaration
97 $data = preg_replace('/\<\?xml[^\?].*\?\>/','',$data);
99 // Create a fake feed wrapper so simplepie doesn't choke
101 $tpl = load_view_file('view/fake_feed.tpl');
103 $base = substr($data,strpos($data,'<entry'));
105 $feedxml = $tpl . $base . '</feed>';
108 file_put_contents('salmon.out', 'Processed feed: ' . $feedxml . "\n", FILE_APPEND);
111 // Now parse it like a normal atom feed to scrape out the author URI
113 $feed = new SimplePie();
114 $feed->set_raw_data($feedxml);
115 $feed->enable_order_by_date(false);
119 file_put_contents('salmon.out', "\n" . 'Feed parsed.' . "\n", FILE_APPEND);
123 if($feed->get_item_quantity()) {
124 foreach($feed->get_items() as $item) {
125 $author = $item->get_author();
126 $author_link = unxmlify($author->get_link());
133 file_put_contents('salmon.out',"\n" . 'Could not retrieve author URI.' . "\n", FILE_APPEND);
137 // Once we have the author URI, go to the web and try to find their public key
140 file_put_contents('salmon.out', "\n" . 'Fetching key for ' . $author_link . "\n", FILE_APPEND);
143 $key = get_salmon_key($author_link,$keyhash);
147 file_put_contents('salmon.out',"\n" . 'Could not retrieve author key.' . "\n", FILE_APPEND);
151 // Setup RSA stuff to verify the signature
153 set_include_path(get_include_path() . PATH_SEPARATOR . 'phpsec');
155 require_once('phpsec/Crypt/RSA.php');
157 $key_info = explode('.',$key);
159 $m = base64url_decode($key_info[1]);
160 $e = base64url_decode($key_info[2]);
163 file_put_contents('salmon.out',"\n" . print_r($key_info,true) . "\n", FILE_APPEND);
165 $rsa = new CRYPT_RSA();
166 $rsa->signatureMode = CRYPT_RSA_SIGNATURE_PKCS1;
167 $rsa->setHash('sha256');
169 $rsa->modulus = new Math_BigInteger($m, 256);
170 $rsa->k = strlen($rsa->modulus->toBytes());
171 $rsa->exponent = new Math_BigInteger($e, 256);
173 // We should have everything we need now. Let's see if it verifies.
174 // If it fails with the proper data format, try again using just the data
177 $verify = $rsa->verify($signed_data,$signature);
180 $verify = $rsa->verify($stnet_signed_data,$signature);
184 file_put_contents('salmon.out',"\n" . 'Message did not verify. Discarding.' . "\n", FILE_APPEND);
189 file_put_contents('salmon.out',"\n" . 'Message verified.' . "\n", FILE_APPEND);
194 * If we reached this point, the message is good. Now let's figure out if the author is allowed to send us stuff.
198 $r = q("SELECT * FROM `contact` WHERE `network` = 'stat' AND `lrdd` = '%s' AND `uid` = %d LIMIT 1",
200 intval($importer['uid'])
204 file_put_contents('salmon.out',"\n" . 'Author unknown to us.' . "\n", FILE_APPEND);
208 require_once('include/items.php');
210 // Placeholder for hub discovery. We shouldn't find any hubs
211 // since we supplied the fake feed header - and it doesn't have any.
215 consume_feed($feedxml,$importer,$r[0],$hub);