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.
 
 
 
 
 
 

124 lines
3.1 KiB

  1. <?php
  2. require_once("boot.php");
  3. require_once("include/ostatus.php");
  4. use \Friendica\Core\Config;
  5. use \Friendica\Core\PConfig;
  6. function handle_pubsubhubbub($id) {
  7. global $a, $db;
  8. $r = q("SELECT * FROM `push_subscriber` WHERE `id` = %d", intval($id));
  9. if (!$r)
  10. return;
  11. else
  12. $rr = $r[0];
  13. logger("Generate feed of user ".$rr['nickname']." to ".$rr['callback_url']." - last updated ".$rr['last_update'], LOGGER_DEBUG);
  14. $params = ostatus::feed($a, $rr['nickname'], $rr['last_update']);
  15. $hmac_sig = hash_hmac("sha1", $params, $rr['secret']);
  16. $headers = array("Content-type: application/atom+xml",
  17. sprintf("Link: <%s>;rel=hub,<%s>;rel=self",
  18. App::get_baseurl().'/pubsubhubbub',
  19. $rr['topic']),
  20. "X-Hub-Signature: sha1=".$hmac_sig);
  21. logger('POST '.print_r($headers, true)."\n".$params, LOGGER_DEBUG);
  22. post_url($rr['callback_url'], $params, $headers);
  23. $ret = $a->get_curl_code();
  24. if ($ret >= 200 && $ret <= 299) {
  25. logger('successfully pushed to '.$rr['callback_url']);
  26. // set last_update to "now", and reset push=0
  27. $date_now = datetime_convert('UTC','UTC','now','Y-m-d H:i:s');
  28. q("UPDATE `push_subscriber` SET `push` = 0, last_update = '%s' WHERE id = %d",
  29. dbesc($date_now),
  30. intval($rr['id']));
  31. } else {
  32. logger('error when pushing to '.$rr['callback_url'].' HTTP: '.$ret);
  33. // we use the push variable also as a counter, if we failed we
  34. // increment this until some upper limit where we give up
  35. $new_push = intval($rr['push']) + 1;
  36. if ($new_push > 30) // OK, let's give up
  37. $new_push = 0;
  38. q("UPDATE `push_subscriber` SET `push` = %d WHERE id = %d",
  39. $new_push,
  40. intval($rr['id']));
  41. }
  42. }
  43. function pubsubpublish_run(&$argv, &$argc){
  44. global $a, $db;
  45. if(is_null($a)){
  46. $a = new App;
  47. }
  48. if(is_null($db)){
  49. @include(".htconfig.php");
  50. require_once("include/dba.php");
  51. $db = new dba($db_host, $db_user, $db_pass, $db_data);
  52. unset($db_host, $db_user, $db_pass, $db_data);
  53. };
  54. require_once('include/items.php');
  55. Config::load();
  56. // Don't check this stuff if the function is called by the poller
  57. if (App::callstack() != "poller_run") {
  58. if (App::is_already_running("pubsubpublish", "include/pubsubpublish.php", 540)) {
  59. return;
  60. }
  61. }
  62. $a->set_baseurl(get_config('system','url'));
  63. load_hooks();
  64. if ($argc > 1) {
  65. $pubsubpublish_id = intval($argv[1]);
  66. }
  67. else {
  68. // We'll push to each subscriber that has push > 0,
  69. // i.e. there has been an update (set in notifier.php).
  70. $r = q("SELECT `id`, `callback_url` FROM `push_subscriber` WHERE `push` > 0");
  71. // Use the delivery interval that is also used for the notifier
  72. $interval = Config::get("system", "delivery_interval", 2);
  73. // If we are using the worker we don't need a delivery interval
  74. if (get_config("system", "worker")) {
  75. $interval = false;
  76. }
  77. foreach ($r as $rr) {
  78. logger("Publish feed to ".$rr["callback_url"], LOGGER_DEBUG);
  79. proc_run(PRIORITY_HIGH, 'include/pubsubpublish.php', $rr["id"]);
  80. if($interval)
  81. @time_sleep_until(microtime(true) + (float) $interval);
  82. }
  83. }
  84. handle_pubsubhubbub($pubsubpublish_id);
  85. return;
  86. }
  87. if (array_search(__file__,get_included_files())===0){
  88. pubsubpublish_run($_SERVER["argv"],$_SERVER["argc"]);
  89. killme();
  90. }