9 require_once('include/salmon.php');
10 require_once('library/simplepie/simplepie.inc');
12 function receive_return($val) {
16 if($val >= 200 && $val < 300)
19 logger('mod-diaspora returns ' . $val);
20 header($_SERVER["SERVER_PROTOCOL"] . ' ' . $val . ' ' . $err);
25 function receive_post(&$a) {
27 if($a->argc != 3 || $a->argv[1] !== 'users')
32 $r = q("SELECT * FROM `user` WHERE `guid` = '%s' LIMIT 1",
42 logger('mod-diaspora: new salmon ' . $xml, LOGGER_DATA);
49 $dom = simplexml_load_string($xml,'SimpleXMLElement',0,NAMESPACE_SALMON_ME);
51 // figure out where in the DOM tree our data is hiding
53 if($dom->provenance->data)
54 $base = $dom->provenance;
55 elseif($dom->env->data)
61 logger('mod-diaspora: unable to locate salmon data in xml ');
65 // Stash the signature away for now. We have to find their key or it won't be good for anything.
66 $signature = base64url_decode($base->sig);
70 // strip whitespace so our data element will return to one big base64 blob
71 $data = str_replace(array(" ","\t","\r","\n"),array("","","",""),$base->data);
73 // stash away some other stuff for later
75 $type = $base->data[0]->attributes()->type[0];
76 $keyhash = $base->sig[0]->attributes()->keyhash[0];
77 $encoding = $base->encoding;
80 $signed_data = $data . '.' . base64url_encode($type) . '.' . base64url_encode($encoding) . '.' . base64url_encode($alg);
83 $data = base64url_decode($data);
85 // Remove the xml declaration
86 $data = preg_replace('/\<\?xml[^\?].*\?\>/','',$data);
88 // Create a fake feed wrapper so simplepie doesn't choke
90 $tpl = get_markup_template('fake_feed.tpl');
92 $base = substr($data,strpos($data,'<entry'));
94 $feedxml = $tpl . $base . '</feed>';
96 logger('mod-diaspora: Processed feed: ' . $feedxml);
98 // Now parse it like a normal atom feed to scrape out the author URI
100 $feed = new SimplePie();
101 $feed->set_raw_data($feedxml);
102 $feed->enable_order_by_date(false);
105 logger('mod-diaspora: Feed parsed.');
107 if($feed->get_item_quantity()) {
108 foreach($feed->get_items() as $item) {
109 $author = $item->get_author();
110 $author_link = unxmlify($author->get_link());
116 logger('mod-diaspora: Could not retrieve author URI.');
120 // Once we have the author URI, go to the web and try to find their public key
122 logger('mod-salmon: Fetching key for ' . $author_link );
125 $key = get_salmon_key($author_link,$keyhash);
128 logger('mod-salmon: Could not retrieve author key.');
132 // Setup RSA stuff to verify the signature
134 set_include_path(get_include_path() . PATH_SEPARATOR . 'library' . PATH_SEPARATOR . 'phpsec');
136 require_once('library/phpsec/Crypt/RSA.php');
138 $key_info = explode('.',$key);
140 $m = base64url_decode($key_info[1]);
141 $e = base64url_decode($key_info[2]);
143 logger('mod-salmon: key details: ' . print_r($key_info,true));
145 $rsa = new CRYPT_RSA();
146 $rsa->signatureMode = CRYPT_RSA_SIGNATURE_PKCS1;
147 $rsa->setHash('sha256');
149 $rsa->modulus = new Math_BigInteger($m, 256);
150 $rsa->k = strlen($rsa->modulus->toBytes());
151 $rsa->exponent = new Math_BigInteger($e, 256);
153 $verify = $rsa->verify($signed_data,$signature);
156 logger('mod-diaspora: Message did not verify. Discarding.');
160 logger('mod-diaspora: Message verified.');
162 /* decrypt the sucker */
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` = 'dspr' AND ( `url` = '%s' OR `alias` = '%s')
174 AND `uid` = %d LIMIT 1",
177 intval($importer['uid'])
180 logger('mod-diaspora: Author unknown to us.');
183 // is this a follower? Or have we ignored the person?
184 // If so we can not accept this post.
186 if((count($r)) && (($r[0]['readonly']) || ($r[0]['rel'] == REL_VIP) || ($r[0]['blocked']))) {
187 logger('mod-diaspora: Ignoring this author.');
192 require_once('include/items.php');
194 // Placeholder for hub discovery. We shouldn't find any hubs
195 // since we supplied the fake feed header - and it doesn't have any.
201 * anti-spam measure: consume_feed will accept a follow activity from
202 * this person (and nothing else) if there is no existing contact record.
206 $contact_rec = ((count($r)) ? $r[0] : null);
208 consume_feed($feedxml,$importer,$contact_rec,$hub);