4 // There is a lot of debug stuff in here because this is quite a
5 // complicated process to try and sort out.
7 require_once('include/salmon.php');
8 require_once('library/simplepie/simplepie.inc');
10 function salmon_return($val) {
14 if($val >= 200 && $val < 300)
17 logger('mod-salmon returns ' . $val);
18 header($_SERVER["SERVER_PROTOCOL"] . ' ' . $val . ' ' . $err);
23 function salmon_post(&$a) {
25 $xml = file_get_contents('php://input');
27 logger('mod-salmon: new salmon ' . $xml);
29 $nick = (($a->argc > 1) ? notags(trim($a->argv[1])) : '');
30 $mentions = (($a->argc > 2 && $a->argv[2] === 'mention') ? true : false);
32 $r = q("SELECT * FROM `user` WHERE `nickname` = '%s' LIMIT 1",
42 $dom = simplexml_load_string($xml,'SimpleXMLElement',0,NAMESPACE_SALMON_ME);
44 // figure out where in the DOM tree our data is hiding
46 if($dom->provenance->data)
47 $base = $dom->provenance;
48 elseif($dom->env->data)
54 logger('mod-salmon: unable to locate salmon data in xml ');
58 // Stash the signature away for now. We have to find their key or it won't be good for anything.
61 $signature = base64url_decode($base->sig);
65 // strip whitespace so our data element will return to one big base64 blob
66 $data = str_replace(array(" ","\t","\r","\n"),array("","","",""),$base->data);
68 // stash away some other stuff for later
70 $type = $base->data[0]->attributes()->type[0];
71 $keyhash = $base->sig[0]->attributes()->keyhash[0];
72 $encoding = $base->encoding;
75 // If we're talking to status.net or one of their ilk, they aren't following the magic envelope spec
76 // and only signed the data element. We'll be nice and let them validate anyway.
78 $stnet_signed_data = $data;
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-salmon: 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-salmon: 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-salmon: 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 // We should have everything we need now. Let's see if it verifies.
153 // If it fails with the proper data format, try again using just the data
156 $verify = $rsa->verify($signed_data,$signature);
159 logger('mod-salmon: message did not verify using protocol. Trying statusnet hack.');
160 $verify = $rsa->verify($stnet_signed_data,$signature);
164 logger('mod-salmon: Message did not verify. Discarding.');
168 logger('mod-salmon: Message verified.');
173 * If we reached this point, the message is good. Now let's figure out if the author is allowed to send us stuff.
177 $r = q("SELECT * FROM `contact` WHERE `network` = 'stat' AND ( `url` = '%s' OR `alias` = '%s')
178 AND `uid` = %d LIMIT 1",
181 intval($importer['uid'])
184 logger('mod-salmon: Author unknown to us.');
187 // is this a follower? Or have we ignored the person?
188 // If so we can not accept this post.
190 if((count($r)) && (($r[0]['readonly']) || ($r[0]['rel'] == REL_VIP) || ($r[0]['blocked']))) {
191 logger('mod-salmon: Ignoring this author.');
196 require_once('include/items.php');
198 // Placeholder for hub discovery. We shouldn't find any hubs
199 // since we supplied the fake feed header - and it doesn't have any.
205 * anti-spam measure: consume_feed will accept a follow activity from
206 * this person (and nothing else) if there is no existing contact record.
210 $contact_rec = ((count($r)) ? $r[0] : null);
212 consume_feed($feedxml,$importer,$contact_rec,$hub);