4 // TODO: pass keyhash to key discovery
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);
72 file_put_contents('salmon.out', "\n" . 'Encoded Signature: ' . $base->sig . "\n" , FILE_APPEND);
74 // unpack our data element.
77 $data = str_replace(array(" ","\t","\r","\n"),array("","","",""),$base->data);
78 $type = $base->data[0]->attributes()->type[0];
79 $encoding = $base->encoding;
83 // . '.' . base64url_encode($type) . '.' . base64url_encode($encoding) . '.' . base64url_encode($alg);
85 $data = base64url_decode($data);
88 file_put_contents('salmon.out', "\n" . 'Signed data:>>>' . $signed_data . "<<<\n" , FILE_APPEND);
90 // Remove the xml declaration
91 $data = preg_replace('/\<\?xml[^\?].*\?\>/','',$data);
93 // Create a fake feed wrapper so simplepie doesn't choke
95 $tpl = load_view_file('view/fake_feed.tpl');
97 $base = substr($data,strpos($data,'<entry'));
99 $feedxml = $tpl . $base . '</feed>';
102 file_put_contents('salmon.out', 'Processed feed: ' . $feedxml . "\n", FILE_APPEND);
105 // Now parse it like a normal atom feed to scrape out the author URI
107 $feed = new SimplePie();
108 $feed->set_raw_data($feedxml);
109 $feed->enable_order_by_date(false);
113 file_put_contents('salmon.out', "\n" . 'Feed parsed.' . "\n", FILE_APPEND);
117 if($feed->get_item_quantity()) {
118 foreach($feed->get_items() as $item) {
119 $author = $item->get_author();
120 $author_link = unxmlify($author->get_link());
127 file_put_contents('salmon.out',"\n" . 'Could not retrieve author URI.' . "\n", FILE_APPEND);
131 // Once we have the author URI, go to the web and find their public key
134 file_put_contents('salmon.out', "\n" . 'Fetching key for ' . $author_link . "\n", FILE_APPEND);
137 $key = get_salmon_key($author_link,$keyhash);
141 file_put_contents('salmon.out',"\n" . 'Could not retrieve author key.' . "\n", FILE_APPEND);
145 // Setup RSA stuff to verify the signature
147 set_include_path(get_include_path() . PATH_SEPARATOR . 'phpsec');
149 require_once('phpsec/Crypt/RSA.php');
151 $key_info = explode('.',$key);
153 $m = base64url_decode($key_info[1]);
154 $e = base64url_decode($key_info[2]);
156 file_put_contents('salmon.out',"\n" . print_r($key_info,true) . "\n", FILE_APPEND);
158 $rsa = new CRYPT_RSA();
159 $rsa->signatureMode = CRYPT_RSA_SIGNATURE_PKCS1;
160 $rsa->setHash('sha256');
162 $rsa->modulus = new Math_BigInteger($m, 256);
163 $rsa->k = strlen($rsa->modulus->toBytes());
164 $rsa->exponent = new Math_BigInteger($e, 256);
166 // We should have everything we need now. Let's see if it verifies.
168 $verify = $rsa->verify($signed_data,$signature);
172 file_put_contents('salmon.out',"\n" . 'Message did not verify. Discarding.' . "\n", FILE_APPEND);
177 file_put_contents('salmon.out',"\n" . 'Message verified.' . "\n", FILE_APPEND);
182 * If we reached this point, the message is good. Now let's figure out if the author is allowed to send us stuff.
186 $r = q("SELECT * FROM `contact` WHERE `network` = 'stat' AND `lrdd` = '%s' AND `uid` = %d LIMIT 1",
188 intval($importer['uid'])
192 file_put_contents('salmon.out',"\n" . 'Author unknown to us.' . "\n", FILE_APPEND);
197 require_once('include/items.php');
201 consume_feed($feedxml,$importer,$r[0],$hub);