Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

214 lines
4.9 KiB

  1. <?php
  2. /**
  3. * Diaspora endpoint
  4. */
  5. require_once('include/salmon.php');
  6. require_once('library/simplepie/simplepie.inc');
  7. function receive_return($val) {
  8. if($val >= 400)
  9. $err = 'Error';
  10. if($val >= 200 && $val < 300)
  11. $err = 'OK';
  12. logger('mod-diaspora returns ' . $val);
  13. header($_SERVER["SERVER_PROTOCOL"] . ' ' . $val . ' ' . $err);
  14. killme();
  15. }
  16. function receive_post(&$a) {
  17. if($a->argc != 3 || $a->argv[1] !== 'users')
  18. receive_return(500);
  19. $guid = $a->argv[2];
  20. $r = q("SELECT * FROM `user` WHERE `guid` = '%s' LIMIT 1",
  21. dbesc($guid)
  22. );
  23. if(! count($r))
  24. salmon_return(500);
  25. $importer = $r[0];
  26. $xml = $_POST['xml'];
  27. logger('mod-diaspora: new salmon ' . $xml, LOGGER_DATA);
  28. if(! $xml)
  29. receive_return(500);
  30. // parse the xml
  31. $dom = simplexml_load_string($xml,'SimpleXMLElement',0,NAMESPACE_SALMON_ME);
  32. // figure out where in the DOM tree our data is hiding
  33. if($dom->provenance->data)
  34. $base = $dom->provenance;
  35. elseif($dom->env->data)
  36. $base = $dom->env;
  37. elseif($dom->data)
  38. $base = $dom;
  39. if(! $base) {
  40. logger('mod-diaspora: unable to locate salmon data in xml ');
  41. receive_return(400);
  42. }
  43. // Stash the signature away for now. We have to find their key or it won't be good for anything.
  44. $signature = base64url_decode($base->sig);
  45. // unpack the data
  46. // strip whitespace so our data element will return to one big base64 blob
  47. $data = str_replace(array(" ","\t","\r","\n"),array("","","",""),$base->data);
  48. // stash away some other stuff for later
  49. $type = $base->data[0]->attributes()->type[0];
  50. $keyhash = $base->sig[0]->attributes()->keyhash[0];
  51. $encoding = $base->encoding;
  52. $alg = $base->alg;
  53. $signed_data = $data . '.' . base64url_encode($type) . '.' . base64url_encode($encoding) . '.' . base64url_encode($alg);
  54. // decode the data
  55. $data = base64url_decode($data);
  56. // Remove the xml declaration
  57. $data = preg_replace('/\<\?xml[^\?].*\?\>/','',$data);
  58. // Create a fake feed wrapper so simplepie doesn't choke
  59. $tpl = get_markup_template('fake_feed.tpl');
  60. $base = substr($data,strpos($data,'<entry'));
  61. $feedxml = $tpl . $base . '</feed>';
  62. logger('mod-diaspora: Processed feed: ' . $feedxml);
  63. // Now parse it like a normal atom feed to scrape out the author URI
  64. $feed = new SimplePie();
  65. $feed->set_raw_data($feedxml);
  66. $feed->enable_order_by_date(false);
  67. $feed->init();
  68. logger('mod-diaspora: Feed parsed.');
  69. if($feed->get_item_quantity()) {
  70. foreach($feed->get_items() as $item) {
  71. $author = $item->get_author();
  72. $author_link = unxmlify($author->get_link());
  73. break;
  74. }
  75. }
  76. if(! $author_link) {
  77. logger('mod-diaspora: Could not retrieve author URI.');
  78. receive_return(400);
  79. }
  80. // Once we have the author URI, go to the web and try to find their public key
  81. logger('mod-salmon: Fetching key for ' . $author_link );
  82. $key = get_salmon_key($author_link,$keyhash);
  83. if(! $key) {
  84. logger('mod-salmon: Could not retrieve author key.');
  85. receive_return(400);
  86. }
  87. // Setup RSA stuff to verify the signature
  88. set_include_path(get_include_path() . PATH_SEPARATOR . 'library' . PATH_SEPARATOR . 'phpsec');
  89. require_once('library/phpsec/Crypt/RSA.php');
  90. $key_info = explode('.',$key);
  91. $m = base64url_decode($key_info[1]);
  92. $e = base64url_decode($key_info[2]);
  93. logger('mod-salmon: key details: ' . print_r($key_info,true));
  94. $rsa = new CRYPT_RSA();
  95. $rsa->signatureMode = CRYPT_RSA_SIGNATURE_PKCS1;
  96. $rsa->setHash('sha256');
  97. $rsa->modulus = new Math_BigInteger($m, 256);
  98. $rsa->k = strlen($rsa->modulus->toBytes());
  99. $rsa->exponent = new Math_BigInteger($e, 256);
  100. $verify = $rsa->verify($signed_data,$signature);
  101. if(! $verify) {
  102. logger('mod-diaspora: Message did not verify. Discarding.');
  103. receive_return(400);
  104. }
  105. logger('mod-diaspora: Message verified.');
  106. /* decrypt the sucker */
  107. /*
  108. // TODO
  109. */
  110. /*
  111. *
  112. * If we reached this point, the message is good. Now let's figure out if the author is allowed to send us stuff.
  113. *
  114. */
  115. $r = q("SELECT * FROM `contact` WHERE `network` = 'dspr' AND ( `url` = '%s' OR `alias` = '%s')
  116. AND `uid` = %d LIMIT 1",
  117. dbesc($author_link),
  118. dbesc($author_link),
  119. intval($importer['uid'])
  120. );
  121. if(! count($r)) {
  122. logger('mod-diaspora: Author unknown to us.');
  123. }
  124. // is this a follower? Or have we ignored the person?
  125. // If so we can not accept this post.
  126. if((count($r)) && (($r[0]['readonly']) || ($r[0]['rel'] == REL_VIP) || ($r[0]['blocked']))) {
  127. logger('mod-diaspora: Ignoring this author.');
  128. receive_return(202);
  129. // NOTREACHED
  130. }
  131. require_once('include/items.php');
  132. // Placeholder for hub discovery. We shouldn't find any hubs
  133. // since we supplied the fake feed header - and it doesn't have any.
  134. $hub = '';
  135. /**
  136. *
  137. * anti-spam measure: consume_feed will accept a follow activity from
  138. * this person (and nothing else) if there is no existing contact record.
  139. *
  140. */
  141. $contact_rec = ((count($r)) ? $r[0] : null);
  142. consume_feed($feedxml,$importer,$contact_rec,$hub);
  143. receive_return(200);
  144. }