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.
 
 
 
 
 
 

212 lines
5.3 KiB

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