3 * @file src/Worker/PubSubPublish.php
6 namespace Friendica\Worker;
8 use Friendica\BaseObject;
9 use Friendica\Core\System;
10 use Friendica\Database\DBA;
11 use Friendica\Model\PushSubscriber;
12 use Friendica\Protocol\OStatus;
13 use Friendica\Util\Network;
15 require_once 'include/items.php';
19 public static function execute($pubsubpublish_id = 0)
21 if ($pubsubpublish_id == 0) {
25 self::publish($pubsubpublish_id);
28 private static function publish($id)
30 $a = BaseObject::getApp();
32 $subscriber = DBA::selectFirst('push_subscriber', [], ['id' => $id]);
33 if (!DBA::isResult($subscriber)) {
37 /// @todo Check server status with PortableContact::checkServer()
38 // Before this can be done we need a way to safely detect the server url.
40 logger("Generate feed of user " . $subscriber['nickname']. " to " . $subscriber['callback_url']. " - last updated " . $subscriber['last_update'], LOGGER_DEBUG);
42 $last_update = $subscriber['last_update'];
43 $params = OStatus::feed($subscriber['nickname'], $last_update);
49 $hmac_sig = hash_hmac("sha1", $params, $subscriber['secret']);
51 $headers = ["Content-type: application/atom+xml",
52 sprintf("Link: <%s>;rel=hub,<%s>;rel=self",
53 System::baseUrl() . '/pubsubhubbub/' . $subscriber['nickname'],
54 $subscriber['topic']),
55 "X-Hub-Signature: sha1=" . $hmac_sig];
57 logger('POST ' . print_r($headers, true) . "\n" . $params, LOGGER_DATA);
59 $postResult = Network::post($subscriber['callback_url'], $params, $headers);
60 $ret = $postResult->getReturnCode();
62 $condition = ['id' => $subscriber['id']];
64 if ($ret >= 200 && $ret <= 299) {
65 logger('Successfully pushed to ' . $subscriber['callback_url']);
67 PushSubscriber::reset($subscriber['id'], $last_update);
69 logger('Delivery error when pushing to ' . $subscriber['callback_url'] . ' HTTP: ' . $ret);
71 PushSubscriber::delay($subscriber['id']);