]> git.mxchange.org Git - friendica.git/blob - src/Worker/PubSubPublish.php
38d9c07868a56b75edc592d22f3f55a3d55255ae
[friendica.git] / src / Worker / PubSubPublish.php
1 <?php
2 /**
3  * @file src/Worker/PubSubPublish.php
4  */
5
6 namespace Friendica\Worker;
7
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;
14
15 require_once 'include/items.php';
16
17 class PubSubPublish
18 {
19         public static function execute($pubsubpublish_id = 0)
20         {
21                 if ($pubsubpublish_id == 0) {
22                         return;
23                 }
24
25                 self::publish($pubsubpublish_id);
26         }
27
28         private static function publish($id)
29         {
30                 $a = BaseObject::getApp();
31
32                 $subscriber = DBA::selectFirst('push_subscriber', [], ['id' => $id]);
33                 if (!DBA::isResult($subscriber)) {
34                         return;
35                 }
36
37                 /// @todo Check server status with PortableContact::checkServer()
38                 // Before this can be done we need a way to safely detect the server url.
39
40                 logger("Generate feed of user " . $subscriber['nickname']. " to " . $subscriber['callback_url']. " - last updated " . $subscriber['last_update'], LOGGER_DEBUG);
41
42                 $last_update = $subscriber['last_update'];
43                 $params = OStatus::feed($subscriber['nickname'], $last_update);
44
45                 if (!$params) {
46                         return;
47                 }
48
49                 $hmac_sig = hash_hmac("sha1", $params, $subscriber['secret']);
50
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];
56
57                 logger('POST ' . print_r($headers, true) . "\n" . $params, LOGGER_DATA);
58
59                 $postResult = Network::post($subscriber['callback_url'], $params, $headers);
60                 $ret = $postResult->getReturnCode();
61
62                 $condition = ['id' => $subscriber['id']];
63
64                 if ($ret >= 200 && $ret <= 299) {
65                         logger('Successfully pushed to ' . $subscriber['callback_url']);
66
67                         PushSubscriber::reset($subscriber['id'], $last_update);
68                 } else {
69                         logger('Delivery error when pushing to ' . $subscriber['callback_url'] . ' HTTP: ' . $ret);
70
71                         PushSubscriber::delay($subscriber['id']);
72                 }
73         }
74 }