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)
23 if ($pubsubpublish_id == 0) {
27 self::publish($pubsubpublish_id);
30 private static function publish($id) {
33 $subscriber = dba::selectFirst('push_subscriber', [], ['id' => $id]);
34 if (!DBM::is_result($subscriber)) {
38 /// @todo Check server status with PortableContact::checkServer()
39 // Before this can be done we need a way to safely detect the server url.
41 logger("Generate feed of user " . $subscriber['nickname']. " to " . $subscriber['callback_url']. " - last updated " . $subscriber['last_update'], LOGGER_DEBUG);
43 $last_update = $subscriber['last_update'];
44 $params = OStatus::feed($subscriber['nickname'], $last_update);
50 $hmac_sig = hash_hmac("sha1", $params, $subscriber['secret']);
52 $headers = ["Content-type: application/atom+xml",
53 sprintf("Link: <%s>;rel=hub,<%s>;rel=self",
54 System::baseUrl() . '/pubsubhubbub/' . $subscriber['nickname'],
55 $subscriber['topic']),
56 "X-Hub-Signature: sha1=" . $hmac_sig];
58 logger('POST ' . print_r($headers, true) . "\n" . $params, LOGGER_DATA);
60 Network::post($subscriber['callback_url'], $params, $headers);
61 $ret = $a->get_curl_code();
63 $condition = ['id' => $subscriber['id']];
65 if ($ret >= 200 && $ret <= 299) {
66 logger('Successfully pushed to ' . $subscriber['callback_url']);
68 // set last_update to the "created" date of the last item, and reset push=0
69 $fields = ['push' => 0, 'next_try' => NULL_DATE, 'last_update' => $last_update];
70 dba::update('push_subscriber', $fields, $condition);
73 logger('Delivery error when pushing to ' . $subscriber['callback_url'] . ' HTTP: ' . $ret);
75 // we use the push variable also as a counter, if we failed we
76 // increment this until some upper limit where we give up
77 $retrial = $subscriber['push'];
80 dba::update('push_subscriber', ['push' => 0, 'next_try' => NULL_DATE], $condition);
81 logger('Delivery error: Giving up for ' . $subscriber['callback_url'], LOGGER_DEBUG);
83 // Calculate the delay until the next trial
84 $delay = (($retrial + 3) ** 4) + (rand(1, 30) * ($retrial + 1));
85 $next = DateTimeFormat::utc('now + ' . $delay . ' seconds');
87 $retrial = $retrial + 1;
89 dba::update('push_subscriber', ['push' => $retrial, 'next_try' => $next], $condition);
90 logger('Delivery error: Next try (' . $retrial . ') for ' . $subscriber['callback_url'] . ' at ' . $next, LOGGER_DEBUG);