3 * @file src/Worker/PubSubPublish.php
6 namespace Friendica\Worker;
9 use Friendica\Core\System;
10 use Friendica\Core\Config;
11 use Friendica\Core\Worker;
12 use Friendica\Database\DBM;
13 use Friendica\Protocol\OStatus;
14 use Friendica\Util\Network;
15 use Friendica\Util\DateTimeFormat;
18 require_once 'include/items.php';
21 public static function execute($pubsubpublish_id = 0)
25 if ($pubsubpublish_id != 0) {
26 self::publish($pubsubpublish_id);
30 // We'll push to each subscriber that has push > 0,
31 // i.e. there has been an update (set in notifier.php).
32 $subscribers = dba::select('push_subscriber', ['id', 'callback_url'], ["`push` > 0 AND `next_try` < UTC_TIMESTAMP()"]);
34 while ($subscriber = dba::fetch($subscribers)) {
35 logger("Publish feed to " . $subscriber["callback_url"], LOGGER_DEBUG);
36 Worker::add(['priority' => $a->queue['priority'], 'created' => $a->queue['created'], 'dont_fork' => true],
37 'PubSubPublish', (int)$subscriber["id"]);
40 dba::close($subscribers);
43 private static function publish($id) {
46 $subscriber = dba::selectFirst('push_subscriber', [], ['id' => $id]);
47 if (!DBM::is_result($subscriber)) {
51 /// @todo Check server status with PortableContact::checkServer()
52 // Before this can be done we need a way to safely detect the server url.
54 logger("Generate feed of user " . $subscriber['nickname']. " to " . $subscriber['callback_url']. " - last updated " . $subscriber['last_update'], LOGGER_DEBUG);
56 $last_update = $subscriber['last_update'];
57 $params = OStatus::feed($subscriber['nickname'], $last_update);
63 $hmac_sig = hash_hmac("sha1", $params, $subscriber['secret']);
65 $headers = ["Content-type: application/atom+xml",
66 sprintf("Link: <%s>;rel=hub,<%s>;rel=self",
67 System::baseUrl() . '/pubsubhubbub/' . $subscriber['nickname'],
68 $subscriber['topic']),
69 "X-Hub-Signature: sha1=" . $hmac_sig];
71 logger('POST ' . print_r($headers, true) . "\n" . $params, LOGGER_DATA);
73 Network::post($subscriber['callback_url'], $params, $headers);
74 $ret = $a->get_curl_code();
76 $condition = ['id' => $subscriber['id']];
78 if ($ret >= 200 && $ret <= 299) {
79 logger('Successfully pushed to ' . $subscriber['callback_url']);
81 // set last_update to the "created" date of the last item, and reset push=0
82 $fields = ['push' => 0, 'next_try' => NULL_DATE, 'last_update' => $last_update];
83 dba::update('push_subscriber', $fields, $condition);
86 logger('Delivery error when pushing to ' . $subscriber['callback_url'] . ' HTTP: ' . $ret);
88 // we use the push variable also as a counter, if we failed we
89 // increment this until some upper limit where we give up
90 $retrial = $subscriber['push'];
93 dba::update('push_subscriber', ['push' => 0, 'next_try' => NULL_DATE], $condition);
94 logger('Delivery error: Giving up for ' . $subscriber['callback_url'], LOGGER_DEBUG);
96 // Calculate the delay until the next trial
97 $delay = (($retrial + 3) ** 4) + (rand(1, 30) * ($retrial + 1));
98 $next = DateTimeFormat::utc('now + ' . $delay . ' seconds');
100 $retrial = $retrial + 1;
102 dba::update('push_subscriber', ['push' => $retrial, 'next_try' => $next], $condition);
103 logger('Delivery error: Next try (' . $retrial . ') for ' . $subscriber['callback_url'] . ' at ' . $next, LOGGER_DEBUG);