8 require_once('include/salmon.php');
9 require_once('library/simplepie/simplepie.inc');
11 function receive_return($val) {
15 if($val >= 200 && $val < 300)
18 logger('mod-diaspora returns ' . $val);
19 header($_SERVER["SERVER_PROTOCOL"] . ' ' . $val . ' ' . $err);
24 function receive_post(&$a) {
26 if($a->argc != 3 || $a->argv[1] !== 'users')
31 $r = q("SELECT * FROM `user` WHERE `guid` = '%s' LIMIT 1",
41 logger('mod-diaspora: new salmon ' . $xml, LOGGER_DATA);
48 $dom = simplexml_load_string($xml,'SimpleXMLElement',0,NAMESPACE_SALMON_ME);
50 // figure out where in the DOM tree our data is hiding
52 if($dom->provenance->data)
53 $base = $dom->provenance;
54 elseif($dom->env->data)
60 logger('mod-diaspora: unable to locate salmon data in xml ');
64 // Stash the signature away for now. We have to find their key or it won't be good for anything.
65 $signature = base64url_decode($base->sig);
69 // strip whitespace so our data element will return to one big base64 blob
70 $data = str_replace(array(" ","\t","\r","\n"),array("","","",""),$base->data);
72 // stash away some other stuff for later
74 $type = $base->data[0]->attributes()->type[0];
75 $keyhash = $base->sig[0]->attributes()->keyhash[0];
76 $encoding = $base->encoding;
79 $signed_data = $data . '.' . base64url_encode($type) . '.' . base64url_encode($encoding) . '.' . base64url_encode($alg);
82 $data = base64url_decode($data);
84 // Remove the xml declaration
85 $data = preg_replace('/\<\?xml[^\?].*\?\>/','',$data);
87 // Create a fake feed wrapper so simplepie doesn't choke
89 $tpl = get_markup_template('fake_feed.tpl');
91 $base = substr($data,strpos($data,'<entry'));
93 $feedxml = $tpl . $base . '</feed>';
95 logger('mod-diaspora: Processed feed: ' . $feedxml);
97 // Now parse it like a normal atom feed to scrape out the author URI
99 $feed = new SimplePie();
100 $feed->set_raw_data($feedxml);
101 $feed->enable_order_by_date(false);
104 logger('mod-diaspora: Feed parsed.');
106 if($feed->get_item_quantity()) {
107 foreach($feed->get_items() as $item) {
108 $author = $item->get_author();
109 $author_link = unxmlify($author->get_link());
115 logger('mod-diaspora: Could not retrieve author URI.');
119 // Once we have the author URI, go to the web and try to find their public key
121 logger('mod-salmon: Fetching key for ' . $author_link );
124 $key = get_salmon_key($author_link,$keyhash);
127 logger('mod-salmon: Could not retrieve author key.');
131 // Setup RSA stuff to verify the signature
133 set_include_path(get_include_path() . PATH_SEPARATOR . 'library' . PATH_SEPARATOR . 'phpsec');
135 require_once('library/phpsec/Crypt/RSA.php');
137 $key_info = explode('.',$key);
139 $m = base64url_decode($key_info[1]);
140 $e = base64url_decode($key_info[2]);
142 logger('mod-salmon: key details: ' . print_r($key_info,true));
144 $rsa = new CRYPT_RSA();
145 $rsa->signatureMode = CRYPT_RSA_SIGNATURE_PKCS1;
146 $rsa->setHash('sha256');
148 $rsa->modulus = new Math_BigInteger($m, 256);
149 $rsa->k = strlen($rsa->modulus->toBytes());
150 $rsa->exponent = new Math_BigInteger($e, 256);
152 $verify = $rsa->verify($signed_data,$signature);
155 logger('mod-diaspora: Message did not verify. Discarding.');
159 logger('mod-diaspora: Message verified.');
161 /* decrypt the sucker */
168 * If we reached this point, the message is good. Now let's figure out if the author is allowed to send us stuff.
172 $r = q("SELECT * FROM `contact` WHERE `network` = 'dspr' AND ( `url` = '%s' OR `alias` = '%s')
173 AND `uid` = %d LIMIT 1",
176 intval($importer['uid'])
179 logger('mod-diaspora: Author unknown to us.');
182 // is this a follower? Or have we ignored the person?
183 // If so we can not accept this post.
185 if((count($r)) && (($r[0]['readonly']) || ($r[0]['rel'] == REL_VIP) || ($r[0]['blocked']))) {
186 logger('mod-diaspora: Ignoring this author.');
191 require_once('include/items.php');
193 // Placeholder for hub discovery. We shouldn't find any hubs
194 // since we supplied the fake feed header - and it doesn't have any.
200 * anti-spam measure: consume_feed will accept a follow activity from
201 * this person (and nothing else) if there is no existing contact record.
205 $contact_rec = ((count($r)) ? $r[0] : null);
207 consume_feed($feedxml,$importer,$contact_rec,$hub);