Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

pubsubpublish.php 2.1 KiB

3 lat temu
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. use Friendica\Core\Config;
  3. require_once('include/items.php');
  4. require_once('include/ostatus.php');
  5. function pubsubpublish_run(&$argv, &$argc){
  6. if ($argc > 1) {
  7. $pubsubpublish_id = intval($argv[1]);
  8. } else {
  9. // We'll push to each subscriber that has push > 0,
  10. // i.e. there has been an update (set in notifier.php).
  11. $r = q("SELECT `id`, `callback_url` FROM `push_subscriber` WHERE `push` > 0");
  12. foreach ($r as $rr) {
  13. logger("Publish feed to ".$rr["callback_url"], LOGGER_DEBUG);
  14. proc_run(PRIORITY_HIGH, 'include/pubsubpublish.php', $rr["id"]);
  15. }
  16. }
  17. handle_pubsubhubbub($pubsubpublish_id);
  18. return;
  19. }
  20. function handle_pubsubhubbub($id) {
  21. global $a;
  22. $r = q("SELECT * FROM `push_subscriber` WHERE `id` = %d", intval($id));
  23. if (!$r)
  24. return;
  25. else
  26. $rr = $r[0];
  27. logger("Generate feed of user ".$rr['nickname']." to ".$rr['callback_url']." - last updated ".$rr['last_update'], LOGGER_DEBUG);
  28. $params = ostatus::feed($a, $rr['nickname'], $rr['last_update']);
  29. $hmac_sig = hash_hmac("sha1", $params, $rr['secret']);
  30. $headers = array("Content-type: application/atom+xml",
  31. sprintf("Link: <%s>;rel=hub,<%s>;rel=self",
  32. App::get_baseurl().'/pubsubhubbub',
  33. $rr['topic']),
  34. "X-Hub-Signature: sha1=".$hmac_sig);
  35. logger('POST '.print_r($headers, true)."\n".$params, LOGGER_DEBUG);
  36. post_url($rr['callback_url'], $params, $headers);
  37. $ret = $a->get_curl_code();
  38. if ($ret >= 200 && $ret <= 299) {
  39. logger('successfully pushed to '.$rr['callback_url']);
  40. // set last_update to "now", and reset push=0
  41. $date_now = datetime_convert('UTC','UTC','now','Y-m-d H:i:s');
  42. q("UPDATE `push_subscriber` SET `push` = 0, last_update = '%s' WHERE id = %d",
  43. dbesc($date_now),
  44. intval($rr['id']));
  45. } else {
  46. logger('error when pushing to '.$rr['callback_url'].' HTTP: '.$ret);
  47. // we use the push variable also as a counter, if we failed we
  48. // increment this until some upper limit where we give up
  49. $new_push = intval($rr['push']) + 1;
  50. if ($new_push > 30) // OK, let's give up
  51. $new_push = 0;
  52. q("UPDATE `push_subscriber` SET `push` = %d WHERE id = %d",
  53. $new_push,
  54. intval($rr['id']));
  55. }
  56. }