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.
 
 
 
 
 
 

115 lines
2.7 KiB

  1. <?php
  2. require_once("boot.php");
  3. function handle_pubsubhubbub() {
  4. global $a, $db;
  5. logger('start');
  6. // We'll push to each subscriber that has push > 0,
  7. // i.e. there has been an update (set in notifier.php).
  8. $r = q("SELECT * FROM `push_subscriber` WHERE `push` > 0");
  9. foreach($r as $rr) {
  10. $params = get_feed_for($a, '', $rr['nickname'], $rr['last_update'], 0, true);
  11. $hmac_sig = hash_hmac("sha1", $params, $rr['secret']);
  12. $headers = array("Content-type: application/atom+xml",
  13. sprintf("Link: <%s>;rel=hub," .
  14. "<%s>;rel=self",
  15. $a->get_baseurl() . '/pubsubhubbub',
  16. $rr['topic']),
  17. "X-Hub-Signature: sha1=" . $hmac_sig);
  18. logger('POST '. print_r($headers, true)."\n".$params, LOGGER_DEBUG);
  19. post_url($rr['callback_url'], $params, $headers);
  20. $ret = $a->get_curl_code();
  21. if ($ret >= 200 && $ret <= 299) {
  22. logger('successfully pushed to '.$rr['callback_url']);
  23. // set last_update to "now", and reset push=0
  24. $date_now = datetime_convert('UTC','UTC','now','Y-m-d H:i:s');
  25. q("UPDATE `push_subscriber` SET `push` = 0, last_update = '%s' WHERE id = %d",
  26. dbesc($date_now),
  27. intval($rr['id']));
  28. } else {
  29. logger('error when pushing to '.$rr['callback_url'].' HTTP: '.$ret);
  30. // we use the push variable also as a counter, if we failed we
  31. // increment this until some upper limit where we give up
  32. $new_push = intval($rr['push']) + 1;
  33. if ($new_push > 30) // OK, let's give up
  34. $new_push = 0;
  35. q("UPDATE `push_subscriber` SET `push` = %d, last_update = '%s' WHERE id = %d",
  36. $new_push,
  37. dbesc($date_now),
  38. intval($rr['id']));
  39. }
  40. }
  41. logger('done');
  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. require_once('include/pidfile.php');
  56. load_config('config');
  57. load_config('system');
  58. $lockpath = get_lockpath();
  59. if ($lockpath != '') {
  60. $pidfile = new pidfile($lockpath, 'pubsubpublish');
  61. if($pidfile->is_already_running()) {
  62. logger("Already running");
  63. if ($pidfile->running_time() > 9*60) {
  64. $pidfile->kill();
  65. logger("killed stale process");
  66. // Calling a new instance
  67. proc_run('php',"include/pubsubpublish.php");
  68. }
  69. return;
  70. }
  71. }
  72. $a->set_baseurl(get_config('system','url'));
  73. load_hooks();
  74. if($argc > 1)
  75. $pubsubpublish_id = intval($argv[1]);
  76. else
  77. $pubsubpublish_id = 0;
  78. handle_pubsubhubbub();
  79. return;
  80. }
  81. if (array_search(__file__,get_included_files())===0){
  82. pubsubpublish_run($_SERVER["argv"],$_SERVER["argc"]);
  83. killme();
  84. }